src/Pure/Isar/toplevel.ML
author wenzelm
Thu Mar 27 17:12:40 2014 +0100 (2014-03-27 ago)
changeset 56303 4cc3f4db3447
parent 56265 785569927666
child 56334 6b3739fee456
permissions -rw-r--r--
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
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@51555
    15
  val is_skipped_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@37953
    24
  val end_theory: Position.T -> state -> theory
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 interact: bool Unsynchronized.ref
wenzelm@32738
    30
  val timing: bool Unsynchronized.ref
wenzelm@32738
    31
  val profiling: int Unsynchronized.ref
wenzelm@16682
    32
  type transition
wenzelm@5828
    33
  val empty: transition
wenzelm@38888
    34
  val print_of: transition -> bool
wenzelm@27427
    35
  val name_of: transition -> string
wenzelm@28105
    36
  val pos_of: transition -> Position.T
wenzelm@5828
    37
  val name: string -> transition -> transition
wenzelm@5828
    38
  val position: Position.T -> transition -> transition
wenzelm@5828
    39
  val interactive: bool -> transition -> transition
wenzelm@38888
    40
  val set_print: bool -> transition -> transition
wenzelm@5828
    41
  val print: transition -> transition
wenzelm@44187
    42
  val init_theory: (unit -> theory) -> transition -> transition
wenzelm@44187
    43
  val is_init: transition -> bool
wenzelm@44186
    44
  val modify_init: (unit -> theory) -> transition -> transition
wenzelm@6689
    45
  val exit: transition -> transition
wenzelm@5828
    46
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    47
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@5828
    48
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@27840
    49
  val ignored: Position.T -> transition
wenzelm@51268
    50
  val is_ignored: transition -> bool
wenzelm@27840
    51
  val malformed: Position.T -> string -> transition
wenzelm@48772
    52
  val is_malformed: transition -> bool
wenzelm@26491
    53
  val generic_theory: (generic_theory -> generic_theory) -> transition -> transition
wenzelm@7612
    54
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@49012
    55
  val theory: (theory -> theory) -> transition -> transition
wenzelm@20985
    56
  val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
wenzelm@21007
    57
  val end_local_theory: transition -> transition
wenzelm@47069
    58
  val open_target: (generic_theory -> local_theory) -> transition -> transition
wenzelm@47069
    59
  val close_target: transition -> transition
wenzelm@45488
    60
  val local_theory': (xstring * Position.T) option -> (bool -> local_theory -> local_theory) ->
wenzelm@45488
    61
    transition -> transition
wenzelm@45488
    62
  val local_theory: (xstring * Position.T) option -> (local_theory -> local_theory) ->
wenzelm@29380
    63
    transition -> transition
wenzelm@45488
    64
  val present_local_theory: (xstring * Position.T) option -> (state -> unit) ->
berghofe@24453
    65
    transition -> transition
wenzelm@45488
    66
  val local_theory_to_proof': (xstring * Position.T) option ->
wenzelm@45488
    67
    (bool -> local_theory -> Proof.state) -> transition -> transition
wenzelm@45488
    68
  val local_theory_to_proof: (xstring * Position.T) option ->
wenzelm@45488
    69
    (local_theory -> Proof.state) -> transition -> transition
wenzelm@17363
    70
  val theory_to_proof: (theory -> Proof.state) -> transition -> transition
wenzelm@21007
    71
  val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition
wenzelm@21007
    72
  val forget_proof: transition -> transition
wenzelm@30366
    73
  val present_proof: (state -> unit) -> transition -> transition
wenzelm@49863
    74
  val proofs': (bool -> Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition
wenzelm@17904
    75
  val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
wenzelm@49863
    76
  val proofs: (Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition
wenzelm@21177
    77
  val proof: (Proof.state -> Proof.state) -> transition -> transition
wenzelm@33390
    78
  val actual_proof: (Proof_Node.T -> Proof_Node.T) -> transition -> transition
wenzelm@27564
    79
  val skip_proof: (int -> int) -> transition -> transition
wenzelm@17904
    80
  val skip_proof_to_theory: (int -> bool) -> transition -> transition
wenzelm@52536
    81
  val exec_id: Document_ID.exec -> 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@28103
    86
  val add_hook: (transition -> state -> state -> unit) -> unit
wenzelm@51423
    87
  val get_timing: transition -> Time.time option
wenzelm@51423
    88
  val put_timing: Time.time option -> transition -> transition
wenzelm@26602
    89
  val transition: bool -> transition -> state -> (state * (exn * string) option) option
wenzelm@51323
    90
  val command_errors: bool -> transition -> state -> Runtime.error list * state option
wenzelm@51284
    91
  val command_exception: bool -> transition -> state -> state
wenzelm@51332
    92
  type result
wenzelm@51332
    93
  val join_results: result -> (transition * state) list
wenzelm@51332
    94
  val element_result: transition Thy_Syntax.element -> state -> result * state
wenzelm@5828
    95
end;
wenzelm@5828
    96
wenzelm@6965
    97
structure Toplevel: TOPLEVEL =
wenzelm@5828
    98
struct
wenzelm@5828
    99
wenzelm@5828
   100
(** toplevel state **)
wenzelm@5828
   101
wenzelm@31476
   102
exception UNDEF = Runtime.UNDEF;
wenzelm@19063
   103
wenzelm@19063
   104
wenzelm@21294
   105
(* local theory wrappers *)
wenzelm@5828
   106
haftmann@38350
   107
val loc_init = Named_Target.context_cmd;
haftmann@52118
   108
val loc_exit = Local_Theory.assert_bottom true #> Local_Theory.exit_global;
wenzelm@21294
   109
wenzelm@47274
   110
fun loc_begin loc (Context.Theory thy) =
wenzelm@47274
   111
      (Context.Theory o loc_exit, loc_init (the_default ("-", Position.none) loc) thy)
wenzelm@47274
   112
  | loc_begin NONE (Context.Proof lthy) =
wenzelm@47274
   113
      (Context.Proof o Local_Theory.restore, lthy)
wenzelm@47274
   114
  | loc_begin (SOME loc) (Context.Proof lthy) =
wenzelm@55446
   115
      (Context.Proof o Named_Target.reinit lthy,
wenzelm@55446
   116
        loc_init loc (loc_exit (Local_Theory.assert_nonbrittle lthy)));
wenzelm@21294
   117
wenzelm@21294
   118
wenzelm@21958
   119
(* datatype node *)
wenzelm@21294
   120
wenzelm@5828
   121
datatype node =
wenzelm@27576
   122
  Theory of generic_theory * Proof.context option
wenzelm@27576
   123
    (*theory with presentation context*) |
wenzelm@33390
   124
  Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory)
