1 (* Title: Pure/Isar/local_theory.ML
4 Local theory operations, with abstract target context.
7 type local_theory = Proof.context;
8 type generic_theory = Context.generic;
10 signature LOCAL_THEORY =
13 val affirm: local_theory -> local_theory
14 val naming_of: local_theory -> Name_Space.naming
15 val full_name: local_theory -> binding -> string
16 val map_naming: (Name_Space.naming -> Name_Space.naming) -> local_theory -> local_theory
17 val conceal: local_theory -> local_theory
18 val new_group: local_theory -> local_theory
19 val reset_group: local_theory -> local_theory
20 val restore_naming: local_theory -> local_theory -> local_theory
21 val target_of: local_theory -> Proof.context
22 val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
23 val raw_theory: (theory -> theory) -> local_theory -> local_theory
24 val background_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
25 val background_theory: (theory -> theory) -> local_theory -> local_theory
26 val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
27 val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
28 val map_contexts: (Context.generic -> Context.generic) -> local_theory -> local_theory
29 val propagate_ml_env: generic_theory -> generic_theory
30 val standard_morphism: local_theory -> Proof.context -> morphism
31 val target_morphism: local_theory -> morphism
32 val global_morphism: local_theory -> morphism
33 val define: (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
34 (term * (string * thm)) * local_theory
35 val note: Attrib.binding * thm list -> local_theory -> (string * thm list) * local_theory
36 val notes: (Attrib.binding * (thm list * Attrib.src list) list) list ->
37 local_theory -> (string * thm list) list * local_theory
38 val notes_kind: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
39 local_theory -> (string * thm list) list * local_theory
40 val abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
41 (term * term) * local_theory
42 val declaration: bool -> declaration -> local_theory -> local_theory
43 val syntax_declaration: bool -> declaration -> local_theory -> local_theory
44 val pretty: local_theory -> Pretty.T list
45 val set_defsort: sort -> local_theory -> local_theory
46 val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> local_theory -> local_theory
47 val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
48 val class_alias: binding -> class -> local_theory -> local_theory
49 val type_alias: binding -> string -> local_theory -> local_theory
50 val const_alias: binding -> string -> local_theory -> local_theory
51 val init: serial option -> string -> operations -> Proof.context -> local_theory
52 val restore: local_theory -> local_theory
53 val exit: local_theory -> Proof.context
54 val exit_global: local_theory -> theory
55 val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
56 val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
59 structure Local_Theory: LOCAL_THEORY =
62 (** local theory data **)
67 {define: (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
68 (term * (string * thm)) * local_theory,
70 (Attrib.binding * (thm list * Attrib.src list) list) list ->
71 local_theory -> (string * thm list) list * local_theory,
72 abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
73 (term * term) * local_theory,
74 declaration: bool -> declaration -> local_theory -> local_theory,
75 syntax_declaration: bool -> declaration -> local_theory -> local_theory,
76 pretty: local_theory -> Pretty.T list,
77 exit: local_theory -> Proof.context};
79 datatype lthy = LThy of
80 {naming: Name_Space.naming,
81 theory_prefix: string,
82 operations: operations,
83 target: Proof.context};
85 fun make_lthy (naming, theory_prefix, operations, target) =
86 LThy {naming = naming, theory_prefix = theory_prefix,
87 operations = operations, target = target};
92 structure Data = Proof_Data
99 (case Data.get lthy of
100 NONE => error "No local theory context"
101 | SOME (LThy data) => data);
103 fun map_lthy f lthy =
104 let val {naming, theory_prefix, operations, target} = get_lthy lthy
105 in Data.put (SOME (make_lthy (f (naming, theory_prefix, operations, target)))) lthy end;
107 val affirm = tap get_lthy;
112 val naming_of = #naming o get_lthy;
113 val full_name = Name_Space.full_name o naming_of;
115 fun map_naming f = map_lthy (fn (naming, theory_prefix, operations, target) =>
116 (f naming, theory_prefix, operations, target));
118 val conceal = map_naming Name_Space.conceal;
119 val new_group = map_naming Name_Space.new_group;
120 val reset_group = map_naming Name_Space.reset_group;
122 val restore_naming = map_naming o K o naming_of;
127 val target_of = #target o get_lthy;
129 fun map_target f = map_lthy (fn (naming, theory_prefix, operations, target) =>
130 (naming, theory_prefix, operations, f target));
133 (* substructure mappings *)
135 fun raw_theory_result f lthy =
137 val (res, thy') = f (Proof_Context.theory_of lthy);
139 |> map_target (Proof_Context.transfer thy')
140 |> Proof_Context.transfer thy';
143 fun raw_theory f = #2 o raw_theory_result (f #> pair ());
145 val checkpoint = raw_theory Theory.checkpoint;
147 fun background_theory_result f lthy =
148 lthy |> raw_theory_result (fn thy =>
150 |> Sign.map_naming (K (naming_of lthy))
152 ||> Sign.restore_naming thy
153 ||> Theory.checkpoint);
155 fun background_theory f = #2 o background_theory_result (f #> pair ());
157 fun target_result f lthy =
159 val (res, ctxt') = target_of lthy
160 |> Context_Position.set_visible false
162 ||> Context_Position.restore_visible lthy;
163 val thy' = Proof_Context.theory_of ctxt';
165 |> map_target (K ctxt')
166 |> Proof_Context.transfer thy';
169 fun target f = #2 o target_result (f #> pair ());
172 background_theory (Context.theory_map f) #>
173 target (Context.proof_map f) #>
176 fun propagate_ml_env (context as Context.Proof lthy) =
177 Context.Proof (map_contexts (ML_Env.inherit context) lthy)
178 | propagate_ml_env context = context;
183 fun standard_morphism lthy ctxt =
184 Proof_Context.norm_export_morphism lthy ctxt $>
185 Morphism.binding_morphism (Name_Space.transform_binding (naming_of lthy));
187 fun target_morphism lthy = standard_morphism lthy (target_of lthy);
188 fun global_morphism lthy =
189 standard_morphism lthy (Proof_Context.init_global (Proof_Context.theory_of lthy));
192 (* primitive operations *)
194 fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
195 fun operation1 f x = operation (fn ops => f ops x);
196 fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
198 val pretty = operation #pretty;
199 val abbrev = apsnd checkpoint ooo operation2 #abbrev;
200 val define = apsnd checkpoint oo operation1 #define;
201 val notes_kind = apsnd checkpoint ooo operation2 #notes;
202 val declaration = checkpoint ooo operation2 #declaration;
203 val syntax_declaration = checkpoint ooo operation2 #syntax_declaration;
207 (** basic derived operations **)
209 val notes = notes_kind "";
210 fun note (a, ths) = notes [(a, [(ths, [])])] #>> the_single;
213 syntax_declaration false (K (Context.mapping (Sign.set_defsort S) (Proof_Context.set_defsort S)));
218 fun type_notation add mode raw_args lthy =
219 let val args = map (apfst (Morphism.typ (target_morphism lthy))) raw_args
220 in syntax_declaration false (Proof_Context.target_type_notation add mode args) lthy end;
222 fun notation add mode raw_args lthy =
223 let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
224 in syntax_declaration false (Proof_Context.target_notation add mode args) lthy end;
227 (* name space aliases *)
229 fun alias global_alias local_alias b name =
230 syntax_declaration false (fn phi =>
231 let val b' = Morphism.binding phi b
232 in Context.mapping (global_alias b' name) (local_alias b' name) end)
233 #> local_alias b name;
235 val class_alias = alias Sign.class_alias Proof_Context.class_alias;
236 val type_alias = alias Sign.type_alias Proof_Context.type_alias;
237 val const_alias = alias Sign.const_alias Proof_Context.const_alias;
241 (** init and exit **)
245 fun init group theory_prefix operations target =
247 Sign.naming_of (Proof_Context.theory_of target)
248 |> Name_Space.set_group group
249 |> Name_Space.mandatory_path theory_prefix;
252 (fn NONE => SOME (make_lthy (naming, theory_prefix, operations, target))
253 | SOME _ => error "Local theory already initialized")
258 let val {naming, theory_prefix, operations, target} = get_lthy lthy
259 in init (Name_Space.get_group naming) theory_prefix operations target end;
264 val exit = Proof_Context.background_theory Theory.checkpoint o operation #exit;
265 val exit_global = Proof_Context.theory_of o exit;
267 fun exit_result f (x, lthy) =
269 val ctxt = exit lthy;
270 val phi = standard_morphism lthy ctxt;
271 in (f phi x, ctxt) end;
273 fun exit_result_global f (x, lthy) =
275 val thy = exit_global lthy;
276 val thy_ctxt = Proof_Context.init_global thy;
277 val phi = standard_morphism lthy thy_ctxt;
278 in (f phi x, thy) end;