| author | wenzelm | 
| Thu, 14 Feb 2008 21:33:44 +0100 | |
| changeset 26069 | 321c4ca82923 | 
| parent 25384 | 7b9dfd4774a6 | 
| child 26327 | fc8df36e2644 | 
| permissions | -rw-r--r-- | 
| 256 | 1 | (* Title: Pure/type.ML | 
| 0 | 2 | ID: $Id$ | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 3 | Author: Tobias Nipkow, Lawrence C Paulson, and Markus Wenzel | 
| 0 | 4 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 5 | Type signatures and certified types, special treatment of type vars, | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 6 | matching and unification of types, extend and merge type signatures. | 
| 0 | 7 | *) | 
| 8 | ||
| 9 | signature TYPE = | |
| 2964 | 10 | sig | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 11 | (*type signatures and certified types*) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 12 | datatype decl = | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 13 | LogicalType of int | | 
| 14989 | 14 | Abbreviation of string list * typ * bool | | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 15 | Nonterminal | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 16 | type tsig | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 17 | val rep_tsig: tsig -> | 
| 19642 | 18 |    {classes: NameSpace.T * Sorts.algebra,
 | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 19 | default: sort, | 
| 20674 | 20 | types: (decl * serial) NameSpace.table, | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 21 | log_types: string list, | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 22 | witness: (typ * sort) option} | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 23 | val empty_tsig: tsig | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 24 | val defaultS: tsig -> sort | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 25 | val logical_types: tsig -> string list | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 26 | val universal_witness: tsig -> (typ * sort) option | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 27 | val eq_sort: tsig -> sort * sort -> bool | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 28 | val subsort: tsig -> sort * sort -> bool | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 29 | val of_sort: tsig -> typ * sort -> bool | 
| 19464 | 30 | val inter_sort: tsig -> sort * sort -> sort | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 31 | val cert_class: tsig -> class -> class | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 32 | val cert_sort: tsig -> sort -> sort | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 33 | val witness_sorts: tsig -> sort list -> sort list -> (typ * sort) list | 
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 34 | type mode | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 35 | val mode_default: mode | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 36 | val mode_syntax: mode | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 37 | val mode_abbrev: mode | 
| 24484 | 38 | val get_mode: Proof.context -> mode | 
| 39 | val set_mode: mode -> Proof.context -> Proof.context | |
| 40 | val restore_mode: Proof.context -> Proof.context -> Proof.context | |
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 41 | val cert_typ_mode: mode -> tsig -> typ -> typ | 
| 14993 | 42 | val cert_typ: tsig -> typ -> typ | 
| 19464 | 43 | val arity_number: tsig -> string -> int | 
| 44 | val arity_sorts: Pretty.pp -> tsig -> string -> sort -> sort list | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 45 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 46 | (*special treatment of type vars*) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 47 | val strip_sorts: typ -> typ | 
| 24982 | 48 | val similar_types: term * term -> bool | 
| 621 | 49 | val no_tvars: typ -> typ | 
| 21116 | 50 | val varify: (string * sort) list -> term -> ((string * sort) * indexname) list * term | 
| 16289 | 51 | val freeze_thaw_type: typ -> typ * (typ -> typ) | 
| 52 | val freeze_type: typ -> typ | |
| 53 | val freeze_thaw: term -> term * (term -> term) | |
| 54 | val freeze: term -> term | |
| 2964 | 55 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 56 | (*matching and unification*) | 
| 2964 | 57 | exception TYPE_MATCH | 
| 25324 | 58 | type tyenv = (sort * typ) Vartab.table | 
| 15797 | 59 | val lookup: tyenv * (indexname * sort) -> typ option | 
| 16946 | 60 | val typ_match: tsig -> typ * typ -> tyenv -> tyenv | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 61 | val typ_instance: tsig -> typ * typ -> bool | 
| 16946 | 62 | val raw_match: typ * typ -> tyenv -> tyenv | 
| 19694 | 63 | val raw_matches: typ list * typ list -> tyenv -> tyenv | 
| 16946 | 64 | val raw_instance: typ * typ -> bool | 
| 2964 | 65 | exception TUNIFY | 
| 16946 | 66 | val unify: tsig -> typ * typ -> tyenv * int -> tyenv * int | 
| 67 | val raw_unify: typ * typ -> tyenv -> tyenv | |
| 19696 | 68 | val raw_unifys: typ list * typ list -> tyenv -> tyenv | 
| 17804 | 69 | val could_unify: typ * typ -> bool | 
| 19696 | 70 | val could_unifys: typ list * typ list -> bool | 
| 16650 | 71 | val eq_type: tyenv -> typ * typ -> bool | 
| 0 | 72 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 73 | (*extend and merge type signatures*) | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 74 | val add_class: Pretty.pp -> NameSpace.naming -> bstring * class list -> tsig -> tsig | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 75 | val hide_classes: bool -> string list -> tsig -> tsig | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 76 | val set_defsort: sort -> tsig -> tsig | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 77 | val add_types: NameSpace.naming -> (bstring * int) list -> tsig -> tsig | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 78 | val add_abbrevs: NameSpace.naming -> (string * string list * typ) list -> tsig -> tsig | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 79 | val add_nonterminals: NameSpace.naming -> string list -> tsig -> tsig | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 80 | val hide_types: bool -> string list -> tsig -> tsig | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 81 | val add_arity: Pretty.pp -> arity -> tsig -> tsig | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 82 | val add_classrel: Pretty.pp -> class * class -> tsig -> tsig | 
| 14830 
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
 wenzelm parents: 
