src/Pure/Isar/toplevel.ML
author wenzelm
Tue Feb 26 13:27:24 2013 +0100 (2013-02-26 ago)
changeset 51282 3d3c1ea0a401
parent 51278 1ee77b36490e
child 51284 59a03019f3bf
permissions -rw-r--r--
tuned 2464ba6e6fc9 -- NB: approximative_id is NONE for PIDE document transactions;
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@51268
    54
  val is_ignored: transition -> bool
wenzelm@27840
    55
  val malformed: Position.T -> string -> transition
wenzelm@48772
    56
  val is_malformed: transition -> bool
wenzelm@26491
    57
  val generic_theory: (generic_theory -> generic_theory) -> transition -> transition
wenzelm@7612
    58
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@49012
    59
  val theory: (theory -> theory) -> transition -> transition
wenzelm@20985
    60
  val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
wenzelm@21007
    61
  val end_local_theory: transition -> transition
wenzelm@47069
    62
  val open_target: (generic_theory -> local_theory) -> transition -> transition
wenzelm@47069
    63
  val close_target: transition -> transition
wenzelm@45488
    64
  val local_theory': (xstring * Position.T) option -> (bool -> local_theory -> local_theory) ->
wenzelm@45488
    65
    transition -> transition
wenzelm@45488
    66
  val local_theory: (xstring * Position.T) option -> (local_theory -> local_theory) ->
wenzelm@29380
    67
    transition -> transition
wenzelm@45488
    68
  val present_local_theory: (xstring * Position.T) option -> (state -> unit) ->
berghofe@24453
    69
    transition -> transition
wenzelm@45488
    70
  val local_theory_to_proof': (xstring * Position.T) option ->
wenzelm@45488
    71
    (bool -> local_theory -> Proof.state) -> transition -> transition
wenzelm@45488
    72
  val local_theory_to_proof: (xstring * Position.T) option ->
wenzelm@45488
    73
    (local_theory -> Proof.state) -> transition -> transition
wenzelm@17363
    74
  val theory_to_proof: (theory -> Proof.state) -> transition -> transition
wenzelm@21007
    75
  val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition
wenzelm@21007
    76
  val forget_proof: transition -> transition
wenzelm@30366
    77
  val present_proof: (state -> unit) -> transition -> transition
wenzelm@49863
    78
  val proofs': (bool -> Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition
wenzelm@17904
    79
  val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
wenzelm@49863
    80
  val proofs: (Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition
wenzelm@21177
    81
  val proof: (Proof.state -> Proof.state) -> transition -> transition
wenzelm@33390
    82
  val actual_proof: (Proof_Node.T -> Proof_Node.T) -> transition -> transition
wenzelm@27564
    83
  val skip_proof: (int -> int) -> transition -> transition
wenzelm@17904
    84
  val skip_proof_to_theory: (int -> bool) -> transition -> transition
wenzelm@27427
    85
  val get_id: transition -> string option
wenzelm@27427
    86
  val put_id: string -> transition -> transition
wenzelm@9512
    87
  val unknown_theory: transition -> transition
wenzelm@9512
    88
  val unknown_proof: transition -> transition
wenzelm@9512
    89
  val unknown_context: transition -> transition
wenzelm@28425
    90
  val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b
wenzelm@27606
    91
  val status: transition -> Markup.T -> unit
wenzelm@28103
    92
  val add_hook: (transition -> state -> state -> unit) -> unit
wenzelm@51228
    93
  val approximative_id: transition -> {file: string, offset: int, name: string} option
wenzelm@51228
    94
  val get_timing: transition -> Time.time
wenzelm@51228
    95
  val put_timing: Time.time -> transition -> transition
wenzelm@26602
    96
  val transition: bool -> transition -> state -> (state * (exn * string) option) option
wenzelm@28425
    97
  val command: transition -> state -> state
wenzelm@51278
    98
  val atom_result: transition -> state -> (transition * state) * state
wenzelm@51273
    99
  val element_result: transition Thy_Syntax.element -> state ->
wenzelm@46959
   100
    (transition * state) list future * state
wenzelm@5828
   101
end;
wenzelm@5828
   102
wenzelm@6965
   103
structure Toplevel: TOPLEVEL =
wenzelm@5828
   104
struct
wenzelm@5828
   105
wenzelm@5828
   106
(** toplevel state **)
wenzelm@5828
   107
wenzelm@31476
   108
exception UNDEF = Runtime.UNDEF;
wenzelm@19063
   109
wenzelm@19063
   110
wenzelm@21294
   111
(* local theory wrappers *)
wenzelm@5828
   112
haftmann@38350
   113
val loc_init = Named_Target.context_cmd;
wenzelm@47069
   114
val loc_exit = Local_Theory.assert_bottom true #> Local_Theory.exit_global;
wenzelm@21294
   115
wenzelm@47274
   116
fun loc_begin loc (Context.Theory thy) =
wenzelm@47274
   117
      (Context.Theory o loc_exit, loc_init (the_default ("-", Position.none) loc) thy)
wenzelm@47274
   118
  | loc_begin NONE (Context.Proof lthy) =
wenzelm@47274
   119
      (Context.Proof o Local_Theory.restore, lthy)
wenzelm@47274
   120
  | loc_begin (SOME loc) (Context.Proof lthy) =
wenzelm@47274
   121
      (Context.Proof o Named_Target.reinit lthy, loc_init loc (loc_exit lthy));
wenzelm@21294
   122
wenzelm@21294
   123
wenzelm@21958
   124
(* datatype node *)
wenzelm@21294
   125
wenzelm@5828
   126
datatype node =
wenzelm@27576
   127
  Theory of generic_theory * Proof.context option
wenzelm@27576
   128
    (*theory with presentation context*) |
wenzelm@33390
   129
  Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory)
