author | wenzelm |
Tue, 07 Sep 2021 21:16:22 +0200 | |
changeset 74261 | d28a51dd9da6 |
parent 74122 | 7d3e818fe21f |
child 74561 | 8e6c973003c8 |
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 |
|
72605 | 9 |
type name = string |
74261
d28a51dd9da6
export other entities, e.g. relevant for formal document output;
wenzelm
parents:
74122
diff
changeset
|
10 |
val bundle_space: Context.generic -> Name_Space.T |
47057 | 11 |
val check: Proof.context -> xstring * Position.T -> string |
72605 | 12 |
val get: Proof.context -> name -> Attrib.thms |
72452
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
13 |
val read: Proof.context -> xstring * Position.T -> Attrib.thms |
74122 | 14 |
val extern: Proof.context -> string -> xstring |
63270 | 15 |
val print_bundles: bool -> Proof.context -> unit |
63267 | 16 |
val bundle: binding * Attrib.thms -> |
47057 | 17 |
(binding * typ option * mixfix) list -> local_theory -> local_theory |
58011
bc6bced136e5
tuned signature -- moved type src to Token, without aliases;
wenzelm
parents:
56334
diff
changeset
|
18 |
val bundle_cmd: binding * (Facts.ref * Token.src list) list -> |
47057 | 19 |
(binding * string option * mixfix) list -> local_theory -> local_theory |
63270 | 20 |
val init: binding -> theory -> local_theory |
72605 | 21 |
val unbundle: name list -> local_theory -> local_theory |
63282 | 22 |
val unbundle_cmd: (xstring * Position.T) list -> local_theory -> local_theory |
72605 | 23 |
val includes: name list -> Proof.context -> Proof.context |
47067
4ef29b0c568f
optional 'includes' element for long theorem statements;
wenzelm
parents:
47066
diff
changeset
|
24 |
val includes_cmd: (xstring * Position.T) list -> Proof.context -> Proof.context |
72605 | 25 |
val include_: name list -> Proof.state -> Proof.state |
47066
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
26 |
val include_cmd: (xstring * Position.T) list -> Proof.state -> Proof.state |
72605 | 27 |
val including: name list -> Proof.state -> Proof.state |
47066
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
28 |
val including_cmd: (xstring * Position.T) list -> Proof.state -> Proof.state |
47057 | 29 |
end; |
30 |
||
31 |
structure Bundle: BUNDLE = |
|
32 |
struct |
|
33 |
||
63270 | 34 |
(** context data **) |
35 |
||
36 |
structure Data = Generic_Data |
|
37 |
( |
|
38 |
type T = Attrib.thms Name_Space.table * Attrib.thms option; |
|
74261
d28a51dd9da6
export other entities, e.g. relevant for formal document output;
wenzelm
parents:
74122
diff
changeset
|
39 |
val empty : T = (Name_Space.empty_table Markup.bundleN, NONE); |
63270 | 40 |
val extend = I; |
41 |
fun merge ((tab1, target1), (tab2, target2)) = |
|
42 |
(Name_Space.merge_tables (tab1, tab2), merge_options (target1, target2)); |
|
43 |
); |
|
44 |
||
45 |
||
46 |
(* bundles *) |
|
47 |
||
72605 | 48 |
type name = string |
49 |
||
72452
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
50 |
val get_all_generic = #1 o Data.get; |
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
51 |
val get_all = get_all_generic o Context.Proof; |
63270 | 52 |
|
74261
d28a51dd9da6
export other entities, e.g. relevant for formal document output;
wenzelm
parents:
74122
diff
changeset
|
53 |
val bundle_space = Name_Space.space_of_table o #1 o Data.get; |
d28a51dd9da6
export other entities, e.g. relevant for formal document output;
wenzelm
parents:
74122
diff
changeset
|
54 |
|
72452
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
55 |
fun check ctxt = #1 o Name_Space.check (Context.Proof ctxt) (get_all ctxt); |
63270 | 56 |
|
72452
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
57 |
val get = Name_Space.get o get_all_generic o Context.Proof; |
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
58 |
|
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
59 |
fun read ctxt = get ctxt o check ctxt; |
63270 | 60 |
|
74122 | 61 |
fun extern ctxt = Name_Space.extern ctxt (Name_Space.space_of_table (get_all ctxt)); |
62 |
||
67627 | 63 |
fun define_bundle (b, bundle) context = |
63270 | 64 |
let |
67652 | 65 |
val bundle' = Attrib.trim_context_thms bundle; |
72452
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
66 |
val (name, bundles') = Name_Space.define context true (b, bundle') (get_all_generic context); |
63270 | 67 |
val context' = (Data.map o apfst o K) bundles' context; |
68 |
in (name, context') end; |
|
69 |
||
70 |
||
71 |
(* target -- bundle under construction *) |
|
72 |
||
73 |
fun the_target thy = |
|
74 |
(case #2 (Data.get (Context.Theory thy)) of |
|
75 |
SOME thms => thms |
|
76 |
| NONE => error "Missing bundle target"); |
|
77 |
||
78 |
val reset_target = (Context.theory_map o Data.map o apsnd o K) NONE; |
|
67652 | 79 |
val set_target = Context.theory_map o Data.map o apsnd o K o SOME o Attrib.trim_context_thms; |
63270 | 80 |
|
81 |
fun augment_target thms = |
|
82 |
Local_Theory.background_theory (fn thy => set_target (the_target thy @ thms) thy); |
|
83 |
||
84 |
||
85 |
(* print bundles *) |
|
86 |
||
87 |
fun pretty_bundle ctxt (markup_name, bundle) = |
|
88 |
let |
|
89 |
val prt_thm = Pretty.cartouche o Thm.pretty_thm ctxt; |
|
63275 | 90 |
fun prt_thm_attribs atts th = |
91 |
Pretty.block (Pretty.breaks (prt_thm th :: Attrib.pretty_attribs ctxt atts)); |
|
63270 | 92 |
fun prt_thms (ths, []) = map prt_thm ths |
63275 | 93 |
| prt_thms (ths, atts) = map (prt_thm_attribs atts) ths; |
63270 | 94 |
in |
95 |
Pretty.block ([Pretty.keyword1 "bundle", Pretty.str " ", Pretty.mark_str markup_name] @ |
|
63275 | 96 |
(if null bundle then [] else Pretty.fbreaks (Pretty.str " =" :: maps prt_thms bundle))) |
63270 | 97 |
end; |
98 |
||
99 |
fun print_bundles verbose ctxt = |
|
100 |
Pretty.writeln_chunks |
|
72452
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
101 |
(map (pretty_bundle ctxt) (Name_Space.markup_table verbose ctxt (get_all ctxt))); |
63270 | 102 |
|
103 |
||
104 |
||
105 |
(** define bundle **) |
|
47057 | 106 |
|
63267 | 107 |
fun transform_bundle phi = |
61814
1ca1142e1711
clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents:
61268
diff
changeset
|
108 |
map (fn (fact, atts) => (Morphism.fact phi fact, (map o map) (Token.transform phi) atts)); |
47057 | 109 |
|
110 |
||
63270 | 111 |
(* command *) |
47057 | 112 |
|
113 |
local |
|
114 |
||
60469 | 115 |
fun gen_bundle prep_fact prep_att add_fixes (binding, raw_bundle) raw_fixes lthy = |
47057 | 116 |
let |
60469 | 117 |
val (_, ctxt') = add_fixes raw_fixes lthy; |
47057 | 118 |
val bundle0 = raw_bundle |
53111 | 119 |
|> map (fn (fact, atts) => (prep_fact ctxt' fact, map (prep_att ctxt') atts)); |
47057 | 120 |
val bundle = |
63352 | 121 |
Attrib.partial_evaluation ctxt' [(Binding.empty_atts, bundle0)] |> map snd |> flat |
47057 | 122 |
|> transform_bundle (Proof_Context.export_morphism ctxt' lthy); |
123 |
in |
|
124 |
lthy |> Local_Theory.declaration {syntax = false, pervasive = true} |
|
63270 | 125 |
(fn phi => #2 o define_bundle (Morphism.binding phi binding, transform_bundle phi bundle)) |
47057 | 126 |
end; |
127 |
||
128 |
in |
|
129 |
||
60469 | 130 |
val bundle = gen_bundle (K I) (K I) Proof_Context.add_fixes; |
131 |
val bundle_cmd = gen_bundle Proof_Context.get_fact Attrib.check_src Proof_Context.add_fixes_cmd; |
|
47057 | 132 |
|
133 |
end; |
|
134 |
||
135 |
||
63270 | 136 |
(* target *) |
137 |
||
138 |
local |
|
139 |
||
140 |
fun bad_operation _ = error "Not possible in bundle target"; |
|
141 |
||
63275 | 142 |
fun conclude invisible binding = |
63270 | 143 |
Local_Theory.background_theory_result (fn thy => |
63275 | 144 |
thy |
145 |
|> invisible ? Context_Position.set_visible_global false |
|
146 |
|> Context.Theory |
|
147 |
|> define_bundle (binding, the_target thy) |
|
63277 | 148 |
||> (Context.the_theory |
149 |
#> invisible ? Context_Position.restore_visible_global thy |
|
150 |
#> reset_target)); |
|
63270 | 151 |
|
152 |
fun pretty binding lthy = |
|
153 |
let |
|
154 |
val bundle = the_target (Proof_Context.theory_of lthy); |
|
63275 | 155 |
val (name, lthy') = lthy |
156 |
|> Local_Theory.raw_theory (Context_Position.set_visible_global false) |
|
157 |
|> conclude true binding; |
|
63270 | 158 |
val thy_ctxt' = Proof_Context.init_global (Proof_Context.theory_of lthy'); |
159 |
val markup_name = |
|
72452
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
160 |
Name_Space.markup_extern thy_ctxt' (Name_Space.space_of_table (get_all thy_ctxt')) name; |
63270 | 161 |
in [pretty_bundle lthy' (markup_name, bundle)] end; |
162 |
||
163 |
fun bundle_notes kind facts lthy = |
|
164 |
let |
|
165 |
val bundle = facts |
|
166 |
|> maps (fn ((_, more_atts), thms) => map (fn (ths, atts) => (ths, atts @ more_atts)) thms); |
|
167 |
in |
|
168 |
lthy |
|
169 |
|> augment_target (transform_bundle (Local_Theory.standard_morphism_theory lthy) bundle) |
|
63278
6c963f1fc388
avoid duplicate Attrib.local_notes in aux. context;
wenzelm
parents:
63277
diff
changeset
|
170 |
|> Generic_Target.standard_notes (op <>) kind facts |
63270 | 171 |
|> Attrib.local_notes kind facts |
172 |
end; |
|
173 |
||
174 |
fun bundle_declaration decl lthy = |
|
175 |
lthy |
|
176 |
|> (augment_target o Attrib.internal_declaration) |
|
177 |
(Morphism.transform (Local_Theory.standard_morphism_theory lthy) decl) |
|
178 |
|> Generic_Target.standard_declaration (K true) decl; |
|
179 |
||
180 |
in |
|
181 |
||
182 |
fun init binding thy = |
|
183 |
thy |
|
72516
17dc99589a91
unified Local_Theory.init with Generic_Target.init
haftmann
parents:
72504
diff
changeset
|
184 |
|> Local_Theory.init |
66335
a849ce33923d
treat exit separate from regular local theory operations
haftmann
parents:
63352
diff
changeset
|
185 |
{background_naming = Sign.naming_of thy, |
66337
5caea089dd17
more structural sharing between common target Generic_Target.init
haftmann
parents:
66336
diff
changeset
|
186 |
setup = set_target [] #> Proof_Context.init_global, |
5caea089dd17
more structural sharing between common target Generic_Target.init
haftmann
parents:
66336
diff
changeset
|
187 |
conclude = conclude false binding #> #2} |
63270 | 188 |
{define = bad_operation, |
189 |
notes = bundle_notes, |
|
190 |
abbrev = bad_operation, |
|
191 |
declaration = K bundle_declaration, |
|
192 |
theory_registration = bad_operation, |
|
193 |
locale_dependency = bad_operation, |
|
66335
a849ce33923d
treat exit separate from regular local theory operations
haftmann
parents:
63352
diff
changeset
|
194 |
pretty = pretty binding} |
63270 | 195 |
|
196 |
end; |
|
197 |
||
198 |
||
199 |
||
63282 | 200 |
(** activate bundles **) |
47066
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
201 |
|
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
202 |
local |
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
203 |
|
72452
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
204 |
fun gen_activate notes prep_bundle args ctxt = |
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
205 |
let val decls = maps (prep_bundle ctxt) args in |
63031 | 206 |
ctxt |
207 |
|> Context_Position.set_visible false |
|
63352 | 208 |
|> notes [(Binding.empty_atts, decls)] |> #2 |
63031 | 209 |
|> Context_Position.restore_visible ctxt |
210 |
end; |
|
47057 | 211 |
|
72504 | 212 |
fun gen_unbundle prep_bundle = gen_activate Local_Theory.notes prep_bundle; |
213 |
||
72452
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
214 |
fun gen_includes prep_bundle = gen_activate (Attrib.local_notes "") prep_bundle; |
63282 | 215 |
|
72504 | 216 |
fun gen_include prep_bundle bs = |
217 |
Proof.assert_forward #> Proof.map_context (gen_includes prep_bundle bs) #> Proof.reset_facts; |
|
218 |
||
219 |
fun gen_including prep_bundle bs = |
|
220 |
Proof.assert_backward #> Proof.map_context (gen_includes prep_bundle bs) |
|
221 |
||
47066
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
222 |
in |
47057 | 223 |
|
72504 | 224 |
val unbundle = gen_unbundle get; |
225 |
val unbundle_cmd = gen_unbundle read; |
|
63282 | 226 |
|
72452
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
227 |
val includes = gen_includes get; |
9017dfa56367
avoid _cmd suffix where no Isar command is involved
haftmann
parents:
72450
diff
changeset
|
228 |
val includes_cmd = gen_includes read; |
47066
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
229 |
|
72504 | 230 |
val include_ = gen_include get; |
231 |
val include_cmd = gen_include read; |
|
47066
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
232 |
|
72504 | 233 |
val including = gen_including get; |
234 |
val including_cmd = gen_including read; |
|
47067
4ef29b0c568f
optional 'includes' element for long theorem statements;
wenzelm
parents:
47066
diff
changeset
|
235 |
|
47066
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47057
diff
changeset
|
236 |
end; |
47057 | 237 |
|
238 |
end; |