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