| author | nipkow | 
| Mon, 02 Apr 2012 20:12:10 +0200 | |
| changeset 47302 | 70239da25ef6 | 
| parent 47005 | 421760a1efe7 | 
| child 48992 | 0518bf89c777 | 
| 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 = | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 18 | LogicalType 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 | 
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 21 | type tsig | 
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 22 | 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 | 23 | val rep_tsig: tsig -> | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33094diff
changeset | 24 |    {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 | 25 | default: sort, | 
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33097diff
changeset | 26 | types: decl Name_Space.table, | 
| 26641 | 27 | 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 | 28 | val empty_tsig: tsig | 
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 29 | val class_space: tsig -> Name_Space.T | 
| 35680 | 30 | val class_alias: Name_Space.naming -> binding -> string -> tsig -> tsig | 
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 31 | val intern_class: tsig -> xstring -> string | 
| 42358 
b47d41d9f4b5
Name_Space: proper configuration options long_names, short_names, unique_names instead of former unsynchronized references;
 wenzelm parents: 
41421diff
changeset | 32 | val extern_class: Proof.context -> tsig -> string -> xstring | 
| 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 | 33 | 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 | 34 | 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 | 35 | 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 | 36 | 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 | 37 | val of_sort: tsig -> typ * sort -> bool | 
| 19464 | 38 | 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 | 39 | 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 | 40 | val cert_sort: tsig -> sort -> sort | 
| 36447 | 41 | val minimize_sort: tsig -> sort -> sort | 
| 31946 
99ac0321cd47
witness_sorts: proper type witnesses for hyps, not invented "'hyp" variables;
 wenzelm parents: 
30343diff
changeset | 42 | val witness_sorts: tsig -> (typ * sort) list -> sort list -> (typ * sort) list | 
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 43 | type mode | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 44 | val mode_default: mode | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 45 | val mode_syntax: mode | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 46 | val mode_abbrev: mode | 
| 24484 | 47 | val get_mode: Proof.context -> mode | 
| 48 | val set_mode: mode -> Proof.context -> Proof.context | |
| 49 | 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 | 50 | val type_space: tsig -> Name_Space.T | 
| 35680 | 51 | val type_alias: Name_Space.naming -> binding -> string -> tsig -> tsig | 
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 52 | val intern_type: tsig -> xstring -> string | 
| 42358 
b47d41d9f4b5
Name_Space: proper configuration options long_names, short_names, unique_names instead of former unsynchronized references;
 wenzelm parents: 
41421diff
changeset | 53 | val extern_type: Proof.context -> tsig -> string -> xstring | 
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 54 | val is_logtype: tsig -> string -> bool | 
| 42468 | 55 | val the_decl: tsig -> string * Position.T -> decl | 
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 56 | val cert_typ_mode: mode -> tsig -> typ -> typ | 
| 14993 | 57 | val cert_typ: tsig -> typ -> typ | 
| 19464 | 58 | val arity_number: tsig -> string -> int | 
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 59 | val arity_sorts: Context.pretty -> 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 | 60 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 61 | (*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 | 62 | val sort_of_atyp: typ -> 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 | 63 | val strip_sorts: typ -> typ | 
| 621 | 64 | val no_tvars: typ -> typ | 
| 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 | 65 | val varify_global: (string * sort) list -> term -> ((string * sort) * indexname) list * term | 
| 33832 | 66 | val legacy_freeze_thaw_type: typ -> typ * (typ -> typ) | 
| 67 | val legacy_freeze_type: typ -> typ | |
| 68 | val legacy_freeze_thaw: term -> term * (term -> term) | |
| 69 | val legacy_freeze: term -> term | |
| 2964 | 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 | (*matching and unification*) | 
| 2964 | 72 | exception TYPE_MATCH | 
| 25324 | 73 | type tyenv = (sort * typ) Vartab.table | 
| 26327 | 74 | val lookup: tyenv -> indexname * sort -> typ option | 
| 32648 
143e0b0a6b33
Correct chasing of type variable instantiations during type unification.
 paulson parents: 
32030diff
changeset | 75 | val devar: tyenv -> typ -> typ | 
| 16946 | 76 | 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 | 77 | val typ_instance: tsig -> typ * typ -> bool | 
| 16946 | 78 | val raw_match: typ * typ -> tyenv -> tyenv | 
| 19694 | 79 | val raw_matches: typ list * typ list -> tyenv -> tyenv | 
| 16946 | 80 | val raw_instance: typ * typ -> bool | 
| 2964 | 81 | exception TUNIFY | 
| 16946 | 82 | val unify: tsig -> typ * typ -> tyenv * int -> tyenv * int | 
| 83 | val raw_unify: typ * typ -> tyenv -> tyenv | |
| 19696 | 84 | val raw_unifys: typ list * typ list -> tyenv -> tyenv | 
| 17804 | 85 | val could_unify: typ * typ -> bool | 
| 19696 | 86 | val could_unifys: typ list * typ list -> bool | 
| 16650 | 87 | val eq_type: tyenv -> typ * typ -> bool | 
| 0 | 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 | (*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 | 90 | val add_class: Context.generic -> binding * class list -> tsig -> tsig | 
| 26669 | 91 | 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 | 92 | 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 | 93 | 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 | 94 | 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 | 95 | val add_nonterminal: Context.generic -> binding -> tsig -> tsig | 
| 26669 | 96 | val hide_type: bool -> string -> tsig -> tsig | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 97 | val add_arity: Context.pretty -> arity -> tsig -> tsig | 
| 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 98 | val add_classrel: Context.pretty -> class * class -> tsig -> tsig | 
| 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 99 | val merge_tsig: Context.pretty -> tsig * tsig -> tsig | 
| 2964 | 100 | end; | 
| 101 | ||
| 102 | structure Type: TYPE = | |
| 0 | 103 | struct | 
| 104 | ||
| 39288 | 105 | (** constraints **) | 
| 106 | ||
| 39290 
44e4d8dfd6bf
load type_infer.ML later -- proper context for Type_Infer.infer_types;
 wenzelm parents: 
39289diff
changeset | 107 | (*indicate polymorphic Vars*) | 
| 
44e4d8dfd6bf
load type_infer.ML later -- proper context for Type_Infer.infer_types;
 wenzelm parents: 
39289diff
changeset | 108 | fun mark_polymorphic T = Type ("_polymorphic_", [T]);
 | 
| 
44e4d8dfd6bf
load type_infer.ML later -- proper context for Type_Infer.infer_types;
 wenzelm parents: 
39289diff
changeset | 109 | |
| 39288 | 110 | fun constraint T t = | 
| 111 | if T = dummyT then t | |
| 112 |   else Const ("_type_constraint_", T --> T) $ t;
 | |
| 113 | ||
| 45445 
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
 wenzelm parents: 
44116diff
changeset | 114 | 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 | 115 |   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 | 116 |   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 | 117 | |
| 39292 
6f085332c7d3
Type_Infer.preterm: eliminated separate Constraint;
 wenzelm parents: 
39290diff
changeset | 118 | fun strip_constraints (Const ("_type_constraint_", _) $ t) = strip_constraints t
 | 
| 
6f085332c7d3
Type_Infer.preterm: eliminated separate Constraint;
 wenzelm parents: 
39290diff
changeset | 119 | | strip_constraints (t $ u) = strip_constraints t $ strip_constraints u | 
| 
6f085332c7d3
Type_Infer.preterm: eliminated separate Constraint;
 wenzelm parents: 
39290diff
changeset | 120 | | strip_constraints (Abs (x, T, t)) = Abs (x, T, strip_constraints t) | 
| 
6f085332c7d3
Type_Infer.preterm: eliminated separate Constraint;
 wenzelm parents: 
39290diff
changeset | 121 | | strip_constraints a = a; | 
| 
6f085332c7d3
Type_Infer.preterm: eliminated separate Constraint;
 wenzelm parents: 
39290diff
changeset | 122 | |
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 123 | 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 | 124 | cat_lines | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 125 | ["Failed to meet type constraint:", "", | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 126 | Pretty.string_of (Pretty.block | 
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 127 | [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 | 128 | 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 | 129 | Pretty.string_of (Pretty.block | 
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 130 | [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 | 131 | | appl_error ctxt t T u U = | 
| 39289 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 132 | cat_lines | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 133 | ["Type error in application: " ^ | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 134 | (case T of | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 135 |             Type ("fun", _) => "incompatible operand type"
 | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 136 | | _ => "operator not of function type"), | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 137 | "", | 
| 
92b50c8bb67b
common Type.appl_error, which also covers explicit constraints;
 wenzelm parents: 
39288diff
changeset | 138 | Pretty.string_of (Pretty.block | 
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 139 | [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 | 140 | 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 | 141 | Pretty.string_of (Pretty.block | 
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 142 | [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 | 143 | 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 | 144 | |
| 39288 | 145 | |
| 146 | ||
| 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 | 147 | (** type signatures and certified types **) | 
| 2964 | 148 | |
| 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 | 149 | (* 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 | 150 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 151 | 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 | 152 | LogicalType of int | | 
| 14989 | 153 | 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 | 154 | 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 | 155 | |
| 
0d984ee030a1
major cleanup of tsig 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 | (* 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 | 158 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 159 | 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 | 160 |   TSig of {
 | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33094diff
changeset | 161 | 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 | 162 | 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 | 163 | types: decl Name_Space.table, (*declared types*) | 
| 26641 | 164 | 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 | 165 | |
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 166 | fun eq_tsig | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 167 |    (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 | 168 |     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 | 169 | pointer_eq (classes1, classes2) andalso | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 170 | default1 = default2 andalso | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 171 | pointer_eq (types1, types2); | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 172 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 173 | 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 | 174 | |
| 26641 | 175 | fun make_tsig (classes, default, types, log_types) = | 
| 176 |   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 | 177 | |
| 33094 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 178 | 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 | 179 | 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 | 180 | val log_types = | 
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33097diff
changeset | 181 | Symtab.fold (fn (c, LogicalType n) => cons (c, n) | _ => I) (snd types) [] | 
| 33094 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 182 | |> Library.sort (int_ord o pairself snd) |> map fst; | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 183 | 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 | 184 | |
| 26641 | 185 | fun map_tsig f (TSig {classes, default, types, log_types = _}) =
 | 
| 19642 | 186 | 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 | 187 | |
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 188 | val empty_tsig = | 
| 45666 | 189 | build_tsig ((Name_Space.empty Isabelle_Markup.classN, Sorts.empty_algebra), [], | 
| 46649 
bb185c45037e
clarifed name space "type name", which covers logical and non-logical types, and often occurs inside outer syntax "type" markup;
 wenzelm parents: 
45666diff
changeset | 190 | Name_Space.empty_table Isabelle_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 | 191 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 192 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 193 | (* 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 | 194 | |
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 195 | 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 | 196 | |
| 35680 | 197 | fun class_alias naming binding name = map_tsig (fn ((space, classes), default, types) => | 
| 198 | ((Name_Space.alias naming binding name space, classes), default, types)); | |
| 199 | ||
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 200 | val intern_class = Name_Space.intern o class_space; | 
| 42358 
b47d41d9f4b5
Name_Space: proper configuration options long_names, short_names, unique_names instead of former unsynchronized references;
 wenzelm parents: 
41421diff
changeset | 201 | fun extern_class ctxt = Name_Space.extern ctxt o class_space; | 
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 202 | |
| 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 | 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 | 204 | 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 | 205 | |
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 206 | fun eq_sort (TSig {classes, ...}) = Sorts.sort_eq (#2 classes);
 | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 207 | fun subsort (TSig {classes, ...}) = Sorts.sort_le (#2 classes);
 | 
| 19642 | 208 | fun of_sort (TSig {classes, ...}) = Sorts.of_sort (#2 classes);
 | 
| 19464 | 209 | 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 | 210 | |
| 45595 | 211 | fun cert_class (TSig {classes = (_, algebra), ...}) c =
 | 
| 212 | if can (Graph.get_entry (Sorts.classes_of algebra)) c then c | |
| 213 |   else raise TYPE ("Undeclared class: " ^ quote c, [], []);
 | |
| 214 | ||
| 215 | val cert_sort = map o cert_class; | |
| 216 | ||
| 36447 | 217 | 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 | 218 | |
| 32784 | 219 | fun witness_sorts (TSig {classes, log_types, ...}) =
 | 
| 19642 | 220 | 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 | 221 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 222 | |
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 223 | (* certification mode *) | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 224 | |
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 225 | datatype mode = Mode of {normalize: bool, logical: bool};
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 226 | |
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 227 | val mode_default = Mode {normalize = true, logical = true};
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 228 | val mode_syntax = Mode {normalize = true, logical = false};
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 229 | val mode_abbrev = Mode {normalize = false, logical = false};
 | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 230 | |
| 33519 | 231 | structure Mode = Proof_Data | 
| 24484 | 232 | ( | 
| 233 | type T = mode; | |
| 234 | fun init _ = mode_default; | |
| 235 | ); | |
| 236 | ||
| 237 | val get_mode = Mode.get; | |
| 238 | fun set_mode mode = Mode.map (K mode); | |
| 239 | fun restore_mode ctxt = set_mode (get_mode ctxt); | |
| 240 | ||
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 241 | |
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 242 | (* types *) | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 243 | |
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 244 | val type_space = #1 o #types o rep_tsig; | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 245 | |
| 35680 | 246 | fun type_alias naming binding name = map_tsig (fn (classes, default, (space, types)) => | 
| 247 | (classes, default, (Name_Space.alias naming binding name space, types))); | |
| 248 | ||
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 249 | val intern_type = Name_Space.intern o type_space; | 
| 42358 
b47d41d9f4b5
Name_Space: proper configuration options long_names, short_names, unique_names instead of former unsynchronized references;
 wenzelm parents: 
41421diff
changeset | 250 | fun extern_type ctxt = Name_Space.extern ctxt o type_space; | 
| 35669 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 251 | |
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 252 | val is_logtype = member (op =) o logical_types; | 
| 
a91c7ed801b8
added ProofContext.tsig_of -- proforma version for local name space only, not logical content;
 wenzelm parents: 
35359diff
changeset | 253 | |
| 27313 | 254 | |
| 255 | fun undecl_type c = "Undeclared type constructor: " ^ quote c; | |
| 256 | ||
| 33094 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 257 | fun lookup_type (TSig {types = (_, types), ...}) = Symtab.lookup types;
 | 
| 27313 | 258 | |
| 42468 | 259 | fun the_decl tsig (c, pos) = | 
| 35359 
3ec03a3cd9d0
provide direct access to the different kinds of type declarations;
 wenzelm parents: 
34272diff
changeset | 260 | (case lookup_type tsig c of | 
| 42468 | 261 | NONE => error (undecl_type c ^ Position.str_of pos) | 
| 35359 
3ec03a3cd9d0
provide direct access to the different kinds of type declarations;
 wenzelm parents: 
34272diff
changeset | 262 | | SOME decl => decl); | 
| 
3ec03a3cd9d0
provide direct access to the different kinds of type declarations;
 wenzelm parents: 
34272diff
changeset | 263 | |
| 27313 | 264 | |
| 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 | 265 | (* 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 | 266 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 267 | 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 | 268 | |
| 14998 | 269 | local | 
| 270 | ||
| 271 | fun inst_typ env (Type (c, Ts)) = Type (c, map (inst_typ env) Ts) | |
| 18957 | 272 | | inst_typ env (T as TFree (x, _)) = the_default T (AList.lookup (op =) env x) | 
| 14998 | 273 | | inst_typ _ T = T; | 
| 274 | ||
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 275 | in | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 276 | |
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 277 | 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 | 278 | 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 | 279 | 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 | 280 | |
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 281 | val check_logical = | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 282 |       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 | 283 | else fn _ => (); | 
| 14989 | 284 | |
| 285 | fun cert (T as Type (c, Ts)) = | |
| 286 | let | |
| 287 | val Ts' = map cert Ts; | |
| 288 | fun nargs n = if length Ts <> n then err (bad_nargs c) else (); | |
| 289 | in | |
| 42468 | 290 | (case the_decl tsig (c, Position.none) of | 
| 35359 
3ec03a3cd9d0
provide direct access to the different kinds of type declarations;
 wenzelm parents: 
34272diff
changeset | 291 | LogicalType n => (nargs n; Type (c, Ts')) | 
| 
3ec03a3cd9d0
provide direct access to the different kinds of type declarations;
 wenzelm parents: 
34272diff
changeset | 292 | | Abbreviation (vs, U, syn) => | 
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 293 | (nargs (length vs); | 
| 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 294 | if syn then check_logical c else (); | 
| 14993 | 295 | if normalize then inst_typ (vs ~~ Ts') U | 
| 14989 | 296 | else Type (c, Ts')) | 
| 35359 
3ec03a3cd9d0
provide direct access to the different kinds of type declarations;
 wenzelm parents: 
34272diff
changeset | 297 | | Nonterminal => (nargs 0; check_logical c; 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 | 298 | end | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 299 | | cert (TFree (x, S)) = TFree (x, cert_sort tsig S) | 
| 14989 | 300 | | cert (TVar (xi as (_, i), S)) = | 
| 14993 | 301 | if i < 0 then | 
| 302 |             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 | 303 | 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 | 304 | |
| 14989 | 305 | val ty' = cert ty; | 
| 14993 | 306 | 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 | 307 | |
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 308 | 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 | 309 | |
| 14998 | 310 | end; | 
| 311 | ||
| 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 | 312 | |
| 19464 | 313 | (* type arities *) | 
| 314 | ||
| 27302 | 315 | fun arity_number tsig a = | 
| 316 | (case lookup_type tsig a of | |
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33097diff
changeset | 317 | SOME (LogicalType n) => n | 
| 19464 | 318 | | _ => error (undecl_type a)); | 
| 319 | ||
| 320 | fun arity_sorts _ tsig a [] = replicate (arity_number tsig a) [] | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 321 |   | arity_sorts pp (TSig {classes, ...}) a S =
 | 
| 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 322 | Sorts.mg_domain (#2 classes) a S | 
| 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 323 | handle Sorts.CLASS_ERROR err => error (Sorts.class_error pp err); | 
| 19464 | 324 | |
| 325 | ||
| 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 | 326 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 327 | (** 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 | 328 | |
| 36621 
2fd4e2c76636
proof terms for strip_shyps, based on the version by krauss/schropp with some notable differences:
 wenzelm parents: 
36450diff
changeset | 329 | (* sort_of_atyp *) | 
| 
2fd4e2c76636
proof terms for strip_shyps, based on the version by krauss/schropp with some notable differences:
 wenzelm parents: 
36450diff
changeset | 330 | |
| 
2fd4e2c76636
proof terms for strip_shyps, based on the version by krauss/schropp with some notable differences:
 wenzelm parents: 
36450diff
changeset | 331 | 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 | 332 | | 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 | 333 |   | 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 | 334 | |
| 
2fd4e2c76636
proof terms for strip_shyps, based on the version by krauss/schropp with some notable differences:
 wenzelm parents: 
36450diff
changeset | 335 | |
| 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 | 336 | (* 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 | 337 | |
| 
0d984ee030a1
major cleanup of tsig 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 | 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 | 339 | | 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 | 340 | | 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 | 341 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 342 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 343 | (* no_tvars *) | 
| 621 | 344 | |
| 345 | fun no_tvars T = | |
| 29275 
9fa69e3858d6
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
 wenzelm parents: 
29269diff
changeset | 346 | (case Term.add_tvarsT T [] of [] => T | 
| 12501 | 347 |   | vs => raise TYPE ("Illegal schematic type variable(s): " ^
 | 
| 29275 
9fa69e3858d6
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
 wenzelm parents: 
29269diff
changeset | 348 | commas_quote (map (Term.string_of_vname o #1) (rev vs)), [T], [])); | 
| 621 | 349 | |
| 7641 | 350 | |
| 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 | 351 | (* varify_global *) | 
| 621 | 352 | |
| 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 | 353 | fun varify_global fixed t = | 
| 621 | 354 | let | 
| 19305 | 355 | val fs = Term.fold_types (Term.fold_atyps | 
| 356 | (fn TFree v => if member (op =) fixed v then I else insert (op =) v | _ => I)) t []; | |
| 29260 | 357 | val used = Name.context | 
| 358 | |> fold_types (fold_atyps (fn TVar ((a, _), _) => Name.declare a | _ => I)) t; | |
| 43326 
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
 wenzelm parents: 
43324diff
changeset | 359 | val fmap = fs ~~ map (rpair 0) (#1 (fold_map Name.variant (map fst fs) used)); | 
| 32784 | 360 | fun thaw (f as (_, S)) = | 
| 17184 | 361 | (case AList.lookup (op =) fmap f of | 
| 15531 | 362 | NONE => TFree f | 
| 16946 | 363 | | SOME xi => TVar (xi, S)); | 
| 21116 | 364 | in (fmap, map_types (map_type_tfree thaw) t) end; | 
| 2964 | 365 | |
| 366 | ||
| 7641 | 367 | (* 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 | 368 | |
| 7641 | 369 | local | 
| 370 | ||
| 44116 
c70257b4cb7e
avoid OldTerm operations -- with subtle changes of semantics;
 wenzelm parents: 
43552diff
changeset | 371 | fun new_name ix (pairs, used) = | 
| 43324 
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
 wenzelm parents: 
42468diff
changeset | 372 | let val v = singleton (Name.variant_list used) (string_of_indexname ix) | 
| 16289 | 373 | in ((ix, v) :: pairs, v :: used) end; | 
| 621 | 374 | |
| 16289 | 375 | fun freeze_one alist (ix, sort) = | 
| 17184 | 376 | TFree (the (AList.lookup (op =) alist ix), sort) | 
| 15531 | 377 | handle Option => | 
| 3790 | 378 |       raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
 | 
| 2964 | 379 | |
| 17184 | 380 | fun thaw_one alist (a, sort) = TVar (the (AList.lookup (op =) alist a), sort) | 
| 16289 | 381 | handle Option => TFree (a, sort); | 
| 416 | 382 | |
| 10495 | 383 | in | 
| 384 | ||
| 33832 | 385 | fun legacy_freeze_thaw_type T = | 
| 7641 | 386 | let | 
| 44116 
c70257b4cb7e
avoid OldTerm operations -- with subtle changes of semantics;
 wenzelm parents: 
43552diff
changeset | 387 | val used = Term.add_tfree_namesT T []; | 
| 
c70257b4cb7e
avoid OldTerm operations -- with subtle changes of semantics;
 wenzelm parents: 
43552diff
changeset | 388 | val (alist, _) = fold_rev new_name (map #1 (Term.add_tvarsT T [])) ([], used); | 
| 7641 | 389 | in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end; | 
| 390 | ||
| 33832 | 391 | val legacy_freeze_type = #1 o legacy_freeze_thaw_type; | 
| 16289 | 392 | |
| 33832 | 393 | fun legacy_freeze_thaw t = | 
| 7641 | 394 | let | 
| 44116 
c70257b4cb7e
avoid OldTerm operations -- with subtle changes of semantics;
 wenzelm parents: 
43552diff
changeset | 395 | val used = Term.add_tfree_names t []; | 
| 
c70257b4cb7e
avoid OldTerm operations -- with subtle changes of semantics;
 wenzelm parents: 
43552diff
changeset | 396 | val (alist, _) = fold_rev new_name (map #1 (Term.add_tvars t [])) ([], used); | 
| 7641 | 397 | in | 
| 398 | (case alist of | |
| 399 | [] => (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 | 400 | | _ => (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 | 401 | map_types (map_type_tfree (thaw_one (map swap alist))))) | 
| 7641 | 402 | end; | 
| 403 | ||
| 33832 | 404 | val legacy_freeze = #1 o legacy_freeze_thaw; | 
| 16289 | 405 | |
| 7641 | 406 | end; | 
| 407 | ||
| 256 | 408 | |
| 409 | ||
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 410 | (** matching and unification of types **) | 
| 8899 | 411 | |
| 15797 | 412 | type tyenv = (sort * typ) Vartab.table; | 
| 256 | 413 | |
| 15797 | 414 | fun tvar_clash ixn S S' = raise TYPE ("Type variable " ^
 | 
| 415 | quote (Term.string_of_vname ixn) ^ " has two distinct sorts", | |
| 416 | [TVar (ixn, S), TVar (ixn, S')], []); | |
| 0 | 417 | |
| 26327 | 418 | fun lookup tye (ixn, S) = | 
| 17412 | 419 | (case Vartab.lookup tye ixn of | 
| 15797 | 420 | NONE => NONE | 
| 16289 | 421 | | SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S'); | 
| 422 | ||
| 0 | 423 | |
| 14790 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 424 | (* matching *) | 
| 0 | 425 | |
| 2964 | 426 | exception TYPE_MATCH; | 
| 0 | 427 | |
| 16946 | 428 | fun typ_match tsig = | 
| 2964 | 429 | let | 
| 41421 
2db1d3d2ed54
uniform treatment of typ_match and raw_match (cf. b654fa27fbc4);
 wenzelm parents: 
41254diff
changeset | 430 | fun match (V as TVar (v, S), T) subs = | 
| 26327 | 431 | (case lookup subs (v, S) of | 
| 15531 | 432 | NONE => | 
| 41421 
2db1d3d2ed54
uniform treatment of typ_match and raw_match (cf. b654fa27fbc4);
 wenzelm parents: 
41254diff
changeset | 433 | if V = T then subs | 
| 
2db1d3d2ed54
uniform treatment of typ_match and raw_match (cf. b654fa27fbc4);
 wenzelm parents: 
41254diff
changeset | 434 | else if of_sort tsig (T, S) then Vartab.update_new (v, (S, T)) subs | 
| 14993 | 435 | else raise TYPE_MATCH | 
| 15531 | 436 | | SOME U => if U = T then subs else raise TYPE_MATCH) | 
| 16340 | 437 | | match (Type (a, Ts), Type (b, Us)) subs = | 
| 2964 | 438 | if a <> b then raise TYPE_MATCH | 
| 16885 | 439 | else matches (Ts, Us) subs | 
| 16340 | 440 | | match (TFree x, TFree y) subs = | 
| 2964 | 441 | if x = y then subs else raise TYPE_MATCH | 
| 16885 | 442 | | match _ _ = raise TYPE_MATCH | 
| 443 | and matches (T :: Ts, U :: Us) subs = matches (Ts, Us) (match (T, U) subs) | |
| 444 | | matches _ subs = subs; | |
| 16946 | 445 | in match end; | 
| 0 | 446 | |
| 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 | 447 | fun typ_instance tsig (T, U) = | 
| 16946 | 448 | (typ_match tsig (U, T) Vartab.empty; true) handle TYPE_MATCH => false; | 
| 449 | ||
| 450 | (*purely structural matching*) | |
| 41421 
2db1d3d2ed54
uniform treatment of typ_match and raw_match (cf. b654fa27fbc4);
 wenzelm parents: 
41254diff
changeset | 451 | fun raw_match (V as TVar (v, S), T) subs = | 
| 26327 | 452 | (case lookup subs (v, S) of | 
| 41421 
2db1d3d2ed54
uniform treatment of typ_match and raw_match (cf. b654fa27fbc4);
 wenzelm parents: 
41254diff
changeset | 453 | NONE => if V = T then subs else Vartab.update_new (v, (S, T)) subs | 
| 16946 | 454 | | SOME U => if U = T then subs else raise TYPE_MATCH) | 
| 455 | | raw_match (Type (a, Ts), Type (b, Us)) subs = | |
| 456 | if a <> b then raise TYPE_MATCH | |
| 457 | else raw_matches (Ts, Us) subs | |
| 458 | | raw_match (TFree x, TFree y) subs = | |
| 459 | if x = y then subs else raise TYPE_MATCH | |
| 460 | | raw_match _ _ = raise TYPE_MATCH | |
| 461 | and raw_matches (T :: Ts, U :: Us) subs = raw_matches (Ts, Us) (raw_match (T, U) subs) | |
| 19694 | 462 | | raw_matches ([], []) subs = subs | 
| 463 | | raw_matches _ _ = raise TYPE_MATCH; | |
| 16946 | 464 | |
| 465 | fun raw_instance (T, U) = | |
| 466 | (raw_match (U, T) Vartab.empty; true) handle TYPE_MATCH => false; | |
| 2964 | 467 | |
| 0 | 468 | |
| 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 | 469 | (* unification *) | 
| 2964 | 470 | |
| 0 | 471 | exception TUNIFY; | 
| 472 | ||
| 26641 | 473 | (*occurs check*) | 
| 2964 | 474 | fun occurs v tye = | 
| 475 | let | |
| 476 | fun occ (Type (_, Ts)) = exists occ Ts | |
| 477 | | occ (TFree _) = false | |
| 15797 | 478 | | occ (TVar (w, S)) = | 
| 29269 
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
 wenzelm parents: 
29260diff
changeset | 479 | Term.eq_ix (v, w) orelse | 
| 26327 | 480 | (case lookup tye (w, S) of | 
| 15531 | 481 | NONE => false | 
| 482 | | SOME U => occ U); | |
| 0 | 483 | in occ end; | 
| 484 | ||
| 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 | 485 | (*chase variable assignments; if devar returns a type var then it must be unassigned*) | 
| 16885 | 486 | fun devar tye (T as TVar v) = | 
| 26327 | 487 | (case lookup tye v of | 
| 16885 | 488 | SOME U => devar tye U | 
| 15531 | 489 | | NONE => T) | 
| 32784 | 490 | | devar _ T = T; | 
| 0 | 491 | |
| 17804 | 492 | (*order-sorted unification*) | 
| 32784 | 493 | fun unify (TSig {classes = (_, classes), ...}) TU (tyenv, maxidx) =
 | 
| 2964 | 494 | let | 
| 32738 | 495 | val tyvar_count = Unsynchronized.ref maxidx; | 
| 496 | fun gen_tyvar S = TVar ((Name.aT, Unsynchronized.inc tyvar_count), S); | |
| 2964 | 497 | |
| 19642 | 498 | fun mg_domain a S = Sorts.mg_domain classes a S | 
| 499 | handle Sorts.CLASS_ERROR _ => raise TUNIFY; | |
| 2964 | 500 | |
| 16885 | 501 | fun meet (_, []) tye = tye | 
| 502 | | 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 | 503 | if Sorts.sort_le classes (S', S) then tye | 
| 17412 | 504 | else Vartab.update_new | 
| 17221 | 505 | (xi, (S', gen_tyvar (Sorts.inter_sort classes (S', S)))) tye | 
| 16885 | 506 | | 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 | 507 | if Sorts.sort_le classes (S', S) then tye | 
| 2964 | 508 | else raise TUNIFY | 
| 16885 | 509 | | meet (Type (a, Ts), S) tye = meets (Ts, mg_domain a S) tye | 
| 510 | and meets (T :: Ts, S :: Ss) tye = meets (Ts, Ss) (meet (devar tye T, S) tye) | |
| 511 | | meets _ tye = tye; | |
| 2964 | 512 | |
| 16885 | 513 | fun unif (ty1, ty2) tye = | 
| 514 | (case (devar tye ty1, devar tye ty2) of | |
| 2964 | 515 | (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 | 516 | if Term.eq_ix (v, w) then | 
| 15797 | 517 | 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 | 518 | else if Sorts.sort_le classes (S1, S2) then | 
| 17412 | 519 | 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 | 520 | else if Sorts.sort_le classes (S2, S1) then | 
| 17412 | 521 | Vartab.update_new (v, (S1, U)) tye | 
| 2964 | 522 | 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 | 523 | let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in | 
| 17412 | 524 | Vartab.update_new (v, (S1, S)) (Vartab.update_new (w, (S2, S)) tye) | 
| 2964 | 525 | end | 
| 526 | | (TVar (v, S), T) => | |
| 527 | if occurs v tye T then raise TUNIFY | |
| 17412 | 528 | else meet (T, S) (Vartab.update_new (v, (S, T)) tye) | 
| 2964 | 529 | | (T, TVar (v, S)) => | 
| 530 | if occurs v tye T then raise TUNIFY | |
| 17412 | 531 | else meet (T, S) (Vartab.update_new (v, (S, T)) tye) | 
| 2964 | 532 | | (Type (a, Ts), Type (b, Us)) => | 
| 533 | if a <> b then raise TUNIFY | |
| 16885 | 534 | else unifs (Ts, Us) tye | 
| 535 | | (T, U) => if T = U then tye else raise TUNIFY) | |
| 536 | and unifs (T :: Ts, U :: Us) tye = unifs (Ts, Us) (unif (T, U) tye) | |
| 537 | | unifs _ tye = tye; | |
| 538 | in (unif TU tyenv, ! tyvar_count) end; | |
| 0 | 539 | |
| 16946 | 540 | (*purely structural unification*) | 
| 541 | fun raw_unify (ty1, ty2) tye = | |
| 542 | (case (devar tye ty1, devar tye ty2) of | |
| 32784 | 543 | (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 | 544 | if Term.eq_ix (v, w) then | 
| 16946 | 545 | if S1 = S2 then tye else tvar_clash v S1 S2 | 
| 17412 | 546 | else Vartab.update_new (w, (S2, T)) tye | 
| 16946 | 547 | | (TVar (v, S), T) => | 
| 548 | if occurs v tye T then raise TUNIFY | |
| 17412 | 549 | else Vartab.update_new (v, (S, T)) tye | 
| 16946 | 550 | | (T, TVar (v, S)) => | 
| 551 | if occurs v tye T then raise TUNIFY | |
| 17412 | 552 | else Vartab.update_new (v, (S, T)) tye | 
| 16946 | 553 | | (Type (a, Ts), Type (b, Us)) => | 
| 554 | if a <> b then raise TUNIFY | |
| 555 | else raw_unifys (Ts, Us) tye | |
| 556 | | (T, U) => if T = U then tye else raise TUNIFY) | |
| 557 | and raw_unifys (T :: Ts, U :: Us) tye = raw_unifys (Ts, Us) (raw_unify (T, U) tye) | |
| 19696 | 558 | | raw_unifys ([], []) tye = tye | 
| 559 | | raw_unifys _ _ = raise TUNIFY; | |
| 16946 | 560 | |
| 17804 | 561 | (*fast unification filter*) | 
| 562 | fun could_unify (Type (a, Ts), Type (b, Us)) = a = b andalso could_unifys (Ts, Us) | |
| 563 | | could_unify (TFree (a, _), TFree (b, _)) = a = b | |
| 564 | | could_unify (TVar _, _) = true | |
| 565 | | could_unify (_, TVar _) = true | |
| 566 | | could_unify _ = false | |
| 567 | and could_unifys (T :: Ts, U :: Us) = could_unify (T, U) andalso could_unifys (Ts, Us) | |
| 19696 | 568 | | could_unifys ([], []) = true | 
| 569 | | could_unifys _ = false; | |
| 450 | 570 | |
| 17804 | 571 | |
| 572 | (*equality with respect to a type environment*) | |
| 32030 | 573 | fun equal_type tye (T, T') = | 
| 16885 | 574 | (case (devar tye T, devar tye T') of | 
| 16650 | 575 | (Type (s, Ts), Type (s', Ts')) => | 
| 32030 | 576 | s = s' andalso ListPair.all (equal_type tye) (Ts, Ts') | 
| 16650 | 577 | | (U, U') => U = U'); | 
| 578 | ||
| 32030 | 579 | fun eq_type tye = | 
| 580 | if Vartab.is_empty tye then op = else equal_type tye; | |
| 581 | ||
| 450 | 582 | |
| 0 | 583 | |
| 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 | 584 | (** 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 | 585 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 586 | (* 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 | 587 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 588 | fun add_class context (c, cs) tsig = | 
| 19642 | 589 | tsig |> map_tsig (fn ((space, classes), default, types) => | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 590 | let | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 591 | val cs' = map (cert_class tsig) cs | 
| 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 592 | handle TYPE (msg, _, _) => error msg; | 
| 41254 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
39997diff
changeset | 593 | 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 | 594 | val (c', space') = space |> Name_Space.declare context true c; | 
| 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 595 | val classes' = classes |> Sorts.add_class (Context.pretty_generic context) (c', cs'); | 
| 19642 | 596 | 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 | 597 | |
| 26669 | 598 | 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 | 599 | ((Name_Space.hide fully c space, classes), default, types)); | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 600 | |
| 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 | 601 | |
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 602 | (* arities *) | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 603 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 604 | 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 | 605 | let | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 606 | val _ = | 
| 27302 | 607 | (case lookup_type tsig t of | 
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33097diff
changeset | 608 | SOME (LogicalType 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 | 609 |       | SOME _ => error ("Logical type constructor expected: " ^ quote t)
 | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 610 | | NONE => error (undecl_type t)); | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 611 | 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 | 612 | handle TYPE (msg, _, _) => error msg; | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 613 | val classes' = classes |> Sorts.add_arities pp ((t, map (fn c' => (c', Ss')) S')); | 
| 19642 | 614 | in ((space, classes'), default, types) end); | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 615 | |
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 616 | |
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 617 | (* classrel *) | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 618 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 619 | fun add_classrel pp rel tsig = | 
| 19642 | 620 | tsig |> map_tsig (fn ((space, classes), default, types) => | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 621 | let | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 622 | val rel' = pairself (cert_class tsig) rel | 
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 623 | handle TYPE (msg, _, _) => error msg; | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 624 | val classes' = classes |> Sorts.add_classrel pp rel'; | 
| 19642 | 625 | in ((space, classes'), default, types) end); | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 626 | |
| 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
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 | (* 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 | 629 | |
| 19642 | 630 | fun set_defsort S tsig = tsig |> map_tsig (fn (classes, _, types) => | 
| 631 | (classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types)); | |
| 0 | 632 | |
| 633 | ||
| 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 | 634 | (* 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 | 635 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 636 | 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 | 637 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 638 | 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 | 639 | (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 | 640 | |
| 19642 | 641 | fun map_types f = map_tsig (fn (classes, default, types) => | 
| 19579 
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
 wenzelm parents: 
19530diff
changeset | 642 | let | 
| 
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
 wenzelm parents: 
19530diff
changeset | 643 | val (space', tab') = f types; | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33094diff
changeset | 644 | val _ = Name_Space.intern space' "dummy" = "dummy" orelse | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21116diff
changeset | 645 | error "Illegal declaration of dummy type"; | 
| 19642 | 646 | in (classes, default, (space', tab')) end); | 
| 2964 | 647 | |
| 14989 | 648 | fun syntactic types (Type (c, Ts)) = | 
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33097diff
changeset | 649 | (case Symtab.lookup types c of SOME Nonterminal => true | _ => false) | 
| 14989 | 650 | orelse exists (syntactic types) Ts | 
| 651 | | syntactic _ _ = false; | |
| 652 | ||
| 27302 | 653 | in | 
| 654 | ||
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 655 | fun add_type context (c, n) = | 
| 42381 
309ec68442c6
added Binding.print convenience, which includes quote already;
 wenzelm parents: 
42375diff
changeset | 656 |   if n < 0 then error ("Bad type constructor declaration " ^ Binding.print c)
 | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 657 | else map_types (new_decl context (c, LogicalType n)); | 
| 27302 | 658 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 659 | 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 | 660 | let | 
| 30343 
79f022df8527
replace old bstring by binding for logical primitives: class, type, const etc.;
 wenzelm parents: 
29275diff
changeset | 661 | fun err msg = | 
| 42381 
309ec68442c6
added Binding.print convenience, which includes quote already;
 wenzelm parents: 
42375diff
changeset | 662 |       cat_error msg ("The error(s) above occurred in type abbreviation " ^ Binding.print a);
 | 
| 24274 
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
 wenzelm parents: 
23655diff
changeset | 663 | 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 | 664 | handle TYPE (msg, _, _) => err msg; | 
| 33094 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 665 | val _ = | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 666 | (case duplicates (op =) vs of | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 667 | [] => [] | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 668 |       | dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
 | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 669 | val _ = | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 670 | (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 | 671 | [] => [] | 
| 
ef0d77b1e687
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 672 |       | extras => err ("Extra variables on rhs: " ^ commas_quote extras));
 | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 673 | in types |> new_decl context (a, Abbreviation (vs, rhs', syntactic (#2 types) rhs')) end); | 
| 0 | 674 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 675 | 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 | 676 | |
| 
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
 wenzelm parents: 
13666diff
changeset | 677 | end; | 
| 0 | 678 | |
| 26669 | 679 | fun hide_type fully c = map_tsig (fn (classes, default, (space, types)) => | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33094diff
changeset | 680 | (classes, default, (Name_Space.hide fully c space, types))); | 
| 16370 
033d890fe91f
name space of classes and types maintained in tsig;
 wenzelm parents: 
16340diff
changeset | 681 | |
| 3790 | 682 | |
| 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 | 683 | (* 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 | 684 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 685 | fun merge_tsig 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 | 686 | let | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 687 |     val (TSig {classes = (space1, classes1), default = default1, types = types1,
 | 
| 26641 | 688 | log_types = _}) = tsig1; | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 689 |     val (TSig {classes = (space2, classes2), default = default2, types = types2,
 | 
| 26641 | 690 | 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 | 691 | |
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33094diff
changeset | 692 | val space' = Name_Space.merge (space1, space2); | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46649diff
changeset | 693 | val classes' = Sorts.merge_algebra pp (classes1, classes2); | 
| 19515 
9f650083da65
build classes/arities: refer to operations in sorts.ML;
 wenzelm parents: 
19464diff
changeset | 694 | 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 | 695 | val types' = Name_Space.merge_tables (types1, types2); | 
| 19642 | 696 | 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 | 697 | |
| 0 | 698 | end; |