5 Theorem storage. The ProtoPure theory. |
5 Theorem storage. The ProtoPure theory. |
6 *) |
6 *) |
7 |
7 |
8 signature BASIC_PURE_THY = |
8 signature BASIC_PURE_THY = |
9 sig |
9 sig |
10 type thmref |
10 datatype interval = FromTo of int * int | From of int | Single of int |
|
11 datatype thmref = Name of string | NameSelection of string * interval list |
11 val print_theorems: theory -> unit |
12 val print_theorems: theory -> unit |
12 val print_theory: theory -> unit |
13 val print_theory: theory -> unit |
13 val get_thm: theory -> thmref -> thm |
14 val get_thm: theory -> thmref -> thm |
14 val get_thms: theory -> thmref -> thm list |
15 val get_thms: theory -> thmref -> thm list |
15 val get_thmss: theory -> thmref list -> thm list |
16 val get_thmss: theory -> thmref list -> thm list |
21 end; |
22 end; |
22 |
23 |
23 signature PURE_THY = |
24 signature PURE_THY = |
24 sig |
25 sig |
25 include BASIC_PURE_THY |
26 include BASIC_PURE_THY |
26 datatype interval = FromTo of int * int | From of int | Single of int |
|
27 val string_of_thmref: thmref -> string |
27 val string_of_thmref: thmref -> string |
28 val theorem_space: theory -> NameSpace.T |
28 val theorem_space: theory -> NameSpace.T |
|
29 val print_theorems_diff: theory -> theory -> unit |
29 val get_thm_closure: theory -> thmref -> thm |
30 val get_thm_closure: theory -> thmref -> thm |
30 val get_thms_closure: theory -> thmref -> thm list |
31 val get_thms_closure: theory -> thmref -> thm list |
31 val single_thm: string -> thm list -> thm |
32 val single_thm: string -> thm list -> thm |
|
33 val name_of_thmref: thmref -> string |
|
34 val map_name_of_thmref: (string -> string) -> thmref -> thmref |
32 val select_thm: thmref -> thm list -> thm list |
35 val select_thm: thmref -> thm list -> thm list |
33 val selections: string * thm list -> (thmref * thm) list |
36 val selections: string * thm list -> (thmref * thm) list |
34 val fact_index_of: theory -> FactIndex.T |
37 val fact_index_of: theory -> FactIndex.T |
35 val valid_thms: theory -> thmref * thm list -> bool |
38 val valid_thms: theory -> thmref * thm list -> bool |
36 val thms_containing: theory -> FactIndex.spec -> (string * thm list) list |
39 val thms_containing: theory -> FactIndex.spec -> (string * thm list) list |
76 |
79 |
77 (*** theorem database ***) |
80 (*** theorem database ***) |
78 |
81 |
79 (** dataype theorems **) |
82 (** dataype theorems **) |
80 |
83 |
81 fun pretty_theorems thy theorems = |
84 fun pretty_theorems_diff thy prev_thms (space, thms) = |
82 let |
85 let |
83 val prt_thm = Display.pretty_thm_sg thy; |
86 val prt_thm = Display.pretty_thm_sg thy; |
84 fun prt_thms (name, [th]) = |
87 fun prt_thms (name, [th]) = |
85 Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th] |
88 Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th] |
86 | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths); |
89 | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths); |
87 val thmss = NameSpace.extern_table theorems; |
90 |
|
91 val diff_thmss = Symtab.fold (fn fact => |
|
92 if not (Symtab.member eq_thms prev_thms fact) then cons fact else I) thms []; |
|
93 val thmss = diff_thmss |> map (apfst (NameSpace.extern space)) |> Library.sort_wrt #1; |
88 in Pretty.big_list "theorems:" (map prt_thms thmss) end; |
94 in Pretty.big_list "theorems:" (map prt_thms thmss) end; |
|
95 |
|
96 fun pretty_theorems thy = pretty_theorems_diff thy Symtab.empty; |
89 |
97 |
90 structure TheoremsData = TheoryDataFun |
98 structure TheoremsData = TheoryDataFun |
91 (struct |
99 (struct |
92 val name = "Pure/theorems"; |
100 val name = "Pure/theorems"; |
93 type T = |
101 type T = |
102 val extend = mk_empty; |
110 val extend = mk_empty; |
103 fun merge _ = mk_empty; |
111 fun merge _ = mk_empty; |
104 fun print thy (ref {theorems, index}) = Pretty.writeln (pretty_theorems thy theorems); |
112 fun print thy (ref {theorems, index}) = Pretty.writeln (pretty_theorems thy theorems); |
105 end); |
113 end); |
106 |
114 |
107 val get_theorems = TheoremsData.get; |
115 val get_theorems_ref = TheoremsData.get; |
108 val theorem_space = #1 o #theorems o ! o get_theorems; |
116 val get_theorems = ! o get_theorems_ref; |
109 val fact_index_of = #index o ! o get_theorems; |
117 val theorem_space = #1 o #theorems o get_theorems; |
|
118 val fact_index_of = #index o get_theorems; |
110 |
119 |
111 |
120 |
112 (* print theory *) |
121 (* print theory *) |
113 |
122 |
114 val print_theorems = TheoremsData.print; |
123 val print_theorems = TheoremsData.print; |
|
124 |
|
125 fun print_theorems_diff prev_thy thy = |
|
126 Pretty.writeln (pretty_theorems_diff thy |
|
127 (#2 (#theorems (get_theorems prev_thy))) (#theorems (get_theorems thy))); |
115 |
128 |
116 fun print_theory thy = |
129 fun print_theory thy = |
117 Display.pretty_full_theory thy @ |
130 Display.pretty_full_theory thy @ |
118 [pretty_theorems thy (#theorems (! (get_theorems thy)))] |
131 [pretty_theorems thy (#theorems (get_theorems thy))] |
119 |> Pretty.chunks |> Pretty.writeln; |
132 |> Pretty.chunks |> Pretty.writeln; |
120 |
133 |
121 |
134 |
122 |
135 |
123 (** retrieve theorems **) |
136 (** retrieve theorems **) |
143 fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j |
156 fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j |
144 | string_of_interval (From i) = string_of_int i ^ "-" |
157 | string_of_interval (From i) = string_of_int i ^ "-" |
145 | string_of_interval (Single i) = string_of_int i; |
158 | string_of_interval (Single i) = string_of_int i; |
146 |
159 |
147 |
160 |
148 (* type thmref *) |
161 (* datatype thmref *) |
149 |
162 |
150 type thmref = xstring * interval list option; |
163 datatype thmref = |
151 |
164 Name of string | |
152 fun string_of_thmref (name, NONE) = name |
165 NameSelection of string * interval list; |
153 | string_of_thmref (name, SOME is) = |
166 |
|
167 fun name_of_thmref (Name name) = name |
|
168 | name_of_thmref (NameSelection (name, _)) = name; |
|
169 |
|
170 fun map_name_of_thmref f (Name name) = Name (f name) |
|
171 | map_name_of_thmref f (NameSelection (name, is)) = NameSelection (f name, is); |
|
172 |
|
173 fun string_of_thmref (Name name) = name |
|
174 | string_of_thmref (NameSelection (name, is)) = |
154 name ^ enclose "(" ")" (commas (map string_of_interval is)); |
175 name ^ enclose "(" ")" (commas (map string_of_interval is)); |
155 |
176 |
156 |
177 |
157 (* select_thm *) |
178 (* select_thm *) |
158 |
179 |
159 fun select_thm (_, NONE) thms = thms |
180 fun select_thm (Name _) thms = thms |
160 | select_thm (name, SOME is) thms = |
181 | select_thm (NameSelection (name, is)) thms = |
161 let |
182 let |
162 val n = length thms; |
183 val n = length thms; |
163 fun select i = |
184 fun select i = |
164 if i < 1 orelse i > n then |
185 if i < 1 orelse i > n then |
165 error ("Bad subscript " ^ string_of_int i ^ " for " ^ |
186 error ("Bad subscript " ^ string_of_int i ^ " for " ^ |
168 in map select (List.concat (map (interval n) is)) end; |
189 in map select (List.concat (map (interval n) is)) end; |
169 |
190 |
170 |
191 |
171 (* selections *) |
192 (* selections *) |
172 |
193 |
173 fun selections (name, [thm]) = [((name, NONE), thm)] |
194 fun selections (name, [thm]) = [(Name name, thm)] |
174 | selections (name, thms) = (1 upto length thms, thms) |> ListPair.map (fn (i, thm) => |
195 | selections (name, thms) = (1 upto length thms, thms) |> ListPair.map (fn (i, thm) => |
175 ((name, SOME [Single i]), thm)); |
196 (NameSelection (name, [Single i]), thm)); |
176 |
197 |
177 |
198 |
178 (* get_thm(s)_closure -- statically scoped versions *) |
199 (* get_thm(s)_closure -- statically scoped versions *) |
179 |
200 |
180 (*beware of proper order of evaluation!*) |
201 (*beware of proper order of evaluation!*) |
181 |
202 |
182 fun lookup_thms thy = |
203 fun lookup_thms thy = |
183 let |
204 let |
184 val thy_ref = Theory.self_ref thy; |
205 val thy_ref = Theory.self_ref thy; |
185 val ref {theorems = (space, thms), ...} = get_theorems thy; |
206 val (space, thms) = #theorems (get_theorems thy); |
186 in |
207 in |
187 fn name => |
208 fn name => |
188 Option.map (map (Thm.transfer (Theory.deref thy_ref))) (*dynamic identity*) |
209 Option.map (map (Thm.transfer (Theory.deref thy_ref))) (*dynamic identity*) |
189 (Symtab.lookup (thms, NameSpace.intern space name)) (*static content*) |
210 (Symtab.lookup (thms, NameSpace.intern space name)) (*static content*) |
190 end; |
211 end; |
191 |
212 |
192 fun get_thms_closure thy = |
213 fun get_thms_closure thy = |
193 let val closures = map lookup_thms (thy :: Theory.ancestors_of thy) in |
214 let val closures = map lookup_thms (thy :: Theory.ancestors_of thy) in |
194 fn (name, i) => select_thm (name, i) (the_thms name (get_first (fn f => f name) closures)) |
215 fn thmref => |
|
216 let val name = name_of_thmref thmref |
|
217 in select_thm thmref (the_thms name (get_first (fn f => f name) closures)) end |
195 end; |
218 end; |
196 |
219 |
197 fun get_thm_closure thy = |
220 fun get_thm_closure thy = |
198 let val get = get_thms_closure thy |
221 let val get = get_thms_closure thy |
199 in fn (name, i) => single_thm name (get (name, i)) end; |
222 in fn thmref => single_thm (name_of_thmref thmref) (get thmref) end; |
200 |
223 |
201 |
224 |
202 (* get_thms etc. *) |
225 (* get_thms etc. *) |
203 |
226 |
204 fun get_thms theory (name, i) = |
227 fun get_thms theory thmref = |
205 get_first (fn thy => lookup_thms thy name) (theory :: Theory.ancestors_of theory) |
228 let val name = name_of_thmref thmref in |
206 |> the_thms name |> select_thm (name, i) |> map (Thm.transfer theory); |
229 get_first (fn thy => lookup_thms thy name) (theory :: Theory.ancestors_of theory) |
207 |
230 |> the_thms name |> select_thm thmref |> map (Thm.transfer theory) |
208 fun get_thmss thy names = List.concat (map (get_thms thy) names); |
231 end; |
209 fun get_thm thy (name, i) = single_thm name (get_thms thy (name, i)); |
232 |
|
233 fun get_thmss thy thmrefs = List.concat (map (get_thms thy) thmrefs); |
|
234 fun get_thm thy thmref = single_thm (name_of_thmref thmref) (get_thms thy thmref); |
210 |
235 |
211 |
236 |
212 (* thms_containing etc. *) |
237 (* thms_containing etc. *) |
213 |
238 |
214 fun valid_thms thy (thmref, ths) = |
239 fun valid_thms thy (thmref, ths) = |
218 |
243 |
219 fun thms_containing theory spec = |
244 fun thms_containing theory spec = |
220 (theory :: Theory.ancestors_of theory) |
245 (theory :: Theory.ancestors_of theory) |
221 |> map (fn thy => |
246 |> map (fn thy => |
222 FactIndex.find (fact_index_of thy) spec |
247 FactIndex.find (fact_index_of thy) spec |
223 |> List.filter (fn (name, ths) => valid_thms theory ((name, NONE), ths)) |
248 |> List.filter (fn (name, ths) => valid_thms theory (Name name, ths)) |
224 |> gen_distinct eq_fst) |
249 |> gen_distinct eq_fst) |
225 |> List.concat; |
250 |> List.concat; |
226 |
251 |
227 fun thms_containing_consts thy consts = |
252 fun thms_containing_consts thy consts = |
228 thms_containing thy (consts, []) |> map #2 |> List.concat |
253 thms_containing thy (consts, []) |> map #2 |> List.concat |
244 |
269 |
245 (* hiding -- affects current theory node only *) |
270 (* hiding -- affects current theory node only *) |
246 |
271 |
247 fun hide_thms fully names thy = |
272 fun hide_thms fully names thy = |
248 let |
273 let |
249 val r as ref {theorems = (space, thms), index} = get_theorems thy; |
274 val r as ref {theorems = (space, thms), index} = get_theorems_ref thy; |
250 val space' = fold (NameSpace.hide fully) names space; |
275 val space' = fold (NameSpace.hide fully) names space; |
251 in r := {theorems = (space', thms), index = index}; thy end; |
276 in r := {theorems = (space', thms), index = index}; thy end; |
252 |
277 |
253 |
278 |
254 (* naming *) |
279 (* naming *) |
278 |
303 |
279 fun enter_thms _ _ app_att ("", thms) thy = app_att (thy, thms) |
304 fun enter_thms _ _ app_att ("", thms) thy = app_att (thy, thms) |
280 | enter_thms pre_name post_name app_att (bname, thms) thy = |
305 | enter_thms pre_name post_name app_att (bname, thms) thy = |
281 let |
306 let |
282 val name = Sign.full_name thy bname; |
307 val name = Sign.full_name thy bname; |
283 val r as ref {theorems = (space, theorems), index} = get_theorems thy; |
308 val r as ref {theorems = (space, theorems), index} = get_theorems_ref thy; |
284 val space' = Sign.declare_name thy name space; |
309 val space' = Sign.declare_name thy name space; |
285 val (thy', thms') = apsnd (post_name name) (app_att (thy, pre_name name thms)); |
310 val (thy', thms') = apsnd (post_name name) (app_att (thy, pre_name name thms)); |
286 val theorems' = Symtab.update ((name, thms'), theorems); |
311 val theorems' = Symtab.update ((name, thms'), theorems); |
287 val index' = FactIndex.add (K false) (name, thms') index; |
312 val index' = FactIndex.add (K false) (name, thms') index; |
288 in |
313 in |
459 |> Sign.local_path |
484 |> Sign.local_path |
460 |> (#1 oo (add_defs_i false o map Thm.no_attributes)) |
485 |> (#1 oo (add_defs_i false o map Thm.no_attributes)) |
461 [("Goal_def", Logic.mk_equals (Logic.mk_goal A, A))] |
486 [("Goal_def", Logic.mk_equals (Logic.mk_goal A, A))] |
462 |> (#1 o add_thmss [(("nothing", []), [])]) |
487 |> (#1 o add_thmss [(("nothing", []), [])]) |
463 |> Theory.add_axioms_i Proofterm.equality_axms |
488 |> Theory.add_axioms_i Proofterm.equality_axms |
464 |> Context.end_theory; |
489 |> Theory.end_theory; |
465 |
490 |
466 structure ProtoPure = |
491 structure ProtoPure = |
467 struct |
492 struct |
468 val thy = proto_pure; |
493 val thy = proto_pure; |
469 val Goal_def = get_axiom thy "Goal_def"; |
494 val Goal_def = get_axiom thy "Goal_def"; |