| author | haftmann | 
| Mon, 20 Jan 2025 22:15:11 +0100 | |
| changeset 81876 | ac0716ca151b | 
| parent 81521 | 1bfad73ab115 | 
| child 81991 | c61434d8558e | 
| permissions | -rw-r--r-- | 
| 256 | 1 | (* Title: Pure/type.ML | 
| 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 | 2 | Author: Tobias Nipkow, Lawrence C Paulson, and Markus Wenzel | 
| 0 | 3 | |
| 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 | 4 | 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 | 5 | matching and unification of types, extend and merge type signatures. | 
| 0 | 6 | *) | 
| 7 | ||
| 8 | signature TYPE = | |
| 2964 | 9 | sig | 
| 39288 | 10 | (*constraints*) | 
| 39290 
44e4d8dfd6bf
load type_infer.ML later -- proper context for Type_Infer.infer_types;
 wenzelm parents: 
39289diff
changeset | 11 | val mark_polymorphic: typ -> typ | 
| 39288 | 12 | val constraint: typ -> term -> term | 
| 45445 
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
 wenzelm parents: 
44116diff
changeset | 13 | val constraint_type: Proof.context -> typ -> typ | 
| 39292 
6f085332c7d3
Type_Infer.preterm: eliminated separate Constraint;
 wenzelm parents: 