wenzelm@27576
   125
    (*proof node, finish, original theory*) |
wenzelm@51555
   126
  Skipped_Proof of int * (generic_theory * generic_theory);
wenzelm@27564
   127
    (*proof depth, resulting theory, original theory*)
wenzelm@5828
   128
wenzelm@20963
   129
val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
wenzelm@18589
   130
val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
wenzelm@51555
   131
val skipped_proof_node = fn Skipped_Proof _ => true | _ => false;
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@51555
   135
  | cases_node f _ (Skipped_Proof (_, (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@51555
   152
  | level (State (SOME (Skipped_Proof (d, _)), _)) = d + 1;   (*different notion of proof depth!*)
wenzelm@17076
   153
wenzelm@52565
   154
fun str_of_state (State (NONE, SOME (Theory (Context.Theory thy, _)))) =
wenzelm@52565
   155
      "at top level, result theory " ^ quote (Context.theory_name thy)
wenzelm@52565
   156
  | str_of_state (State (NONE, _)) = "at top level"
wenzelm@37953
   157
  | str_of_state (State (SOME (Theory (Context.Theory _, _)), _)) = "in theory mode"
wenzelm@37953
   158
  | str_of_state (State (SOME (Theory (Context.Proof _, _)), _)) = "in local theory mode"
wenzelm@37953
   159
  | str_of_state (State (SOME (Proof _), _)) = "in proof mode"
wenzelm@51555
   160
  | str_of_state (State (SOME (Skipped_Proof _), _)) = "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@37953
   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@51555
   170
fun is_skipped_proof state = not (is_toplevel state) andalso skipped_proof_node (node_of state);
wenzelm@18589
   171
wenzelm@19063
   172
fun node_case f g state = cases_node f g (node_of state);
wenzelm@5828
   173
wenzelm@30398
   174
fun presentation_context_of state =
wenzelm@30398
   175
  (case try node_of state of
wenzelm@30398
   176
    SOME (Theory (_, SOME ctxt)) => ctxt
wenzelm@30398
   177
  | SOME node => context_node node
wenzelm@30398
   178
  | NONE => raise UNDEF);
wenzelm@30366
   179
wenzelm@30801
   180
fun previous_context_of (State (_, NONE)) = NONE
wenzelm@37953
   181
  | previous_context_of (State (_, SOME prev)) = SOME (context_node prev);
wenzelm@30801
   182
wenzelm@21506
   183
val context_of = node_case Context.proof_of Proof.context_of;
wenzelm@22089
   184
val generic_theory_of = node_case I (Context.Proof o Proof.context_of);
wenzelm@20963
   185
val theory_of = node_case Context.theory_of Proof.theory_of;
wenzelm@18589
   186
val proof_of = node_case (fn _ => raise UNDEF) I;
wenzelm@17208
   187
wenzelm@18589
   188
fun proof_position_of state =
wenzelm@18589
   189
  (case node_of state of
wenzelm@33390
   190
    Proof (prf, _) => Proof_Node.position prf
wenzelm@18589
   191
  | _ => raise UNDEF);
wenzelm@6664
   192
wenzelm@43667
   193
fun end_theory _ (State (NONE, SOME (Theory (Context.Theory thy, _)))) = thy
wenzelm@48992
   194
  | end_theory pos (State (NONE, _)) = error ("Bad theory" ^ Position.here pos)
wenzelm@48992
   195
  | end_theory pos (State (SOME _, _)) = error ("Unfinished theory" ^ Position.here pos);
wenzelm@37953
   196
wenzelm@5828
   197
wenzelm@16815
   198
(* print state *)
wenzelm@16815
   199
haftmann@38388
   200
val pretty_context = Local_Theory.pretty o Context.cases (Named_Target.theory_init) I;
wenzelm@16815
   201
wenzelm@23640
   202
fun print_state_context state =
wenzelm@24795
   203
  (case try node_of state of
wenzelm@21506
   204
    NONE => []
wenzelm@24795
   205
  | SOME (Theory (gthy, _)) => pretty_context gthy
wenzelm@24795
   206
  | SOME (Proof (_, (_, gthy))) => pretty_context gthy
wenzelm@51555
   207
  | SOME (Skipped_Proof (_, (gthy, _))) => pretty_context gthy)
wenzelm@23640
   208
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@16815
   209
wenzelm@23640
   210
fun print_state prf_only state =
wenzelm@23701
   211
  (case try node_of state of
wenzelm@23701
   212
    NONE => []
wenzelm@23701
   213
  | SOME (Theory (gthy, _)) => if prf_only then [] else pretty_context gthy
wenzelm@23701
   214
  | SOME (Proof (prf, _)) =>
wenzelm@33390
   215
      Proof.pretty_state (Proof_Node.position prf) (Proof_Node.current prf)
wenzelm@51555
   216
  | SOME (Skipped_Proof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)])
wenzelm@50201
   217
  |> Pretty.markup_chunks Markup.state |> Pretty.writeln;
wenzelm@16815
   218
wenzelm@37858
   219
fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">");
wenzelm@37858
   220
wenzelm@16815
   221
wenzelm@15668
   222
wenzelm@5828
   223
(** toplevel transitions **)
wenzelm@5828
   224
wenzelm@53403
   225
val quiet = Unsynchronized.ref false;  (*Proof General legacy*)
wenzelm@53403
   226
val interact = Unsynchronized.ref false;  (*Proof General legacy*)
wenzelm@53403
   227
val timing = Unsynchronized.ref false;  (*Proof General legacy*)
wenzelm@32738
   228
val profiling = Unsynchronized.ref 0;
wenzelm@16682
   229
wenzelm@53709
   230
wenzelm@27601
   231
(* node transactions -- maintaining stable checkpoints *)
wenzelm@7022
   232
wenzelm@31476
   233
exception FAILURE of state * exn;
wenzelm@31476
   234
wenzelm@6689
   235
local
wenzelm@6689
   236
wenzelm@30366
   237
fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
wenzelm@30366
   238
  | reset_presentation node = node;
wenzelm@30366
   239
wenzelm@27576
   240
fun map_theory f (Theory (gthy, ctxt)) =
wenzelm@33671
   241
      Theory (Context.mapping f (Local_Theory.raw_theory f) gthy, ctxt)
wenzelm@27576
   242
  | map_theory _ node = node;
wenzelm@6689
   243
wenzelm@6689
   244
in
wenzelm@6689
   245
wenzelm@37953
   246
fun apply_transaction f g node =
wenzelm@20128
   247
  let
wenzelm@27576
   248
    val cont_node = reset_presentation node;
wenzelm@56265
   249
    val context = cases_node I (Context.Proof o Proof.context_of) cont_node;
wenzelm@37953
   250
    fun state_error e nd = (State (SOME nd, SOME node), e);
wenzelm@6689
   251
wenzelm@20128
   252
    val (result, err) =
wenzelm@20128
   253
      cont_node
wenzelm@56303
   254
      |> Runtime.controlled_execution (SOME context) f
wenzelm@26624
   255
      |> state_error NONE
wenzelm@26624
   256
      handle exn => state_error (SOME exn) cont_node;
wenzelm@20128
   257
  in
wenzelm@52696
   258
    (case err of
wenzelm@52696
   259
      NONE => tap g result
wenzelm@52696
   260
    | SOME exn => raise FAILURE (result, exn))
wenzelm@20128
   261
  end;
wenzelm@6689
   262
wenzelm@43667
   263
val exit_transaction =
wenzelm@43667
   264
  apply_transaction
wenzelm@43667
   265
    (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (Theory.end_theory thy), NONE)
wenzelm@43667
   266
      | node => node) (K ())
