| author | wenzelm | 
| Tue, 06 Jul 1999 21:06:03 +0200 | |
| changeset 6903 | 682f8a9ec75d | 
| parent 6895 | 450b1f67f099 | 
| child 6931 | bd8aa6ae6bcd | 
| permissions | -rw-r--r-- | 
| 5819 | 1 | (* Title: Pure/Isar/proof_context.ML | 
| 2 | ID: $Id$ | |
| 3 | Author: Markus Wenzel, TU Muenchen | |
| 4 | ||
| 5 | Proof context information. | |
| 6 | *) | |
| 7 | ||
| 8 | signature PROOF_CONTEXT = | |
| 9 | sig | |
| 10 | type context | |
| 11 | exception CONTEXT of string * context | |
| 12 | val theory_of: context -> theory | |
| 13 | val sign_of: context -> Sign.sg | |
| 6528 | 14 | val verbose: bool ref | 
| 5819 | 15 | val print_binds: context -> unit | 
| 16 | val print_thms: context -> unit | |
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 17 | val strings_of_context: context -> string list | 
| 5819 | 18 | val print_proof_data: theory -> unit | 
| 5874 | 19 | val init: theory -> context | 
| 5819 | 20 | val read_typ: context -> string -> typ | 
| 21 | val cert_typ: context -> typ -> typ | |
| 5874 | 22 | val read_termTs: context -> (string * typ) list -> term list * (indexname * typ) list | 
| 5819 | 23 | val read_term: context -> string -> term | 
| 24 | val read_prop: context -> string -> term | |
| 5935 | 25 | val read_term_pat: context -> string -> term | 
| 26 | val read_prop_pat: context -> string -> term | |
| 5819 | 27 | val cert_term: context -> term -> term | 
| 28 | val cert_prop: context -> term -> term | |
| 29 | val declare_term: term -> context -> context | |
| 30 | val declare_terms: term list -> context -> context | |
| 5874 | 31 | val declare_thm: thm -> context -> context | 
| 5819 | 32 | val add_binds: (indexname * string) list -> context -> context | 
| 33 | val add_binds_i: (indexname * term) list -> context -> context | |
| 5935 | 34 | val match_binds: (string list * string) list -> context -> context | 
| 35 | val match_binds_i: (term list * term) list -> context -> context | |
| 36 | val bind_propp: context * (string * string list) -> context * term | |
| 37 | val bind_propp_i: context * (term * term list) -> context * term | |
| 6789 | 38 | val auto_bind_goal: term -> context -> context | 
| 6797 | 39 | val auto_bind_facts: string -> term list -> context -> context | 
| 6091 | 40 | val thms_closure: context -> xstring -> thm list option | 
| 41 | val get_thm: context -> string -> thm | |
| 42 | val get_thms: context -> string -> thm list | |
| 43 | val get_thmss: context -> string list -> thm list | |
| 44 | val put_thm: string * thm -> context -> context | |
| 45 | val put_thms: string * thm list -> context -> context | |
| 46 | val put_thmss: (string * thm list) list -> context -> context | |
| 6875 | 47 | val have_thmss: thm list -> string -> context attribute list | 
| 6091 | 48 | -> (thm list * context attribute list) list -> context -> context * (string * thm list) | 
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 49 | val assumptions: context -> (cterm * (int -> tactic)) list | 
| 5819 | 50 | val fixed_names: context -> string list | 
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 51 | val assume: (int -> tactic) -> string -> context attribute list -> (string * string list) list | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 52 | -> context -> context * ((string * thm list) * thm list) | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 53 | val assume_i: (int -> tactic) -> string -> context attribute list -> (term * term list) list | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 54 | -> context -> context * ((string * thm list) * thm list) | 
| 5819 | 55 | val fix: (string * string option) list -> context -> context | 
| 56 | val fix_i: (string * typ) list -> context -> context | |
| 6895 | 57 | val transfer_used_names: context -> context -> context | 
| 5819 | 58 | val setup: (theory -> theory) list | 
| 59 | end; | |
| 60 | ||
| 61 | signature PROOF_CONTEXT_PRIVATE = | |
| 62 | sig | |
| 63 | include PROOF_CONTEXT | |
| 64 | val init_data: Object.kind -> (theory -> Object.T) * (context -> Object.T -> unit) | |
| 65 | -> theory -> theory | |
| 66 | val print_data: Object.kind -> context -> unit | |
| 67 | val get_data: Object.kind -> (Object.T -> 'a) -> context -> 'a | |
| 68 |   val put_data: Object.kind -> ('a -> Object.T) -> 'a -> context -> context
 | |
| 69 | end; | |
| 70 | ||
| 71 | structure ProofContext: PROOF_CONTEXT_PRIVATE = | |
| 72 | struct | |
| 73 | ||
| 74 | ||
| 75 | (** datatype context **) | |
| 76 | ||
| 77 | datatype context = | |
| 78 | Context of | |
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 79 |    {thy: theory,                                                        (*current theory*)
 | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 80 | data: Object.T Symtab.table, (*user data*) | 
| 5819 | 81 | asms: | 
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 82 | ((cterm * (int -> tactic)) list * (string * thm list) list) * (*assumes: A ==> _*) | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 83 | ((string * string) list * string list), (*fixes: !!x. _*) | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 84 | binds: (term * typ) Vartab.table, (*term bindings*) | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 85 | thms: thm list Symtab.table, (*local thms*) | 
| 5819 | 86 | defs: | 
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 87 | typ Vartab.table * (*type constraints*) | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 88 | sort Vartab.table * (*default sorts*) | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 89 | int * (*maxidx*) | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 90 | string list}; (*used type var names*) | 
| 5819 | 91 | |
| 92 | exception CONTEXT of string * context; | |
| 93 | ||
| 94 | ||
| 95 | fun make_context (thy, data, asms, binds, thms, defs) = | |
| 96 |   Context {thy = thy, data = data, asms = asms, binds = binds, thms = thms, defs = defs};
 | |
| 97 | ||
| 98 | fun map_context f (Context {thy, data, asms, binds, thms, defs}) =
 | |
| 99 | make_context (f (thy, data, asms, binds, thms, defs)); | |
| 100 | ||
| 101 | fun theory_of (Context {thy, ...}) = thy;
 | |
| 102 | val sign_of = Theory.sign_of o theory_of; | |
| 103 | ||
| 104 | ||
| 105 | ||
| 106 | (** print context information **) | |
| 107 | ||
| 6528 | 108 | val verbose = ref false; | 
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 109 | fun verb f x = if ! verbose then f x else []; | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 110 | val verb_string = verb (Library.single o Pretty.string_of); | 
| 5819 | 111 | |
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 112 | fun strings_of_items prt name items = | 
| 5819 | 113 | let | 
| 114 | fun pretty_itms (name, [x]) = Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt x] | |
| 115 | | pretty_itms (name, xs) = Pretty.big_list (name ^ ":") (map prt xs); | |
| 6721 | 116 | in | 
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 117 | if null items andalso not (! verbose) then [] | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 118 | else [Pretty.string_of (Pretty.big_list name (map pretty_itms items))] | 
| 6721 | 119 | end; | 
| 5819 | 120 | |
| 121 | ||
| 122 | (* term bindings *) | |
| 123 | ||
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 124 | fun strings_of_binds (Context {thy, binds, ...}) =
 | 
| 5819 | 125 | let | 
| 126 | val prt_term = Sign.pretty_term (Theory.sign_of thy); | |
| 127 | ||
| 128 | fun fix_var (x, i) = | |
| 129 | (case try Syntax.dest_binding x of | |
| 130 | None => Syntax.string_of_vname (x, i) | |
| 131 | | Some x' => if i = 0 then "??" ^ x' else Syntax.string_of_vname (x, i)); | |
| 132 | fun pretty_bind (xi, (t, T)) = Pretty.block | |
| 133 | [Pretty.str (fix_var xi), Pretty.str " ==", Pretty.brk 1, prt_term t]; | |
| 6721 | 134 | in | 
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 135 | if Vartab.is_empty binds andalso not (! verbose) then [] | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 136 | else [Pretty.string_of (Pretty.big_list "Term bindings:" (map pretty_bind (Vartab.dest binds)))] | 
| 6721 | 137 | end; | 
| 5819 | 138 | |
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 139 | val print_binds = seq writeln o strings_of_binds; | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 140 | |
| 5819 | 141 | |
| 142 | (* local theorems *) | |
| 143 | ||
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 144 | fun strings_of_thms (Context {thms, ...}) =
 | 
| 6870 | 145 | strings_of_items (setmp Display.show_hyps false Display.pretty_thm) | 
| 146 | "Local theorems:" (Symtab.dest thms); | |
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 147 | |
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 148 | val print_thms = seq writeln o strings_of_thms; | 
| 5819 | 149 | |
| 150 | ||
| 151 | (* main context *) | |
| 152 | ||
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 153 | fun strings_of_context (ctxt as Context {thy, data = _, asms = ((_, prems), (fixes, _)), binds = _,
 | 
| 5819 | 154 | thms = _, defs = (types, sorts, maxidx, used)}) = | 
| 155 | let | |
| 156 | val sign = Theory.sign_of thy; | |
| 157 | ||
| 158 | val prt_term = Sign.pretty_term sign; | |
| 159 | val prt_typ = Sign.pretty_typ sign; | |
| 160 | val prt_sort = Sign.pretty_sort sign; | |
| 161 | ||
| 162 | (*theory*) | |
| 163 | val pretty_thy = Pretty.block [Pretty.str "Theory:", Pretty.brk 1, Sign.pretty_sg sign]; | |
| 164 | ||
| 165 | (*fixes*) | |
| 166 | fun prt_fix (x, x') = Pretty.str (x ^ " = " ^ x'); | |
| 167 | ||
| 168 | (* defaults *) | |
| 169 | ||
| 170 | fun prt_atom prt prtT (x, X) = Pretty.block | |
| 171 | [prt x, Pretty.str " ::", Pretty.brk 1, prtT X]; | |
| 172 | ||
| 173 | fun prt_var (x, ~1) = prt_term (Syntax.free x) | |
| 174 | | prt_var xi = prt_term (Syntax.var xi); | |
| 175 | ||
| 176 | fun prt_varT (x, ~1) = prt_typ (TFree (x, [])) | |
| 177 | | prt_varT xi = prt_typ (TVar (xi, [])); | |
| 178 | ||
| 179 | val prt_defT = prt_atom prt_var prt_typ; | |
| 180 | val prt_defS = prt_atom prt_varT prt_sort; | |
| 181 | in | |
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 182 | verb_string pretty_thy @ | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 183 | (if null fixes andalso not (! verbose) then [] | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 184 | else [Pretty.string_of (Pretty.big_list "Fixed variables:" (map prt_fix (rev fixes)))]) @ | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 185 | strings_of_items (prt_term o #prop o Thm.rep_thm) "Assumptions:" prems @ | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 186 | verb strings_of_binds ctxt @ | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 187 | verb strings_of_thms ctxt @ | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 188 | verb_string (Pretty.big_list "Type constraints:" (map prt_defT (Vartab.dest types))) @ | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 189 | verb_string (Pretty.big_list "Default sorts:" (map prt_defS (Vartab.dest sorts))) @ | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 190 |     verb_string (Pretty.str ("Maxidx: " ^ string_of_int maxidx)) @
 | 
| 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 191 |     verb_string (Pretty.strs ("Used type variable names:" :: used))
 | 
| 5819 | 192 | end; | 
| 193 | ||
| 194 | ||
| 195 | ||
| 196 | (** user data **) | |
| 197 | ||
| 198 | (* errors *) | |
| 199 | ||
| 200 | fun of_theory thy = "\nof theory " ^ Sign.str_of_sg (Theory.sign_of thy); | |
| 201 | ||
| 202 | fun err_inconsistent kinds = | |
| 203 |   error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " proof data");
 | |
| 204 | ||
| 205 | fun err_dup_init thy kind = | |
| 206 |   error ("Duplicate initialization of " ^ quote kind ^ " proof data" ^ of_theory thy);
 | |
| 207 | ||
| 208 | fun err_undef ctxt kind = | |
| 209 |   raise CONTEXT ("Tried to access undefined " ^ quote kind ^ " proof data", ctxt);
 | |
| 210 | ||
| 211 | fun err_uninit ctxt kind = | |
| 212 |   raise CONTEXT ("Tried to access uninitialized " ^ quote kind ^ " proof data" ^
 | |
| 213 | of_theory (theory_of ctxt), ctxt); | |
| 214 | ||
| 215 | fun err_access ctxt kind = | |
| 216 |   raise CONTEXT ("Unauthorized access to " ^ quote kind ^ " proof data" ^
 | |
| 217 | of_theory (theory_of ctxt), ctxt); | |
| 218 | ||
| 219 | ||
| 220 | (* data kind 'Isar/proof_data' *) | |
| 221 | ||
| 222 | structure ProofDataDataArgs = | |
| 223 | struct | |
| 224 | val name = "Isar/proof_data"; | |
| 225 | type T = (Object.kind * ((theory -> Object.T) * (context -> Object.T -> unit))) Symtab.table; | |
| 226 | ||
| 227 | val empty = Symtab.empty; | |
| 6550 | 228 | val copy = I; | 
| 5819 | 229 | val prep_ext = I; | 
| 230 | fun merge tabs = Symtab.merge (Object.eq_kind o pairself fst) tabs | |
| 231 | handle Symtab.DUPS kinds => err_inconsistent kinds; | |
| 232 | fun print _ tab = Pretty.writeln (Pretty.strs (map #1 (Symtab.dest tab))); | |
| 233 | end; | |
| 234 | ||
| 235 | structure ProofDataData = TheoryDataFun(ProofDataDataArgs); | |
| 236 | val print_proof_data = ProofDataData.print; | |
| 237 | ||
| 238 | ||
| 239 | (* init proof data *) | |
| 240 | ||
| 241 | fun init_data kind meths thy = | |
| 242 | let | |
| 243 | val name = Object.name_of_kind kind; | |
| 244 | val tab = Symtab.update_new ((name, (kind, meths)), ProofDataData.get thy) | |
| 245 | handle Symtab.DUP _ => err_dup_init thy name; | |
| 246 | in thy |> ProofDataData.put tab end; | |
| 247 | ||
| 248 | ||
| 249 | (* access data *) | |
| 250 | ||
| 251 | fun lookup_data (ctxt as Context {data, ...}) kind =
 | |
| 252 | let | |
| 253 | val thy = theory_of ctxt; | |
| 254 | val name = Object.name_of_kind kind; | |
| 255 | in | |
| 256 | (case Symtab.lookup (ProofDataData.get thy, name) of | |
| 257 | Some (k, meths) => | |
| 258 | if Object.eq_kind (kind, k) then | |
| 259 | (case Symtab.lookup (data, name) of | |
| 260 | Some x => (x, meths) | |
| 261 | | None => err_undef ctxt name) | |
| 262 | else err_access ctxt name | |
| 263 | | None => err_uninit ctxt name) | |
| 264 | end; | |
| 265 | ||
| 266 | fun get_data kind f ctxt = | |
| 267 | let val (x, _) = lookup_data ctxt kind | |
| 268 | in f x handle Match => Object.kind_error kind end; | |
| 269 | ||
| 270 | fun print_data kind ctxt = | |
| 271 | let val (x, (_, prt)) = lookup_data ctxt kind | |
| 272 | in prt ctxt x end; | |
| 273 | ||
| 274 | fun put_data kind f x ctxt = | |
| 275 | (lookup_data ctxt kind; | |
| 276 | ctxt |> map_context (fn (thy, data, asms, binds, thms, defs) => | |
| 277 | (thy, Symtab.update ((Object.name_of_kind kind, f x), data), asms, binds, thms, defs))); | |
| 278 | ||
| 279 | ||
| 280 | (* init context *) | |
| 281 | ||
| 5874 | 282 | fun init thy = | 
| 283 | let val data = Symtab.map (fn (_, (f, _)) => f thy) (ProofDataData.get thy) in | |
| 6797 | 284 | make_context (thy, data, (([], []), ([], [])), Vartab.empty, Symtab.empty, | 
| 5819 | 285 | (Vartab.empty, Vartab.empty, ~1, [])) | 
| 286 | end; | |
| 287 | ||
| 288 | ||
| 289 | ||
| 290 | (** prepare types **) | |
| 291 | ||
| 292 | fun read_typ (ctxt as Context {defs = (_, sorts, _, _), ...}) s =
 | |
| 293 | let | |
| 294 | val sign = sign_of ctxt; | |
| 295 | fun def_sort xi = Vartab.lookup (sorts, xi); | |
| 296 | in | |
| 297 | transform_error (Sign.read_typ (sign, def_sort)) s | |
| 298 | handle ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt) | |
| 299 | end; | |
| 300 | ||
| 301 | fun cert_typ ctxt raw_T = | |
| 302 | Sign.certify_typ (sign_of ctxt) raw_T | |
| 303 | handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt); | |
| 304 | ||
| 305 | ||
| 306 | ||
| 307 | (** prepare terms and propositions **) | |
| 308 | ||
| 309 | (* | |
| 310 | (1) read / certify wrt. signature of context | |
| 311 | (2) intern Skolem constants | |
| 312 | (3) expand term bindings | |
| 313 | *) | |
| 314 | ||
| 315 | ||
| 316 | (* read / certify wrt. signature *) (*exception ERROR*) (*exception TERM*) | |
| 317 | ||
| 5874 | 318 | fun read_def_termTs freeze sg (types, sorts, used) sTs = | 
| 319 | let val (cts, env) = Thm.read_def_cterms (sg, types, sorts) used freeze sTs | |
| 320 | in (map Thm.term_of cts, env) end; | |
| 321 | ||
| 322 | fun read_def_termT freeze sg defs sT = apfst hd (read_def_termTs freeze sg defs [sT]); | |
| 323 | ||
| 5819 | 324 | |
| 6762 | 325 | fun read_term_sg freeze sg (defs as (_, _, used)) s = | 
| 326 | #1 (read_def_termT freeze sg defs (s, TVar ((variant used "'z", 0), logicS))); | |
| 5819 | 327 | |
| 6762 | 328 | fun read_prop_sg freeze sg defs s = | 
| 329 | #1 (read_def_termT freeze sg defs (s, propT)); | |
| 5819 | 330 | |
| 331 | ||
| 332 | fun cert_term_sg sg t = Thm.term_of (Thm.cterm_of sg t); | |
| 333 | ||
| 334 | fun cert_prop_sg sg tm = | |
| 335 | let | |
| 336 | val ctm = Thm.cterm_of sg tm; | |
| 337 |     val {t, T, ...} = Thm.rep_cterm ctm;
 | |
| 338 | in | |
| 339 | if T = propT then t | |
| 340 |     else raise TERM ("Term not of type prop", [t])
 | |
| 341 | end; | |
| 342 | ||
| 343 | ||
| 344 | (* intern_skolem *) | |
| 345 | ||
| 346 | fun get_skolem (Context {asms = (_, (fixes, _)), ...}) x = assoc (fixes, x);
 | |
| 347 | ||
| 348 | fun check_skolem ctxt check x = | |
| 349 | if check andalso can Syntax.dest_skolem x then | |
| 350 |     raise CONTEXT ("Illegal reference to internal Skolem constant: " ^ quote x, ctxt)
 | |
| 351 | else x; | |
| 352 | ||
| 353 | fun intern_skolem ctxt check = | |
| 354 | let | |
| 355 | fun intern (t as Free (x, T)) = | |
| 356 | (case get_skolem ctxt (check_skolem ctxt check x) of | |
| 357 | Some x' => Free (x', T) | |
| 358 | | None => t) | |
| 359 | | intern (t $ u) = intern t $ intern u | |
| 360 | | intern (Abs (x, T, t)) = Abs (x, T, intern t) | |
| 361 | | intern a = a; | |
| 362 | in intern end; | |
| 363 | ||
| 364 | ||
| 365 | (* norm_term *) | |
| 366 | ||
| 367 | (*beta normal form for terms (not eta normal form), chase variables in | |
| 368 | bindings environment (code taken from Pure/envir.ML)*) | |
| 369 | ||
| 370 | fun norm_term (ctxt as Context {binds, ...}) =
 | |