wenzelm@27576
   130
    (*proof node, finish, original theory*) |
wenzelm@27564
   131
  SkipProof of int * (generic_theory * generic_theory);
wenzelm@27564
   132
    (*proof depth, resulting theory, original theory*)
wenzelm@5828
   133
wenzelm@20963
   134
val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
wenzelm@18589
   135
val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
wenzelm@18589
   136
wenzelm@20963
   137
fun cases_node f _ (Theory (gthy, _)) = f gthy
wenzelm@33390
   138
  | cases_node _ g (Proof (prf, _)) = g (Proof_Node.current prf)
wenzelm@21007
   139
  | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy;
wenzelm@19063
   140
wenzelm@29066
   141
val context_node = cases_node Context.proof_of Proof.context_of;
wenzelm@29066
   142
wenzelm@21958
   143
wenzelm@21958
   144
(* datatype state *)
wenzelm@21958
   145
wenzelm@37953
   146
datatype state = State of node option * node option;  (*current, previous*)
wenzelm@5828
   147
wenzelm@27576
   148
val toplevel = State (NONE, NONE);
wenzelm@5828
   149
wenzelm@27576
   150
fun is_toplevel (State (NONE, _)) = true
wenzelm@7732
   151
  | is_toplevel _ = false;
wenzelm@7732
   152
wenzelm@27576
   153
fun level (State (NONE, _)) = 0
wenzelm@37953
   154
  | level (State (SOME (Theory _), _)) = 0
wenzelm@37953
   155
  | level (State (SOME (Proof (prf, _)), _)) = Proof.level (Proof_Node.current prf)
wenzelm@37953
   156
  | level (State (SOME (SkipProof (d, _)), _)) = d + 1;   (*different notion of proof depth!*)
wenzelm@17076
   157
wenzelm@27576
   158
fun str_of_state (State (NONE, _)) = "at top level"
wenzelm@37953
   159
  | str_of_state (State (SOME (Theory (Context.Theory _, _)), _)) = "in theory mode"
wenzelm@37953
   160
  | str_of_state (State (SOME (Theory (Context.Proof _, _)), _)) = "in local theory mode"
wenzelm@37953
   161
  | str_of_state (State (SOME (Proof _), _)) = "in proof mode"
wenzelm@37953
   162
  | str_of_state (State (SOME (SkipProof _), _)) = "in skipped proof mode";
wenzelm@5946
   163
wenzelm@5946
   164
wenzelm@27576
   165
(* current node *)
wenzelm@5828
   166
wenzelm@27576
   167
fun node_of (State (NONE, _)) = raise UNDEF
wenzelm@37953
   168
  | node_of (State (SOME node, _)) = node;
wenzelm@5828
   169
wenzelm@18589
   170
fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
wenzelm@18589
   171
fun is_proof state = not (is_toplevel state) andalso is_some (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@23640
   203
fun print_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@24795
   208
  | SOME (SkipProof (_, (gthy, _))) => pretty_context gthy)
wenzelm@23640
   209
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@16815
   210
wenzelm@23640
   211
fun print_state prf_only state =
wenzelm@23701
   212
  (case try node_of state of
wenzelm@23701
   213
    NONE => []
wenzelm@23701
   214
  | SOME (Theory (gthy, _)) => if prf_only then [] else pretty_context gthy
wenzelm@23701
   215
  | SOME (Proof (prf, _)) =>
wenzelm@33390
   216
      Proof.pretty_state (Proof_Node.position prf) (Proof_Node.current prf)
wenzelm@27564
   217
  | SOME (SkipProof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)])
wenzelm@50201
   218
  |> 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@32738
   226
val quiet = Unsynchronized.ref false;
wenzelm@39513
   227
val debug = Runtime.debug;
wenzelm@32738
   228
val interact = Unsynchronized.ref false;
wenzelm@42012
   229
val timing = Unsynchronized.ref false;
wenzelm@32738
   230
val profiling = Unsynchronized.ref 0;
wenzelm@32738
   231
val skip_proofs = Unsynchronized.ref false;
wenzelm@16682
   232
wenzelm@33604
   233
fun program body =
wenzelm@33604
   234
 (body
wenzelm@31476
   235
  |> Runtime.controlled_execution
wenzelm@33604
   236
  |> Runtime.toplevel_error (Output.error_msg o ML_Compiler.exn_message)) ();
wenzelm@33604
   237
wenzelm@33604
   238
fun thread interrupts body =
wenzelm@33604
   239
  Thread.fork