39290diff
changeset | 14 | val strip_constraints: term -> term | 
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 15 | val appl_error: Proof.context -> term -> typ -> term -> typ -> string | 
| 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 | 16 | (*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 | 17 | datatype decl = | 
| 79469 | 18 | Logical_Type of int | | 
| 14989 | 19 | 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 | 20 | Nonterminal | 
| 79470 | 21 | val decl_args: decl -> int | 
| 22 | val decl_logical: decl -> 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 | 23 | type tsig | 
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 24 | val eq_tsig: tsig * tsig -> 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 | 25 | val rep_tsig: tsig -> | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33094diff
changeset | 26 |    {classes: Name_Space.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 | 27 | default: sort, | 
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33097diff
changeset | 28 | types: decl Name_Space.table, | 
| 26641 | 29 | log_types: string list} | 
| 56056 
4d46d53566e6
more efficient local theory operations, by imposing a linear change discipline on the main types/consts tables, in order to speed-up Proof_Context.transfer_syntax required for Local_Theory.raw_theory_result;
 wenzelm parents: 
56050diff
changeset | 30 | val change_base: bool -> tsig -> tsig | 
| 56139 
b7add947a6ef
more frugal recording of changes: join merely requires information from one side;
 wenzelm parents: 
56056diff
changeset | 31 | val change_ignore: 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 | 32 | val empty_tsig: tsig | 
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 33 | val class_space: tsig -> Name_Space.T | 
| 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 | 34 | 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 | 35 | 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 | 36 | 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 | 37 | 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 | 38 | val of_sort: tsig -> typ * sort -> bool | 
| 19464 | 39 | 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 | 40 | 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 | 41 | val cert_sort: tsig -> sort -> sort | 
| 36447 | 42 | val minimize_sort: tsig -> sort -> sort | 
| 79395 | 43 | val witness_sorts: tsig -> | 
| 44 | (typ * sort) list -> sort Ord_List.T -> | |
| 79394 | 45 | (typ * sort) list * sort Ord_List.T | 
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 46 | type mode | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 47 | val mode_default: mode | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 48 | val mode_syntax: mode | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 49 | val mode_abbrev: mode | 
| 24484 | 50 | val get_mode: Proof.context -> mode | 
| 51 | val set_mode: mode -> Proof.context -> Proof.context | |
| 52 | val restore_mode: Proof.context -> Proof.context -> Proof.context | |
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 53 | val type_space: tsig -> Name_Space.T | 
| 35680 | 54 | val type_alias: Name_Space.naming -> binding -> string -> tsig -> tsig | 
| 55922 
710bc66f432c
more markup for inner syntax class/type names (notably for completion);
 wenzelm parents: 
55841diff
changeset | 55 | val check_decl: Context.generic -> tsig -> | 
| 
710bc66f432c
more markup for inner syntax class/type names (notably for completion);
 wenzelm parents: 
55841diff
changeset | 56 | xstring * Position.T -> (string * Position.report list) * decl | 
| 42468 | 57 | val the_decl: tsig -> string * Position.T -> decl | 
| 79461 | 58 | val certify_typ_same: mode -> tsig -> typ Same.operation | 
| 79455 | 59 | val certify_typ: mode -> tsig -> typ -> typ | 
| 60 | val certify_types: mode -> tsig -> term -> term | |
| 19464 | 61 | val arity_number: tsig -> string -> int | 
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 62 | val arity_sorts: Context.generic -> 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 | 63 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 64 | (*special treatment of type vars*) | 
| 36621 
2fd4e2c76636
proof terms for strip_shyps, based on the version by krauss/schropp with some notable differences:
 wenzelm parents: 
36450diff
changeset | 65 | val sort_of_atyp: typ -> sort | 
| 621 | 66 | val no_tvars: typ -> typ | 
| 79134 | 67 | val varify_global_names: TFrees.set -> term -> ((string * sort) * (indexname * sort)) list | 
| 68 | val varify_global: TFrees.set -> term -> ((string * sort) * (indexname * sort)) list * term | |
| 79151 | 69 | val legacy_freezeT: term -> ((string * int) * sort -> typ option) option | 
| 33832 | 70 | val legacy_freeze_thaw_type: typ -> typ * (typ -> typ) | 
| 71 | val legacy_freeze_type: typ -> typ | |
| 72 | val legacy_freeze_thaw: term -> term * (term -> term) | |
| 73 | val legacy_freeze: term -> term | |
| 2964 | 74 | |
| 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 | 75 | (*matching and unification*) | 
| 2964 | 76 | exception TYPE_MATCH | 
| 25324 | 77 | type tyenv = (sort * typ) Vartab.table | 
| 26327 | 78 | val lookup: tyenv -> indexname * sort -> typ option | 
| 32648 
143e0b0a6b33
Correct chasing of type variable instantiations during type unification.
 paulson parents: 
32030diff
changeset | 79 | val devar: tyenv -> typ -> typ | 
| 16946 | 80 | 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 | 81 | val typ_instance: tsig -> typ * typ -> bool | 
| 16946 | 82 | val raw_match: typ * typ -> tyenv -> tyenv | 
| 19694 | 83 | val raw_matches: typ list * typ list -> tyenv -> tyenv | 
| 56050 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 84 | val could_match: typ * typ -> bool | 
| 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 85 | val could_matches: typ list * typ list -> bool | 
| 16946 | 86 | val raw_instance: typ * typ -> bool | 
| 2964 | 87 | exception TUNIFY | 
| 16946 | 88 | val unify: tsig -> typ * typ -> tyenv * int -> tyenv * int | 
| 89 | val raw_unify: typ * typ -> tyenv -> tyenv | |
| 19696 | 90 | val raw_unifys: typ list * typ list -> tyenv -> tyenv | 
| 17804 | 91 | val could_unify: typ * typ -> bool | 
| 19696 | 92 | val could_unifys: typ list * typ list -> bool | 
| 58949 
e9559088ba29
clarified name of Type.unified, to emphasize its connection to the "unify" family;
 wenzelm parents: 
58942diff
changeset | 93 | val unified: tyenv -> typ * typ -> bool | 
| 0 | 94 | |
| 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 | 95 | (*extend and merge type signatures*) | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 96 | val add_class: Context.generic -> binding * class list -> tsig -> tsig | 
| 26669 | 97 | 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 | 98 | val set_defsort: sort -> tsig -> tsig | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 99 | val add_type: Context.generic -> binding * int -> tsig -> tsig | 
| 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 100 | val add_abbrev: Context.generic -> binding * string list * typ -> tsig -> tsig | 
| 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 101 | val add_nonterminal: Context.generic -> binding -> tsig -> tsig | 
| 26669 | 102 | val hide_type: bool -> string -> tsig -> tsig | 
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 103 | val add_arity: Context.generic -> arity -> tsig -> tsig | 
| 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 104 | val add_classrel: Context.generic -> class * class -> tsig -> tsig | 
| 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 105 | val merge_tsig: Context.generic -> tsig * tsig -> tsig | 
| 2964 | 106 | end; | 
| 107 | ||
| 108 | structure Type: TYPE = | |
| 0 | 109 | struct | 
| 110 | ||
| 39288 | 111 | (** constraints **) | 
| 112 | ||
| 39290 
44e4d8dfd6bf
load type_infer.ML later -- proper context for Type_Infer.infer_types;
 wenzelm parents: 
39289diff
changeset | 113 | (*indicate polymorphic Vars*) | 
| 
44e4d8dfd6bf
load type_infer.ML later -- proper context for Type_Infer.infer_types;
 wenzelm parents: 
39289diff
changeset | 114 | fun mark_polymorphic T = Type ("_polymorphic_", [T]);
 | 
| 
44e4d8dfd6bf
load type_infer.ML later -- proper context for Type_Infer.infer_types;
 wenzelm parents: 
39289diff
changeset | 115 | |
| 39288 | 116 | fun constraint T t = | 
| 117 | if T = dummyT then t | |
| 118 |   else Const ("_type_constraint_", T --> T) $ t;
 | |
| 119 | ||
| 45445 
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
 wenzelm parents: 
44116diff
changeset | 120 | fun constraint_type ctxt T = | 
| 
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
 wenzelm parents: 
44116diff
changeset | 121 |   let fun err () = error ("Malformed internal type constraint: " ^ Syntax.string_of_typ ctxt T);
 | 
| 
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
 wenzelm parents: 
44116diff
changeset | 122 |   in (case T of Type ("fun", [A, B]) => if A = B then A else err () | _ => err ()) end;
 | 
| 
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
 wenzelm parents: 
44116diff
changeset | 123 | |
| 39292 
6f085332c7d3
Type_Infer.preterm: eliminated separate Constraint;
 wenzelm parents: 
39290diff
changeset | 124 | fun strip_constraints (Const ("_type_constraint_", _) $ t) = strip_constraints t
 | 
| 
6f085332c7d3
Type_Infer.preterm: eliminated separate Constraint;
 wenzelm parents: 
39290diff
changeset | 125 | | strip_constraints (t $ u) = strip_constraints t $ strip_constraints u | 
| 
6f085332c7d3
Type_Infer.preterm: eliminated separate Constraint;
 wenzelm parents: 
39290diff
changeset | 126 | | strip_constraints (Abs (x, T, t)) = Abs (x, T, strip_constraints t) | 
| 
6f085332c7d3
Type_Infer.preterm: eliminated separate Constraint;
 wenzelm parents: 
39290diff
changeset | 127 | | strip_constraints a = a; | 
| 
6f085332c7d3
Type_Infer.preterm: eliminated separate Constraint;
 wenzelm parents: 
39290diff
changeset | 128 | |
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 129 | fun appl_error ctxt (Const ("_type_constraint_", Type ("fun", [T, _]))) _ u U =
 | 
| 39289 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 130 | cat_lines | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 131 | ["Failed to meet type constraint:", "", | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 132 | Pretty.string_of (Pretty.block | 
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 133 | [Pretty.str "Term:", Pretty.brk 2, Syntax.pretty_term ctxt u, | 
| 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 134 | Pretty.str " ::", Pretty.brk 1, Syntax.pretty_typ ctxt U]), | 
| 39289 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 135 | Pretty.string_of (Pretty.block | 
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 136 | [Pretty.str "Type:", Pretty.brk 2, Syntax.pretty_typ ctxt T])] | 
| 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 137 | | appl_error ctxt t T u U = | 
| 39289 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 138 | cat_lines | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 139 | ["Type error in application: " ^ | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 140 | (case T of | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 141 |             Type ("fun", _) => "incompatible operand type"
 | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 142 | | _ => "operator not of function type"), | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 143 | "", | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 144 | Pretty.string_of (Pretty.block | 
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 145 | [Pretty.str "Operator:", Pretty.brk 2, Syntax.pretty_term ctxt t, | 
| 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 146 | Pretty.str " ::", Pretty.brk 1, Syntax.pretty_typ ctxt T]), | 
| 39289 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 147 | Pretty.string_of (Pretty.block | 
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 148 | [Pretty.str "Operand:", Pretty.brk 3, Syntax.pretty_term ctxt u, | 
| 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 149 | Pretty.str " ::", Pretty.brk 1, Syntax.pretty_typ ctxt U])]; | 
| 39289 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 150 | |
| 39288 | 151 | |
| 152 | ||
| 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 | 153 | (** type signatures and certified types **) | 
| 2964 | 154 | |
| 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 | 155 | (* 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 | 156 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 157 | datatype decl = | 
| 79469 | 158 | Logical_Type of int | | 
| 14989 | 159 | 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 | 160 | 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 | 161 | |
| 79469 | 162 | fun decl_args (Logical_Type n) = n | 
| 79452 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 163 | | decl_args (Abbreviation (vs, _, _)) = length vs | 
| 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 164 | | decl_args Nonterminal = 0; | 
| 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 165 | |
| 79470 | 166 | fun decl_logical (Logical_Type _) = true | 
| 167 | | decl_logical _ = false; | |
| 168 | ||
| 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 | 169 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 170 | (* 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 | 171 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 172 | 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 | 173 |   TSig of {
 | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33094diff
changeset | 174 | classes: Name_Space.T * Sorts.algebra, (*order-sorted algebra of type classes*) | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 175 | default: sort, (*default sort on input*) | 
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33097diff
changeset | 176 | types: decl Name_Space.table, (*declared types*) | 
| 26641 | 177 | 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 | 178 | |
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 179 | fun eq_tsig | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 180 |    (TSig {classes = classes1, default = default1, types = types1, log_types = _},
 | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 181 |     TSig {classes = classes2, default = default2, types = types2, log_types = _}) =
 | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 182 | pointer_eq (classes1, classes2) andalso | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 183 | default1 = default2 andalso | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 184 | pointer_eq (types1, types2); | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 185 | |
| 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 | 186 | 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 | 187 | |
| 26641 | 188 | fun make_tsig (classes, default, types, log_types) = | 
| 189 |   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 | 190 | |
| 56056 
4d46d53566e6
more efficient local theory operations, by imposing a linear change discipline on the main types/consts tables, in order to speed-up Proof_Context.transfer_syntax required for Local_Theory.raw_theory_result;
 wenzelm parents: 
56050diff
changeset | 191 | fun change_base begin (TSig {classes, default, types, log_types}) =
 | 
| 
4d46d53566e6
more efficient local theory operations, by imposing a linear change discipline on the main types/consts tables, in order to speed-up Proof_Context.transfer_syntax required for Local_Theory.raw_theory_result;
 wenzelm parents: 
56050diff
changeset | 192 | make_tsig (classes, default, Name_Space.change_base begin types, log_types); | 
| 
4d46d53566e6
more efficient local theory operations, by imposing a linear change discipline on the main types/consts tables, in order to speed-up Proof_Context.transfer_syntax required for Local_Theory.raw_theory_result;
 wenzelm parents: 
56050diff
changeset | 193 | |
| 56139 
b7add947a6ef
more frugal recording of changes: join merely requires information from one side;
 wenzelm parents: 
56056diff
changeset | 194 | fun change_ignore (TSig {classes, default, types, log_types}) =
 | 
| 
b7add947a6ef
more frugal recording of changes: join merely requires information from one side;
 wenzelm parents: 
56056diff
changeset | 195 | make_tsig (classes, default, Name_Space.change_ignore types, log_types); | 
| 
b7add947a6ef
more frugal recording of changes: join merely requires information from one side;
 wenzelm parents: 
56056diff
changeset | 196 | |
| 33094 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 197 | fun build_tsig (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 | 198 | 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 | 199 | val log_types = | 
| 79469 | 200 | Name_Space.fold_table (fn (c, Logical_Type n) => cons (c, n) | _ => I) types [] | 
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58949diff
changeset | 201 | |> Library.sort (int_ord o apply2 snd) |> map fst; | 
| 33094 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 202 | in make_tsig (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 | 203 | |
| 26641 | 204 | fun map_tsig f (TSig {classes, default, types, log_types = _}) =
 | 
| 19642 | 205 | 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 | 206 | |
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 207 | val empty_tsig = | 
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
49687diff
changeset | 208 | build_tsig ((Name_Space.empty Markup.classN, Sorts.empty_algebra), [], | 
| 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
49687diff
changeset | 209 | Name_Space.empty_table Markup.type_nameN); | 
| 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 | 210 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 211 | |
| 
0d984ee030a1
major cleanup of tsig 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 | (* 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 | 213 | |
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 214 | val class_space = #1 o #classes o rep_tsig; | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 215 | |
| 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 | 216 | 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 | 217 | 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 | 218 | |
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 219 | fun eq_sort (TSig {classes, ...}) = Sorts.sort_eq (#2 classes);
 | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 220 | fun subsort (TSig {classes, ...}) = Sorts.sort_le (#2 classes);
 | 
| 19642 | 221 | fun of_sort (TSig {classes, ...}) = Sorts.of_sort (#2 classes);
 | 
| 19464 | 222 | 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 | 223 | |
| 79447 | 224 | fun cert_class (TSig {classes, ...}) = Sorts.cert_class (#2 classes);
 | 
| 225 | fun cert_sort (TSig {classes, ...}) = Sorts.cert_sort (#2 classes);
 | |
| 45595 | 226 | |
| 36447 | 227 | fun minimize_sort (TSig {classes, ...}) = Sorts.minimize_sort (#2 classes);
 | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 228 | |
| 32784 | 229 | fun witness_sorts (TSig {classes, log_types, ...}) =
 | 
| 19642 | 230 | 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 | 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 | |
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 233 | (* certification mode *) | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 234 | |
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 235 | datatype mode = Mode of {normalize: bool, logical: bool};
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 236 | |
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 237 | val mode_default = Mode {normalize = true, logical = true};
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 238 | val mode_syntax = Mode {normalize = true, logical = false};
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 239 | val mode_abbrev = Mode {normalize = false, logical = false};
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 240 | |
| 33519 | 241 | structure Mode = Proof_Data | 
| 24484 | 242 | ( | 
| 243 | type T = mode; | |
| 244 | fun init _ = mode_default; | |
| 245 | ); | |
| 246 | ||
| 247 | val get_mode = Mode.get; | |
| 248 | fun set_mode mode = Mode.map (K mode); | |
| 249 | fun restore_mode ctxt = set_mode (get_mode ctxt); | |
| 250 | ||
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 251 | |
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 252 | (* types *) | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 253 | |
| 56025 | 254 | val type_space = Name_Space.space_of_table o #types o rep_tsig; | 
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 255 | |
| 56025 | 256 | fun type_alias naming binding name = map_tsig (fn (classes, default, types) => | 
| 77979 
a12c48fbf10f
back to more elementary concept of aliases as adhoc change of accesses, but now with "suppress" information (see also 31ea5c1f874d);
 wenzelm parents: 
77970diff
changeset | 257 | (classes, default, (Name_Space.alias_table naming binding name types))); | 
| 35680 | 258 | |
| 27313 | 259 | |
| 260 | fun undecl_type c = "Undeclared type constructor: " ^ quote c; | |
| 261 | ||
| 59884 | 262 | fun lookup_type (TSig {types, ...}) = Name_Space.lookup types;
 | 
| 27313 | 263 | |
| 55956 
94d384d621b0
reject internal term names outright, and complete consts instead;
 wenzelm parents: 
55922diff
changeset | 264 | fun check_decl context (TSig {types, ...}) (c, pos) =
 | 
| 
94d384d621b0
reject internal term names outright, and complete consts instead;
 wenzelm parents: 
55922diff
changeset | 265 | Name_Space.check_reports context types (c, [pos]); | 
| 55841 
a232c0ff3c20
prefer Name_Space.check with its builtin reports (including completion);
 wenzelm parents: 
51930diff
changeset | 266 | |
| 42468 | 267 | fun the_decl tsig (c, pos) = | 
| 35359 
3ec03a3cd9d0
provide direct access to the different kinds of type declarations;
 wenzelm parents: 
34272diff
changeset | 268 | (case lookup_type tsig c of | 
| 48992 | 269 | NONE => error (undecl_type c ^ Position.here pos) | 
| 35359 
3ec03a3cd9d0
provide direct access to the different kinds of type declarations;
 wenzelm parents: 
34272diff
changeset | 270 | | SOME decl => decl); | 
| 
3ec03a3cd9d0
provide direct access to the different kinds of type declarations;
 wenzelm parents: 
34272diff
changeset | 271 | |
| 27313 | 272 | |
| 79455 | 273 | (* certify 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 | 274 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 275 | 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 | 276 | |
| 79461 | 277 | fun certify_typ_same (Mode {normalize, logical}) 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 | 278 | let | 
| 79452 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 279 | fun err T msg = raise TYPE (msg, [T], []); | 
| 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 280 |     fun err_syntactic T c = err T ("Illegal occurrence of syntactic type: " ^ quote c);
 | 
| 14989 | 281 | |
| 79452 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 282 | fun sort S = (cert_sort tsig S; raise Same.SAME); | 
| 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 283 | fun typ (T as Type (c, args)) = | 
| 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 284 | let val decl = the_decl tsig (c, Position.none) in | 
| 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 285 | if length args <> decl_args decl then err T (bad_nargs c) | 
| 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 286 | else | 
| 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 287 | (case decl of | 
| 79469 | 288 | Logical_Type _ => Type (c, Same.map typ args) | 
| 79452 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 289 | | Abbreviation (vs, U, syntactic) => | 
| 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 290 | if syntactic andalso logical then err_syntactic T c | 
| 79468 | 291 | else if normalize then inst_typ vs args U | 
| 79452 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 292 | else Type (c, Same.map typ args) | 
| 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 293 | | Nonterminal => if logical then err_syntactic T c else raise Same.SAME) | 
| 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 | 294 | end | 
| 79452 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 295 | | typ (TFree (_, S)) = sort S | 
| 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 296 | | typ (T as TVar ((x, i), S)) = | 
| 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 297 |           if i < 0 then err T ("Malformed type variable: " ^ quote (Term.string_of_vname (x, i)))
 | 
| 79468 | 298 | else sort S | 
| 299 | and inst_typ vs args = | |
| 300 | Term_Subst.instantiateT_frees | |
| 301 | (TFrees.build (fold2 (fn v => fn T => TFrees.add ((v, []), Same.commit typ T)) vs args)); | |
| 79452 
664d0cec18fd
misc tuning and clarification: prefer Same.operation;
 wenzelm parents: 
79449diff
changeset | 302 | in typ end; | 
| 14830 
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
 wenzelm parents: 
14790diff
changeset | 303 | |
| 79461 | 304 | val certify_typ = Same.commit oo certify_typ_same; | 
| 305 | val certify_types = Term.map_types oo certify_typ_same; | |
| 79455 | 306 | |
| 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 | 307 | |
| 19464 | 308 | (* type arities *) | 
| 309 | ||
| 27302 | 310 | fun arity_number tsig a = | 
| 311 | (case lookup_type tsig a of | |
| 79469 | 312 | SOME (Logical_Type n) => n | 
| 19464 | 313 | | _ => error (undecl_type a)); | 
| 314 | ||
| 315 | fun arity_sorts _ tsig a [] = replicate (arity_number tsig a) [] | |
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 316 |   | arity_sorts context (TSig {classes, ...}) a S =
 | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 317 | Sorts.mg_domain (#2 classes) a S | 
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 318 | handle Sorts.CLASS_ERROR err => error (Sorts.class_error context err); | 
| 19464 | 319 | |
| 320 | ||
| 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 | 321 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 322 | (** 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 | 323 | |
| 36621 
2fd4e2c76636
proof terms for strip_shyps, based on the version by krauss/schropp with some notable differences:
 wenzelm parents: 
36450diff
changeset | 324 | (* sort_of_atyp *) | 
| 
2fd4e2c76636
proof terms for strip_shyps, based on the version by krauss/schropp with some notable differences:
 wenzelm parents: 
36450diff
changeset | 325 | |
| 
2fd4e2c76636
proof terms for strip_shyps, based on the version by krauss/schropp with some notable differences:
 wenzelm parents: 
36450diff
changeset | 326 | fun sort_of_atyp (TFree (_, S)) = S | 
| 
2fd4e2c76636
proof terms for strip_shyps, based on the version by krauss/schropp with some notable differences:
 wenzelm parents: 
36450diff
changeset | 327 | | sort_of_atyp (TVar (_, S)) = S | 
| 
2fd4e2c76636
proof terms for strip_shyps, based on the version by krauss/schropp with some notable differences:
 wenzelm parents: 
36450diff
changeset | 328 |   | sort_of_atyp T = raise TYPE ("sort_of_atyp", [T], []);
 | 
| 
2fd4e2c76636
proof terms for strip_shyps, based on the version by krauss/schropp with some notable differences:
 wenzelm parents: 
36450diff
changeset | 329 | |
| 
2fd4e2c76636
proof terms for strip_shyps, based on the version by krauss/schropp with some notable differences:
 wenzelm parents: 
36450diff
changeset | 330 | |
| 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 | 331 | (* no_tvars *) | 
| 621 | 332 | |
| 333 | fun no_tvars T = | |
| 79448 | 334 | (case Term.add_tvarsT T [] of | 
| 335 | [] => T | |
| 336 | | vs => | |
| 337 |       raise TYPE ("Illegal schematic type variable(s): " ^
 | |
| 338 | commas_quote (map (Term.string_of_vname o #1) (rev vs)), [T], [])); | |
| 621 | 339 | |
| 7641 | 340 | |
| 35845 
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
 wenzelm parents: 
35680diff
changeset | 341 | (* varify_global *) | 
| 621 | 342 | |
| 79134 | 343 | fun varify_global_names fixed t = | 
| 621 | 344 | let | 
| 79134 | 345 | val xs = | 
| 74234 | 346 | build (t |> (Term.fold_types o Term.fold_atyps) | 
| 74266 | 347 | (fn TFree v => if TFrees.defined fixed v then I else insert (op =) v | _ => I)); | 
| 81515 | 348 | val used = Name.build_context (t |> Term.declare_tvar_names (K true)); | 
| 81521 | 349 | val ys = Name.variants used (map #1 xs); | 
| 79134 | 350 | in map2 (fn (a, S) => fn b => ((a, S), ((b, 0), S))) xs ys end; | 
| 351 | ||
| 352 | fun varify_global fixed t = | |
| 353 | let | |
| 354 | val names = varify_global_names fixed t; | |
| 355 | val tab = TFrees.make names; | |
| 356 | fun get v = | |
| 357 | (case TFrees.lookup tab v of | |
| 79412 | 358 | NONE => raise Same.SAME | 
| 79134 | 359 | | SOME w => TVar w); | 
| 360 | in (names, (map_types o map_type_tfree) get t) end; | |
| 2964 | 361 | |
| 362 | ||
| 79151 | 363 | (* legacy_freezeT *) | 
| 364 | ||
| 365 | local | |
| 366 | ||
| 367 | fun new_name ix (pairs, used) = | |
| 368 | let val v = singleton (Name.variant_list used) (string_of_indexname ix) | |
| 369 | in ((ix, v) :: pairs, v :: used) end; | |
| 370 | ||
| 371 | fun freeze_one alist (ix, S) = | |
| 372 | AList.lookup (op =) alist ix |> Option.map (fn a => TFree (a, S)); | |
| 373 | ||
| 374 | in | |
| 375 | ||
| 376 | fun legacy_freezeT t = | |
| 377 | let | |
| 378 | val used = Term.add_tfree_names t []; | |
| 379 | val (alist, _) = fold_rev new_name (map #1 (Term.add_tvars t [])) ([], used); | |
| 380 | in if null alist then NONE else SOME (freeze_one alist) end; | |
| 381 | ||
| 382 | end; | |
| 383 | ||
| 384 | ||
| 7641 | 385 | (* 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 | 386 | |
| 7641 | 387 | local | 
| 388 | ||
| 44116 
c70257b4cb7e
avoid OldTerm operations -- with subtle changes of semantics;
 wenzelm parents: 
43552diff
changeset | 389 | fun new_name ix (pairs, used) = | 
| 43324 
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
 wenzelm parents: 
42468diff
changeset | 390 | let val v = singleton (Name.variant_list used) (string_of_indexname ix) | 
| 16289 | 391 | in ((ix, v) :: pairs, v :: used) end; | 
| 621 | 392 | |
| 16289 | 393 | fun freeze_one alist (ix, sort) = | 
| 17184 | 394 | TFree (the (AList.lookup (op =) alist ix), sort) | 
| 51930 
52fd62618631
prefer explicitly qualified exceptions, which is particular important for robust handlers;
 wenzelm parents: 
51701diff
changeset | 395 | handle Option.Option => | 
| 3790 | 396 |       raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
 | 
| 2964 | 397 | |
| 17184 | 398 | fun thaw_one alist (a, sort) = TVar (the (AList.lookup (op =) alist a), sort) | 
| 79412 | 399 | handle Option.Option => raise Same.SAME; | 
| 416 | 400 | |
| 10495 | 401 | in | 
| 402 | ||
| 33832 | 403 | fun legacy_freeze_thaw_type T = | 
| 7641 | 404 | let | 
| 44116 
c70257b4cb7e
avoid OldTerm operations -- with subtle changes of semantics;
 wenzelm parents: 
43552diff
changeset | 405 | val used = Term.add_tfree_namesT T []; | 
| 
c70257b4cb7e
avoid OldTerm operations -- with subtle changes of semantics;
 wenzelm parents: 
43552diff
changeset | 406 | val (alist, _) = fold_rev new_name (map #1 (Term.add_tvarsT T [])) ([], used); | 
| 7641 | 407 | in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end; | 
| 408 | ||
| 33832 | 409 | val legacy_freeze_type = #1 o legacy_freeze_thaw_type; | 
| 16289 | 410 | |
| 33832 | 411 | fun legacy_freeze_thaw t = | 
| 7641 | 412 | let | 
| 44116 
c70257b4cb7e
avoid OldTerm operations -- with subtle changes of semantics;
 wenzelm parents: 
43552diff
changeset | 413 | val used = Term.add_tfree_names t []; | 
| 
c70257b4cb7e
avoid OldTerm operations -- with subtle changes of semantics;
 wenzelm parents: 
43552diff
changeset | 414 | val (alist, _) = fold_rev new_name (map #1 (Term.add_tvars t [])) ([], used); | 
| 7641 | 415 | in | 
| 416 | (case alist of | |
| 417 | [] => (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 | 418 | | _ => (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 | 419 | map_types (map_type_tfree (thaw_one (map swap alist))))) | 
| 7641 | 420 | end; | 
| 421 | ||
| 33832 | 422 | val legacy_freeze = #1 o legacy_freeze_thaw; | 
| 16289 | 423 | |
| 7641 | 424 | end; | 
| 425 | ||
| 256 | 426 | |
| 427 | ||
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 428 | (** matching and unification of types **) | 
| 8899 | 429 | |
| 15797 | 430 | type tyenv = (sort * typ) Vartab.table; | 
| 256 | 431 | |
| 51701 
1e29891759c4
tuned exceptions -- avoid composing error messages in low-level situations;
 wenzelm parents: 
50201diff
changeset | 432 | fun tvar_clash ixn S S' = | 
| 
1e29891759c4
tuned exceptions -- avoid composing error messages in low-level situations;
 wenzelm parents: 
50201diff
changeset | 433 |   raise TYPE ("Type variable has two distinct sorts", [TVar (ixn, S), TVar (ixn, S')], []);
 | 
| 0 | 434 | |
| 26327 | 435 | fun lookup tye (ixn, S) = | 
| 17412 | 436 | (case Vartab.lookup tye ixn of | 
| 15797 | 437 | NONE => NONE | 
| 16289 | 438 | | SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S'); | 
| 439 | ||
| 0 | 440 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 441 | (* matching *) | 
| 0 | 442 | |
| 2964 | 443 | exception TYPE_MATCH; | 
| 0 | 444 | |
| 16946 | 445 | fun typ_match tsig = | 
| 2964 | 446 | let | 
| 58942 
97f0ba373b1a
recovered type matching, which was broken in 8a765db7e0f8 (see also 8a765db7e0f8, 2db1d3d2ed54);
 wenzelm parents: 
56139diff
changeset | 447 | fun match (TVar (v, S), T) subs = | 
| 26327 | 448 | (case lookup subs (v, S) of | 
| 15531 | 449 | NONE => | 
| 58942 
97f0ba373b1a
recovered type matching, which was broken in 8a765db7e0f8 (see also 8a765db7e0f8, 2db1d3d2ed54);
 wenzelm parents: 
56139diff
changeset | 450 | if of_sort tsig (T, S) then Vartab.update_new (v, (S, T)) subs | 
| 14993 | 451 | else raise TYPE_MATCH | 
| 15531 | 452 | | SOME U => if U = T then subs else raise TYPE_MATCH) | 
| 16340 | 453 | | match (Type (a, Ts), Type (b, Us)) subs = | 
| 2964 | 454 | if a <> b then raise TYPE_MATCH | 
| 16885 | 455 | else matches (Ts, Us) subs | 
| 16340 | 456 | | match (TFree x, TFree y) subs = | 
| 2964 | 457 | if x = y then subs else raise TYPE_MATCH | 
| 16885 | 458 | | match _ _ = raise TYPE_MATCH | 
| 459 | and matches (T :: Ts, U :: Us) subs = matches (Ts, Us) (match (T, U) subs) | |
| 460 | | matches _ subs = subs; | |
| 16946 | 461 | in match end; | 
| 0 | 462 | |
| 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 | 463 | fun typ_instance tsig (T, U) = | 
| 74232 | 464 | (Vartab.build (typ_match tsig (U, T)); true) handle TYPE_MATCH => false; | 
| 16946 | 465 | |
| 466 | (*purely structural matching*) | |
| 58942 
97f0ba373b1a
recovered type matching, which was broken in 8a765db7e0f8 (see also 8a765db7e0f8, 2db1d3d2ed54);
 wenzelm parents: 
56139diff
changeset | 467 | fun raw_match (TVar (v, S), T) subs = | 
| 26327 | 468 | (case lookup subs (v, S) of | 
| 58942 
97f0ba373b1a
recovered type matching, which was broken in 8a765db7e0f8 (see also 8a765db7e0f8, 2db1d3d2ed54);
 wenzelm parents: 
56139diff
changeset | 469 | NONE => Vartab.update_new (v, (S, T)) subs | 
| 16946 | 470 | | SOME U => if U = T then subs else raise TYPE_MATCH) | 
| 471 | | raw_match (Type (a, Ts), Type (b, Us)) subs = | |
| 472 | if a <> b then raise TYPE_MATCH | |
| 473 | else raw_matches (Ts, Us) subs | |
| 474 | | raw_match (TFree x, TFree y) subs = | |
| 475 | if x = y then subs else raise TYPE_MATCH | |
| 476 | | raw_match _ _ = raise TYPE_MATCH | |
| 477 | and raw_matches (T :: Ts, U :: Us) subs = raw_matches (Ts, Us) (raw_match (T, U) subs) | |
| 19694 | 478 | | raw_matches ([], []) subs = subs | 
| 479 | | raw_matches _ _ = raise TYPE_MATCH; | |
| 16946 | 480 | |
| 56050 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 481 | (*fast matching filter*) | 
| 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 482 | fun could_match (Type (a, Ts), Type (b, Us)) = a = b andalso could_matches (Ts, Us) | 
| 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 483 | | could_match (TFree (a, _), TFree (b, _)) = a = b | 
| 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 484 | | could_match (TVar _, _) = true | 
| 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 485 | | could_match _ = false | 
| 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 486 | and could_matches (T :: Ts, U :: Us) = could_match (T, U) andalso could_matches (Ts, Us) | 
| 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 487 | | could_matches ([], []) = true | 
| 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 488 | | could_matches _ = false; | 
| 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 489 | |
| 16946 | 490 | fun raw_instance (T, U) = | 
| 56050 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 491 | if could_match (U, T) then | 
| 74232 | 492 | (Vartab.build (raw_match (U, T)); true) handle TYPE_MATCH => false | 
| 56050 
fdccbb97915a
minor performance tuning via fast matching filter;
 wenzelm parents: 
56025diff
changeset | 493 | else false; | 
| 2964 | 494 | |
| 0 | 495 | |
| 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 | 496 | (* unification *) | 
| 2964 | 497 | |
| 0 | 498 | exception TUNIFY; | 
| 499 | ||
| 26641 | 500 | (*occurs check*) | 
| 2964 | 501 | fun occurs v tye = | 
| 502 | let | |
| 503 | fun occ (Type (_, Ts)) = exists occ Ts | |
| 504 | | occ (TFree _) = false | |
| 15797 | 505 | | occ (TVar (w, S)) = | 
| 29269 
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
 wenzelm parents: 
29260diff
changeset | 506 | Term.eq_ix (v, w) orelse | 
| 26327 | 507 | (case lookup tye (w, S) of | 
| 15531 | 508 | NONE => false | 
| 509 | | SOME U => occ U); | |
| 0 | 510 | in occ end; | 
| 511 | ||
| 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 | 512 | (*chase variable assignments; if devar returns a type var then it must be unassigned*) | 
| 16885 | 513 | fun devar tye (T as TVar v) = | 
| 26327 | 514 | (case lookup tye v of | 
| 16885 | 515 | SOME U => devar tye U | 
| 15531 | 516 | | NONE => T) | 
| 32784 | 517 | | devar _ T = T; | 
| 0 | 518 | |
| 17804 | 519 | (*order-sorted unification*) | 
| 32784 | 520 | fun unify (TSig {classes = (_, classes), ...}) TU (tyenv, maxidx) =
 | 
| 2964 | 521 | let | 
| 32738 | 522 | val tyvar_count = Unsynchronized.ref maxidx; | 
| 523 | fun gen_tyvar S = TVar ((Name.aT, Unsynchronized.inc tyvar_count), S); | |
| 2964 | 524 | |
| 19642 | 525 | fun mg_domain a S = Sorts.mg_domain classes a S | 
| 526 | handle Sorts.CLASS_ERROR _ => raise TUNIFY; | |
| 2964 | 527 | |
| 16885 | 528 | fun meet (_, []) tye = tye | 
| 529 | | 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 | 530 | if Sorts.sort_le classes (S', S) then tye | 
| 17412 | 531 | else Vartab.update_new | 
| 17221 | 532 | (xi, (S', gen_tyvar (Sorts.inter_sort classes (S', S)))) tye | 
| 16885 | 533 | | 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 | 534 | if Sorts.sort_le classes (S', S) then tye | 
| 2964 | 535 | else raise TUNIFY | 
| 16885 | 536 | | meet (Type (a, Ts), S) tye = meets (Ts, mg_domain a S) tye | 
| 537 | and meets (T :: Ts, S :: Ss) tye = meets (Ts, Ss) (meet (devar tye T, S) tye) | |
| 538 | | meets _ tye = tye; | |
| 2964 | 539 | |
| 16885 | 540 | fun unif (ty1, ty2) tye = | 
| 541 | (case (devar tye ty1, devar tye ty2) of | |
| 2964 | 542 | (T as TVar (v, S1), U as TVar (w, S2)) => | 
| 29269 
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
 wenzelm parents: 
29260diff
changeset | 543 | if Term.eq_ix (v, w) then | 
| 15797 | 544 | 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 | 545 | else if Sorts.sort_le classes (S1, S2) then | 
| 17412 | 546 | 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 | 547 | else if Sorts.sort_le classes (S2, S1) then | 
| 17412 | 548 | Vartab.update_new (v, (S1, U)) tye | 
| 2964 | 549 | 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 | 550 | let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in | 
| 17412 | 551 | Vartab.update_new (v, (S1, S)) (Vartab.update_new (w, (S2, S)) tye) | 
| 2964 | 552 | end | 
| 553 | | (TVar (v, S), T) => | |
| 554 | if occurs v tye T then raise TUNIFY | |
| 17412 | 555 | else meet (T, S) (Vartab.update_new (v, (S, T)) tye) | 
| 2964 | 556 | | (T, TVar (v, S)) => | 
| 557 | if occurs v tye T then raise TUNIFY | |
| 17412 | 558 | else meet (T, S) (Vartab.update_new (v, (S, T)) tye) | 
| 2964 | 559 | | (Type (a, Ts), Type (b, Us)) => | 
| 560 | if a <> b then raise TUNIFY | |
| 16885 | 561 | else unifs (Ts, Us) tye | 
| 562 | | (T, U) => if T = U then tye else raise TUNIFY) | |
| 563 | and unifs (T :: Ts, U :: Us) tye = unifs (Ts, Us) (unif (T, U) tye) | |
| 564 | | unifs _ tye = tye; | |
| 565 | in (unif TU tyenv, ! tyvar_count) end; | |
| 0 | 566 | |
| 16946 | 567 | (*purely structural unification*) | 
| 568 | fun raw_unify (ty1, ty2) tye = | |
| 569 | (case (devar tye ty1, devar tye ty2) of | |
| 32784 | 570 | (T as TVar (v, S1), TVar (w, S2)) => | 
| 29269 
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
 wenzelm parents: 
29260diff
changeset | 571 | if Term.eq_ix (v, w) then | 
| 16946 | 572 | if S1 = S2 then tye else tvar_clash v S1 S2 | 
| 17412 | 573 | else Vartab.update_new (w, (S2, T)) tye | 
| 16946 | 574 | | (TVar (v, S), T) => | 
| 575 | if occurs v tye T then raise TUNIFY | |
| 17412 | 576 | else Vartab.update_new (v, (S, T)) tye | 
| 16946 | 577 | | (T, TVar (v, S)) => | 
| 578 | if occurs v tye T then raise TUNIFY | |
| 17412 | 579 | else Vartab.update_new (v, (S, T)) tye | 
| 16946 | 580 | | (Type (a, Ts), Type (b, Us)) => | 
| 581 | if a <> b then raise TUNIFY | |
| 582 | else raw_unifys (Ts, Us) tye | |
| 583 | | (T, U) => if T = U then tye else raise TUNIFY) | |
| 584 | and raw_unifys (T :: Ts, U :: Us) tye = raw_unifys (Ts, Us) (raw_unify (T, U) tye) | |
| 19696 | 585 | | raw_unifys ([], []) tye = tye | 
| 586 | | raw_unifys _ _ = raise TUNIFY; | |
| 16946 | 587 | |
| 17804 | 588 | (*fast unification filter*) | 
| 589 | fun could_unify (Type (a, Ts), Type (b, Us)) = a = b andalso could_unifys (Ts, Us) | |
| 590 | | could_unify (TFree (a, _), TFree (b, _)) = a = b | |
| 591 | | could_unify (TVar _, _) = true | |
| 592 | | could_unify (_, TVar _) = true | |
| 593 | | could_unify _ = false | |
| 594 | and could_unifys (T :: Ts, U :: Us) = could_unify (T, U) andalso could_unifys (Ts, Us) | |
| 19696 | 595 | | could_unifys ([], []) = true | 
| 596 | | could_unifys _ = false; | |
| 450 | 597 | |
| 17804 | 598 | (*equality with respect to a type environment*) | 
| 58949 
e9559088ba29
clarified name of Type.unified, to emphasize its connection to the "unify" family;
 wenzelm parents: 
58942diff
changeset | 599 | fun unified tye = | 
| 
e9559088ba29
clarified name of Type.unified, to emphasize its connection to the "unify" family;
 wenzelm parents: 
58942diff
changeset | 600 | let | 
| 
e9559088ba29
clarified name of Type.unified, to emphasize its connection to the "unify" family;
 wenzelm parents: 
58942diff
changeset | 601 | fun unif (T, T') = | 
| 
e9559088ba29
clarified name of Type.unified, to emphasize its connection to the "unify" family;
 wenzelm parents: 
58942diff
changeset | 602 | (case (devar tye T, devar tye T') of | 
| 
e9559088ba29
clarified name of Type.unified, to emphasize its connection to the "unify" family;
 wenzelm parents: 
58942diff
changeset | 603 | (Type (s, Ts), Type (s', Ts')) => s = s' andalso unifs (Ts, Ts') | 
| 
e9559088ba29
clarified name of Type.unified, to emphasize its connection to the "unify" family;
 wenzelm parents: 
58942diff
changeset | 604 | | (U, U') => U = U') | 
| 
e9559088ba29
clarified name of Type.unified, to emphasize its connection to the "unify" family;
 wenzelm parents: 
58942diff
changeset | 605 | and unifs ([], []) = true | 
| 
e9559088ba29
clarified name of Type.unified, to emphasize its connection to the "unify" family;
 wenzelm parents: 
58942diff
changeset | 606 | | unifs (T :: Ts, T' :: Ts') = unif (T', T') andalso unifs (Ts, Ts') | 
| 
e9559088ba29
clarified name of Type.unified, to emphasize its connection to the "unify" family;
 wenzelm parents: 
58942diff
changeset | 607 | | unifs _ = false; | 
| 
e9559088ba29
clarified name of Type.unified, to emphasize its connection to the "unify" family;
 wenzelm parents: 
58942diff
changeset | 608 | in if Vartab.is_empty tye then op = else unif end; | 
| 32030 | 609 | |
| 450 | 610 | |
| 0 | 611 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 612 | (** 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 | 613 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 614 | (* 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 | 615 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 616 | fun add_class context (c, cs) tsig = | 
| 19642 | 617 | tsig |> map_tsig (fn ((space, classes), default, types) => | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 618 | let | 
| 79447 | 619 | val cs' = cert_sort tsig cs handle TYPE (msg, _, _) => error msg; | 
| 41254 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
39997diff
changeset | 620 | val _ = Binding.check c; | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 621 | val (c', space') = space |> Name_Space.declare context true c; | 
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 622 | val classes' = classes |> Sorts.add_class context (c', cs'); | 
| 19642 | 623 | 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 | 624 | |
| 26669 | 625 | fun hide_class fully c = map_tsig (fn ((space, classes), default, types) => | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33094diff
changeset | 626 | ((Name_Space.hide fully c space, classes), default, types)); | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 627 | |
| 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 | 628 | |
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 629 | (* arities *) | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 630 | |
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 631 | fun add_arity context (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 | 632 | let | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 633 | val _ = | 
| 27302 | 634 | (case lookup_type tsig t of | 
| 79469 | 635 | SOME (Logical_Type n) => if length Ss <> n then error (bad_nargs t) else () | 
| 33094 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 636 |       | SOME _ => error ("Logical type constructor expected: " ^ quote t)
 | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 637 | | NONE => error (undecl_type t)); | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 638 | 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 | 639 | handle TYPE (msg, _, _) => error msg; | 
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 640 | val classes' = classes |> Sorts.add_arities context ((t, map (fn c' => (c', Ss')) S')); | 
| 19642 | 641 | in ((space, classes'), default, types) end); | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 642 | |
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 643 | |
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 644 | (* classrel *) | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 645 | |
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 646 | fun add_classrel context rel tsig = | 
| 19642 | 647 | tsig |> map_tsig (fn ((space, classes), default, types) => | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 648 | let | 
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58949diff
changeset | 649 | val rel' = apply2 (cert_class tsig) rel | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 650 | handle TYPE (msg, _, _) => error msg; | 
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 651 | val classes' = classes |> Sorts.add_classrel context rel'; | 
| 19642 | 652 | in ((space, classes'), default, types) end); | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 653 | |
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 654 | |
| 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 | 655 | (* 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 | 656 | |
| 19642 | 657 | fun set_defsort S tsig = tsig |> map_tsig (fn (classes, _, types) => | 
| 658 | (classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types)); | |
| 0 | 659 | |
| 660 | ||
| 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 | 661 | (* 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 | 662 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 663 | 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 | 664 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 665 | fun new_decl context (c, decl) types = | 
| 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 666 | (Binding.check c; #2 (Name_Space.define context true (c, decl) 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 | 667 | |
| 19642 | 668 | fun map_types f = map_tsig (fn (classes, default, types) => | 
| 19579 
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
 wenzelm parents: 
19530diff
changeset | 669 | let | 
| 56025 | 670 | val types' = f types; | 
| 671 | val _ = | |
| 59885 
3470a265d404
subtle change of long-standing name space policy: unknown entries are treated as hidden, consequently "private" is understood in the strict sense;
 wenzelm parents: 
59884diff
changeset | 672 | not (Name_Space.defined types' "dummy") orelse | 
| 56025 | 673 | Name_Space.intern (Name_Space.space_of_table types') "dummy" = "dummy" orelse | 
| 59885 
3470a265d404
subtle change of long-standing name space policy: unknown entries are treated as hidden, consequently "private" is understood in the strict sense;
 wenzelm parents: 
59884diff
changeset | 674 | error "Illegal declaration of dummy type"; | 
| 56025 | 675 | in (classes, default, types') end); | 
| 2964 | 676 | |
| 56025 | 677 | fun syntactic tsig (Type (c, Ts)) = | 
| 678 | (case lookup_type tsig c of SOME Nonterminal => true | _ => false) | |
| 679 | orelse exists (syntactic tsig) Ts | |
| 14989 | 680 | | syntactic _ _ = false; | 
| 681 | ||
| 27302 | 682 | in | 
| 683 | ||
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 684 | fun add_type context (c, n) = | 
| 42381 
309ec68442c6
added Binding.print convenience, which includes quote already;
 wenzelm parents: 
42375diff
changeset | 685 |   if n < 0 then error ("Bad type constructor declaration " ^ Binding.print c)
 | 
| 79469 | 686 | else map_types (new_decl context (c, Logical_Type n)); | 
| 27302 | 687 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 688 | fun add_abbrev context (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 | 689 | let | 
| 30343 
79f022df8527
replace old bstring by binding for logical primitives: class, type, const etc.;
 wenzelm parents: 
29275diff
changeset | 690 | fun err msg = | 
| 42381 
309ec68442c6
added Binding.print convenience, which includes quote already;
 wenzelm parents: 
42375diff
changeset | 691 |       cat_error msg ("The error(s) above occurred in type abbreviation " ^ Binding.print a);
 | 
| 79455 | 692 | val rhs' = Term.strip_sortsT (no_tvars (certify_typ 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 | 693 | handle TYPE (msg, _, _) => err msg; | 
| 33094 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 694 | val _ = | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 695 | (case duplicates (op =) vs of | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 696 | [] => [] | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 697 |       | dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
 | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 698 | val _ = | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 699 | (case subtract (op =) vs (map #1 (Term.add_tfreesT rhs' [])) of | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 700 | [] => [] | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 701 |       | extras => err ("Extra variables on rhs: " ^ commas_quote extras));
 | 
| 56025 | 702 | in types |> new_decl context (a, Abbreviation (vs, rhs', syntactic tsig rhs')) end); | 
| 0 | 703 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 704 | fun add_nonterminal context = map_types o new_decl context o 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 | 705 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 706 | end; | 
| 0 | 707 | |
| 56025 | 708 | fun hide_type fully c = map_tsig (fn (classes, default, types) => | 
| 709 | (classes, default, Name_Space.hide_table fully c types)); | |
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 710 | |
| 3790 | 711 | |
| 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 | 712 | (* 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 | 713 | |
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 714 | fun merge_tsig context (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 | 715 | let | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 716 |     val (TSig {classes = (space1, classes1), default = default1, types = types1,
 | 
| 26641 | 717 | log_types = _}) = tsig1; | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 718 |     val (TSig {classes = (space2, classes2), default = default2, types = types2,
 | 
| 26641 | 719 | 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 | 720 | |
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33094diff
changeset | 721 | val space' = Name_Space.merge (space1, space2); | 
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
59885diff
changeset | 722 | val classes' = Sorts.merge_algebra context (classes1, classes2); | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 723 | val default' = Sorts.inter_sort classes' (default1, default2); | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33094diff
changeset | 724 | val types' = Name_Space.merge_tables (types1, types2); | 
| 19642 | 725 | 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 | 726 | |
| 0 | 727 | end; |