| author | wenzelm | 
| Tue, 13 Aug 2024 18:31:40 +0200 | |
| changeset 80700 | f6c6d0988fba | 
| parent 79471 | 593fdddc6d98 | 
| child 81220 | 3d09d6f4c5b1 | 
| 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: 
30364diff
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: 
56025diff
changeset | 12 | val change_base: bool -> T -> T | 
| 56139 
b7add947a6ef
more frugal recording of changes: join merely requires information from one side;
 wenzelm parents: 
56062diff
changeset | 13 | val change_ignore: T -> T | 
| 30566 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 14 | val retrieve_abbrevs: T -> string list -> term -> (term * term) 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: 
74266diff
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: 
33092diff
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: 
55950diff
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: 
45666diff
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: 
19027diff
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: 
45666diff
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: 
33092diff
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: 
30466diff
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: 
30364diff
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: 
30364diff
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: 
30364diff
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: 
30364diff
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: 
30364diff
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: 
30364diff
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: 
30364diff
changeset | 66 | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
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: 
30280diff
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: 
30280diff
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: 
23655diff
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: 
56025diff
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: 
56025diff
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: 
56025diff
changeset | 75 | |
| 56139 
b7add947a6ef
more frugal recording of changes: join merely requires information from one side;
 wenzelm parents: 
56062diff
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: 
56062diff
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: 
56062diff
changeset | 78 | |
| 30566 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 79 | |
| 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 80 | (* reverted abbrevs *) | 
| 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 81 | |
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33158diff
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: 
30466diff
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: 
30466diff
changeset | 87 | |
| 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 88 | fun retrieve_abbrevs (Consts {rev_abbrevs, ...}) modes =
 | 
| 63632 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 89 | let val nets = map_filter (Symtab.lookup rev_abbrevs) modes in | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 90 | fn t => | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 91 | let | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 92 | val retrieve = | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 93 | if Term.could_beta_eta_contract t | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 94 | then Item_Net.retrieve | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 95 | else Item_Net.retrieve_matching | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 96 | in maps (fn net => retrieve net t) nets end | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 97 | end; | 
| 19364 | 98 | |
| 18965 | 99 | |
| 19364 | 100 | (* dest consts *) | 
| 101 | ||
| 56025 | 102 | fun dest (Consts {decls, constraints, ...}) =
 | 
