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