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