| author | haftmann |
| Fri, 04 Aug 2017 08:12:58 +0200 | |
| changeset 66337 | 5caea089dd17 |
| parent 66335 | a849ce33923d |
| child 66338 | 1a8441ec5ced |
| 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 |
|
66334
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
14 |
val init: string -> theory -> local_theory |
|
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
15 |
val init': {setup: local_theory -> local_theory, conclude: local_theory -> local_theory} ->
|
|
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
16 |
string -> theory -> local_theory |
| 38388 | 17 |
val theory_init: theory -> local_theory |
| 58665 | 18 |
val theory_map: (local_theory -> local_theory) -> theory -> theory |
|
57483
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
19 |
val begin: xstring * Position.T -> theory -> local_theory |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
20 |
val exit: local_theory -> theory |
| 63402 | 21 |
val switch: (xstring * Position.T) option -> Context.generic -> |
22 |
(local_theory -> Context.generic) * local_theory |
|
| 20894 | 23 |
end; |
24 |
||
|
38350
480b2de9927c
renamed Theory_Target to the more appropriate Named_Target
haftmann
parents:
38349
diff
changeset
|
25 |
structure Named_Target: NAMED_TARGET = |
| 20894 | 26 |
struct |
27 |
||
| 21006 | 28 |
(* context data *) |
29 |
||
| 66333 | 30 |
datatype target = Theory | Locale of string | Class of string; |
31 |
||
32 |
fun ident_of_target Theory = "" |
|
33 |
| ident_of_target (Locale locale) = locale |
|
34 |
| ident_of_target (Class class) = class; |
|
35 |
||
36 |
fun target_is_theory (SOME Theory) = true |
|
37 |
| target_is_theory _ = false; |
|
38 |
||
39 |
fun locale_of_target (SOME (Locale locale)) = SOME locale |
|
40 |
| locale_of_target (SOME (Class locale)) = SOME locale |
|
41 |
| locale_of_target _ = NONE; |
|
42 |
||
43 |
fun class_of_target (SOME (Class class)) = SOME class |
|
44 |
| class_of_target _ = NONE; |
|
45 |
||
| 33519 | 46 |
structure Data = Proof_Data |
| 21006 | 47 |
( |
| 66333 | 48 |
type T = target option; |
|
38391
ba1cc1815ce1
named target is optional; explicit Name_Target.reinit
haftmann
parents:
38389
diff
changeset
|
49 |
fun init _ = NONE; |
| 21006 | 50 |
); |
51 |
||
| 66333 | 52 |
val get_bottom_target = Data.get; |
|
57195
ec0e10f11276
recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents:
57193
diff
changeset
|
53 |
|
| 66333 | 54 |
fun get_target lthy = |
|
57177
dce365931721
sharpened criterion: bare named target is only at the bottom level
haftmann
parents:
57176
diff
changeset
|
55 |
if Local_Theory.level lthy = 1 |
| 66333 | 56 |
then get_bottom_target lthy |
|
57177
dce365931721
sharpened criterion: bare named target is only at the bottom level
haftmann
parents:
57176
diff
changeset
|
57 |
else NONE; |
| 21006 | 58 |
|
| 66333 | 59 |
fun ident_of lthy = |
60 |
case get_target lthy of |
|
|
57484
cc309f3c0490
restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents:
57483
diff
changeset
|
61 |
NONE => error "Not in a named target" |
| 66333 | 62 |
| SOME target => ident_of_target target; |
|
57484
cc309f3c0490
restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents:
57483
diff
changeset
|
63 |
|
| 66333 | 64 |
val is_theory = target_is_theory o get_target; |
|
57195
ec0e10f11276
recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents:
57193
diff
changeset
|
65 |
|
| 66333 | 66 |
val locale_of = locale_of_target o get_target; |
| 57196 | 67 |
|
| 66333 | 68 |
val bottom_locale_of = locale_of_target o get_bottom_target; |
| 57182 | 69 |
|
| 66333 | 70 |
val class_of = class_of_target o get_target; |
| 57182 | 71 |
|
| 21006 | 72 |
|
| 61777 | 73 |
(* operations *) |
|
38318
1fade69519ab
different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents:
38314
diff
changeset
|
74 |
|
| 57074 | 75 |
fun locale_foundation locale (((b, U), mx), (b_def, rhs)) params = |
| 47289 | 76 |
Generic_Target.background_foundation (((b, U), NoSyn), (b_def, rhs)) params |
| 57074 | 77 |
#-> (fn (lhs, def) => Generic_Target.locale_const locale Syntax.mode_default ((b, mx), lhs) |
| 47282 | 78 |
#> pair (lhs, def)); |
|
38318
1fade69519ab
different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents:
38314
diff
changeset
|
79 |
|
| 57074 | 80 |
fun class_foundation class (((b, U), mx), (b_def, rhs)) params = |
| 47289 | 81 |
Generic_Target.background_foundation (((b, U), NoSyn), (b_def, rhs)) params |
| 57074 | 82 |
#-> (fn (lhs, def) => Class.const class ((b, mx), lhs) params |
| 47282 | 83 |
#> pair (lhs, def)); |
|
38318
1fade69519ab
different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents:
38314
diff
changeset
|
84 |
|
| 66333 | 85 |
fun foundation Theory = Generic_Target.theory_target_foundation |
86 |
| foundation (Locale locale) = locale_foundation locale |
|
87 |
| foundation (Class class) = class_foundation class; |
|
|
38303
ad4b59e9d0f9
factored out foundation of `define` into separate function
haftmann
parents:
38302
diff
changeset
|
88 |
|
| 66333 | 89 |
fun notes Theory = Generic_Target.theory_target_notes |
90 |
| notes (Locale locale) = Generic_Target.locale_target_notes locale |
|
91 |
| notes (Class class) = Generic_Target.locale_target_notes class; |
|
|
38308
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
92 |
|
| 66333 | 93 |
fun abbrev Theory = Generic_Target.theory_abbrev |
94 |
| abbrev (Locale locale) = Generic_Target.locale_abbrev locale |
|
95 |
| abbrev (Class class) = Class.abbrev class; |
|
|
38308
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
96 |
|
| 66333 | 97 |
fun declaration Theory _ decl = Generic_Target.theory_declaration decl |
98 |
| declaration (Locale locale) flags decl = Generic_Target.locale_declaration locale flags decl |
|
99 |
| declaration (Class class) flags decl = Generic_Target.locale_declaration class flags decl; |
|
|
47246
2bbab021c0e6
clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents:
47081
diff
changeset
|
100 |
|
| 66333 | 101 |
fun theory_registration Theory = Generic_Target.theory_registration |
|
61890
f6ded81f5690
abandoned attempt to unify sublocale and interpretation into global theories
haftmann
parents:
61777
diff
changeset
|
102 |
| theory_registration _ = (fn _ => error "Not possible in theory target"); |
|
f6ded81f5690
abandoned attempt to unify sublocale and interpretation into global theories
haftmann
parents:
61777
diff
changeset
|
103 |
|
| 66333 | 104 |
fun locale_dependency Theory = (fn _ => error "Not possible in theory target") |
105 |
| locale_dependency (Locale locale) = Generic_Target.locale_dependency locale |
|
106 |
| locale_dependency (Class class) = Generic_Target.locale_dependency class; |
|
|
56723
a8f71445c265
subscription as target-specific implementation device
haftmann
parents:
56056
diff
changeset
|
107 |
|
| 66333 | 108 |
fun pretty Theory ctxt = |
109 |
[Pretty.block [Pretty.keyword1 "theory", Pretty.brk 1, |
|
110 |
Pretty.str (Context.theory_name (Proof_Context.theory_of ctxt))]] |
|
111 |
| pretty (Locale locale) ctxt = Locale.pretty_locale (Proof_Context.theory_of ctxt) false locale |
|
112 |
| pretty (Class class) ctxt = |
|
113 |
Class.pretty_specification (Proof_Context.theory_of ctxt) class; |
|
|
38308
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
114 |
|
|
0e4649095739
try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents:
38307
diff
changeset
|
115 |
|
| 38378 | 116 |
(* init *) |
| 25291 | 117 |
|
| 66333 | 118 |
fun make_target _ "" = Theory |
119 |
| make_target thy ident = |
|
120 |
if Locale.defined thy ident |
|
121 |
then (if Class.is_class thy ident then Class else Locale) ident |
|
122 |
else error ("No such locale: " ^ quote ident);
|
|
| 57196 | 123 |
|
| 66333 | 124 |
fun init_context Theory = Proof_Context.init_global |
125 |
| init_context (Locale locale) = Locale.init locale |
|
126 |
| init_context (Class class) = Class.init class; |
|
| 25269 | 127 |
|
|
66334
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
128 |
fun init' {setup, conclude} ident thy =
|
| 39639 | 129 |
let |
| 66333 | 130 |
val target = make_target thy ident; |
| 39639 | 131 |
in |
132 |
thy |
|
|
66337
5caea089dd17
more structural sharing between common target Generic_Target.init
haftmann
parents:
66335
diff
changeset
|
133 |
|> Generic_Target.init |
|
66335
a849ce33923d
treat exit separate from regular local theory operations
haftmann
parents:
66334
diff
changeset
|
134 |
{background_naming = Sign.naming_of thy |> Name_Space.mandatory_path (Long_Name.base_name ident),
|
|
66337
5caea089dd17
more structural sharing between common target Generic_Target.init
haftmann
parents:
66335
diff
changeset
|
135 |
setup = init_context target #> setup, |
|
5caea089dd17
more structural sharing between common target Generic_Target.init
haftmann
parents:
66335
diff
changeset
|
136 |
conclude = conclude} |
| 66333 | 137 |
{define = Generic_Target.define (foundation target),
|
138 |
notes = Generic_Target.notes (notes target), |
|
139 |
abbrev = abbrev target, |
|
140 |
declaration = declaration target, |
|
141 |
theory_registration = theory_registration target, |
|
142 |
locale_dependency = locale_dependency target, |
|
|
66335
a849ce33923d
treat exit separate from regular local theory operations
haftmann
parents:
66334
diff
changeset
|
143 |
pretty = pretty target} |
| 39639 | 144 |
end; |
| 38247 | 145 |
|
|
66334
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
146 |
fun init ident thy = |
|
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
147 |
init' {setup = Data.put (SOME (make_target thy ident)), conclude = I} ident thy;
|
|
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
148 |
|
|
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
149 |
val theory_init = init ""; |
| 66333 | 150 |
|
| 58665 | 151 |
fun theory_map f = theory_init #> f #> Local_Theory.exit_global; |
152 |
||
|
57483
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
153 |
|
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
154 |
(* toplevel interaction *) |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
155 |
|
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
156 |
fun begin ("-", _) thy = theory_init thy
|
|
66334
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
157 |
| begin target thy = init (Locale.check thy target) thy; |
|
57483
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
158 |
|
| 66259 | 159 |
val exit = Local_Theory.assert_bottom #> Local_Theory.exit_global; |
|
57483
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
160 |
|
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
161 |
fun switch NONE (Context.Theory thy) = |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
162 |
(Context.Theory o exit, theory_init thy) |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
163 |
| switch (SOME name) (Context.Theory thy) = |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
164 |
(Context.Theory o exit, begin name thy) |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
165 |
| switch NONE (Context.Proof lthy) = |
| 62514 | 166 |
(Context.Proof o Local_Theory.reset, lthy) |
|
57483
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
167 |
| switch (SOME name) (Context.Proof lthy) = |
|
66334
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
168 |
(Context.Proof o init (ident_of lthy) o exit, |
|
57483
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
169 |
(begin name o exit o Local_Theory.assert_nonbrittle) lthy); |
| 33282 | 170 |
|
| 20894 | 171 |
end; |