src/Pure/Isar/toplevel.ML
author wenzelm
Tue Nov 10 23:18:03 2009 +0100 (2009-11-10 ago)
changeset 33604 d4220df6fde2
parent 33553 35f2b30593a8
child 33671 4b0f2599ed48
permissions -rw-r--r--
Toplevel.thread provides Isar-style exception output;
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@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@27576
    48
  val init_theory: string -> (bool -> theory) -> (theory -> unit) -> transition -> transition
wenzelm@6689
    49
  val exit: transition -> transition
wenzelm@5828
    50
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    51
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@5828
    52
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@27840
    53
  val ignored: Position.T -> transition
wenzelm@27840
    54
  val malformed: Position.T -> string -> transition
wenzelm@5828
    55
  val theory: (theory -> theory) -> transition -> transition
wenzelm@26491
    56
  val generic_theory: (generic_theory -> generic_theory) -> transition -> transition
wenzelm@7612
    57
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@20985
    58
  val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
wenzelm@21007
    59
  val end_local_theory: transition -> transition
wenzelm@29380
    60
  val local_theory': xstring option -> (bool -> local_theory -> local_theory) ->
wenzelm@29380
    61
    transition -> transition
wenzelm@20963
    62
  val local_theory: xstring option -> (local_theory -> local_theory) -> transition -> transition
wenzelm@30366
    63
  val present_local_theory: xstring option -> (state -> unit) -> transition -> transition
berghofe@24453
    64
  val local_theory_to_proof': xstring option -> (bool -> local_theory -> Proof.state) ->
berghofe@24453
    65
    transition -> transition
wenzelm@21007
    66
  val local_theory_to_proof: xstring option -> (local_theory -> Proof.state) ->
wenzelm@21007
    67
    transition -> transition
wenzelm@17363
    68
  val theory_to_proof: (theory -> Proof.state) -> transition -> transition
wenzelm@21007
    69
  val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition
wenzelm@21007
    70
  val forget_proof: transition -> transition
wenzelm@30366
    71
  val present_proof: (state -> unit) -> transition -> transition
wenzelm@21177
    72
  val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@17904
    73
  val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
wenzelm@21177
    74
  val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@21177
    75
  val proof: (Proof.state -> Proof.state) -> transition -> transition
wenzelm@33390
    76
  val actual_proof: (Proof_Node.T -> Proof_Node.T) -> transition -> transition
wenzelm@27564
    77
  val skip_proof: (int -> int) -> transition -> transition
wenzelm@17904
    78
  val skip_proof_to_theory: (int -> bool) -> transition -> transition
wenzelm@27427
    79
  val get_id: transition -> string option
wenzelm@27427
    80
  val put_id: string -> transition -> transition
wenzelm@9512
    81
  val unknown_theory: transition -> transition
wenzelm@9512
    82
  val unknown_proof: transition -> transition
wenzelm@9512
    83
  val unknown_context: transition -> transition
wenzelm@28425
    84
  val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b
wenzelm@27606
    85
  val status: transition -> Markup.T -> unit
wenzelm@26602
    86
  val error_msg: transition -> exn * string -> unit
wenzelm@28103
    87
  val add_hook: (transition -> state -> state -> unit) -> unit
wenzelm@26602
    88
  val transition: bool -> transition -> state -> (state * (exn * string) option) option
wenzelm@28425
    89
  val commit_exit: Position.T -> transition
wenzelm@28425
    90
  val command: transition -> state -> state
wenzelm@29516
    91
  val run_command: string -> transition -> state -> state option
wenzelm@29427
    92
  val excursion: (transition * transition list) list -> (transition * state) list lazy
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
wenzelm@20963
   105
type generic_theory = Context.generic;    (*theory or local_theory*)
wenzelm@20963
   106
wenzelm@33553
   107
val loc_init = Theory_Target.context;
wenzelm@28394
   108
val loc_exit = LocalTheory.exit_global;
wenzelm@21294
   109
wenzelm@25292
   110
fun loc_begin loc (Context.Theory thy) = loc_init (the_default "-" loc) thy
wenzelm@21294
   111
  | loc_begin NONE (Context.Proof lthy) = lthy
