-
Notifications
You must be signed in to change notification settings - Fork 17
/
prelude.1ml
249 lines (216 loc) · 5.15 KB
/
prelude.1ml
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
;; Fun
Fun =
{
bot = rec bot => fun x => bot x;
id x = x;
const x y = x;
(>>) (f, g) x = g (f x);
(<<) (f, g) x = f (g x);
curry f x y = f (x, y);
uncurry f (x, y) = f x y;
};
include Fun;
;; Bool
Bool =
{
type bool = primitive "bool";
type t = bool;
true = primitive "true" ();
false = primitive "false" ();
not b = if b then false else true;
print b = primitive "Text.print" (if b then "true" else "false");
};
type bool = Bool.t;
true = Bool.true;
false = Bool.false;
not = Bool.not;
(==) 'a (x : a, y : a) = primitive "==" a (x, y);
(<>) 'a (x : a, y : a) = not (x == y);
;; Int
Int =
{
type int = primitive "int";
type t = int;
(+) = primitive "Int.+";
(-) = primitive "Int.-";
(*) = primitive "Int.*";
(/) = primitive "Int./";
(%) = primitive "Int.%";
(<) = primitive "Int.<";
(>) = primitive "Int.>";
(<=) = primitive "Int.<=";
(>=) = primitive "Int.>=";
print = primitive "Int.print";
};
type int = Int.t;
(+) = Int.+;
(-) = Int.-;
(*) = Int.*;
(/) = Int./;
(%) = Int.%;
(<) = Int.<;
(>) = Int.>;
(<=) = Int.<=;
(>=) = Int.>=;
;; Char
Char =
{
type char = primitive "char";
type t = char;
toInt = primitive "Char.toInt";
fromInt = primitive "Char.fromInt";
print = primitive "Char.print";
};
type char = Char.t;
;; Text
Text =
{
type text = primitive "text";
type t = text;
(++) = primitive "Text.++";
(<) = primitive "Text.<";
(>) = primitive "Text.>";
(<=) = primitive "Text.<=";
(>=) = primitive "Text.>=";
length t = primitive "Text.length" t;
sub t i = primitive "Text.sub" (t, i);
fromChar c = primitive "Text.fromChar" c;
print = primitive "Text.print";
};
type text = Text.t;
(++) = Text.++;
print = Text.print;
;; Opt
type OPT =
{
type opt a;
none 'a : opt a;
some 'a : a -> opt a;
caseopt 'a 'b : opt a -> (() -> b) -> (a -> b) -> b;
};
Opt :> OPT =
{
type opt a = wrap (b : type) => (() -> b) -> (a -> b) -> b;
none 'a = wrap (fun (b : type) (n : () -> b) (s : a -> b) => n ()) : opt a;
some 'a x = wrap (fun (b : type) (n : () -> b) (s : a -> b) => s x) : opt a;
caseopt xo = (unwrap xo : opt _) _;
};
include Opt;
;; Alt
type ALT =
{
type alt a b;
left 'a 'b : a -> alt a b;
right 'a 'b : b -> alt a b;
casealt 'a 'b 'c : alt a b -> (a -> c) -> (b -> c) -> c;
};
Alt :> ALT =
{
type alt a b = wrap (c : type) => (a -> c) -> (b -> c) -> c;
left 'a 'b x =
wrap (fun (c : type) (l : a -> c) (r : b -> c) => l x) : alt a b;
right 'a 'b x =
wrap (fun (c : type) (l : a -> c) (r : b -> c) => r x) : alt a b;
casealt xy = (unwrap xy : alt _ _) _;
};
include Alt;
;; List
type LIST_CORE =
{
type list a;
nil 'a : list a;
cons 'a : a -> list a -> list a;
foldr 'a 'b : list a -> b -> (a -> b -> b) -> b;
};
type LIST =
{
include LIST_CORE;
caselist 'a 'b : list a -> (() -> b) -> (a -> list a -> b) -> b;
isNil 'a : list a -> bool;
head 'a : list a -> opt a;
tail 'a : list a -> opt (list a);
length 'a : list a -> int;
cat 'a : list a -> list a -> list a;
rev 'a : list a -> list a;
nth 'a : list a -> int -> opt a;
map 'a 'b : list a -> (a -> b) -> list b;
filter 'a : list a -> (a -> bool) -> list a;
foldl 'a 'b : list a -> b -> (b -> a -> b) -> b;
};
List :> LIST =
{
include
{
type list a = wrap (b : type) => b -> (a -> b -> b) -> b;
nil 'a = wrap (fun (b : type) (n : b) (c : a -> b -> b) => n) : list _;
cons x xs =
wrap (fun (b : type) (n : b) (c : _ -> b -> b) =>
c x ((unwrap xs : list _) b n c)) : list _;
foldr xs = (unwrap xs : list _) _;
} :> LIST_CORE;
isNil xs = foldr xs true (fun _ _ => false);
head xs = foldr xs none (fun x _ => some x);
tail xs =
foldr xs (nil, none)
(fun x (acc : (_, _)) => (cons x (acc.1), some (acc.1))) .2;
caselist xs n c =
caseopt (head xs) n (fun x => caseopt (tail xs) n (fun xs' => c x xs'));
length xs = foldr xs 0 (fun _ n => n + 1);
cat xs1 xs2 = foldr xs1 xs2 cons;
rev xs = foldr xs nil (fun x xs => cat xs (cons x nil));
map xs f = foldr xs nil (fun x => cons (f x));
foldl xs x f = foldr (rev xs) x (fun x y => f y x);
filter xs f = foldr xs nil (fun x ys => if f x then cons x ys else ys);
nth xs n =
foldr xs (length xs - 1, none) (fun x (p : (_, _)) =>
(p.1 - 1, if p.1 == n then some x else p.2)
) .2;
};
include List;
;; Set
type ORD =
{
type t;
(<=) : (t, t) -> bool;
};
type SET =
{
type set;
type elem;
type t = set;
empty : set;
add : elem -> set -> set;
mem : elem -> set -> bool;
card : set -> int;
};
Set (Elem : ORD) :> SET with (elem = Elem.t) =
{
type elem = Elem.t;
type set = (int, elem -> bool);
type t = set;
empty = (0, fun (x : elem) => false);
card (s : set) = s._1;
mem (x : elem) (s : set) = s._2 x;
add (x : elem) (s : set) =
if mem x s then s
else (s._1 + 1, fun (y : elem) => x == y or mem y s) : set;
};
;; Map
type MAP =
{
type map a;
type key;
type t a = map a;
empty 'a : map a;
add 'a : key -> a -> map a -> map a;
lookup 'a : key -> map a -> opt a;
};
Map (Key : ORD) :> MAP with (key = Key.t) =
{
type key = Key.t;
type map a = key -> opt a;
t = map;
empty x = none;
lookup x m = m x;
add x y m z = if x == z then some y else m x;
};