wenzelm@43667
   267
  #> (fn State (node', _) => State (NONE, node'));
wenzelm@43667
   268
wenzelm@6689
   269
end;
wenzelm@6689
   270
wenzelm@6689
   271
wenzelm@6689
   272
(* primitive transitions *)
wenzelm@6689
   273
wenzelm@5828
   274
datatype trans =
wenzelm@44187
   275
  Init of unit -> theory |               (*init theory*)
wenzelm@37953
   276
  Exit |                                 (*formal exit of theory*)
wenzelm@37953
   277
  Keep of bool -> state -> unit |        (*peek at state*)
wenzelm@30366
   278
  Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
wenzelm@21958
   279
wenzelm@6689
   280
local
wenzelm@5828
   281
wenzelm@44187
   282
fun apply_tr _ (Init f) (State (NONE, _)) =
wenzelm@56303
   283
      State (SOME (Theory (Context.Theory (Runtime.controlled_execution NONE f ()), NONE)), NONE)
wenzelm@43667
   284
  | apply_tr _ Exit (State (SOME (state as Theory (Context.Theory _, _)), _)) =
wenzelm@43667
   285
      exit_transaction state
wenzelm@32792
   286
  | apply_tr int (Keep f) state =
wenzelm@56303
   287
      Runtime.controlled_execution (try generic_theory_of state) (fn x => tap (f int) x) state
wenzelm@32792
   288
  | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
wenzelm@32792
   289
      apply_transaction (fn x => f int x) g state
wenzelm@32792
   290
  | apply_tr _ _ _ = raise UNDEF;
wenzelm@5828
   291
wenzelm@32792
   292
fun apply_union _ [] state = raise FAILURE (state, UNDEF)
wenzelm@32792
   293
  | apply_union int (tr :: trs) state =
wenzelm@32792
   294
      apply_union int trs state
wenzelm@32792
   295
        handle Runtime.UNDEF => apply_tr int tr state
