I am trying to have precomputed data embedded in Haskell. That is
catToMap li = Map.fromList $ zip [0..] li
cat1 = catToMap ["aa", "bb", "cc"]
dim = Map.size cat1
I would like to use dim
statically in a type definition:
type Network = Grenade.Network
'[Grenade.FullyConnected dim 20, Grenade.FullyConnected 20 1, Grenade.Logit]
'[Grenade.D1 dim, Grenade.D1 20, Grenade.D1 1, Grenade.D1 1]
(imported from grenade
library)
However, the above gives the error that dim
is not in scope.
I am also trying to create the function
import qualified Numeric.LinearAlgebra.Static as SA
-- | ith standard basis in Rn
stdbasis :: forall n . KnownNat n => Int -> SA.R n
stdbasis i = SA.vector [builder x| x <- [0..n-1]]
where
builder j = if i == j then 1 else 0
but this gives me the error that n
is not in scope.
My attempt to fix the first problem is with template Haskell:
catToMap = $(\li -> Map.fromList $ zip [0..] li)
cat1 = $(catToMap ["aa", "bb", "cc"])
dim = $(Map.size cat1)
but it gives me the error
• Couldn't match expected type ‘Q Exp’
with actual type ‘[a0] -> Map.Map Integer a0’
• The lambda expression ‘\ li -> (Map.fromList $ zip ... li)’
has one argument,
but its type ‘Language.Haskell.TH.Lib.ExpQ’ has none
In the expression: \ li -> (Map.fromList $ zip [0 .. ] li)
In the untyped splice: $(\ li -> (Map.fromList $ zip [0 .. ] li))
What I am trying to achieve is similar to C++ templates:
template <int size> Vector<n>
stdbasis(int i);