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