wenzelm@32792
   296
          | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
wenzelm@6689
   297
          | exn as FAILURE _ => raise exn
wenzelm@6689
   298
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   299
wenzelm@6689
   300
in
wenzelm@6689
   301
wenzelm@32792
   302
fun apply_trans int trs state = (apply_union int trs state, NONE)
skalberg@15531
   303
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   304
wenzelm@6689
   305
end;
wenzelm@5828
   306
wenzelm@5828
   307
wenzelm@5828
   308
(* datatype transition *)
wenzelm@5828
   309
wenzelm@5828
   310
datatype transition = Transition of
wenzelm@26621
   311
 {name: string,              (*command name*)
wenzelm@26621
   312
  pos: Position.T,           (*source position*)
wenzelm@53403
   313
  int_only: bool,            (*interactive-only*)  (* TTY / Proof General legacy*)
wenzelm@26621
   314
  print: bool,               (*print result state*)
wenzelm@51423
   315
  timing: Time.time option,  (*prescient timing information*)
wenzelm@26621
   316
  trans: trans list};        (*primitive transitions (union)*)
wenzelm@5828
   317
wenzelm@51658
   318
fun make_transition (name, pos, int_only, print, timing, trans) =
wenzelm@51217
   319
  Transition {name = name, pos = pos, int_only = int_only, print = print,
wenzelm@51658
   320
    timing = timing, trans = trans};
wenzelm@5828
   321
wenzelm@51658
   322
fun map_transition f (Transition {name, pos, int_only, print, timing, trans}) =
wenzelm@51658
   323
  make_transition (f (name, pos, int_only, print, timing, trans));
wenzelm@5828
   324
wenzelm@51658
   325
val empty = make_transition ("", Position.none, false, false, NONE, []);
wenzelm@5828
   326
wenzelm@5828
   327
wenzelm@5828
   328
(* diagnostics *)
wenzelm@5828
   329
wenzelm@38888
   330
fun print_of (Transition {print, ...}) = print;
wenzelm@27427
   331
fun name_of (Transition {name, ...}) = name;
wenzelm@28105
   332
fun pos_of (Transition {pos, ...}) = pos;
wenzelm@5828
   333
wenzelm@48993
   334
fun command_msg msg tr = msg ^ "command " ^ quote (name_of tr) ^ Position.here (pos_of tr);
wenzelm@38875
   335
fun at_command tr = command_msg "At " tr;
wenzelm@5828
   336
wenzelm@5828
   337
