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