author | desharna |
Fri, 04 Apr 2025 15:30:03 +0200 | |
changeset 82399 | 9d457dfb56c5 |
parent 81541 | 5335b1ca6233 |
permissions | -rw-r--r-- |
18060 | 1 |
(* Title: Pure/consts.ML |
2 |
Author: Makarius |
|
3 |
||
18935 | 4 |
Polymorphic constants: declarations, abbreviations, additional type |
5 |
constraints. |
|
18060 | 6 |
*) |
7 |
||
8 |
signature CONSTS = |
|
9 |
sig |
|
10 |
type T |
|
30424
692279df7cc2
Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents:
30364
diff
changeset
|
11 |
val eq_consts: T * T -> bool |
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:
56025
diff
changeset
|
12 |
val change_base: bool -> T -> T |
56139
b7add947a6ef
more frugal recording of changes: join merely requires information from one side;
wenzelm
parents:
56062
diff
changeset
|
13 |
val change_ignore: T -> T |
81220 | 14 |
val revert_abbrevs: T -> string list -> (term * term) Item_Net.T list |
18935 | 15 |
val dest: T -> |
56025 | 16 |
{const_space: Name_Space.T, |
56062 | 17 |
constants: (string * (typ * term option)) list, |
18 |
constraints: (string * typ) list} |
|
79462 | 19 |
val get_const_name: T -> string -> string option |
79463 | 20 |
val the_const_type: T -> string -> typ (*exception TYPE*) |
25048 | 21 |
val the_abbreviation: T -> string -> typ * term (*exception TYPE*) |
22 |
val type_scheme: T -> string -> typ (*exception TYPE*) |
|
74291
b83fa8f3a271
ML antiquotations for type constructors and term constants;
wenzelm
parents:
74266
diff
changeset
|
23 |
val type_arguments: T -> string -> int list list (*exception TYPE*) |
25048 | 24 |
val is_monomorphic: T -> string -> bool (*exception TYPE*) |
25 |
val the_constraint: T -> string -> typ (*exception TYPE*) |
|
33095
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents:
33092
diff
changeset
|
26 |
val space_of: T -> Name_Space.T |
35680 | 27 |
val alias: Name_Space.naming -> binding -> string -> T -> T |
33158 | 28 |
val is_concealed: T -> string -> bool |
19364 | 29 |
val intern: T -> xstring -> string |
35255 | 30 |
val intern_syntax: T -> xstring -> string |
55956
94d384d621b0
reject internal term names outright, and complete consts instead;
wenzelm
parents:
55950
diff
changeset
|
31 |
val check_const: Context.generic -> T -> xstring * Position.T list -> term * Position.report list |
79471 | 32 |
val certify: {normalize: bool} -> Context.generic -> Type.tsig -> T -> term -> term (*exception TYPE*) |
18146 | 33 |
val typargs: T -> string * typ -> typ list |
18163 | 34 |
val instance: T -> string * typ list -> typ |
70785 | 35 |
val dummy_types: T -> term -> term |
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
45666
diff
changeset
|
36 |
val declare: Context.generic -> binding * typ -> T -> T |
19098
fc736dbbe333
constrain: assert const declaration, optional type (i.e. may delete constraints);
wenzelm
parents:
19027
diff
changeset
|
37 |
val constrain: string * typ option -> T -> T |
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
45666
diff
changeset
|
38 |
val abbreviate: Context.generic -> Type.tsig -> string -> binding * term -> T -> (term * term) * T |
25048 | 39 |
val revert_abbrev: string -> string -> T -> T |
18060 | 40 |
val hide: bool -> string -> T -> T |
41 |
val empty: T |
|
42 |
val merge: T * T -> T |
|
19364 | 43 |
end; |
18060 | 44 |
|
45 |
structure Consts: CONSTS = |
|
46 |
struct |
|
47 |
||
19364 | 48 |
(** consts type **) |
49 |
||
50 |
(* datatype T *) |
|
18060 | 51 |
|
35255 | 52 |
type decl = {T: typ, typargs: int list list}; |
79471 | 53 |
type abbrev = {rhs: term, normal_rhs: term, internal: bool}; |
18935 | 54 |
|
18060 | 55 |
datatype T = Consts of |
33095
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents:
33092
diff
changeset
|
56 |
{decls: (decl * abbrev option) Name_Space.table, |
19364 | 57 |
constraints: typ Symtab.table, |
30566
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents:
30466
diff
changeset
|
58 |
rev_abbrevs: (term * term) Item_Net.T Symtab.table}; |
18060 | 59 |
|
30424
692279df7cc2
Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents:
30364
diff
changeset
|
60 |
fun eq_consts |
692279df7cc2
Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents:
30364
diff
changeset
|
61 |
(Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1}, |
692279df7cc2
Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents:
30364
diff
changeset
|
62 |
Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) = |
692279df7cc2
Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents:
30364
diff
changeset
|
63 |
pointer_eq (decls1, decls2) andalso |
692279df7cc2
Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents:
30364
diff
changeset
|
64 |
pointer_eq (constraints1, constraints2) andalso |
692279df7cc2
Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents:
30364
diff
changeset
|
65 |
pointer_eq (rev_abbrevs1, rev_abbrevs2); |
692279df7cc2
Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents:
30364
diff
changeset
|
66 |
|
24673
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents:
23655
diff
changeset
|
67 |
fun make_consts (decls, constraints, rev_abbrevs) = |
30284
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
wenzelm
parents:
30280
diff
changeset
|
68 |
Consts {decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs}; |
18060 | 69 |
|
30284
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
wenzelm
parents:
30280
diff
changeset
|
70 |
fun map_consts f (Consts {decls, constraints, rev_abbrevs}) = |
24673
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents:
23655
diff
changeset
|
71 |
make_consts (f (decls, constraints, rev_abbrevs)); |
19364 | 72 |
|
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:
56025
diff
changeset
|
73 |
fun change_base begin = map_consts (fn (decls, constraints, rev_abbrevs) => |
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:
56025
diff
changeset
|
74 |
(Name_Space.change_base begin decls, constraints, rev_abbrevs)); |
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:
56025
diff
changeset
|
75 |
|
56139
b7add947a6ef
more frugal recording of changes: join merely requires information from one side;
wenzelm
parents:
56062
diff
changeset
|
76 |
val change_ignore = map_consts (fn (decls, constraints, rev_abbrevs) => |
b7add947a6ef
more frugal recording of changes: join merely requires information from one side;
wenzelm
parents:
56062
diff
changeset
|
77 |
(Name_Space.change_ignore decls, constraints, rev_abbrevs)); |
b7add947a6ef
more frugal recording of changes: join merely requires information from one side;
wenzelm
parents:
56062
diff
changeset
|
78 |
|
30566
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents:
30466
diff
changeset
|
79 |
|
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents:
30466
diff
changeset
|
80 |
(* reverted abbrevs *) |
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents:
30466
diff
changeset
|
81 |
|
33173
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
wenzelm
parents:
33158
diff
changeset
|
82 |
val empty_abbrevs = |
33373 | 83 |
Item_Net.init (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u') (single o #1); |
30566
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents:
30466
diff
changeset
|
84 |
|
33373 | 85 |
fun update_abbrevs mode abbrs = |
86 |
Symtab.map_default (mode, empty_abbrevs) (Item_Net.update abbrs); |
|
30566
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents:
30466
diff
changeset
|
87 |
|
81220 | 88 |
fun revert_abbrevs (Consts {rev_abbrevs, ...}) modes = |
89 |
map_filter (Symtab.lookup rev_abbrevs) modes; |
|
19364 | 90 |
|
18965 | 91 |
|
19364 | 92 |
(* dest consts *) |
93 |
||
56025 | 94 |
fun dest (Consts {decls, constraints, ...}) = |
95 |
{const_space = Name_Space.space_of_table decls, |
|
96 |
constants = |
|
97 |
Name_Space.fold_table (fn (c, ({T, ...}, abbr)) => |
|
56062 | 98 |
cons (c, (T, Option.map #rhs abbr))) decls [], |
99 |
constraints = Symtab.dest constraints}; |
|
18060 | 100 |
|
101 |
||
102 |
(* lookup consts *) |
|
103 |
||
79465 | 104 |
fun get_const_name (Consts {decls, ...}) = Name_Space.lookup_key decls #> Option.map #1; |
79462 | 105 |
|
79464 | 106 |
fun get_entry (Consts {decls, ...}) = Name_Space.lookup decls; |
107 |
||
108 |
fun the_entry consts c = |
|
109 |
(case get_entry consts c of |
|
43794
49cbbe2768a8
sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents:
43552
diff
changeset
|
110 |
SOME entry => entry |
25041 | 111 |
| NONE => raise TYPE ("Unknown constant: " ^ quote c, [], [])); |
18935 | 112 |
|
79463 | 113 |
fun the_const_type consts c = |
43794
49cbbe2768a8
sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents:
43552
diff
changeset
|
114 |
(case the_entry consts c of |
79464 | 115 |
({T, ...}, NONE) => T |
21720
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents:
21694
diff
changeset
|
116 |
| _ => raise TYPE ("Not a logical constant: " ^ quote c, [], [])); |
18060 | 117 |
|
21720
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents:
21694
diff
changeset
|
118 |
fun the_abbreviation consts c = |
43794
49cbbe2768a8
sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents:
43552
diff
changeset
|
119 |
(case the_entry consts c of |
79464 | 120 |
({T, ...}, SOME {rhs, ...}) => (T, rhs) |
21720
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents:
21694
diff
changeset
|
121 |
| _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], [])); |
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents:
21694
diff
changeset
|
122 |
|
79464 | 123 |
fun the_decl consts = #1 o the_entry consts; |
25041 | 124 |
val type_scheme = #T oo the_decl; |
125 |
val type_arguments = #typargs oo the_decl; |
|
126 |
||
21720
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents:
21694
diff
changeset
|
127 |
val is_monomorphic = null oo type_arguments; |
18935 | 128 |
|
30284
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
wenzelm
parents:
30280
diff
changeset
|
129 |
fun the_constraint (consts as Consts {constraints, ...}) c = |
18060 | 130 |
(case Symtab.lookup constraints c of |
131 |
SOME T => T |
|
25041 | 132 |
| NONE => type_scheme consts c); |
18935 | 133 |
|
134 |
||
19657 | 135 |
(* name space and syntax *) |
19364 | 136 |
|
56025 | 137 |
fun space_of (Consts {decls, ...}) = Name_Space.space_of_table decls; |
19364 | 138 |
|
56025 | 139 |
fun alias naming binding name = map_consts (fn (decls, constraints, rev_abbrevs) => |
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
|
140 |
((Name_Space.alias_table naming binding name decls), constraints, rev_abbrevs)); |
35680 | 141 |
|
33158 | 142 |
val is_concealed = Name_Space.is_concealed o space_of; |
143 |
||
33095
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents:
33092
diff
changeset
|
144 |
val intern = Name_Space.intern o space_of; |
19364 | 145 |
|
35554 | 146 |
fun intern_syntax consts s = |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42083
diff
changeset
|
147 |
(case try Lexicon.unmark_const s of |
35255 | 148 |
SOME c => c |
35554 | 149 |
| NONE => intern consts s); |
150 |
||
18060 | 151 |
|
55843
3fa61f39d1f2
prefer Name_Space.check with its builtin reports (including completion);
wenzelm
parents:
50768
diff
changeset
|
152 |
(* check_const *) |
19364 | 153 |
|
55956
94d384d621b0
reject internal term names outright, and complete consts instead;
wenzelm
parents:
55950
diff
changeset
|
154 |
fun check_const context consts (xname, ps) = |
19364 | 155 |
let |
55843
3fa61f39d1f2
prefer Name_Space.check with its builtin reports (including completion);
wenzelm
parents:
50768
diff
changeset
|
156 |
val Consts {decls, ...} = consts; |
55956
94d384d621b0
reject internal term names outright, and complete consts instead;
wenzelm
parents:
55950
diff
changeset
|
157 |
val ((c, reports), _) = Name_Space.check_reports context decls (xname, ps); |
94d384d621b0
reject internal term names outright, and complete consts instead;
wenzelm
parents:
55950
diff
changeset
|
158 |
val T = type_scheme consts c handle TYPE (msg, _, _) => error (msg ^ Position.here_list ps); |
55950
3bb5c7179234
clarified treatment of consts -- prefer value-oriented reports;
wenzelm
parents:
55843
diff
changeset
|
159 |
in (Const (c, T), reports) end; |
19364 | 160 |
|
161 |
||
162 |
(* certify *) |
|
163 |
||
79471 | 164 |
fun certify {normalize} context tsig consts = |
18965 | 165 |
let |
166 |
fun err msg (c, T) = |
|
42383
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
wenzelm
parents:
42381
diff
changeset
|
167 |
raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ |
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
56139
diff
changeset
|
168 |
Syntax.string_of_typ (Syntax.init_pretty context) T, [], []); |
79456 | 169 |
|
170 |
fun err_const const = err "Illegal type for constant" const; |
|
171 |
||
79459
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
172 |
val need_expand = |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
173 |
Term.exists_Const (fn (c, _) => |
79467 | 174 |
(case get_entry consts c of |
79471 | 175 |
SOME (_, SOME {internal, ...}) => normalize orelse internal |
79459
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
176 |
| _ => false)); |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
177 |
|
79456 | 178 |
val expand_typ = Type.certify_typ Type.mode_default tsig; |
179 |
fun expand_term tm = |
|
18965 | 180 |
let |
181 |
val (head, args) = Term.strip_comb tm; |
|
79456 | 182 |
val args' = map expand_term args; |
21694 | 183 |
fun comb head' = Term.list_comb (head', args'); |
18965 | 184 |
in |
185 |
(case head of |
|
79460 | 186 |
Const (c, T) => |
19364 | 187 |
let |
79456 | 188 |
val T' = expand_typ T; |
79464 | 189 |
val ({T = U, ...}, abbr) = the_entry consts c; |
25048 | 190 |
fun expand u = |
191 |
Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ => |
|
192 |
err "Illegal type for abbreviation" (c, T), args'); |
|
19364 | 193 |
in |
79456 | 194 |
if not (Type.raw_instance (T', U)) then err_const (c, T) |
19364 | 195 |
else |
24909 | 196 |
(case abbr of |
79471 | 197 |
SOME {rhs, normal_rhs, internal} => |
198 |
if normalize then expand normal_rhs |
|
199 |
else if internal then expand rhs |
|
21720
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents:
21694
diff
changeset
|
200 |
else comb head |
19364 | 201 |
| _ => comb head) |
202 |
end |
|
79460 | 203 |
| Abs (x, T, t) => comb (Abs (x, expand_typ T, expand_term t)) |
79457
3d867a430413
more robust: certify types uniformly (see also 62b75508eb66);
wenzelm
parents:
79456
diff
changeset
|
204 |
| Free (x, T) => comb (Free (x, expand_typ T)) |
3d867a430413
more robust: certify types uniformly (see also 62b75508eb66);
wenzelm
parents:
79456
diff
changeset
|
205 |
| Var (xi, T) => comb (Var (xi, expand_typ T)) |
19364 | 206 |
| _ => comb head) |
18965 | 207 |
end; |
79459
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
208 |
|
79461 | 209 |
val typ = Type.certify_typ_same Type.mode_default tsig; |
79459
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
210 |
fun term (Const (c, T)) = |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
211 |
let |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
212 |
val (T', same) = Same.commit_id typ T; |
79466 | 213 |
val U = type_scheme consts c; |
79459
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
214 |
in |
79466 | 215 |
if not (Type.raw_instance (T', U)) then err_const (c, T) |
79459
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
216 |
else if same then raise Same.SAME else Const (c, T') |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
217 |
end |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
218 |
| term (Free (x, T)) = Free (x, typ T) |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
219 |
| term (Var (xi, T)) = Var (xi, typ T) |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
220 |
| term (Bound _) = raise Same.SAME |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
221 |
| term (Abs (x, T, t)) = |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
222 |
(Abs (x, typ T, Same.commit term t) |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
223 |
handle Same.SAME => Abs (x, T, term t)) |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
224 |
| term (t $ u) = |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
225 |
(term t $ Same.commit term u |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
226 |
handle Same.SAME => t $ term u); |
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
227 |
|
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
wenzelm
parents:
79458
diff
changeset
|
228 |
in fn tm => if need_expand tm then expand_term tm else Same.commit term tm end; |
18965 | 229 |
|
230 |
||
18060 | 231 |
(* typargs -- view actual const type as instance of declaration *) |
232 |
||
24909 | 233 |
local |
234 |
||
235 |
fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos |
|
236 |
| args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos) |
|
237 |
| args_of (TFree _) _ = I |
|
238 |
and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is |
|
239 |
| args_of_list [] _ _ = I; |
|
240 |
||
19364 | 241 |
fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is |
242 |
| subscript T [] = T |
|
32784 | 243 |
| subscript _ _ = raise Subscript; |
18060 | 244 |
|
24909 | 245 |
in |
246 |
||
247 |
fun typargs_of T = map #2 (rev (args_of T [] [])); |
|
248 |
||
19364 | 249 |
fun typargs consts (c, T) = map (subscript T) (type_arguments consts c); |
18060 | 250 |
|
24909 | 251 |
end; |
252 |
||
18163 | 253 |
fun instance consts (c, Ts) = |
254 |
let |
|
25041 | 255 |
val declT = type_scheme consts c; |
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
70785
diff
changeset
|
256 |
val args = typargs consts (c, declT); |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
70785
diff
changeset
|
257 |
val inst = |
74266 | 258 |
TVars.build (fold2 (fn a => fn T => TVars.add (Term.dest_TVar a, T)) args Ts) |
259 |
handle ListPair.UnequalLengths => raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]); |
|
31977 | 260 |
in declT |> Term_Subst.instantiateT inst end; |
18163 | 261 |
|
70785 | 262 |
fun dummy_types consts = |
263 |
let |
|
264 |
fun dummy (Const (c, T)) = |
|
265 |
Const (c, instance consts (c, replicate (length (typargs consts (c, T))) dummyT)) |
|
266 |
| dummy (Free (x, _)) = Free (x, dummyT) |
|
267 |
| dummy (Var (xi, _)) = Var (xi, dummyT) |
|
268 |
| dummy (b as Bound _) = b |
|
269 |
| dummy (t $ u) = dummy t $ dummy u |
|
270 |
| dummy (Abs (a, _, b)) = Abs (a, dummyT, dummy b); |
|
271 |
in dummy end; |
|
272 |
||
18060 | 273 |
|
274 |
||
19364 | 275 |
(** build consts **) |
18060 | 276 |
|
18935 | 277 |
(* name space *) |
18060 | 278 |
|
24673
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents:
23655
diff
changeset
|
279 |
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) => |
56025 | 280 |
(Name_Space.hide_table fully c decls, constraints, rev_abbrevs)); |
18935 | 281 |
|
282 |
||
283 |
(* declarations *) |
|
284 |
||
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
45666
diff
changeset
|
285 |
fun declare context (b, declT) = |
33092
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
wenzelm
parents:
32784
diff
changeset
|
286 |
map_consts (fn (decls, constraints, rev_abbrevs) => |
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
wenzelm
parents:
32784
diff
changeset
|
287 |
let |
35255 | 288 |
val decl = {T = declT, typargs = typargs_of declT}; |
41254
78c3e472bb35
extra checking of name bindings for classes, types, consts;
wenzelm
parents:
40722
diff
changeset
|
289 |
val _ = Binding.check b; |
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
45666
diff
changeset
|
290 |
val (_, decls') = decls |> Name_Space.define context true (b, (decl, NONE)); |
33092
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
wenzelm
parents:
32784
diff
changeset
|
291 |
in (decls', constraints, rev_abbrevs) end); |
19364 | 292 |
|
293 |
||
294 |
(* constraints *) |
|
295 |
||
296 |
fun constrain (c, C) consts = |
|
24673
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents:
23655
diff
changeset
|
297 |
consts |> map_consts (fn (decls, constraints, rev_abbrevs) => |
43794
49cbbe2768a8
sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents:
43552
diff
changeset
|
298 |
(#2 (the_entry consts c) handle TYPE (msg, _, _) => error msg; |
19364 | 299 |
(decls, |
300 |
constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c), |
|
24673
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents:
23655
diff
changeset
|
301 |
rev_abbrevs))); |
18060 | 302 |
|
18935 | 303 |
|
304 |
(* abbreviations *) |
|
305 |
||
19027 | 306 |
local |
307 |
||
30568
e6a55291102e
strip_abss: always strip abstractions as far as possible, without keeping alternatives (which appear to be redundant anyway, but cause significant slowdown since discrimination nets collapse abstractions);
wenzelm
parents:
30566
diff
changeset
|
308 |
fun strip_abss (t as Abs (x, T, b)) = |
42083
e1209fc7ecdc
added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
wenzelm
parents:
41254
diff
changeset
|
309 |
if Term.is_dependent b then strip_abss b |>> cons (x, T) (* FIXME decr!? *) |
30568
e6a55291102e
strip_abss: always strip abstractions as far as possible, without keeping alternatives (which appear to be redundant anyway, but cause significant slowdown since discrimination nets collapse abstractions);
wenzelm
parents:
30566
diff
changeset
|
310 |
else ([], t) |
e6a55291102e
strip_abss: always strip abstractions as far as possible, without keeping alternatives (which appear to be redundant anyway, but cause significant slowdown since discrimination nets collapse abstractions);
wenzelm
parents:
30566
diff
changeset
|
311 |
| strip_abss t = ([], t); |
19027 | 312 |
|
21720
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents:
21694
diff
changeset
|
313 |
fun rev_abbrev lhs rhs = |
18060 | 314 |
let |
30568
e6a55291102e
strip_abss: always strip abstractions as far as possible, without keeping alternatives (which appear to be redundant anyway, but cause significant slowdown since discrimination nets collapse abstractions);
wenzelm
parents:
30566
diff
changeset
|
315 |
val (xs, body) = strip_abss (Envir.beta_eta_contract rhs); |
81541
5335b1ca6233
more elementary operation Term.variant_bounds: only for bounds vs. frees, no consts, no tfrees;
wenzelm
parents:
81516
diff
changeset
|
316 |
val vars = map (fn (x, T) => Var ((x, 0), T)) (Term.variant_bounds body xs); |
30568
e6a55291102e
strip_abss: always strip abstractions as far as possible, without keeping alternatives (which appear to be redundant anyway, but cause significant slowdown since discrimination nets collapse abstractions);
wenzelm
parents:
30566
diff
changeset
|
317 |
in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end; |
19027 | 318 |
|
319 |
in |
|
18965 | 320 |
|
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
45666
diff
changeset
|
321 |
fun abbreviate context tsig mode (b, raw_rhs) consts = |
18965 | 322 |
let |
79471 | 323 |
val cert_term = certify {normalize = false} context tsig consts; |
324 |
val expand_term = certify {normalize = true} context tsig consts; |
|
325 |
val internal = mode = Print_Mode.internal; |
|
21720
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents:
21694
diff
changeset
|
326 |
|
30286
cf89a03ee308
Consts.abbreviate: reject schematic term variables, prevent schematic type variables (hidden polymorphism) via Term.close_schematic_term -- see also 8f84a608883d;
wenzelm
parents:
30284
diff
changeset
|
327 |
val _ = Term.exists_subterm Term.is_Var raw_rhs andalso |
42381
309ec68442c6
added Binding.print convenience, which includes quote already;
wenzelm
parents:
42375
diff
changeset
|
328 |
error ("Illegal schematic variables on rhs of abbreviation " ^ Binding.print b); |
30286
cf89a03ee308
Consts.abbreviate: reject schematic term variables, prevent schematic type variables (hidden polymorphism) via Term.close_schematic_term -- see also 8f84a608883d;
wenzelm
parents:
30284
diff
changeset
|
329 |
|
79458 | 330 |
val rhs = raw_rhs |> cert_term |> Term.close_schematic_term; |
25404 | 331 |
val normal_rhs = expand_term rhs; |
21794 | 332 |
val T = Term.fastype_of rhs; |
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
45666
diff
changeset
|
333 |
val lhs = Const (Name_Space.full_name (Name_Space.naming_of context) b, T); |
19364 | 334 |
in |
24673
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents:
23655
diff
changeset
|
335 |
consts |> map_consts (fn (decls, constraints, rev_abbrevs) => |
19364 | 336 |
let |
35255 | 337 |
val decl = {T = T, typargs = typargs_of T}; |
79471 | 338 |
val abbr = {rhs = rhs, normal_rhs = normal_rhs, internal = internal}; |
41254
78c3e472bb35
extra checking of name bindings for classes, types, consts;
wenzelm
parents:
40722
diff
changeset
|
339 |
val _ = Binding.check b; |
28861 | 340 |
val (_, decls') = decls |
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
45666
diff
changeset
|
341 |
|> Name_Space.define context true (b, (decl, SOME abbr)); |
19364 | 342 |
val rev_abbrevs' = rev_abbrevs |
33373 | 343 |
|> update_abbrevs mode (rev_abbrev lhs rhs); |
24673
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents:
23655
diff
changeset
|
344 |
in (decls', constraints, rev_abbrevs') end) |
21794 | 345 |
|> pair (lhs, rhs) |
19364 | 346 |
end; |
18935 | 347 |
|
25048 | 348 |
fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) => |
349 |
let |
|
350 |
val (T, rhs) = the_abbreviation consts c; |
|
351 |
val rev_abbrevs' = rev_abbrevs |
|
33373 | 352 |
|> update_abbrevs mode (rev_abbrev (Const (c, T)) rhs); |
25048 | 353 |
in (decls, constraints, rev_abbrevs') end); |
354 |
||
19027 | 355 |
end; |
356 |
||
18060 | 357 |
|
358 |
(* empty and merge *) |
|
359 |
||
45666 | 360 |
val empty = |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48992
diff
changeset
|
361 |
make_consts (Name_Space.empty_table Markup.constantN, Symtab.empty, Symtab.empty); |
18060 | 362 |
|
363 |
fun merge |
|
30284
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
wenzelm
parents:
30280
diff
changeset
|
364 |
(Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1}, |
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
wenzelm
parents:
30280
diff
changeset
|
365 |
Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) = |
18060 | 366 |
let |
33095
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents:
33092
diff
changeset
|
367 |
val decls' = Name_Space.merge_tables (decls1, decls2); |
50768
2172f82de515
tuned -- prefer high-level Table.merge with its slightly more conservative update;
wenzelm
parents:
50201
diff
changeset
|
368 |
val constraints' = Symtab.merge (K true) (constraints1, constraints2); |
30566
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents:
30466
diff
changeset
|
369 |
val rev_abbrevs' = Symtab.join (K Item_Net.merge) (rev_abbrevs1, rev_abbrevs2); |
24673
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents:
23655
diff
changeset
|
370 |
in make_consts (decls', constraints', rev_abbrevs') end; |
18060 | 371 |
|
372 |
end; |