fun type_error tr state =
wenzelm@18685
   338
  ERROR (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
wenzelm@5828
   339
wenzelm@5828
   340
wenzelm@5828
   341
(* modify transitions *)
wenzelm@5828
   342
wenzelm@51658
   343
fun name name = map_transition (fn (_, pos, int_only, print, timing, trans) =>
wenzelm@51658
   344
  (name, pos, int_only, print, timing, trans));
wenzelm@9010
   345
wenzelm@51658
   346
fun position pos = map_transition (fn (name, _, int_only, print, timing, trans) =>
wenzelm@51658
   347
  (name, pos, int_only, print, timing, trans));
wenzelm@5828
   348
wenzelm@51658
   349
fun interactive int_only = map_transition (fn (name, pos, _, print, timing, trans) =>
wenzelm@51658
   350
  (name, pos, int_only, print, timing, trans));
wenzelm@14923
   351
wenzelm@51658
   352
fun add_trans tr = map_transition (fn (name, pos, int_only, print, timing, trans) =>
wenzelm@51658
   353
  (name, pos, int_only, print, timing, tr :: trans));
wenzelm@16607
   354
wenzelm@51658
   355
val reset_trans = map_transition (fn (name, pos, int_only, print, timing, _) =>
wenzelm@51658
   356
  (name, pos, int_only, print, timing, []));
wenzelm@28433
   357
wenzelm@51658
   358
fun set_print print = map_transition (fn (name, pos, int_only, _, timing, trans) =>
wenzelm@51658
   359
  (name, pos, int_only, print, timing, trans));
wenzelm@28453
   360
wenzelm@28453
   361
val print = set_print true;
wenzelm@5828
   362
wenzelm@5828
   363
wenzelm@21007
   364
(* basic transitions *)
wenzelm@5828
   365
wenzelm@44187
   366
fun init_theory f = add_trans (Init f);
wenzelm@37977
   367
wenzelm@44187
   368
fun is_init (Transition {trans = [Init _], ...}) = true
wenzelm@44187
   369
  | is_init _ = false;
wenzelm@44187
   370
wenzelm@44187
   371
fun modify_init f tr = if is_init tr then init_theory f (reset_trans tr) else tr;
wenzelm@37977
   372
wenzelm@6689
   373
val exit = add_trans Exit;
wenzelm@7612
   374
val keep' = add_trans o Keep;
wenzelm@30366
   375
wenzelm@30366
   376
fun present_transaction f g = add_trans (Transaction (f, g));
wenzelm@30366
   377
fun transaction f = present_transaction f (K ());
wenzelm@5828
   378
wenzelm@7612
   379
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   380
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   381
wenzelm@27840
   382
fun ignored pos = empty |> name "<ignored>" |> position pos |> imperative I;
wenzelm@51268
   383
fun is_ignored tr = name_of tr = "<ignored>";
wenzelm@48772
   384
wenzelm@48772
   385
val malformed_name = "<malformed>";
wenzelm@27840
   386
fun malformed pos msg =
wenzelm@48772
   387
  empty |> name malformed_name |> position pos |> imperative (fn () => error msg);
wenzelm@48772
   388
fun is_malformed tr = name_of tr = malformed_name;
wenzelm@27840
   389
wenzelm@21007
   390
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@21007
   391
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@21007
   392
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@15668
   393
wenzelm@21007
   394
wenzelm@49012
   395
(* theory transitions *)
wenzelm@44304
   396
wenzelm@27601
   397
fun generic_theory f = transaction (fn _ =>
wenzelm@26491
   398
  (fn Theory (gthy, _) => Theory (f gthy, NONE)
wenzelm@26491
   399
    | _ => raise UNDEF));
wenzelm@26491
   400
wenzelm@27601
   401
fun theory' f = transaction (fn int =>
wenzelm@33725
   402
  (fn Theory (Context.Theory thy, _) =>
wenzelm@33725
   403
      let val thy' = thy
wenzelm@49012
   404
        |> Sign.new_group
wenzelm@33725
   405
        |> f int
wenzelm@33725
   406
        |> Sign.reset_group;
wenzelm@33725
   407
      in Theory (Context.Theory thy', NONE) end
wenzelm@20963
   408
    | _ => raise UNDEF));
wenzelm@20963
   409
wenzelm@20963
   410
fun theory f = theory' (K f);
wenzelm@20963
   411
wenzelm@27601
   412
fun begin_local_theory begin f = transaction (fn _ =>
wenzelm@20963
   413
  (fn Theory (Context.Theory thy, _) =>
wenzelm@20963
   414
        let
wenzelm@20985
   415
          val lthy = f thy;
wenzelm@21294
   416
          val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
wenzelm@21294
   417
        in Theory (gthy, SOME lthy) end
wenzelm@20963
   418
    | _ => raise UNDEF));
wenzelm@17076
   419
wenzelm@27601
   420
val end_local_theory = transaction (fn _ =>
wenzelm@21294
   421
  (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
wenzelm@21007
   422
    | _ => raise UNDEF));
wenzelm@21007
   423
wenzelm@47069
   424
fun open_target f = transaction (fn _ =>
wenzelm@47069
   425
  (fn Theory (gthy, _) =>
wenzelm@47069
   426
        let val lthy = f gthy
wenzelm@47069
   427
        in Theory (Context.Proof lthy, SOME lthy) end
wenzelm@47069
   428
    | _ => raise UNDEF));
wenzelm@47069
   429
wenzelm@47069
   430
val close_target = transaction (fn _ =>
wenzelm@47069
   431
  (fn Theory (Context.Proof lthy, _) =>
wenzelm@47069
   432
        (case try Local_Theory.close_target lthy of
wenzelm@50739
   433
          SOME ctxt' =>
wenzelm@50739
   434
            let
wenzelm@50739
   435
              val gthy' =
wenzelm@50739
   436
                if can Local_Theory.assert ctxt'
wenzelm@50739
   437
                then Context.Proof ctxt'
wenzelm@50739
   438
                else Context.Theory (Proof_Context.theory_of ctxt');
wenzelm@50739
   439
            in Theory (gthy', SOME lthy) end
wenzelm@47069
   440
        | NONE => raise UNDEF)
wenzelm@47069
   441
    | _ => raise UNDEF));
wenzelm@47069
   442
wenzelm@47069
   443
wenzelm@21007
   444
local
wenzelm@21007
   445
wenzelm@30366
   446
fun local_theory_presentation loc f = present_transaction (fn int =>
wenzelm@21294
   447
  (fn Theory (gthy, _) =>
wenzelm@21294
   448
        let
wenzelm@49062
   449
          val (finish, lthy) = loc_begin loc gthy;
wenzelm@47274
   450
          val lthy' = lthy
wenzelm@49012
   451
            |> Local_Theory.new_group
wenzelm@33725
   452
            |> f int
wenzelm@33725
   453
            |> Local_Theory.reset_group;
wenzelm@21294
   454
        in Theory (finish lthy', SOME lthy') end
wenzelm@30366
   455
    | _ => raise UNDEF));
wenzelm@15668
   456
wenzelm@21007
   457
in
wenzelm@21007
   458
wenzelm@30366
   459
fun local_theory' loc f = local_theory_presentation loc f (K ());
wenzelm@29380
   460
fun local_theory loc f = local_theory' loc (K f);
wenzelm@30366
   461
fun present_local_theory loc = local_theory_presentation loc (K I);
wenzelm@18955
   462
wenzelm@21007
   463
end;
wenzelm@21007
   464
wenzelm@21007
   465
wenzelm@21007
   466
(* proof transitions *)
wenzelm@21007
   467
wenzelm@27601
   468
fun end_proof f = transaction (fn int =>
wenzelm@24795
   469
  (fn Proof (prf, (finish, _)) =>
wenzelm@33390
   470
        let val state = Proof_Node.current prf in
wenzelm@21007
   471
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   472
            let
wenzelm@21007
   473
              val ctxt' = f int state;
wenzelm@21007
   474
              val gthy' = finish ctxt';
wenzelm@21007
   475
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   476
          else raise UNDEF
wenzelm@21007
   477
        end
wenzelm@51555
   478
    | Skipped_Proof (0, (gthy, _)) => Theory (gthy, NONE)
wenzelm@21007
   479
    | _ => raise UNDEF));
wenzelm@21007
   480
wenzelm@21294
   481
local
wenzelm@21294
   482
wenzelm@47274
   483
fun begin_proof init = transaction (fn int =>
wenzelm@21294
   484
  (fn Theory (gthy, _) =>
wenzelm@21294
   485
    let
wenzelm@47274
   486
      val (finish, prf) = init int gthy;
wenzelm@52499
   487
      val skip = Goal.skip_proofs_enabled ();
wenzelm@40960
   488
      val (is_goal, no_skip) =
wenzelm@40960
   489
        (true, Proof.schematic_goal prf) handle ERROR _ => (false, true);
wenzelm@47274
   490
      val _ =
wenzelm@47274
   491
        if is_goal andalso skip andalso no_skip then
wenzelm@47274
   492
          warning "Cannot skip proof of schematic goal statement"
wenzelm@47274
   493
        else ();
wenzelm@21294
   494
    in
wenzelm@40960
   495
      if skip andalso not no_skip then
wenzelm@51555
   496
        Skipped_Proof (0, (finish (Proof.global_skip_proof true prf), gthy))
wenzelm@47274
   497
      else Proof (Proof_Node.init prf, (finish, gthy))
wenzelm@21294
   498
    end
wenzelm@21294
   499
  | _ => raise UNDEF));
wenzelm@21294
   500
wenzelm@21294
   501
in
wenzelm@21294
   502
wenzelm@24780
   503
fun local_theory_to_proof' loc f = begin_proof
wenzelm@47274
   504
  (fn int => fn gthy =>
wenzelm@49062
   505
    let val (finish, lthy) = loc_begin loc gthy
wenzelm@49012
   506
    in (finish o Local_Theory.reset_group, f int (Local_Theory.new_group lthy)) end);
wenzelm@24780
   507
berghofe@24453
   508
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   509
wenzelm@21294
   510
fun theory_to_proof f = begin_proof
wenzelm@47274
   511
  (fn _ => fn gthy =>
wenzelm@56057
   512
    (Context.Theory o Sign.reset_group o Sign.change_check o Proof_Context.theory_of,
wenzelm@49062
   513
      (case gthy of
wenzelm@52788
   514
        Context.Theory thy => f (Sign.new_group thy)
wenzelm@49012
   515
      | _ => raise UNDEF)));
wenzelm@21294
   516
wenzelm@21294
   517
end;
wenzelm@21294
   518
wenzelm@27601
   519
val forget_proof = transaction (fn _ =>
wenzelm@21007
   520
  (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@51555
   521
    | Skipped_Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   522
    | _ => raise UNDEF));
wenzelm@21007
   523
wenzelm@30366
   524
val present_proof = present_transaction (fn _ =>
wenzelm@49062
   525
  (fn Proof (prf, x) => Proof (Proof_Node.apply I prf, x)
wenzelm@51555
   526
    | skip as Skipped_Proof _ => skip
wenzelm@30366
   527
    | _ => raise UNDEF));
wenzelm@21177
   528
wenzelm@27601
   529
fun proofs' f = transaction (fn int =>
wenzelm@49062
   530
  (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
wenzelm@51555
   531
    | skip as Skipped_Proof _ => skip
wenzelm@16815
   532
    | _ => raise UNDEF));
wenzelm@15668
   533
wenzelm@49863
   534
fun proof' f = proofs' ((Seq.single o Seq.Result) oo f);
wenzelm@17904
   535
val proofs = proofs' o K;
wenzelm@6689
   536
val proof = proof' o K;
wenzelm@16815
   537
wenzelm@27601
   538
fun actual_proof f = transaction (fn _ =>
wenzelm@21007
   539
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   540
    | _ => raise UNDEF));
wenzelm@16815
   541
wenzelm@53403
   542
(*Proof General legacy*)
wenzelm@27601
   543
fun skip_proof f = transaction (fn _ =>
wenzelm@51555
   544
  (fn Skipped_Proof (h, x) => Skipped_Proof (f h, x)
wenzelm@18563
   545
    | _ => raise UNDEF));
wenzelm@18563
   546
wenzelm@53403
   547
(*Proof General legacy*)
wenzelm@27601
   548
fun skip_proof_to_theory pred = transaction (fn _ =>
wenzelm@51555
   549
  (fn Skipped_Proof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
wenzelm@33725
   550
    | _ => raise UNDEF));
wenzelm@5828
   551
wenzelm@5828
   552
wenzelm@5828
   553
wenzelm@5828
   554
(** toplevel transactions **)
wenzelm@5828
   555
wenzelm@52527
   556
(* runtime position *)
wenzelm@27427
   557
wenzelm@52536
   558
fun exec_id id (tr as Transition {pos, ...}) =
wenzelm@52536
   559
  position (Position.put_id (Document_ID.print id) pos) tr;
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@25799
   564
wenzelm@28095
   565
(* post-transition hooks *)
wenzelm@28095
   566
wenzelm@37905
   567
local
wenzelm@56147
   568
  val hooks =
wenzelm@56147
   569
    Synchronized.var "Toplevel.hooks" ([]: (transition -> state -> state -> unit) list);
wenzelm@37905
   570
in
wenzelm@28095
   571
wenzelm@56147
   572
fun add_hook hook = Synchronized.change hooks (cons hook);
wenzelm@56147
   573
fun get_hooks () = Synchronized.value hooks;
wenzelm@28095
   574
wenzelm@28095
   575
end;
wenzelm@28095
   576
wenzelm@28095
   577
wenzelm@5828
   578
(* apply transitions *)
wenzelm@5828
   579
wenzelm@51217
   580
fun get_timing (Transition {timing, ...}) = timing;
wenzelm@51658
   581
fun put_timing timing = map_transition (fn (name, pos, int_only, print, _, trans) =>
wenzelm@51658
   582
  (name, pos, int_only, print, timing, trans));
wenzelm@51217
   583
wenzelm@6664
   584
local
wenzelm@6664
   585
wenzelm@51658
   586
fun app int (tr as Transition {trans, print, ...}) =
wenzelm@25819
   587
  setmp_thread_position tr (fn state =>
wenzelm@25799
   588
    let
wenzelm@51595
   589
      val timing_start = Timing.start ();
wenzelm@25799
   590
wenzelm@51595
   591
      val (result, opt_err) =
wenzelm@51658
   592
         state |> (apply_trans int trans |> ! profiling > 0 ? profile (! profiling));
wenzelm@26621
   593
      val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
wenzelm@51216
   594
wenzelm@51595
   595
      val timing_result = Timing.result timing_start;
wenzelm@51662
   596
      val timing_props =
wenzelm@51662
   597
        Markup.command_timing :: (Markup.nameN, name_of tr) :: Position.properties_of (pos_of tr);
wenzelm@51662
   598
      val _ = Timing.protocol_message timing_props timing_result;
wenzelm@51595
   599
    in
wenzelm@51595
   600
      (result, Option.map (fn UNDEF => type_error tr state | exn => exn) opt_err)
wenzelm@51595
   601
    end);
wenzelm@6664
   602
wenzelm@6664
   603
in
wenzelm@5828
   604
wenzelm@26602
   605
fun transition int tr st =
wenzelm@28095
   606
  let
wenzelm@28095
   607
    val hooks = get_hooks ();
wenzelm@28103
   608
    fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
wenzelm@28095
   609
wenzelm@28095
   610
    val ctxt = try context_of st;
wenzelm@28095
   611
    val res =
wenzelm@28095
   612
      (case app int tr st of
wenzelm@38888
   613
        (_, SOME Runtime.TERMINATE) => NONE
wenzelm@38888
   614
      | (st', SOME (Runtime.EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
wenzelm@31476
   615
      | (st', SOME exn) => SOME (st', SOME (Runtime.exn_context ctxt exn, at_command tr))
wenzelm@28103
   616
      | (st', NONE) => SOME (st', NONE));
wenzelm@28103
   617
    val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
wenzelm@28095
   618
  in res end;
wenzelm@6664
   619
wenzelm@6664
   620
end;
wenzelm@5828
   621
wenzelm@5828
   622
wenzelm@51284
   623
(* managed commands *)
wenzelm@5828
   624
wenzelm@51323
   625
fun command_errors int tr st =
wenzelm@51323
   626
  (case transition int tr st of
wenzelm@51323
   627
    SOME (st', NONE) => ([], SOME st')
wenzelm@56303
   628
  | SOME (_, SOME (exn, _)) => (Runtime.exn_messages_ids exn, NONE)
wenzelm@56303
   629
  | NONE => (Runtime.exn_messages_ids Runtime.TERMINATE, NONE));
wenzelm@51323
   630
wenzelm@51284
   631
fun command_exception int tr st =
wenzelm@51284
   632
  (case transition int tr st of
wenzelm@28425
   633
    SOME (st', NONE) => st'
wenzelm@39285
   634
  | SOME (_, SOME (exn, info)) =>
wenzelm@39285
   635
      if Exn.is_interrupt exn then reraise exn else raise Runtime.EXCURSION_FAIL (exn, info)
wenzelm@38888
   636
  | NONE => raise Runtime.EXCURSION_FAIL (Runtime.TERMINATE, at_command tr));
wenzelm@27576
   637
wenzelm@51323
   638
fun command tr = command_exception (! interact) tr;
wenzelm@51284
   639
wenzelm@28433
   640
wenzelm@46959
   641
(* scheduled proof result *)
wenzelm@28433
   642
wenzelm@51332
   643
datatype result =
wenzelm@51332
   644
  Result of transition * state |
wenzelm@51332
   645
  Result_List of result list |
wenzelm@51332
   646
  Result_Future of result future;
wenzelm@51332
   647
wenzelm@51332
   648
fun join_results (Result x) = [x]
wenzelm@51332
   649
  | join_results (Result_List xs) = maps join_results xs
wenzelm@51332
   650
  | join_results (Result_Future x) = join_results (Future.join x);
wenzelm@51332
   651
wenzelm@51323
   652
local
wenzelm@51323
   653
wenzelm@47417
   654
structure Result = Proof_Data
wenzelm@28974
   655
(
wenzelm@51332
   656
  type T = result;
wenzelm@51332
   657
  val empty: T = Result_List [];
wenzelm@47417
   658
  fun init _ = empty;
wenzelm@28974
   659
);
wenzelm@28974
   660
wenzelm@51332
   661
val get_result = Result.get o Proof.context_of;
wenzelm@51332
   662
val put_result = Proof.map_context o Result.put;
wenzelm@51324
   663
wenzelm@51423
   664
fun timing_estimate include_head elem =
wenzelm@51423
   665
  let
wenzelm@51423
   666
    val trs = Thy_Syntax.flat_element elem |> not include_head ? tl;
wenzelm@51423
   667
    val timings = map get_timing trs;
wenzelm@51423
   668
  in
wenzelm@51423
   669
    if forall is_some timings then
wenzelm@51423
   670
      SOME (fold (curry Time.+ o the) timings Time.zeroTime)
wenzelm@51423
   671
    else NONE
wenzelm@51423
   672
  end;
wenzelm@51423
   673
wenzelm@51423
   674
fun priority NONE = ~1
wenzelm@51423
   675
  | priority (SOME estimate) =
wenzelm@51423
   676
      Int.min (Real.floor (Real.max (Math.log10 (Time.toReal estimate), ~3.0)) - 3, ~1);
wenzelm@51423
   677
wenzelm@51423
   678
fun proof_future_enabled estimate st =
wenzelm@51324
   679
  (case try proof_of st of
wenzelm@51324
   680
    NONE => false
wenzelm@51324
   681
  | SOME state =>
wenzelm@51324
   682
      not (Proof.is_relevant state) andalso
wenzelm@51324
   683
       (if can (Proof.assert_bottom true) state
wenzelm@53189
   684
        then Goal.future_enabled 1
wenzelm@51423
   685
        else
wenzelm@51423
   686
          (case estimate of
wenzelm@53189
   687
            NONE => Goal.future_enabled 2
wenzelm@51423
   688
          | SOME t => Goal.future_enabled_timing t)));
wenzelm@51278
   689
wenzelm@51323
   690
fun atom_result tr st =
wenzelm@51323
   691
  let
wenzelm@51323
   692
    val st' =
wenzelm@53189
   693
      if Goal.future_enabled 1 andalso Keyword.is_diag (name_of tr) then
wenzelm@53192
   694
        (Execution.fork
wenzelm@51605
   695
          {name = "Toplevel.diag", pos = pos_of tr,
wenzelm@51605
   696
            pri = priority (timing_estimate true (Thy_Syntax.atom tr))}
wenzelm@51605
   697
          (fn () => command tr st); st)
wenzelm@51323
   698
      else command tr st;
wenzelm@51332
   699
  in (Result (tr, st'), st') end;
wenzelm@51323
   700
wenzelm@51323
   701
in
wenzelm@51323
   702
wenzelm@51332
   703
fun element_result (Thy_Syntax.Element (tr, NONE)) st = atom_result tr st
wenzelm@51423
   704
  | element_result (elem as Thy_Syntax.Element (head_tr, SOME element_rest)) st =
wenzelm@48633
   705
      let
wenzelm@51324
   706
        val (head_result, st') = atom_result head_tr st;
wenzelm@51332
   707
        val (body_elems, end_tr) = element_rest;
wenzelm@51423
   708
        val estimate = timing_estimate false elem;
wenzelm@51324
   709
      in
wenzelm@51423
   710
        if not (proof_future_enabled estimate st')
wenzelm@51324
   711
        then
wenzelm@51332
   712
          let
wenzelm@51332
   713
            val proof_trs = maps Thy_Syntax.flat_element body_elems @ [end_tr];
wenzelm@51332
   714
            val (proof_results, st'') = fold_map atom_result proof_trs st';
wenzelm@51332
   715
          in (Result_List (head_result :: proof_results), st'') end
wenzelm@51324
   716
        else
wenzelm@51324
   717
          let
wenzelm@51324
   718
            val finish = Context.Theory o Proof_Context.theory_of;
wenzelm@28974
   719
wenzelm@51605
   720
            val future_proof =
wenzelm@51605
   721
              Proof.future_proof (fn state =>
wenzelm@53192
   722
                Execution.fork
wenzelm@51605
   723
                  {name = "Toplevel.future_proof", pos = pos_of head_tr, pri = priority estimate}
wenzelm@51605
   724
                  (fn () =>
wenzelm@51605
   725
                    let
wenzelm@51605
   726
                      val State (SOME (Proof (prf, (_, orig_gthy))), prev) = st';
wenzelm@51605
   727
                      val prf' = Proof_Node.apply (K state) prf;
wenzelm@51605
   728
                      val (result, result_state) =
wenzelm@51605
   729
                        State (SOME (Proof (prf', (finish, orig_gthy))), prev)
wenzelm@51605
   730
                        |> fold_map element_result body_elems ||> command end_tr;
wenzelm@51605
   731
                    in (Result_List result, presentation_context_of result_state) end))
wenzelm@51332
   732
              #> (fn (res, state') => state' |> put_result (Result_Future res));
wenzelm@51332
   733
wenzelm@51332
   734
            val forked_proof =
wenzelm@51332
   735
              proof (future_proof #>
wenzelm@51332
   736
                (fn state => state |> Proof.local_done_proof |> put_result (get_result state))) o
wenzelm@51332
   737
              end_proof (fn _ => future_proof #>
wenzelm@51332
   738
                (fn state => state |> Proof.global_done_proof |> Result.put (get_result state)));
wenzelm@28974
   739
wenzelm@51324
   740
            val st'' = st'
wenzelm@51332
   741
              |> command (head_tr |> set_print false |> reset_trans |> forked_proof);
wenzelm@51332
   742
            val end_result = Result (end_tr, st'');
wenzelm@51324
   743
            val result =
wenzelm@51332
   744
              Result_List [head_result, Result.get (presentation_context_of st''), end_result];
wenzelm@51324
   745
          in (result, st'') end
wenzelm@51324
   746
      end;
wenzelm@28433
   747
wenzelm@6664
   748
end;
wenzelm@51323
   749
wenzelm@51323
   750
end;