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