9 sig |
9 sig |
10 val infer_types: (term -> Pretty.T) -> (typ -> Pretty.T) |
10 val infer_types: (term -> Pretty.T) -> (typ -> Pretty.T) |
11 -> (string -> typ option) -> Sorts.classrel -> Sorts.arities |
11 -> (string -> typ option) -> Sorts.classrel -> Sorts.arities |
12 -> string list -> bool -> (indexname -> bool) -> term list -> typ list |
12 -> string list -> bool -> (indexname -> bool) -> term list -> typ list |
13 -> term list * typ list * (indexname * typ) list |
13 -> term list * typ list * (indexname * typ) list |
|
14 val appl_error: (term -> Pretty.T) -> (typ -> Pretty.T) |
|
15 -> string -> term -> typ -> term -> typ -> string list |
14 end; |
16 end; |
15 |
17 |
16 structure TypeInfer: TYPE_INFER = |
18 structure TypeInfer: TYPE_INFER = |
17 struct |
19 struct |
18 |
20 |
293 |
295 |
294 |
296 |
295 |
297 |
296 (** type inference **) |
298 (** type inference **) |
297 |
299 |
|
300 fun appl_error prt prT why t T u U = |
|
301 ["Type error in application: " ^ why, |
|
302 "", |
|
303 Pretty.string_of |
|
304 (Pretty.block [Pretty.str "Operator:", Pretty.brk 2, prt t, |
|
305 Pretty.str " ::", Pretty.brk 1, prT T]), |
|
306 Pretty.string_of |
|
307 (Pretty.block [Pretty.str "Operand:", Pretty.brk 3, prt u, |
|
308 Pretty.str " ::", Pretty.brk 1, prT U]), |
|
309 ""]; |
|
310 |
298 (* infer *) (*DESTRUCTIVE*) |
311 (* infer *) (*DESTRUCTIVE*) |
299 |
312 |
300 fun infer prt prT classrel arities = |
313 fun infer prt prT classrel arities = |
301 let |
314 let |
302 (* errors *) |
315 (* errors *) |
303 |
316 |
304 fun unif_failed msg = |
317 fun unif_failed msg = |
305 "Type unification failed" ^ (if msg = "" then "." else ": " ^ msg) ^ "\n"; |
318 "Type unification failed" ^ (if msg = "" then "." else ": " ^ msg) ^ "\n"; |
306 |
|
307 val str_of = Pretty.string_of; |
|
308 |
319 |
309 fun prep_output bs ts Ts = |
320 fun prep_output bs ts Ts = |
310 let |
321 let |
311 val (Ts_bTs', ts') = typs_terms_of [] PTFree "??" (Ts @ map snd bs, ts); |
322 val (Ts_bTs', ts') = typs_terms_of [] PTFree "??" (Ts @ map snd bs, ts); |
312 val len = length Ts; |
323 val len = length Ts; |
323 val ([t', u'], [T', U']) = prep_output bs [t, u] [T, U]; |
334 val ([t', u'], [T', U']) = prep_output bs [t, u] [T, U]; |
324 val why = |
335 val why = |
325 (case T' of |
336 (case T' of |
326 Type ("fun", _) => "Incompatible operand type." |
337 Type ("fun", _) => "Incompatible operand type." |
327 | _ => "Operator not of function type."); |
338 | _ => "Operator not of function type."); |
328 val text = cat_lines |
339 val text = unif_failed msg ^ |
329 [unif_failed msg, |
340 cat_lines (appl_error prt prT why t' T' u' U'); |
330 "Type error in application: " ^ why, |
|
331 "", |
|
332 str_of (Pretty.block [Pretty.str "Operator:", Pretty.brk 2, prt t', |
|
333 Pretty.str " ::", Pretty.brk 1, prT T']), |
|
334 str_of (Pretty.block [Pretty.str "Operand:", Pretty.brk 3, prt u', |
|
335 Pretty.str " ::", Pretty.brk 1, prT U']), ""]; |
|
336 in raise TYPE (text, [T', U'], [t', u']) end; |
341 in raise TYPE (text, [T', U'], [t', u']) end; |
337 |
342 |
338 fun err_constraint msg bs t T U = |
343 fun err_constraint msg bs t T U = |
339 let |
344 let |
340 val ([t'], [T', U']) = prep_output bs [t] [T, U]; |
345 val ([t'], [T', U']) = prep_output bs [t] [T, U]; |
341 val text = cat_lines |
346 val text = cat_lines |
342 [unif_failed msg, |
347 [unif_failed msg, |
343 "Cannot meet type constraint:", |
348 "Cannot meet type constraint:", "", |
344 "", |
349 Pretty.string_of |
345 str_of (Pretty.block [Pretty.str "Term:", Pretty.brk 2, prt t', |
350 (Pretty.block [Pretty.str "Term:", Pretty.brk 2, prt t', |
346 Pretty.str " ::", Pretty.brk 1, prT T']), |
351 Pretty.str " ::", Pretty.brk 1, prT T']), |
347 str_of (Pretty.block [Pretty.str "Type:", Pretty.brk 2, prT U']), ""]; |
352 Pretty.string_of |
|
353 (Pretty.block [Pretty.str "Type:", Pretty.brk 2, prT U']), ""]; |
348 in raise TYPE (text, [T', U'], [t']) end; |
354 in raise TYPE (text, [T', U'], [t']) end; |
349 |
355 |
350 |
356 |
351 (* main *) |
357 (* main *) |
352 |
358 |