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