14790diff
changeset | 83 | val merge_tsigs: Pretty.pp -> tsig * tsig -> tsig | 
| 2964 | 84 | end; | 
| 85 | ||
| 86 | structure Type: TYPE = | |
| 0 | 87 | struct | 
| 88 | ||
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 89 | (** type signatures and certified types **) | 
| 2964 | 90 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 91 | (* type declarations *) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 92 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 93 | datatype decl = | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 94 | LogicalType of int | | 
| 14989 | 95 | Abbreviation of string list * typ * bool | | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 96 | Nonterminal; | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 97 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 98 | fun str_of_decl (LogicalType _) = "logical type constructor" | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 99 | | str_of_decl (Abbreviation _) = "type abbreviation" | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 100 | | str_of_decl Nonterminal = "syntactic type"; | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 101 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 102 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 103 | (* type tsig *) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 104 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 105 | datatype tsig = | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 106 |   TSig of {
 | 
| 19642 | 107 | classes: NameSpace.T * Sorts.algebra, (*order-sorted algebra of type classes*) | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 108 | default: sort, (*default sort on input*) | 
| 20674 | 109 | types: (decl * serial) NameSpace.table, (*declared types*) | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 110 | log_types: string list, (*logical types sorted by number of arguments*) | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 111 | witness: (typ * sort) option}; (*witness for non-emptiness of strictest sort*) | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 112 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 113 | fun rep_tsig (TSig comps) = comps; | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 114 | |
| 19642 | 115 | fun make_tsig (classes, default, types, log_types, witness) = | 
| 116 |   TSig {classes = classes, default = default, types = types,
 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 117 | log_types = log_types, witness = witness}; | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 118 | |
| 19642 | 119 | fun build_tsig ((space, classes), default, types) = | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 120 | let | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 121 | val log_types = | 
| 17756 | 122 | Symtab.fold (fn (c, (LogicalType n, _)) => cons (c, n) | _ => I) (snd types) [] | 
| 123 | |> Library.sort (Library.int_ord o pairself snd) |> map fst; | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 124 | val witness = | 
| 21934 | 125 | (case Sorts.witness_sorts classes log_types [] [Sorts.minimal_classes classes] of | 
| 15531 | 126 | [w] => SOME w | _ => NONE); | 
| 19642 | 127 | in make_tsig ((space, classes), default, types, log_types, witness) end; | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 128 | |
| 19642 | 129 | fun map_tsig f (TSig {classes, default, types, log_types = _, witness = _}) =
 | 
