45 val varify: term * string list -> term * (string * indexname) list |
44 val varify: term * string list -> term * (string * indexname) list |
46 val freeze_thaw_type : typ -> typ * (typ -> typ) |
45 val freeze_thaw_type : typ -> typ * (typ -> typ) |
47 val freeze_thaw : term -> term * (term -> term) |
46 val freeze_thaw : term -> term * (term -> term) |
48 |
47 |
49 (*matching and unification*) |
48 (*matching and unification*) |
50 val inst_term_tvars: tsig -> (indexname * typ) list -> term -> term |
49 val inst_typ_tvars: Pretty.pp -> tsig -> (indexname * typ) list -> typ -> typ |
51 val inst_typ_tvars: tsig -> (indexname * typ) list -> typ -> typ |
50 val inst_term_tvars: Pretty.pp -> tsig -> (indexname * typ) list -> term -> term |
52 exception TYPE_MATCH |
51 exception TYPE_MATCH |
53 val typ_match: tsig -> typ Vartab.table * (typ * typ) -> typ Vartab.table |
52 val typ_match: tsig -> typ Vartab.table * (typ * typ) -> typ Vartab.table |
54 val typ_instance: tsig -> typ * typ -> bool |
53 val typ_instance: tsig -> typ * typ -> bool |
55 exception TUNIFY |
54 exception TUNIFY |
56 val unify: tsig -> typ Vartab.table * int -> typ * typ -> typ Vartab.table * int |
55 val unify: tsig -> typ Vartab.table * int -> typ * typ -> typ Vartab.table * int |
57 val raw_unify: typ * typ -> bool |
56 val raw_unify: typ * typ -> bool |
58 |
57 |
59 (*extend and merge type signatures*) |
58 (*extend and merge type signatures*) |
60 val add_classes: (class * class list) list -> tsig -> tsig |
59 val add_classes: Pretty.pp -> (class * class list) list -> tsig -> tsig |
61 val add_classrel: (class * class) list -> tsig -> tsig |
60 val add_classrel: Pretty.pp -> (class * class) list -> tsig -> tsig |
62 val set_defsort: sort -> tsig -> tsig |
61 val set_defsort: sort -> tsig -> tsig |
63 val add_types: (string * int) list -> tsig -> tsig |
62 val add_types: (string * int) list -> tsig -> tsig |
64 val add_abbrs: (string * string list * typ) list -> tsig -> tsig |
63 val add_abbrs: (string * string list * typ) list -> tsig -> tsig |
65 val add_nonterminals: string list -> tsig -> tsig |
64 val add_nonterminals: string list -> tsig -> tsig |
66 val add_arities: (string * sort list * sort) list -> tsig -> tsig |
65 val add_arities: Pretty.pp -> arity list -> tsig -> tsig |
67 val merge_tsigs: tsig * tsig -> tsig |
66 val merge_tsigs: Pretty.pp -> tsig * tsig -> tsig |
68 end; |
67 end; |
69 |
68 |
70 structure Type: TYPE = |
69 structure Type: TYPE = |
71 struct |
70 struct |
72 |
71 |
293 |
296 |
294 (** matching and unification of types **) |
297 (** matching and unification of types **) |
295 |
298 |
296 (* instantiation *) |
299 (* instantiation *) |
297 |
300 |
298 fun type_of_sort tsig (T, S) = |
301 fun type_of_sort pp tsig (T, S) = |
299 if of_sort tsig (T, S) then T |
302 if of_sort tsig (T, S) then T |
300 else raise TYPE ("Type not of sort " ^ Sorts.str_of_sort S, [T], []); |
303 else raise TYPE ("Type not of sort " ^ Pretty.string_of_sort pp S, [T], []); |
301 |
304 |
302 fun inst_typ_tvars tsig tye = |
305 fun inst_typ_tvars pp tsig tye = |
303 let |
306 let |
304 fun inst (var as (v, S)) = |
307 fun inst (var as (v, S)) = |
305 (case assoc_string_int (tye, v) of |
308 (case assoc_string_int (tye, v) of |
306 Some U => type_of_sort tsig (U, S) |
309 Some U => type_of_sort pp tsig (U, S) |
307 | None => TVar var); |
310 | None => TVar var); |
308 in map_type_tvar inst end; |
311 in map_type_tvar inst end; |
309 |
312 |
310 fun inst_term_tvars _ [] t = t |
313 fun inst_term_tvars _ _ [] t = t |
311 | inst_term_tvars tsig tye t = map_term_types (inst_typ_tvars tsig tye) t; |
314 | inst_term_tvars pp tsig tye t = map_term_types (inst_typ_tvars pp tsig tye) t; |
312 |
315 |
313 |
316 |
314 (* matching *) |
317 (* matching *) |
315 |
318 |
316 exception TYPE_MATCH; |
319 exception TYPE_MATCH; |
317 |
320 |
318 fun typ_match tsig = |
321 fun typ_match tsig = |
319 let |
322 let |
320 fun match (subs, (TVar (v, S), T)) = |
323 fun match (subs, (TVar (v, S), T)) = |
321 (case Vartab.lookup (subs, v) of |
324 (case Vartab.lookup (subs, v) of |
322 None => (Vartab.update_new ((v, type_of_sort tsig (T, S)), subs) |
325 None => (Vartab.update_new ((v, type_of_sort Pretty.pp_undef tsig (T, S)), subs) |
323 handle TYPE _ => raise TYPE_MATCH) |
326 handle TYPE _ => raise TYPE_MATCH) |
324 | Some U => if U = T then subs else raise TYPE_MATCH) |
327 | Some U => if U = T then subs else raise TYPE_MATCH) |
325 | match (subs, (Type (a, Ts), Type (b, Us))) = |
328 | match (subs, (Type (a, Ts), Type (b, Us))) = |
326 if a <> b then raise TYPE_MATCH |
329 if a <> b then raise TYPE_MATCH |
327 else foldl match (subs, Ts ~~ Us) |
330 else foldl match (subs, Ts ~~ Us) |
415 (* arities *) |
418 (* arities *) |
416 |
419 |
417 local |
420 local |
418 |
421 |
419 fun err_decl t decl = error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t); |
422 fun err_decl t decl = error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t); |
420 fun err_undecl t = error ("Undeclared type constructor: " ^ quote t); |
423 |
421 |
424 fun for_classes _ None = "" |
422 fun err_conflict t (c1, c2) (c, Ss) (c', Ss') = |
425 | for_classes pp (Some (c1, c2)) = |
423 error ("Conflict of type arities for classes " ^ quote c1 ^ " < " ^ quote c2 ^ ":\n " ^ |
426 " for classes " ^ Pretty.string_of_classrel pp [c1, c2]; |
424 Sorts.str_of_arity (t, Ss, [c]) ^ " and\n " ^ |
427 |
425 Sorts.str_of_arity (t, Ss', [c'])); |
428 fun err_conflict pp t cc (c, Ss) (c', Ss') = |
426 |
429 error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n " ^ |
427 fun coregular C t (c, Ss) ars = |
430 Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n " ^ |
|
431 Pretty.string_of_arity pp (t, Ss', [c'])); |
|
432 |
|
433 fun coregular pp C t (c, Ss) ars = |
428 let |
434 let |
429 fun conflict (c', Ss') = |
435 fun conflict (c', Ss') = |
430 if Sorts.class_le C (c, c') andalso not (Sorts.sorts_le C (Ss, Ss')) then |
436 if Sorts.class_le C (c, c') andalso not (Sorts.sorts_le C (Ss, Ss')) then |
431 Some ((c, c'), (c', Ss')) |
437 Some ((c, c'), (c', Ss')) |
432 else if Sorts.class_le C (c', c) andalso not (Sorts.sorts_le C (Ss', Ss)) then |
438 else if Sorts.class_le C (c', c) andalso not (Sorts.sorts_le C (Ss', Ss)) then |
433 Some ((c', c), (c', Ss')) |
439 Some ((c', c), (c', Ss')) |
434 else None; |
440 else None; |
435 in |
441 in |
436 (case Library.get_first conflict ars of |
442 (case Library.get_first conflict ars of |
437 Some ((c1, c2), (c', Ss')) => err_conflict t (c1, c2) (c, Ss) (c', Ss') |
443 Some ((c1, c2), (c', Ss')) => err_conflict pp t (Some (c1, c2)) (c, Ss) (c', Ss') |
438 | None => (c, Ss) :: ars) |
444 | None => (c, Ss) :: ars) |
439 end; |
445 end; |
440 |
446 |
441 fun insert C t ((c, Ss), ars) = |
447 fun insert pp C t ((c, Ss), ars) = |
442 (case assoc_string (ars, c) of |
448 (case assoc_string (ars, c) of |
443 None => coregular C t (c, Ss) ars |
449 None => coregular pp C t (c, Ss) ars |
444 | Some Ss' => |
450 | Some Ss' => |
445 if Sorts.sorts_le C (Ss, Ss') then ars |
451 if Sorts.sorts_le C (Ss, Ss') then ars |
446 else if Sorts.sorts_le C (Ss', Ss) |
452 else if Sorts.sorts_le C (Ss', Ss) |
447 then coregular C t (c, Ss) (ars \ (c, Ss')) |
453 then coregular pp C t (c, Ss) (ars \ (c, Ss')) |
448 else coregular C t (c, Ss) ars); |
454 else err_conflict pp t None (c, Ss) (c, Ss')); |
449 |
455 |
450 fun complete C (c, Ss) = map (rpair Ss) (Graph.all_succs C [c]); |
456 fun complete C (c, Ss) = map (rpair Ss) (Graph.all_succs C [c]); |
451 |
457 |
452 fun insert_arities classes (arities, (t, ars)) = |
458 fun insert_arities pp classes (arities, (t, ars)) = |
453 let val ars' = |
459 let val ars' = |
454 Symtab.lookup_multi (arities, t) |
460 Symtab.lookup_multi (arities, t) |
455 |> curry (foldr (insert classes t)) (flat (map (complete classes) ars)) |
461 |> curry (foldr (insert pp classes t)) (flat (map (complete classes) ars)) |
456 in Symtab.update ((t, ars'), arities) end; |
462 in Symtab.update ((t, ars'), arities) end; |
457 |
463 |
458 fun insert_table classes = Symtab.foldl (fn (arities, (t, ars)) => |
464 fun insert_table pp classes = Symtab.foldl (fn (arities, (t, ars)) => |
459 insert_arities classes (arities, (t, map (apsnd (map (Sorts.norm_sort classes))) ars))); |
465 insert_arities pp classes (arities, (t, map (apsnd (map (Sorts.norm_sort classes))) ars))); |
460 |
466 |
461 in |
467 in |
462 |
468 |
463 fun add_arities decls tsig = tsig |> change_tsig (fn (classes, default, types, arities) => |
469 fun add_arities pp decls tsig = tsig |> change_tsig (fn (classes, default, types, arities) => |
464 let |
470 let |
465 fun prep (t, Ss, S) = |
471 fun prep (t, Ss, S) = |
466 (case Symtab.lookup (types, t) of |
472 (case Symtab.lookup (types, t) of |
467 Some (LogicalType n, _) => |
473 Some (LogicalType n, _) => |
468 if length Ss = n then |
474 if length Ss = n then |
469 (t, map (cert_sort tsig) Ss, cert_sort tsig S) |
475 (t, map (cert_sort tsig) Ss, cert_sort tsig S) |
470 handle TYPE (msg, _, _) => error msg |
476 handle TYPE (msg, _, _) => error msg |
471 else error (bad_nargs t) |
477 else error (bad_nargs t) |
472 | Some (decl, _) => err_decl t decl |
478 | Some (decl, _) => err_decl t decl |
473 | None => err_undecl t); |
479 | None => error (undecl_type t)); |
474 |
480 |
475 val ars = decls |> map ((fn (t, Ss, S) => (t, map (fn c => (c, Ss)) S)) o prep); |
481 val ars = decls |> map ((fn (t, Ss, S) => (t, map (fn c => (c, Ss)) S)) o prep); |
476 val arities' = foldl (insert_arities classes) (arities, ars); |
482 val arities' = foldl (insert_arities pp classes) (arities, ars); |
477 in (classes, default, types, arities') end); |
483 in (classes, default, types, arities') end); |
478 |
484 |
479 fun rebuild_arities classes arities = |
485 fun rebuild_arities pp classes arities = |
480 insert_table classes (Symtab.empty, arities); |
486 insert_table pp classes (Symtab.empty, arities); |
481 |
487 |
482 fun merge_arities classes (arities1, arities2) = |
488 fun merge_arities pp classes (arities1, arities2) = |
483 insert_table classes (insert_table classes (Symtab.empty, arities1), arities2); |
489 insert_table pp classes (insert_table pp classes (Symtab.empty, arities1), arities2); |
484 |
490 |
485 end; |
491 end; |
486 |
492 |
487 |
493 |
488 (* classes *) |
494 (* classes *) |
490 local |
496 local |
491 |
497 |
492 fun err_dup_classes cs = |
498 fun err_dup_classes cs = |
493 error ("Duplicate declaration of class(es): " ^ commas_quote cs); |
499 error ("Duplicate declaration of class(es): " ^ commas_quote cs); |
494 |
500 |
495 fun err_cyclic_classes css = |
501 fun err_cyclic_classes pp css = |
496 error (cat_lines (map (fn cs => |
502 error (cat_lines (map (fn cs => |
497 "Cycle in class relation: " ^ space_implode " < " (map quote cs)) css)); |
503 "Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css)); |
498 |
504 |
499 fun add_class (c, cs) tsig = tsig |> change_tsig (fn (classes, default, types, arities) => |
505 fun add_class pp (c, cs) tsig = tsig |> change_tsig (fn (classes, default, types, arities) => |
500 let |
506 let |
501 val cs' = map (cert_class tsig) cs |
507 val cs' = map (cert_class tsig) cs |
502 handle TYPE (msg, _, _) => error msg; |
508 handle TYPE (msg, _, _) => error msg; |
503 val classes' = classes |> Graph.new_node (c, stamp ()) |
509 val classes' = classes |> Graph.new_node (c, stamp ()) |
504 handle Graph.DUP d => err_dup_classes [d]; |
510 handle Graph.DUP d => err_dup_classes [d]; |
505 val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs') |
511 val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs') |
506 handle Graph.CYCLES css => err_cyclic_classes css; |
512 handle Graph.CYCLES css => err_cyclic_classes pp css; |
507 in (classes'', default, types, arities) end); |
513 in (classes'', default, types, arities) end); |
508 |
514 |
509 in |
515 in |
510 |
516 |
511 val add_classes = fold add_class; |
517 val add_classes = fold o add_class; |
512 |
518 |
513 fun add_classrel ps tsig = tsig |> change_tsig (fn (classes, default, types, arities) => |
519 fun add_classrel pp ps tsig = tsig |> change_tsig (fn (classes, default, types, arities) => |
514 let |
520 let |
515 val ps' = map (pairself (cert_class tsig)) ps |
521 val ps' = map (pairself (cert_class tsig)) ps |
516 handle TYPE (msg, _, _) => error msg; |
522 handle TYPE (msg, _, _) => error msg; |
517 val classes' = classes |> fold Graph.add_edge_trans_acyclic ps' |
523 val classes' = classes |> fold Graph.add_edge_trans_acyclic ps' |
518 handle Graph.CYCLES css => err_cyclic_classes css; |
524 handle Graph.CYCLES css => err_cyclic_classes pp css; |
519 val default' = default |> Sorts.norm_sort classes'; |
525 val default' = default |> Sorts.norm_sort classes'; |
520 val arities' = arities |> rebuild_arities classes'; |
526 val arities' = arities |> rebuild_arities pp classes'; |
521 in (classes', default', types, arities') end); |
527 in (classes', default', types, arities') end); |
522 |
528 |
523 fun merge_classes CC = Graph.merge_trans_acyclic (op =) CC |
529 fun merge_classes pp CC = Graph.merge_trans_acyclic (op =) CC |
524 handle Graph.DUPS cs => err_dup_classes cs |
530 handle Graph.DUPS cs => err_dup_classes cs |
525 | Graph.CYCLES css => err_cyclic_classes css; |
531 | Graph.CYCLES css => err_cyclic_classes pp css; |
526 |
532 |
527 end; |
533 end; |
528 |
534 |
529 |
535 |
530 (* default sort *) |
536 (* default sort *) |
590 end; |
597 end; |
591 |
598 |
592 |
599 |
593 (* merge type signatures *) |
600 (* merge type signatures *) |
594 |
601 |
595 fun merge_tsigs (tsig1, tsig2) = |
602 fun merge_tsigs pp (tsig1, tsig2) = |
596 let |
603 let |
597 val (TSig {classes = classes1, default = default1, types = types1, arities = arities1, |
604 val (TSig {classes = classes1, default = default1, types = types1, arities = arities1, |
598 log_types = _, witness = _}) = tsig1; |
605 log_types = _, witness = _}) = tsig1; |
599 val (TSig {classes = classes2, default = default2, types = types2, arities = arities2, |
606 val (TSig {classes = classes2, default = default2, types = types2, arities = arities2, |
600 log_types = _, witness = _}) = tsig2; |
607 log_types = _, witness = _}) = tsig2; |
601 |
608 |
602 val classes' = merge_classes (classes1, classes2); |
609 val classes' = merge_classes pp (classes1, classes2); |
603 val default' = Sorts.inter_sort classes' (default1, default2); |
610 val default' = Sorts.inter_sort classes' (default1, default2); |
604 val types' = merge_types (types1, types2); |
611 val types' = merge_types (types1, types2); |
605 val arities' = merge_arities classes' (arities1, arities2); |
612 val arities' = merge_arities pp classes' (arities1, arities2); |
606 in build_tsig (classes', default', types', arities') end; |
613 in build_tsig (classes', default', types', arities') end; |
607 |
614 |
608 end; |
615 end; |