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