| 130 | build_tsig (f (classes, default, types)); | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 131 | |
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 132 | val empty_tsig = | 
| 19642 | 133 | build_tsig ((NameSpace.empty, Sorts.empty_algebra), [], NameSpace.empty_table); | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 134 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 135 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 136 | (* classes and sorts *) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 137 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 138 | fun defaultS (TSig {default, ...}) = default;
 | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 139 | fun logical_types (TSig {log_types, ...}) = log_types;
 | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 140 | fun universal_witness (TSig {witness, ...}) = witness;
 | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 141 | |
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 142 | fun eq_sort (TSig {classes, ...}) = Sorts.sort_eq (#2 classes);
 | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 143 | fun subsort (TSig {classes, ...}) = Sorts.sort_le (#2 classes);
 | 
| 19642 | 144 | fun of_sort (TSig {classes, ...}) = Sorts.of_sort (#2 classes);
 | 
| 19464 | 145 | fun inter_sort (TSig {classes, ...}) = Sorts.inter_sort (#2 classes);
 | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 146 | |
| 19642 | 147 | fun cert_class (TSig {classes, ...}) = Sorts.certify_class (#2 classes);
 | 
| 148 | fun cert_sort (TSig {classes, ...}) = Sorts.certify_sort (#2 classes);
 | |
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 149 | |
| 19642 | 150 | fun witness_sorts (tsig as TSig {classes, log_types, ...}) =
 | 
| 151 | Sorts.witness_sorts (#2 classes) log_types; | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 152 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 153 | |
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 154 | (* certification mode *) | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 155 | |
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 156 | datatype mode = Mode of {normalize: bool, logical: bool};
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 157 | |
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 158 | val mode_default = Mode {normalize = true, logical = true};
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 159 | val mode_syntax = Mode {normalize = true, logical = false};
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 160 | val mode_abbrev = Mode {normalize = false, logical = false};
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 161 | |
| 24484 | 162 | structure Mode = ProofDataFun | 
| 163 | ( | |
| 164 | type T = mode; | |
| 165 | fun init _ = mode_default; | |
| 166 | ); | |
| 167 | ||
| 168 | val get_mode = Mode.get; | |
| 169 | fun set_mode mode = Mode.map (K mode); | |
| 170 | fun restore_mode ctxt = set_mode (get_mode ctxt); | |
| 171 | ||
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 172 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 173 | (* certified types *) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 174 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 175 | fun bad_nargs t = "Bad number of arguments for type constructor: " ^ quote t; | 
| 14830 
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
 wenzelm parents: 
14790diff
changeset | 176 | fun undecl_type c = "Undeclared type constructor: " ^ quote c; | 
| 
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
 wenzelm parents: 
14790diff
changeset | 177 | |
| 14998 | 178 | local | 
| 179 | ||
| 180 | fun inst_typ env (Type (c, Ts)) = Type (c, map (inst_typ env) Ts) | |
| 18957 | 181 | | inst_typ env (T as TFree (x, _)) = the_default T (AList.lookup (op =) env x) | 
| 14998 | 182 | | inst_typ _ T = T; | 
| 183 | ||
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 184 | in | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 185 | |
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 186 | fun cert_typ_mode (Mode {normalize, logical}) tsig ty =
 | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 187 | let | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 188 |     val TSig {types = (_, types), ...} = tsig;
 | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 189 | fun err msg = raise TYPE (msg, [ty], []); | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 190 | |
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 191 | val check_logical = | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 192 |       if logical then fn c => err ("Illegal occurrence of syntactic type: " ^ quote c)
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 193 | else fn _ => (); | 
| 14989 | 194 | |
| 195 | fun cert (T as Type (c, Ts)) = | |
| 196 | let | |
| 197 | val Ts' = map cert Ts; | |
| 198 | fun nargs n = if length Ts <> n then err (bad_nargs c) else (); | |
| 199 | in | |
| 17412 | 200 | (case Symtab.lookup types c of | 
| 15531 | 201 | SOME (LogicalType n, _) => (nargs n; Type (c, Ts')) | 
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 202 | | SOME (Abbreviation (vs, U, syn), _) => | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 203 | (nargs (length vs); | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 204 | if syn then check_logical c else (); | 
| 14993 | 205 | if normalize then inst_typ (vs ~~ Ts') U | 
| 14989 | 206 | else Type (c, Ts')) | 
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 207 | | SOME (Nonterminal, _) => (nargs 0; check_logical c; T) | 
| 15531 | 208 | | NONE => err (undecl_type c)) | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 209 | end | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 210 | | cert (TFree (x, S)) = TFree (x, cert_sort tsig S) | 
| 14989 | 211 | | cert (TVar (xi as (_, i), S)) = | 
| 14993 | 212 | if i < 0 then | 
| 213 |             err ("Malformed type variable: " ^ quote (Term.string_of_vname xi))
 | |
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 214 | else TVar (xi, cert_sort tsig S); | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 215 | |
| 14989 | 216 | val ty' = cert ty; | 
| 14993 | 217 | in if ty = ty' then ty else ty' end; (*avoid copying of already normal type*) | 
| 14830 
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
 wenzelm parents: 
14790diff
changeset | 218 | |
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 219 | val cert_typ = cert_typ_mode mode_default; | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 220 | |
| 14998 | 221 | end; | 
| 222 | ||
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 223 | |
| 19464 | 224 | (* type arities *) | 
| 225 | ||
| 226 | fun arity_number (TSig {types = (_, types), ...}) a =
 | |
| 227 | (case Symtab.lookup types a of | |
| 228 | SOME (LogicalType n, _) => n | |
| 229 | | _ => error (undecl_type a)); | |
| 230 | ||
| 231 | fun arity_sorts _ tsig a [] = replicate (arity_number tsig a) [] | |
| 19642 | 232 |   | arity_sorts pp (TSig {classes, ...}) a S = Sorts.mg_domain (#2 classes) a S
 | 
| 233 | handle Sorts.CLASS_ERROR err => Sorts.class_error pp err; | |
| 19464 | 234 | |
| 235 | ||
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 236 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 237 | (** special treatment of type vars **) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 238 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 239 | (* strip_sorts *) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 240 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 241 | fun strip_sorts (Type (a, Ts)) = Type (a, map strip_sorts Ts) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 242 | | strip_sorts (TFree (x, _)) = TFree (x, []) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 243 | | strip_sorts (TVar (xi, _)) = TVar (xi, []); | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 244 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 245 | |
| 25384 
7b9dfd4774a6
similar_types: uniform treatment of TFrees/TVars;
 wenzelm parents: 
25324diff
changeset | 246 | (* equivalence up to renaming of atomic types *) | 
| 24982 | 247 | |
| 248 | local | |
| 249 | ||
| 250 | fun standard_types t = | |
| 251 | let | |
| 25384 
7b9dfd4774a6
similar_types: uniform treatment of TFrees/TVars;
 wenzelm parents: 
25324diff
changeset | 252 | val Ts = fold_types (fold_atyps (insert (op =))) t []; | 
| 
7b9dfd4774a6
similar_types: uniform treatment of TFrees/TVars;
 wenzelm parents: 
25324diff
changeset | 253 | val Ts' = map (fn a => TFree (a, [])) (Name.invents Name.context Name.aT (length Ts)); | 
| 
7b9dfd4774a6
similar_types: uniform treatment of TFrees/TVars;
 wenzelm parents: 
25324diff
changeset | 254 | in map_types (map_atyps (perhaps (AList.lookup (op =) (Ts ~~ Ts')))) t end; | 
| 24982 | 255 | |
| 256 | in | |
| 257 | ||
| 258 | val similar_types = op aconv o pairself (Term.map_types strip_sorts o standard_types); | |
| 259 | ||
| 260 | end; | |
| 261 | ||
| 262 | ||
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 263 | (* no_tvars *) | 
| 621 | 264 | |
| 265 | fun no_tvars T = | |
| 12501 | 266 | (case typ_tvars T of [] => T | 
| 267 |   | vs => raise TYPE ("Illegal schematic type variable(s): " ^
 | |
| 14830 
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
 wenzelm parents: 
14790diff
changeset | 268 | commas_quote (map (Term.string_of_vname o #1) vs), [T], [])); | 
| 621 | 269 | |
| 7641 | 270 | |
| 19806 | 271 | (* varify *) | 
| 621 | 272 | |
| 21116 | 273 | fun varify fixed t = | 
| 621 | 274 | let | 
| 19305 | 275 | val fs = Term.fold_types (Term.fold_atyps | 
| 276 | (fn TFree v => if member (op =) fixed v then I else insert (op =) v | _ => I)) t []; | |
| 621 | 277 | val ixns = add_term_tvar_ixns (t, []); | 
| 20071 
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
 wenzelm parents: 
19806diff
changeset | 278 | val fmap = fs ~~ map (rpair 0) (Name.variant_list (map #1 ixns) (map fst fs)) | 
| 2964 | 279 | fun thaw (f as (a, S)) = | 
| 17184 | 280 | (case AList.lookup (op =) fmap f of | 
| 15531 | 281 | NONE => TFree f | 
| 16946 | 282 | | SOME xi => TVar (xi, S)); | 
| 21116 | 283 | in (fmap, map_types (map_type_tfree thaw) t) end; | 
| 2964 | 284 | |
| 285 | ||
| 7641 | 286 | (* freeze_thaw: freeze TVars in a term; return the "thaw" inverse *) | 
| 3411 
163f8f4a42d7
Removal of freeze_vars and thaw_vars.  New freeze_thaw
 paulson parents: 
3175diff
changeset | 287 | |
| 7641 | 288 | local | 
| 289 | ||
| 16289 | 290 | fun new_name (ix, (pairs, used)) = | 
| 20071 
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
 wenzelm parents: 
19806diff
changeset | 291 | let val v = Name.variant used (string_of_indexname ix) | 
| 16289 | 292 | in ((ix, v) :: pairs, v :: used) end; | 
| 621 | 293 | |
| 16289 | 294 | fun freeze_one alist (ix, sort) = | 
| 17184 | 295 | TFree (the (AList.lookup (op =) alist ix), sort) | 
| 15531 | 296 | handle Option => | 
| 3790 | 297 |       raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
 | 
| 2964 | 298 | |
| 17184 | 299 | fun thaw_one alist (a, sort) = TVar (the (AList.lookup (op =) alist a), sort) | 
| 16289 | 300 | handle Option => TFree (a, sort); | 
| 416 | 301 | |
| 10495 | 302 | in | 
| 303 | ||
| 304 | (*this sort of code could replace unvarifyT*) | |
| 7641 | 305 | fun freeze_thaw_type T = | 
| 306 | let | |
| 307 | val used = add_typ_tfree_names (T, []) | |
| 308 | and tvars = map #1 (add_typ_tvars (T, [])); | |
| 23178 | 309 | val (alist, _) = List.foldr new_name ([], used) tvars; | 
| 7641 | 310 | in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end; | 
| 311 | ||
| 16289 | 312 | val freeze_type = #1 o freeze_thaw_type; | 
| 313 | ||
| 3411 
163f8f4a42d7
Removal of freeze_vars and thaw_vars.  New freeze_thaw
 paulson parents: 
3175diff
changeset | 314 | fun freeze_thaw t = | 
| 7641 | 315 | let | 
| 316 | val used = it_term_types add_typ_tfree_names (t, []) | |
| 317 | and tvars = map #1 (it_term_types add_typ_tvars (t, [])); | |
| 23178 | 318 | val (alist, _) = List.foldr new_name ([], used) tvars; | 
| 7641 | 319 | in | 
| 320 | (case alist of | |
| 321 | [] => (t, fn x => x) (*nothing to do!*) | |
| 20548 
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
 wenzelm parents: 
20071diff
changeset | 322 | | _ => (map_types (map_type_tvar (freeze_one alist)) t, | 
| 
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
 wenzelm parents: 
20071diff
changeset | 323 | map_types (map_type_tfree (thaw_one (map swap alist))))) | 
| 7641 | 324 | end; | 
| 325 | ||
| 16289 | 326 | val freeze = #1 o freeze_thaw; | 
| 327 | ||
| 7641 | 328 | end; | 
| 329 | ||
| 256 | 330 | |
| 331 | ||
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 332 | (** matching and unification of types **) | 
| 8899 | 333 | |
| 15797 | 334 | type tyenv = (sort * typ) Vartab.table; | 
| 256 | 335 | |
| 15797 | 336 | fun tvar_clash ixn S S' = raise TYPE ("Type variable " ^
 | 
| 337 | quote (Term.string_of_vname ixn) ^ " has two distinct sorts", | |
| 338 | [TVar (ixn, S), TVar (ixn, S')], []); | |
| 0 | 339 | |
| 16289 | 340 | fun lookup (tye, (ixn, S)) = | 
| 17412 | 341 | (case Vartab.lookup tye ixn of | 
| 15797 | 342 | NONE => NONE | 
| 16289 | 343 | | SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S'); | 
| 344 | ||
| 0 | 345 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 346 | (* matching *) | 
| 0 | 347 | |
| 2964 | 348 | exception TYPE_MATCH; | 
| 0 | 349 | |
| 16946 | 350 | fun typ_match tsig = | 
| 2964 | 351 | let | 
| 16340 | 352 | fun match (TVar (v, S), T) subs = | 
| 15797 | 353 | (case lookup (subs, (v, S)) of | 
| 15531 | 354 | NONE => | 
| 17412 | 355 | if of_sort tsig (T, S) then Vartab.update_new (v, (S, T)) subs | 
| 14993 | 356 | else raise TYPE_MATCH | 
| 15531 | 357 | | SOME U => if U = T then subs else raise TYPE_MATCH) | 
| 16340 | 358 | | match (Type (a, Ts), Type (b, Us)) subs = | 
| 2964 | 359 | if a <> b then raise TYPE_MATCH | 
| 16885 | 360 | else matches (Ts, Us) subs | 
| 16340 | 361 | | match (TFree x, TFree y) subs = | 
| 2964 | 362 | if x = y then subs else raise TYPE_MATCH | 
| 16885 | 363 | | match _ _ = raise TYPE_MATCH | 
| 364 | and matches (T :: Ts, U :: Us) subs = matches (Ts, Us) (match (T, U) subs) | |
| 365 | | matches _ subs = subs; | |
| 16946 | 366 | in match end; | 
| 0 | 367 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 368 | fun typ_instance tsig (T, U) = | 
| 16946 | 369 | (typ_match tsig (U, T) Vartab.empty; true) handle TYPE_MATCH => false; | 
| 370 | ||
| 371 | (*purely structural matching*) | |
| 372 | fun raw_match (TVar (v, S), T) subs = | |
| 373 | (case lookup (subs, (v, S)) of | |
| 17412 | 374 | NONE => Vartab.update_new (v, (S, T)) subs | 
| 16946 | 375 | | SOME U => if U = T then subs else raise TYPE_MATCH) | 
| 376 | | raw_match (Type (a, Ts), Type (b, Us)) subs = | |
| 377 | if a <> b then raise TYPE_MATCH | |
| 378 | else raw_matches (Ts, Us) subs | |
| 379 | | raw_match (TFree x, TFree y) subs = | |
| 380 | if x = y then subs else raise TYPE_MATCH | |
| 381 | | raw_match _ _ = raise TYPE_MATCH | |
| 382 | and raw_matches (T :: Ts, U :: Us) subs = raw_matches (Ts, Us) (raw_match (T, U) subs) | |
| 19694 | 383 | | raw_matches ([], []) subs = subs | 
| 384 | | raw_matches _ _ = raise TYPE_MATCH; | |
| 16946 | 385 | |
| 386 | fun raw_instance (T, U) = | |
| 387 | (raw_match (U, T) Vartab.empty; true) handle TYPE_MATCH => false; | |
| 2964 | 388 | |
| 0 | 389 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 390 | (* unification *) | 
| 2964 | 391 | |
| 0 | 392 | exception TUNIFY; | 
| 393 | ||
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 394 | (*occurs_check*) | 
| 2964 | 395 | fun occurs v tye = | 
| 396 | let | |
| 397 | fun occ (Type (_, Ts)) = exists occ Ts | |
| 398 | | occ (TFree _) = false | |
| 15797 | 399 | | occ (TVar (w, S)) = | 
| 2964 | 400 | eq_ix (v, w) orelse | 
| 15797 | 401 | (case lookup (tye, (w, S)) of | 
| 15531 | 402 | NONE => false | 
| 403 | | SOME U => occ U); | |
| 0 | 404 | in occ end; | 
| 405 | ||
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 406 | (*chase variable assignments; if devar returns a type var then it must be unassigned*) | 
| 16885 | 407 | fun devar tye (T as TVar v) = | 
| 408 | (case lookup (tye, v) of | |
| 409 | SOME U => devar tye U | |
| 15531 | 410 | | NONE => T) | 
| 16885 | 411 | | devar tye T = T; | 
| 0 | 412 | |
| 17804 | 413 | (*order-sorted unification*) | 
| 19642 | 414 | fun unify (tsig as TSig {classes = (_, classes), ...}) TU (tyenv, maxidx) =
 | 
| 2964 | 415 | let | 
| 416 | val tyvar_count = ref maxidx; | |
| 24848 | 417 | fun gen_tyvar S = TVar ((Name.aT, inc tyvar_count), S); | 
| 2964 | 418 | |
| 19642 | 419 | fun mg_domain a S = Sorts.mg_domain classes a S | 
| 420 | handle Sorts.CLASS_ERROR _ => raise TUNIFY; | |
| 2964 | 421 | |
| 16885 | 422 | fun meet (_, []) tye = tye | 
| 423 | | meet (TVar (xi, S'), S) tye = | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 424 | if Sorts.sort_le classes (S', S) then tye | 
| 17412 | 425 | else Vartab.update_new | 
| 17221 | 426 | (xi, (S', gen_tyvar (Sorts.inter_sort classes (S', S)))) tye | 
| 16885 | 427 | | meet (TFree (_, S'), S) tye = | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 428 | if Sorts.sort_le classes (S', S) then tye | 
| 2964 | 429 | else raise TUNIFY | 
| 16885 | 430 | | meet (Type (a, Ts), S) tye = meets (Ts, mg_domain a S) tye | 
| 431 | and meets (T :: Ts, S :: Ss) tye = meets (Ts, Ss) (meet (devar tye T, S) tye) | |
| 432 | | meets _ tye = tye; | |
| 2964 | 433 | |
| 16885 | 434 | fun unif (ty1, ty2) tye = | 
| 435 | (case (devar tye ty1, devar tye ty2) of | |
| 2964 | 436 | (T as TVar (v, S1), U as TVar (w, S2)) => | 
| 15797 | 437 | if eq_ix (v, w) then | 
| 438 | if S1 = S2 then tye else tvar_clash v S1 S2 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 439 | else if Sorts.sort_le classes (S1, S2) then | 
| 17412 | 440 | Vartab.update_new (w, (S2, T)) tye | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 441 | else if Sorts.sort_le classes (S2, S1) then | 
| 17412 | 442 | Vartab.update_new (v, (S1, U)) tye | 
| 2964 | 443 | else | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 444 | let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in | 
| 17412 | 445 | Vartab.update_new (v, (S1, S)) (Vartab.update_new (w, (S2, S)) tye) | 
| 2964 | 446 | end | 
| 447 | | (TVar (v, S), T) => | |
| 448 | if occurs v tye T then raise TUNIFY | |
| 17412 | 449 | else meet (T, S) (Vartab.update_new (v, (S, T)) tye) | 
| 2964 | 450 | | (T, TVar (v, S)) => | 
| 451 | if occurs v tye T then raise TUNIFY | |
| 17412 | 452 | else meet (T, S) (Vartab.update_new (v, (S, T)) tye) | 
| 2964 | 453 | | (Type (a, Ts), Type (b, Us)) => | 
| 454 | if a <> b then raise TUNIFY | |
| 16885 | 455 | else unifs (Ts, Us) tye | 
| 456 | | (T, U) => if T = U then tye else raise TUNIFY) | |
| 457 | and unifs (T :: Ts, U :: Us) tye = unifs (Ts, Us) (unif (T, U) tye) | |
| 458 | | unifs _ tye = tye; | |
| 459 | in (unif TU tyenv, ! tyvar_count) end; | |
| 0 | 460 | |
| 16946 | 461 | (*purely structural unification*) | 
| 462 | fun raw_unify (ty1, ty2) tye = | |
| 463 | (case (devar tye ty1, devar tye ty2) of | |
| 464 | (T as TVar (v, S1), U as TVar (w, S2)) => | |
| 465 | if eq_ix (v, w) then | |
| 466 | if S1 = S2 then tye else tvar_clash v S1 S2 | |
| 17412 | 467 | else Vartab.update_new (w, (S2, T)) tye | 
| 16946 | 468 | | (TVar (v, S), T) => | 
| 469 | if occurs v tye T then raise TUNIFY | |
| 17412 | 470 | else Vartab.update_new (v, (S, T)) tye | 
| 16946 | 471 | | (T, TVar (v, S)) => | 
| 472 | if occurs v tye T then raise TUNIFY | |
| 17412 | 473 | else Vartab.update_new (v, (S, T)) tye | 
| 16946 | 474 | | (Type (a, Ts), Type (b, Us)) => | 
| 475 | if a <> b then raise TUNIFY | |
| 476 | else raw_unifys (Ts, Us) tye | |
| 477 | | (T, U) => if T = U then tye else raise TUNIFY) | |
| 478 | and raw_unifys (T :: Ts, U :: Us) tye = raw_unifys (Ts, Us) (raw_unify (T, U) tye) | |
| 19696 | 479 | | raw_unifys ([], []) tye = tye | 
| 480 | | raw_unifys _ _ = raise TUNIFY; | |
| 16946 | 481 | |
| 17804 | 482 | (*fast unification filter*) | 
| 483 | fun could_unify (Type (a, Ts), Type (b, Us)) = a = b andalso could_unifys (Ts, Us) | |
| 484 | | could_unify (TFree (a, _), TFree (b, _)) = a = b | |
| 485 | | could_unify (TVar _, _) = true | |
| 486 | | could_unify (_, TVar _) = true | |
| 487 | | could_unify _ = false | |
| 488 | and could_unifys (T :: Ts, U :: Us) = could_unify (T, U) andalso could_unifys (Ts, Us) | |
| 19696 | 489 | | could_unifys ([], []) = true | 
| 490 | | could_unifys _ = false; | |
| 450 | 491 | |
| 17804 | 492 | |
| 493 | (*equality with respect to a type environment*) | |
| 16650 | 494 | fun eq_type tye (T, T') = | 
| 16885 | 495 | (case (devar tye T, devar tye T') of | 
| 16650 | 496 | (Type (s, Ts), Type (s', Ts')) => | 
| 497 | s = s' andalso ListPair.all (eq_type tye) (Ts, Ts') | |
| 498 | | (U, U') => U = U'); | |
| 499 | ||
| 450 | 500 | |
| 0 | 501 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 502 | (** extend and merge type signatures **) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 503 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 504 | (* classes *) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 505 | |
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 506 | fun add_class pp naming (c, cs) tsig = | 
| 19642 | 507 | tsig |> map_tsig (fn ((space, classes), default, types) => | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 508 | let | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 509 | val c' = NameSpace.full naming c; | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 510 | val cs' = map (cert_class tsig) cs | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 511 | handle TYPE (msg, _, _) => error msg; | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 512 | val space' = space |> NameSpace.declare naming c'; | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 513 | val classes' = classes |> Sorts.add_class pp (c', cs'); | 
| 19642 | 514 | in ((space', classes'), default, types) end); | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 515 | |
| 19642 | 516 | fun hide_classes fully cs = map_tsig (fn ((space, classes), default, types) => | 
| 517 | ((fold (NameSpace.hide fully) cs space, classes), default, types)); | |
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 518 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 519 | |
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 520 | (* arities *) | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 521 | |
| 19642 | 522 | fun add_arity pp (t, Ss, S) tsig = tsig |> map_tsig (fn ((space, classes), default, types) => | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 523 | let | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 524 | val _ = | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 525 | (case Symtab.lookup (#2 types) t of | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 526 | SOME (LogicalType n, _) => if length Ss <> n then error (bad_nargs t) else () | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 527 |       | SOME (decl, _) => error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t)
 | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 528 | | NONE => error (undecl_type t)); | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 529 | val (Ss', S') = (map (cert_sort tsig) Ss, cert_sort tsig S) | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 530 | handle TYPE (msg, _, _) => error msg; | 
| 19642 | 531 | val classes' = classes |> Sorts.add_arities pp ((t, map (fn c' => (c', Ss')) S')); | 
| 532 | in ((space, classes'), default, types) end); | |
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 533 | |
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 534 | |
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 535 | (* classrel *) | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 536 | |
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 537 | fun add_classrel pp rel tsig = | 
| 19642 | 538 | tsig |> map_tsig (fn ((space, classes), default, types) => | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 539 | let | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 540 | val rel' = pairself (cert_class tsig) rel | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 541 | handle TYPE (msg, _, _) => error msg; | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 542 | val classes' = classes |> Sorts.add_classrel pp rel; | 
| 19642 | 543 | in ((space, classes'), default, types) end); | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 544 | |
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 545 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 546 | (* default sort *) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 547 | |
| 19642 | 548 | fun set_defsort S tsig = tsig |> map_tsig (fn (classes, _, types) => | 
| 549 | (classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types)); | |
| 0 | 550 | |
| 551 | ||
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 552 | (* types *) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 553 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 554 | local | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 555 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 556 | fun err_neg_args c = | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 557 |   error ("Negative number of arguments in type constructor declaration: " ^ quote c);
 | 
| 949 
83c588d6fee9
Changed treatment of during type inference internally generated type
 nipkow parents: 
621diff
changeset | 558 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 559 | fun err_in_decls c decl decl' = | 
| 14906 | 560 | let val s = str_of_decl decl and s' = str_of_decl decl' in | 
| 561 |     if s = s' then error ("Duplicate declaration of " ^ s ^ ": " ^ quote c)
 | |
| 14830 
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
 wenzelm parents: 
14790diff
changeset | 562 |     else error ("Conflict of " ^ s ^ " with " ^ s' ^ ": " ^ quote c)
 | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 563 | end; | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 564 | |
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 565 | fun new_decl naming (c, decl) (space, types) = | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 566 | let | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 567 | val c' = NameSpace.full naming c; | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 568 | val space' = NameSpace.declare naming c' space; | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 569 | val types' = | 
| 17412 | 570 | (case Symtab.lookup types c' of | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 571 | SOME (decl', _) => err_in_decls c' decl decl' | 
| 20674 | 572 | | NONE => Symtab.update (c', (decl, serial ())) types); | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 573 | in (space', types') end; | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 574 | |
| 17412 | 575 | fun the_decl (_, types) = fst o the o Symtab.lookup types; | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 576 | |
| 19642 | 577 | fun map_types f = map_tsig (fn (classes, default, types) => | 
| 19579 
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
 wenzelm parents: 
19530diff
changeset | 578 | let | 
| 
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
 wenzelm parents: 
19530diff
changeset | 579 | val (space', tab') = f types; | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21116diff
changeset | 580 | val _ = NameSpace.intern space' "dummy" = "dummy" orelse | 
| 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21116diff
changeset | 581 | error "Illegal declaration of dummy type"; | 
| 19642 | 582 | in (classes, default, (space', tab')) end); | 
| 2964 | 583 | |
| 14989 | 584 | fun syntactic types (Type (c, Ts)) = | 
| 17412 | 585 | (case Symtab.lookup types c of SOME (Nonterminal, _) => true | _ => false) | 
| 14989 | 586 | orelse exists (syntactic types) Ts | 
| 587 | | syntactic _ _ = false; | |
| 588 | ||
| 19579 
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
 wenzelm parents: 
19530diff
changeset | 589 | fun add_abbrev naming (a, vs, rhs) tsig = tsig |> map_types (fn types => | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 590 | let | 
| 19250 | 591 |     fun err msg = cat_error msg ("The error(s) above occurred in type abbreviation: " ^ quote a);
 | 
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 592 | val rhs' = strip_sorts (no_tvars (cert_typ_mode mode_syntax tsig rhs)) | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 593 | handle TYPE (msg, _, _) => err msg; | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 594 | in | 
| 18964 | 595 | (case duplicates (op =) vs of | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 596 | [] => [] | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 597 |     | dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
 | 
| 20951 
868120282837
gen_rem(s) abandoned in favour of remove / subtract
 haftmann parents: 
20674diff
changeset | 598 | (case subtract (op =) vs (map (#1 o #1) (typ_tvars rhs')) of | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 599 | [] => [] | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 600 |     | extras => err ("Extra variables on rhs: " ^ commas_quote extras));
 | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 601 | types |> new_decl naming (a, Abbreviation (vs, rhs', syntactic (#2 types) rhs')) | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 602 | end); | 
| 0 | 603 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 604 | in | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 605 | |
| 19579 
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
 wenzelm parents: 
19530diff
changeset | 606 | fun add_types naming ps = map_types (fold (new_decl naming) (ps |> map (fn (c, n) => | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 607 | if n < 0 then err_neg_args c else (c, LogicalType n)))); | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 608 | |
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 609 | val add_abbrevs = fold o add_abbrev; | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 610 | |
| 19579 
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
 wenzelm parents: 
19530diff
changeset | 611 | fun add_nonterminals naming = map_types o fold (new_decl naming) o map (rpair Nonterminal); | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 612 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 613 | fun merge_types (types1, types2) = | 
| 20674 | 614 | NameSpace.merge_tables (Library.eq_snd (op = : serial * serial -> bool)) (types1, types2) | 
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
23178diff
changeset | 615 | handle Symtab.DUP d => err_in_decls d (the_decl types1 d) (the_decl types2 d); | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 616 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 617 | end; | 
| 0 | 618 | |
| 19642 | 619 | fun hide_types fully cs = map_tsig (fn (classes, default, (space, types)) => | 
| 620 | (classes, default, (fold (NameSpace.hide fully) cs space, types))); | |
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 621 | |
| 3790 | 622 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 623 | (* merge type signatures *) | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 624 | |
| 14830 
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
 wenzelm parents: 
14790diff
changeset | 625 | fun merge_tsigs pp (tsig1, tsig2) = | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 626 | let | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 627 |     val (TSig {classes = (space1, classes1), default = default1, types = types1,
 | 
| 19642 | 628 | log_types = _, witness = _}) = tsig1; | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 629 |     val (TSig {classes = (space2, classes2), default = default2, types = types2,
 | 
| 19642 | 630 | log_types = _, witness = _}) = tsig2; | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 631 | |
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 632 | val space' = NameSpace.merge (space1, space2); | 
| 19642 | 633 | val classes' = Sorts.merge_algebra pp (classes1, classes2); | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 634 | val default' = Sorts.inter_sort classes' (default1, default2); | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 635 | val types' = merge_types (types1, types2); | 
| 19642 | 636 | in build_tsig ((space', classes'), default', types') end; | 
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 637 | |
| 0 | 638 | end; |