| author | Fabian Huch <huch@in.tum.de> | 
| Wed, 03 Jul 2024 21:11:24 +0200 | |
| changeset 80497 | bd00bdf39c86 | 
| parent 72953 | 90ada01470cb | 
| child 81116 | 0fb1e2dd4122 | 
| permissions | -rw-r--r-- | 
| 72453 | 1 | (* Title: Pure/Isar/target_context.ML | 
| 2 | Author: Florian Haftmann | |
| 3 | ||
| 4 | Handling of named and nested targets at the Isar toplevel via context begin / end blocks. | |
| 5 | *) | |
| 6 | ||
| 7 | signature TARGET_CONTEXT = | |
| 8 | sig | |
| 72536 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 9 | val context_begin_named_cmd: (xstring * Position.T) list -> xstring * Position.T -> | 
| 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 10 | theory -> local_theory | 
| 72505 | 11 | val end_named_cmd: local_theory -> Context.generic | 
| 72453 | 12 | val switch_named_cmd: (xstring * Position.T) option -> Context.generic -> | 
| 13 | (local_theory -> Context.generic) * local_theory | |
| 14 | val context_begin_nested_cmd: (xstring * Position.T) list -> Element.context list -> | |
| 15 | Context.generic -> local_theory | |
| 16 | val end_nested_cmd: local_theory -> Context.generic | |
| 17 | end; | |
| 18 | ||
| 19 | structure Target_Context: TARGET_CONTEXT = | |
| 20 | struct | |
| 21 | ||
| 72536 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 22 | fun prep_includes thy = | 
| 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 23 | map (Bundle.check (Proof_Context.init_global thy)); | 
| 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 24 | |
| 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 25 | fun context_begin_named_cmd raw_includes ("-", _) thy =
 | 
| 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 26 | Named_Target.init (prep_includes thy raw_includes) "" thy | 
| 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 27 | | context_begin_named_cmd raw_includes name thy = | 
| 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 28 | Named_Target.init (prep_includes thy raw_includes) (Locale.check thy name) thy; | 
| 72453 | 29 | |
| 72505 | 30 | val end_named_cmd = Context.Theory o Local_Theory.exit_global; | 
| 72453 | 31 | |
| 32 | fun switch_named_cmd NONE (Context.Theory thy) = | |
| 72536 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 33 | (end_named_cmd, Named_Target.theory_init thy) | 
| 72453 | 34 | | switch_named_cmd (SOME name) (Context.Theory thy) = | 
| 72536 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 35 | (end_named_cmd, context_begin_named_cmd [] name thy) | 
| 72453 | 36 | | switch_named_cmd NONE (Context.Proof lthy) = | 
| 37 | (Context.Proof o Local_Theory.reset, lthy) | |
| 38 | | switch_named_cmd (SOME name) (Context.Proof lthy) = | |
| 72953 | 39 | let | 
| 40 | val (reinit, thy) = Named_Target.exit_global_reinitialize lthy | |
| 41 | in | |
| 42 | (Context.Proof o reinit o Local_Theory.exit_global, | |
| 43 | context_begin_named_cmd [] name thy) | |
| 44 | end; | |
| 72453 | 45 | |
| 72536 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 46 | fun includes raw_includes lthy = | 
| 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 47 | Bundle.includes (map (Bundle.check lthy) raw_includes) lthy; | 
| 72453 | 48 | |
| 72536 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 49 | fun context_begin_nested_cmd raw_includes raw_elems gthy = | 
| 72453 | 50 | gthy | 
| 51 | |> Context.cases Named_Target.theory_init Local_Theory.assert | |
| 72536 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72505diff
changeset | 52 | |> includes raw_includes | 
| 72453 | 53 | |> Expression.read_declaration ([], []) I raw_elems | 
| 54 | |> (#4 o fst) | |
| 55 | |> Local_Theory.begin_nested | |
| 56 | |> snd; | |
| 57 | ||
| 72505 | 58 | fun end_nested_cmd lthy = | 
| 59 | let | |
| 60 | val lthy' = Local_Theory.end_nested lthy | |
| 61 | in | |
| 62 | if Named_Target.is_theory lthy' | |
| 63 | then Context.Theory (Local_Theory.exit_global lthy') | |
| 64 | else Context.Proof lthy' | |
| 65 | end; | |
| 72453 | 66 | |
| 67 | end; | |
| 68 | ||
| 69 | structure Local_Theory : LOCAL_THEORY = struct open Local_Theory; end; |