Skip to content

Instantly share code, notes, and snippets.

@haitlahcen
Created December 17, 2018 14:28
Show Gist options
  • Select an option

  • Save haitlahcen/f70fc8e4404fca3661eb85d4d3060913 to your computer and use it in GitHub Desktop.

Select an option

Save haitlahcen/f70fc8e4404fca3661eb85d4d3060913 to your computer and use it in GitHub Desktop.
dependent types
data Vect :: Nat -> * -> * where
Cons :: KnownNat n => a -> Vect (n - 1) a -> Vect n a
Nil :: (KnownNat n, 0 <= n) => Vect n a
k :: Vect 1 Int
k = Cons 1 Nil
u :: Vect 0 Int
u = Nil
j = vhead k
p = vtail k
vhead :: KnownNat n => Vect (n + 1) a -> a
vhead (Cons x _) = x
vtail :: (KnownNat n, n ~ (n + 1 - 1)) => Vect (n + 1) a -> Vect n a
vtail Nil = Nil
vtail (Cons _ xs) = xs
vzip :: Vect n a -> Vect n b -> Vect n (a, b)
vzip Nil Nil = Nil
vzip (Cons x xs) (Cons y ys) = Cons (x, y) $ vzip xs ys
vindex :: (KnownNat n, KnownNat i, i <= n - 1) => Proxy i -> Vect n a -> a
vindex (pi :: Proxy i) (Cons x xs) =
case natVal pi of
0 -> x
_ -> vindex (Proxy :: Proxy (i - 1)) xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment