Source file typechecking.ml
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
open Term
open Typing
exception No_method of string * Type.t
exception Top_level_override of string * Pos.t option
let debug = ref false
(** {1 Type checking / inference} *)
(** Can a function return type be generalized after being applied? Current
implementation considers it safe to generalize when all universal types in
the function return type are specified by a non-optional argument.
For instance,
- this can be generalized:
[def f() = fun (x) -> x end; fn = f() : fun ('a) -> 'a ]
- but this cannot: [s = single() : source('A) ]
- and this cannot either:
[def f() = fun (x=null) -> ref(x) end; fn = f() : (?'A?) -> ref('A) ]
When all universal types in the return are specified by a non-optional
argument, we are assured that anything that would be generalized is later
specified when the required argument is passed. If this argument is still of
a universal type, then the argument's universal type takes over the type
that was generalized. *)
let function_app_value_restriction fn =
let rec filter_app_vars l t =
let t = Type.deref t in
match t.descr with
| Int | Float | String | Bool | Never -> l
| Custom c -> c.filter_vars filter_app_vars l c.typ
| Getter t -> filter_app_vars l t
| List { t } | Nullable t -> filter_app_vars l t
| Tuple aa -> List.fold_left filter_app_vars l aa
| Meth ({ scheme = g, t }, u) ->
let l =
List.filter (fun v -> not (List.mem v g)) (filter_app_vars l t)
in
filter_app_vars l u
| Constr c ->
List.fold_left (fun l (_, t) -> filter_app_vars l t) l c.params
| Var { contents = Free var } -> var :: l
| Var { contents = Link _ } -> assert false
| Arrow (p, t) ->
let l = filter_app_vars l t in
let pl =
List.fold_left
(fun pl -> function
| true, _, _ -> pl | false, _, t -> filter_app_vars pl t)
[] p
in
List.filter (fun v -> not (List.memq v pl)) l
in
match Type.demeth fn.Term.t with
| { Type.descr = Arrow (_, t) } -> filter_app_vars [] t = []
| _ -> false
(** Terms for which generalization is safe. *)
let value_restriction t =
let rec value_restriction t =
match t.term with
| `Var _ -> true
| `Fun _ -> true
| `Null -> true
| `App (fn, _) -> function_app_value_restriction fn
| `List l | `Tuple l -> List.for_all value_restriction l
| `Int _ | `Float _ | `String _ | `Bool _ | `Custom _ -> true
| `Let l -> value_restriction l.def && value_restriction l.body
| `Cast { cast = t } -> value_restriction t
| _ -> false
in
value_restriction t
&& Methods.for_all (fun _ meth_term -> value_restriction meth_term) t.methods
(** A simple mechanism for delaying printing toplevel tasks as late as possible,
to avoid seeing too many unknown variables. *)
let add_task, pop_tasks =
let q = Queue.create () in
( (fun f -> Queue.add f q),
fun () ->
try
while true do
(Queue.take q) ()
done
with Queue.Empty -> () )
(** Generate a type with fresh variables for a pattern. *)
let type_of_pat ~level ~pos = function
| `PVar x ->
let a = Type.var ~level ?pos () in
([(x, a)], a)
| `PTuple l ->
let env, l =
List.fold_left
(fun (env, l) var ->
let a = Type.var ~level ?pos () in
(([var], a) :: env, a :: l))
([], []) l
in
let l = List.rev l in
(env, Type.make ?pos (Type.Tuple l))
let rec check ?(print_toplevel = false) ~throw ~level ~env e =
let check = check ~throw in
if !debug then Printf.printf "\n# %s : ?\n\n%!" (Term.to_string e);
let check ?print_toplevel ~level ~env e =
check ?print_toplevel ~level ~env e;
if !debug then
Printf.printf "\n# %s : %s\n\n%!" (Term.to_string e) (Type.to_string e.t)
in
let pos = e.t.Type.pos in
let mk t = Type.make ?pos t in
let check_fun ~env e { arguments; body } =
let base_check = check ~level ~env in
let proto_t, env =
List.fold_left
(fun (p, env) -> function
| { label; as_variable; typ; default = None; pos } ->
update_level level typ;
( (false, label, typ) :: p,
env#add ~pos (Option.value ~default:label as_variable) ([], typ)
)
| { label; as_variable; typ; default = Some v; pos } ->
update_level level typ;
base_check v;
v.t <: typ;
( (true, label, typ) :: p,
env#add ~pos (Option.value ~default:label as_variable) ([], typ)
))
([], env) arguments
in
let proto_t = List.rev proto_t in
List.fold_left
(fun labels (_, l, _) ->
if l = "" then labels
else (
if List.mem l labels then raise (Duplicate_label (e.t.Type.pos, l));
l :: labels))
[] proto_t
|> ignore;
check ~level ~env body;
e.t >: mk (Type.Arrow (proto_t, body.t))
in
let base_type = Type.var () in
let () =
match e.term with
| `Cache_env r ->
r :=
{
var_name = Atomic.get Type_base.var_name_atom;
var_id = Atomic.get Type_base.var_id_atom;
env = env#current;
};
base_type >: mk (Tuple [])
| `Int _ -> base_type >: mk Int
| `Float _ -> base_type >: mk Float
| `String _ -> base_type >: mk String
| `Bool _ -> base_type >: mk Bool
| `Custom h -> base_type >: mk h.handler.typ.Type.descr
| `Encoder f ->
let rec check_enc (_, p) =
List.iter
(function
| `Labelled (_, t) -> check ~level ~env t
| `Anonymous _ -> ()
| `Encoder e -> check_enc e)
p
in
check_enc f;
let t =
try !Hooks.type_of_encoder ~pos f
with Not_found ->
let bt = Printexc.get_raw_backtrace () in
Printexc.raise_with_backtrace
(Unsupported_encoder (pos, Term.to_string e))
bt
in
base_type >: t
| `List l ->
List.iter (fun x -> check ~level ~env x) l;
let t = Type.var ~level ?pos () in
List.iter (fun e -> e.t <: t) l;
base_type >: mk Type.(List { t; json_repr = `Tuple })
| `Tuple l ->
List.iter (fun a -> check ~level ~env a) l;
base_type >: mk (Type.Tuple (List.map (fun a -> a.t) l))
| `Null -> base_type >: mk (Type.Nullable (Type.var ~level ?pos ()))
| `Cast { cast = a; typ = t } ->
check ~level ~env a;
a.t <: t;
base_type >: t
| `Hide (a, methods) ->
check ~level ~env a;
let ty =
List.fold_left
(fun ty name ->
Type.make ?pos
Type.(
Meth
( {
meth = name;
optional = true;
scheme = ([], Type.make ?pos Type.Never);
doc = { meth_descr = ""; category = `Method };
json_name = None;
},
ty )))
a.t methods
in
base_type >: ty
| `Invoke { invoked = a; invoke_default; meth = l } ->
check ~level ~env a;
let rec aux t =
match (Type.deref t).Type.descr with
| Type.(
Meth ({ meth = l'; scheme = (_, { descr }) as s; optional }, _))
when l = l' && (optional = false || descr = Never) ->
(fst s, Typing.instantiate ~level s)
| Type.(Meth (_, c)) -> aux c
| _ ->
let x = Type.var ~level ?pos () in
let y = Type.var ~level ?pos () in
a.t
<: mk
Type.(
Meth
( {
meth = l;
optional = invoke_default <> None;
scheme = ([], x);
doc = { meth_descr = ""; category = `Method };
json_name = None;
},
y ));
([], x)
in
let vars, typ = aux a.t in
let typ =
match (invoke_default, Type.deref typ) with
| None, { descr = Never } -> raise (No_method (l, a.t))
| None, _ -> typ
| Some v, _ -> (
check ~level ~env v;
let v_t = Typing.instantiate ~level (vars, v.t) in
match typ.Type.descr with
| Never -> v_t
| _ ->
let typ =
match (Type.deref v.t).descr with
| Type.Nullable _ -> mk Type.(Nullable typ)
| _ -> typ
in
v_t <: typ;
typ)
in
base_type >: typ
| `Open (a, b) ->
check ~level ~env a;
a.t <: mk Type.unit;
let rec aux env t =
match (Type.deref t).Type.descr with
| Type.(Meth ({ meth = l; scheme = g, u }, t)) ->
aux (env#add ~pos l (g, u)) t
| _ -> env
in
let env = aux env a.t in
check ~level ~env b;
base_type >: b.t
| `Seq (a, b) ->
check ~env ~level a;
if not (can_ignore a.t) then (
let bt = Printexc.get_callstack 0 in
throw ~bt (Ignored a));
check ~print_toplevel ~level ~env b;
base_type >: b.t
| `App (a, l) -> (
check ~level ~env a;
List.iter (fun (_, b) -> check ~env ~level b) l;
match (Type.demeth a.t).Type.descr with
| Type.Arrow (ap, t) ->
let get_arg lbl l =
let rec aux acc = function
| [] -> None
| (o, lbl', t) :: l ->
if lbl = lbl' then Some (o, t, List.rev_append acc l)
else aux ((o, lbl', t) :: acc) l
in
aux [] l
in
let _, ap =
List.fold_left
(fun (already, ap) (lbl, v) ->
match get_arg lbl ap with
| None ->
let first = not (List.mem lbl already) in
raise (No_label (a, lbl, first, v))
| Some (_, t, ap') ->
(match (a.term, lbl) with
| `Var "if", "then" | `Var "if", "else" -> (
match
( (Type.deref v.t).descr,
(Type.deref t).descr )
with
| Type.Arrow ([], vt), Type.Arrow ([], t) ->
vt <: t
| _ -> assert false)
| _ -> v.t <: t);
(lbl :: already, ap'))
([], ap) l
in
let mandatory =
List.filter_map
(fun (o, l, t) -> if o then None else Some (l, t))
ap
in
if mandatory <> [] then
raise (Term.Missing_arguments (pos, mandatory));
base_type >: t
| _ ->
let p = List.map (fun (lbl, b) -> (false, lbl, b.t)) l in
a.t <: Type.make (Type.Arrow (p, base_type)))
| `Fun p ->
let env =
match p.name with
| None -> env
| Some name -> env#add ~pos name ([], base_type)
in
check_fun ~env e p
| `Var var ->
let s = env#get ~pos var in
base_type >: Typing.instantiate ~level s;
if Lazy.force Term.debug then
Printf.eprintf "Instantiate %s : %s becomes %s\n" var
(Type.string_of_scheme s) (Type.to_string base_type)
| `Let ({ pat; replace; def; body; _ } as l) ->
check ~level:(level + 1) ~env def;
let generalized =
if value_restriction def then fst (generalize ~level def.t) else []
in
let penv, pa = type_of_pat ~level ~pos pat in
def.t <: pa;
let env =
List.fold_left
(fun env (ll, a) ->
match ll with
| [] -> assert false
| [x] ->
let a =
if replace then Type.remeth (snd (env#get ~pos x)) a
else a
in
if !debug then
Printf.printf "\nLET %s : %s\n%!" x
(Repr.string_of_scheme (generalized, a));
env#add ~pos x (generalized, a)
| l :: ll ->
let g, t = env#get ~pos l in
let a =
if replace then Type.remeth (snd (Type.invokes t ll)) a
else a
in
let invoke t l =
try Type.invoke t l
with Not_found ->
raise (No_method (l, { t with Type.pos }))
in
env#override l
(g, Type.meths ?pos ~invoke ll (generalized, a) t))
env penv
in
l.gen <- generalized;
if print_toplevel then
add_task (fun () ->
Format.printf "@[<2>%s :@ %a@]@."
(let name = string_of_pat pat in
let l = String.length name and max = 5 in
if l >= max then name else name ^ String.make (max - l) ' ')
(fun f t -> Repr.print_scheme f (generalized, t))
def.t);
check ~print_toplevel ~level ~env body;
base_type >: body.t
in
e.t
>: Methods.fold
(fun meth meth_term t ->
check ~level ~env meth_term;
Type.make ?pos
(Type.Meth
( {
Type.meth;
optional = false;
scheme = Typing.generalize ~level meth_term.t;
doc = { meth_descr = ""; category = `Method };
json_name = None;
},
t )))
e.methods base_type
let display_types = ref false
let check ?env ~check_top_level_override ~throw e =
let print_toplevel = !display_types in
try
let env =
match env with
| Some env -> env
| None -> Environment.default_typing_environment ()
in
let top_level_variables =
if check_top_level_override then List.map fst env else []
in
let env =
object (self)
val env = env
val top_level_variables = top_level_variables
method current = env
method check_top_level_override ~pos var =
if List.mem var top_level_variables then (
let bt = Printexc.get_callstack 1 in
throw ~bt (Top_level_override (var, pos)))
method add ~pos var v =
self#check_top_level_override ~pos var;
self#override var v
method override var v =
{<env = (var, v) :: env
; top_level_variables = List.filter
(fun v -> v <> var)
top_level_variables>}
method get ~pos var =
try List.assoc var env with Not_found -> raise (Unbound (pos, var))
end
in
check ~print_toplevel ~throw ~level:0 ~env e;
if print_toplevel && (Type.deref e.t).Type.descr <> Type.unit then
add_task (fun () ->
Format.printf "@[<2>- :@ %a@]@." Repr.print_type e.t);
pop_tasks ()
with e ->
let bt = Printexc.get_raw_backtrace () in
pop_tasks ();
Printexc.raise_with_backtrace e bt