@@ -21,20 +21,35 @@ Stability : experimental
21module Grenade.Core.Shape (
21module Grenade.Core.Shape (
23 , Shape (..)
23 , Shape (..)
24+#if MIN_VERSION_singletons(2,6,0)
24 , SShape (..)
25 , SShape (..)
25 , randomOfShape
30 , randomOfShape
26 , fromStorable
31 , fromStorable
29import Control.DeepSeq (NFData (..))
34import Control.DeepSeq (NFData (..))
30import Control.Monad.Random ( MonadRandom, getRandom )
35import Control.Monad.Random ( MonadRandom, getRandom )
37+#if MIN_VERSION_base(4,13,0)
31import Data.Kind (Type)
38import Data.Kind (Type)
32import Data.Proxy
40import Data.Proxy
33import Data.Serialize
41import Data.Serialize
34import Data.Singletons
42import Data.Singletons
43+import Data.Singletons.TypeLits
35import Data.Vector.Storable ( Vector )
44import Data.Vector.Storable ( Vector )
36import qualified Data.Vector.Storable as V
45import qualified Data.Vector.Storable as V
47+#if MIN_VERSION_base(4,11,0)
48+import GHC.TypeLits hiding (natVal)
37import GHC.TypeLits
50import GHC.TypeLits
38import qualified Numeric.LinearAlgebra.Static as H
53import qualified Numeric.LinearAlgebra.Static as H
39import Numeric.LinearAlgebra.Static
54import Numeric.LinearAlgebra.Static
40import qualified Numeric.LinearAlgebra as NLA
55import qualified Numeric.LinearAlgebra as NLA
@@ -84,9 +99,9 @@ deriving instance Show (S n)
84type instance Sing = SShape
99type instance Sing = SShape
86data SShape :: Shape -> Type where
101data SShape :: Shape -> Type where
87− D1Sing :: KnownNat a => SShape ('D1 a)
102+ D1Sing :: Sing a -> SShape ('D1 a)
88− D2Sing :: (KnownNat a, KnownNat b) => SShape ('D2 a b)
103+ D2Sing :: Sing a -> Sing b -> SShape ('D2 a b)
89− D3Sing :: (KnownNat (a * c), KnownNat a, KnownNat b, KnownNat c) => SShape ('D3 a b c)
104+ D3Sing :: KnownNat (a * c) => Sing a -> Sing b -> Sing c -> SShape ('D3 a b c)
91data instance Sing (n :: Shape) where
106data instance Sing (n :: Shape) where
92 D1Sing :: Sing a -> Sing ('D1 a)
107 D1Sing :: Sing a -> Sing ('D1 a)
@@ -95,11 +110,11 @@ data instance Sing (n :: Shape) where
97instance KnownNat a => SingI ('D1 a) where
112instance KnownNat a => SingI ('D1 a) where
98− sing = D1Sing
113+ sing = D1Sing sing
99instance (KnownNat a, KnownNat b) => SingI ('D2 a b) where
114instance (KnownNat a, KnownNat b) => SingI ('D2 a b) where
100− sing = D2Sing
115+ sing = D2Sing sing sing
101instance (KnownNat a, KnownNat b, KnownNat c, KnownNat (a * c)) => SingI ('D3 a b c) where
116instance (KnownNat a, KnownNat b, KnownNat c, KnownNat (a * c)) => SingI ('D3 a b c) where
102− sing = D3Sing
117+ sing = D3Sing sing sing sing
104instance SingI x => Num (S x) where
119instance SingI x => Num (S x) where
105 (+) = n2 (+)
120 (+) = n2 (+)
@@ -148,13 +163,13 @@ randomOfShape :: forall x m. ( MonadRandom m, SingI x ) => m (S x)
148randomOfShape = do
163randomOfShape = do
149 seed :: Int <- getRandom
164 seed :: Int <- getRandom
150 return $ case (sing :: Sing x) of
165 return $ case (sing :: Sing x) of
151− D1Sing ->
166+ D1Sing SNat ->
152 S1D (randomVector seed Uniform * 2 - 1)
167 S1D (randomVector seed Uniform * 2 - 1)
154− D2Sing ->
169+ D2Sing SNat SNat ->
155 S2D (uniformSample seed (-1) 1)
170 S2D (uniformSample seed (-1) 1)
157− D3Sing ->
172+ D3Sing SNat SNat SNat ->
158 S3D (uniformSample seed (-1) 1)
173 S3D (uniformSample seed (-1) 1)
160-- | Generate a shape from a Storable Vector.
175-- | Generate a shape from a Storable Vector.
@@ -162,13 +177,13 @@ randomOfShape = do
162-- Returns Nothing if the vector is of the wrong size.
177-- Returns Nothing if the vector is of the wrong size.
163fromStorable :: forall x. SingI x => Vector Double -> Maybe (S x)
178fromStorable :: forall x. SingI x => Vector Double -> Maybe (S x)
164fromStorable xs = case sing :: Sing x of
179fromStorable xs = case sing :: Sing x of
165− D1Sing ->
180+ D1Sing SNat ->
166 S1D <$> H.create xs
181 S1D <$> H.create xs
168− D2Sing ->
183+ D2Sing SNat SNat ->
169 S2D <$> mkL xs
184 S2D <$> mkL xs
171− D3Sing ->
186+ D3Sing SNat SNat SNat ->
172 S3D <$> mkL xs
187 S3D <$> mkL xs
174 mkL :: forall rows columns. (KnownNat rows, KnownNat columns)
189 mkL :: forall rows columns. (KnownNat rows, KnownNat columns)
@@ -205,13 +220,13 @@ n2 f (S2D x) (S2D y) = S2D (f x y)
205n2 f (S3D x) (S3D y) = S3D (f x y)
220n2 f (S3D x) (S3D y) = S3D (f x y)
207-- Helper function for creating the number instances
222-- 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
209nk x = case (sing :: Sing x) of
224nk x = case (sing :: Sing x) of
210− D1Sing ->
225+ D1Sing SNat ->
211 S1D (konst x)
226 S1D (konst x)
213− D2Sing ->
228+ D2Sing SNat SNat ->
214 S2D (konst x)
229 S2D (konst x)
216− D3Sing ->
231+ D3Sing SNat SNat SNat ->
217 S3D (konst x)
232 S3D (konst x)