Source file type_error.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
open Util
open Ast
open Ast_defs
open Ast_util
open Type_check
include Type_internal
let opt_explain_all_variables = ref false
let opt_explain_constraints = ref false
let opt_explain_all_overloads = ref false
type suggestion = Suggest_add_constraint of n_constraint | Suggest_none
let analyze_unresolved_quant locals ncs = function
| QI_aux (QI_constraint nc, _) ->
let gen_kids = List.filter is_kid_generated (KidSet.elements (tyvars_of_constraint nc)) in
if gen_kids = [] then Suggest_add_constraint nc
else (
let is_subst v = function
| NC_aux (NC_equal (A_aux (A_nexp (Nexp_aux (Nexp_var v', _)), _), A_aux (A_nexp nexp, _)), _)
when Kid.compare v v' = 0 && not (KidSet.exists is_kid_generated (tyvars_of_nexp nexp)) ->
[(v, nexp)]
| NC_aux (NC_equal (A_aux (A_nexp nexp, _), A_aux (A_nexp (Nexp_aux (Nexp_var v', _)), _)), _)
when Kid.compare v v' = 0 && not (KidSet.exists is_kid_generated (tyvars_of_nexp nexp)) ->
[(v, nexp)]
| _ -> []
in
let substs = List.concat (List.map (fun v -> List.concat (List.map (fun nc -> is_subst v nc) ncs)) gen_kids) in
let nc = List.fold_left (fun nc (v, nexp) -> constraint_subst v (arg_nexp nexp) nc) nc substs in
if not (KidSet.exists is_kid_generated (tyvars_of_constraint nc)) then Suggest_add_constraint nc
else (
let is_linked v = function
| id, (Immutable, (Typ_aux (Typ_app (ty_id, [A_aux (A_nexp (Nexp_aux (Nexp_var v', _)), _)]), _) as typ))
when Id.compare ty_id (mk_id "atom") = 0 && Kid.compare v v' = 0 ->
[(v, nid id, typ)]
| id, (mut, typ) -> []
in
let substs =
List.concat
(List.map (fun v -> List.concat (List.map (fun nc -> is_linked v nc) (Bindings.bindings locals))) gen_kids)
in
let nc = List.fold_left (fun nc (v, nexp, _) -> constraint_subst v (arg_nexp nexp) nc) nc substs in
if not (KidSet.exists is_kid_generated (tyvars_of_constraint nc)) then Suggest_none else Suggest_none
)
)
| QI_aux (QI_id _, _) -> Suggest_none
let readable_name (Kid_aux (Var str, l)) =
let str = String.concat "" (String.split_on_char '#' str) in
let str =
if String.length str > 1 && str.[1] = '_' then String.sub str 0 1 ^ String.sub str 2 (String.length str - 2)
else str
in
Kid_aux (Var (String.concat "" (String.split_on_char '#' str)), l)
let has_underscore (Kid_aux (Var str, l)) = String.length str > 1 && str.[1] = '_'
let error_string_of_kid substs kid =
match KBindings.find_opt kid substs with Some nexp -> string_of_nexp nexp | None -> string_of_kid kid
let _error_string_of_nexp substs nexp = string_of_nexp (subst_kids_nexp substs nexp)
let error_string_of_nc substs nexp = string_of_n_constraint (subst_kids_nc substs nexp)
let error_string_of_typ substs typ = string_of_typ (subst_kids_typ substs typ)
let error_string_of_typ_arg substs arg = string_of_typ_arg (subst_kids_typ_arg substs arg)
let has_variable set arg = not (KidSet.is_empty (KidSet.inter set (tyvars_of_typ_arg arg)))
let rewrite_equality preferred_on_right (NC_aux (aux, l) as nc) =
let equality =
match aux with
| NC_equal (lhs, rhs) ->
if has_variable preferred_on_right lhs && not (has_variable preferred_on_right rhs) then Some (rhs, lhs)
else Some (lhs, rhs)
| _ -> None
in
match equality with Some (lhs, rhs) -> NC_aux (NC_equal (lhs, rhs), l) | None -> nc
let subst_preferred_variables prefs constraints =
let simplified_by v arg = function
| Some (l, str) ->
Some
( l,
fun substs ->
"simplified by " ^ str ^ " with " ^ error_string_of_kid substs v ^ " = "
^ error_string_of_typ_arg substs arg
)
| None -> None
in
let original_location = function Some (l, str) -> Some (l, fun _ -> "introduced here by " ^ str) | None -> None in
let all_substs, constraints =
Util.map_split
(function
| r, NC_aux (NC_equal (A_aux (A_nexp (Nexp_aux (Nexp_var v, _)), _), rhs), _)
when has_variable prefs rhs && not (KidSet.mem v (tyvars_of_typ_arg rhs)) ->
Ok (r, v, rhs)
| r, NC_aux (NC_app (id, [A_aux (A_bool (NC_aux (NC_var v, _)), _)]), _) when string_of_id id = "not" ->
Ok (r, v, arg_bool nc_false)
| r, NC_aux (NC_var v, _) -> Ok (r, v, arg_bool nc_true)
| r, nc -> Error (r, nc)
)
constraints
in
let rename_substs, other_substs =
List.partition (function _, _, A_aux (A_nexp (Nexp_aux (Nexp_var _, _)), _) -> true | _ -> false) all_substs
in
let constraints =
List.map
(fun (r, nc) -> (r, List.fold_left (fun nc (_, v, arg) -> constraint_subst v arg nc) nc rename_substs))
constraints
in
List.map
(fun (r, orig_nc) ->
List.fold_left
(fun (rs, b, nc) (r', v, arg) ->
if KidSet.mem v (tyvars_of_constraint nc) then (simplified_by v arg r' :: rs, true, constraint_subst v arg nc)
else (rs, b, nc)
)
([original_location r], false, orig_nc)
other_substs
|> fun (r, changed, nc) -> (r, (if changed then Some orig_nc else None), constraint_simp nc)
)
constraints
let rec map_typ_arg ?(under = []) f (Typ_aux (aux, l)) =
let aux =
match aux with
| Typ_internal_unknown -> Typ_internal_unknown
| Typ_id id -> Typ_id id
| Typ_var v -> Typ_var v
| Typ_fn (typs, typ) -> Typ_fn (List.map (map_typ_arg ~under f) typs, map_typ_arg ~under f typ)
| Typ_bidir (typ1, typ2) -> Typ_bidir (map_typ_arg ~under f typ1, map_typ_arg ~under f typ2)
| Typ_tuple typs -> Typ_tuple (List.map (map_typ_arg ~under f) typs)
| Typ_app (id, args) ->
List.map
(function
| A_aux (A_typ typ, l) ->
let typ = map_typ_arg ~under f typ in
f under id (A_aux (A_typ typ, l))
| arg -> f under id arg
)
args
|> fun args -> Typ_app (id, args)
| Typ_exist (vars, nc, typ) -> Typ_exist (vars, nc, map_typ_arg ~under:((vars, nc) :: under) f typ)
in
Typ_aux (aux, l)
let simp_typ =
map_typ_arg (fun _ _ -> function
| A_aux (A_nexp nexp, l) -> A_aux (A_nexp (nexp_simp nexp), l)
| A_aux (A_bool nc, l) -> A_aux (A_bool (constraint_simp nc), l)
| arg -> arg
)
let rank_overload_errors errs =
let with_heuristic, without_heuristic =
Util.map_split
(function
| id, l, Err_instantiation_info (heuristic, err) -> Ok (id, heuristic, l, err) | id, l, err -> Error (id, l, err)
)
errs
in
match List.stable_sort (fun (_, h1, _, _) (_, h2, _, _) -> Int.compare h1 h2) with_heuristic with
| (id, heuristic, l, err) :: rest ->
let same, worse = Util.take_drop (fun (_, h, _, _) -> heuristic = h) rest in
( without_heuristic @ ((id, l, err) :: List.map (fun (id, _, l, err) -> (id, l, err)) same),
Some (List.map (fun (id, _, _, _) -> id) worse)
)
| [] -> (without_heuristic, None)
let rec overload_messages_all_same = function
| (_, l1, m1) :: (id2, l2, m2) :: rest ->
if l1 = l2 && m1 = m2 then overload_messages_all_same ((id2, l2, m2) :: rest) else None
| [(_, l1, m1)] -> Some m1
| [] -> None
let find_closest name f =
let closest = ref (Int.max_int, None) in
let distance_callback other_id =
let other_name = string_of_id other_id in
if abs (String.length name - String.length other_name) <= 2 then (
let distance = Util.levenshtein_distance ~osa:true name other_name in
let max_distance = min 4 (max 1 (String.length name - 3)) in
if distance <= max_distance && distance < fst !closest then closest := (distance, Some other_name)
)
in
f distance_callback;
snd !closest
let message_of_type_error type_error =
let open Error_format in
let have_shown_overload_help = ref false in
let preserve var f x =
let preserved = !var in
var := true;
let result = f x in
var := preserved;
result
in
let rec to_message = function
| Err_hint h -> (Seq [], Some h)
| Err_with_hint (h, err) ->
let msg, _ = to_message err in
(msg, Some h)
| Err_inner (err, l', prefix, err') ->
let msg, hint = to_message err in
if err = err' then (msg, hint)
else (
let prefix = if prefix = "" then "" else Util.(prefix ^ " " |> yellow |> clear) in
let msg', hint' = to_message err' in
(Seq [msg; Line ""; Location (prefix, hint', l', msg')], hint)
)
| Err_other str -> ((if str = "" then Seq [] else Line str), None)
| Err_function_arg (_, typ, err) ->
let msg, _ = to_message err in
(msg, Some ("checking function argument has type " ^ string_of_typ typ))
| Err_unbound_id { id; locals; have_function } ->
let name = string_of_id id in
let closest = find_closest name (fun callback -> Bindings.iter (fun other_id _ -> callback other_id) locals) in
let hint_msg = match closest with Some other_name -> ". Did you mean " ^ other_name ^ "?" | None -> "" in
if have_function then
( Seq
[
Line ("Identifier " ^ name ^ " is unbound" ^ hint_msg);
Line "";
Line ("There is a also a function " ^ name ^ " in scope.");
],
None
)
else (Line ("Identifier " ^ name ^ " is unbound" ^ hint_msg), None)
| Err_no_function_type { id; functions } ->
let name = string_of_id id in
let closest =
find_closest name (fun callback -> Bindings.iter (fun other_id _ -> callback other_id) functions)
in
let hint_msg = match closest with Some other_name -> ". Did you mean " ^ other_name ^ "?" | None -> "" in
(Line ("Function " ^ name ^ " not found" ^ hint_msg), None)
| Err_no_overloading (id, errs) ->
let overload_errs =
List.map (fun (id, l, err) -> (id, l, preserve have_shown_overload_help to_message err)) errs
in
let s =
if !have_shown_overload_help then []
else (
have_shown_overload_help := true;
[Line s]
)
in
begin
match overload_messages_all_same overload_errs with
| Some msg -> msg
| None ->
let likely, others = if !opt_explain_all_overloads then (errs, None) else rank_overload_errors errs in
let other_msg =
match others with
| None | Some [] -> []
| Some [id] ->
[Line ""; Line ("Also tried " ^ string_of_id id)]
@ extra_help "This seems less likely, use --explain-all-overloads to see full details"
| Some ids ->
[Line ""; Line ("Also tried: " ^ Util.string_of_list ", " string_of_id ids)]
@ extra_help "These seem less likely, use --explain-all-overloads to see full details"
in
let no_overloading_msg =
match likely with
| [] -> Line ("No possible overloading for " ^ string_of_id id)
| _ ->
Seq
([
Line ("No overloading for " ^ string_of_id id ^ ", tried:");
List
(List.map
(fun (id, l, err) ->
let msg, hint = to_message err in
(string_of_id id, Location ("", hint, l, msg))
)
likely
);
]
@ other_msg
)
in
(no_overloading_msg, Some (string_of_id id))
end
| Err_instantiation_info (_, err) -> to_message err
| Err_unresolved_quants (id, quants, locals, tyvars, ncs) ->
( Seq
[
Line ("Could not resolve quantifiers for " ^ string_of_id id);
Line (bullet ^ " " ^ Util.string_of_list ("\n" ^ bullet ^ " ") string_of_quant_item quants);
],
None
)
| Err_failed_constraint (check, locals, _, ncs) ->
let simplified = constraint_simp check in
begin
match simplified with
| NC_aux (NC_false, _) ->
( Line
("Failed to prove constraint: " ^ string_of_n_constraint check
^ ", as it simplifies directly to false"
),
None
)
| _ -> (Line ("Failed to prove constraint: " ^ string_of_n_constraint (constraint_simp check)), None)
end
| Err_subtype (typ1, typ2, nc, all_constraints, tyvars) ->
let nc = Option.map constraint_simp nc in
let typ1, typ2 = (simp_typ typ1, simp_typ typ2) in
let nc_vars = match nc with Some nc -> tyvars_of_constraint nc | None -> KidSet.empty in
let appear_vars =
KBindings.bindings tyvars.vars
|> List.map (fun (v, (l, _)) -> (v, l))
|> List.filter (fun (v, _) ->
KidSet.mem v (KidSet.union nc_vars (KidSet.union (tyvars_of_typ typ1) (tyvars_of_typ typ2)))
)
in
let vars = List.filter (fun (v, _) -> is_kid_generated v || has_underscore v) appear_vars in
let preferred = KidSet.of_list (List.map fst appear_vars) in
let rewritten_constraints =
List.map (fun (reason, nc) -> (reason, rewrite_equality preferred nc)) all_constraints
|> subst_preferred_variables preferred
in
let var_constraints =
List.map
(fun (v, l) ->
(v, l, List.filter (fun (_, _, nc) -> KidSet.mem v (tyvars_of_constraint nc)) rewritten_constraints)
)
(if !opt_explain_all_variables then appear_vars else vars)
in
let substs =
List.fold_left
(fun (substs, new_vars) (v, _) ->
if is_kid_generated v || has_underscore v then (
let v' = readable_name v in
if (not (KBindings.mem v' tyvars.vars)) && not (KidSet.mem v' new_vars) then
(KBindings.add v (nvar v') substs, KidSet.add v' new_vars)
else (substs, new_vars)
)
else (substs, new_vars)
)
(KBindings.empty, KidSet.empty) vars
|> fst
in
let format_var_constraint (reasons, original_nc, nc) =
if List.for_all (function None -> true | Some _ -> false) reasons || not !opt_explain_constraints then
Line ("has constraint: " ^ error_string_of_nc substs nc)
else
Seq
(Line ("has constraint " ^ error_string_of_nc substs nc)
::
( match original_nc with
| Some nc -> [Line ("original constraint was " ^ error_string_of_nc substs nc)]
| None -> []
)
@ List.filter_map
(function
| None -> None
| Some (l, hint) -> Some (Location ("", Some (hint substs), Reporting.start_loc l, Seq []))
)
reasons
)
in
let format_var_constraints = function
| [info] -> format_var_constraint info
| infos -> Seq (List.map format_var_constraint infos)
in
( Severity
( Sev_warn,
Seq
(Line (error_string_of_typ substs typ1 ^ " is not a subtype of " ^ error_string_of_typ substs typ2)
::
( match nc with
| Some nc -> [Line ("as " ^ error_string_of_nc substs nc ^ " could not be proven")]
| None -> []
)
@ List.map
(fun (v, l, ncs) ->
Seq
[
Line "";
Line ("type variable " ^ error_string_of_kid substs v ^ ":");
Location ("", Some "bound here", l, format_var_constraints ncs);
]
)
var_constraints
)
),
None
)
| Err_no_num_ident id -> (Line ("No num identifier " ^ string_of_id id), None)
| Err_not_in_scope (explanation, Some l, item_scope, into_scope, priv) ->
let suggest, in_mod, add_requires_here =
match (item_scope, into_scope) with
| None, None -> ("Try bringing the following into scope:", "", [])
| Some (item, _), None ->
(Printf.sprintf "Try requiring module %s to bring the following into scope:" item, " in " ^ item, [])
| None, Some (into, into_l) ->
( Printf.sprintf "Try bringing the following into scope for module %s:" into,
"",
[Location ("", Some "add requires here", Project.to_loc into_l, Seq [])]
)
| Some (item, _), Some (into, into_l) ->
( Printf.sprintf "Try requiring module %s to bring the following into scope for module %s:" item into,
" in " ^ item,
[
Location
( "",
Some (Printf.sprintf "add 'requires %s' within %s here" item into),
Project.to_loc into_l,
Seq []
);
]
)
in
if not priv then
( Seq
([
Line (Option.value ~default:"Not in scope" explanation);
Line "";
Line suggest;
Location ("", Some ("definition here" ^ in_mod), l, Seq []);
]
@ add_requires_here
),
None
)
else
( Seq
[
Line (Option.value ~default:"Cannot use private definition" explanation);
Line "";
Location ("", Some ("private definition here" ^ in_mod), l, Seq []);
],
None
)
| Err_not_in_scope (explanation, None, _, _, _) -> (Line (Option.value ~default:"Not in scope" explanation), None)
in
to_message type_error
let string_of_type_error err =
let open Error_format in
let b = Buffer.create 20 in
let msg, hint = message_of_type_error err in
format_message msg (buffer_formatter b);
(Buffer.contents b, hint)
let to_reporting_exn l err =
let str, hint = string_of_type_error err in
Reporting.err_typ ?hint l str
let check_defs : Env.t -> untyped_def list -> typed_def list * Env.t =
fun env defs -> try Type_check.check_defs env defs with Type_error (l, err) -> raise (to_reporting_exn l err)
let check : Env.t -> untyped_ast -> typed_ast * Env.t =
fun env defs -> try Type_check.check env defs with Type_error (l, err) -> raise (to_reporting_exn l err)