wenzelm@39232
   240
    (((fn () => body () handle exn => if Exn.is_interrupt exn then () else reraise exn)
wenzelm@33604
   241
        |> Runtime.debugging
wenzelm@33604
   242
        |> Runtime.toplevel_error
wenzelm@40132
   243
          (fn exn =>
wenzelm@40132
   244
            Output.urgent_message ("## INTERNAL ERROR ##\n" ^ ML_Compiler.exn_message exn))),
wenzelm@37216
   245
      Simple_Thread.attributes interrupts);
wenzelm@20128
   246
wenzelm@5828
   247
wenzelm@27601
   248
(* node transactions -- maintaining stable checkpoints *)
wenzelm@7022
   249
wenzelm@31476
   250
exception FAILURE of state * exn;
wenzelm@31476
   251
wenzelm@6689
   252
local
wenzelm@6689
   253
wenzelm@30366
   254
fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
wenzelm@30366
   255
  | reset_presentation node = node;
wenzelm@30366
   256
wenzelm@26624
   257
fun is_draft_theory (Theory (gthy, _)) = Context.is_draft (Context.theory_of gthy)
wenzelm@26624
   258
  | is_draft_theory _ = false;
wenzelm@26624
   259
wenzelm@31476
   260
fun is_stale state = Context.is_stale (theory_of state) handle Runtime.UNDEF => false;
wenzelm@27601
   261
wenzelm@26624
   262
fun stale_error NONE = SOME (ERROR "Stale theory encountered after successful execution!")
wenzelm@26624
   263
  | stale_error some = some;
wenzelm@16815
   264
wenzelm@27576
   265
fun map_theory f (Theory (gthy, ctxt)) =
wenzelm@33671
   266
      Theory (Context.mapping f (Local_Theory.raw_theory f) gthy, ctxt)
wenzelm@27576
   267
  | map_theory _ node = node;
wenzelm@6689
   268
wenzelm@6689
   269
in
wenzelm@6689
   270
wenzelm@37953
   271
fun apply_transaction f g node =
wenzelm@20128
   272
  let
wenzelm@27576
   273
    val _ = is_draft_theory node andalso error "Illegal draft theory in toplevel state";
wenzelm@27576
   274
    val cont_node = reset_presentation node;
wenzelm@27576
   275
    val back_node = map_theory (Theory.checkpoint o Theory.copy) cont_node;
wenzelm@37953
   276
    fun state_error e nd = (State (SOME nd, SOME node), e);
wenzelm@6689
   277
wenzelm@20128
   278
    val (result, err) =
wenzelm@20128
   279
      cont_node
wenzelm@31476
   280
      |> Runtime.controlled_execution f
wenzelm@26624
   281
      |> map_theory Theory.checkpoint
wenzelm@26624
   282
      |> state_error NONE
wenzelm@26624
   283
      handle exn => state_error (SOME exn) cont_node;
wenzelm@26624
   284
wenzelm@26624
   285
    val (result', err') =
wenzelm@26624
   286
      if is_stale result then state_error (stale_error err) back_node
wenzelm@26624
   287
      else (result, err);
wenzelm@20128
   288
  in
