1 (* Title: Pure/pure_thy.ML
3 Author: Markus Wenzel, TU Muenchen
5 Theorem storage. The ProtoPure theory.
8 signature BASIC_PURE_THY =
10 val get_thm: theory -> Facts.ref -> thm
11 val get_thms: theory -> Facts.ref -> thm list
17 val conjunction_def: thm
23 include BASIC_PURE_THY
24 val tag_rule: Markup.property -> thm -> thm
25 val untag_rule: string -> thm -> thm
26 val tag: Markup.property -> attribute
27 val untag: string -> attribute
28 val has_name_hint: thm -> bool
29 val get_name_hint: thm -> string
30 val put_name_hint: string -> thm -> thm
31 val get_group: thm -> string option
32 val put_group: string -> thm -> thm
33 val group: string -> attribute
34 val has_kind: thm -> bool
35 val get_kind: thm -> string
36 val kind_rule: string -> thm -> thm
37 val kind: string -> attribute
38 val kind_internal: attribute
39 val has_internal: Markup.property list -> bool
40 val is_internal: thm -> bool
41 val get_thms_silent: theory -> Facts.ref -> thm list
42 val theorems_of: theory -> thm list NameSpace.table
43 val all_facts_of: theory -> Facts.T
44 val thms_of: theory -> (string * thm) list
45 val all_thms_of: theory -> (string * thm) list
46 val hide_thms: bool -> string list -> theory -> theory
47 val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
48 val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
49 val burrow_facts: ('a list -> 'b list) ->
50 ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
51 val name_multi: string -> 'a list -> (string * 'a) list
52 val name_thm: bool -> bool -> string -> thm -> thm
53 val name_thms: bool -> bool -> string -> thm list -> thm list
54 val name_thmss: bool -> string -> (thm list * 'a) list -> (thm list * 'a) list
55 val store_thm: (bstring * thm) * attribute list -> theory -> thm * theory
56 val smart_store_thms: (bstring * thm list) -> thm list
57 val smart_store_thms_open: (bstring * thm list) -> thm list
58 val forall_elim_var: int -> thm -> thm
59 val forall_elim_vars: int -> thm -> thm
60 val add_thms: ((bstring * thm) * attribute list) list -> theory -> thm list * theory
61 val add_thmss: ((bstring * thm list) * attribute list) list -> theory -> thm list list * theory
62 val note: string -> string * thm -> theory -> thm * theory
63 val note_thmss: string -> ((bstring * attribute list) *
64 (Facts.ref * attribute list) list) list -> theory -> (bstring * thm list) list * theory
65 val note_thmss_i: string -> ((bstring * attribute list) *
66 (thm list * attribute list) list) list -> theory -> (bstring * thm list) list * theory
67 val note_thmss_grouped: string -> string -> ((bstring * attribute list) *
68 (thm list * attribute list) list) list -> theory -> (bstring * thm list) list * theory
69 val note_thmss_qualified: string -> string -> ((bstring * attribute list) *
70 (thm list * attribute list) list) list -> theory -> (bstring * thm list) list * theory
71 val add_axioms: ((bstring * string) * attribute list) list -> theory -> thm list * theory
72 val add_axioms_i: ((bstring * term) * attribute list) list -> theory -> thm list * theory
73 val add_axiomss: ((bstring * string list) * attribute list) list ->
74 theory -> thm list list * theory
75 val add_axiomss_i: ((bstring * term list) * attribute list) list ->
76 theory -> thm list list * theory
77 val add_defs: bool -> ((bstring * string) * attribute list) list ->
78 theory -> thm list * theory
79 val add_defs_i: bool -> ((bstring * term) * attribute list) list ->
80 theory -> thm list * theory
81 val add_defs_unchecked: bool -> ((bstring * string) * attribute list) list ->
82 theory -> thm list * theory
83 val add_defs_unchecked_i: bool -> ((bstring * term) * attribute list) list ->
84 theory -> thm list * theory
85 val appl_syntax: (string * typ * mixfix) list
86 val applC_syntax: (string * typ * mixfix) list
89 structure PureThy: PURE_THY =
93 (*** theorem tags ***)
95 (* add / delete tags *)
97 fun tag_rule tg = Thm.map_tags (insert (op =) tg);
98 fun untag_rule s = Thm.map_tags (filter_out (fn (s', _) => s = s'));
100 fun tag tg x = Thm.rule_attribute (K (tag_rule tg)) x;
101 fun untag s x = Thm.rule_attribute (K (untag_rule s)) x;
104 (* unofficial theorem names *)
106 fun the_name_hint thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.nameN);
108 val has_name_hint = can the_name_hint;
109 val get_name_hint = the_default "??.unknown" o try the_name_hint;
111 fun put_name_hint name = untag_rule Markup.nameN #> tag_rule (Markup.nameN, name);
116 fun get_group thm = AList.lookup (op =) (Thm.get_tags thm) Markup.groupN;
119 if name = "" then I else Thm.map_tags (AList.update (op =) (Markup.groupN, name));
121 fun group name = Thm.rule_attribute (K (put_group name));
126 fun the_kind thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.kindN);
128 val has_kind = can the_kind;
129 val get_kind = the_default "" o try the_kind;
131 fun kind_rule k = tag_rule (Markup.kindN, k) o untag_rule Markup.kindN;
132 fun kind k x = if k = "" then x else Thm.rule_attribute (K (kind_rule k)) x;
133 fun kind_internal x = kind Thm.internalK x;
134 fun has_internal tags = exists (fn tg => tg = (Markup.kindN, Thm.internalK)) tags;
135 val is_internal = has_internal o Thm.get_tags;
139 (*** theorem database ***)
141 (** dataype theorems **)
143 datatype thms = Thms of
144 {theorems: thm list NameSpace.table, (* FIXME legacy *)
147 fun make_thms theorems all_facts = Thms {theorems = theorems, all_facts = all_facts};
149 structure TheoremsData = TheoryDataFun
151 type T = thms ref; (* FIXME legacy *)
152 val empty = ref (make_thms NameSpace.empty_table Facts.empty);
153 fun copy (ref x) = ref x;
154 fun extend (ref (Thms {theorems = _, all_facts})) = ref (make_thms NameSpace.empty_table all_facts);
156 (ref (Thms {theorems = _, all_facts = all_facts1}),
157 ref (Thms {theorems = _, all_facts = all_facts2})) =
158 ref (make_thms NameSpace.empty_table (Facts.merge (all_facts1, all_facts2)));
161 val get_theorems_ref = TheoremsData.get;
162 val get_theorems = (fn Thms args => args) o ! o get_theorems_ref;
163 val theorems_of = #theorems o get_theorems;
164 val all_facts_of = #all_facts o get_theorems;
168 (** retrieve theorems **)
170 fun the_thms _ (SOME thms) = thms
171 | the_thms name NONE = error ("Unknown theorem(s) " ^ quote name);
175 fun lookup_thms thy xname =
177 val (space, thms) = #theorems (get_theorems thy);
178 val name = NameSpace.intern space xname;
179 in Option.map (pair name) (Symtab.lookup thms name) end;
181 fun lookup_fact thy xname =
183 val facts = all_facts_of thy;
184 val name = NameSpace.intern (Facts.space_of facts) xname;
185 in Option.map (pair name) (Facts.lookup facts name) end;
187 fun show_result NONE = "none"
188 | show_result (SOME (name, _)) = quote name;
190 fun get_fact silent theory thmref =
192 val name = Facts.name_of_ref thmref;
193 val new_res = lookup_fact theory name;
194 val old_res = get_first (fn thy => lookup_thms thy name) (theory :: Theory.ancestors_of theory);
196 (case (new_res, old_res) of
198 | (SOME (name1, ths1), SOME (name2, ths2)) => name1 = name2 andalso Thm.eq_thms (ths1, ths2)
201 if is_same orelse silent then ()
202 else legacy_feature ("Fact lookup differs from old-style thm database:\n" ^
203 show_result new_res ^ " vs " ^ show_result old_res ^
204 Position.str_of (Position.thread_data ()));
205 in Option.map #2 old_res |> the_thms name |> Facts.select thmref |> map (Thm.transfer theory) end;
209 val get_thms_silent = get_fact true;
210 val get_thms = get_fact false;
211 fun get_thm thy thmref = Facts.the_single (Facts.name_of_ref thmref) (get_thms thy thmref);
219 let val thms = #2 (theorems_of thy)
220 in map (`(get_name_hint)) (maps snd (Symtab.dest thms)) end;
222 fun all_thms_of thy = maps thms_of (thy :: Theory.ancestors_of thy);
226 (** store theorems **) (*DESTRUCTIVE*)
228 (* hiding -- affects current theory node only *)
230 fun hide_thms fully names thy = CRITICAL (fn () =>
232 val r as ref (Thms {theorems = (space, thms), all_facts}) = get_theorems_ref thy;
233 val space' = fold (NameSpace.hide fully) names space;
234 in r := make_thms (space', thms) all_facts; thy end);
237 (* fact specifications *)
239 fun map_facts f = map (apsnd (map (apfst (map f))));
240 fun burrow_fact f = split_list #>> burrow f #> op ~~;
241 fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
246 fun gen_names _ len "" = replicate len ""
247 | gen_names j len name = map (fn i => name ^ "_" ^ string_of_int i) (j + 1 upto j + len);
249 fun name_multi name [x] = [(name, x)]
250 | name_multi name xs = gen_names 0 (length xs) name ~~ xs;
252 fun name_thm pre official name thm = thm
253 |> (if Thm.get_name thm <> "" andalso pre orelse not official then I else Thm.put_name name)
254 |> (if has_name_hint thm andalso pre orelse name = "" then I else put_name_hint name)
255 |> Thm.map_tags (Position.default_properties (Position.thread_data ()));
257 fun name_thms pre official name xs =
258 map (uncurry (name_thm pre official)) (name_multi name xs);
260 fun name_thmss official name fact =
261 burrow_fact (name_thms true official name) fact;
266 fun warn_overwrite name = warning ("Replaced old copy of theorems " ^ quote name);
267 fun warn_same name = warning ("Theorem database already contains a copy of " ^ quote name);
269 fun enter_thms _ _ app_att ("", thms) thy = app_att (thy, thms) |> swap
270 | enter_thms pre_name post_name app_att (bname, thms) thy = CRITICAL (fn () =>
272 val name = Sign.full_name thy bname;
273 val (thy', thms') = apsnd (post_name name) (app_att (thy, pre_name name thms));
274 val r as ref (Thms {theorems = (space, theorems), all_facts}) = get_theorems_ref thy';
275 val space' = Sign.declare_name thy' name space;
276 val theorems' = Symtab.update (name, thms') theorems;
277 val all_facts' = Facts.add_global (Sign.naming_of thy') (name, thms') all_facts;
279 (case Symtab.lookup theorems name of
282 if Thm.eq_thms (thms', thms'') then warn_same name
283 else warn_overwrite name);
284 r := make_thms (space', theorems') all_facts';
291 fun add_thms_atts pre_name ((bname, thms), atts) =
292 enter_thms pre_name (name_thms false true)
293 (foldl_map (Thm.theory_attributes atts)) (bname, thms);
295 fun gen_add_thmss pre_name =
296 fold_map (add_thms_atts pre_name);
298 fun gen_add_thms pre_name args =
299 apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
301 val add_thmss = gen_add_thmss (name_thms true true);
302 val add_thms = gen_add_thms (name_thms true true);
309 fun gen_note_thmss get tag = fold_map (fn ((bname, more_atts), ths_atts) => fn thy =>
311 fun app (x, (ths, atts)) = foldl_map (Thm.theory_attributes atts) (x, ths);
312 val (thms, thy') = thy |> enter_thms
313 (name_thmss true) (name_thms false true) (apsnd flat o foldl_map app)
314 (bname, map (fn (ths, atts) => (get thy ths, surround tag (atts @ more_atts))) ths_atts);
315 in ((bname, thms), thy') end);
319 fun note_thmss k = gen_note_thmss get_thms (kind k);
320 fun note_thmss_i k = gen_note_thmss (K I) (kind k);
321 fun note_thmss_grouped k g = gen_note_thmss (K I) (kind k #> group g);
325 fun note kind (name, thm) =
326 note_thmss_i kind [((name, []), [([thm], [])])]
327 #>> (fn [(_, [thm])] => thm);
329 fun note_thmss_qualified k path facts thy =
331 |> Sign.add_path path
332 |> Sign.no_base_names
333 |> note_thmss_i k facts
334 ||> Sign.restore_naming thy;
339 fun store_thm ((bname, thm), atts) thy =
340 let val ([th'], thy') = add_thms_atts (name_thms true true) ((bname, [thm]), atts) thy
344 (* smart_store_thms(_open) *)
348 fun smart_store _ (name, []) =
349 error ("Cannot store empty list of theorems: " ^ quote name)
350 | smart_store official (name, [thm]) =
351 fst (enter_thms (name_thms true official) (name_thms false official) I (name, [thm])
352 (Thm.theory_of_thm thm))
353 | smart_store official (name, thms) =
354 let val thy = Theory.merge_list (map Thm.theory_of_thm thms) in
355 fst (enter_thms (name_thms true official) (name_thms false official) I (name, thms) thy)
360 val smart_store_thms = smart_store true;
361 val smart_store_thms_open = smart_store false;
366 (* forall_elim_var(s) -- belongs to drule.ML *)
368 fun forall_elim_vars_aux strip_vars i th =
370 val {thy, tpairs, prop, ...} = Thm.rep_thm th;
371 val add_used = Term.fold_aterms
372 (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I);
373 val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
374 val vars = strip_vars prop;
375 val cvars = (Name.variant_list used (map #1 vars), vars)
376 |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
377 in fold Thm.forall_elim cvars th end;
379 val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
381 fun forall_elim_var i th = forall_elim_vars_aux
382 (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
383 | _ => raise THM ("forall_elim_vars", i, [th])) i th;
386 (* store axioms as theorems *)
389 fun get_ax thy (name, _) = Thm.get_axiom_i thy (Sign.full_name thy name);
390 fun get_axs thy named_axs = map (forall_elim_vars 0 o get_ax thy) named_axs;
391 fun add_single add ((name, ax), atts) thy =
393 val named_ax = [(name, ax)];
394 val thy' = add named_ax thy;
395 val thm = hd (get_axs thy' named_ax);
396 in apfst hd (gen_add_thms (K I) [((name, thm), atts)] thy') end;
397 fun add_multi add ((name, axs), atts) thy =
399 val named_axs = name_multi name axs;
400 val thy' = add named_axs thy;
401 val thms = get_axs thy' named_axs;
402 in apfst hd (gen_add_thmss (K I) [((name, thms), atts)] thy') end;
403 fun add_singles add = fold_map (add_single add);
404 fun add_multis add = fold_map (add_multi add);
406 val add_axioms = add_singles Theory.add_axioms;
407 val add_axioms_i = add_singles Theory.add_axioms_i;
408 val add_axiomss = add_multis Theory.add_axioms;
409 val add_axiomss_i = add_multis Theory.add_axioms_i;
410 val add_defs = add_singles o Theory.add_defs false;
411 val add_defs_i = add_singles o Theory.add_defs_i false;
412 val add_defs_unchecked = add_singles o Theory.add_defs true;
413 val add_defs_unchecked_i = add_singles o Theory.add_defs_i true;
418 (*** the ProtoPure theory ***)
420 val typ = SimpleSyntax.read_typ;
421 val term = SimpleSyntax.read_term;
422 val prop = SimpleSyntax.read_prop;
425 [("_appl", typ "('b => 'a) => args => logic", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000)),
426 ("_appl", typ "('b => 'a) => args => aprop", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000))];
429 [("", typ "'a => cargs", Delimfix "_"),
430 ("_cargs", typ "'a => cargs => cargs", Mixfix ("_/ _", [1000, 1000], 1000)),
431 ("_applC", typ "('b => 'a) => cargs => logic", Mixfix ("(1_/ _)", [1000, 1000], 999)),
432 ("_applC", typ "('b => 'a) => cargs => aprop", Mixfix ("(1_/ _)", [1000, 1000], 999))];
436 |> Compress.init_data
441 ("itself", 1, NoSyn),
443 |> Sign.add_nonterminals Syntax.basic_nonterms
445 [("_lambda", typ "pttrns => 'a => logic", Mixfix ("(3%_./ _)", [0, 3], 3)),
446 ("_abs", typ "'a", NoSyn),
447 ("", typ "'a => args", Delimfix "_"),
448 ("_args", typ "'a => args => args", Delimfix "_,/ _"),
449 ("", typ "id => idt", Delimfix "_"),
450 ("_idtdummy", typ "idt", Delimfix "'_"),
451 ("_idtyp", typ "id => type => idt", Mixfix ("_::_", [], 0)),
452 ("_idtypdummy", typ "type => idt", Mixfix ("'_()::_", [], 0)),
453 ("", typ "idt => idt", Delimfix "'(_')"),
454 ("", typ "idt => idts", Delimfix "_"),
455 ("_idts", typ "idt => idts => idts", Mixfix ("_/ _", [1, 0], 0)),
456 ("", typ "idt => pttrn", Delimfix "_"),
457 ("", typ "pttrn => pttrns", Delimfix "_"),
458 ("_pttrns", typ "pttrn => pttrns => pttrns", Mixfix ("_/ _", [1, 0], 0)),
459 ("", typ "id => aprop", Delimfix "_"),
460 ("", typ "longid => aprop", Delimfix "_"),
461 ("", typ "var => aprop", Delimfix "_"),
462 ("_DDDOT", typ "aprop", Delimfix "..."),
463 ("_aprop", typ "aprop => prop", Delimfix "PROP _"),
464 ("_asm", typ "prop => asms", Delimfix "_"),
465 ("_asms", typ "prop => asms => asms", Delimfix "_;/ _"),
466 ("_bigimpl", typ "asms => prop => prop", Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
467 ("_ofclass", typ "type => logic => prop", Delimfix "(1OFCLASS/(1'(_,/ _')))"),
468 ("_mk_ofclass", typ "dummy", NoSyn),
469 ("_TYPE", typ "type => logic", Delimfix "(1TYPE/(1'(_')))"),
470 ("", typ "id => logic", Delimfix "_"),
471 ("", typ "longid => logic", Delimfix "_"),
472 ("", typ "var => logic", Delimfix "_"),
473 ("_DDDOT", typ "logic", Delimfix "..."),
474 ("_constify", typ "num => num_const", Delimfix "_"),
475 ("_indexnum", typ "num_const => index", Delimfix "\\<^sub>_"),
476 ("_index", typ "logic => index", Delimfix "(00\\<^bsub>_\\<^esub>)"),
477 ("_indexdefault", typ "index", Delimfix ""),
478 ("_indexvar", typ "index", Delimfix "'\\<index>"),
479 ("_struct", typ "index => logic", Mixfix ("\\<struct>_", [1000], 1000)),
480 ("==>", typ "prop => prop => prop", Delimfix "op ==>"),
481 (Term.dummy_patternN, typ "aprop", Delimfix "'_")]
482 |> Sign.add_syntax_i appl_syntax
483 |> Sign.add_modesyntax_i (Symbol.xsymbolsN, true)
484 [("fun", typ "type => type => type", Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
485 ("_bracket", typ "types => type => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
486 ("_ofsort", typ "tid => sort => type", Mixfix ("_\\<Colon>_", [1000, 0], 1000)),
487 ("_constrain", typ "'a => type => 'a", Mixfix ("_\\<Colon>_", [4, 0], 3)),
488 ("_idtyp", typ "id => type => idt", Mixfix ("_\\<Colon>_", [], 0)),
489 ("_idtypdummy", typ "type => idt", Mixfix ("'_()\\<Colon>_", [], 0)),
490 ("_type_constraint_", typ "'a", NoSyn),
491 ("_lambda", typ "pttrns => 'a => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
492 ("==", typ "'a => 'a => prop", InfixrName ("\\<equiv>", 2)),
493 ("all_binder", typ "idts => prop => prop", Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
494 ("==>", typ "prop => prop => prop", InfixrName ("\\<Longrightarrow>", 1)),
495 ("_DDDOT", typ "aprop", Delimfix "\\<dots>"),
496 ("_bigimpl", typ "asms => prop => prop", Mixfix ("((1\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
497 ("_DDDOT", typ "logic", Delimfix "\\<dots>")]
498 |> Sign.add_modesyntax_i ("", false)
499 [("prop", typ "prop => prop", Mixfix ("_", [0], 0)),
500 ("ProtoPure.term", typ "'a => prop", Delimfix "TERM _"),
501 ("ProtoPure.conjunction", typ "prop => prop => prop", InfixrName ("&&", 2))]
502 |> Sign.add_modesyntax_i ("HTML", false)
503 [("_lambda", typ "pttrns => 'a => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3))]
505 [("==", typ "'a => 'a => prop", InfixrName ("==", 2)),
506 ("==>", typ "prop => prop => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
507 ("all", typ "('a => prop) => prop", Binder ("!!", 0, 0)),
508 ("prop", typ "prop => prop", NoSyn),
509 ("TYPE", typ "'a itself", NoSyn),
510 (Term.dummy_patternN, typ "'a", Delimfix "'_")]
511 |> Theory.add_deps "==" ("==", typ "'a => 'a => prop") []
512 |> Theory.add_deps "==>" ("==>", typ "prop => prop => prop") []
513 |> Theory.add_deps "all" ("all", typ "('a => prop) => prop") []
514 |> Theory.add_deps "TYPE" ("TYPE", typ "'a itself") []
515 |> Theory.add_deps Term.dummy_patternN (Term.dummy_patternN, typ "'a") []
516 |> Sign.add_trfuns Syntax.pure_trfuns
517 |> Sign.add_trfunsT Syntax.pure_trfunsT
520 [("term", typ "'a => prop", NoSyn),
521 ("conjunction", typ "prop => prop => prop", NoSyn)]
522 |> (add_defs_i false o map Thm.no_attributes)
523 [("prop_def", prop "(CONST prop :: prop => prop) (A::prop) == A::prop"),
524 ("term_def", prop "(CONST ProtoPure.term :: 'a => prop) (x::'a) == (!!A::prop. A ==> A)"),
525 ("conjunction_def", prop "(A && B) == (!!C::prop. (A ==> B ==> C) ==> C)")] |> snd
526 |> Sign.hide_consts false ["conjunction", "term"]
527 |> add_thmss [(("nothing", []), [])] |> snd
528 |> Theory.add_axioms_i Proofterm.equality_axms
529 |> Theory.end_theory;
531 structure ProtoPure =
533 val thy = proto_pure;
534 val prop_def = get_axiom thy "prop_def";
535 val term_def = get_axiom thy "term_def";
536 val conjunction_def = get_axiom thy "conjunction_def";
541 structure BasicPureThy: BASIC_PURE_THY = PureThy;