diff options
author | Sidharth Kapur <sidharthkapur1@gmail.com> | 2016-03-13 17:25:23 -0500 |
---|---|---|
committer | Sidharth Kapur <sidharthkapur1@gmail.com> | 2016-03-13 17:25:23 -0500 |
commit | 91e9c879bfa3b509cd737ba31580cbba0c0bf340 (patch) | |
tree | ea2243a1bd1e8618cf4b2c3e4c8a3e692e151f01 | |
parent | 3824df2f2f17c8395832b88b27d61fdc22553f2e (diff) |
Add dimension check in Dim binary instance
-rw-r--r-- | packages/base/src/Internal/Static.hs | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/packages/base/src/Internal/Static.hs b/packages/base/src/Internal/Static.hs index 058b9d0..a0af085 100644 --- a/packages/base/src/Internal/Static.hs +++ b/packages/base/src/Internal/Static.hs | |||
@@ -35,8 +35,10 @@ import Control.DeepSeq | |||
35 | import Data.Proxy(Proxy) | 35 | import Data.Proxy(Proxy) |
36 | import Foreign.Storable(Storable) | 36 | import Foreign.Storable(Storable) |
37 | import Text.Printf | 37 | import Text.Printf |
38 | |||
38 | import Data.Binary | 39 | import Data.Binary |
39 | import GHC.Generics (Generic) | 40 | import GHC.Generics (Generic) |
41 | import Data.Proxy (Proxy(..)) | ||
40 | 42 | ||
41 | -------------------------------------------------------------------------------- | 43 | -------------------------------------------------------------------------------- |
42 | 44 | ||
@@ -51,7 +53,17 @@ instance Binary a => Binary (Complex a) | |||
51 | put (r :+ i) = put (r, i) | 53 | put (r :+ i) = put (r, i) |
52 | get = (\(r,i) -> r :+ i) <$> get | 54 | get = (\(r,i) -> r :+ i) <$> get |
53 | 55 | ||
54 | instance (Binary a) => Binary (Dim n a) | 56 | instance (KnownNat n, Binary a) => Binary (Dim n a) where |
57 | get = do | ||
58 | k <- get | ||
59 | let n = natVal (Proxy :: Proxy n) | ||
60 | if n == k | ||
61 | then Dim <$> get | ||
62 | else fail ("Expected dimension " ++ (show n) ++ ", but found dimension " ++ (show k)) | ||
63 | |||
64 | put (Dim x) = do | ||
65 | put (natVal (Proxy :: Proxy n)) | ||
66 | put x | ||
55 | 67 | ||
56 | lift1F | 68 | lift1F |
57 | :: (c t -> c t) | 69 | :: (c t -> c t) |