src/Pure/Isar/target_context.ML
author haftmann
Mon, 12 Oct 2020 07:25:38 +0000
changeset 72453 e4dde7beab39
child 72502 ff181cd78bb7
permissions -rw-r--r--
dedicated module for toplevel target handling
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72453
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Isar/target_context.ML
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
     3
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
     4
Handling of named and nested targets at the Isar toplevel via context begin / end blocks.
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
     5
*)
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
     6
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
     7
signature TARGET_CONTEXT =
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
     8
sig
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
     9
  val context_begin_named_cmd: xstring * Position.T -> theory -> local_theory
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    10
  val end_named_cmd: local_theory -> theory
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    11
  val switch_named_cmd: (xstring * Position.T) option -> Context.generic ->
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    12
    (local_theory -> Context.generic) * local_theory
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    13
  val context_begin_nested_cmd: (xstring * Position.T) list -> Element.context list ->
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    14
    Context.generic -> local_theory
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    15
  val end_nested_cmd: local_theory -> Context.generic
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    16
end;
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    17
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    18
structure Target_Context: TARGET_CONTEXT =
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    19
struct
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    20
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    21
fun context_begin_named_cmd ("-", _) thy = Named_Target.theory_init thy
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    22
  | context_begin_named_cmd target thy = Named_Target.init (Locale.check thy target) thy;
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    23
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    24
val end_named_cmd = Local_Theory.assert_bottom #> Local_Theory.exit_global;
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    25
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    26
fun switch_named_cmd NONE (Context.Theory thy) =
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    27
      (Context.Theory o end_named_cmd, Named_Target.theory_init thy)
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    28
  | switch_named_cmd (SOME name) (Context.Theory thy) =
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    29
      (Context.Theory o end_named_cmd, context_begin_named_cmd name thy)
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    30
  | switch_named_cmd NONE (Context.Proof lthy) =
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    31
      (Context.Proof o Local_Theory.reset, lthy)
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    32
  | switch_named_cmd (SOME name) (Context.Proof lthy) =
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    33
      (Context.Proof o Named_Target.reinit lthy o end_named_cmd,
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    34
        (context_begin_named_cmd name o end_named_cmd o Local_Theory.assert_nonbrittle) lthy);
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    35
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    36
fun includes raw_incls lthy =
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    37
  Bundle.includes (map (Bundle.check lthy) raw_incls) lthy;
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    38
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    39
fun context_begin_nested_cmd raw_incls raw_elems gthy =
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    40
  gthy
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    41
  |> Context.cases Named_Target.theory_init Local_Theory.assert
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    42
  |> includes raw_incls
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    43
  |> Expression.read_declaration ([], []) I raw_elems
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    44
  |> (#4 o fst)
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    45
  |> Local_Theory.begin_nested
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    46
  |> snd;
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    47
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    48
fun end_nested_cmd lthy = 
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    49
  if Named_Target.is_theory lthy
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    50
  then Context.Theory (Local_Theory.exit_global lthy)
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    51
  else Context.Proof lthy;
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    52
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    53
end;
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    54
e4dde7beab39 dedicated module for toplevel target handling
haftmann
parents:
diff changeset
    55
structure Local_Theory : LOCAL_THEORY = struct open Local_Theory; end;