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