xxxxxxxxxx111-- explicit2append: (elem: Type) -> (n: Nat) -> (m: Nat) ->3 Vect n elem -> Vect m elem -> Vect (n + m) elem4-- > append Char 2 2 ['a', 'b'] ['c', 'd']5-- > append _ _ _ ['a', 'b'] ['c', 'd']67-- implicit8append: Vect n elem -> Vect m elem -> Vect (n + m) elem -- unbound9append: {elem: Type} -> {n: Nat} -> {m: Nat} ->10 Vect n elem -> Vect m elem -> Vect (n + m) elem -- bound11-- > append ['a', 'b'] ['c', 'd']
xxxxxxxxxx131-- plain2length: Vect n elem -> Nat3length [] = Z4length (x :: xs) = 1 + length xs56-- dependent7length: Vect n elem -> Nat8length {n} xs = n910-- dependent with pattern matching11createEmpties: Vect n (Vect 0 a)12createEmpties {n = Z} = []13createEmpties {n = (S k)} = [] :: createEmpties
xxxxxxxxxx201-- enumerated types (enum)2data Bool = False | True3data Direction = North | East | South | West45-- union types (parameterized enum)6data Shape = Triangle Double Double7 | Rectangle Double Double8 | Circle Double9data Shape: Type where10 Triangle: Double -> Double -> Shape11 Rectangle: Double -> Double -> Shape12 Circle: Double -> Shape13 14-- recursive types (self-defining types)15data Nat = Z | S Nat16data Infinite = Forever Infinite1718-- generic types (type-parameterized)19data Maybe valtype = Nothing | Just valtype20data Either a b = Left a | Right b
xxxxxxxxxx51insert x orig@(Node left val right)2 = case compare x val of3 LT => Node (insert x left) val right4 EQ => orig5 GT => Node left val (insert x right)
xxxxxxxxxx151data PowerSouce = Petrol | Pedal23data Vehicle: PowerSource -> Type where -- dependent types / families of types4 Bicycle: Vehicle Pedal5 Car: (fuel: Nat) -> Vehicle Petrol6 Bus: (fuel: Nat) -> Vehicle Petrol7 8data Vect: Nat -> Type -> Type where9 Nil: Vect Z a10 (::): (x: a) -> (xs: Vect k a) -> Vect (S k) a11 12data DataStore: Type where13 MkData: (size: Nat) ->14 (items: Vect size String) -> -- computation from parameters15 DataStore
xxxxxxxxxx21anyVect: (n: Nat ** Vect n String) -- second element computed from the first2anyVect: (n ** Vect n String) -- type inference
x
1 -- type calculation functions2StringOrInt: Bool -> Type3StringOrInt False = String4StringOrInt True = Int5-- usage6getStringOrInt: (isInt: Bool) -> StringOrInt isInt7getStringOrInt False = "Ninety Four"8getStringOrInt True = 94910-- or use case expression directly11valToString': (isInt: Bool) -> (case isInt of False => String True => Int) -> String12valToString' False y = trim y13valToString' True y = cast y
xxxxxxxxxx141Eq Matter where2 (==) Solid Solid = True3 (==) Liquid Liquid = True4 (==) Gas Gas = True5 (==) _ _ = False6 7 (/=) x y = not (x == y)8 9-- default methods10interface Eq a where11 (==): a -> a -> Bool12 (/=): a -> a -> Bool13 14 (==) x y = not (x /= y)15 (/=) x y = not (x == y)