|
| 1 | +{-# LANGUAGE FlexibleContexts #-} |
| 2 | +{-# LANGUAGE FlexibleInstances #-} |
| 3 | +{-# LANGUAGE GADTs #-} |
| 4 | +{-# LANGUAGE MultiParamTypeClasses #-} |
| 5 | +{-# LANGUAGE UndecidableInstances #-} |
| 6 | + |
| 7 | +{-# OPTIONS_HADDOCK hide #-} |
| 8 | + |
| 9 | +{-# OPTIONS_GHC -fplugin=GHC.TypeLits.KnownNat.Solver #-} |
| 10 | + |
| 11 | +module Clash.Class.Convert.Internal.Convert where |
| 12 | + |
| 13 | +import Prelude |
| 14 | + |
| 15 | +import Clash.Class.BitPack |
| 16 | +import Clash.Class.Resize |
| 17 | +import Clash.Sized.BitVector |
| 18 | +import Clash.Sized.Index |
| 19 | +import Clash.Sized.Signed |
| 20 | +import Clash.Sized.Unsigned |
| 21 | + |
| 22 | +import GHC.TypeLits.Extra (CLog) |
| 23 | +import GHC.TypeLits (KnownNat, type (<=), type (+), type (^)) |
| 24 | + |
| 25 | +import Data.Int (Int16, Int32, Int64, Int8) |
| 26 | +import Data.Word (Word16, Word32, Word64, Word8) |
| 27 | + |
| 28 | +{- $setup |
| 29 | +>>> import Clash.Prelude |
| 30 | +>>> import Clash.Class.Convert |
| 31 | +-} |
| 32 | + |
| 33 | +{- | Conversions that are, based on their types, guaranteed to succeed. |
| 34 | +
|
| 35 | +== __Laws__ |
| 36 | +A conversion is safe and total if a round trip conversion is guaranteed to be |
| 37 | +lossless. I.e., |
| 38 | +
|
| 39 | +> Just x == maybeConvert (convert @a @b x) |
| 40 | +
|
| 41 | +for all values @x@ of type @a@. It should also preserve the numerical value |
| 42 | +interpretation of the bits. For types that have an "Integral" instance, this |
| 43 | +intuition is captured by: |
| 44 | +
|
| 45 | +> toInteger x == toInteger (convert @a @b x) |
| 46 | +
|
| 47 | +Instances should make sure their constraints are as \"tight\" as possible. I.e., |
| 48 | +if an instance exist, but the constraints cannot be satisfied, then |
| 49 | +'Clash.Class.Convert.convertMaybe' should return 'Nothing' for one or more values in |
| 50 | +the domain of the source type @a@: |
| 51 | +
|
| 52 | +> L.any isNothing (L.map (maybeConvert @a @b) [minBound ..]) |
| 53 | +
|
| 54 | +Additionally, any implementation should be translatable to synthesizable RTL. |
| 55 | +-} |
| 56 | +class Convert a b where |
| 57 | + {- | Convert a supplied value of type @a@ to a value of type @b@. The conversion |
| 58 | + is guaranteed to succeed. |
| 59 | +
|
| 60 | + >>> convert (3 :: Index 8) :: Unsigned 8 |
| 61 | + 3 |
| 62 | +
|
| 63 | + The following will fail with a type error, as we cannot prove that all values |
| 64 | + of @Index 8@ can be represented by an @Unsigned 2@: |
| 65 | +
|
| 66 | + >>> convert (3 :: Index 8) :: Unsigned 2 |
| 67 | + ... |
| 68 | +
|
| 69 | + For the time being, if the input is an @XException@, then the output is too. This |
| 70 | + property might be relaxed in the future. |
| 71 | + -} |
| 72 | + convert :: a -> b |
| 73 | + |
| 74 | +instance (KnownNat n, KnownNat m, n <= m) => Convert (Index n) (Index m) where |
| 75 | + convert = resize |
| 76 | + |
| 77 | +instance (KnownNat n, KnownNat m, 1 <= n, n <= 2 ^ m) => Convert (Index n) (Unsigned m) where |
| 78 | + convert !a = resize $ bitCoerce a |
| 79 | + |
| 80 | +{- | Note: Conversion from @Index 1@ to @Signed 0@ is total, but not within the |
| 81 | +constraints of the instance. |
| 82 | +-} |
| 83 | +instance (KnownNat n, KnownNat m, 1 <= n, CLog 2 n + 1 <= m) => Convert (Index n) (Signed m) where |
| 84 | + convert !a = convert $ bitCoerce @_ @(Unsigned (CLog 2 n)) a |
| 85 | + |
| 86 | +instance (KnownNat n, KnownNat m, 1 <= n, n <= 2 ^ m) => Convert (Index n) (BitVector m) where |
| 87 | + convert !a = resize $ pack a |
| 88 | + |
| 89 | +instance (KnownNat n, KnownNat m, 1 <= m, 2 ^ n <= m) => Convert (Unsigned n) (Index m) where |
| 90 | + convert !a = bitCoerce $ resize a |
| 91 | + |
| 92 | +instance (KnownNat n, KnownNat m, n <= m) => Convert (Unsigned n) (Unsigned m) where |
| 93 | + convert = resize |
| 94 | + |
| 95 | +{- | Note: Conversion from @Unsigned 0@ to @Signed 0@ is total, but not within the |
| 96 | +constraints of the instance. |
| 97 | +-} |
| 98 | +instance (KnownNat n, KnownNat m, n + 1 <= m) => Convert (Unsigned n) (Signed m) where |
| 99 | + convert = bitCoerce . resize |
| 100 | + |
| 101 | +instance (KnownNat n, KnownNat m, n <= m) => Convert (Unsigned n) (BitVector m) where |
| 102 | + convert !a = resize $ pack a |
| 103 | + |
| 104 | +instance (KnownNat n, KnownNat m, n <= m) => Convert (Signed n) (Signed m) where |
| 105 | + convert !a = resize a |
| 106 | + |
| 107 | +instance (KnownNat n, KnownNat m, 1 <= m, 2 ^ n <= m) => Convert (BitVector n) (Index m) where |
| 108 | + convert = unpack . resize |
| 109 | + |
| 110 | +instance (KnownNat n, KnownNat m, n <= m) => Convert (BitVector n) (Unsigned m) where |
| 111 | + convert = unpack . resize |
| 112 | + |
| 113 | +{- | Note: Conversion from @BitVector 0@ to @Signed 0@ is total, but not within the |
| 114 | +constraints of the instance. |
| 115 | +-} |
| 116 | +instance (KnownNat n, KnownNat m, n + 1 <= m) => Convert (BitVector n) (Signed m) where |
| 117 | + convert = unpack . resize |
| 118 | + |
| 119 | +instance (KnownNat n, KnownNat m, n <= m) => Convert (BitVector n) (BitVector m) where |
| 120 | + convert = resize |
| 121 | + |
| 122 | +instance (Convert (Unsigned 64) a) => Convert Word a where |
| 123 | + convert = convert . bitCoerce @_ @(Unsigned 64) |
| 124 | +instance (Convert (Unsigned 64) a) => Convert Word64 a where |
| 125 | + convert = convert . bitCoerce @_ @(Unsigned 64) |
| 126 | +instance (Convert (Unsigned 32) a) => Convert Word32 a where |
| 127 | + convert = convert . bitCoerce @_ @(Unsigned 32) |
| 128 | +instance (Convert (Unsigned 16) a) => Convert Word16 a where |
| 129 | + convert = convert . bitCoerce @_ @(Unsigned 16) |
| 130 | +instance (Convert (Unsigned 8) a) => Convert Word8 a where |
| 131 | + convert = convert . bitCoerce @_ @(Unsigned 8) |
| 132 | + |
| 133 | +instance (Convert (Signed 64) a) => Convert Int a where |
| 134 | + convert = convert . bitCoerce @_ @(Signed 64) |
| 135 | +instance (Convert (Signed 64) a) => Convert Int64 a where |
| 136 | + convert = convert . bitCoerce @_ @(Signed 64) |
| 137 | +instance (Convert (Signed 32) a) => Convert Int32 a where |
| 138 | + convert = convert . bitCoerce @_ @(Signed 32) |
| 139 | +instance (Convert (Signed 16) a) => Convert Int16 a where |
| 140 | + convert = convert . bitCoerce @_ @(Signed 16) |
| 141 | +instance (Convert (Signed 8) a) => Convert Int8 a where |
| 142 | + convert = convert . bitCoerce @_ @(Signed 8) |
| 143 | + |
| 144 | +instance (Convert a (Unsigned 64)) => Convert a Word where |
| 145 | + convert = bitCoerce @(Unsigned 64) . convert |
| 146 | +instance (Convert a (Unsigned 64)) => Convert a Word64 where |
| 147 | + convert = bitCoerce @(Unsigned 64) . convert |
| 148 | +instance (Convert a (Unsigned 32)) => Convert a Word32 where |
| 149 | + convert = bitCoerce @(Unsigned 32) . convert |
| 150 | +instance (Convert a (Unsigned 16)) => Convert a Word16 where |
| 151 | + convert = bitCoerce @(Unsigned 16) . convert |
| 152 | +instance (Convert a (Unsigned 8)) => Convert a Word8 where |
| 153 | + convert = bitCoerce @(Unsigned 8) . convert |
| 154 | + |
| 155 | +instance (Convert a (Signed 64)) => Convert a Int where |
| 156 | + convert = bitCoerce @(Signed 64) . convert |
| 157 | +instance (Convert a (Signed 64)) => Convert a Int64 where |
| 158 | + convert = bitCoerce @(Signed 64) . convert |
| 159 | +instance (Convert a (Signed 32)) => Convert a Int32 where |
| 160 | + convert = bitCoerce @(Signed 32) . convert |
| 161 | +instance (Convert a (Signed 16)) => Convert a Int16 where |
| 162 | + convert = bitCoerce @(Signed 16) . convert |
| 163 | +instance (Convert a (Signed 8)) => Convert a Int8 where |
| 164 | + convert = bitCoerce @(Signed 8) . convert |
0 commit comments