wenzelm@26624
   289
    (case err' of
wenzelm@30366
   290
      NONE => tap g result'
wenzelm@26624
   291
    | SOME exn => raise FAILURE (result', exn))
wenzelm@20128
   292
  end;
wenzelm@6689
   293
wenzelm@43667
   294
val exit_transaction =
wenzelm@43667
   295
  apply_transaction
wenzelm@43667
   296
    (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (Theory.end_theory thy), NONE)
wenzelm@43667
   297
      | node => node) (K ())
wenzelm@43667
   298
  #> (fn State (node', _) => State (NONE, node'));
wenzelm@43667
   299
wenzelm@6689
   300
end;
wenzelm@6689
   301
wenzelm@6689
   302
wenzelm@6689
   303
(* primitive transitions *)
wenzelm@6689
   304
wenzelm@5828
   305
datatype trans =
wenzelm@44187
   306
  Init of unit -> theory |               (*init theory*)
wenzelm@37953
   307
  Exit |                                 (*formal exit of theory*)
wenzelm@37953
   308
  Keep of bool -> state -> unit |        (*peek at state*)
wenzelm@30366
   309
  Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
wenzelm@21958
   310
wenzelm@6689
   311
local
wenzelm@5828
   312
wenzelm@44187
   313
fun apply_tr _ (Init f) (State (NONE, _)) =
wenzelm@33727
   314
      State (SOME (Theory (Context.Theory
wenzelm@44186
   315
          (Theory.checkpoint (Runtime.controlled_execution f ())), NONE)), NONE)
wenzelm@43667
   316
  | apply_tr _ Exit (State (SOME (state as Theory (Context.Theory _, _)), _)) =
wenzelm@43667
   317
      exit_transaction state
wenzelm@32792
   318
  | apply_tr int (Keep f) state =
wenzelm@31476
   319
      Runtime.controlled_execution (fn x => tap (f int) x) state
wenzelm@32792
   320
  | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
wenzelm@32792
   321
      apply_transaction (fn x => f int x) g state
wenzelm@32792
   322
  | apply_tr _ _ _ = raise UNDEF;
wenzelm@5828
   323
wenzelm@32792
   324
fun apply_union _ [] state = raise FAILURE (state, UNDEF)
wenzelm@32792
   325
  | apply_union int (tr :: trs) state =
wenzelm@32792
   326
      apply_union int trs state
wenzelm@32792
   327
        handle Runtime.UNDEF => apply_tr int tr state
wenzelm@32792
   328
          | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
wenzelm@6689
   329
          | exn as FAILURE _ => raise exn
wenzelm@6689
   330
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   331
wenzelm@6689
   332
in
wenzelm@6689
   333
wenzelm@32792
   334
fun apply_trans int trs state = (apply_union int trs state, NONE)
skalberg@15531
   335
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   336
wenzelm@6689
   337
end;
wenzelm@5828
   338
wenzelm@5828
   339
wenzelm@5828
   340
(* datatype transition *)
wenzelm@5828
   341
wenzelm@5828
   342
datatype transition = Transition of
wenzelm@26621
   343
 {name: string,              (*command name*)
wenzelm@26621
   344
  pos: Position.T,           (*source position*)
wenzelm@26621
   345
  int_only: bool,            (*interactive-only*)
wenzelm@26621
   346
  print: bool,               (*print result state*)
wenzelm@26621
   347
  no_timing: bool,           (*suppress timing*)
wenzelm@51228
   348
  timing: Time.time,         (*prescient timing information*)
wenzelm@26621
   349
  trans: trans list};        (*primitive transitions (union)*)
wenzelm@5828
   350
wenzelm@51217
   351
fun make_transition (name, pos, int_only, print, no_timing, timing, trans) =
wenzelm@51217
   352
  Transition {name = name, pos = pos, int_only = int_only, print = print,
wenzelm@51217
   353
    no_timing = no_timing, timing = timing, trans = trans};
wenzelm@5828
   354
wenzelm@51217
   355
fun map_transition f (Transition {name, pos, int_only, print, no_timing, timing, trans}) =
wenzelm@51217
   356
  make_transition (f (name, pos, int_only, print, no_timing, timing, trans));
wenzelm@5828
   357
wenzelm@51228
   358
val empty = make_transition ("", Position.none, false, false, false, Time.zeroTime, []);
wenzelm@5828
   359
wenzelm@5828
   360
wenzelm@5828
   361
(* diagnostics *)
wenzelm@5828
   362
wenzelm@38888
   363
fun print_of (Transition {print, ...}) = print;
wenzelm@27427
   364
fun name_of (Transition {name, ...}) = name;
wenzelm@28105
   365
fun pos_of (Transition {pos, ...}) = pos;
wenzelm@5828
   366
wenzelm@48993
   367
fun command_msg msg tr = msg ^ "command " ^ quote (name_of tr) ^ Position.here (pos_of tr);
wenzelm@38875
   368
fun at_command tr = command_msg "At " tr;
wenzelm@5828
   369
wenzelm@5828
   370
fun type_error tr state =
wenzelm@18685
   371
  ERROR (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
wenzelm@5828
   372
wenzelm@5828
   373
wenzelm@5828
   374
(* modify transitions *)
wenzelm@5828
   375
wenzelm@51217
   376
fun name name = map_transition (fn (_, pos, int_only, print, no_timing, timing, trans) =>
wenzelm@51217
   377
  (name, pos, int_only, print, no_timing, timing, trans));
wenzelm@9010
   378
wenzelm@51217
   379
fun position pos = map_transition (fn (name, _, int_only, print, no_timing, timing, trans) =>
wenzelm@51217
   380
  (name, pos, int_only, print, no_timing, timing, trans));
wenzelm@5828
   381
wenzelm@51217
   382
fun interactive int_only = map_transition (fn (name, pos, _, print, no_timing, timing, trans) =>
wenzelm@51217
   383
  (name, pos, int_only, print, no_timing, timing, trans));
wenzelm@14923
   384
wenzelm@51217
   385
val no_timing = map_transition (fn (name, pos, int_only, print, _, timing, trans) =>
wenzelm@51217
   386
  (name, pos, int_only, print, true, timing, trans));
wenzelm@17363
   387
wenzelm@51217
   388
fun add_trans tr = map_transition (fn (name, pos, int_only, print, no_timing, timing, trans) =>
wenzelm@51217
   389
  (name, pos, int_only, print, no_timing, timing, tr :: trans));
wenzelm@16607
   390
wenzelm@51217
   391
val reset_trans = map_transition (fn (name, pos, int_only, print, no_timing, timing, _) =>
wenzelm@51217
   392
  (name, pos, int_only, print, no_timing, timing, []));
wenzelm@28433
   393
wenzelm@51217
   394
fun set_print print = map_transition (fn (name, pos, int_only, _, no_timing, timing, trans) =>
wenzelm@51217
   395
  (name, pos, int_only, print, no_timing, timing, trans));
wenzelm@28453
   396
wenzelm@28453
   397
val print = set_print true;
wenzelm@5828
   398
wenzelm@5828
   399
wenzelm@21007
   400
(* basic transitions *)
wenzelm@5828
   401
wenzelm@44187
   402
fun init_theory f = add_trans (Init f);
wenzelm@37977
   403
wenzelm@44187
   404
fun is_init (Transition {trans = [Init _], ...}) = true
wenzelm@44187
   405
  | is_init _ = false;
wenzelm@44187
   406
wenzelm@44187
   407
fun modify_init f tr = if is_init tr then init_theory f (reset_trans tr) else tr;
wenzelm@37977
   408
wenzelm@6689
   409
val exit = add_trans Exit;
wenzelm@7612
   410
val keep' = add_trans o Keep;
wenzelm@30366
   411
wenzelm@30366
   412
fun present_transaction f g = add_trans (Transaction (f, g));
wenzelm@30366
   413
fun transaction f = present_transaction f (K ());
wenzelm@5828
   414
wenzelm@7612
   415
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   416
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   417
wenzelm@27840
   418
fun ignored pos = empty |> name "<ignored>" |> position pos |> imperative I;
wenzelm@51268
   419
fun is_ignored tr = name_of tr = "<ignored>";
wenzelm@48772
   420
wenzelm@48772
   421
val malformed_name = "<malformed>";
wenzelm@27840
   422
fun malformed pos msg =
wenzelm@48772
   423
  empty |> name malformed_name |> position pos |> imperative (fn () => error msg);
wenzelm@48772
   424
fun is_malformed tr = name_of tr = malformed_name;
wenzelm@27840
   425
wenzelm@21007
   426
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@21007
   427
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@21007
   428
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@15668
   429
wenzelm@21007
   430
wenzelm@49012
   431
(* theory transitions *)
wenzelm@44304
   432
wenzelm@27601
   433
fun generic_theory f = transaction (fn _ =>
wenzelm@26491
   434
  (fn Theory (gthy, _) => Theory (f gthy, NONE)
wenzelm@26491
   435
    | _ => raise UNDEF));
wenzelm@26491
   436
wenzelm@27601
   437
fun theory' f = transaction (fn int =>
wenzelm@33725
   438
  (fn Theory (Context.Theory thy, _) =>
wenzelm@33725
   439
      let val thy' = thy
wenzelm@49012
   440
        |> Sign.new_group
wenzelm@49012
   441
        |> Theory.checkpoint
wenzelm@33725
   442
        |> f int
wenzelm@33725
   443
        |> Sign.reset_group;
wenzelm@33725
   444
      in Theory (Context.Theory thy', NONE) end
wenzelm@20963
   445
    | _ => raise UNDEF));
wenzelm@20963
   446
wenzelm@20963
   447
fun theory f = theory' (K f);
wenzelm@20963
   448
wenzelm@27601
   449
fun begin_local_theory begin f = transaction (fn _ =>
wenzelm@20963
   450
  (fn Theory (Context.Theory thy, _) =>
wenzelm@20963
   451
        let
wenzelm@20985
   452
          val lthy = f thy;
wenzelm@21294
   453
          val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
wenzelm@21294
   454
        in Theory (gthy, SOME lthy) end
wenzelm@20963
   455
    | _ => raise UNDEF));
wenzelm@17076
   456
wenzelm@27601
   457
val end_local_theory = transaction (fn _ =>
wenzelm@21294
   458
  (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
wenzelm@21007
   459
    | _ => raise UNDEF));
wenzelm@21007
   460
wenzelm@47069
   461
fun open_target f = transaction (fn _ =>
wenzelm@47069
   462
  (fn Theory (gthy, _) =>
wenzelm@47069
   463
        let val lthy = f gthy
wenzelm@47069
   464
        in Theory (Context.Proof lthy, SOME lthy) end
wenzelm@47069
   465
    | _ => raise UNDEF));
wenzelm@47069
   466
wenzelm@47069
   467
val close_target = transaction (fn _ =>
wenzelm@47069
   468
  (fn Theory (Context.Proof lthy, _) =>
wenzelm@47069
   469
        (case try Local_Theory.close_target lthy of
wenzelm@50739
   470
          SOME ctxt' =>
wenzelm@50739
   471
            let
wenzelm@50739
   472
              val gthy' =
wenzelm@50739
   473
                if can Local_Theory.assert ctxt'
wenzelm@50739
   474
                then Context.Proof ctxt'
wenzelm@50739
   475
                else Context.Theory (Proof_Context.theory_of ctxt');
wenzelm@50739
   476
            in Theory (gthy', SOME lthy) end
wenzelm@47069
   477
        | NONE => raise UNDEF)
wenzelm@47069
   478
    | _ => raise UNDEF));
wenzelm@47069
   479
wenzelm@47069
   480
wenzelm@21007
   481
local
wenzelm@21007
   482
wenzelm@30366
   483
fun local_theory_presentation loc f = present_transaction (fn int =>
wenzelm@21294
   484
  (fn Theory (gthy, _) =>
wenzelm@21294
   485
        let
wenzelm@49062
   486
          val (finish, lthy) = loc_begin loc gthy;
wenzelm@47274
   487
          val lthy' = lthy
wenzelm@49012
   488
            |> Local_Theory.new_group
wenzelm@33725
   489
            |> f int
wenzelm@33725
   490
            |> Local_Theory.reset_group;
wenzelm@21294
   491
        in Theory (finish lthy', SOME lthy') end
wenzelm@30366
   492
    | _ => raise UNDEF));
wenzelm@15668
   493
wenzelm@21007
   494
in
wenzelm@21007
   495
wenzelm@30366
   496
fun local_theory' loc f = local_theory_presentation loc f (K ());
wenzelm@29380
   497
fun local_theory loc f = local_theory' loc (K f);
wenzelm@30366
   498
fun present_local_theory loc = local_theory_presentation loc (K I);
wenzelm@18955
   499
wenzelm@21007
   500
end;
wenzelm@21007
   501
wenzelm@21007
   502
wenzelm@21007
   503
(* proof transitions *)
wenzelm@21007
   504
wenzelm@27601
   505
fun end_proof f = transaction (fn int =>
wenzelm@24795
   506
  (fn Proof (prf, (finish, _)) =>
wenzelm@33390
   507
        let val state = Proof_Node.current prf in
wenzelm@21007
   508
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   509
            let
wenzelm@21007
   510
              val ctxt' = f int state;
wenzelm@21007
   511
              val gthy' = finish ctxt';
wenzelm@21007
   512
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   513
          else raise UNDEF
wenzelm@21007
   514
        end
wenzelm@27564
   515
    | SkipProof (0, (gthy, _)) => Theory (gthy, NONE)
wenzelm@21007
   516
    | _ => raise UNDEF));
wenzelm@21007
   517
wenzelm@21294
   518
local
wenzelm@21294
   519
wenzelm@47274
   520
fun begin_proof init = transaction (fn int =>
wenzelm@21294
   521
  (fn Theory (gthy, _) =>
wenzelm@21294
   522
    let
wenzelm@47274
   523
      val (finish, prf) = init int gthy;
wenzelm@28433
   524
      val skip = ! skip_proofs;
wenzelm@40960
   525
      val (is_goal, no_skip) =
wenzelm@40960
   526
        (true, Proof.schematic_goal prf) handle ERROR _ => (false, true);
wenzelm@47274
   527
      val _ =
wenzelm@47274
   528
        if is_goal andalso skip andalso no_skip then
wenzelm@47274
   529
          warning "Cannot skip proof of schematic goal statement"
wenzelm@47274
   530
        else ();
wenzelm@21294
   531
    in
wenzelm@40960
   532
      if skip andalso not no_skip then
wenzelm@47274
   533
        SkipProof (0, (finish (Proof.global_skip_proof int prf), gthy))
wenzelm@47274
   534
      else Proof (Proof_Node.init prf, (finish, gthy))
wenzelm@21294
   535
    end
wenzelm@21294
   536
  | _ => raise UNDEF));
wenzelm@21294
   537
wenzelm@21294
   538
in
wenzelm@21294
   539
wenzelm@24780
   540
fun local_theory_to_proof' loc f = begin_proof
wenzelm@47274
   541
  (fn int => fn gthy =>
wenzelm@49062
   542
    let val (finish, lthy) = loc_begin loc gthy
wenzelm@49012
   543
    in (finish o Local_Theory.reset_group, f int (Local_Theory.new_group lthy)) end);
wenzelm@24780
   544
berghofe@24453
   545
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   546
wenzelm@21294
   547
fun theory_to_proof f = begin_proof
wenzelm@47274
   548
  (fn _ => fn gthy =>
wenzelm@47274
   549
    (Context.Theory o Sign.reset_group o Proof_Context.theory_of,
wenzelm@49062
   550
      (case gthy of
wenzelm@49012
   551
        Context.Theory thy => f (Theory.checkpoint (Sign.new_group thy))
wenzelm@49012
   552
      | _ => raise UNDEF)));
wenzelm@21294
   553
wenzelm@21294
   554
end;
wenzelm@21294
   555
wenzelm@27601
   556
val forget_proof = transaction (fn _ =>
wenzelm@21007
   557
  (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   558
    | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   559
    | _ => raise UNDEF));
wenzelm@21007
   560
wenzelm@30366
   561
val present_proof = present_transaction (fn _ =>
wenzelm@49062
   562
  (fn Proof (prf, x) => Proof (Proof_Node.apply I prf, x)
wenzelm@27564
   563
    | skip as SkipProof _ => skip
wenzelm@30366
   564
    | _ => raise UNDEF));
wenzelm@21177
   565
wenzelm@27601
   566
fun proofs' f = transaction (fn int =>
wenzelm@49062
   567
  (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
wenzelm@27564
   568
    | skip as SkipProof _ => skip
wenzelm@16815
   569
    | _ => raise UNDEF));
wenzelm@15668
   570
wenzelm@49863
   571
fun proof' f = proofs' ((Seq.single o Seq.Result) oo f);
wenzelm@17904
   572
val proofs = proofs' o K;
wenzelm@6689
   573
val proof = proof' o K;
wenzelm@16815
   574
wenzelm@27601
   575
fun actual_proof f = transaction (fn _ =>
wenzelm@21007
   576
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   577
    | _ => raise UNDEF));
wenzelm@16815
   578
wenzelm@27601
   579
fun skip_proof f = transaction (fn _ =>
wenzelm@21007
   580
  (fn SkipProof (h, x) => SkipProof (f h, x)
wenzelm@18563
   581
    | _ => raise UNDEF));
wenzelm@18563
   582
wenzelm@27601
   583
fun skip_proof_to_theory pred = transaction (fn _ =>
wenzelm@27564
   584
  (fn SkipProof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
wenzelm@33725
   585
    | _ => raise UNDEF));
wenzelm@5828
   586
wenzelm@5828
   587
wenzelm@5828
   588
wenzelm@5828
   589
(** toplevel transactions **)
wenzelm@5828
   590
wenzelm@27427
   591
(* identification *)
wenzelm@27427
   592
wenzelm@27427
   593
fun get_id (Transition {pos, ...}) = Position.get_id pos;
wenzelm@27427
   594
fun put_id id (tr as Transition {pos, ...}) = position (Position.put_id id pos) tr;
wenzelm@27427
   595
wenzelm@27427
   596
wenzelm@51228
   597
(* approximative identification within source file *)
wenzelm@51228
   598
wenzelm@51228
   599
fun approximative_id tr =
wenzelm@51228
   600
  let
wenzelm@51228
   601
    val name = name_of tr;
wenzelm@51228
   602
    val pos = pos_of tr;
wenzelm@51228
   603
  in
wenzelm@51228
   604
    (case (Position.file_of pos, Position.offset_of pos) of
wenzelm@51228
   605
      (SOME file, SOME offset) => SOME {file = file, offset = offset, name = name}
wenzelm@51228
   606
    | _ => NONE)
wenzelm@51228
   607
  end;
wenzelm@51228
   608
wenzelm@51228
   609
wenzelm@25960
   610
(* thread position *)
wenzelm@25799
   611
wenzelm@25960
   612
fun setmp_thread_position (Transition {pos, ...}) f x =
wenzelm@25819
   613
  Position.setmp_thread_data pos f x;
wenzelm@25799
   614
wenzelm@27606
   615
fun status tr m =
wenzelm@43665
   616
  setmp_thread_position tr (fn () => Output.status (Markup.markup_only m)) ();
wenzelm@27606
   617
wenzelm@25799
   618
wenzelm@28095
   619
(* post-transition hooks *)
wenzelm@28095
   620
wenzelm@37905
   621
local
wenzelm@37905
   622
  val hooks = Unsynchronized.ref ([]: (transition -> state -> state -> unit) list);
wenzelm@37905
   623
in
wenzelm@28095
   624
wenzelm@32738
   625
fun add_hook f = CRITICAL (fn () => Unsynchronized.change hooks (cons f));
wenzelm@33223
   626
fun get_hooks () = ! hooks;
wenzelm@28095
   627
wenzelm@28095
   628
end;
wenzelm@28095
   629
wenzelm@28095
   630
wenzelm@5828
   631
(* apply transitions *)
wenzelm@5828
   632
wenzelm@51217
   633
fun get_timing (Transition {timing, ...}) = timing;
wenzelm@51217
   634
fun put_timing timing = map_transition (fn (name, pos, int_only, print, no_timing, _, trans) =>
wenzelm@51217
   635
  (name, pos, int_only, print, no_timing, timing, trans));
wenzelm@51217
   636
wenzelm@6664
   637
local
wenzelm@6664
   638
wenzelm@51241
   639
fun timing_message tr (t: Timing.timing) =
wenzelm@51282
   640
  if Timing.is_relevant_time (#elapsed t) then
wenzelm@51228
   641
    (case approximative_id tr of
wenzelm@51228
   642
      SOME id =>
wenzelm@51228
   643
        (Output.protocol_message
wenzelm@51228
   644
          (Markup.command_timing :: Markup.command_timing_properties id (#elapsed t)) ""
wenzelm@51228
   645
        handle Fail _ => ())
wenzelm@51228
   646
    | NONE => ())
wenzelm@51216
   647
  else ();
wenzelm@51216
   648
wenzelm@32792
   649
fun app int (tr as Transition {trans, print, no_timing, ...}) =
wenzelm@25819
   650
  setmp_thread_position tr (fn state =>
wenzelm@25799
   651
    let
wenzelm@25799
   652
      fun do_timing f x = (warning (command_msg "" tr); timeap f x);
wenzelm@25799
   653
      fun do_profiling f x = profile (! profiling) f x;
wenzelm@25799
   654
wenzelm@51216
   655
      val start = Timing.start ();
wenzelm@51216
   656
wenzelm@26256
   657
      val (result, status) =
wenzelm@37905
   658
         state |>
wenzelm@37905
   659
          (apply_trans int trans
wenzelm@37905
   660
            |> (! profiling > 0 andalso not no_timing) ? do_profiling
wenzelm@37905
   661
            |> (! profiling > 0 orelse ! timing andalso not no_timing) ? do_timing);
wenzelm@26256
   662
wenzelm@26621
   663
      val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
wenzelm@51216
   664
wenzelm@51216
   665
      val _ = timing_message tr (Timing.result start);
wenzelm@26256
   666
    in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);
wenzelm@6664
   667
wenzelm@6664
   668
in
wenzelm@5828
   669
wenzelm@26602
   670
fun transition int tr st =
wenzelm@28095
   671
  let
wenzelm@28095
   672
    val hooks = get_hooks ();
wenzelm@28103
   673
    fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
wenzelm@28095
   674
wenzelm@28095
   675
    val ctxt = try context_of st;
wenzelm@28095
   676
    val res =
wenzelm@28095
   677
      (case app int tr st of
wenzelm@38888
   678
        (_, SOME Runtime.TERMINATE) => NONE
wenzelm@38888
   679
      | (st', SOME (Runtime.EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
wenzelm@31476
   680
      | (st', SOME exn) => SOME (st', SOME (Runtime.exn_context ctxt exn, at_command tr))
wenzelm@28103
   681
      | (st', NONE) => SOME (st', NONE));
wenzelm@28103
   682
    val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
wenzelm@28095
   683
  in res end;
wenzelm@6664
   684
wenzelm@6664
   685
end;
wenzelm@5828
   686
wenzelm@5828
   687
wenzelm@28425
   688
(* nested commands *)
wenzelm@5828
   689
wenzelm@28425
   690
fun command tr st =
wenzelm@27576
   691
  (case transition (! interact) tr st of
wenzelm@28425
   692
    SOME (st', NONE) => st'
wenzelm@39285
   693
  | SOME (_, SOME (exn, info)) =>
wenzelm@39285
   694
      if Exn.is_interrupt exn then reraise exn else raise Runtime.EXCURSION_FAIL (exn, info)
wenzelm@38888
   695
  | NONE => raise Runtime.EXCURSION_FAIL (Runtime.TERMINATE, at_command tr));
wenzelm@27576
   696
wenzelm@28433
   697
wenzelm@46959
   698
(* scheduled proof result *)
wenzelm@28433
   699
wenzelm@47417
   700
structure Result = Proof_Data
wenzelm@28974
   701
(
wenzelm@47417
   702
  type T = (transition * state) list future;
wenzelm@47417
   703
  val empty: T = Future.value [];
wenzelm@47417
   704
  fun init _ = empty;
wenzelm@28974
   705
);
wenzelm@28974
   706
wenzelm@51278
   707
fun priority trs =
wenzelm@51278
   708
  let val estimate = fold (curry Time.+ o get_timing) trs Time.zeroTime in
wenzelm@51278
   709
    if estimate = Time.zeroTime then ~1
wenzelm@51278
   710
    else Int.min (Real.floor (Real.max (Math.log10 (Time.toReal estimate), ~3.0)) - 3, ~1)
wenzelm@51278
   711
  end;
wenzelm@51278
   712
wenzelm@51278
   713
fun atom_result tr st =
wenzelm@51278
   714
  let val st' = command tr st
wenzelm@51278
   715
  in ((tr, st'), st') end;
wenzelm@51278
   716
wenzelm@51273
   717
fun element_result (Thy_Syntax.Element (head_tr, opt_proof)) st =
wenzelm@51273
   718
  let
wenzelm@51273
   719
    val proof_trs =
wenzelm@51273
   720
      (case opt_proof of
wenzelm@51273
   721
        NONE => []
wenzelm@51273
   722
      | SOME (a, b) => (maps Thy_Syntax.flat_element a @ [b]) |> filter_out is_ignored);
wenzelm@51273
   723
wenzelm@51278
   724
    val (_, st') = atom_result head_tr st;
wenzelm@51273
   725
  in
wenzelm@51278
   726
    if not (Goal.future_enabled ()) orelse is_ignored head_tr orelse
wenzelm@51274
   727
      null proof_trs orelse not (can proof_of st') orelse Proof.is_relevant (proof_of st')
wenzelm@48633
   728
    then
wenzelm@51273
   729
      let val (results, st'') = fold_map atom_result proof_trs st'
wenzelm@51273
   730
      in (Future.value (if is_ignored head_tr then results else (head_tr, st') :: results), st'') end
wenzelm@48633
   731
    else
wenzelm@48633
   732
      let
wenzelm@48633
   733
        val (body_trs, end_tr) = split_last proof_trs;
wenzelm@48633
   734
        val finish = Context.Theory o Proof_Context.theory_of;
wenzelm@28974
   735
wenzelm@48633
   736
        val future_proof = Proof.global_future_proof
wenzelm@48633
   737
          (fn prf =>
wenzelm@51278
   738
            Goal.fork_name "Toplevel.future_proof" (priority proof_trs)
wenzelm@48633
   739
              (fn () =>
wenzelm@48633
   740
                let val (result, result_state) =
wenzelm@48633
   741
                  (case st' of State (SOME (Proof (_, (_, orig_gthy))), prev)
wenzelm@48633
   742
                    => State (SOME (Proof (Proof_Node.init prf, (finish, orig_gthy))), prev))
wenzelm@51273
   743
                  |> fold_map atom_result body_trs ||> command end_tr;
wenzelm@48633
   744
                in (result, presentation_context_of result_state) end))
wenzelm@48633
   745
          #-> Result.put;
wenzelm@28974
   746
wenzelm@48633
   747
        val st'' = st'
wenzelm@51273
   748
          |> command (head_tr |> set_print false |> reset_trans |> end_proof (K future_proof));
wenzelm@48633
   749
        val result =
wenzelm@48633
   750
          Result.get (presentation_context_of st'')
wenzelm@51273
   751
          |> Future.map (fn body => (head_tr, st') :: body @ [(end_tr, st'')]);
wenzelm@28974
   752
wenzelm@48633
   753
      in (result, st'') end
wenzelm@48633
   754
  end;
wenzelm@28433
   755
wenzelm@6664
   756
end;