-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.timl
93 lines (74 loc) · 2.85 KB
/
test.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
datatype 'a list =
nil
| cons of 'a * 'a list
datatype {Nat} 'a ilist =
NilI of unit -> {0} 'a ilist
| ConsI of {n' : Nat} 'a * {n'} 'a ilist -> {n' + 1} 'a ilist
datatype {Nat} {Bool} 'a rbt =
Leaf of unit -> {0} {true} 'a rbt
| Black of {lc : Bool} {rc : Bool} {bh : Nat}
'a * {bh} {lc} 'a rbt * {bh} {rc} 'a rbt -> {bh + 1} {true} 'a rbt
| Red of {bh : Nat}
'a * {bh} {true} 'a rbt * {bh} {true} 'a rbt -> {bh} {false} 'a rbt
val n = 77
val _ =
let
val n = 77
in
n
end
val (a, (b, (c, ()))) = (1, (2, (3, ())))
val _ = fn ['a] ['b] ((x, y) : 'a * 'b) => (y, x)
(*(* not exhaustive *)*)
(* val _ = fn ['a] (nil : 'a list) => nil ['a] *)
val _ =
fn ['a] (ls : 'a list) =>
case ls of
nil => nil ['a]
| cons _ => nil ['a]
val _ =
fn ['a] {n : Nat} (ls : {n} 'a ilist) =>
case ls of
NilI => 0
| ConsI {_} _ => 0
(* redundant *)
(* val _ = *)
(* fn ['a] {n : Nat} (ls : {n} 'a ilist) => *)
(* case ls of *)
(* NilI => 0 *)
(* | ConsI {_} _ => 0 *)
(* | ConsI {_} (_, NilI) => 0 *)
(* not exhaustive *)
(* val _ = *)
(* fn ['a] {n : Nat} (ls : {n} 'a ilist) => *)
(* case ls of *)
(* NilI => 0 *)
(* | ConsI {_} (_, NilI) => 0 *)
(* | ConsI {_} (_, (ConsI {_} (_, NilI))) => 0 *)
val map =
fn ['a] ['b] {m : Time} (f : 'a -- m --> 'b) =>
fix map {n : Nat} (ls : {n} 'a ilist) : {n} 'b ilist |> (m + 2.0) * $n =>
case ls return {n} 'b ilist |> (m + 2.0) * $n of
NilI => NilI ['b]
| ConsI {n'} (x, xs) => ConsI ['b] {n'} (f x, map {n'} xs)
(* omit time in return clause *)
val map2 =
fn ['a] ['b] {m : Time} (f : 'a -- m --> 'b) =>
rec (map : forall {n : Nat}, {n} 'a ilist -- (m + 2.0) * $n --> {n} 'b ilist) {n : Nat} (ls : {n} 'a ilist) =>
case ls return {n} 'b ilist of
NilI => NilI ['b]
| ConsI {n'} (x, xs) => ConsI ['b] {n'} (f x, map {n'} xs) |> (m + 2.0) * $n
(* omit return clause *)
val map3 =
fn ['a] ['b] {m : Time} (f : 'a -- m --> 'b) =>
rec (map : forall {n : Nat}, {n} 'a ilist -- (m + 2.0) * $n --> {n} 'b ilist) {n : Nat} (ls : {n} 'a ilist) =>
case ls of
NilI => NilI ['b] : {n} 'b ilist
| ConsI {n'} (x, xs) => ConsI ['b] {n'} (f x, map {n'} xs) : {n} 'b ilist |> (m + 2.0) * $n
val map4 =
fn ['a] ['b] {m : Time} (f : 'a -- m --> 'b) =>
rec (map : forall {n : Nat}, {n} 'a ilist -- (m + 2.0) * $n --> {n} 'b ilist) {n : Nat} (ls : {n} 'a ilist) =>
case ls return {n} 'b ilist |> (m + 2.0) * $n of
NilI => NilI ['b]
| ConsI {n'} (pair as (x, xs as NilI)) => ConsI ['b] {n'} (f x, map {n'} xs)
| ConsI {n'} (x, xs) => ConsI ['b] {n'} (f x, map {n'} xs)