src/Pure/Isar/toplevel.ML
author haftmann
Thu Aug 12 13:23:46 2010 +0200 (2010-08-12)
changeset 38388 94d5624dd1f7
parent 38384 07c33be08476
child 38389 d7d915bae307
permissions -rw-r--r--
Named_Target.theory_init
wenzelm@5828
     1
(*  Title:      Pure/Isar/toplevel.ML
wenzelm@5828
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5828
     3
wenzelm@26602
     4
Isabelle/Isar toplevel transactions.
wenzelm@5828
     5
*)
wenzelm@5828
     6
wenzelm@5828
     7
signature TOPLEVEL =
wenzelm@5828
     8
sig
wenzelm@19063
     9
  exception UNDEF
wenzelm@5828
    10
  type state
wenzelm@26602
    11
  val toplevel: state
wenzelm@7732
    12
  val is_toplevel: state -> bool
wenzelm@18589
    13
  val is_theory: state -> bool
wenzelm@18589
    14
  val is_proof: state -> bool
wenzelm@17076
    15
  val level: state -> int
wenzelm@30398
    16
  val presentation_context_of: state -> Proof.context
wenzelm@30801
    17
  val previous_context_of: state -> Proof.context option
wenzelm@21506
    18
  val context_of: state -> Proof.context
wenzelm@22089
    19
  val generic_theory_of: state -> generic_theory
wenzelm@5828
    20
  val theory_of: state -> theory
wenzelm@5828
    21
  val proof_of: state -> Proof.state
wenzelm@18589
    22
  val proof_position_of: state -> int
wenzelm@37953
    23
  val end_theory: Position.T -> state -> theory
wenzelm@16815
    24
  val print_state_context: state -> unit
wenzelm@16815
    25
  val print_state: bool -> state -> unit
wenzelm@37858
    26
  val pretty_abstract: state -> Pretty.T
wenzelm@32738
    27
  val quiet: bool Unsynchronized.ref
wenzelm@32738
    28
  val debug: bool Unsynchronized.ref
wenzelm@32738
    29
  val interact: bool Unsynchronized.ref
wenzelm@32738
    30
  val timing: bool Unsynchronized.ref
wenzelm@32738
    31
  val profiling: int Unsynchronized.ref
wenzelm@32738
    32
  val skip_proofs: bool Unsynchronized.ref
wenzelm@5828
    33
  exception TERMINATE
wenzelm@27583
    34
  exception TOPLEVEL_ERROR