haftmann@25269
   112
  | loc_begin (SOME loc) (Context.Proof lthy) = loc_init loc (loc_exit lthy);
wenzelm@21294
   113
wenzelm@21294
   114
fun loc_finish _ (Context.Theory _) = Context.Theory o loc_exit
wenzelm@21294
   115
  | loc_finish NONE (Context.Proof _) = Context.Proof o LocalTheory.restore
wenzelm@25292
   116
  | loc_finish (SOME _) (Context.Proof lthy) = fn lthy' =>
wenzelm@25292
   117
      Context.Proof (LocalTheory.reinit (LocalTheory.raw_theory (K (loc_exit lthy')) lthy));
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@27601
   142
type node_info = node * (theory -> unit);  (*node with exit operation*)
wenzelm@27601
   143
datatype state = State of node_info option * node_info option;  (*current, previous*)
wenzelm@5828
   144
wenzelm@27576
   145
val toplevel = State (NONE, NONE);
wenzelm@5828
   146
wenzelm@27576
   147
fun is_toplevel (State (NONE, _)) = true
wenzelm@7732
   148
  | is_toplevel _ = false;
wenzelm@7732
   149
wenzelm@27576
   150
fun level (State (NONE, _)) = 0
wenzelm@27576
   151
  | level (State (SOME (Theory _, _), _)) = 0
wenzelm@33390
   152
  | level (State (SOME (Proof (prf, _), _), _)) = Proof.level (Proof_Node.current prf)
wenzelm@27576
   153
  | level (State (SOME (SkipProof (d, _), _), _)) = d + 1;   (*different notion of proof depth!*)
wenzelm@17076
   154
wenzelm@27576
   155
fun str_of_state (State (NONE, _)) = "at top level"
wenzelm@27576
   156
  | str_of_state (State (SOME (Theory (Context.Theory _, _), _), _)) = "in theory mode"
wenzelm@27576
   157
  | str_of_state (State (SOME (Theory (Context.Proof _, _), _), _)) = "in local theory mode"
wenzelm@27576
   158
  | str_of_state (State (SOME (Proof _, _), _)) = "in proof mode"
wenzelm@27576
   159
  | str_of_state (State (SOME (SkipProof _, _), _)) = "in skipped proof mode";
wenzelm@5946
   160
wenzelm@5946
   161
wenzelm@27576
   162
(* current node *)
wenzelm@5828
   163
wenzelm@27576
   164
fun node_of (State (NONE, _)) = raise UNDEF
wenzelm@27576
   165
  | node_of (State (SOME (node, _), _)) = node;
wenzelm@5828
   166
wenzelm@18589
   167
fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
wenzelm@18589
   168
fun is_proof state = not (is_toplevel state) andalso is_some (proof_node (node_of state));
wenzelm@18589
   169
wenzelm@19063
   170
fun node_case f g state = cases_node f g (node_of state);
wenzelm@5828
   171
wenzelm@30398
   172
fun presentation_context_of state =
wenzelm@30398
   173
  (case try node_of state of
wenzelm@30398
   174
    SOME (Theory (_, SOME ctxt)) => ctxt
wenzelm@30398
   175
  | SOME node => context_node node
wenzelm@30398
   176
  | NONE => raise UNDEF);
wenzelm@30366
   177
wenzelm@30801
   178
fun previous_context_of (State (_, NONE)) = NONE
wenzelm@30801
   179
  | previous_context_of (State (_, SOME (prev, _))) = SOME (context_node prev);
wenzelm@30801
   180
wenzelm@21506
   181
val context_of = node_case Context.proof_of Proof.context_of;
wenzelm@22089
   182
val generic_theory_of = node_case I (Context.Proof o Proof.context_of);
wenzelm@20963
   183
val theory_of = node_case Context.theory_of Proof.theory_of;
wenzelm@18589
   184
val proof_of = node_case (fn _ => raise UNDEF) I;
wenzelm@17208
   185
wenzelm@18589
   186
fun proof_position_of state =
wenzelm@18589
   187
  (case node_of state of
wenzelm@33390
   188
    Proof (prf, _) => Proof_Node.position prf
wenzelm@18589
   189
  | _ => raise UNDEF);
wenzelm@6664
   190
wenzelm@21007
   191
val enter_proof_body = node_case (Proof.init o Context.proof_of) Proof.enter_forward;
wenzelm@5828
   192
wenzelm@5828
   193
wenzelm@16815
   194
(* print state *)
wenzelm@16815
   195
wenzelm@33553
   196
val pretty_context = LocalTheory.pretty o Context.cases (Theory_Target.init NONE) 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@16815
   215
wenzelm@15668
   216
wenzelm@5828
   217
(** toplevel transitions **)
wenzelm@5828
   218
wenzelm@32738
   219
val quiet = Unsynchronized.ref false;
wenzelm@22135
   220
val debug = Output.debugging;
wenzelm@32738
   221
val interact = Unsynchronized.ref false;
wenzelm@16682
   222
val timing = Output.timing;
wenzelm@32738
   223
val profiling = Unsynchronized.ref 0;
wenzelm@32738
   224
val skip_proofs = Unsynchronized.ref false;
wenzelm@16682
   225
wenzelm@31476
   226
exception TERMINATE = Runtime.TERMINATE;
wenzelm@31476
   227
exception EXCURSION_FAIL = Runtime.EXCURSION_FAIL;
wenzelm@31476
   228
exception TOPLEVEL_ERROR = Runtime.TOPLEVEL_ERROR;
wenzelm@20128
   229
wenzelm@33604
   230
fun program body =
wenzelm@33604
   231
 (body
wenzelm@31476
   232
  |> Runtime.controlled_execution
wenzelm@33604
   233
  |> Runtime.toplevel_error (Output.error_msg o ML_Compiler.exn_message)) ();
wenzelm@33604
   234
wenzelm@33604
   235
fun thread interrupts body =
wenzelm@33604
   236
  Thread.fork
wenzelm@33604
   237
    (((fn () => body () handle Exn.Interrupt => ())
wenzelm@33604
   238
        |> Runtime.debugging
wenzelm@33604
   239
        |> Runtime.toplevel_error
wenzelm@33604
   240
          (fn exn => priority ("## INTERNAL ERROR ##\n" ^ ML_Compiler.exn_message exn))),
wenzelm@33604
   241
      SimpleThread.attributes interrupts);
wenzelm@20128
   242
wenzelm@5828
   243
wenzelm@27601
   244
(* node transactions -- maintaining stable checkpoints *)
wenzelm@7022
   245
wenzelm@31476
   246
exception FAILURE of state * exn;
wenzelm@31476
   247
wenzelm@6689
   248
local
wenzelm@6689
   249
wenzelm@30366
   250
fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
wenzelm@30366
   251
  | reset_presentation node = node;
wenzelm@30366
   252
wenzelm@26624
   253
fun is_draft_theory (Theory (gthy, _)) = Context.is_draft (Context.theory_of gthy)
wenzelm@26624
   254
  | is_draft_theory _ = false;
wenzelm@26624
   255
wenzelm@31476
   256
fun is_stale state = Context.is_stale (theory_of state) handle Runtime.UNDEF => false;
wenzelm@27601
   257
wenzelm@26624
   258
fun stale_error NONE = SOME (ERROR "Stale theory encountered after successful execution!")
wenzelm@26624
   259
  | stale_error some = some;
wenzelm@16815
   260
wenzelm@27576
   261
fun map_theory f (Theory (gthy, ctxt)) =
wenzelm@27576
   262
      Theory (Context.mapping f (LocalTheory.raw_theory f) gthy, ctxt)
wenzelm@27576
   263
  | map_theory _ node = node;
wenzelm@6689
   264
wenzelm@6689
   265
in
wenzelm@6689
   266
wenzelm@32792
   267
fun apply_transaction f g (node, exit) =
wenzelm@20128
   268
  let
wenzelm@27576
   269
    val _ = is_draft_theory node andalso error "Illegal draft theory in toplevel state";
wenzelm@27576
   270
    val cont_node = reset_presentation node;
wenzelm@27576
   271
    val back_node = map_theory (Theory.checkpoint o Theory.copy) cont_node;
wenzelm@27601
   272
    fun state_error e nd = (State (SOME (nd, exit), SOME (node, exit)), e);
wenzelm@6689
   273
wenzelm@20128
   274
    val (result, err) =
wenzelm@20128
   275
      cont_node
wenzelm@31476
   276
      |> Runtime.controlled_execution f
wenzelm@26624
   277
      |> map_theory Theory.checkpoint
wenzelm@26624
   278
      |> state_error NONE
wenzelm@26624
   279
      handle exn => state_error (SOME exn) cont_node;
wenzelm@26624
   280
wenzelm@26624
   281
    val (result', err') =
wenzelm@26624
   282
      if is_stale result then state_error (stale_error err) back_node
wenzelm@26624
   283
      else (result, err);
wenzelm@20128
   284
  in
wenzelm@26624
   285
    (case err' of
wenzelm@30366
   286
      NONE => tap g result'
wenzelm@26624
   287
    | SOME exn => raise FAILURE (result', exn))
wenzelm@20128
   288
  end;
wenzelm@6689
   289
wenzelm@6689
   290
end;
wenzelm@6689
   291
wenzelm@6689
   292
wenzelm@6689
   293
(* primitive transitions *)
wenzelm@6689
   294
wenzelm@5828
   295
datatype trans =
wenzelm@27576
   296
  Init of string * (bool -> theory) * (theory -> unit) | (*theory name, init, exit*)
wenzelm@27576
   297
  Exit |                                         (*formal exit of theory -- without committing*)
wenzelm@27576
   298
  CommitExit |                                   (*exit and commit final theory*)
wenzelm@27576
   299
  Keep of bool -> state -> unit |                (*peek at state*)
wenzelm@30366
   300
  Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
wenzelm@21958
   301
wenzelm@6689
   302
local
wenzelm@5828
   303
wenzelm@32792
   304
fun apply_tr int (Init (_, f, exit)) (State (NONE, _)) =
wenzelm@27576
   305
      State (SOME (Theory (Context.Theory (Theory.checkpoint (f int)), NONE), exit), NONE)
wenzelm@32792
   306
  | apply_tr _ Exit (State (prev as SOME (Theory (Context.Theory _, _), _), _)) =
wenzelm@27603
   307
      State (NONE, prev)
wenzelm@32792
   308
  | apply_tr _ CommitExit (State (NONE, SOME (Theory (Context.Theory thy, _), exit))) =
wenzelm@31476
   309
      (Runtime.controlled_execution exit thy; toplevel)
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@27441
   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@27576
   396
fun init_theory name f exit = add_trans (Init (name, f, exit));
wenzelm@6689
   397
val exit = add_trans Exit;
wenzelm@7612
   398
val keep' = add_trans o Keep;
wenzelm@30366
   399
wenzelm@30366
   400
fun present_transaction f g = add_trans (Transaction (f, g));
wenzelm@30366
   401
fun transaction f = present_transaction f (K ());
wenzelm@5828
   402
wenzelm@7612
   403
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   404
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   405
wenzelm@27840
   406
fun ignored pos = empty |> name "<ignored>" |> position pos |> imperative I;
wenzelm@27840
   407
fun malformed pos msg =
wenzelm@27840
   408
  empty |> name "<malformed>" |> position pos |> imperative (fn () => error msg);
wenzelm@27840
   409
wenzelm@21007
   410
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@21007
   411
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@21007
   412
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@15668
   413
wenzelm@21007
   414
wenzelm@21007
   415
(* theory transitions *)
wenzelm@15668
   416
wenzelm@27601
   417
fun generic_theory f = transaction (fn _ =>
wenzelm@26491
   418
  (fn Theory (gthy, _) => Theory (f gthy, NONE)
wenzelm@26491
   419
    | _ => raise UNDEF));
wenzelm@26491
   420
wenzelm@27601
   421
fun theory' f = transaction (fn int =>
wenzelm@20963
   422
  (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (f int thy), NONE)
wenzelm@20963
   423
    | _ => raise UNDEF));
wenzelm@20963
   424
wenzelm@20963
   425
fun theory f = theory' (K f);
wenzelm@20963
   426
wenzelm@27601
   427
fun begin_local_theory begin f = transaction (fn _ =>
wenzelm@20963
   428
  (fn Theory (Context.Theory thy, _) =>
wenzelm@20963
   429
        let
wenzelm@20985
   430
          val lthy = f thy;
wenzelm@21294
   431
          val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
wenzelm@21294
   432
        in Theory (gthy, SOME lthy) end
wenzelm@20963
   433
    | _ => raise UNDEF));
wenzelm@17076
   434
wenzelm@27601
   435
val end_local_theory = transaction (fn _ =>
wenzelm@21294
   436
  (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
wenzelm@21007
   437
    | _ => raise UNDEF));
wenzelm@21007
   438
wenzelm@21007
   439
local
wenzelm@21007
   440
wenzelm@30366
   441
fun local_theory_presentation loc f = present_transaction (fn int =>
wenzelm@21294
   442
  (fn Theory (gthy, _) =>
wenzelm@21294
   443
        let
wenzelm@21294
   444
          val finish = loc_finish loc gthy;
wenzelm@29380
   445
          val lthy' = f int (loc_begin loc gthy);
wenzelm@21294
   446
        in Theory (finish lthy', SOME lthy') end
wenzelm@30366
   447
    | _ => raise UNDEF));
wenzelm@15668
   448
wenzelm@21007
   449
in
wenzelm@21007
   450
wenzelm@30366
   451
fun local_theory' loc f = local_theory_presentation loc f (K ());
wenzelm@29380
   452
fun local_theory loc f = local_theory' loc (K f);
wenzelm@30366
   453
fun present_local_theory loc = local_theory_presentation loc (K I);
wenzelm@18955
   454
wenzelm@21007
   455
end;
wenzelm@21007
   456
wenzelm@21007
   457
wenzelm@21007
   458
(* proof transitions *)
wenzelm@21007
   459
wenzelm@27601
   460
fun end_proof f = transaction (fn int =>
wenzelm@24795
   461
  (fn Proof (prf, (finish, _)) =>
wenzelm@33390
   462
        let val state = Proof_Node.current prf in
wenzelm@21007
   463
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   464
            let
wenzelm@21007
   465
              val ctxt' = f int state;
wenzelm@21007
   466
              val gthy' = finish ctxt';
wenzelm@21007
   467
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   468
          else raise UNDEF
wenzelm@21007
   469
        end
wenzelm@27564
   470
    | SkipProof (0, (gthy, _)) => Theory (gthy, NONE)
wenzelm@21007
   471
    | _ => raise UNDEF));
wenzelm@21007
   472
wenzelm@21294
   473
local
wenzelm@21294
   474
wenzelm@27601
   475
fun begin_proof init finish = transaction (fn int =>
wenzelm@21294
   476
  (fn Theory (gthy, _) =>
wenzelm@21294
   477
    let
berghofe@24453
   478
      val prf = init int gthy;
wenzelm@28433
   479
      val skip = ! skip_proofs;
wenzelm@21294
   480
      val schematic = Proof.schematic_goal prf;
wenzelm@21294
   481
    in
wenzelm@28433
   482
      if skip andalso schematic then
wenzelm@21294
   483
        warning "Cannot skip proof of schematic goal statement"
wenzelm@21294
   484
      else ();
wenzelm@28433
   485
      if skip andalso not schematic then
wenzelm@27564
   486
        SkipProof (0, (finish gthy (Proof.global_skip_proof int prf), gthy))
wenzelm@33390
   487
      else Proof (Proof_Node.init prf, (finish gthy, gthy))
wenzelm@21294
   488
    end
wenzelm@21294
   489
  | _ => raise UNDEF));
wenzelm@21294
   490
wenzelm@21294
   491
in
wenzelm@21294
   492
wenzelm@24780
   493
fun local_theory_to_proof' loc f = begin_proof
wenzelm@25960
   494
  (fn int => fn gthy => f int (loc_begin loc gthy))
wenzelm@24780
   495
  (loc_finish loc);
wenzelm@24780
   496
berghofe@24453
   497
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   498
wenzelm@21294
   499
fun theory_to_proof f = begin_proof
wenzelm@24780
   500
  (K (fn Context.Theory thy => f thy | _ => raise UNDEF))
wenzelm@24780
   501
  (K (Context.Theory o ProofContext.theory_of));
wenzelm@21294
   502
wenzelm@21294
   503
end;
wenzelm@21294
   504
wenzelm@27601
   505
val forget_proof = transaction (fn _ =>
wenzelm@21007
   506
  (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   507
    | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   508
    | _ => raise UNDEF));
wenzelm@21007
   509
wenzelm@30366
   510
val present_proof = present_transaction (fn _ =>
wenzelm@33390
   511
  (fn Proof (prf, x) => Proof (Proof_Node.apply I prf, x)
wenzelm@27564
   512
    | skip as SkipProof _ => skip
wenzelm@30366
   513
    | _ => raise UNDEF));
wenzelm@21177
   514
wenzelm@27601
   515
fun proofs' f = transaction (fn int =>
wenzelm@33390
   516
  (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
wenzelm@27564
   517
    | skip as SkipProof _ => skip
wenzelm@16815
   518
    | _ => raise UNDEF));
wenzelm@15668
   519
wenzelm@17904
   520
fun proof' f = proofs' (Seq.single oo f);
wenzelm@17904
   521
val proofs = proofs' o K;
wenzelm@6689
   522
val proof = proof' o K;
wenzelm@16815
   523
wenzelm@27601
   524
fun actual_proof f = transaction (fn _ =>
wenzelm@21007
   525
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   526
    | _ => raise UNDEF));
wenzelm@16815
   527
wenzelm@27601
   528
fun skip_proof f = transaction (fn _ =>
wenzelm@21007
   529
  (fn SkipProof (h, x) => SkipProof (f h, x)
wenzelm@18563
   530
    | _ => raise UNDEF));
wenzelm@18563
   531
wenzelm@27601
   532
fun skip_proof_to_theory pred = transaction (fn _ =>
wenzelm@27564
   533
  (fn SkipProof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
wenzelm@17904
   534
  | _ => raise UNDEF));
wenzelm@5828
   535
wenzelm@5828
   536
wenzelm@5828
   537
wenzelm@5828
   538
(** toplevel transactions **)
wenzelm@5828
   539
wenzelm@27427
   540
(* identification *)
wenzelm@27427
   541
wenzelm@27427
   542
fun get_id (Transition {pos, ...}) = Position.get_id pos;
wenzelm@27427
   543
fun put_id id (tr as Transition {pos, ...}) = position (Position.put_id id pos) tr;
wenzelm@27427
   544
wenzelm@27427
   545
wenzelm@25960
   546
(* thread position *)
wenzelm@25799
   547
wenzelm@25960
   548
fun setmp_thread_position (Transition {pos, ...}) f x =
wenzelm@25819
   549
  Position.setmp_thread_data pos f x;
wenzelm@25799
   550
wenzelm@27606
   551
fun status tr m =
wenzelm@27606
   552
  setmp_thread_position tr (fn () => Output.status (Markup.markup m "")) ();
wenzelm@27606
   553
wenzelm@26602
   554
fun error_msg tr exn_info =
wenzelm@31476
   555
  setmp_thread_position tr (fn () =>
wenzelm@31476
   556
    Output.error_msg (ML_Compiler.exn_message (EXCURSION_FAIL exn_info))) ();
wenzelm@26602
   557
wenzelm@25799
   558
wenzelm@28095
   559
(* post-transition hooks *)
wenzelm@28095
   560
wenzelm@32738
   561
local val hooks = Unsynchronized.ref ([]: (transition -> state -> state -> unit) list) in
wenzelm@28095
   562
wenzelm@32738
   563
fun add_hook f = CRITICAL (fn () => Unsynchronized.change hooks (cons f));
wenzelm@33223
   564
fun get_hooks () = ! hooks;
wenzelm@28095
   565
wenzelm@28095
   566
end;
wenzelm@28095
   567
wenzelm@28095
   568
wenzelm@5828
   569
(* apply transitions *)
wenzelm@5828
   570
wenzelm@6664
   571
local
wenzelm@6664
   572
wenzelm@32792
   573
fun app int (tr as Transition {trans, print, no_timing, ...}) =
wenzelm@25819
   574
  setmp_thread_position tr (fn state =>
wenzelm@25799
   575
    let
wenzelm@25799
   576
      fun do_timing f x = (warning (command_msg "" tr); timeap f x);
wenzelm@25799
   577
      fun do_profiling f x = profile (! profiling) f x;
wenzelm@25799
   578
wenzelm@26256
   579
      val (result, status) =
wenzelm@32792
   580
         state |> (apply_trans int trans
wenzelm@25799
   581
          |> (if ! profiling > 0 andalso not no_timing then do_profiling else I)
wenzelm@25799
   582
          |> (if ! profiling > 0 orelse ! timing andalso not no_timing then do_timing else I));
wenzelm@26256
   583
wenzelm@26621
   584
      val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
wenzelm@26256
   585
    in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);
wenzelm@6664
   586
wenzelm@6664
   587
in
wenzelm@5828
   588
wenzelm@26602
   589
fun transition int tr st =
wenzelm@28095
   590
  let
wenzelm@28095
   591
    val hooks = get_hooks ();
wenzelm@28103
   592
    fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
wenzelm@28095
   593
wenzelm@28095
   594
    val ctxt = try context_of st;
wenzelm@28095
   595
    val res =
wenzelm@28095
   596
      (case app int tr st of
wenzelm@28095
   597
        (_, SOME TERMINATE) => NONE
wenzelm@28103
   598
      | (st', SOME (EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
wenzelm@31476
   599
      | (st', SOME exn) => SOME (st', SOME (Runtime.exn_context ctxt exn, at_command tr))
wenzelm@28103
   600
      | (st', NONE) => SOME (st', NONE));
wenzelm@28103
   601
    val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
wenzelm@28095
   602
  in res end;
wenzelm@6664
   603
wenzelm@6664
   604
end;
wenzelm@5828
   605
wenzelm@5828
   606
wenzelm@27601
   607
(* commit final exit *)
wenzelm@27601
   608
wenzelm@28425
   609
fun commit_exit pos =
wenzelm@27601
   610
  name "end" empty
wenzelm@28425
   611
  |> position pos
wenzelm@27601
   612
  |> add_trans CommitExit
wenzelm@27601
   613
  |> imperative (fn () => warning "Expected to find finished theory");
wenzelm@27601
   614
wenzelm@27601
   615
wenzelm@28425
   616
(* nested commands *)
wenzelm@5828
   617
wenzelm@28425
   618
fun command tr st =
wenzelm@27576
   619
  (case transition (! interact) tr st of
wenzelm@28425
   620
    SOME (st', NONE) => st'
wenzelm@28425
   621
  | SOME (_, SOME exn_info) => raise EXCURSION_FAIL exn_info
wenzelm@27576
   622
  | NONE => raise EXCURSION_FAIL (TERMINATE, at_command tr));
wenzelm@27576
   623
wenzelm@29483
   624
fun command_result tr st =
wenzelm@29483
   625
  let val st' = command tr st
wenzelm@29483
   626
  in (st', st') end;
wenzelm@29483
   627
wenzelm@29516
   628
fun run_command thy_name tr st =
wenzelm@29516
   629
  (case
wenzelm@29516
   630
      (case init_of tr of
wenzelm@29516
   631
        SOME name => Exn.capture (fn () => ThyLoad.check_name thy_name name) ()
wenzelm@29516
   632
      | NONE => Exn.Result ()) of
wenzelm@29516
   633
    Exn.Result () =>
wenzelm@29516
   634
      (case transition true tr st of
wenzelm@29516
   635
        SOME (st', NONE) => (status tr Markup.finished; SOME st')
wenzelm@29516
   636
      | SOME (_, SOME exn_info) => (error_msg tr exn_info; status tr Markup.failed; NONE)
wenzelm@29516
   637
      | NONE => (error_msg tr (TERMINATE, at_command tr); status tr Markup.failed; NONE))
wenzelm@29516
   638
  | Exn.Exn exn => (error_msg tr (exn, at_command tr); status tr Markup.failed; NONE));
wenzelm@29483
   639
wenzelm@28433
   640
wenzelm@28433
   641
(* excursion of units, consisting of commands with proof *)
wenzelm@28433
   642
wenzelm@33519
   643
structure States = Proof_Data
wenzelm@28974
   644
(
wenzelm@28974
   645
  type T = state list future option;
wenzelm@28974
   646
  fun init _ = NONE;
wenzelm@28974
   647
);
wenzelm@28974
   648
wenzelm@28974
   649
fun proof_result immediate (tr, proof_trs) st =
wenzelm@28433
   650
  let val st' = command tr st in
wenzelm@28453
   651
    if immediate orelse null proof_trs orelse
wenzelm@28453
   652
      not (can proof_of st') orelse Proof.is_relevant (proof_of st')
wenzelm@28433
   653
    then
wenzelm@28453
   654
      let val (states, st'') = fold_map command_result proof_trs st'
wenzelm@28974
   655
      in (Lazy.value ((tr, st') :: (proof_trs ~~ states)), st'') end
wenzelm@28453
   656
    else
wenzelm@28433
   657
      let
wenzelm@28433
   658
        val (body_trs, end_tr) = split_last proof_trs;
wenzelm@28453
   659
        val finish = Context.Theory o ProofContext.theory_of;
wenzelm@28974
   660
wenzelm@29386
   661
        val future_proof = Proof.global_future_proof
wenzelm@28974
   662
          (fn prf =>
wenzelm@32062
   663
            Future.fork_pri ~1 (fn () =>
wenzelm@30366
   664
              let val (states, result_state) =
wenzelm@28974
   665
                (case st' of State (SOME (Proof (_, (_, orig_gthy)), exit), prev)
wenzelm@33390
   666
                  => State (SOME (Proof (Proof_Node.init prf, (finish, orig_gthy)), exit), prev))
wenzelm@28974
   667
                |> fold_map command_result body_trs
wenzelm@28974
   668
                ||> command (end_tr |> set_print false);
wenzelm@30398
   669
              in (states, presentation_context_of result_state) end))
wenzelm@28974
   670
          #> (fn (states, ctxt) => States.put (SOME states) ctxt);
wenzelm@28974
   671
wenzelm@28974
   672
        val st'' = st' |> command (end_tr |> reset_trans |> end_proof (K future_proof));
wenzelm@28974
   673
wenzelm@28974
   674
        val states =
wenzelm@30398
   675
          (case States.get (presentation_context_of st'') of
wenzelm@28974
   676
            NONE => sys_error ("No future states for " ^ name_of tr ^ Position.str_of (pos_of tr))
wenzelm@28974
   677
          | SOME states => states);
wenzelm@28974
   678
        val result = Lazy.lazy
wenzelm@28974
   679
          (fn () => (tr, st') :: (body_trs ~~ Future.join states) @ [(end_tr, st'')]);
wenzelm@28974
   680
wenzelm@28974
   681
      in (result, st'') end
wenzelm@28433
   682
  end;
wenzelm@28433
   683
wenzelm@29068
   684
fun excursion input =
wenzelm@28425
   685
  let
wenzelm@28433
   686
    val end_pos = if null input then error "No input" else pos_of (fst (List.last input));
wenzelm@28433
   687
wenzelm@29448
   688
    val immediate = not (Goal.future_enabled ());
wenzelm@29427
   689
    val (results, end_state) = fold_map (proof_result immediate) input toplevel;
wenzelm@28433
   690
    val _ =
wenzelm@28433
   691
      (case end_state of
wenzelm@29427
   692
        State (NONE, SOME (Theory (Context.Theory _, _), _)) =>
wenzelm@29427
   693
          command (commit_exit end_pos) end_state
wenzelm@28433
   694
      | _ => error "Unfinished development at end of input");
wenzelm@29427
   695
  in Lazy.lazy (fn () => maps Lazy.force results) end;
wenzelm@7062
   696
wenzelm@6664
   697
end;