| author | wenzelm | 
| Tue, 13 Oct 2020 19:12:58 +0200 | |
| changeset 72466 | 04403e1ef176 | 
| parent 70785 | edaeb8feb4d0 | 
| child 74220 | c49134ee16c1 | 
| 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} | |
| 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 | 19 | val the_const: T -> string -> string * typ (*exception TYPE*) | 
| 25048 | 20 | val the_abbreviation: T -> string -> typ * term (*exception TYPE*) | 
| 21 | val type_scheme: T -> string -> typ (*exception TYPE*) | |
| 22 | val is_monomorphic: T -> string -> bool (*exception TYPE*) | |
| 23 | 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 | 24 | val space_of: T -> Name_Space.T | 
| 35680 | 25 | val alias: Name_Space.naming -> binding -> string -> T -> T | 
| 33158 | 26 | val is_concealed: T -> string -> bool | 
| 19364 | 27 | val intern: T -> xstring -> string | 
| 35255 | 28 | val intern_syntax: T -> xstring -> string | 
| 55956 
94d384d621b0
reject internal term names outright, and complete consts instead;
 wenzelm parents: 
55950diff
changeset | 29 | val check_const: Context.generic -> T -> xstring * Position.T list -> term * Position.report list | 
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
56139diff
changeset | 30 | val certify: Context.generic -> Type.tsig -> bool -> T -> term -> term (*exception TYPE*) | 
| 18146 | 31 | val typargs: T -> string * typ -> typ list | 
| 18163 | 32 | val instance: T -> string * typ list -> typ | 
| 70785 | 33 | 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 | 34 | 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 | 35 | 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 | 36 | val abbreviate: Context.generic -> Type.tsig -> string -> binding * term -> T -> (term * term) * T | 
| 25048 | 37 | val revert_abbrev: string -> string -> T -> T | 
| 18060 | 38 | val hide: bool -> string -> T -> T | 
| 39 | val empty: T | |
| 40 | val merge: T * T -> T | |
| 19364 | 41 | end; | 
| 18060 | 42 | |
| 43 | structure Consts: CONSTS = | |
| 44 | struct | |
| 45 | ||
| 19364 | 46 | (** consts type **) | 
| 47 | ||
| 48 | (* datatype T *) | |
| 18060 | 49 | |
| 35255 | 50 | type decl = {T: typ, typargs: int list list};
 | 
| 24909 | 51 | type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
 | 
