While learning about singletons and (semi) dependent typing, I started trying to make a sized list from a normal Haskell list with sizes given by Nats such as 0, 1, 2, 3,... instead of Z, S Z, S (S Z), etc.. I'm using the Data.Singletons.TypeLits library btw. (singletons-2.5.1).
This is the way I've been trying to do it:
data NatList (a :: *) (n :: Nat) where
Nah :: NatList a 0
Yea :: a -> NatList a n -> NatList a (n + 1)
fromList :: SNat n -> [a] -> NatList a n
fromList s [] = Nah
fromList s (x:xs) = Yea x (fromList (s %- (SNat :: SNat 1)) xs)
This code doesn't compile and gives me an error
"Couldn't match type ‘n’ with ‘0’ ‘n’ is a rigid type variable bound by the type signature for: fromList :: forall (n :: Nat) a. SNat n -> [a] -> NatList a n
Maybe
or something like that in the result type. This function claims to make a list of a fixed size, known at a type level, out of a list of an unknown size. There is no type level information about the size of the[a]
list. What should happen if you call like thisfromList (sing :: Sing 3) [1]
? – David Youngdata Nat = Z | S Nat
. – user2407038