| 103 |  {const_space = Name_Space.space_of_table decls,
 | |
| 104 | constants = | |
| 105 |     Name_Space.fold_table (fn (c, ({T, ...}, abbr)) =>
 | |
| 56062 | 106 | cons (c, (T, Option.map #rhs abbr))) decls [], | 
| 107 | constraints = Symtab.dest constraints}; | |
| 18060 | 108 | |
| 109 | ||
| 110 | (* lookup consts *) | |
| 111 | ||
| 79465 | 112 | fun get_const_name (Consts {decls, ...}) = Name_Space.lookup_key decls #> Option.map #1;
 | 
| 79462 | 113 | |
| 79464 | 114 | fun get_entry (Consts {decls, ...}) = Name_Space.lookup decls;
 | 
| 115 | ||
| 116 | fun the_entry consts c = | |
| 117 | (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: 
43552diff
changeset | 118 | SOME entry => entry | 
| 25041 | 119 |   | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
 | 
| 18935 | 120 | |
| 79463 | 121 | 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: 
43552diff
changeset | 122 | (case the_entry consts c of | 
| 79464 | 123 |     ({T, ...}, NONE) => T
 | 
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 124 |   | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
 | 
| 18060 | 125 | |
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 126 | 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: 
43552diff
changeset | 127 | (case the_entry consts c of | 
| 79464 | 128 |     ({T, ...}, SOME {rhs, ...}) => (T, rhs)
 | 
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 129 |   | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
 | 
| 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 130 | |
| 79464 | 131 | fun the_decl consts = #1 o the_entry consts; | 
| 25041 | 132 | val type_scheme = #T oo the_decl; | 
| 133 | val type_arguments = #typargs oo the_decl; | |
| 134 | ||
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 135 | val is_monomorphic = null oo type_arguments; | 
| 18935 | 136 | |
| 30284 
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
 wenzelm parents: 
30280diff
changeset | 137 | fun the_constraint (consts as Consts {constraints, ...}) c =
 | 
| 18060 | 138 | (case Symtab.lookup constraints c of | 
| 139 | SOME T => T | |
| 25041 | 140 | | NONE => type_scheme consts c); | 
| 18935 | 141 | |
| 142 | ||
| 19657 | 143 | (* name space and syntax *) | 
| 19364 | 144 | |
| 56025 | 145 | fun space_of (Consts {decls, ...}) = Name_Space.space_of_table decls;
 | 
| 19364 | 146 | |
| 56025 | 147 | 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: 
77970diff
changeset | 148 | ((Name_Space.alias_table naming binding name decls), constraints, rev_abbrevs)); | 
| 35680 | 149 | |
| 33158 | 150 | val is_concealed = Name_Space.is_concealed o space_of; | 
| 151 | ||
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 152 | val intern = Name_Space.intern o space_of; | 
| 19364 | 153 | |
| 35554 | 154 | fun intern_syntax consts s = | 
| 42290 
b1f544c84040
discontinued special treatment of structure Lexicon;
 wenzelm parents: 
42083diff
changeset | 155 | (case try Lexicon.unmark_const s of | 
| 35255 | 156 | SOME c => c | 
| 35554 | 157 | | NONE => intern consts s); | 
| 158 | ||
| 18060 | 159 | |
| 55843 
3fa61f39d1f2
prefer Name_Space.check with its builtin reports (including completion);
 wenzelm parents: 
50768diff
changeset | 160 | (* check_const *) | 
| 19364 | 161 | |
| 55956 
94d384d621b0
reject internal term names outright, and complete consts instead;
 wenzelm parents: 
55950diff
changeset | 162 | fun check_const context consts (xname, ps) = | 
| 19364 | 163 | let | 
| 55843 
3fa61f39d1f2
prefer Name_Space.check with its builtin reports (including completion);
 wenzelm parents: 
50768diff
changeset | 164 |     val Consts {decls, ...} = consts;
 | 
| 55956 
94d384d621b0
reject internal term names outright, and complete consts instead;
 wenzelm parents: 
55950diff
changeset | 165 | val ((c, reports), _) = Name_Space.check_reports context decls (xname, ps); | 
| 
94d384d621b0
reject internal term names outright, and complete consts instead;
 wenzelm parents: 
55950diff
changeset | 166 | 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: 
55843diff
changeset | 167 | in (Const (c, T), reports) end; | 
| 19364 | 168 | |
| 169 | ||
| 170 | (* certify *) | |
| 171 | ||
| 79471 | 172 | fun certify {normalize} context tsig consts =
 | 
| 18965 | 173 | let | 
| 174 | fun err msg (c, T) = | |
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 175 | raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ | 
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
56139diff
changeset | 176 | Syntax.string_of_typ (Syntax.init_pretty context) T, [], []); | 
| 79456 | 177 | |
| 178 | fun err_const const = err "Illegal type for constant" const; | |
| 179 | ||
| 79459 
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
 wenzelm parents: 
79458diff
changeset | 180 | 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: 
79458diff
changeset | 181 | Term.exists_Const (fn (c, _) => | 
| 79467 | 182 | (case get_entry consts c of | 
| 79471 | 183 |           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: 
79458diff
changeset | 184 | | _ => false)); | 
| 
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
 wenzelm parents: 
79458diff
changeset | 185 | |
| 79456 | 186 | val expand_typ = Type.certify_typ Type.mode_default tsig; | 
| 187 | fun expand_term tm = | |
| 18965 | 188 | let | 
| 189 | val (head, args) = Term.strip_comb tm; | |
| 79456 | 190 | val args' = map expand_term args; | 
| 21694 | 191 | fun comb head' = Term.list_comb (head', args'); | 
| 18965 | 192 | in | 
| 193 | (case head of | |
| 79460 | 194 | Const (c, T) => | 
| 19364 | 195 | let | 
| 79456 | 196 | val T' = expand_typ T; | 
| 79464 | 197 |               val ({T = U, ...}, abbr) = the_entry consts c;
 | 
| 25048 | 198 | fun expand u = | 
| 199 | Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ => | |
| 200 | err "Illegal type for abbreviation" (c, T), args'); | |
| 19364 | 201 | in | 
| 79456 | 202 | if not (Type.raw_instance (T', U)) then err_const (c, T) | 
| 19364 | 203 | else | 
| 24909 | 204 | (case abbr of | 
| 79471 | 205 |                   SOME {rhs, normal_rhs, internal} =>
 | 
| 206 | if normalize then expand normal_rhs | |
| 207 | else if internal then expand rhs | |
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 208 | else comb head | 
| 19364 | 209 | | _ => comb head) | 
| 210 | end | |
| 79460 | 211 | | 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: 
79456diff
changeset | 212 | | Free (x, T) => comb (Free (x, expand_typ T)) | 
| 
3d867a430413
more robust: certify types uniformly (see also 62b75508eb66);
 wenzelm parents: 
79456diff
changeset | 213 | | Var (xi, T) => comb (Var (xi, expand_typ T)) | 
| 19364 | 214 | | _ => comb head) | 
| 18965 | 215 | end; | 
| 79459 
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
 wenzelm parents: 
79458diff
changeset | 216 | |
| 79461 | 217 | 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: 
79458diff
changeset | 218 | 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: 
79458diff
changeset | 219 | let | 
| 
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
 wenzelm parents: 
79458diff
changeset | 220 | val (T', same) = Same.commit_id typ T; | 
| 79466 | 221 | 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: 
79458diff
changeset | 222 | in | 
| 79466 | 223 | 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: 
79458diff
changeset | 224 | 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: 
79458diff
changeset | 225 | end | 
| 
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
 wenzelm parents: 
79458diff
changeset | 226 | | 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: 
79458diff
changeset | 227 | | 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: 
79458diff
changeset | 228 | | 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: 
79458diff
changeset | 229 | | 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: 
79458diff
changeset | 230 | (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: 
79458diff
changeset | 231 | 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: 
79458diff
changeset | 232 | | 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: 
79458diff
changeset | 233 | (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: 
79458diff
changeset | 234 | 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: 
79458diff
changeset | 235 | |
| 
c53c261d91b9
minor performance tuning, for important special case where consts are already expanded (e.g. re-certification within proof procedure);
 wenzelm parents: 
79458diff
changeset | 236 | in fn tm => if need_expand tm then expand_term tm else Same.commit term tm end; | 
| 18965 | 237 | |
| 238 | ||
| 18060 | 239 | (* typargs -- view actual const type as instance of declaration *) | 
| 240 | ||
| 24909 | 241 | local | 
| 242 | ||
| 243 | fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos | |
| 244 | | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos) | |
| 245 | | args_of (TFree _) _ = I | |
| 246 | and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is | |
| 247 | | args_of_list [] _ _ = I; | |
| 248 | ||
| 19364 | 249 | fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is | 
| 250 | | subscript T [] = T | |
| 32784 | 251 | | subscript _ _ = raise Subscript; | 
| 18060 | 252 | |
| 24909 | 253 | in | 
| 254 | ||
| 255 | fun typargs_of T = map #2 (rev (args_of T [] [])); | |
| 256 | ||
| 19364 | 257 | fun typargs consts (c, T) = map (subscript T) (type_arguments consts c); | 
| 18060 | 258 | |
| 24909 | 259 | end; | 
| 260 | ||
| 18163 | 261 | fun instance consts (c, Ts) = | 
| 262 | let | |
| 25041 | 263 | val declT = type_scheme consts c; | 
| 74220 
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
 wenzelm parents: 
70785diff
changeset | 264 | val args = typargs consts (c, declT); | 
| 
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
 wenzelm parents: 
70785diff
changeset | 265 | val inst = | 
| 74266 | 266 | TVars.build (fold2 (fn a => fn T => TVars.add (Term.dest_TVar a, T)) args Ts) | 
| 267 |         handle ListPair.UnequalLengths => raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
 | |
| 31977 | 268 | in declT |> Term_Subst.instantiateT inst end; | 
| 18163 | 269 | |
| 70785 | 270 | fun dummy_types consts = | 
| 271 | let | |
| 272 | fun dummy (Const (c, T)) = | |
| 273 | Const (c, instance consts (c, replicate (length (typargs consts (c, T))) dummyT)) | |
| 274 | | dummy (Free (x, _)) = Free (x, dummyT) | |
| 275 | | dummy (Var (xi, _)) = Var (xi, dummyT) | |
| 276 | | dummy (b as Bound _) = b | |
| 277 | | dummy (t $ u) = dummy t $ dummy u | |
| 278 | | dummy (Abs (a, _, b)) = Abs (a, dummyT, dummy b); | |
| 279 | in dummy end; | |
| 280 | ||
| 18060 | 281 | |
| 282 | ||
| 19364 | 283 | (** build consts **) | 
| 18060 | 284 | |
| 18935 | 285 | (* name space *) | 
| 18060 | 286 | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 287 | fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 56025 | 288 | (Name_Space.hide_table fully c decls, constraints, rev_abbrevs)); | 
| 18935 | 289 | |
| 290 | ||
| 291 | (* declarations *) | |
| 292 | ||
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
45666diff
changeset | 293 | fun declare context (b, declT) = | 
| 33092 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 294 | map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 295 | let | 
| 35255 | 296 |       val decl = {T = declT, typargs = typargs_of declT};
 | 
| 41254 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
40722diff
changeset | 297 | val _ = Binding.check b; | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
45666diff
changeset | 298 | 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: 
32784diff
changeset | 299 | in (decls', constraints, rev_abbrevs) end); | 
| 19364 | 300 | |
| 301 | ||
| 302 | (* constraints *) | |
| 303 | ||
| 304 | fun constrain (c, C) consts = | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 305 | 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: 
43552diff
changeset | 306 | (#2 (the_entry consts c) handle TYPE (msg, _, _) => error msg; | 
| 19364 | 307 | (decls, | 
| 308 | 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: 
23655diff
changeset | 309 | rev_abbrevs))); | 
| 18060 | 310 | |
| 18935 | 311 | |
| 312 | (* abbreviations *) | |
| 313 | ||
| 19027 | 314 | local | 
| 315 | ||
| 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: 
30566diff
changeset | 316 | 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: 
41254diff
changeset | 317 | 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: 
30566diff
changeset | 318 | 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: 
30566diff
changeset | 319 | | strip_abss t = ([], t); | 
| 19027 | 320 | |
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 321 | fun rev_abbrev lhs rhs = | 
| 18060 | 322 | 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: 
30566diff
changeset | 323 | val (xs, body) = strip_abss (Envir.beta_eta_contract rhs); | 
| 
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: 
30566diff
changeset | 324 | val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) []; | 
| 
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: 
30566diff
changeset | 325 | in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end; | 
| 19027 | 326 | |
| 327 | in | |
| 18965 | 328 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
45666diff
changeset | 329 | fun abbreviate context tsig mode (b, raw_rhs) consts = | 
| 18965 | 330 | let | 
| 79471 | 331 |     val cert_term = certify {normalize = false} context tsig consts;
 | 
| 332 |     val expand_term = certify {normalize = true} context tsig consts;
 | |
| 333 | val internal = mode = Print_Mode.internal; | |
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 334 | |
| 30286 
cf89a03ee308
Consts.abbreviate: reject schematic term variables, prevent schematic type variables (hidden polymorphism) via Term.close_schematic_term -- see also 8f84a608883d;
 wenzelm parents: 
30284diff
changeset | 335 | val _ = Term.exists_subterm Term.is_Var raw_rhs andalso | 
| 42381 
309ec68442c6
added Binding.print convenience, which includes quote already;
 wenzelm parents: 
42375diff
changeset | 336 |       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: 
30284diff
changeset | 337 | |
| 79458 | 338 | val rhs = raw_rhs |> cert_term |> Term.close_schematic_term; | 
| 25404 | 339 | val normal_rhs = expand_term rhs; | 
| 21794 | 340 | 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: 
45666diff
changeset | 341 | val lhs = Const (Name_Space.full_name (Name_Space.naming_of context) b, T); | 
| 19364 | 342 | in | 
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 343 | consts |> map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 19364 | 344 | let | 
| 35255 | 345 |         val decl = {T = T, typargs = typargs_of T};
 | 
| 79471 | 346 |         val abbr = {rhs = rhs, normal_rhs = normal_rhs, internal = internal};
 | 
| 41254 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
40722diff
changeset | 347 | val _ = Binding.check b; | 
| 28861 | 348 | val (_, decls') = decls | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
45666diff
changeset | 349 | |> Name_Space.define context true (b, (decl, SOME abbr)); | 
| 19364 | 350 | val rev_abbrevs' = rev_abbrevs | 
| 33373 | 351 | |> update_abbrevs mode (rev_abbrev lhs rhs); | 
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 352 | in (decls', constraints, rev_abbrevs') end) | 
| 21794 | 353 | |> pair (lhs, rhs) | 
| 19364 | 354 | end; | 
| 18935 | 355 | |
| 25048 | 356 | fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 357 | let | |
| 358 | val (T, rhs) = the_abbreviation consts c; | |
| 359 | val rev_abbrevs' = rev_abbrevs | |
| 33373 | 360 | |> update_abbrevs mode (rev_abbrev (Const (c, T)) rhs); | 
| 25048 | 361 | in (decls, constraints, rev_abbrevs') end); | 
| 362 | ||
| 19027 | 363 | end; | 
| 364 | ||
| 18060 | 365 | |
| 366 | (* empty and merge *) | |
| 367 | ||
| 45666 | 368 | val empty = | 
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
48992diff
changeset | 369 | make_consts (Name_Space.empty_table Markup.constantN, Symtab.empty, Symtab.empty); | 
| 18060 | 370 | |
| 371 | fun merge | |
| 30284 
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
 wenzelm parents: 
30280diff
changeset | 372 |    (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: 
30280diff
changeset | 373 |     Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
 | 
| 18060 | 374 | let | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 375 | val decls' = Name_Space.merge_tables (decls1, decls2); | 
| 50768 
2172f82de515
tuned -- prefer high-level Table.merge with its slightly more conservative update;
 wenzelm parents: 
50201diff
changeset | 376 | val constraints' = Symtab.merge (K true) (constraints1, constraints2); | 
| 30566 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 377 | 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: 
23655diff
changeset | 378 | in make_consts (decls', constraints', rev_abbrevs') end; | 
| 18060 | 379 | |
| 380 | end; |