| author | wenzelm | 
| Wed, 03 Mar 2010 00:28:22 +0100 | |
| changeset 35429 | afa8cf9e63d8 | 
| parent 35262 | 9ea4445d2ccf | 
| child 35554 | 1e05ea0a5cd7 | 
| 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} | 
| 25048 | 16 | val the_type: T -> string -> typ (*exception TYPE*) | 
| 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 | 
| 33158 | 22 | val is_concealed: T -> string -> bool | 
| 19364 | 23 | val intern: T -> xstring -> string | 
| 35255 | 24 | val intern_syntax: T -> xstring -> string | 
| 19364 | 25 | val extern: T -> string -> xstring | 
| 18965 | 26 | val read_const: T -> string -> term | 
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 27 | val certify: Pretty.pp -> Type.tsig -> bool -> T -> term -> term (*exception TYPE*) | 
| 18146 | 28 | val typargs: T -> string * typ -> typ list | 
| 18163 | 29 | val instance: T -> string * typ list -> typ | 
| 35255 | 30 | val declare: Name_Space.naming -> binding * typ -> T -> T | 
| 19098 
fc736dbbe333
constrain: assert const declaration, optional type (i.e. may delete constraints);
 wenzelm parents: 
19027diff
changeset | 31 | val constrain: string * typ option -> T -> T | 
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33158diff
changeset | 32 | val abbreviate: Pretty.pp -> Type.tsig -> Name_Space.naming -> string -> | 
| 29581 | 33 | binding * term -> T -> (term * term) * T | 
| 25048 | 34 | val revert_abbrev: string -> string -> T -> T | 
| 18060 | 35 | val hide: bool -> string -> T -> T | 
| 36 | val empty: T | |
| 37 | val merge: T * T -> T | |
| 19364 | 38 | end; | 
| 18060 | 39 | |
| 40 | structure Consts: CONSTS = | |
| 41 | struct | |
| 42 | ||
| 19364 | 43 | (** consts type **) | 
| 44 | ||
| 45 | (* datatype T *) | |
| 18060 | 46 | |
| 35255 | 47 | type decl = {T: typ, typargs: int list list};
 | 
| 24909 | 48 | type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
 | 
