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