| 18935 | 52 | |
| 18060 | 53 | datatype T = Consts of | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 54 |  {decls: (decl * abbrev option) Name_Space.table,
 | 
| 19364 | 55 | constraints: typ Symtab.table, | 
| 30566 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 56 | rev_abbrevs: (term * term) Item_Net.T Symtab.table}; | 
| 18060 | 57 | |
| 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 | 58 | 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 | 59 |    (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 | 60 |     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 | 61 | 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 | 62 | 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 | 63 | 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 | 64 | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 65 | 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 | 66 |   Consts {decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs};
 | 
| 18060 | 67 | |
| 30284 
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
 wenzelm parents: 
30280diff
changeset | 68 | 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 | 69 | make_consts (f (decls, constraints, rev_abbrevs)); | 
| 19364 | 70 | |
| 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 | 71 | 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 | 72 | (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 | 73 | |
| 56139 
b7add947a6ef
more frugal recording of changes: join merely requires information from one side;
 wenzelm parents: 
56062diff
changeset | 74 | 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 | 75 | (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 | 76 | |
| 30566 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 77 | |
| 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 78 | (* reverted abbrevs *) | 
| 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 79 | |
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33158diff
changeset | 80 | val empty_abbrevs = | 
| 33373 | 81 | 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 | 82 | |
| 33373 | 83 | fun update_abbrevs mode abbrs = | 
| 84 | Symtab.map_default (mode, empty_abbrevs) (Item_Net.update abbrs); | |
| 30566 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 85 | |
| 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 86 | fun retrieve_abbrevs (Consts {rev_abbrevs, ...}) modes =
 | 
| 63632 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 87 | 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 | 88 | fn t => | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 89 | let | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 90 | val retrieve = | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 91 | if Term.could_beta_eta_contract t | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 92 | then Item_Net.retrieve | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 93 | else Item_Net.retrieve_matching | 
| 
a59d9b81be24
Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
 wenzelm parents: 
63573diff
changeset | 94 | 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 | 95 | end; | 
| 19364 | 96 | |
| 18965 | 97 | |
| 19364 | 98 | (* dest consts *) | 
| 99 | ||
| 56025 | 100 | fun dest (Consts {decls, constraints, ...}) =
 | 
| 101 |  {const_space = Name_Space.space_of_table decls,
 | |
| 102 | constants = | |
| 103 |     Name_Space.fold_table (fn (c, ({T, ...}, abbr)) =>
 | |
| 56062 | 104 | cons (c, (T, Option.map #rhs abbr))) decls [], | 
| 105 | constraints = Symtab.dest constraints}; | |
| 18060 | 106 | |
| 107 | ||
| 108 | (* lookup consts *) | |
| 109 | ||
| 56025 | 110 | fun the_entry (Consts {decls, ...}) c =
 | 
| 111 | (case Name_Space.lookup_key decls 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 | 112 | SOME entry => entry | 
| 25041 | 113 |   | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
 | 
| 18935 | 114 | |
| 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 | 115 | fun the_const consts c = | 
| 
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 | 116 | (case the_entry consts c of | 
| 
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 | 117 |     (c', ({T, ...}, NONE)) => (c', T)
 | 
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 118 |   | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
 | 
| 18060 | 119 | |
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 120 | 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 | 121 | (case the_entry consts c of | 
| 
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 |     (_, ({T, ...}, SOME {rhs, ...})) => (T, rhs)
 | 
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 123 |   | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
 | 
| 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 124 | |
| 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 | 125 | fun the_decl consts = #1 o #2 o the_entry consts; | 
| 25041 | 126 | val type_scheme = #T oo the_decl; | 
| 127 | val type_arguments = #typargs oo the_decl; | |
| 128 | ||
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 129 | val is_monomorphic = null oo type_arguments; | 
| 18935 | 130 | |
| 30284 
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
 wenzelm parents: 
30280diff
changeset | 131 | fun the_constraint (consts as Consts {constraints, ...}) c =
 | 
| 18060 | 132 | (case Symtab.lookup constraints c of | 
| 133 | SOME T => T | |
| 25041 | 134 | | NONE => type_scheme consts c); | 
| 18935 | 135 | |
| 136 | ||
| 19657 | 137 | (* name space and syntax *) | 
| 19364 | 138 | |
| 56025 | 139 | fun space_of (Consts {decls, ...}) = Name_Space.space_of_table decls;
 | 
| 19364 | 140 | |
| 56025 | 141 | fun alias naming binding name = map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 142 | ((Name_Space.alias_table naming binding name decls), constraints, rev_abbrevs)); | |
| 35680 | 143 | |
| 33158 | 144 | val is_concealed = Name_Space.is_concealed o space_of; | 
| 145 | ||
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 146 | val intern = Name_Space.intern o space_of; | 
| 19364 | 147 | |
| 35554 | 148 | fun intern_syntax consts s = | 
| 42290 
b1f544c84040
discontinued special treatment of structure Lexicon;
 wenzelm parents: 
42083diff
changeset | 149 | (case try Lexicon.unmark_const s of | 
| 35255 | 150 | SOME c => c | 
| 35554 | 151 | | NONE => intern consts s); | 
| 152 | ||
| 18060 | 153 | |
| 55843 
3fa61f39d1f2
prefer Name_Space.check with its builtin reports (including completion);
 wenzelm parents: 
50768diff
changeset | 154 | (* check_const *) | 
| 19364 | 155 | |
| 55956 
94d384d621b0
reject internal term names outright, and complete consts instead;
 wenzelm parents: 
55950diff
changeset | 156 | fun check_const context consts (xname, ps) = | 
| 19364 | 157 | let | 
| 55843 
3fa61f39d1f2
prefer Name_Space.check with its builtin reports (including completion);
 wenzelm parents: 
50768diff
changeset | 158 |     val Consts {decls, ...} = consts;
 | 
| 55956 
94d384d621b0
reject internal term names outright, and complete consts instead;
 wenzelm parents: 
55950diff
changeset | 159 | 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 | 160 | 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 | 161 | in (Const (c, T), reports) end; | 
| 19364 | 162 | |
| 163 | ||
| 164 | (* certify *) | |
| 165 | ||
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
56139diff
changeset | 166 | fun certify context tsig do_expand consts = | 
| 18965 | 167 | let | 
| 168 | fun err msg (c, T) = | |
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42381diff
changeset | 169 | raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ | 
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
56139diff
changeset | 170 | Syntax.string_of_typ (Syntax.init_pretty context) T, [], []); | 
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 171 | val certT = Type.cert_typ tsig; | 
| 18965 | 172 | fun cert tm = | 
| 173 | let | |
| 174 | val (head, args) = Term.strip_comb tm; | |
| 21694 | 175 | val args' = map cert args; | 
| 176 | fun comb head' = Term.list_comb (head', args'); | |
| 18965 | 177 | in | 
| 178 | (case head of | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 179 | Abs (x, T, t) => comb (Abs (x, certT T, cert t)) | 
| 19364 | 180 | | Const (c, T) => | 
| 181 | let | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 182 | val T' = certT T; | 
| 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 | 183 |               val (_, ({T = U, ...}, abbr)) = the_entry consts c;
 | 
| 25048 | 184 | fun expand u = | 
| 185 | Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ => | |
| 186 | err "Illegal type for abbreviation" (c, T), args'); | |
| 19364 | 187 | in | 
| 19433 
c7a2b7a8c4cb
certify: ignore sort constraints of declarations (MAJOR CHANGE);
 wenzelm parents: 
19364diff
changeset | 188 | if not (Type.raw_instance (T', U)) then | 
| 19364 | 189 | err "Illegal type for constant" (c, T) | 
| 190 | else | |
| 24909 | 191 | (case abbr of | 
| 25048 | 192 |                   SOME {rhs, normal_rhs, force_expand} =>
 | 
| 193 | if do_expand then expand normal_rhs | |
| 194 | else if force_expand then expand rhs | |
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 195 | else comb head | 
| 19364 | 196 | | _ => comb head) | 
| 197 | end | |
| 198 | | _ => comb head) | |
| 18965 | 199 | end; | 
| 200 | in cert end; | |
| 201 | ||
| 202 | ||
| 18060 | 203 | (* typargs -- view actual const type as instance of declaration *) | 
| 204 | ||
| 24909 | 205 | local | 
| 206 | ||
| 207 | fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos | |
| 208 | | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos) | |
| 209 | | args_of (TFree _) _ = I | |
| 210 | and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is | |
| 211 | | args_of_list [] _ _ = I; | |
| 212 | ||
| 19364 | 213 | fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is | 
| 214 | | subscript T [] = T | |
| 32784 | 215 | | subscript _ _ = raise Subscript; | 
| 18060 | 216 | |
| 24909 | 217 | in | 
| 218 | ||
| 219 | fun typargs_of T = map #2 (rev (args_of T [] [])); | |
| 220 | ||
| 19364 | 221 | fun typargs consts (c, T) = map (subscript T) (type_arguments consts c); | 
| 18060 | 222 | |
| 24909 | 223 | end; | 
| 224 | ||
| 18163 | 225 | fun instance consts (c, Ts) = | 
| 226 | let | |
| 25041 | 227 | val declT = type_scheme consts c; | 
| 18163 | 228 | val vars = map Term.dest_TVar (typargs consts (c, declT)); | 
| 40722 
441260986b63
make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
 wenzelm parents: 
37146diff
changeset | 229 | val inst = vars ~~ Ts handle ListPair.UnequalLengths => | 
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 230 |       raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
 | 
| 31977 | 231 | in declT |> Term_Subst.instantiateT inst end; | 
| 18163 | 232 | |
| 70785 | 233 | fun dummy_types consts = | 
| 234 | let | |
| 235 | fun dummy (Const (c, T)) = | |
| 236 | Const (c, instance consts (c, replicate (length (typargs consts (c, T))) dummyT)) | |
| 237 | | dummy (Free (x, _)) = Free (x, dummyT) | |
| 238 | | dummy (Var (xi, _)) = Var (xi, dummyT) | |
| 239 | | dummy (b as Bound _) = b | |
| 240 | | dummy (t $ u) = dummy t $ dummy u | |
| 241 | | dummy (Abs (a, _, b)) = Abs (a, dummyT, dummy b); | |
| 242 | in dummy end; | |
| 243 | ||
| 18060 | 244 | |
| 245 | ||
| 19364 | 246 | (** build consts **) | 
| 18060 | 247 | |
| 18935 | 248 | (* name space *) | 
| 18060 | 249 | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 250 | fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 56025 | 251 | (Name_Space.hide_table fully c decls, constraints, rev_abbrevs)); | 
| 18935 | 252 | |
| 253 | ||
| 254 | (* declarations *) | |
| 255 | ||
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
45666diff
changeset | 256 | fun declare context (b, declT) = | 
| 33092 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 257 | map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 258 | let | 
| 35255 | 259 |       val decl = {T = declT, typargs = typargs_of declT};
 | 
| 41254 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
40722diff
changeset | 260 | 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 | 261 | 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 | 262 | in (decls', constraints, rev_abbrevs) end); | 
| 19364 | 263 | |
| 264 | ||
| 265 | (* constraints *) | |
| 266 | ||
| 267 | fun constrain (c, C) consts = | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 268 | 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 | 269 | (#2 (the_entry consts c) handle TYPE (msg, _, _) => error msg; | 
| 19364 | 270 | (decls, | 
| 271 | 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 | 272 | rev_abbrevs))); | 
| 18060 | 273 | |
| 18935 | 274 | |
| 275 | (* abbreviations *) | |
| 276 | ||
| 19027 | 277 | local | 
| 278 | ||
| 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 | 279 | 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 | 280 | 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 | 281 | 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 | 282 | | strip_abss t = ([], t); | 
| 19027 | 283 | |
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 284 | fun rev_abbrev lhs rhs = | 
| 18060 | 285 | 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 | 286 | 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 | 287 | 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 | 288 | in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end; | 
| 19027 | 289 | |
| 290 | in | |
| 18965 | 291 | |
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
45666diff
changeset | 292 | fun abbreviate context tsig mode (b, raw_rhs) consts = | 
| 18965 | 293 | let | 
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
56139diff
changeset | 294 | val cert_term = certify context tsig false consts; | 
| 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
56139diff
changeset | 295 | val expand_term = certify context tsig true consts; | 
| 37146 
f652333bbf8e
renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
 wenzelm parents: 
35680diff
changeset | 296 | val force_expand = mode = Print_Mode.internal; | 
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 297 | |
| 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 | 298 | val _ = Term.exists_subterm Term.is_Var raw_rhs andalso | 
| 42381 
309ec68442c6
added Binding.print convenience, which includes quote already;
 wenzelm parents: 
42375diff
changeset | 299 |       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 | 300 | |
| 19364 | 301 | val rhs = raw_rhs | 
| 20548 
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
 wenzelm parents: 
20509diff
changeset | 302 | |> Term.map_types (Type.cert_typ tsig) | 
| 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 | 303 | |> cert_term | 
| 
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 | 304 | |> Term.close_schematic_term; | 
| 25404 | 305 | val normal_rhs = expand_term rhs; | 
| 21794 | 306 | 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 | 307 | val lhs = Const (Name_Space.full_name (Name_Space.naming_of context) b, T); | 
| 19364 | 308 | in | 
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 309 | consts |> map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 19364 | 310 | let | 
| 35255 | 311 |         val decl = {T = T, typargs = typargs_of T};
 | 
| 25404 | 312 |         val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
 | 
| 41254 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
40722diff
changeset | 313 | val _ = Binding.check b; | 
| 28861 | 314 | val (_, decls') = decls | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
45666diff
changeset | 315 | |> Name_Space.define context true (b, (decl, SOME abbr)); | 
| 19364 | 316 | val rev_abbrevs' = rev_abbrevs | 
| 33373 | 317 | |> update_abbrevs mode (rev_abbrev lhs rhs); | 
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 318 | in (decls', constraints, rev_abbrevs') end) | 
| 21794 | 319 | |> pair (lhs, rhs) | 
| 19364 | 320 | end; | 
| 18935 | 321 | |
| 25048 | 322 | fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 323 | let | |
| 324 | val (T, rhs) = the_abbreviation consts c; | |
| 325 | val rev_abbrevs' = rev_abbrevs | |
| 33373 | 326 | |> update_abbrevs mode (rev_abbrev (Const (c, T)) rhs); | 
| 25048 | 327 | in (decls, constraints, rev_abbrevs') end); | 
| 328 | ||
| 19027 | 329 | end; | 
| 330 | ||
| 18060 | 331 | |
| 332 | (* empty and merge *) | |
| 333 | ||
| 45666 | 334 | val empty = | 
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
48992diff
changeset | 335 | make_consts (Name_Space.empty_table Markup.constantN, Symtab.empty, Symtab.empty); | 
| 18060 | 336 | |
| 337 | fun merge | |
| 30284 
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
 wenzelm parents: 
30280diff
changeset | 338 |    (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 | 339 |     Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
 | 
| 18060 | 340 | let | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 341 | 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 | 342 | val constraints' = Symtab.merge (K true) (constraints1, constraints2); | 
| 30566 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 343 | 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 | 344 | in make_consts (decls', constraints', rev_abbrevs') end; | 
| 18060 | 345 | |
| 346 | end; |