Skip to content

Instantly share code, notes, and snippets.

@Tritlo
Last active December 3, 2016 23:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tritlo/ce5510e80935ac398a33934ee47c7930 to your computer and use it in GitHub Desktop.
Save Tritlo/ce5510e80935ac398a33934ee47c7930 to your computer and use it in GitHub Desktop.
{-# LANGUAGE TypeFamilies, GADTs, TypeOperators, DataKinds, PolyKinds #-}
-- We can also just add TypeInType, and skip DataKinds and KindSignatures,
-- since TypeInType enables both of those, PolyKinds (and more!)
module Main where
import Data.Kind (Type)
-- Define a Type for the natural numbers, Zero and a successor
data Nat = Z | S Nat
class Addable k where
type (a :: k) + (b :: k) :: k
instance Addable Nat where
type (Z + a) = a
type (S a + b) = S (a + b)
infixr 5 :.
data Vect :: Nat -> Type -> Type where
Nil :: Vect Z a
(:.) :: a -> Vect n a -> Vect (S n) a
app :: Vect n a -> Vect m a -> Vect (n + m) a
app Nil a = a
app (a :. as) bs = a :. (app as bs)
vhead :: Vect (S k) a -> a
vhead (a :. v) = a
ex1 :: Vect (S (S Z)) Int
ex1 = 1 :. 2 :. Nil
ex2 :: Vect (S Z) Int
ex2 = 1 :. Nil
ex3 :: Vect (S (S (S Z))) Int
ex3 = ex1 `app` ex2
empty :: Vect Z ()
empty = Nil
main :: IO ()
main = print $ vhead ex3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment