author | wenzelm |
Thu, 05 Mar 2015 13:28:04 +0100 | |
changeset 59616 | eb59c6968219 |
parent 58665 | 50b229a5a097 |
child 59876 | 8564d7abe5c5 |
permissions | -rw-r--r-- |
41959 | 1 |
(* Title: Pure/Isar/named_target.ML |
20894 | 2 |
Author: Makarius |
30437 | 3 |
Author: Florian Haftmann, TU Muenchen |
20894 | 4 |
|
47066
8a6124d09ff5
basic support for nested contexts including bundles;
wenzelm
parents:
47061
diff
changeset
|
5 |
Targets for theory, locale, class -- at the bottom the nested structure. |
20894 | 6 |
*) |
7 |
||
38350
480b2de9927c
renamed Theory_Target to the more appropriate Named_Target
haftmann
parents:
38349
diff
changeset
|
8 |
signature NAMED_TARGET = |
20894 | 9 |
sig |
52140 | 10 |
val is_theory: local_theory -> bool |
57182 | 11 |
val locale_of: local_theory -> string option |
57195
ec0e10f11276
recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents:
57193
diff
changeset
|
12 |
val bottom_locale_of: local_theory -> string option |
57182 | 13 |
val class_of: local_theory -> string option |
57181
2d13bf9ea77b
dropped obscure and unused ad-hoc before_exit hook for named targets
haftmann
parents:
57177
diff
changeset
|
14 |
val init: string -> theory -> local_theory |
38388 | 15 |
val theory_init: theory -> local_theory |
58665 | 16 |
val theory_map: (local_theory -> local_theory) -> theory -> theory |
57485 | 17 |
val theory_like_init: (local_theory -> local_theory) -> theory -> local_theory |
57483
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
18 |
val begin: xstring * Position.T -> theory -> local_theory |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
19 |
val exit: local_theory -> theory |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
20 |
val switch: (xstring * Position.T) option -> Context.generic |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
21 |
-> (local_theory -> Context.generic) * local_theory |
20894 | 22 |
end; |
23 |
||
38350
480b2de9927c
renamed Theory_Target to the more appropriate Named_Target
haftmann
parents:
38349
diff
changeset
|
24 |
structure Named_Target: NAMED_TARGET = |
20894 | 25 |
struct |
26 |
||
21006 | 27 |
(* context data *) |
28 |
||
33519 | 29 |
structure Data = Proof_Data |
21006 | 30 |
( |
57196 | 31 |
type T = (string * bool) option; |
38391
ba1cc1815ce1
named target is optional; explicit Name_Target.reinit
haftmann
parents:
38389
diff
changeset
|
32 |
fun init _ = NONE; |
21006 | 33 |
); |
34 |
||
57196 | 35 |
val get_bottom_data = Data.get; |
57195
ec0e10f11276
recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents:
57193
diff
changeset
|
36 |
|
ec0e10f11276
recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents:
57193
diff
changeset
|
37 |
fun get_data lthy = |
57177
dce365931721
sharpened criterion: bare named target is only at the bottom level
haftmann
parents:
57176
diff
changeset
|
38 |
if Local_Theory.level lthy = 1 |
57195
ec0e10f11276
recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents:
57193
diff
changeset
|
39 |
then get_bottom_data lthy |
57177
dce365931721
sharpened criterion: bare named target is only at the bottom level
haftmann
parents:
57176
diff
changeset
|
40 |
else NONE; |
21006 | 41 |
|
57185 | 42 |
fun is_theory lthy = |
57195
ec0e10f11276
recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents:
57193
diff
changeset
|
43 |
case get_data lthy of |
57196 | 44 |
SOME ("", _) => true |
57185 | 45 |
| _ => false; |
52140 | 46 |
|
57484
cc309f3c0490
restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents:
57483
diff
changeset
|
47 |
fun target_of lthy = |
cc309f3c0490
restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents:
57483
diff
changeset
|
48 |
case get_data lthy of |
cc309f3c0490
restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents:
57483
diff
changeset
|
49 |
NONE => error "Not in a named target" |
cc309f3c0490
restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents:
57483
diff
changeset
|
50 |
| SOME (target, _) => target; |
cc309f3c0490
restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents:
57483
diff
changeset
|
51 |
|
57196 | 52 |
fun locale_name_of NONE = NONE |
53 |
| locale_name_of (SOME ("", _)) = NONE |
|
54 |
| locale_name_of (SOME (locale, _)) = SOME locale; |
|
57195
ec0e10f11276
recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents:
57193
diff
changeset
|
55 |
|
57196 | 56 |
val locale_of = locale_name_of o get_data; |
57 |
||
58 |
val bottom_locale_of = locale_name_of o get_bottom_data; |
|
57182 | 59 |
|
60 |
fun class_of lthy = |
|
57195
ec0e10f11276
recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents:
57193
diff
changeset
|
61 |
case get_data lthy of |
57196 | 62 |
SOME (class, true) => SOME class |
57182 | 63 |
| _ => NONE; |
64 |
||
21006 | 65 |
|
38318
1fade69519ab
different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents:
38314
diff
changeset
|
66 |
(* define *) |
1fade69519ab
different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents:
38314
diff
changeset
|
67 |
|
57074 | 68 |
fun locale_foundation locale (((b, U), mx), (b_def, rhs)) params = |
47289 | 69 |
Generic_Target.background_foundation (((b, U), NoSyn), (b_def, rhs)) params |
57074 | 70 |
#-> (fn (lhs, def) => Generic_Target.locale_const locale Syntax.mode_default ((b, mx), lhs) |
47282 | 71 |
#> pair (lhs, def)); |
38318
1fade69519ab
different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents:
38314
diff
changeset
|
72 |
|
57074 | 73 |
fun class_foundation class (((b, U), mx), (b_def, rhs)) params = |
47289 | 74 |
Generic_Target.background_foundation (((b, U), NoSyn), (b_def, rhs)) params |
57074 | 75 |
#-> (fn (lhs, def) => Class.const class ((b, mx), lhs) params |
47282 | 76 |
#> pair (lhs, def)); |
38318
1fade69519ab
different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents:
38314
diff
changeset
|
77 |
|
57196 | 78 |
fun foundation ("", _) = Generic_Target.theory_foundation |
79 |
| foundation (locale, false) = locale_foundation locale |
|
80 |
| foundation (class, true) = class_foundation class; |
|
38303
ad4b59e9d0f9
factored out foundation of `define` into separate function
haftmann
parents:
38302
diff
changeset
|
81 |
|
24939 | 82 |
|
38308
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
83 |
(* notes *) |
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
84 |
|
57196 | 85 |
fun notes ("", _) = Generic_Target.theory_notes |
86 |
| notes (locale, _) = Generic_Target.locale_notes locale; |
|
38308
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
87 |
|
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
88 |
|
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
89 |
(* abbrev *) |
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
90 |
|
57118 | 91 |
fun locale_abbrev locale prmode (b, mx) global_rhs params = |
57160 | 92 |
Generic_Target.background_abbrev (b, global_rhs) (snd params) |
57074 | 93 |
#-> (fn (lhs, _) => Generic_Target.locale_const locale prmode ((b, mx), lhs)); |
38308
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
94 |
|
57118 | 95 |
fun class_abbrev class prmode (b, mx) global_rhs params = |
57160 | 96 |
Generic_Target.background_abbrev (b, global_rhs) (snd params) |
57161
6254c51cd210
formal treatment of dangling parameters for class abbrevs analogously to class consts
haftmann
parents:
57160
diff
changeset
|
97 |
#-> (fn (lhs, rhs) => Class.abbrev class prmode ((b, mx), lhs) rhs params); |
57066
78651e94746f
tuned: prefer separate function trails for locales and classes rather than ad-hoc case distinction
haftmann
parents:
57065
diff
changeset
|
98 |
|
57196 | 99 |
fun abbrev ("", _) = Generic_Target.theory_abbrev |
100 |
| abbrev (locale, false) = locale_abbrev locale |
|
101 |
| abbrev (class, true) = class_abbrev class; |
|
38308
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
102 |
|
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
103 |
|
47246
2bbab021c0e6
clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents:
47081
diff
changeset
|
104 |
(* declaration *) |
2bbab021c0e6
clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents:
47081
diff
changeset
|
105 |
|
57196 | 106 |
fun declaration ("", _) flags decl = Generic_Target.theory_declaration decl |
107 |
| declaration (locale, _) flags decl = Generic_Target.locale_declaration locale flags decl; |
|
47246
2bbab021c0e6
clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents:
47081
diff
changeset
|
108 |
|
2bbab021c0e6
clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents:
47081
diff
changeset
|
109 |
|
56723
a8f71445c265
subscription as target-specific implementation device
haftmann
parents:
56056
diff
changeset
|
110 |
(* subscription *) |
a8f71445c265
subscription as target-specific implementation device
haftmann
parents:
56056
diff
changeset
|
111 |
|
57196 | 112 |
fun subscription ("", _) = Generic_Target.theory_registration |
113 |
| subscription (locale, _) = Generic_Target.locale_dependency locale; |
|
56723
a8f71445c265
subscription as target-specific implementation device
haftmann
parents:
56056
diff
changeset
|
114 |
|
a8f71445c265
subscription as target-specific implementation device
haftmann
parents:
56056
diff
changeset
|
115 |
|
38308
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
116 |
(* pretty *) |
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
117 |
|
57196 | 118 |
fun pretty (target, is_class) ctxt = |
38308
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
119 |
let |
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
120 |
val target_name = |
55763 | 121 |
[Pretty.keyword1 (if is_class then "class" else "locale"), Pretty.brk 1, |
52103 | 122 |
Locale.pretty_name ctxt target]; |
45353 | 123 |
val fixes = |
124 |
map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) |
|
125 |
(#1 (Proof_Context.inferred_fixes ctxt)); |
|
126 |
val assumes = |
|
127 |
map (fn A => (Attrib.empty_binding, [(Thm.term_of A, [])])) |
|
128 |
(Assumption.all_assms_of ctxt); |
|
38308
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
129 |
val elems = |
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
130 |
(if null fixes then [] else [Element.Fixes fixes]) @ |
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
131 |
(if null assumes then [] else [Element.Assumes assumes]); |
40782 | 132 |
val body_elems = |
57196 | 133 |
if target = "" then [] |
39639 | 134 |
else if null elems then [Pretty.block target_name] |
135 |
else [Pretty.block (Pretty.fbreaks (Pretty.block (target_name @ [Pretty.str " ="]) :: |
|
40782 | 136 |
map (Pretty.chunks o Element.pretty_ctxt ctxt) elems))]; |
38308
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
137 |
in |
55763 | 138 |
Pretty.block [Pretty.keyword1 "theory", Pretty.brk 1, |
42360 | 139 |
Pretty.str (Context.theory_name (Proof_Context.theory_of ctxt))] :: body_elems |
38308
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
140 |
end; |
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
141 |
|
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
142 |
|
38378 | 143 |
(* init *) |
25291 | 144 |
|
57196 | 145 |
fun make_name_data _ "" = ("", false) |
146 |
| make_name_data thy target = |
|
147 |
if Locale.defined thy target |
|
148 |
then (target, Class.is_class thy target) |
|
149 |
else error ("No such locale: " ^ quote target); |
|
150 |
||
151 |
fun init_context ("", _) = Proof_Context.init_global |
|
152 |
| init_context (locale, false) = Locale.init locale |
|
153 |
| init_context (class, true) = Class.init class; |
|
25269 | 154 |
|
57485 | 155 |
fun gen_init before_exit target thy = |
39639 | 156 |
let |
57196 | 157 |
val name_data = make_name_data thy target; |
47061
355317493f34
clarified Local_Theory.init: avoid hardwired naming policy, discontinued odd/unused group argument (cf. 5ee13e0428d2);
wenzelm
parents:
47000
diff
changeset
|
158 |
val naming = Sign.naming_of thy |
355317493f34
clarified Local_Theory.init: avoid hardwired naming policy, discontinued odd/unused group argument (cf. 5ee13e0428d2);
wenzelm
parents:
47000
diff
changeset
|
159 |
|> Name_Space.mandatory_path (Long_Name.base_name target); |
39639 | 160 |
in |
161 |
thy |
|
56056
4d46d53566e6
more efficient local theory operations, by imposing a linear change discipline on the main types/consts tables, in order to speed-up Proof_Context.transfer_syntax required for Local_Theory.raw_theory_result;
wenzelm
parents:
55763
diff
changeset
|
162 |
|> Sign.change_begin |
57196 | 163 |
|> init_context name_data |
57485 | 164 |
|> is_none before_exit ? Data.put (SOME name_data) |
47061
355317493f34
clarified Local_Theory.init: avoid hardwired naming policy, discontinued odd/unused group argument (cf. 5ee13e0428d2);
wenzelm
parents:
47000
diff
changeset
|
165 |
|> Local_Theory.init naming |
57196 | 166 |
{define = Generic_Target.define (foundation name_data), |
167 |
notes = Generic_Target.notes (notes name_data), |
|
168 |
abbrev = Generic_Target.abbrev (abbrev name_data), |
|
169 |
declaration = declaration name_data, |
|
170 |
subscription = subscription name_data, |
|
171 |
pretty = pretty name_data, |
|
57485 | 172 |
exit = the_default I before_exit |
173 |
#> Local_Theory.target_of #> Sign.change_end_local} |
|
39639 | 174 |
end; |
38247 | 175 |
|
57485 | 176 |
val init = gen_init NONE |
177 |
||
57181
2d13bf9ea77b
dropped obscure and unused ad-hoc before_exit hook for named targets
haftmann
parents:
57177
diff
changeset
|
178 |
val theory_init = init ""; |
25269 | 179 |
|
58665 | 180 |
fun theory_map f = theory_init #> f #> Local_Theory.exit_global; |
181 |
||
57485 | 182 |
fun theory_like_init before_exit = gen_init (SOME before_exit) ""; |
183 |
||
57483
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
184 |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
185 |
(* toplevel interaction *) |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
186 |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
187 |
fun begin ("-", _) thy = theory_init thy |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
188 |
| begin target thy = init (Locale.check thy target) thy; |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
189 |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
190 |
val exit = Local_Theory.assert_bottom true #> Local_Theory.exit_global; |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
191 |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
192 |
fun switch NONE (Context.Theory thy) = |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
193 |
(Context.Theory o exit, theory_init thy) |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
194 |
| switch (SOME name) (Context.Theory thy) = |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
195 |
(Context.Theory o exit, begin name thy) |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
196 |
| switch NONE (Context.Proof lthy) = |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
197 |
(Context.Proof o Local_Theory.restore, lthy) |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
198 |
| switch (SOME name) (Context.Proof lthy) = |
57484
cc309f3c0490
restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents:
57483
diff
changeset
|
199 |
(Context.Proof o init (target_of lthy) o exit, |
57483
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
200 |
(begin name o exit o Local_Theory.assert_nonbrittle) lthy); |
33282 | 201 |
|
20894 | 202 |
end; |