| 371 | let | |
| 372 | (*raised when norm has no effect on a term, to do sharing instead of copying*) | |
| 373 | exception SAME; | |
| 374 | ||
| 375 | fun norm (t as Var (xi, T)) = | |
| 376 | (case Vartab.lookup (binds, xi) of | |
| 377 | Some (u, U) => | |
| 378 | if T = U then (norm u handle SAME => u) | |
| 379 |               else raise TYPE ("norm_term: ill-typed variable assigment", [T, U], [t, u])
 | |
| 380 | | None => | |
| 381 | if can Syntax.dest_binding (#1 xi) then | |
| 382 |                 raise CONTEXT ("Unbound binding: " ^ Syntax.string_of_vname xi, ctxt)
 | |
| 383 | else raise SAME) | |
| 384 | | norm (Abs (a, T, body)) = Abs (a, T, norm body) | |
| 385 | | norm (Abs (_, _, body) $ t) = normh (subst_bound (t, body)) | |
| 386 | | norm (f $ t) = | |
| 387 | ((case norm f of | |
| 388 | Abs (_, _, body) => normh (subst_bound (t, body)) | |
| 389 | | nf => nf $ (norm t handle SAME => t)) handle SAME => f $ norm t) | |
| 390 | | norm _ = raise SAME | |
| 391 | and normh t = norm t handle SAME => t | |
| 392 | in normh end; | |
| 393 | ||
| 394 | ||
| 6550 | 395 | (* dummy patterns *) | 
| 396 | ||
| 6762 | 397 | local | 
| 398 | ||
| 6560 
1436349f8b28
renamed 'dummy' to 'dummy_pattern' (less dangerous);
 wenzelm parents: 
6550diff
changeset | 399 | fun is_dummy (Const (c, _)) = c = PureThy.dummy_patternN | 
| 6550 | 400 | | is_dummy _ = false; | 
| 401 | ||
| 6762 | 402 | fun prep_dummy (i, t) = | 
| 403 |   if is_dummy t then (i + 1, Var (("_dummy_", i), Term.fastype_of t)) else (i, t);
 | |
| 404 | ||
| 405 | in | |
| 406 | ||
| 407 | fun prepare_dummies tm = #2 (Term.foldl_map_aterms prep_dummy (1, tm)); | |
| 408 | ||
| 6550 | 409 | fun reject_dummies ctxt tm = | 
| 410 | if foldl_aterms (fn (ok, t) => ok andalso not (is_dummy t)) (true, tm) then tm | |
| 411 |   else raise CONTEXT ("Illegal dummy pattern(s) in term", ctxt);
 | |
| 412 | ||
| 6762 | 413 | end; | 
| 6550 | 414 | |
| 415 | ||
| 5819 | 416 | (* read terms *) | 
| 417 | ||
| 5874 | 418 | fun gen_read read app is_pat (ctxt as Context {binds, defs = (types, sorts, _, used), ...}) s =
 | 
| 5819 | 419 | let | 
| 420 | val sign = sign_of ctxt; | |
| 421 | ||
| 422 | fun def_type xi = | |
| 423 | (case Vartab.lookup (types, xi) of | |
| 424 | None => if is_pat then None else apsome #2 (Vartab.lookup (binds, xi)) | |
| 425 | | some => some); | |
| 426 | ||
| 427 | fun def_sort xi = Vartab.lookup (sorts, xi); | |
| 428 | in | |
| 429 | (transform_error (read sign (def_type, def_sort, used)) s | |
| 430 | handle TERM (msg, _) => raise CONTEXT (msg, ctxt) | |
| 431 | | ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt)) | |
| 5874 | 432 | |> app (intern_skolem ctxt true) | 
| 433 | |> app (if is_pat then I else norm_term ctxt) | |
| 6550 | 434 | |> app (if is_pat then prepare_dummies else (reject_dummies ctxt)) | 
| 5819 | 435 | end; | 
| 436 | ||
| 5874 | 437 | val read_termTs = gen_read (read_def_termTs false) (apfst o map) false; | 
| 6762 | 438 | val read_term = gen_read (read_term_sg true) I false; | 
| 439 | val read_prop = gen_read (read_prop_sg true) I false; | |
| 440 | val read_term_pat = gen_read (read_term_sg false) I true; | |
| 441 | val read_prop_pat = gen_read (read_prop_sg false) I true; | |
| 5819 | 442 | |
| 443 | ||
| 444 | (* certify terms *) | |
| 445 | ||
| 446 | fun gen_cert cert is_pat ctxt t = | |
| 447 | (cert (sign_of ctxt) t handle TERM (msg, _) => raise CONTEXT (msg, ctxt)) | |
| 448 | |> intern_skolem ctxt false | |
| 449 | |> (if is_pat then I else norm_term ctxt); | |
| 450 | ||
| 451 | val cert_term = gen_cert cert_term_sg false; | |
| 452 | val cert_prop = gen_cert cert_prop_sg false; | |
| 5935 | 453 | val cert_term_pat = gen_cert cert_term_sg true; | 
| 454 | val cert_prop_pat = gen_cert cert_prop_sg true; | |
| 5819 | 455 | |
| 456 | ||
| 457 | (* declare terms *) | |
| 458 | ||
| 459 | val ins_types = foldl_aterms | |
| 460 | (fn (types, Free (x, T)) => Vartab.update (((x, ~1), T), types) | |
| 461 | | (types, Var v) => Vartab.update (v, types) | |
| 462 | | (types, _) => types); | |
| 463 | ||
| 464 | val ins_sorts = foldl_types (foldl_atyps | |
| 465 | (fn (sorts, TFree (x, S)) => Vartab.update (((x, ~1), S), sorts) | |
| 466 | | (sorts, TVar v) => Vartab.update (v, sorts) | |
| 467 | | (sorts, _) => sorts)); | |
| 468 | ||
| 469 | val ins_used = foldl_types (foldl_atyps | |
| 470 | (fn (used, TFree (x, _)) => x ins used | |
| 471 | | (used, TVar ((x, _), _)) => x ins used | |
| 472 | | (used, _) => used)); | |
| 473 | ||
| 5994 | 474 | fun ins_skolem def_type = foldr | 
| 475 | (fn ((x, x'), types) => | |
| 476 | (case def_type x' of | |
| 477 | Some T => Vartab.update (((x, ~1), T), types) | |
| 478 | | None => types)); | |
| 479 | ||
| 5819 | 480 | fun map_defaults f = map_context | 
| 481 | (fn (thy, data, asms, binds, thms, defs) => (thy, data, asms, binds, thms, f defs)); | |
| 482 | ||
| 5994 | 483 | fun declare (ctxt as Context {asms = (_, (fixes, _)), ...}, t) =
 | 
| 5819 | 484 | ctxt | 
| 485 | |> map_defaults (fn (types, sorts, maxidx, used) => (ins_types (types, t), sorts, maxidx, used)) | |
| 486 | |> map_defaults (fn (types, sorts, maxidx, used) => (types, ins_sorts (sorts, t), maxidx, used)) | |
| 487 | |> map_defaults (fn (types, sorts, maxidx, used) => (types, sorts, maxidx, ins_used (used, t))) | |
| 488 | |> map_defaults (fn (types, sorts, maxidx, used) => | |
| 5994 | 489 | (types, sorts, Int.max (maxidx, Term.maxidx_of_term t), used)) | 
| 490 | |> map_defaults (fn (types, sorts, maxidx, used) => | |
| 491 | (ins_skolem (fn x => Vartab.lookup (types, (x, ~1))) (fixes, types), sorts, maxidx, used)); | |
| 5819 | 492 | |
| 493 | ||
| 494 | fun declare_term t ctxt = declare (ctxt, t); | |
| 495 | fun declare_terms ts ctxt = foldl declare (ctxt, ts); | |
| 496 | ||
| 5874 | 497 | fun declare_thm thm ctxt = | 
| 498 |   let val {prop, hyps, ...} = Thm.rep_thm thm
 | |
| 499 | in ctxt |> declare_terms (prop :: hyps) end; | |
| 500 | ||
| 5819 | 501 | |
| 502 | ||
| 503 | (** bindings **) | |
| 504 | ||
| 505 | (* update_binds *) | |
| 506 | ||
| 507 | fun upd_bind (ctxt, (xi, t)) = | |
| 508 | let val T = fastype_of t in | |
| 509 | ctxt | |
| 510 | |> declare_term t | |
| 511 | |> map_context (fn (thy, data, asms, binds, thms, defs) => | |
| 512 | (thy, data, asms, Vartab.update ((xi, (t, T)), binds), thms, defs)) | |
| 513 | end; | |
| 514 | ||
| 515 | fun update_binds bs ctxt = foldl upd_bind (ctxt, bs); | |
| 516 | fun update_binds_env env = update_binds (Envir.alist_of env); | |
| 517 | ||
| 518 | ||
| 519 | (* add_binds(_i) -- sequential *) | |
| 520 | ||
| 521 | fun gen_bind prep (ctxt, (xi as (x, _), raw_t)) = | |
| 522 | let val t = prep ctxt raw_t in | |
| 523 | if can Syntax.dest_binding x then ctxt |> update_binds [(xi, t)] | |
| 524 |     else raise CONTEXT ("Illegal variable name for term binding: " ^
 | |
| 525 | quote (Syntax.string_of_vname xi), ctxt) | |
| 526 | end; | |
| 527 | ||
| 528 | fun gen_binds prep binds ctxt = foldl (gen_bind prep) (ctxt, binds); | |
| 529 | ||
| 530 | val add_binds = gen_binds read_term; | |
| 531 | val add_binds_i = gen_binds cert_term; | |
| 532 | ||
| 533 | ||
| 534 | (* match_binds(_i) -- parallel *) | |
| 535 | ||
| 5935 | 536 | fun prep_declare_match (prep_pat, prep) (ctxt, (raw_pats, raw_t)) = | 
| 5819 | 537 | let | 
| 5935 | 538 | val t = prep ctxt raw_t; | 
| 5994 | 539 | val ctxt' = ctxt |> declare_term t; | 
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 540 | val pats = map (prep_pat ctxt') raw_pats; (* FIXME seq / par / simult (??) *) | 
| 5994 | 541 | in (ctxt', (map (rpair t) pats, t)) end; | 
| 5819 | 542 | |
| 5935 | 543 | fun gen_match_binds _ [] ctxt = ctxt | 
| 544 | | gen_match_binds prepp raw_pairs ctxt = | |
| 545 | let | |
| 546 | val (ctxt', matches) = foldl_map (prep_declare_match prepp) (ctxt, raw_pairs); | |
| 547 | val pairs = flat (map #1 matches); | |
| 548 |         val Context {defs = (_, _, maxidx, _), ...} = ctxt';
 | |
| 549 | val envs = Unify.smash_unifiers (sign_of ctxt', Envir.empty maxidx, pairs); | |
| 550 | val env = | |
| 551 | (case Seq.pull envs of | |
| 552 |             None => raise CONTEXT ("Pattern match failed!", ctxt')
 | |
| 553 | | Some (env, _) => env); | |
| 554 | in ctxt' |> update_binds_env env end; | |
| 555 | ||
| 556 | val match_binds = gen_match_binds (read_term_pat, read_term); | |
| 557 | val match_binds_i = gen_match_binds (cert_term_pat, cert_term); | |
| 558 | ||
| 559 | ||
| 560 | (* bind proposition patterns *) | |
| 561 | ||
| 562 | fun gen_bind_propp prepp (ctxt, (raw_prop, raw_pats)) = | |
| 563 | let val (ctxt', (pairs, prop)) = prep_declare_match prepp (ctxt, (raw_pats, raw_prop)) | |
| 564 | in (ctxt' |> match_binds_i (map (apfst single) pairs), prop) end; | |
| 565 | ||
| 566 | val bind_propp = gen_bind_propp (read_prop_pat, read_prop); | |
| 567 | val bind_propp_i = gen_bind_propp (cert_prop_pat, cert_prop); | |
| 5819 | 568 | |
| 569 | ||
| 6789 | 570 | (* auto binds *) | 
| 571 | ||
| 572 | val auto_bind_goal = add_binds_i o AutoBind.goal; | |
| 6797 | 573 | val auto_bind_facts = add_binds_i oo AutoBind.facts; | 
| 6789 | 574 | |
| 575 | ||
| 5819 | 576 | |
| 577 | (** theorems **) | |
| 578 | ||
| 579 | (* thms_closure *) | |
| 580 | ||
| 581 | fun thms_closure (Context {thy, thms, ...}) =
 | |
| 582 | let | |
| 583 | val global_closure = PureThy.thms_closure thy; | |
| 584 | fun get name = | |
| 585 | (case global_closure name of | |
| 586 | None => Symtab.lookup (thms, name) | |
| 587 | | some => some); | |
| 588 | in get end; | |
| 589 | ||
| 590 | ||
| 6091 | 591 | (* get_thm(s) *) | 
| 5819 | 592 | |
| 6091 | 593 | fun get_thm (ctxt as Context {thy, thms, ...}) name =
 | 
| 5819 | 594 | (case Symtab.lookup (thms, name) of | 
| 595 | Some [th] => th | |
| 596 |   | Some _ => raise CONTEXT ("Single theorem expected: " ^ quote name, ctxt)
 | |
| 6091 | 597 | | None => (PureThy.get_thm thy name handle THEORY (msg, _) => raise CONTEXT (msg, ctxt))); | 
| 5819 | 598 | |
| 6091 | 599 | fun get_thms (ctxt as Context {thy, thms, ...}) name =
 | 
| 5819 | 600 | (case Symtab.lookup (thms, name) of | 
| 601 | Some ths => ths | |
| 6091 | 602 | | None => (PureThy.get_thms thy name handle THEORY (msg, _) => raise CONTEXT (msg, ctxt))); | 
| 5819 | 603 | |
| 6091 | 604 | fun get_thmss ctxt names = flat (map (get_thms ctxt) names); | 
| 5819 | 605 | |
| 606 | ||
| 6091 | 607 | (* put_thm(s) *) | 
| 5819 | 608 | |
| 6091 | 609 | fun put_thms (name, ths) = map_context | 
| 5819 | 610 | (fn (thy, data, asms, binds, thms, defs) => | 
| 611 | (thy, data, asms, binds, Symtab.update ((name, ths), thms), defs)); | |
| 612 | ||
| 6091 | 613 | fun put_thm (name, th) = put_thms (name, [th]); | 
| 5819 | 614 | |
| 6091 | 615 | fun put_thmss [] ctxt = ctxt | 
| 616 | | put_thmss (th :: ths) ctxt = ctxt |> put_thms th |> put_thmss ths; | |
| 5819 | 617 | |
| 618 | ||
| 6091 | 619 | (* have_thmss *) | 
| 5819 | 620 | |
| 6875 | 621 | fun have_thmss more_ths name more_attrs ths_attrs ctxt = | 
| 5819 | 622 | let | 
| 623 | fun app ((ct, ths), (th, attrs)) = | |
| 6091 | 624 | let val (ct', th') = Thm.applys_attributes ((ct, th), attrs @ more_attrs) | 
| 5819 | 625 | in (ct', th' :: ths) end | 
| 626 | val (ctxt', rev_thms) = foldl app ((ctxt, []), ths_attrs); | |
| 6875 | 627 | val thms = flat (rev rev_thms) @ more_ths; | 
| 6091 | 628 | in (ctxt' |> put_thms (name, thms), (name, thms)) end; | 
| 5819 | 629 | |
| 630 | ||
| 631 | ||
| 632 | (** assumptions **) | |
| 633 | ||
| 634 | (* get assumptions *) | |
| 635 | ||
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 636 | fun assumptions (Context {asms = ((asms, _), _), ...}) = asms;
 | 
| 5819 | 637 | fun fixed_names (Context {asms = (_, (fixes, _)), ...}) = map #2 fixes;
 | 
| 638 | ||
| 639 | ||
| 640 | (* assume *) | |
| 641 | ||
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 642 | fun prems (Context {asms = ((_, asms), _), ...}) = flat (map #2 asms);
 | 
| 6797 | 643 | |
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 644 | fun gen_assume prepp tac name attrs raw_prop_pats ctxt = | 
| 5819 | 645 | let | 
| 5935 | 646 | val (ctxt', props) = foldl_map prepp (ctxt, raw_prop_pats); | 
| 5819 | 647 | val sign = sign_of ctxt'; | 
| 6797 | 648 |     val Context {defs = (_, _, maxidx, _), ...} = ctxt';
 | 
| 5819 | 649 | |
| 6797 | 650 | val cprops = map (Thm.cterm_of sign) props; | 
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 651 | val cprops_tac = map (rpair tac) cprops; | 
| 6797 | 652 | val asms = map (Drule.forall_elim_vars (maxidx + 1) o Thm.assume) cprops; | 
| 5919 | 653 | |
| 654 | val ths = map (fn th => ([th], [])) asms; | |
| 6091 | 655 | val (ctxt'', (_, thms)) = | 
| 5819 | 656 | ctxt' | 
| 6797 | 657 | |> auto_bind_facts name props | 
| 6875 | 658 | |> have_thmss [] name (attrs @ [Drule.tag_assumption]) ths; | 
| 5819 | 659 | |
| 660 | val ctxt''' = | |
| 661 | ctxt'' | |
| 6797 | 662 | |> map_context (fn (thy, data, ((asms_ct, asms_th), fixes), binds, thms, defs) => | 
| 6847 
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
 wenzelm parents: 
6797diff
changeset | 663 | (thy, data, ((asms_ct @ cprops_tac, asms_th @ [(name, asms)]), fixes), binds, thms, defs)); | 
| 6797 | 664 | in (ctxt''', ((name, thms), prems ctxt')) end; | 
| 5819 | 665 | |
| 5935 | 666 | val assume = gen_assume bind_propp; | 
| 667 | val assume_i = gen_assume bind_propp_i; | |
| 5819 | 668 | |
| 669 | ||
| 670 | (* fix *) | |
| 671 | ||
| 672 | fun read_skolemT (Context {defs = (_, _, _, used), ...}) None = Type.param used ("'z", logicS)
 | |
| 673 | | read_skolemT ctxt (Some s) = read_typ ctxt s; | |
| 674 | ||
| 675 | fun gen_fix prep check (ctxt, (x, raw_T)) = | |
| 676 | ctxt | |
| 677 | |> declare_term (Free (check_skolem ctxt check x, prep ctxt raw_T)) | |
| 678 | |> map_context (fn (thy, data, (assumes, (fixes, names)), binds, thms, defs) => | |
| 679 | let val x' = variant names x in | |
| 680 | (thy, data, (assumes, ((x, Syntax.skolem x') :: fixes, x' :: names)), binds, thms, defs) | |
| 681 | end); | |
| 682 | ||
| 683 | fun gen_fixs prep check xs ctxt = foldl (gen_fix prep check) (ctxt, xs); | |
| 684 | ||
| 685 | ||
| 686 | val fix = gen_fixs read_skolemT true; | |
| 687 | val fix_i = gen_fixs cert_typ false; | |
| 688 | ||
| 689 | ||
| 6895 | 690 | (* transfer_used_names *) | 
| 691 | ||
| 692 | fun transfer_used_names (Context {asms = (_, (_, names)), defs = (_, _, _, used), ...}) =
 | |
| 693 | map_context (fn (thy, data, (asms, (fixes, _)), binds, thms, (types, sorts, maxidx, _)) => | |
| 694 | (thy, data, (asms, (fixes, names)), binds, thms, (types, sorts, maxidx, used))); | |
| 695 | ||
| 5819 | 696 | |
| 697 | (** theory setup **) | |
| 698 | ||
| 699 | val setup = [ProofDataData.init]; | |
| 700 | ||
| 701 | ||
| 702 | end; |