xxxxxxxxxx
111-- explicit
2append: (elem: Type) -> (n: Nat) -> (m: Nat) ->
3 Vect n elem -> Vect m elem -> Vect (n + m) elem
4-- > append Char 2 2 ['a', 'b'] ['c', 'd']
5-- > append _ _ _ ['a', 'b'] ['c', 'd']
6
7-- implicit
8append: Vect n elem -> Vect m elem -> Vect (n + m) elem -- unbound
9append: {elem: Type} -> {n: Nat} -> {m: Nat} ->
10 Vect n elem -> Vect m elem -> Vect (n + m) elem -- bound
11-- > append ['a', 'b'] ['c', 'd']
xxxxxxxxxx
131-- plain
2length: Vect n elem -> Nat
3length [] = Z
4length (x :: xs) = 1 + length xs
5
6-- dependent
7length: Vect n elem -> Nat
8length {n} xs = n
9
10-- dependent with pattern matching
11createEmpties: Vect n (Vect 0 a)
12createEmpties {n = Z} = []
13createEmpties {n = (S k)} = [] :: createEmpties
xxxxxxxxxx
201-- enumerated types (enum)
2data Bool = False | True
3data Direction = North | East | South | West
4
5-- union types (parameterized enum)
6data Shape = Triangle Double Double
7 | Rectangle Double Double
8 | Circle Double
9data Shape: Type where
10 Triangle: Double -> Double -> Shape
11 Rectangle: Double -> Double -> Shape
12 Circle: Double -> Shape
13
14-- recursive types (self-defining types)
15data Nat = Z | S Nat
16data Infinite = Forever Infinite
17
18-- generic types (type-parameterized)
19data Maybe valtype = Nothing | Just valtype
20data Either a b = Left a | Right b
xxxxxxxxxx
51insert x orig@(Node left val right)
2 = case compare x val of
3 LT => Node (insert x left) val right
4 EQ => orig
5 GT => Node left val (insert x right)
xxxxxxxxxx
151data PowerSouce = Petrol | Pedal
2
3data Vehicle: PowerSource -> Type where -- dependent types / families of types
4 Bicycle: Vehicle Pedal
5 Car: (fuel: Nat) -> Vehicle Petrol
6 Bus: (fuel: Nat) -> Vehicle Petrol
7
8data Vect: Nat -> Type -> Type where
9 Nil: Vect Z a
10 (::): (x: a) -> (xs: Vect k a) -> Vect (S k) a
11
12data DataStore: Type where
13 MkData: (size: Nat) ->
14 (items: Vect size String) -> -- computation from parameters
15 DataStore
xxxxxxxxxx
21anyVect: (n: Nat ** Vect n String) -- second element computed from the first
2anyVect: (n ** Vect n String) -- type inference
x
1 -- type calculation functions
2StringOrInt: Bool -> Type
3StringOrInt False = String
4StringOrInt True = Int
5-- usage
6getStringOrInt: (isInt: Bool) -> StringOrInt isInt
7getStringOrInt False = "Ninety Four"
8getStringOrInt True = 94
9
10-- or use case expression directly
11valToString': (isInt: Bool) -> (case isInt of False => String True => Int) -> String
12valToString' False y = trim y
13valToString' True y = cast y
xxxxxxxxxx
141Eq Matter where
2 (==) Solid Solid = True
3 (==) Liquid Liquid = True
4 (==) Gas Gas = True
5 (==) _ _ = False
6
7 (/=) x y = not (x == y)
8
9-- default methods
10interface Eq a where
11 (==): a -> a -> Bool
12 (/=): a -> a -> Bool
13
14 (==) x y = not (x /= y)
15 (/=) x y = not (x == y)