author | wenzelm |
Tue, 11 Aug 2015 20:32:56 +0200 | |
changeset 60900 | 11a0f333de6f |
parent 59936 | b8ffc3dc9e24 |
child 61049 | 0d401f874942 |
permissions | -rw-r--r-- |
57886
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/Tools/named_theorems.ML |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
3 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
4 |
Named collections of theorems in canonical order. |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
5 |
*) |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
6 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
7 |
signature NAMED_THEOREMS = |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
8 |
sig |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
9 |
val member: Proof.context -> string -> thm -> bool |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
10 |
val get: Proof.context -> string -> thm list |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
11 |
val add_thm: string -> thm -> Context.generic -> Context.generic |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
12 |
val del_thm: string -> thm -> Context.generic -> Context.generic |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
13 |
val add: string -> attribute |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
14 |
val del: string -> attribute |
59920
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
15 |
val check: Proof.context -> string * Position.T -> string |
57888 | 16 |
val declare: binding -> string -> local_theory -> string * local_theory |
57886
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
17 |
end; |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
18 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
19 |
structure Named_Theorems: NAMED_THEOREMS = |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
20 |
struct |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
21 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
22 |
(* context data *) |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
23 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
24 |
structure Data = Generic_Data |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
25 |
( |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
26 |
type T = thm Item_Net.T Symtab.table; |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
27 |
val empty: T = Symtab.empty; |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
28 |
val extend = I; |
57965 | 29 |
val merge : T * T -> T = Symtab.join (K Item_Net.merge); |
57886
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
30 |
); |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
31 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
32 |
fun new_entry name = |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
33 |
Data.map (fn data => |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
34 |
if Symtab.defined data name |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
35 |
then error ("Duplicate declaration of named theorems: " ^ quote name) |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
36 |
else Symtab.update (name, Thm.full_rules) data); |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
37 |
|
59920
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
38 |
fun undeclared name = "Undeclared named theorems " ^ quote name; |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
39 |
|
57886
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
40 |
fun the_entry context name = |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
41 |
(case Symtab.lookup (Data.get context) name of |
59920
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
42 |
NONE => error (undeclared name) |
57886
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
43 |
| SOME entry => entry); |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
44 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
45 |
fun map_entry name f context = |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
46 |
(the_entry context name; Data.map (Symtab.map_entry name f) context); |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
47 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
48 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
49 |
(* maintain content *) |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
50 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
51 |
fun member ctxt = Item_Net.member o the_entry (Context.Proof ctxt); |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
52 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
53 |
fun content context = rev o Item_Net.content o the_entry context; |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
54 |
val get = content o Context.Proof; |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
55 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
56 |
fun add_thm name = map_entry name o Item_Net.update; |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
57 |
fun del_thm name = map_entry name o Item_Net.remove; |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
58 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
59 |
val add = Thm.declaration_attribute o add_thm; |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
60 |
val del = Thm.declaration_attribute o del_thm; |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
61 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
62 |
|
59920
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
63 |
(* check *) |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
64 |
|
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
65 |
fun check ctxt (xname, pos) = |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
66 |
let |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
67 |
val context = Context.Proof ctxt; |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
68 |
val fact_ref = Facts.Named ((xname, Position.none), NONE); |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
69 |
fun err () = error (undeclared xname ^ Position.here pos); |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
70 |
in |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
71 |
(case try (Proof_Context.get_fact_generic context) fact_ref of |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
72 |
SOME (SOME name, _) => if can (the_entry context) name then name else err () |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
73 |
| _ => err ()) |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
74 |
end; |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
75 |
|
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
76 |
|
57886
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
77 |
(* declaration *) |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
78 |
|
57888 | 79 |
fun declare binding descr lthy = |
57886
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
80 |
let |
59879 | 81 |
val name = Local_Theory.full_name lthy binding; |
57886
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
82 |
val description = |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
83 |
"declaration of " ^ (if descr = "" then Binding.name_of binding ^ " rules" else descr); |
57888 | 84 |
val lthy' = lthy |
57929
c5063c033a5a
tuned signature -- proper Local_Theory.add_thms_dynamic;
wenzelm
parents:
57928
diff
changeset
|
85 |
|> Local_Theory.background_theory (Context.theory_map (new_entry name)) |
c5063c033a5a
tuned signature -- proper Local_Theory.add_thms_dynamic;
wenzelm
parents:
57928
diff
changeset
|
86 |
|> Local_Theory.map_contexts (K (Context.proof_map (new_entry name))) |
c5063c033a5a
tuned signature -- proper Local_Theory.add_thms_dynamic;
wenzelm
parents:
57928
diff
changeset
|
87 |
|> Local_Theory.add_thms_dynamic (binding, fn context => content context name) |
57927 | 88 |
|> Attrib.local_setup binding (Attrib.add_del (add name) (del name)) description |
57888 | 89 |
in (name, lthy') end; |
57886
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
90 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
91 |
val _ = |
59936
b8ffc3dc9e24
@{command_spec} is superseded by @{command_keyword};
wenzelm
parents:
59920
diff
changeset
|
92 |
Outer_Syntax.local_theory @{command_keyword named_theorems} |
57886
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
93 |
"declare named collection of theorems" |
59028 | 94 |
(Parse.and_list1 (Parse.binding -- Scan.optional Parse.text "") >> |
95 |
fold (fn (b, descr) => snd o declare b descr)); |
|
57886
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
96 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
97 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
98 |
(* ML antiquotation *) |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
99 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
100 |
val _ = Theory.setup |
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
101 |
(ML_Antiquotation.inline @{binding named_theorems} |
59920
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
102 |
(Args.context -- Scan.lift (Parse.position Args.name) >> |
86d302846b16
check wrt. proper context, e.g. relevant for 'experiment' target;
wenzelm
parents:
59879
diff
changeset
|
103 |
(fn (ctxt, name) => ML_Syntax.print_string (check ctxt name)))); |
57886
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
104 |
|
7cae177c9084
support for named collections of theorems in canonical order;
wenzelm
parents:
diff
changeset
|
105 |
end; |