@@ -21,20 +21,35 @@ Stability : experimental
2121module Grenade.Core.Shape (
2222 S (..)
2323 , Shape (..)
24+#if MIN_VERSION_singletons(2,6,0)
2425 , SShape (..)
26+#else
27+ , Sing (..)
28+#endif
29+
2530 , randomOfShape
2631 , fromStorable
2732 ) where
2833
2934import Control.DeepSeq (NFData (..))
3035import Control.Monad.Random ( MonadRandom, getRandom )
36+
37+#if MIN_VERSION_base(4,13,0)
3138import Data.Kind (Type)
39+#endif
3240import Data.Proxy
3341import Data.Serialize
3442import Data.Singletons
43+import Data.Singletons.TypeLits
3544import Data.Vector.Storable ( Vector )
3645import qualified Data.Vector.Storable as V
46+
47+#if MIN_VERSION_base(4,11,0)
48+import GHC.TypeLits hiding (natVal)
49+#else
3750import GHC.TypeLits
51+#endif
52+
3853import qualified Numeric.LinearAlgebra.Static as H
3954import Numeric.LinearAlgebra.Static
4055import qualified Numeric.LinearAlgebra as NLA
@@ -84,9 +99,9 @@ deriving instance Show (S n)
8499type instance Sing = SShape
85100
86101data SShape :: Shape -> Type where
87− D1Sing :: KnownNat a => SShape ('D1 a)
88− D2Sing :: (KnownNat a, KnownNat b) => SShape ('D2 a b)
89− D3Sing :: (KnownNat (a * c), KnownNat a, KnownNat b, KnownNat c) => SShape ('D3 a b c)
102+ D1Sing :: Sing a -> SShape ('D1 a)
103+ D2Sing :: Sing a -> Sing b -> SShape ('D2 a b)
104+ D3Sing :: KnownNat (a * c) => Sing a -> Sing b -> Sing c -> SShape ('D3 a b c)
90105#else
91106data instance Sing (n :: Shape) where
92107 D1Sing :: Sing a -> Sing ('D1 a)
@@ -95,11 +110,11 @@ data instance Sing (n :: Shape) where
95110#endif
96111
97112instance KnownNat a => SingI ('D1 a) where
98− sing = D1Sing
113+ sing = D1Sing sing
99114instance (KnownNat a, KnownNat b) => SingI ('D2 a b) where
100− sing = D2Sing
115+ sing = D2Sing sing sing
101116instance (KnownNat a, KnownNat b, KnownNat c, KnownNat (a * c)) => SingI ('D3 a b c) where
102− sing = D3Sing
117+ sing = D3Sing sing sing sing
103118
104119instance SingI x => Num (S x) where
105120 (+) = n2 (+)
@@ -148,13 +163,13 @@ randomOfShape :: forall x m. ( MonadRandom m, SingI x ) => m (S x)
148163randomOfShape = do
149164 seed :: Int <- getRandom
150165 return $ case (sing :: Sing x) of
151− D1Sing ->
166+ D1Sing SNat ->
152167 S1D (randomVector seed Uniform * 2 - 1)
153168
154− D2Sing ->
169+ D2Sing SNat SNat ->
155170 S2D (uniformSample seed (-1) 1)
156171
157− D3Sing ->
172+ D3Sing SNat SNat SNat ->
158173 S3D (uniformSample seed (-1) 1)
159174
160175-- | Generate a shape from a Storable Vector.
@@ -162,13 +177,13 @@ randomOfShape = do
162177-- Returns Nothing if the vector is of the wrong size.
163178fromStorable :: forall x. SingI x => Vector Double -> Maybe (S x)
164179fromStorable xs = case sing :: Sing x of
165− D1Sing ->
180+ D1Sing SNat ->
166181 S1D <$> H.create xs
167182
168− D2Sing ->
183+ D2Sing SNat SNat ->
169184 S2D <$> mkL xs
170185
171− D3Sing ->
186+ D3Sing SNat SNat SNat ->
172187 S3D <$> mkL xs
173188 where
174189 mkL :: forall rows columns. (KnownNat rows, KnownNat columns)
@@ -205,13 +220,13 @@ n2 f (S2D x) (S2D y) = S2D (f x y)
205220n2 f (S3D x) (S3D y) = S3D (f x y)
206221
207222-- Helper function for creating the number instances
208−nk :: forall x. (SingI x) => Double -> S x
223+nk :: forall x. SingI x => Double -> S x
209224nk x = case (sing :: Sing x) of
210− D1Sing ->
225+ D1Sing SNat ->
211226 S1D (konst x)
212227
213− D2Sing ->
228+ D2Sing SNat SNat ->
214229 S2D (konst x)
215230
216− D3Sing ->
231+ D3Sing SNat SNat SNat ->
217232 S3D (konst x)