wenzelm@20128
    35
  val program: (unit -> 'a) -> 'a
wenzelm@33604
    36
  val thread: bool -> (unit -> unit) -> Thread.thread
wenzelm@16682
    37
  type transition
wenzelm@5828
    38
  val empty: transition
wenzelm@27441
    39
  val init_of: transition -> string option
wenzelm@27427
    40
  val name_of: transition -> string
wenzelm@28105
    41
  val pos_of: transition -> Position.T
wenzelm@27500
    42
  val str_of: transition -> string
wenzelm@5828
    43
  val name: string -> transition -> transition
wenzelm@5828
    44
  val position: Position.T -> transition -> transition
wenzelm@5828
    45
  val interactive: bool -> transition -> transition
wenzelm@5828
    46
  val print: transition -> transition
wenzelm@9010
    47
  val no_timing: transition -> transition
wenzelm@37977
    48
  val init_theory: string -> (unit -> theory) -> transition -> transition
wenzelm@37977
    49
  val modify_init: (unit -> theory) -> transition -> transition
wenzelm@6689
    50
  val exit: transition -> transition
wenzelm@5828
    51
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    52
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@5828
    53
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@27840
    54
  val ignored: Position.T -> transition
wenzelm@27840
    55
  val malformed: Position.T -> string -> transition
wenzelm@5828
    56
  val theory: (theory -> theory) -> transition -> transition
wenzelm@26491
    57
  val generic_theory: (generic_theory -> generic_theory) -> transition -> transition
wenzelm@7612
    58
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@20985
    59
  val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
wenzelm@21007
    60
  val end_local_theory: transition -> transition
wenzelm@29380
    61
  val local_theory': xstring option -> (bool -> local_theory -> local_theory) ->
wenzelm@29380
    62
    transition -> transition
wenzelm@20963
    63
  val local_theory: xstring option -> (local_theory -> local_theory) -> transition -> transition
wenzelm@30366
    64
  val present_local_theory: xstring option -> (state -> unit) -> transition -> transition
berghofe@24453
    65
  val local_theory_to_proof': xstring option -> (bool -> local_theory -> Proof.state) ->
berghofe@24453
    66
    transition -> transition
wenzelm@21007
    67
  val local_theory_to_proof: xstring option -> (local_theory -> Proof.state) ->
wenzelm@21007
    68
    transition -> transition
wenzelm@17363
    69
  val theory_to_proof: (theory -> Proof.state) -> transition -> transition
wenzelm@21007
    70
  val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition
wenzelm@21007
    71
  val forget_proof: transition -> transition
wenzelm@30366
    72
  val present_proof: (state -> unit) -> transition -> transition
wenzelm@21177
    73
  val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@17904
    74
  val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
wenzelm@21177
    75
  val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@21177
    76
  val proof: (Proof.state -> Proof.state) -> transition -> transition
wenzelm@33390
    77
  val actual_proof: (Proof_Node.T -> Proof_Node.T) -> transition -> transition
wenzelm@27564
    78
  val skip_proof: (int -> int) -> transition -> transition
wenzelm@17904
    79
  val skip_proof_to_theory: (int -> bool) -> transition -> transition
wenzelm@27427
    80
  val get_id: transition -> string option
wenzelm@27427
    81
  val put_id: string -> transition -> transition
wenzelm@9512
    82
  val unknown_theory: transition -> transition
wenzelm@9512
    83
  val unknown_proof: transition -> transition
wenzelm@9512
    84
  val unknown_context: transition -> transition
wenzelm@28425
    85
  val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b
wenzelm@27606
    86
  val status: transition -> Markup.T -> unit
wenzelm@26602
    87
  val error_msg: transition -> exn * string -> unit
wenzelm@28103
    88
  val add_hook: (transition -> state -> state -> unit) -> unit
wenzelm@26602
    89
  val transition: bool -> transition -> state -> (state * (exn * string) option) option
wenzelm@37687
    90
  val run_command: string -> transition -> state -> state option
wenzelm@28425
    91
  val command: transition -> state -> state
wenzelm@37953
    92
  val excursion: (transition * transition list) list -> (transition * state) list lazy * theory
wenzelm@5828
    93
end;
wenzelm@5828
    94
wenzelm@6965
    95
structure Toplevel: TOPLEVEL =
wenzelm@5828
    96
struct
wenzelm@5828
    97
wenzelm@5828
    98
(** toplevel state **)
wenzelm@5828
    99
wenzelm@31476
   100
exception UNDEF = Runtime.UNDEF;
wenzelm@19063
   101
wenzelm@19063
   102
wenzelm@21294
   103
(* local theory wrappers *)
wenzelm@5828
   104
haftmann@38350
   105
val loc_init = Named_Target.context_cmd;
wenzelm@33671
   106
val loc_exit = Local_Theory.exit_global;
wenzelm@21294
   107
wenzelm@25292
   108
fun loc_begin loc (Context.Theory thy) = loc_init (the_default "-" loc) thy
wenzelm@21294
   109
  | loc_begin NONE (Context.Proof lthy) = lthy
haftmann@25269
   110
  | loc_begin (SOME loc) (Context.Proof lthy) = loc_init loc (loc_exit lthy);
wenzelm@21294
   111
wenzelm@21294
   112
fun loc_finish _ (Context.Theory _) = Context.Theory o loc_exit
wenzelm@33671
   113
  | loc_finish NONE (Context.Proof _) = Context.Proof o Local_Theory.restore
haftmann@38380
   114
  | loc_finish (SOME _) (Context.Proof lthy) = fn lthy' => let
haftmann@38380
   115
        val target_name = #target (Named_Target.peek lthy);
haftmann@38380
   116
        val target = if target_name = "" then NONE else SOME target_name;
haftmann@38380
   117
      in Context.Proof (Named_Target.init target (loc_exit lthy')) end;
wenzelm@21294
   118
wenzelm@21294
   119
wenzelm@21958
   120
(* datatype node *)
wenzelm@21294
   121
wenzelm@5828
   122
datatype node =
wenzelm@27576
   123
  Theory of generic_theory * Proof.context option
wenzelm@27576
   124
    (*theory with presentation context*) |
wenzelm@33390
   125
  Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory)
wenzelm@27576
   126
    (*proof node, finish, original theory*) |
wenzelm@27564
   127
  SkipProof of int * (generic_theory * generic_theory);
wenzelm@27564
   128
    (*proof depth, resulting theory, original theory*)
wenzelm@5828
   129
wenzelm@20963
   130
val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
wenzelm@18589
   131
val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
wenzelm@18589
   132
wenzelm@20963
   133
fun cases_node f _ (Theory (gthy, _)) = f gthy
wenzelm@33390
   134
  | cases_node _ g (Proof (prf, _)) = g (Proof_Node.current prf)
wenzelm@21007
   135
  | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy;
wenzelm@19063
   136
wenzelm@29066
   137
val context_node = cases_node Context.proof_of Proof.context_of;
wenzelm@29066
   138
wenzelm@21958
   139
wenzelm@21958
   140
(* datatype state *)
wenzelm@21958
   141
wenzelm@37953
   142
datatype state = State of node option * node option;  (*current, previous*)
wenzelm@5828
   143
wenzelm@27576
   144
val toplevel = State (NONE, NONE);
wenzelm@5828
   145
wenzelm@27576
   146
fun is_toplevel (State (NONE, _)) = true
wenzelm@7732
   147
  | is_toplevel _ = false;
wenzelm@7732
   148
wenzelm@27576
   149
fun level (State (NONE, _)) = 0
wenzelm@37953
   150
  | level (State (SOME (Theory _), _)) = 0
wenzelm@37953
   151
  | level (State (SOME (Proof (prf, _)), _)) = Proof.level (Proof_Node.current prf)
wenzelm@37953
   152
  | level (State (SOME (SkipProof (d, _)), _)) = d + 1;   (*different notion of proof depth!*)
wenzelm@17076
   153
wenzelm@27576
   154
fun str_of_state (State (NONE, _)) = "at top level"
wenzelm@37953
   155
  | str_of_state (State (SOME (Theory (Context.Theory _, _)), _)) = "in theory mode"
wenzelm@37953
   156
  | str_of_state (State (SOME (Theory (Context.Proof _, _)), _)) = "in local theory mode"
wenzelm@37953
   157
  | str_of_state (State (SOME (Proof _), _)) = "in proof mode"
wenzelm@37953
   158
  | str_of_state (State (SOME (SkipProof _), _)) = "in skipped proof mode";
wenzelm@5946
   159
wenzelm@5946
   160
wenzelm@27576
   161
(* current node *)
wenzelm@5828
   162
wenzelm@27576
   163
fun node_of (State (NONE, _)) = raise UNDEF
wenzelm@37953
   164
  | node_of (State (SOME node, _)) = node;
wenzelm@5828
   165
wenzelm@18589
   166
fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
wenzelm@18589
   167
fun is_proof state = not (is_toplevel state) andalso is_some (proof_node (node_of state));
wenzelm@18589
   168
wenzelm@19063
   169
fun node_case f g state = cases_node f g (node_of state);
wenzelm@5828
   170
wenzelm@30398
   171
fun presentation_context_of state =
wenzelm@30398
   172
  (case try node_of state of
wenzelm@30398
   173
    SOME (Theory (_, SOME ctxt)) => ctxt
wenzelm@30398
   174
  | SOME node => context_node node
wenzelm@30398
   175
  | NONE => raise UNDEF);
wenzelm@30366
   176
wenzelm@30801
   177
fun previous_context_of (State (_, NONE)) = NONE
wenzelm@37953
   178
  | previous_context_of (State (_, SOME prev)) = SOME (context_node prev);
wenzelm@30801
   179
wenzelm@21506
   180
val context_of = node_case Context.proof_of Proof.context_of;
wenzelm@22089
   181
val generic_theory_of = node_case I (Context.Proof o Proof.context_of);
wenzelm@20963
   182
val theory_of = node_case Context.theory_of Proof.theory_of;
wenzelm@18589
   183
val proof_of = node_case (fn _ => raise UNDEF) I;
wenzelm@17208
   184
wenzelm@18589
   185
fun proof_position_of state =
wenzelm@18589
   186
  (case node_of state of
wenzelm@33390
   187
    Proof (prf, _) => Proof_Node.position prf
wenzelm@18589
   188
  | _ => raise UNDEF);
wenzelm@6664
   189
wenzelm@37953
   190
fun end_theory _ (State (NONE, SOME (Theory (Context.Theory thy, _)))) = Theory.end_theory thy
wenzelm@37953
   191
  | end_theory pos _ = error ("Unfinished theory at end of input" ^ Position.str_of pos);
wenzelm@37953
   192
wenzelm@5828
   193
wenzelm@16815
   194
(* print state *)
wenzelm@16815
   195
haftmann@38388
   196
val pretty_context = Local_Theory.pretty o Context.cases (Named_Target.theory_init) I;
wenzelm@16815
   197
wenzelm@23640
   198
fun print_state_context state =
wenzelm@24795
   199
  (case try node_of state of
wenzelm@21506
   200
    NONE => []
wenzelm@24795
   201
  | SOME (Theory (gthy, _)) => pretty_context gthy
wenzelm@24795
   202
  | SOME (Proof (_, (_, gthy))) => pretty_context gthy
wenzelm@24795
   203
  | SOME (SkipProof (_, (gthy, _))) => pretty_context gthy)
wenzelm@23640
   204
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@16815
   205
wenzelm@23640
   206
fun print_state prf_only state =
wenzelm@23701
   207
  (case try node_of state of
wenzelm@23701
   208
    NONE => []
wenzelm@23701
   209
  | SOME (Theory (gthy, _)) => if prf_only then [] else pretty_context gthy
wenzelm@23701
   210
  | SOME (Proof (prf, _)) =>
wenzelm@33390
   211
      Proof.pretty_state (Proof_Node.position prf) (Proof_Node.current prf)
wenzelm@27564
   212
  | SOME (SkipProof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)])
wenzelm@23701
   213
  |> Pretty.markup_chunks Markup.state |> Pretty.writeln;
wenzelm@16815
   214
wenzelm@37858
   215
fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">");
wenzelm@37858
   216
wenzelm@16815
   217
wenzelm@15668
   218
wenzelm@5828
   219
(** toplevel transitions **)
wenzelm@5828
   220
wenzelm@32738
   221
val quiet = Unsynchronized.ref false;
wenzelm@22135
   222
val debug = Output.debugging;
wenzelm@32738
   223
val interact = Unsynchronized.ref false;
wenzelm@16682
   224
val timing = Output.timing;
wenzelm@32738
   225
val profiling = Unsynchronized.ref 0;
wenzelm@32738
   226
val skip_proofs = Unsynchronized.ref false;
wenzelm@16682
   227
wenzelm@31476
   228
exception TERMINATE = Runtime.TERMINATE;
wenzelm@31476
   229
exception EXCURSION_FAIL = Runtime.EXCURSION_FAIL;
wenzelm@31476
   230
exception TOPLEVEL_ERROR = Runtime.TOPLEVEL_ERROR;
wenzelm@20128
   231
wenzelm@33604
   232
fun program body =
wenzelm@33604
   233
 (body
wenzelm@31476
   234
  |> Runtime.controlled_execution
wenzelm@33604
   235
  |> Runtime.toplevel_error (Output.error_msg o ML_Compiler.exn_message)) ();
wenzelm@33604
   236
wenzelm@33604
   237
fun thread interrupts body =
wenzelm@33604
   238
  Thread.fork
wenzelm@33604
   239
    (((fn () => body () handle Exn.Interrupt => ())
wenzelm@33604
   240
        |> Runtime.debugging
wenzelm@33604
   241
        |> Runtime.toplevel_error
wenzelm@33604
   242
          (fn exn => priority ("## INTERNAL ERROR ##\n" ^ ML_Compiler.exn_message exn))),
wenzelm@37216
   243
      Simple_Thread.attributes interrupts);
wenzelm@20128
   244
wenzelm@5828
   245
wenzelm@27601
   246
(* node transactions -- maintaining stable checkpoints *)
wenzelm@7022
   247
wenzelm@31476
   248
exception FAILURE of state * exn;
wenzelm@31476
   249
wenzelm@6689
   250
local
wenzelm@6689
   251
wenzelm@30366
   252
fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
wenzelm@30366
   253
  | reset_presentation node = node;
wenzelm@30366
   254
wenzelm@26624
   255
fun is_draft_theory (Theory (gthy, _)) = Context.is_draft (Context.theory_of gthy)
wenzelm@26624
   256
  | is_draft_theory _ = false;
wenzelm@26624
   257
wenzelm@31476
   258
fun is_stale state = Context.is_stale (theory_of state) handle Runtime.UNDEF => false;
wenzelm@27601
   259
wenzelm@26624
   260
fun stale_error NONE = SOME (ERROR "Stale theory encountered after successful execution!")
wenzelm@26624
   261
  | stale_error some = some;
wenzelm@16815
   262
wenzelm@27576
   263
fun map_theory f (Theory (gthy, ctxt)) =
wenzelm@33671
   264
      Theory (Context.mapping f (Local_Theory.raw_theory f) gthy, ctxt)
wenzelm@27576
   265
  | map_theory _ node = node;
wenzelm@6689
   266
wenzelm@6689
   267
in
wenzelm@6689
   268
wenzelm@37953
   269
fun apply_transaction f g node =
wenzelm@20128
   270
  let
wenzelm@27576
   271
    val _ = is_draft_theory node andalso error "Illegal draft theory in toplevel state";
wenzelm@27576
   272
    val cont_node = reset_presentation node;
wenzelm@27576
   273
    val back_node = map_theory (Theory.checkpoint o Theory.copy) cont_node;
wenzelm@37953
   274
    fun state_error e nd = (State (SOME nd, SOME node), e);
wenzelm@6689
   275
wenzelm@20128
   276
    val (result, err) =
wenzelm@20128
   277
      cont_node
wenzelm@31476
   278
      |> Runtime.controlled_execution f
wenzelm@26624
   279
      |> map_theory Theory.checkpoint
wenzelm@26624
   280
      |> state_error NONE
wenzelm@26624
   281
      handle exn => state_error (SOME exn) cont_node;
wenzelm@26624
   282
wenzelm@26624
   283
    val (result', err') =
wenzelm@26624
   284
      if is_stale result then state_error (stale_error err) back_node
wenzelm@26624
   285
      else (result, err);
wenzelm@20128
   286
  in
