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