author | wenzelm |
Mon, 15 Jan 2018 14:31:57 +0100 | |
changeset 67438 | fdb7b995974d |
parent 66338 | 1a8441ec5ced |
child 67615 | 967213048e55 |
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 |
|
66338 | 5 |
Targets for theory, locale, class -- at the bottom of 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 |
||
66338 | 32 |
fun target_of_ident _ "" = Theory |
33 |
| target_of_ident thy ident = |
|
34 |
if Locale.defined thy ident |
|
35 |
then (if Class.is_class thy ident then Class else Locale) ident |
|
36 |
else error ("No such locale: " ^ quote ident); |
|
37 |
||
66333 | 38 |
fun ident_of_target Theory = "" |
39 |
| ident_of_target (Locale locale) = locale |
|
40 |
| ident_of_target (Class class) = class; |
|
41 |
||
42 |
fun target_is_theory (SOME Theory) = true |
|
43 |
| target_is_theory _ = false; |
|
44 |
||
45 |
fun locale_of_target (SOME (Locale locale)) = SOME locale |
|
46 |
| locale_of_target (SOME (Class locale)) = SOME locale |
|
47 |
| locale_of_target _ = NONE; |
|
48 |
||
49 |
fun class_of_target (SOME (Class class)) = SOME class |
|
50 |
| class_of_target _ = NONE; |
|
51 |
||
33519 | 52 |
structure Data = Proof_Data |
21006 | 53 |
( |
66333 | 54 |
type T = target option; |
38391
ba1cc1815ce1
named target is optional; explicit Name_Target.reinit
haftmann
parents:
38389
diff
changeset
|
55 |
fun init _ = NONE; |
21006 | 56 |
); |
57 |
||
66333 | 58 |
val get_bottom_target = Data.get; |
57195
ec0e10f11276
recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents:
57193
diff
changeset
|
59 |
|
66333 | 60 |
fun get_target lthy = |
57177
dce365931721
sharpened criterion: bare named target is only at the bottom level
haftmann
parents:
57176
diff
changeset
|
61 |
if Local_Theory.level lthy = 1 |
66333 | 62 |
then get_bottom_target lthy |
57177
dce365931721
sharpened criterion: bare named target is only at the bottom level
haftmann
parents:
57176
diff
changeset
|
63 |
else NONE; |
21006 | 64 |
|
66333 | 65 |
fun ident_of lthy = |
66 |
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
|
67 |
NONE => error "Not in a named target" |
66333 | 68 |
| 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
|
69 |
|
66333 | 70 |
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
|
71 |
|
66333 | 72 |
val locale_of = locale_of_target o get_target; |
57196 | 73 |
|
66333 | 74 |
val bottom_locale_of = locale_of_target o get_bottom_target; |
57182 | 75 |
|
66333 | 76 |
val class_of = class_of_target o get_target; |
57182 | 77 |
|
21006 | 78 |
|
61777 | 79 |
(* operations *) |
38318
1fade69519ab
different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents:
38314
diff
changeset
|
80 |
|
57074 | 81 |
fun locale_foundation locale (((b, U), mx), (b_def, rhs)) params = |
47289 | 82 |
Generic_Target.background_foundation (((b, U), NoSyn), (b_def, rhs)) params |
57074 | 83 |
#-> (fn (lhs, def) => Generic_Target.locale_const locale Syntax.mode_default ((b, mx), lhs) |
47282 | 84 |
#> pair (lhs, def)); |
38318
1fade69519ab
different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents:
38314
diff
changeset
|
85 |
|
57074 | 86 |
fun class_foundation class (((b, U), mx), (b_def, rhs)) params = |
47289 | 87 |
Generic_Target.background_foundation (((b, U), NoSyn), (b_def, rhs)) params |
57074 | 88 |
#-> (fn (lhs, def) => Class.const class ((b, mx), lhs) params |
47282 | 89 |
#> pair (lhs, def)); |
38318
1fade69519ab
different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents:
38314
diff
changeset
|
90 |
|
66338 | 91 |
fun operations Theory = |
92 |
{define = Generic_Target.define Generic_Target.theory_target_foundation, |
|
93 |
notes = Generic_Target.notes Generic_Target.theory_target_notes, |
|
94 |
abbrev = Generic_Target.theory_abbrev, |
|
95 |
declaration = fn _ => Generic_Target.theory_declaration, |
|
96 |
theory_registration = Generic_Target.theory_registration, |
|
97 |
locale_dependency = fn _ => error "Not possible in theory target", |
|
98 |
pretty = fn ctxt => [Pretty.block [Pretty.keyword1 "theory", Pretty.brk 1, |
|
99 |
Pretty.str (Context.theory_name (Proof_Context.theory_of ctxt))]]} |
|
100 |
| operations (Locale locale) = |
|
101 |
{define = Generic_Target.define (locale_foundation locale), |
|
102 |
notes = Generic_Target.notes (Generic_Target.locale_target_notes locale), |
|
103 |
abbrev = Generic_Target.locale_abbrev locale, |
|
104 |
declaration = Generic_Target.locale_declaration locale, |
|
105 |
theory_registration = fn _ => error "Not possible in locale target", |
|
106 |
locale_dependency = Generic_Target.locale_dependency locale, |
|
107 |
pretty = fn ctxt => Locale.pretty_locale (Proof_Context.theory_of ctxt) false locale} |
|
108 |
| operations (Class class) = |
|
109 |
{define = Generic_Target.define (class_foundation class), |
|
110 |
notes = Generic_Target.notes (Generic_Target.locale_target_notes class), |
|
111 |
abbrev = Class.abbrev class, |
|
112 |
declaration = Generic_Target.locale_declaration class, |
|
113 |
theory_registration = fn _ => error "Not possible in class target", |
|
114 |
locale_dependency = Generic_Target.locale_dependency class, |
|
115 |
pretty = fn ctxt => Class.pretty_specification (Proof_Context.theory_of ctxt) class}; |
|
61890
f6ded81f5690
abandoned attempt to unify sublocale and interpretation into global theories
haftmann
parents:
61777
diff
changeset
|
116 |
|
66338 | 117 |
fun setup_context Theory = Proof_Context.init_global |
118 |
| setup_context (Locale locale) = Locale.init locale |
|
119 |
| setup_context (Class class) = Class.init class; |
|
25269 | 120 |
|
66334
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
121 |
fun init' {setup, conclude} ident thy = |
39639 | 122 |
let |
66338 | 123 |
val target = target_of_ident thy ident; |
39639 | 124 |
in |
125 |
thy |
|
66337
5caea089dd17
more structural sharing between common target Generic_Target.init
haftmann
parents:
66335
diff
changeset
|
126 |
|> Generic_Target.init |
66335
a849ce33923d
treat exit separate from regular local theory operations
haftmann
parents:
66334
diff
changeset
|
127 |
{background_naming = Sign.naming_of thy |> Name_Space.mandatory_path (Long_Name.base_name ident), |
66338 | 128 |
setup = setup_context target #> setup, |
66337
5caea089dd17
more structural sharing between common target Generic_Target.init
haftmann
parents:
66335
diff
changeset
|
129 |
conclude = conclude} |
66338 | 130 |
(operations target) |
39639 | 131 |
end; |
38247 | 132 |
|
66334
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
133 |
fun init ident thy = |
66338 | 134 |
init' {setup = Data.put (SOME (target_of_ident thy ident)), conclude = I} ident thy; |
66334
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
135 |
|
b210ae666a42
provide explicit variant initializers for regular named target vs. almost-named target
haftmann
parents:
66333
diff
changeset
|
136 |
val theory_init = init ""; |
66333 | 137 |
|
58665 | 138 |
fun theory_map f = theory_init #> f #> Local_Theory.exit_global; |
139 |
||
57483
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
140 |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
141 |
(* toplevel interaction *) |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
142 |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
143 |
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
|
144 |
| 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
|
145 |
|
66259 | 146 |
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
|
147 |
|
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
148 |
fun switch NONE (Context.Theory thy) = |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
149 |
(Context.Theory o exit, theory_init thy) |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
150 |
| switch (SOME name) (Context.Theory thy) = |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
151 |
(Context.Theory o exit, begin name thy) |
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
152 |
| switch NONE (Context.Proof lthy) = |
62514 | 153 |
(Context.Proof o Local_Theory.reset, lthy) |
57483
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents:
57196
diff
changeset
|
154 |
| 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
|
155 |
(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
|
156 |
(begin name o exit o Local_Theory.assert_nonbrittle) lthy); |
33282 | 157 |
|
20894 | 158 |
end; |