author | wenzelm |
Wed, 21 Mar 2012 17:25:35 +0100 | |
changeset 47066 | 8a6124d09ff5 |
parent 47057 | 12423b36fcc4 |
child 47067 | 4ef29b0c568f |
permissions | -rw-r--r-- |
47057 | 1 |
(* Title: Pure/Isar/bundle.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Bundled declarations (notes etc.). |
|
5 |
*) |
|
6 |
||
7 |
signature BUNDLE = |
|
8 |
sig |
|
9 |
type bundle = (thm list * Args.src list) list |
|
10 |
val the_bundle: Proof.context -> string -> bundle |
|
11 |
val check: Proof.context -> xstring * Position.T -> string |
|
12 |
val bundle: binding * (thm list * Args.src list) list -> |
|
13 |
(binding * typ option * mixfix) list -> local_theory -> local_theory |
|
14 |
val bundle_cmd: binding * (Facts.ref * Args.src list) list -> |
|
15 |
(binding * string option * mixfix) list -> local_theory -> local_theory |
|
47066
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
16 |
val include_: string list -> Proof.state -> Proof.state |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
17 |
val include_cmd: (xstring * Position.T) list -> Proof.state -> Proof.state |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
18 |
val including: string list -> Proof.state -> Proof.state |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
19 |
val including_cmd: (xstring * Position.T) list -> Proof.state -> Proof.state |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
20 |
val context_includes: string list -> local_theory -> local_theory |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
21 |
val context_includes_cmd: (xstring * Position.T) list -> local_theory -> local_theory |
47057 | 22 |
val print_bundles: Proof.context -> unit |
23 |
end; |
|
24 |
||
25 |
structure Bundle: BUNDLE = |
|
26 |
struct |
|
27 |
||
28 |
(* maintain bundles *) |
|
29 |
||
30 |
type bundle = (thm list * Args.src list) list; |
|
31 |
||
32 |
fun transform_bundle phi : bundle -> bundle = |
|
33 |
map (fn (fact, atts) => (Morphism.fact phi fact, map (Args.transform_values phi) atts)); |
|
34 |
||
35 |
structure Data = Generic_Data |
|
36 |
( |
|
37 |
type T = bundle Name_Space.table; |
|
38 |
val empty : T = Name_Space.empty_table "bundle"; |
|
39 |
val extend = I; |
|
40 |
val merge = Name_Space.merge_tables; |
|
41 |
); |
|
42 |
||
43 |
val get_bundles = Data.get o Context.Proof; |
|
44 |
||
45 |
fun the_bundle ctxt name = |
|
46 |
(case Symtab.lookup (#2 (get_bundles ctxt)) name of |
|
47 |
SOME bundle => bundle |
|
48 |
| NONE => error ("Unknown bundle " ^ quote name)); |
|
49 |
||
50 |
fun check ctxt = #1 o Name_Space.check (Context.Proof ctxt) (get_bundles ctxt); |
|
51 |
||
52 |
||
53 |
(* define bundle *) |
|
54 |
||
55 |
local |
|
56 |
||
57 |
fun gen_bundle prep_fact prep_att prep_vars (binding, raw_bundle) fixes lthy = |
|
58 |
let |
|
59 |
val (_, ctxt') = lthy |> prep_vars fixes |-> Proof_Context.add_fixes; |
|
60 |
val bundle0 = raw_bundle |
|
61 |
|> map (fn (fact, atts) => (prep_fact lthy fact, map (prep_att lthy) atts)); |
|
62 |
val bundle = |
|
63 |
Attrib.partial_evaluation ctxt' [(Attrib.empty_binding, bundle0)] |> map snd |> flat |
|
64 |
|> transform_bundle (Proof_Context.export_morphism ctxt' lthy); |
|
65 |
in |
|
66 |
lthy |> Local_Theory.declaration {syntax = false, pervasive = true} |
|
67 |
(fn phi => fn context => |
|
68 |
context |> Data.map |
|
69 |
(#2 o Name_Space.define context true |
|
70 |
(Morphism.binding phi binding, transform_bundle phi bundle))) |
|
71 |
end; |
|
72 |
||
73 |
in |
|
74 |
||
75 |
val bundle = gen_bundle (K I) (K I) Proof_Context.cert_vars; |
|
76 |
val bundle_cmd = |
|
77 |
gen_bundle Proof_Context.get_fact (Attrib.intern_src o Proof_Context.theory_of) |
|
78 |
Proof_Context.read_vars; |
|
79 |
||
80 |
end; |
|
81 |
||
82 |
||
47066
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
83 |
(* include bundles *) |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
84 |
|
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
85 |
local |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
86 |
|
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
87 |
fun gen_include prep raw_names ctxt = |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
88 |
let |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
89 |
val bundle = maps (the_bundle ctxt o prep ctxt) raw_names; |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
90 |
val attrib = Attrib.attribute_i (Proof_Context.theory_of ctxt); |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
91 |
val note = ((Binding.empty, []), map (apsnd (map attrib)) bundle); |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
92 |
in #2 (Proof_Context.note_thmss "" [note] ctxt) end; |
47057 | 93 |
|
47066
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
94 |
fun gen_includes prep raw_names lthy = |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
95 |
Named_Target.assert lthy |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
96 |
|> gen_include prep raw_names |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
97 |
|> Local_Theory.open_target (Local_Theory.naming_of lthy) (Local_Theory.operations_of lthy); |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
98 |
|
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
99 |
in |
47057 | 100 |
|
47066
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
101 |
fun include_ names = |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
102 |
Proof.assert_forward #> Proof.map_context (gen_include (K I) names) #> Proof.put_facts NONE; |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
103 |
fun include_cmd names = |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
104 |
Proof.assert_forward #> Proof.map_context (gen_include check names) #> Proof.put_facts NONE; |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
105 |
|
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
106 |
fun including names = Proof.assert_backward #> Proof.map_context (gen_include (K I) names); |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
107 |
fun including_cmd names = Proof.assert_backward #> Proof.map_context (gen_include check names); |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
108 |
|
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
109 |
val context_includes = gen_includes (K I); |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
110 |
val context_includes_cmd = gen_includes check; |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
111 |
|
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
112 |
end; |
47057 | 113 |
|
114 |
||
115 |
(* print_bundles *) |
|
116 |
||
117 |
fun print_bundles ctxt = |
|
118 |
let |
|
119 |
val prt_thm = Pretty.backquote o Display.pretty_thm ctxt; |
|
120 |
||
121 |
fun prt_fact (ths, []) = map prt_thm ths |
|
122 |
| prt_fact (ths, atts) = Pretty.enclose "(" ")" |
|
123 |
(Pretty.breaks (map prt_thm ths)) :: Attrib.pretty_attribs ctxt atts; |
|
124 |
||
125 |
fun prt_bundle (name, bundle) = |
|
126 |
Pretty.block (Pretty.command "bundle" :: Pretty.str " " :: Pretty.str name :: |
|
127 |
Pretty.breaks (Pretty.str " =" :: maps prt_fact bundle)); |
|
128 |
in |
|
129 |
map prt_bundle (Name_Space.extern_table ctxt (get_bundles ctxt)) |
|
130 |
end |> Pretty.chunks |> Pretty.writeln; |
|
131 |
||
132 |
end; |
|
133 |