wenzelm@26624
   287
    (case err' of
wenzelm@30366
   288
      NONE => tap g result'
wenzelm@26624
   289
    | SOME exn => raise FAILURE (result', exn))
wenzelm@20128
   290
  end;
wenzelm@6689
   291
wenzelm@6689
   292
end;
wenzelm@6689
   293
wenzelm@6689
   294
wenzelm@6689
   295
(* primitive transitions *)
wenzelm@6689
   296
wenzelm@5828
   297
datatype trans =
wenzelm@37977
   298
  Init of string * (unit -> theory) |    (*theory name, init*)
wenzelm@37953
   299
  Exit |                                 (*formal exit of theory*)
wenzelm@37953
   300
  Keep of bool -> state -> unit |        (*peek at state*)
wenzelm@30366
   301
  Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
wenzelm@21958
   302
wenzelm@6689
   303
local
wenzelm@5828
   304
wenzelm@37977
   305
fun apply_tr _ (Init (_, f)) (State (NONE, _)) =
wenzelm@33727
   306
      State (SOME (Theory (Context.Theory
wenzelm@37977
   307
          (Theory.checkpoint (Runtime.controlled_execution f ())), NONE)), NONE)
wenzelm@37953
   308
  | apply_tr _ Exit (State (prev as SOME (Theory (Context.Theory _, _)), _)) =
wenzelm@27603
   309
      State (NONE, prev)
wenzelm@32792
   310
  | apply_tr int (Keep f) state =
wenzelm@31476
   311
      Runtime.controlled_execution (fn x => tap (f int) x) state
wenzelm@32792
   312
  | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
wenzelm@32792
   313
      apply_transaction (fn x => f int x) g state
wenzelm@32792
   314
  | apply_tr _ _ _ = raise UNDEF;
wenzelm@5828
   315
wenzelm@32792
   316
fun apply_union _ [] state = raise FAILURE (state, UNDEF)
wenzelm@32792
   317
  | apply_union int (tr :: trs) state =
wenzelm@32792
   318
      apply_union int trs state
wenzelm@32792
   319
        handle Runtime.UNDEF => apply_tr int tr state
wenzelm@32792
   320
          | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
wenzelm@6689
   321
          | exn as FAILURE _ => raise exn
wenzelm@6689
   322
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   323
wenzelm@6689
   324
in
wenzelm@6689
   325
wenzelm@32792
   326
fun apply_trans int trs state = (apply_union int trs state, NONE)
skalberg@15531
   327
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   328
wenzelm@6689
   329
end;
wenzelm@5828
   330
wenzelm@5828
   331
wenzelm@5828
   332
(* datatype transition *)
wenzelm@5828
   333
wenzelm@5828
   334
datatype transition = Transition of
wenzelm@26621
   335
 {name: string,              (*command name*)
wenzelm@26621
   336
  pos: Position.T,           (*source position*)
wenzelm@26621
   337
  int_only: bool,            (*interactive-only*)
wenzelm@26621
   338
  print: bool,               (*print result state*)
wenzelm@26621
   339
  no_timing: bool,           (*suppress timing*)
wenzelm@26621
   340
  trans: trans list};        (*primitive transitions (union)*)
wenzelm@5828
   341
wenzelm@26621
   342
fun make_transition (name, pos, int_only, print, no_timing, trans) =
wenzelm@26621
   343
  Transition {name = name, pos = pos, int_only = int_only, print = print, no_timing = no_timing,
wenzelm@26621
   344
    trans = trans};
wenzelm@5828
   345
wenzelm@26621
   346
fun map_transition f (Transition {name, pos, int_only, print, no_timing, trans}) =
wenzelm@26621
   347
  make_transition (f (name, pos, int_only, print, no_timing, trans));
wenzelm@5828
   348
wenzelm@27441
   349
val empty = make_transition ("", Position.none, false, false, false, []);
wenzelm@5828
   350
wenzelm@5828
   351
wenzelm@5828
   352
(* diagnostics *)
wenzelm@5828
   353
wenzelm@37953
   354
fun init_of (Transition {trans = [Init (name, _)], ...}) = SOME name
wenzelm@27441
   355
  | init_of _ = NONE;
wenzelm@27441
   356
wenzelm@27427
   357
fun name_of (Transition {name, ...}) = name;
wenzelm@28105
   358
fun pos_of (Transition {pos, ...}) = pos;
wenzelm@28105
   359
fun str_of tr = quote (name_of tr) ^ Position.str_of (pos_of tr);
wenzelm@5828
   360
wenzelm@27427
   361
fun command_msg msg tr = msg ^ "command " ^ str_of tr;
wenzelm@5828
   362
fun at_command tr = command_msg "At " tr ^ ".";
wenzelm@5828
   363
wenzelm@5828
   364
fun type_error tr state =
wenzelm@18685
   365
  ERROR (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
wenzelm@5828
   366
wenzelm@5828
   367
wenzelm@5828
   368
(* modify transitions *)
wenzelm@5828
   369
wenzelm@28451
   370
fun name name = map_transition (fn (_, pos, int_only, print, no_timing, trans) =>
wenzelm@28451
   371
  (name, pos, int_only, print, no_timing, trans));
wenzelm@9010
   372
wenzelm@26621
   373
fun position pos = map_transition (fn (name, _, int_only, print, no_timing, trans) =>
wenzelm@26621
   374
  (name, pos, int_only, print, no_timing, trans));
wenzelm@5828
   375
wenzelm@26621
   376
fun interactive int_only = map_transition (fn (name, pos, _, print, no_timing, trans) =>
wenzelm@26621
   377
  (name, pos, int_only, print, no_timing, trans));
wenzelm@14923
   378
wenzelm@26621
   379
val no_timing = map_transition (fn (name, pos, int_only, print, _, trans) =>
wenzelm@26621
   380
  (name, pos, int_only, print, true, trans));
wenzelm@17363
   381
wenzelm@26621
   382
fun add_trans tr = map_transition (fn (name, pos, int_only, print, no_timing, trans) =>
wenzelm@28451
   383
  (name, pos, int_only, print, no_timing, tr :: trans));
wenzelm@16607
   384
wenzelm@28433
   385
val reset_trans = map_transition (fn (name, pos, int_only, print, no_timing, _) =>
wenzelm@28433
   386
  (name, pos, int_only, print, no_timing, []));
wenzelm@28433
   387
wenzelm@28453
   388
fun set_print print = map_transition (fn (name, pos, int_only, _, no_timing, trans) =>
wenzelm@28453
   389
  (name, pos, int_only, print, no_timing, trans));
wenzelm@28453
   390
wenzelm@28453
   391
val print = set_print true;
wenzelm@5828
   392
wenzelm@5828
   393
wenzelm@21007
   394
(* basic transitions *)
wenzelm@5828
   395
wenzelm@37953
   396
fun init_theory name f = add_trans (Init (name, f));
wenzelm@37977
   397
wenzelm@37977
   398
fun modify_init f tr =
wenzelm@37977
   399
  (case init_of tr of
wenzelm@37977
   400
    SOME name => init_theory name f (reset_trans tr)
wenzelm@37977
   401
  | NONE => tr);
wenzelm@37977
   402
wenzelm@6689
   403
val exit = add_trans Exit;
wenzelm@7612
   404
val keep' = add_trans o Keep;
wenzelm@30366
   405
wenzelm@30366
   406
fun present_transaction f g = add_trans (Transaction (f, g));
wenzelm@30366
   407
fun transaction f = present_transaction f (K ());
wenzelm@5828
   408
wenzelm@7612
   409
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   410
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   411
wenzelm@27840
   412
fun ignored pos = empty |> name "<ignored>" |> position pos |> imperative I;
wenzelm@27840
   413
fun malformed pos msg =
wenzelm@27840
   414
  empty |> name "<malformed>" |> position pos |> imperative (fn () => error msg);
wenzelm@27840
   415
wenzelm@21007
   416
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@21007
   417
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@21007
   418
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@15668
   419
wenzelm@21007
   420
wenzelm@21007
   421
(* theory transitions *)
wenzelm@15668
   422
wenzelm@27601
   423
fun generic_theory f = transaction (fn _ =>
wenzelm@26491
   424
  (fn Theory (gthy, _) => Theory (f gthy, NONE)
wenzelm@26491
   425
    | _ => raise UNDEF));
wenzelm@26491
   426
wenzelm@27601
   427
fun theory' f = transaction (fn int =>
wenzelm@33725
   428
  (fn Theory (Context.Theory thy, _) =>
wenzelm@33725
   429
      let val thy' = thy
wenzelm@33725
   430
        |> Sign.new_group
wenzelm@33725
   431
        |> Theory.checkpoint
wenzelm@33725
   432
        |> f int
wenzelm@33725
   433
        |> Sign.reset_group;
wenzelm@33725
   434
      in Theory (Context.Theory thy', NONE) end
wenzelm@20963
   435
    | _ => raise UNDEF));
wenzelm@20963
   436
wenzelm@20963
   437
fun theory f = theory' (K f);
wenzelm@20963
   438
wenzelm@27601
   439
fun begin_local_theory begin f = transaction (fn _ =>
wenzelm@20963
   440
  (fn Theory (Context.Theory thy, _) =>
wenzelm@20963
   441
        let
wenzelm@20985
   442
          val lthy = f thy;
wenzelm@21294
   443
          val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
wenzelm@21294
   444
        in Theory (gthy, SOME lthy) end
wenzelm@20963
   445
    | _ => raise UNDEF));
wenzelm@17076
   446
wenzelm@27601
   447
val end_local_theory = transaction (fn _ =>
wenzelm@21294
   448
  (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
wenzelm@21007
   449
    | _ => raise UNDEF));
wenzelm@21007
   450
wenzelm@21007
   451
local
wenzelm@21007
   452
wenzelm@30366
   453
fun local_theory_presentation loc f = present_transaction (fn int =>
wenzelm@21294
   454
  (fn Theory (gthy, _) =>
wenzelm@21294
   455
        let
wenzelm@21294
   456
          val finish = loc_finish loc gthy;
wenzelm@33725
   457
          val lthy' = loc_begin loc gthy
wenzelm@33725
   458
            |> Local_Theory.new_group
wenzelm@33725
   459
            |> f int
wenzelm@33725
   460
            |> Local_Theory.reset_group;
wenzelm@21294
   461
        in Theory (finish lthy', SOME lthy') end
wenzelm@30366
   462
    | _ => raise UNDEF));
wenzelm@15668
   463
wenzelm@21007
   464
in
wenzelm@21007
   465
wenzelm@30366
   466
fun local_theory' loc f = local_theory_presentation loc f (K ());
wenzelm@29380
   467
fun local_theory loc f = local_theory' loc (K f);
wenzelm@30366
   468
fun present_local_theory loc = local_theory_presentation loc (K I);
wenzelm@18955
   469
wenzelm@21007
   470
end;
wenzelm@21007
   471
wenzelm@21007
   472
wenzelm@21007
   473
(* proof transitions *)
wenzelm@21007
   474
wenzelm@27601
   475
fun end_proof f = transaction (fn int =>
wenzelm@24795
   476
  (fn Proof (prf, (finish, _)) =>
wenzelm@33390
   477
        let val state = Proof_Node.current prf in
wenzelm@21007
   478
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   479
            let
wenzelm@21007
   480
              val ctxt' = f int state;
wenzelm@21007
   481
              val gthy' = finish ctxt';
wenzelm@21007
   482
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   483
          else raise UNDEF
wenzelm@21007
   484
        end
wenzelm@27564
   485
    | SkipProof (0, (gthy, _)) => Theory (gthy, NONE)
wenzelm@21007
   486
    | _ => raise UNDEF));
wenzelm@21007
   487
wenzelm@21294
   488
local
wenzelm@21294
   489
wenzelm@27601
   490
fun begin_proof init finish = transaction (fn int =>
wenzelm@21294
   491
  (fn Theory (gthy, _) =>
wenzelm@21294
   492
    let
berghofe@24453
   493
      val prf = init int gthy;
wenzelm@28433
   494
      val skip = ! skip_proofs;
wenzelm@21294
   495
      val schematic = Proof.schematic_goal prf;
wenzelm@21294
   496
    in
wenzelm@28433
   497
      if skip andalso schematic then
wenzelm@21294
   498
        warning "Cannot skip proof of schematic goal statement"
wenzelm@21294
   499
      else ();
wenzelm@28433
   500
      if skip andalso not schematic then
wenzelm@27564
   501
        SkipProof (0, (finish gthy (Proof.global_skip_proof int prf), gthy))
wenzelm@33390
   502
      else Proof (Proof_Node.init prf, (finish gthy, gthy))
wenzelm@21294
   503
    end
wenzelm@21294
   504
  | _ => raise UNDEF));
wenzelm@21294
   505
wenzelm@21294
   506
in
wenzelm@21294
   507
wenzelm@24780
   508
fun local_theory_to_proof' loc f = begin_proof
wenzelm@33725
   509
  (fn int => fn gthy => f int (Local_Theory.new_group (loc_begin loc gthy)))
wenzelm@33725
   510
  (fn gthy => loc_finish loc gthy o Local_Theory.reset_group);
wenzelm@24780
   511
berghofe@24453
   512
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   513
wenzelm@21294
   514
fun theory_to_proof f = begin_proof
wenzelm@33725
   515
  (K (fn Context.Theory thy => f (Theory.checkpoint (Sign.new_group thy)) | _ => raise UNDEF))
wenzelm@33725
   516
  (K (Context.Theory o Sign.reset_group o ProofContext.theory_of));
wenzelm@21294
   517
wenzelm@21294
   518
end;
wenzelm@21294
   519
wenzelm@27601
   520
val forget_proof = transaction (fn _ =>
wenzelm@21007
   521
  (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   522
    | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   523
    | _ => raise UNDEF));
wenzelm@21007
   524
wenzelm@30366
   525
val present_proof = present_transaction (fn _ =>
wenzelm@33390
   526
  (fn Proof (prf, x) => Proof (Proof_Node.apply I prf, x)
wenzelm@27564
   527
    | skip as SkipProof _ => skip
wenzelm@30366
   528
    | _ => raise UNDEF));
wenzelm@21177
   529
wenzelm@27601
   530
fun proofs' f = transaction (fn int =>
wenzelm@33390
   531
  (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
wenzelm@27564
   532
    | skip as SkipProof _ => skip
wenzelm@16815
   533
    | _ => raise UNDEF));
wenzelm@15668
   534
wenzelm@17904
   535
fun proof' f = proofs' (Seq.single oo f);
wenzelm@17904
   536
val proofs = proofs' o K;
wenzelm@6689
   537
val proof = proof' o K;
wenzelm@16815
   538
wenzelm@27601
   539
fun actual_proof f = transaction (fn _ =>
wenzelm@21007
   540
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   541
    | _ => raise UNDEF));
wenzelm@16815
   542
wenzelm@27601
   543
fun skip_proof f = transaction (fn _ =>
wenzelm@21007
   544
  (fn SkipProof (h, x) => SkipProof (f h, x)
wenzelm@18563
   545
    | _ => raise UNDEF));
wenzelm@18563
   546
wenzelm@27601
   547
fun skip_proof_to_theory pred = transaction (fn _ =>
wenzelm@27564
   548
  (fn SkipProof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
wenzelm@33725
   549
    | _ => raise UNDEF));
wenzelm@5828
   550
wenzelm@5828
   551
wenzelm@5828
   552
wenzelm@5828
   553
(** toplevel transactions **)
wenzelm@5828
   554
wenzelm@27427
   555
(* identification *)
wenzelm@27427
   556
wenzelm@27427
   557
fun get_id (Transition {pos, ...}) = Position.get_id pos;
wenzelm@27427
   558
fun put_id id (tr as Transition {pos, ...}) = position (Position.put_id id pos) tr;
wenzelm@27427
   559
wenzelm@27427
   560
wenzelm@25960
   561
(* thread position *)
wenzelm@25799
   562
wenzelm@25960
   563
fun setmp_thread_position (Transition {pos, ...}) f x =
wenzelm@25819
   564
  Position.setmp_thread_data pos f x;
wenzelm@25799
   565
wenzelm@27606
   566
fun status tr m =
wenzelm@27606
   567
  setmp_thread_position tr (fn () => Output.status (Markup.markup m "")) ();
wenzelm@27606
   568
wenzelm@37688
   569
fun async_state (tr as Transition {print, ...}) st =
wenzelm@37688
   570
  if print then
wenzelm@37711
   571
    ignore
wenzelm@37865
   572
      (Future.fork (fn () =>
wenzelm@38236
   573
          setmp_thread_position tr (fn () => Future.status (fn () => print_state false st)) ()))
wenzelm@37688
   574
  else ();
wenzelm@37687
   575
wenzelm@26602
   576
fun error_msg tr exn_info =
wenzelm@31476
   577
  setmp_thread_position tr (fn () =>
wenzelm@31476
   578
    Output.error_msg (ML_Compiler.exn_message (EXCURSION_FAIL exn_info))) ();
wenzelm@26602
   579
wenzelm@25799
   580
wenzelm@28095
   581
(* post-transition hooks *)
wenzelm@28095
   582
wenzelm@37905
   583
local
wenzelm@37905
   584
  val hooks = Unsynchronized.ref ([]: (transition -> state -> state -> unit) list);
wenzelm@37905
   585
in
wenzelm@28095
   586
wenzelm@32738
   587
fun add_hook f = CRITICAL (fn () => Unsynchronized.change hooks (cons f));
wenzelm@33223
   588
fun get_hooks () = ! hooks;
wenzelm@28095
   589
wenzelm@28095
   590
end;
wenzelm@28095
   591
wenzelm@28095
   592
wenzelm@5828
   593
(* apply transitions *)
wenzelm@5828
   594
wenzelm@6664
   595
local
wenzelm@6664
   596
wenzelm@32792
   597
fun app int (tr as Transition {trans, print, no_timing, ...}) =
wenzelm@25819
   598
  setmp_thread_position tr (fn state =>
wenzelm@25799
   599
    let
wenzelm@25799
   600
      fun do_timing f x = (warning (command_msg "" tr); timeap f x);
wenzelm@25799
   601
      fun do_profiling f x = profile (! profiling) f x;
wenzelm@25799
   602
wenzelm@26256
   603
      val (result, status) =
wenzelm@37905
   604
         state |>
wenzelm@37905
   605
          (apply_trans int trans
wenzelm@37905
   606
            |> (! profiling > 0 andalso not no_timing) ? do_profiling
wenzelm@37905
   607
            |> (! profiling > 0 orelse ! timing andalso not no_timing) ? do_timing);
wenzelm@26256
   608
wenzelm@26621
   609
      val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
wenzelm@26256
   610
    in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);
wenzelm@6664
   611
wenzelm@6664
   612
in
wenzelm@5828
   613
wenzelm@26602
   614
fun transition int tr st =
wenzelm@28095
   615
  let
wenzelm@28095
   616
    val hooks = get_hooks ();
wenzelm@28103
   617
    fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
wenzelm@28095
   618
wenzelm@28095
   619
    val ctxt = try context_of st;
wenzelm@28095
   620
    val res =
wenzelm@28095
   621
      (case app int tr st of
wenzelm@28095
   622
        (_, SOME TERMINATE) => NONE
wenzelm@28103
   623
      | (st', SOME (EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
wenzelm@31476
   624
      | (st', SOME exn) => SOME (st', SOME (Runtime.exn_context ctxt exn, at_command tr))
wenzelm@28103
   625
      | (st', NONE) => SOME (st', NONE));
wenzelm@28103
   626
    val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
wenzelm@28095
   627
  in res end;
wenzelm@6664
   628
wenzelm@6664
   629
end;
wenzelm@5828
   630
wenzelm@5828
   631
wenzelm@37687
   632
(* managed execution *)
wenzelm@37687
   633
wenzelm@37906
   634
fun run_command thy_name tr st =
wenzelm@37687
   635
  (case
wenzelm@37687
   636
      (case init_of tr of
wenzelm@37950
   637
        SOME name => Exn.capture (fn () => Thy_Header.consistent_name thy_name name) ()
wenzelm@37687
   638
      | NONE => Exn.Result ()) of
wenzelm@37687
   639
    Exn.Result () =>
wenzelm@37859
   640
      let val int = is_some (init_of tr) in
wenzelm@37859
   641
        (case transition int tr st of
wenzelm@37859
   642
          SOME (st', NONE) =>
wenzelm@37859
   643
            (status tr Markup.finished; if int then () else async_state tr st'; SOME st')
wenzelm@37859
   644
        | SOME (_, SOME (exn as Exn.Interrupt, _)) => reraise exn
wenzelm@37859
   645
        | SOME (_, SOME exn_info) => (error_msg tr exn_info; status tr Markup.failed; NONE)
wenzelm@37859
   646
        | NONE => (error_msg tr (TERMINATE, at_command tr); status tr Markup.failed; NONE))
wenzelm@37859
   647
      end
wenzelm@37687
   648
  | Exn.Exn exn => (error_msg tr (exn, at_command tr); status tr Markup.failed; NONE));
wenzelm@37687
   649
wenzelm@37687
   650
wenzelm@28425
   651
(* nested commands *)
wenzelm@5828
   652
wenzelm@28425
   653
fun command tr st =
wenzelm@27576
   654
  (case transition (! interact) tr st of
wenzelm@28425
   655
    SOME (st', NONE) => st'
wenzelm@28425
   656
  | SOME (_, SOME exn_info) => raise EXCURSION_FAIL exn_info
wenzelm@27576
   657
  | NONE => raise EXCURSION_FAIL (TERMINATE, at_command tr));
wenzelm@27576
   658
wenzelm@29483
   659
fun command_result tr st =
wenzelm@29483
   660
  let val st' = command tr st
wenzelm@29483
   661
  in (st', st') end;
wenzelm@29483
   662
wenzelm@28433
   663
wenzelm@28433
   664
(* excursion of units, consisting of commands with proof *)
wenzelm@28433
   665
wenzelm@33519
   666
structure States = Proof_Data
wenzelm@28974
   667
(
wenzelm@28974
   668
  type T = state list future option;
wenzelm@28974
   669
  fun init _ = NONE;
wenzelm@28974
   670
);
wenzelm@28974
   671
wenzelm@28974
   672
fun proof_result immediate (tr, proof_trs) st =
wenzelm@28433
   673
  let val st' = command tr st in
wenzelm@36315
   674
    if immediate orelse
wenzelm@36315
   675
      null proof_trs orelse
wenzelm@36950
   676
      Keyword.is_schematic_goal (name_of tr) orelse
wenzelm@36950
   677
      exists (Keyword.is_qed_global o name_of) proof_trs orelse
wenzelm@36315
   678
      not (can proof_of st') orelse
wenzelm@36315
   679
      Proof.is_relevant (proof_of st')
wenzelm@28433
   680
    then
wenzelm@28453
   681
      let val (states, st'') = fold_map command_result proof_trs st'
wenzelm@28974
   682
      in (Lazy.value ((tr, st') :: (proof_trs ~~ states)), st'') end
wenzelm@28453
   683
    else
wenzelm@28433
   684
      let
wenzelm@28433
   685
        val (body_trs, end_tr) = split_last proof_trs;
wenzelm@28453
   686
        val finish = Context.Theory o ProofContext.theory_of;
wenzelm@28974
   687
wenzelm@29386
   688
        val future_proof = Proof.global_future_proof
wenzelm@28974
   689
          (fn prf =>
wenzelm@32062
   690
            Future.fork_pri ~1 (fn () =>
wenzelm@30366
   691
              let val (states, result_state) =
wenzelm@37953
   692
                (case st' of State (SOME (Proof (_, (_, orig_gthy))), prev)
wenzelm@37953
   693
                  => State (SOME (Proof (Proof_Node.init prf, (finish, orig_gthy))), prev))
wenzelm@28974
   694
                |> fold_map command_result body_trs
wenzelm@28974
   695
                ||> command (end_tr |> set_print false);
wenzelm@30398
   696
              in (states, presentation_context_of result_state) end))
wenzelm@28974
   697
          #> (fn (states, ctxt) => States.put (SOME states) ctxt);
wenzelm@28974
   698
wenzelm@28974
   699
        val st'' = st' |> command (end_tr |> reset_trans |> end_proof (K future_proof));
wenzelm@28974
   700
wenzelm@28974
   701
        val states =
wenzelm@30398
   702
          (case States.get (presentation_context_of st'') of
wenzelm@37852
   703
            NONE => raise Fail ("No future states for " ^ name_of tr ^ Position.str_of (pos_of tr))
wenzelm@28974
   704
          | SOME states => states);
wenzelm@28974
   705
        val result = Lazy.lazy
wenzelm@28974
   706
          (fn () => (tr, st') :: (body_trs ~~ Future.join states) @ [(end_tr, st'')]);
wenzelm@28974
   707
wenzelm@28974
   708
      in (result, st'') end
wenzelm@28433
   709
  end;
wenzelm@28433
   710
wenzelm@29068
   711
fun excursion input =
wenzelm@28425
   712
  let
wenzelm@28433
   713
    val end_pos = if null input then error "No input" else pos_of (fst (List.last input));
wenzelm@29448
   714
    val immediate = not (Goal.future_enabled ());
wenzelm@29427
   715
    val (results, end_state) = fold_map (proof_result immediate) input toplevel;
wenzelm@37953
   716
    val thy = end_theory end_pos end_state;
wenzelm@37953
   717
  in (Lazy.lazy (fn () => maps Lazy.force results), thy) end;
wenzelm@7062
   718
wenzelm@6664
   719
end;