@@ -21,35 +21,20 @@ Stability : experimental
2121module Grenade.Core.Shape (
2222 S (..)
2323 , Shape (..)
24−#if MIN_VERSION_singletons(2,6,0)
2524 , SShape (..)
26−#else
27− , Sing (..)
28−#endif
29−
3025 , randomOfShape
3126 , fromStorable
3227 ) where
3328
3429import Control.DeepSeq (NFData (..))
3530import Control.Monad.Random ( MonadRandom, getRandom )
36−
37−#if MIN_VERSION_base(4,13,0)
3831import Data.Kind (Type)
39−#endif
4032import Data.Proxy
4133import Data.Serialize
4234import Data.Singletons
43−import Data.Singletons.TypeLits
4435import Data.Vector.Storable ( Vector )
4536import qualified Data.Vector.Storable as V
46−
47−#if MIN_VERSION_base(4,11,0)
48−import GHC.TypeLits hiding (natVal)
49−#else
5037import GHC.TypeLits
51−#endif
52−
5338import qualified Numeric.LinearAlgebra.Static as H
5439import Numeric.LinearAlgebra.Static
5540import qualified Numeric.LinearAlgebra as NLA
@@ -99,9 +84,9 @@ deriving instance Show (S n)
9984type instance Sing = SShape
10085
10186data SShape :: Shape -> Type where
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)
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)
10590#else
10691data instance Sing (n :: Shape) where
10792 D1Sing :: Sing a -> Sing ('D1 a)
@@ -110,11 +95,11 @@ data instance Sing (n :: Shape) where
11095#endif
11196
11297instance KnownNat a => SingI ('D1 a) where
113− sing = D1Sing sing
98+ sing = D1Sing
11499instance (KnownNat a, KnownNat b) => SingI ('D2 a b) where
115− sing = D2Sing sing sing
100+ sing = D2Sing
116101instance (KnownNat a, KnownNat b, KnownNat c, KnownNat (a * c)) => SingI ('D3 a b c) where
117− sing = D3Sing sing sing sing
102+ sing = D3Sing
118103
119104instance SingI x => Num (S x) where
120105 (+) = n2 (+)
@@ -163,13 +148,13 @@ randomOfShape :: forall x m. ( MonadRandom m, SingI x ) => m (S x)
163148randomOfShape = do
164149 seed :: Int <- getRandom
165150 return $ case (sing :: Sing x) of
166− D1Sing SNat ->
151+ D1Sing ->
167152 S1D (randomVector seed Uniform * 2 - 1)
168153
169− D2Sing SNat SNat ->
154+ D2Sing ->
170155 S2D (uniformSample seed (-1) 1)
171156
172− D3Sing SNat SNat SNat ->
157+ D3Sing ->
173158 S3D (uniformSample seed (-1) 1)
174159
175160-- | Generate a shape from a Storable Vector.
@@ -177,13 +162,13 @@ randomOfShape = do
177162-- Returns Nothing if the vector is of the wrong size.
178163fromStorable :: forall x. SingI x => Vector Double -> Maybe (S x)
179164fromStorable xs = case sing :: Sing x of
180− D1Sing SNat ->
165+ D1Sing ->
181166 S1D <$> H.create xs
182167
183− D2Sing SNat SNat ->
168+ D2Sing ->
184169 S2D <$> mkL xs
185170
186− D3Sing SNat SNat SNat ->
171+ D3Sing ->
187172 S3D <$> mkL xs
188173 where
189174 mkL :: forall rows columns. (KnownNat rows, KnownNat columns)
@@ -220,13 +205,13 @@ n2 f (S2D x) (S2D y) = S2D (f x y)
220205n2 f (S3D x) (S3D y) = S3D (f x y)
221206
222207-- 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
224209nk x = case (sing :: Sing x) of
225− D1Sing SNat ->
210+ D1Sing ->
226211 S1D (konst x)
227212
228− D2Sing SNat SNat ->
213+ D2Sing ->
229214 S2D (konst x)
230215
231− D3Sing SNat SNat SNat ->
216+ D3Sing ->
232217 S3D (konst x)