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