-
Notifications
You must be signed in to change notification settings - Fork 17
/
paper.1ml
289 lines (235 loc) · 6.75 KB
/
paper.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
;; Examples from the 1ML paper
;; (assumes prelude.1ml)
;;
;; Note: This is assuming full 1ML, so unlike in the first part of the paper,
;; explicit type parameters must be annotated as 'type'.
;; Section 2: 1ML with Explicit Types
;; Functional Core
application = (fun (n : int) => n + n) 3;
expanded = let f = fun (n : int) => n + n; x = 3 in f x;
moreexpanded = {f = fun (n : int) => n + n; x = 3; body = f x}.body;
;; Reified Types
identity = fun (a : type) => fun (x : a) => x;
pair = fun (a : type) => fun (b : type) => type {fst : a; snd : b};
second = fun (a : type) => fun (b : type) => fun (p : pair a b) => p.snd;
identity (a : type) (x : a) = x;
type pair a b = {fst : a; snd : b};
second (a : type) (b : type) (p : pair a b) = p.snd;
;; Translucency
M =
{
type size = int;
type pair a b = {fst : a; snd : b}
};
M_opaque = M :
{
size : type;
pair : (a : type) => (b : type) => type
};
M_transparent = M :
{
size : (= type int);
pair : (a : type) => (b : type) => (= type {fst : a; snd : b})
};
M_opaque = M :
{
type size;
type pair a b;
};
M_transparent = M :
{
type size = int;
type pair a b = {fst : a; snd : b};
};
;; Functors
type EQ =
{
type t;
eq : t -> t -> bool;
};
type MAP =
{
type key;
type map a;
empty (a : type) : map a;
add (a : type) : key -> a -> map a -> map a;
lookup (a : type) : key -> map a -> opt a;
};
Map (Key : EQ) :> MAP with (type key = Key.t) =
{
type key = Key.t;
type map a = key -> opt a;
empty (a : type) = fun (k : key) => none;
lookup (a : type) (k : key) (m : map a) = m k;
add (a : type) (k : key) (v : a) (m : map a) =
fun (x : key) => if Key.eq x k then some v else m x : opt a
};
;; Applicative vs. Generative
F = (fun (a : type) => type {x : a}) :> type => type;
G = (fun (a : type) => type {x : a}) :> type -> type;
H = fun (a : type) => (type {x : a} :> type);
;; J = G :> type => type; ;; ill-typed!
;; Higher Polymorphism
f (id : (a : type) => a -> a) = {x = id int 5; y = id bool true};
type SHAPE = {type t; area : t -> int; v : t};
volume (height : int) (s : SHAPE) = height * s.area (s.v);
area ss = List.foldl ss 0 (fun a (wrap s : wrap SHAPE) => a + s.area (s.v));
type COLL c =
{
type key;
type val;
empty : c;
add : c -> key -> val -> c;
lookup : c -> key -> opt val;
keys : c -> list key
};
entries (c : type) (C : COLL c) (xs : c) : list (type (C.key, C.val)) =
List.map (C.keys xs) (fun (k : C.key) => (k, caseopt (C.lookup xs k) bot id));
type MONAD (m : type => type) =
{
return (a : type) : a -> m a;
bind (a : type) (b : type) : m a -> (a -> m b) -> m b
};
map (a : type) (b : type) (m : type => type) (M : MONAD m) (f : a -> b) (mx : m a) =
M.bind a b mx (fun (x : a) => M.return b (f x)); ;; : m b
;; ...the same with implicit types:
type MONAD (m : type => type) =
{
return 'a : a -> m a;
bind 'a 'b : m a -> (a -> m b) -> m b
};
map (m : type => type) (M : MONAD m) f mx =
M.bind mx (fun x => M.return (f x));
;; Computed Modules
pickTable (size : int) (threshold : int) (HashMap : MAP) (TreeMap : MAP) =
{
Table = if size > threshold then HashMap else TreeMap : MAP
};
(;
type MONAD =
{
type t a;
return (a : type) : a -> t a;
bind (a : type) (b : type) : t a -> (a -> t b) -> t b
};
type MONAD_TRANS =
{
include MONAD;
type base a;
lift (a : type) : base a -> t a;
join (a : type) : t (base a) -> t a;
};
mapM (M : MONAD) (a : type) (b : type) (f : a -> b) (m : M.t a) : M.t b =
M.bind a b m (fun (x : a) => M.return b (f x));
joinM (M : MONAD) (a : type) (mm : M.t (M.t a)) : M.t a =
M.bind (M.t a) a mm (fun (m : M.t a) => m);
StackM (M : MONAD) =
rec (Loop : (n : int) -> (MONAD_TRANS with (base = M.t))) =>
fun (n : int) =>
if n == 1 then
{ include M;
type base a = t a;
lift (a : type) (m : base a) = m;
join (a : type) (m : t (base a)) = joinM M a m;
}
else
(
let M' = Loop (n - 1) in
{ type t a = M'.t (M.t a);
type base a = M.t a;
lift (a : type) (m : base a) = M'.return (M.t a) m;
join (a : type) (m : t (base a)) = M'.join (base a) m;
return (a : type) (x : a) = M'.return (M.t a) (M.return a x);
bind (a : type) (b : type) (m : t a) (f : a -> t b) =
M'.bind (M.t a) (M.t b) m
(fun (mx : M.t a) => M.bind a b mx
(fun (x : a) => f x));
}
) : MONAD_TRANS with (type base a = M.t a);
;)
;; Predicativity
type T1 = type;
type T2 = {type u};
type T3 = {type u = T2};
type T4 = (x : {}) -> type;
type T5 = (a : type) => {};
type T6 = {type u a = bool};
type Ti = T1;
;; type U = pair Ti Ti; ;; error
;; A = (type Ti) : type; ;; error
;; B = {type u = Ti} :> {type u}; ;; error
;; C = if true then Ti else int : type; ;; error
type T1' = (= type int);
type T2' = {type u = int};
type Ti = T2';
type U = pair Ti Ti;
A = (type Ti) : type;
B = {type u = Ti} :> {type u};
C = if true then Ti else int : type;
;; Section 3: Type System and Elaboration
;; 3.2 Elaboration
;; Subtyping
type MONSTER =
(= (fun (x : {}) => ({type t = int; v = 0} :> {type t; v : t}).v));
;; Test (X : MONSTER) = X : MONSTER; ;; error
;; Section 4: Full 1ML
SubtypingImplicitsWithTypeMatching =
fun (X : '(a : type) => {type t = a; f : a -> t}) =>
X : {type t; f : int -> int};
type MAP =
{
type key;
type map a;
empty 'a : map a;
lookup 'a : key -> map a -> opt a;
add 'a : key -> a -> map a -> map a
};
Map (Key : EQ) :> MAP with (type key = Key.t) =
{
type key = Key.t;
type map a = key -> opt a;
empty = fun x => none;
lookup x m = m x;
add x y m = fun z => if Key.eq z x then some y else m z
};
FirstClassImplicit =
(fun (id : 'a => a -> a) => {x = id 3; y = id true}) (fun x => x);
;; Section 5: Inference
;; 5.2 Incompleteness
;; Type Scoping
id : 'a => a -> a = fun x => x;
G (x : int) = {M = {type t = int; v = x} :> {type t; v : t}; f = id id};
C = G 3;
;; x = C.f (C.M.v); ;; error
;; ...works with pure 'id' function:
id = fun x => x;
G (x : int) = {M = {type t = int; v = x} :> {type t; v : t}; f = id id};
C = G 3;
x = C.f (C.M.v);
;; ...or with type annotation:
id : 'a => a -> a = fun x => x;
G (x : int) = {M = {type t = int; v = x} :> {type t; v : t}; f : M.t -> M.t = id id};
C = G 3;
x = C.f (C.M.v);
;; ...or when applying G twice.
id : 'a => a -> a = fun x => x;
G (x : int) = {M = {type t = int; v = x} :> {type t; v : t}; f = id id};
C = G 3;
C' = G 3;
x = C'.f (C.M.v);
;; x = C.f (C'.M.v); ;; but this is an error
;; Appendix C: Impredicativity
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 'a 'b xo = (unwrap xo : opt a) b;
};