| 18935 | 49 | |
| 18060 | 50 | datatype T = Consts of | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 51 |  {decls: (decl * abbrev option) Name_Space.table,
 | 
| 19364 | 52 | constraints: typ Symtab.table, | 
| 30566 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 53 | rev_abbrevs: (term * term) Item_Net.T Symtab.table}; | 
| 18060 | 54 | |
| 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 | 55 | 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 | 56 |    (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 | 57 |     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 | 58 | 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 | 59 | 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 | 60 | 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 | 61 | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 62 | 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 | 63 |   Consts {decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs};
 | 
| 18060 | 64 | |
| 30284 
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
 wenzelm parents: 
30280diff
changeset | 65 | 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 | 66 | make_consts (f (decls, constraints, rev_abbrevs)); | 
| 19364 | 67 | |
| 30566 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 68 | |
| 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 69 | (* reverted abbrevs *) | 
| 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 70 | |
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33158diff
changeset | 71 | val empty_abbrevs = | 
| 33373 | 72 | 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 | 73 | |
| 33373 | 74 | fun update_abbrevs mode abbrs = | 
| 75 | Symtab.map_default (mode, empty_abbrevs) (Item_Net.update abbrs); | |
| 30566 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 76 | |
| 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 77 | fun retrieve_abbrevs (Consts {rev_abbrevs, ...}) modes =
 | 
| 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 78 | let val nets = map_filter (Symtab.lookup rev_abbrevs) modes | 
| 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 79 | in fn t => maps (fn net => Item_Net.retrieve net t) nets end; | 
| 19364 | 80 | |
| 18965 | 81 | |
| 19364 | 82 | (* dest consts *) | 
| 83 | ||
| 30284 
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
 wenzelm parents: 
30280diff
changeset | 84 | fun dest (Consts {decls = (space, decls), constraints, ...}) =
 | 
| 19364 | 85 |  {constants = (space,
 | 
| 33092 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 86 |     Symtab.fold (fn (c, ({T, ...}, abbr)) =>
 | 
| 25404 | 87 | Symtab.update (c, (T, Option.map #rhs abbr))) decls Symtab.empty), | 
| 18060 | 88 | constraints = (space, constraints)}; | 
| 89 | ||
| 90 | ||
| 91 | (* lookup consts *) | |
| 92 | ||
| 30284 
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
 wenzelm parents: 
30280diff
changeset | 93 | fun the_const (Consts {decls = (_, tab), ...}) c =
 | 
| 19364 | 94 | (case Symtab.lookup tab c of | 
| 33092 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 95 | SOME decl => decl | 
| 25041 | 96 |   | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
 | 
| 18935 | 97 | |
| 25041 | 98 | fun the_type consts c = | 
| 24772 | 99 | (case the_const consts c of | 
| 24909 | 100 |     ({T, ...}, NONE) => T
 | 
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 101 |   | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
 | 
| 18060 | 102 | |
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 103 | fun the_abbreviation consts c = | 
| 24772 | 104 | (case the_const consts c of | 
| 25048 | 105 |     ({T, ...}, SOME {rhs, ...}) => (T, rhs)
 | 
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 106 |   | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
 | 
| 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 107 | |
| 25041 | 108 | val the_decl = #1 oo the_const; | 
| 109 | val type_scheme = #T oo the_decl; | |
| 110 | val type_arguments = #typargs oo the_decl; | |
| 111 | ||
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 112 | val is_monomorphic = null oo type_arguments; | 
| 18935 | 113 | |
| 30284 
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
 wenzelm parents: 
30280diff
changeset | 114 | fun the_constraint (consts as Consts {constraints, ...}) c =
 | 
| 18060 | 115 | (case Symtab.lookup constraints c of | 
| 116 | SOME T => T | |
| 25041 | 117 | | NONE => type_scheme consts c); | 
| 18935 | 118 | |
| 119 | ||
| 19657 | 120 | (* name space and syntax *) | 
| 19364 | 121 | |
| 30284 
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
 wenzelm parents: 
30280diff
changeset | 122 | fun space_of (Consts {decls = (space, _), ...}) = space;
 | 
| 19364 | 123 | |
| 33158 | 124 | val is_concealed = Name_Space.is_concealed o space_of; | 
| 125 | ||
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 126 | val intern = Name_Space.intern o space_of; | 
| 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 127 | val extern = Name_Space.extern o space_of; | 
| 19364 | 128 | |
| 35255 | 129 | fun intern_syntax consts name = | 
| 35262 
9ea4445d2ccf
slightly more abstract syntax mark/unmark operations;
 wenzelm parents: 
35255diff
changeset | 130 | (case try Syntax.unmark_const name of | 
| 35255 | 131 | SOME c => c | 
| 132 | | NONE => intern consts name); | |
| 21181 | 133 | |
| 18060 | 134 | |
| 19364 | 135 | (* read_const *) | 
| 136 | ||
| 137 | fun read_const consts raw_c = | |
| 138 | let | |
| 139 | val c = intern consts raw_c; | |
| 25041 | 140 | val T = type_scheme consts c handle TYPE (msg, _, _) => error msg; | 
| 21205 | 141 | in Const (c, T) end; | 
| 19364 | 142 | |
| 143 | ||
| 144 | (* certify *) | |
| 145 | ||
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 146 | fun certify pp tsig do_expand consts = | 
| 18965 | 147 | let | 
| 148 | fun err msg (c, T) = | |
| 149 | raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []); | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 150 | val certT = Type.cert_typ tsig; | 
| 18965 | 151 | fun cert tm = | 
| 152 | let | |
| 153 | val (head, args) = Term.strip_comb tm; | |
| 21694 | 154 | val args' = map cert args; | 
| 155 | fun comb head' = Term.list_comb (head', args'); | |
| 18965 | 156 | in | 
| 157 | (case head of | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 158 | Abs (x, T, t) => comb (Abs (x, certT T, cert t)) | 
| 19364 | 159 | | Const (c, T) => | 
| 160 | let | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 161 | val T' = certT T; | 
| 24909 | 162 |               val ({T = U, ...}, abbr) = the_const consts c;
 | 
| 25048 | 163 | fun expand u = | 
| 164 | Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ => | |
| 165 | err "Illegal type for abbreviation" (c, T), args'); | |
| 19364 | 166 | in | 
| 19433 
c7a2b7a8c4cb
certify: ignore sort constraints of declarations (MAJOR CHANGE);
 wenzelm parents: 
19364diff
changeset | 167 | if not (Type.raw_instance (T', U)) then | 
| 19364 | 168 | err "Illegal type for constant" (c, T) | 
| 169 | else | |
| 24909 | 170 | (case abbr of | 
| 25048 | 171 |                   SOME {rhs, normal_rhs, force_expand} =>
 | 
| 172 | if do_expand then expand normal_rhs | |
| 173 | else if force_expand then expand rhs | |
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 174 | else comb head | 
| 19364 | 175 | | _ => comb head) | 
| 176 | end | |
| 177 | | _ => comb head) | |
| 18965 | 178 | end; | 
| 179 | in cert end; | |
| 180 | ||
| 181 | ||
| 18060 | 182 | (* typargs -- view actual const type as instance of declaration *) | 
| 183 | ||
| 24909 | 184 | local | 
| 185 | ||
| 186 | fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos | |
| 187 | | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos) | |
| 188 | | args_of (TFree _) _ = I | |
| 189 | and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is | |
| 190 | | args_of_list [] _ _ = I; | |
| 191 | ||
| 19364 | 192 | fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is | 
| 193 | | subscript T [] = T | |
| 32784 | 194 | | subscript _ _ = raise Subscript; | 
| 18060 | 195 | |
| 24909 | 196 | in | 
| 197 | ||
| 198 | fun typargs_of T = map #2 (rev (args_of T [] [])); | |
| 199 | ||
| 19364 | 200 | fun typargs consts (c, T) = map (subscript T) (type_arguments consts c); | 
| 18060 | 201 | |
| 24909 | 202 | end; | 
| 203 | ||
| 18163 | 204 | fun instance consts (c, Ts) = | 
| 205 | let | |
| 25041 | 206 | val declT = type_scheme consts c; | 
| 18163 | 207 | val vars = map Term.dest_TVar (typargs consts (c, declT)); | 
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 208 | val inst = vars ~~ Ts handle UnequalLengths => | 
| 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 209 |       raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
 | 
| 31977 | 210 | in declT |> Term_Subst.instantiateT inst end; | 
| 18163 | 211 | |
| 18060 | 212 | |
| 213 | ||
| 19364 | 214 | (** build consts **) | 
| 18060 | 215 | |
| 18935 | 216 | (* name space *) | 
| 18060 | 217 | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 218 | 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 | 219 | (apfst (Name_Space.hide fully c) decls, constraints, rev_abbrevs)); | 
| 18935 | 220 | |
| 221 | ||
| 222 | (* declarations *) | |
| 223 | ||
| 35255 | 224 | fun declare naming (b, declT) = | 
| 33092 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 225 | map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 226 | let | 
| 35255 | 227 |       val decl = {T = declT, typargs = typargs_of declT};
 | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 228 | val (_, decls') = decls |> Name_Space.define true naming (b, (decl, NONE)); | 
| 33092 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 229 | in (decls', constraints, rev_abbrevs) end); | 
| 19364 | 230 | |
| 231 | ||
| 232 | (* constraints *) | |
| 233 | ||
| 234 | fun constrain (c, C) consts = | |
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 235 | consts |> map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 19364 | 236 | (the_const consts c handle TYPE (msg, _, _) => error msg; | 
| 237 | (decls, | |
| 238 | 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 | 239 | rev_abbrevs))); | 
| 18060 | 240 | |
| 18935 | 241 | |
| 242 | (* abbreviations *) | |
| 243 | ||
| 19027 | 244 | local | 
| 245 | ||
| 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 | 246 | fun strip_abss (t as Abs (x, T, b)) = | 
| 
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 | 247 | if Term.loose_bvar1 (b, 0) then strip_abss b |>> cons (x, 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 | 248 | 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 | 249 | | strip_abss t = ([], t); | 
| 19027 | 250 | |
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 251 | fun rev_abbrev lhs rhs = | 
| 18060 | 252 | 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 | 253 | 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 | 254 | 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 | 255 | in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end; | 
| 19027 | 256 | |
| 257 | in | |
| 18965 | 258 | |
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33158diff
changeset | 259 | fun abbreviate pp tsig naming mode (b, raw_rhs) consts = | 
| 18965 | 260 | let | 
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 261 | val cert_term = certify pp tsig false consts; | 
| 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 262 | val expand_term = certify pp tsig true consts; | 
| 25116 | 263 | val force_expand = mode = PrintMode.internal; | 
| 21720 
059e6b8cee8e
abbreviate: always authentic, force expansion of internal abbreviations;
 wenzelm parents: 
21694diff
changeset | 264 | |
| 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 | 265 | val _ = Term.exists_subterm Term.is_Var raw_rhs andalso | 
| 33092 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32784diff
changeset | 266 |       error ("Illegal schematic variables on rhs of abbreviation " ^ quote (Binding.str_of 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 | 267 | |
| 19364 | 268 | val rhs = raw_rhs | 
| 20548 
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
 wenzelm parents: 
20509diff
changeset | 269 | |> 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 | 270 | |> 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 | 271 | |> Term.close_schematic_term; | 
| 25404 | 272 | val normal_rhs = expand_term rhs; | 
| 21794 | 273 | val T = Term.fastype_of rhs; | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 274 | val lhs = Const (Name_Space.full_name naming b, T); | 
| 19364 | 275 | in | 
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 276 | consts |> map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 19364 | 277 | let | 
| 35255 | 278 |         val decl = {T = T, typargs = typargs_of T};
 | 
| 25404 | 279 |         val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
 | 
| 28861 | 280 | val (_, decls') = decls | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 281 | |> Name_Space.define true naming (b, (decl, SOME abbr)); | 
| 19364 | 282 | val rev_abbrevs' = rev_abbrevs | 
| 33373 | 283 | |> update_abbrevs mode (rev_abbrev lhs rhs); | 
| 24673 
62b75508eb66
certify: do_expand as explicit argument, actually certify type of abstractions;
 wenzelm parents: 
23655diff
changeset | 284 | in (decls', constraints, rev_abbrevs') end) | 
| 21794 | 285 | |> pair (lhs, rhs) | 
| 19364 | 286 | end; | 
| 18935 | 287 | |
| 25048 | 288 | fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) => | 
| 289 | let | |
| 290 | val (T, rhs) = the_abbreviation consts c; | |
| 291 | val rev_abbrevs' = rev_abbrevs | |
| 33373 | 292 | |> update_abbrevs mode (rev_abbrev (Const (c, T)) rhs); | 
| 25048 | 293 | in (decls, constraints, rev_abbrevs') end); | 
| 294 | ||
| 19027 | 295 | end; | 
| 296 | ||
| 18060 | 297 | |
| 298 | (* empty and merge *) | |
| 299 | ||
| 33096 | 300 | val empty = make_consts (Name_Space.empty_table "constant", Symtab.empty, Symtab.empty); | 
| 18060 | 301 | |
| 302 | fun merge | |
| 30284 
907da436f8a9
eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
 wenzelm parents: 
30280diff
changeset | 303 |    (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 | 304 |     Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
 | 
| 18060 | 305 | let | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 306 | val decls' = Name_Space.merge_tables (decls1, decls2); | 
| 25037 | 307 | val constraints' = Symtab.join (K fst) (constraints1, constraints2); | 
| 30566 
9643f54c4184
reverted abbreviations: improved performance via Item_Net.T;
 wenzelm parents: 
30466diff
changeset | 308 | 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 | 309 | in make_consts (decls', constraints', rev_abbrevs') end; | 
| 18060 | 310 | |
| 311 | end; |