-
-
Save Tritlo/ce5510e80935ac398a33934ee47c7930 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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