-
Notifications
You must be signed in to change notification settings - Fork 6
/
nat.timl
76 lines (56 loc) · 2.33 KB
/
nat.timl
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
(* Operations for indexed natural numbers *)
structure Nat = struct
datatype cmp_result {a b : Nat} =
Equal {a = b} of cmp_result {a} {b}
| Less {a < b} of cmp_result {a} {b}
| Greater {a > b} of cmp_result {a} {b}
fun cmp {x y : Nat} (x : nat {x}, y : nat {y}) return cmp_result {x} {y} using 0.0 =
builtin
datatype le_result : {Nat} {Nat} =
Le {a b : Nat} {a <= b} of le_result {a} {b}
| Gt {a b : Nat} {a > b} of le_result {a} {b}
fun le {x : Nat} {y : Nat} (x : nat {x}, y : nat {y}) return le_result {x} {y} using 7.0 =
case cmp (x, y) of
Equal => Le
| Less => Le
| Greater => Gt
datatype min_max_result : {Nat} {Nat} =
MinMaxResult {a b a' b' : Nat} {(a <= b -> a' = a /\ b' = b) /\ (a > b -> a' = b /\ b' = a)} of nat {a'} * nat {b'} --> min_max_result {a} {b}
fun min_max {m : Time} {x y : Nat} (le : nat {x} * nat {y} -- m --> le_result {x} {y}) (x : nat {x}, y : nat {y}) return min_max_result {x} {y} =
case le (x, y) of
Le => MinMaxResult (x, y)
| Gt => MinMaxResult (y, x)
datatype eq_dec {a b : Nat} =
Eq {a = b} of eq_dec {a} {b}
| Neq {a <> b} of eq_dec {a} {b}
fun eq_dec {x y : Nat} (x : nat {x}, y : nat {y}) return eq_dec {x} {y} using 1.0 =
case cmp (x, y) of
Equal => Eq
| Less => Neq
| Greater => Neq
datatype lt_dec {a b : Nat} =
Lt {a < b} of lt_dec {a} {b}
| Ge {a >= b} of lt_dec {a} {b}
fun lt_dec {x y : Nat} (x : nat {x}, y : nat {y}) return lt_dec {x} {y} using 1.0 =
case cmp (x, y) of
Equal => Ge
| Less => Lt
| Greater => Ge
val nat_1 : nat {1} = #1
val nat_2 : nat {2} = #2
val nat_4 : nat {4} = #4
fun nat_plus {a b : Nat} (a : nat {a}, b : nat {b}) return nat {a + b} using 1.0 =
builtin
fun nat_minus {a b : Nat} {a >= b} (a : nat {a}, b : nat {b}) return nat {a - b} using 1.0 =
builtin
fun nat_mult {a b : Nat} (a : nat {a}, b : nat {b}) return nat {a * b} using 1.0 =
builtin
fun nat_max {a b : Nat} (a : nat {a}, b : nat {b}) return nat {a max b} using 1.0 =
builtin
fun floor_half {n : Nat} (n : nat {n}) return nat {floor ($n / 2)} =
builtin
fun ceil_half {n : Nat} (n : nat {n}) return nat {ceil ($n / 2)} =
builtin
datatype nat_less_than {m : Nat} =
NatLT {n : Nat} {n < m} of nat {n} --> nat_less_than {m}
end