File: From.hs

package info (click to toggle)
haskell-basement 0.0.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: haskell: 11,336; ansic: 63; makefile: 5
file content (307 lines) | stat: -rw-r--r-- 11,317 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE ConstraintKinds       #-}
{-# LANGUAGE CPP                   #-}
{-# LANGUAGE MagicHash             #-}
{-# LANGUAGE UndecidableInstances  #-}
{-# LANGUAGE TypeOperators         #-}
-- |
-- Module      : Basement.From
-- License     : BSD-style
-- Maintainer  : Haskell Foundation
--
-- Flexible Type convertion
--
-- From is multi parameter type class that allow converting
-- from a to b.
--
-- Only type that are valid to convert to another type
-- should be From instance; otherwise TryFrom should be used.
--
-- Into (resp TryInto) allows the contrary instances to be able
-- to specify the destination type before the source. This is
-- practical with TypeApplication
module Basement.From
    ( From(..)
    , Into
    , TryFrom(..)
    , TryInto
    , into
    , tryInto
    ) where

import           Basement.Compat.Base

-- basic instances
import           GHC.Types
import           GHC.Prim hiding (word64ToWord#)
import qualified GHC.Prim
import           GHC.Int
import           GHC.Word
import           Basement.Numerical.Number
import           Basement.Numerical.Conversion
import qualified Basement.Block as Block
import qualified Basement.BoxedArray as BoxArray
import           Basement.Cast (cast)
import qualified Basement.UArray as UArray
import qualified Basement.String as String
import qualified Basement.Types.AsciiString as AsciiString
import           Basement.Types.Word128 (Word128(..))
import           Basement.Types.Word256 (Word256(..))
import qualified Basement.Types.Word128 as Word128
import qualified Basement.Types.Word256 as Word256
import           Basement.These
import           Basement.PrimType (PrimType, PrimSize)
import           Basement.Types.OffsetSize
import           Basement.Compat.Natural
import           Basement.Compat.Primitive
import qualified Prelude (fromIntegral)

-- nat instances
#if __GLASGOW_HASKELL__ >= 800
import           Basement.Nat
import qualified Basement.Sized.Block as BlockN
import           Basement.Bounded
#endif

-- | Class of things that can be converted from a to b.
--
-- In a valid instance, the source should be always representable by the destination,
-- otherwise the instance should be using 'TryFrom'
class From a b where
    from :: a -> b

type Into b a = From a b

-- | Same as from but reverse the type variable so that the destination type can be specified first
--
-- e.g. converting:
--
-- from @_ @Word (10 :: Int)
--
-- into @Word (10 :: Int)
--
into :: Into b a => a -> b
into = from

-- | Class of things that can mostly be converted from a to b, but with possible error cases.
class TryFrom a b where
    tryFrom :: a -> Maybe b

type TryInto b a = TryFrom a b

-- | same as tryFrom but reversed
tryInto :: TryInto b a => a -> Maybe b
tryInto = tryFrom

instance From a a where
    from = id

instance IsNatural n => From n Natural where
    from = toNatural
instance IsIntegral n => From n Integer where
    from = toInteger

instance From Int8 Int16 where
    from (I8# i) = I16# (int8ToInt16# i)
instance From Int8 Int32 where
    from (I8# i) = I32# (int8ToInt32# i)
instance From Int8 Int64 where
    from (I8# i) = intToInt64 (I# (int8ToInt# i))
instance From Int8 Int where
    from (I8# i) = I# (int8ToInt# i)

instance From Int16 Int32 where
    from (I16# i) = I32# (int16ToInt32# i)
instance From Int16 Int64 where
    from (I16# i) = intToInt64 (I# (int16ToInt# i))
instance From Int16 Int where
    from (I16# i) = I# (int16ToInt# i)

instance From Int32 Int64 where
    from (I32# i) = intToInt64 (I# (int32ToInt# i))
instance From Int32 Int where
    from (I32# i) = I# (int32ToInt# i)

instance From Int Int64 where
    from = intToInt64

instance From Word8 Word16 where
    from (W8# i) = W16# (word8ToWord16# i)
instance From Word8 Word32 where
    from (W8# i) = W32# (word8ToWord32# i)
instance From Word8 Word64 where
    from (W8# i) = wordToWord64 (W# (word8ToWord# i))
instance From Word8 Word128 where
    from (W8# i) = Word128 0 (wordToWord64 $ W# (word8ToWord# i))
instance From Word8 Word256 where
    from (W8# i) = Word256 0 0 0 (wordToWord64 $ W# (word8ToWord# i))
instance From Word8 Word where
    from (W8# i) = W# (word8ToWord# i)
instance From Word8 Int16 where
    from (W8# w) = I16# (intToInt16# (word2Int# (word8ToWord# w)))
instance From Word8 Int32 where
    from (W8# w) = I32# (intToInt32# (word2Int# (word8ToWord# w)))
instance From Word8 Int64 where
    from (W8# w) = intToInt64 (I# (word2Int# (word8ToWord# w)))
instance From Word8 Int where
    from (W8# w) = I# (word2Int# (word8ToWord# w))

instance From Word16 Word32 where
    from (W16# i) = W32# (word16ToWord32# i)
instance From Word16 Word64 where
    from (W16# i) = wordToWord64 (W# (word16ToWord# i))
instance From Word16 Word128 where
    from (W16# i) = Word128 0 (wordToWord64 $ W# (word16ToWord# i))
instance From Word16 Word256 where
    from (W16# i) = Word256 0 0 0 (wordToWord64 $ W# (word16ToWord# i))
instance From Word16 Word where
    from (W16# i) = W# (word16ToWord# i)
instance From Word16 Int32 where
    from (W16# w) = I32# (intToInt32# (word2Int# (word16ToWord# w)))
instance From Word16 Int64 where
    from (W16# w) = intToInt64 (I# (word2Int# (word16ToWord# w)))
instance From Word16 Int where
    from (W16# w) = I# (word2Int# (word16ToWord# w))

instance From Word32 Word64 where
    from (W32# i) = wordToWord64 (W# (word32ToWord# i))
instance From Word32 Word128 where
    from (W32# i) = Word128 0 (wordToWord64 $ W# (word32ToWord# i))
instance From Word32 Word256 where
    from (W32# i) = Word256 0 0 0 (wordToWord64 $ W# (word32ToWord# i))
instance From Word32 Word where
    from (W32# i) = W# (word32ToWord# i)
instance From Word32 Int64 where
    from (W32# w) = intToInt64 (I# (word2Int# (word32ToWord# w)))
instance From Word32 Int where
    from (W32# w) = I# (word2Int# (word32ToWord# w))

instance From Word64 Word128 where
    from w = Word128 0 w
instance From Word64 Word256 where
    from w = Word256 0 0 0 w

instance From Word Word64 where
    from = wordToWord64

-- Simple prelude types
instance From (Maybe a) (Either () a) where
    from (Just x) = Right x
    from Nothing  = Left ()

-- basic basement types
instance From (CountOf ty) Int where
    from (CountOf n) = n
instance From (CountOf ty) Word where
    -- here it is ok to cast the underlying `Int` held by `CountOf` to a `Word`
    -- as the `Int` should never hold a negative value.
    from (CountOf n) = cast n
instance From Word (Offset ty) where
    from w = Offset (cast w)
instance TryFrom Int (Offset ty) where
    tryFrom i
        | i < 0     = Nothing
        | otherwise = Just (Offset i)
instance TryFrom Int (CountOf ty) where
    tryFrom i
        | i < 0     = Nothing
        | otherwise = Just (CountOf i)
instance From Word (CountOf ty) where
    from w = CountOf (cast w)

instance From (Either a b) (These a b) where
    from (Left a) = This a
    from (Right b) = That b

instance From Word128 Word256 where
    from (Word128 a b) = Word256 0 0 a b

-- basement instances

-- uarrays
instance PrimType ty => From (Block.Block ty) (UArray.UArray ty) where
    from = UArray.fromBlock
instance PrimType ty => From (BoxArray.Array ty) (UArray.UArray ty) where
    from = BoxArray.mapToUnboxed id

-- blocks
instance PrimType ty => From (UArray.UArray ty) (Block.Block ty) where
    from = UArray.toBlock
instance PrimType ty => From (BoxArray.Array ty) (Block.Block ty) where
    from = UArray.toBlock . BoxArray.mapToUnboxed id

-- boxed array
instance PrimType ty => From (UArray.UArray ty) (BoxArray.Array ty) where
    from = BoxArray.mapFromUnboxed id


instance From String.String (UArray.UArray Word8) where
    from = String.toBytes String.UTF8

instance From AsciiString.AsciiString String.String where
    from = String.fromBytesUnsafe . UArray.unsafeRecast . AsciiString.toBytes
instance From AsciiString.AsciiString (UArray.UArray Word8) where
    from = UArray.unsafeRecast . AsciiString.toBytes

instance TryFrom (UArray.UArray Word8) String.String where
    tryFrom arr = case String.fromBytes String.UTF8 arr of
                    (s, Nothing, _) -> Just s
                    (_, Just _, _)  -> Nothing

#if __GLASGOW_HASKELL__ >= 800
instance From (BlockN.BlockN n ty) (Block.Block ty) where
    from = BlockN.toBlock
instance (PrimType a, PrimType b, KnownNat n, KnownNat m, ((PrimSize b) Basement.Nat.* m) ~ ((PrimSize a) Basement.Nat.* n))
      => From (BlockN.BlockN n a) (BlockN.BlockN m b) where
    from = BlockN.cast
instance (NatWithinBound Int n, PrimType ty) => From (BlockN.BlockN n ty) (UArray.UArray ty) where
    from = UArray.fromBlock . BlockN.toBlock
instance (NatWithinBound Int n, PrimType ty) => From (BlockN.BlockN n ty) (BoxArray.Array ty) where
    from = BoxArray.mapFromUnboxed id . UArray.fromBlock . BlockN.toBlock

instance (NatWithinBound (CountOf ty) n, KnownNat n, PrimType ty)
      => TryFrom (Block.Block ty) (BlockN.BlockN n ty) where
    tryFrom = BlockN.toBlockN
instance (NatWithinBound (CountOf ty) n, KnownNat n, PrimType ty)
      => TryFrom (UArray.UArray ty) (BlockN.BlockN n ty) where
    tryFrom = BlockN.toBlockN . UArray.toBlock
instance (NatWithinBound (CountOf ty) n, KnownNat n, PrimType ty)
      => TryFrom (BoxArray.Array ty) (BlockN.BlockN n ty) where
    tryFrom = BlockN.toBlockN . UArray.toBlock . BoxArray.mapToUnboxed id

instance (KnownNat n, NatWithinBound Word8 n) => From (Zn64 n) Word8 where
    from = narrow . unZn64 where narrow (W64# w) = W8# (wordToWord8# (word64ToWord# w))
instance (KnownNat n, NatWithinBound Word16 n) => From (Zn64 n) Word16 where
    from = narrow . unZn64 where narrow (W64# w) = W16# (wordToWord16# (word64ToWord# w))
instance (KnownNat n, NatWithinBound Word32 n) => From (Zn64 n) Word32 where
    from = narrow . unZn64 where narrow (W64# w) = W32# (wordToWord32# (word64ToWord# w))
instance From (Zn64 n) Word64 where
    from = unZn64
instance From (Zn64 n) Word128 where
    from = from . unZn64
instance From (Zn64 n) Word256 where
    from = from . unZn64

instance (KnownNat n, NatWithinBound Word8 n) => From (Zn n) Word8 where
    from = narrow . naturalToWord64 . unZn where narrow (W64# w) = W8# (wordToWord8# (word64ToWord# w))
instance (KnownNat n, NatWithinBound Word16 n) => From (Zn n) Word16 where
    from = narrow . naturalToWord64 . unZn where narrow (W64# w) = W16# (wordToWord16# (word64ToWord# w))
instance (KnownNat n, NatWithinBound Word32 n) => From (Zn n) Word32 where
    from = narrow . naturalToWord64 . unZn where narrow (W64# w) = W32# (wordToWord32# (word64ToWord# w))
instance (KnownNat n, NatWithinBound Word64 n) => From (Zn n) Word64 where
    from = naturalToWord64 . unZn
instance (KnownNat n, NatWithinBound Word128 n) => From (Zn n) Word128 where
    from = Word128.fromNatural . unZn
instance (KnownNat n, NatWithinBound Word256 n) => From (Zn n) Word256 where
    from = Word256.fromNatural . unZn

instance (KnownNat n, NatWithinBound Word64 n) => From (Zn n) (Zn64 n) where
    from = zn64 . naturalToWord64 . unZn
instance KnownNat n => From (Zn64 n) (Zn n) where
    from = zn . from . unZn64

naturalToWord64 :: Natural -> Word64
naturalToWord64 = Prelude.fromIntegral
#endif