1 (* Title: Pure/pure_thy.ML
3 Author: Markus Wenzel, TU Muenchen
5 Theorem storage. Pure theory syntax and logical content.
10 val facts_of: theory -> Facts.T
11 val intern_fact: theory -> xstring -> string
12 val defined_fact: theory -> string -> bool
13 val hide_fact: bool -> string -> theory -> theory
14 val get_fact: Context.generic -> theory -> Facts.ref -> thm list
15 val get_thms: theory -> xstring -> thm list
16 val get_thm: theory -> xstring -> thm
17 val all_thms_of: theory -> (string * thm) list
18 val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
19 val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
20 val burrow_facts: ('a list -> 'b list) ->
21 ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
22 val name_multi: string -> 'a list -> (string * 'a) list
23 val name_thm: bool -> bool -> Position.T -> string -> thm -> thm
24 val name_thms: bool -> bool -> Position.T -> string -> thm list -> thm list
25 val name_thmss: bool -> Position.T -> string -> (thm list * 'a) list -> (thm list * 'a) list
26 val store_thms: bstring * thm list -> theory -> thm list * theory
27 val store_thm: bstring * thm -> theory -> thm * theory
28 val store_thm_open: bstring * thm -> theory -> thm * theory
29 val add_thms: ((bstring * thm) * attribute list) list -> theory -> thm list * theory
30 val add_thm: (bstring * thm) * attribute list -> theory -> thm * theory
31 val add_thmss: ((bstring * thm list) * attribute list) list -> theory -> thm list list * theory
32 val add_thms_dynamic: bstring * (Context.generic -> thm list) -> theory -> theory
33 val note_thmss: string -> ((Name.binding * attribute list) *
34 (thm list * attribute list) list) list -> theory -> (string * thm list) list * theory
35 val note_thmss_grouped: string -> string -> ((Name.binding * attribute list) *
36 (thm list * attribute list) list) list -> theory -> (string * thm list) list * theory
37 val add_axioms: ((bstring * term) * attribute list) list -> theory -> thm list * theory
38 val add_axioms_cmd: ((bstring * string) * attribute list) list -> theory -> thm list * theory
39 val add_defs: bool -> ((bstring * term) * attribute list) list ->
40 theory -> thm list * theory
41 val add_defs_unchecked: bool -> ((bstring * term) * attribute list) list ->
42 theory -> thm list * theory
43 val add_defs_unchecked_cmd: bool -> ((bstring * string) * attribute list) list ->
44 theory -> thm list * theory
45 val add_defs_cmd: bool -> ((bstring * string) * attribute list) list ->
46 theory -> thm list * theory
47 val old_appl_syntax: theory -> bool
48 val old_appl_syntax_setup: theory -> theory
51 structure PureThy: PURE_THY =
55 (*** stored facts ***)
59 structure FactsData = TheoryDataFun
62 val empty = Facts.empty;
65 fun merge _ = Facts.merge;
68 val facts_of = FactsData.get;
70 val intern_fact = Facts.intern o facts_of;
71 val defined_fact = Facts.defined o facts_of;
73 fun hide_fact fully name = FactsData.map (Facts.hide fully name);
77 (** retrieve theorems **)
79 fun get_fact context thy xthmref =
81 val xname = Facts.name_of_ref xthmref;
82 val pos = Facts.pos_of_ref xthmref;
84 val name = intern_fact thy xname;
85 val res = Facts.lookup context (facts_of thy) name;
86 val _ = Theory.check_thy thy;
89 NONE => error ("Unknown fact " ^ quote name ^ Position.str_of pos)
90 | SOME (static, ths) =>
91 (Position.report ((if static then Markup.fact else Markup.dynamic_fact) name) pos;
92 Facts.select xthmref (map (Thm.transfer thy) ths)))
95 fun get_thms thy = get_fact (Context.Theory thy) thy o Facts.named;
96 fun get_thm thy name = Facts.the_single name (get_thms thy name);
99 Facts.fold_static (fn (_, ths) => append (map (`(Thm.get_name_hint)) ths)) (facts_of thy) [];
103 (** store theorems **)
105 (* fact specifications *)
107 fun map_facts f = map (apsnd (map (apfst (map f))));
108 fun burrow_fact f = split_list #>> burrow f #> op ~~;
109 fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
114 fun name_multi name [x] = [(name, x)]
115 | name_multi "" xs = map (pair "") xs
116 | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
118 fun name_thm pre official pos name thm = thm
119 |> (if Thm.get_name thm <> "" andalso pre orelse not official then I else Thm.put_name name)
120 |> (if Thm.has_name_hint thm andalso pre orelse name = "" then I else Thm.put_name_hint name)
121 |> Thm.default_position pos
122 |> Thm.default_position (Position.thread_data ());
124 fun name_thms pre official pos name xs =
125 map (uncurry (name_thm pre official pos)) (name_multi name xs);
127 fun name_thmss official pos name fact =
128 burrow_fact (name_thms true official pos name) fact;
133 fun enter_thms _ _ app_att ("", thms) thy = app_att (thy, thms) |> swap
134 | enter_thms pre_name post_name app_att (bname, thms) thy =
136 val naming = Sign.naming_of thy;
137 val name = NameSpace.full naming bname;
138 val (thy', thms') = apsnd (post_name name) (app_att (thy, pre_name name thms));
139 val thms'' = map (Thm.transfer thy') thms';
140 val thy'' = thy' |> FactsData.map (Facts.add_global naming (name, thms''));
141 in (thms'', thy'') end;
147 enter_thms (name_thms true true Position.none) (name_thms false true Position.none) I;
149 fun store_thm (name, th) = store_thms (name, [th]) #>> the_single;
151 fun store_thm_open (name, th) =
152 enter_thms (name_thms true false Position.none) (name_thms false false Position.none) I
153 (name, [th]) #>> the_single;
158 fun add_thms_atts pre_name ((bname, thms), atts) =
159 enter_thms pre_name (name_thms false true Position.none)
160 (foldl_map (Thm.theory_attributes atts)) (bname, thms);
162 fun gen_add_thmss pre_name =
163 fold_map (add_thms_atts pre_name);
165 fun gen_add_thms pre_name args =
166 apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
168 val add_thmss = gen_add_thmss (name_thms true true Position.none);
169 val add_thms = gen_add_thms (name_thms true true Position.none);
170 val add_thm = yield_singleton add_thms;
173 (* add_thms_dynamic *)
175 fun add_thms_dynamic (bname, f) thy =
176 let val name = Sign.full_name thy bname
177 in thy |> FactsData.map (Facts.add_dynamic (Sign.naming_of thy) (name, f)) end;
184 fun gen_note_thmss tag = fold_map (fn ((binding, more_atts), ths_atts) => fn thy =>
186 val bname = Name.name_of binding;
187 val pos = Name.pos_of binding;
188 val name = Sign.full_name thy bname;
189 val _ = Position.report (Markup.fact_decl name) pos;
191 fun app (x, (ths, atts)) = foldl_map (Thm.theory_attributes atts) (x, ths);
192 val (thms, thy') = thy |> enter_thms
193 (name_thmss true pos) (name_thms false true pos) (apsnd flat o foldl_map app)
194 (bname, map (fn (ths, atts) => (ths, surround tag (atts @ more_atts))) ths_atts);
195 in ((name, thms), thy') end);
199 fun note_thmss k = gen_note_thmss (Thm.kind k);
200 fun note_thmss_grouped k g = gen_note_thmss (Thm.kind k #> Thm.group g);
205 (* store axioms as theorems *)
208 fun get_ax thy (name, _) = Thm.get_axiom_i thy (Sign.full_name thy name);
209 fun get_axs thy named_axs = map (Thm.forall_elim_vars 0 o get_ax thy) named_axs;
210 fun add_axm add = fold_map (fn ((name, ax), atts) => fn thy =>
212 val named_ax = [(name, ax)];
213 val thy' = add named_ax thy;
214 val thm = hd (get_axs thy' named_ax);
215 in apfst hd (gen_add_thms (K I) [((name, thm), atts)] thy') end);
217 val add_defs = add_axm o Theory.add_defs_i false;
218 val add_defs_unchecked = add_axm o Theory.add_defs_i true;
219 val add_axioms = add_axm Theory.add_axioms_i;
220 val add_defs_cmd = add_axm o Theory.add_defs false;
221 val add_defs_unchecked_cmd = add_axm o Theory.add_defs true;
222 val add_axioms_cmd = add_axm Theory.add_axioms;
227 (*** Pure theory syntax and logical content ***)
229 val typ = SimpleSyntax.read_typ;
230 val term = SimpleSyntax.read_term;
231 val prop = SimpleSyntax.read_prop;
234 (* application syntax variants *)
237 [("_appl", typ "('b => 'a) => args => logic", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000)),
238 ("_appl", typ "('b => 'a) => args => aprop", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000))];
241 [("", typ "'a => cargs", Delimfix "_"),
242 ("_cargs", typ "'a => cargs => cargs", Mixfix ("_/ _", [1000, 1000], 1000)),
243 ("_applC", typ "('b => 'a) => cargs => logic", Mixfix ("(1_/ _)", [1000, 1000], 999)),
244 ("_applC", typ "('b => 'a) => cargs => aprop", Mixfix ("(1_/ _)", [1000, 1000], 999))];
246 structure OldApplSyntax = TheoryDataFun
252 fun merge _ (b1, b2) : T =
254 else error "Cannot merge theories with different application syntax";
257 val old_appl_syntax = OldApplSyntax.get;
259 val old_appl_syntax_setup =
260 OldApplSyntax.put true #>
261 Sign.del_modesyntax_i Syntax.mode_default applC_syntax #>
262 Sign.add_syntax_i appl_syntax;
267 val _ = Context.>> (Context.map_theory
268 (OldApplSyntax.init #>
272 ("itself", 1, NoSyn),
274 #> Sign.add_nonterminals Syntax.basic_nonterms
276 [("_lambda", typ "pttrns => 'a => logic", Mixfix ("(3%_./ _)", [0, 3], 3)),
277 ("_abs", typ "'a", NoSyn),
278 ("", typ "'a => args", Delimfix "_"),
279 ("_args", typ "'a => args => args", Delimfix "_,/ _"),
280 ("", typ "id => idt", Delimfix "_"),
281 ("_idtdummy", typ "idt", Delimfix "'_"),
282 ("_idtyp", typ "id => type => idt", Mixfix ("_::_", [], 0)),
283 ("_idtypdummy", typ "type => idt", Mixfix ("'_()::_", [], 0)),
284 ("", typ "idt => idt", Delimfix "'(_')"),
285 ("", typ "idt => idts", Delimfix "_"),
286 ("_idts", typ "idt => idts => idts", Mixfix ("_/ _", [1, 0], 0)),
287 ("", typ "idt => pttrn", Delimfix "_"),
288 ("", typ "pttrn => pttrns", Delimfix "_"),
289 ("_pttrns", typ "pttrn => pttrns => pttrns", Mixfix ("_/ _", [1, 0], 0)),
290 ("", typ "id => aprop", Delimfix "_"),
291 ("", typ "longid => aprop", Delimfix "_"),
292 ("", typ "var => aprop", Delimfix "_"),
293 ("_DDDOT", typ "aprop", Delimfix "..."),
294 ("_aprop", typ "aprop => prop", Delimfix "PROP _"),
295 ("_asm", typ "prop => asms", Delimfix "_"),
296 ("_asms", typ "prop => asms => asms", Delimfix "_;/ _"),
297 ("_bigimpl", typ "asms => prop => prop", Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
298 ("_ofclass", typ "type => logic => prop", Delimfix "(1OFCLASS/(1'(_,/ _')))"),
299 ("_mk_ofclass", typ "dummy", NoSyn),
300 ("_TYPE", typ "type => logic", Delimfix "(1TYPE/(1'(_')))"),
301 ("", typ "id => logic", Delimfix "_"),
302 ("", typ "longid => logic", Delimfix "_"),
303 ("", typ "var => logic", Delimfix "_"),
304 ("_DDDOT", typ "logic", Delimfix "..."),
305 ("_constify", typ "num => num_const", Delimfix "_"),
306 ("_indexnum", typ "num_const => index", Delimfix "\\<^sub>_"),
307 ("_index", typ "logic => index", Delimfix "(00\\<^bsub>_\\<^esub>)"),
308 ("_indexdefault", typ "index", Delimfix ""),
309 ("_indexvar", typ "index", Delimfix "'\\<index>"),
310 ("_struct", typ "index => logic", Mixfix ("\\<struct>_", [1000], 1000)),
311 ("==>", typ "prop => prop => prop", Delimfix "op ==>"),
312 (Term.dummy_patternN, typ "aprop", Delimfix "'_")]
313 #> Sign.add_syntax_i applC_syntax
314 #> Sign.add_modesyntax_i (Symbol.xsymbolsN, true)
315 [("fun", typ "type => type => type", Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
316 ("_bracket", typ "types => type => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
317 ("_ofsort", typ "tid => sort => type", Mixfix ("_\\<Colon>_", [1000, 0], 1000)),
318 ("_constrain", typ "'a => type => 'a", Mixfix ("_\\<Colon>_", [4, 0], 3)),
319 ("_idtyp", typ "id => type => idt", Mixfix ("_\\<Colon>_", [], 0)),
320 ("_idtypdummy", typ "type => idt", Mixfix ("'_()\\<Colon>_", [], 0)),
321 ("_type_constraint_", typ "'a", NoSyn),
322 ("_lambda", typ "pttrns => 'a => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
323 ("==", typ "'a => 'a => prop", InfixrName ("\\<equiv>", 2)),
324 ("all_binder", typ "idts => prop => prop", Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
325 ("==>", typ "prop => prop => prop", InfixrName ("\\<Longrightarrow>", 1)),
326 ("_DDDOT", typ "aprop", Delimfix "\\<dots>"),
327 ("_bigimpl", typ "asms => prop => prop", Mixfix ("((1\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
328 ("_DDDOT", typ "logic", Delimfix "\\<dots>")]
329 #> Sign.add_modesyntax_i ("", false)
330 [("prop", typ "prop => prop", Mixfix ("_", [0], 0)),
331 ("Pure.term", typ "'a => prop", Delimfix "TERM _"),
332 ("Pure.conjunction", typ "prop => prop => prop", InfixrName ("&&", 2))]
333 #> Sign.add_modesyntax_i ("HTML", false)
334 [("_lambda", typ "pttrns => 'a => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3))]
336 [("==", typ "'a => 'a => prop", InfixrName ("==", 2)),
337 ("==>", typ "prop => prop => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
338 ("all", typ "('a => prop) => prop", Binder ("!!", 0, 0)),
339 ("prop", typ "prop => prop", NoSyn),
340 ("TYPE", typ "'a itself", NoSyn),
341 (Term.dummy_patternN, typ "'a", Delimfix "'_")]
342 #> Theory.add_deps "==" ("==", typ "'a => 'a => prop") []
343 #> Theory.add_deps "==>" ("==>", typ "prop => prop => prop") []
344 #> Theory.add_deps "all" ("all", typ "('a => prop) => prop") []
345 #> Theory.add_deps "TYPE" ("TYPE", typ "'a itself") []
346 #> Theory.add_deps Term.dummy_patternN (Term.dummy_patternN, typ "'a") []
347 #> Sign.add_trfuns Syntax.pure_trfuns
348 #> Sign.add_trfunsT Syntax.pure_trfunsT
351 [("term", typ "'a => prop", NoSyn),
352 ("conjunction", typ "prop => prop => prop", NoSyn)]
353 #> (add_defs false o map Thm.no_attributes)
354 [("prop_def", prop "(CONST prop :: prop => prop) (A::prop) == A::prop"),
355 ("term_def", prop "(CONST Pure.term :: 'a => prop) (x::'a) == (!!A::prop. A ==> A)"),
356 ("conjunction_def", prop "(A && B) == (!!C::prop. (A ==> B ==> C) ==> C)")] #> snd
357 #> Sign.hide_const false "Pure.conjunction"
358 #> Sign.hide_const false "Pure.term"
359 #> add_thmss [(("nothing", []), [])] #> snd
360 #> Theory.add_axioms_i Proofterm.equality_axms));