-
Notifications
You must be signed in to change notification settings - Fork 0
/
idris-code.idr
56 lines (39 loc) · 1.31 KB
/
idris-code.idr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
%hide Nat -- hiding Prelude.Nat
data Nat = Zero | Succ Nat
infixr 5 :>
data Vec : Type -> Nat -> Type where
Nil : Vec a Zero
(:>) : (x : a) -> Vec a k -> Vec a (Succ k)
eg0 : Vec Integer (Succ (Succ (Succ Zero)))
-- Idris requires a top-level type annotation for every function
eg0 = (1:>2:>3:>Nil)
hd : Vec a (Succ k) -> a
hd (h :> t) = h
plus : Nat -> Nat -> Nat
plus Zero y = y
plus (Succ x) y = Succ (plus x y)
append : Vec a m -> Vec a n -> Vec a (plus m n)
append Nil ys = ys
append (x :> xs) ys = x :> (append xs ys)
eg1 : Vec Integer (Succ (Succ (Succ (Succ (Succ Zero)))))
-- Idris requires a top-level type annotation for every function
eg1 = append (1:>2:>Nil) (3:>4:>5:>Nil)
data Lt : Nat -> Nat -> Type where
Base : Lt Zero (Succ n)
Ind : Lt n m -> Lt (Succ n) (Succ m)
-- nth : (m:Nat) -> Vec a n -> (prf : Lt m n) -> a
-- nth Zero (x :> xs) prf = x
-- nth (Succ n) (x :> xs) prf = ?rhs
nth : (m:Nat) -> Vec a n -> (prf : Lt m n) -> a
nth Zero (x :> xs) prf = x
nth (Succ n) (x :> xs) (Ind prf) = nth n xs prf
eg2 : Integer
eg2 = nth Zero eg1 Base
eg3 : Integer
eg3 = nth (Succ Zero) eg1 (Ind Base)
nth_auto : (m:Nat) -> Vec a n -> {auto prf : Lt m n} -> a
nth_auto m xs {prf} = nth m xs prf
eg2' : Integer
eg2' = nth_auto Zero eg1
eg3' : Integer
eg3' = nth_auto (Succ Zero) eg1