src/Pure/Isar/toplevel.ML
author wenzelm
Tue Jul 15 15:46:41 2008 +0200 (2008-07-15 ago)
changeset 27606 82399f3a8ca8
parent 27603 69c00b56fb36
child 27840 e9483ef084cc
permissions -rw-r--r--
support for command status;
wenzelm@5828
     1
(*  Title:      Pure/Isar/toplevel.ML
wenzelm@5828
     2
    ID:         $Id$
wenzelm@5828
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5828
     4
wenzelm@26602
     5
Isabelle/Isar toplevel transactions.
wenzelm@5828
     6
*)
wenzelm@5828
     7
wenzelm@5828
     8
signature TOPLEVEL =
wenzelm@5828
     9
sig
wenzelm@19063
    10
  exception UNDEF
wenzelm@20963
    11
  type generic_theory
wenzelm@18589
    12
  type node
wenzelm@20963
    13
  val theory_node: node -> generic_theory option
wenzelm@27564
    14
  val proof_node: node -> ProofNode.T option
wenzelm@20963
    15
  val cases_node: (generic_theory -> 'a) -> (Proof.state -> 'a) -> node -> 'a
wenzelm@20963
    16
  val presentation_context: node option -> xstring option -> Proof.context
wenzelm@5828
    17
  type state
wenzelm@26602
    18
  val toplevel: state
wenzelm@7732
    19
  val is_toplevel: state -> bool
wenzelm@18589
    20
  val is_theory: state -> bool
wenzelm@18589
    21
  val is_proof: state -> bool
wenzelm@17076
    22
  val level: state -> int
wenzelm@27576
    23
  val previous_node_of: state -> node option
wenzelm@5828
    24
  val node_of: state -> node
wenzelm@20963
    25
  val node_case: (generic_theory -> 'a) -> (Proof.state -> 'a) -> state -> 'a
wenzelm@21506
    26
  val context_of: state -> Proof.context
wenzelm@22089
    27
  val generic_theory_of: state -> generic_theory
wenzelm@5828
    28
  val theory_of: state -> theory
wenzelm@5828
    29
  val proof_of: state -> Proof.state
wenzelm@18589
    30
  val proof_position_of: state -> int
wenzelm@21007
    31
  val enter_proof_body: state -> Proof.state
wenzelm@16815
    32
  val print_state_context: state -> unit
wenzelm@16815
    33
  val print_state: bool -> state -> unit
wenzelm@16682
    34
  val quiet: bool ref
wenzelm@16682
    35
  val debug: bool ref
wenzelm@17321
    36
  val interact: bool ref
wenzelm@16682
    37
  val timing: bool ref
wenzelm@16682
    38
  val profiling: int ref
wenzelm@16815
    39
  val skip_proofs: bool ref
wenzelm@5828
    40
  exception TERMINATE
wenzelm@27583
    41
  exception TOPLEVEL_ERROR
wenzelm@26256
    42
  exception CONTEXT of Proof.context * exn
wenzelm@20128
    43
  val exn_message: exn -> string
wenzelm@20128
    44
  val program: (unit -> 'a) -> 'a
wenzelm@16682
    45
  type transition
wenzelm@5828
    46
  val empty: transition
wenzelm@27441
    47
  val init_of: transition -> string option
wenzelm@27427
    48
  val name_of: transition -> string
wenzelm@27500
    49
  val str_of: transition -> string
wenzelm@5828
    50
  val name: string -> transition -> transition
wenzelm@5828
    51
  val position: Position.T -> transition -> transition
wenzelm@5828
    52
  val interactive: bool -> transition -> transition
wenzelm@5828
    53
  val print: transition -> transition
wenzelm@9010
    54
  val no_timing: transition -> transition
wenzelm@27576
    55
  val init_theory: string -> (bool -> theory) -> (theory -> unit) -> transition -> transition
wenzelm@6689
    56
  val exit: transition -> transition
wenzelm@5828
    57
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    58
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@5828
    59
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@5828
    60
  val theory: (theory -> theory) -> transition -> transition
wenzelm@26491
    61
  val generic_theory: (generic_theory -> generic_theory) -> transition -> transition
wenzelm@7612
    62
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@20985
    63
  val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
wenzelm@21007
    64
  val end_local_theory: transition -> transition
wenzelm@20963
    65
  val local_theory: xstring option -> (local_theory -> local_theory) -> transition -> transition
wenzelm@20963
    66
  val present_local_theory: xstring option -> (bool -> node -> unit) -> transition -> transition
berghofe@24453
    67
  val local_theory_to_proof': xstring option -> (bool -> local_theory -> Proof.state) ->
berghofe@24453
    68
    transition -> transition
wenzelm@21007
    69
  val local_theory_to_proof: xstring option -> (local_theory -> Proof.state) ->
wenzelm@21007
    70
    transition -> transition
wenzelm@17363
    71
  val theory_to_proof: (theory -> Proof.state) -> transition -> transition
wenzelm@21007
    72
  val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition
wenzelm@21007
    73
  val forget_proof: transition -> transition
wenzelm@21177
    74
  val present_proof: (bool -> node -> unit) -> transition -> transition
wenzelm@21177
    75
  val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@17904
    76
  val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
wenzelm@21177
    77
  val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@21177
    78
  val proof: (Proof.state -> Proof.state) -> transition -> transition
wenzelm@27564
    79
  val actual_proof: (ProofNode.T -> ProofNode.T) -> transition -> transition
wenzelm@27564
    80
  val skip_proof: (int -> int) -> transition -> transition
wenzelm@17904
    81
  val skip_proof_to_theory: (int -> bool) -> transition -> transition
wenzelm@27427
    82
  val get_id: transition -> string option
wenzelm@27427
    83
  val put_id: string -> transition -> transition
wenzelm@9512
    84
  val unknown_theory: transition -> transition
wenzelm@9512
    85
  val unknown_proof: transition -> transition
wenzelm@9512
    86
  val unknown_context: transition -> transition
wenzelm@27606
    87
  val status: transition -> Markup.T -> unit
wenzelm@26602
    88
  val error_msg: transition -> exn * string -> unit
wenzelm@26602
    89
  val transition: bool -> transition -> state -> (state * (exn * string) option) option
wenzelm@27601
    90
  val commit_exit: transition
wenzelm@27601
    91
  val excursion: transition list -> unit
wenzelm@17076
    92
  val present_excursion: (transition * (state -> state -> 'a -> 'a)) list -> 'a -> 'a
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@19063
   100
exception UNDEF;
wenzelm@19063
   101
wenzelm@19063
   102
wenzelm@21294
   103
(* local theory wrappers *)
wenzelm@5828
   104
wenzelm@20963
   105
type generic_theory = Context.generic;    (*theory or local_theory*)
wenzelm@20963
   106
wenzelm@25292
   107
val loc_init = TheoryTarget.context;
wenzelm@21294
   108
val loc_exit = ProofContext.theory_of o LocalTheory.exit;
wenzelm@21294
   109
wenzelm@25292
   110
fun loc_begin loc (Context.Theory thy) = loc_init (the_default "-" loc) thy
wenzelm@21294
   111
  | loc_begin NONE (Context.Proof lthy) = lthy
haftmann@25269
   112
  | loc_begin (SOME loc) (Context.Proof lthy) = loc_init loc (loc_exit lthy);
wenzelm@21294
   113
wenzelm@21294
   114
fun loc_finish _ (Context.Theory _) = Context.Theory o loc_exit
wenzelm@21294
   115
  | loc_finish NONE (Context.Proof _) = Context.Proof o LocalTheory.restore
wenzelm@25292
   116
  | loc_finish (SOME _) (Context.Proof lthy) = fn lthy' =>
wenzelm@25292
   117
      Context.Proof (LocalTheory.reinit (LocalTheory.raw_theory (K (loc_exit lthy')) lthy));
wenzelm@21294
   118
wenzelm@21294
   119
wenzelm@21958
   120
(* datatype node *)
wenzelm@21294
   121
wenzelm@5828
   122
datatype node =
wenzelm@27576
   123
  Theory of generic_theory * Proof.context option
wenzelm@27576
   124
    (*theory with presentation context*) |
wenzelm@27576
   125
  Proof of ProofNode.T * ((Proof.context -> generic_theory) * generic_theory)
wenzelm@27576
   126
    (*proof node, finish, original theory*) |
wenzelm@27564
   127
  SkipProof of int * (generic_theory * generic_theory);
wenzelm@27564
   128
    (*proof depth, resulting theory, original theory*)
wenzelm@5828
   129
wenzelm@22056
   130
val the_global_theory = fn Theory (Context.Theory thy, _) => thy | _ => raise UNDEF;
wenzelm@20963
   131
val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
wenzelm@18589
   132
val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
wenzelm@18589
   133
wenzelm@20963
   134
fun cases_node f _ (Theory (gthy, _)) = f gthy
wenzelm@27564
   135
  | cases_node _ g (Proof (prf, _)) = g (ProofNode.current prf)
wenzelm@21007
   136
  | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy;
wenzelm@19063
   137
wenzelm@20963
   138
fun presentation_context (SOME (Theory (_, SOME ctxt))) NONE = ctxt
wenzelm@20963
   139
  | presentation_context (SOME node) NONE = cases_node Context.proof_of Proof.context_of node
wenzelm@20963
   140
  | presentation_context (SOME node) (SOME loc) =
haftmann@25269
   141
      loc_init loc (cases_node Context.theory_of Proof.theory_of node)
wenzelm@20963
   142
  | presentation_context NONE _ = raise UNDEF;
wenzelm@19063
   143
wenzelm@26624
   144
fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
wenzelm@26624
   145
  | reset_presentation node = node;
wenzelm@26624
   146
wenzelm@21958
   147
wenzelm@21958
   148
(* datatype state *)
wenzelm@21958
   149
wenzelm@27601
   150
type node_info = node * (theory -> unit);  (*node with exit operation*)
wenzelm@27601
   151
datatype state = State of node_info option * node_info option;  (*current, previous*)
wenzelm@5828
   152
wenzelm@27576
   153
val toplevel = State (NONE, NONE);
wenzelm@5828
   154
wenzelm@27576
   155
fun is_toplevel (State (NONE, _)) = true
wenzelm@7732
   156
  | is_toplevel _ = false;
wenzelm@7732
   157
wenzelm@27576
   158
fun level (State (NONE, _)) = 0
wenzelm@27576
   159
  | level (State (SOME (Theory _, _), _)) = 0
wenzelm@27576
   160
  | level (State (SOME (Proof (prf, _), _), _)) = Proof.level (ProofNode.current prf)
wenzelm@27576
   161
  | level (State (SOME (SkipProof (d, _), _), _)) = d + 1;   (*different notion of proof depth!*)
wenzelm@17076
   162
wenzelm@27576
   163
fun str_of_state (State (NONE, _)) = "at top level"
wenzelm@27576
   164
  | str_of_state (State (SOME (Theory (Context.Theory _, _), _), _)) = "in theory mode"
wenzelm@27576
   165
  | str_of_state (State (SOME (Theory (Context.Proof _, _), _), _)) = "in local theory mode"
wenzelm@27576
   166
  | str_of_state (State (SOME (Proof _, _), _)) = "in proof mode"
wenzelm@27576
   167
  | str_of_state (State (SOME (SkipProof _, _), _)) = "in skipped proof mode";
wenzelm@5946
   168
wenzelm@5946
   169
wenzelm@27576
   170
(* current node *)
wenzelm@5828
   171
wenzelm@27603
   172
fun previous_node_of (State (_, prev)) = Option.map #1 prev;
wenzelm@6689
   173
wenzelm@27576
   174
fun node_of (State (NONE, _)) = raise UNDEF
wenzelm@27576
   175
  | node_of (State (SOME (node, _), _)) = node;
wenzelm@5828
   176
wenzelm@18589
   177
fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
wenzelm@18589
   178
fun is_proof state = not (is_toplevel state) andalso is_some (proof_node (node_of state));
wenzelm@18589
   179
wenzelm@19063
   180
fun node_case f g state = cases_node f g (node_of state);
wenzelm@5828
   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@27564
   189
    Proof (prf, _) => ProofNode.position prf
wenzelm@18589
   190
  | _ => raise UNDEF);
wenzelm@6664
   191
wenzelm@21007
   192
val enter_proof_body = node_case (Proof.init o Context.proof_of) Proof.enter_forward;
wenzelm@5828
   193
wenzelm@5828
   194
wenzelm@16815
   195
(* print state *)
wenzelm@16815
   196
wenzelm@25292
   197
val pretty_context = LocalTheory.pretty o Context.cases (TheoryTarget.init NONE) I;
wenzelm@16815
   198
wenzelm@23640
   199
fun print_state_context state =
wenzelm@24795
   200
  (case try node_of state of
wenzelm@21506
   201
    NONE => []
wenzelm@24795
   202
  | SOME (Theory (gthy, _)) => pretty_context gthy
wenzelm@24795
   203
  | SOME (Proof (_, (_, gthy))) => pretty_context gthy
wenzelm@24795
   204
  | SOME (SkipProof (_, (gthy, _))) => pretty_context gthy)
wenzelm@23640
   205
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@16815
   206
wenzelm@23640
   207
fun print_state prf_only state =
wenzelm@23701
   208
  (case try node_of state of
wenzelm@23701
   209
    NONE => []
wenzelm@23701
   210
  | SOME (Theory (gthy, _)) => if prf_only then [] else pretty_context gthy
wenzelm@23701
   211
  | SOME (Proof (prf, _)) =>
wenzelm@27564
   212
      Proof.pretty_state (ProofNode.position prf) (ProofNode.current prf)
wenzelm@27564
   213
  | SOME (SkipProof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)])
wenzelm@23701
   214
  |> Pretty.markup_chunks Markup.state |> Pretty.writeln;
wenzelm@16815
   215
wenzelm@16815
   216
wenzelm@15668
   217
wenzelm@5828
   218
(** toplevel transitions **)
wenzelm@5828
   219
wenzelm@16682
   220
val quiet = ref false;
wenzelm@22135
   221
val debug = Output.debugging;
wenzelm@17321
   222
val interact = ref false;
wenzelm@16682
   223
val timing = Output.timing;
wenzelm@16682
   224
val profiling = ref 0;
wenzelm@16815
   225
val skip_proofs = ref false;
wenzelm@16682
   226
wenzelm@5828
   227
exception TERMINATE;
wenzelm@7022
   228
exception EXCURSION_FAIL of exn * string;
wenzelm@6689
   229
exception FAILURE of state * exn;
wenzelm@24055
   230
exception TOPLEVEL_ERROR;
wenzelm@5828
   231
wenzelm@20128
   232
wenzelm@20128
   233
(* print exceptions *)
wenzelm@20128
   234
wenzelm@26256
   235
exception CONTEXT of Proof.context * exn;
wenzelm@26256
   236
wenzelm@26256
   237
fun exn_context NONE exn = exn
wenzelm@26256
   238
  | exn_context (SOME ctxt) exn = CONTEXT (ctxt, exn);
wenzelm@26256
   239
wenzelm@20128
   240
local
wenzelm@20128
   241
wenzelm@26256
   242
fun if_context NONE _ _ = []
wenzelm@26256
   243
  | if_context (SOME ctxt) f xs = map (f ctxt) xs;
wenzelm@20128
   244
wenzelm@20128
   245
fun raised name [] = "exception " ^ name ^ " raised"
wenzelm@20128
   246
  | raised name [msg] = "exception " ^ name ^ " raised: " ^ msg
wenzelm@20128
   247
  | raised name msgs = cat_lines (("exception " ^ name ^ " raised:") :: msgs);
wenzelm@20128
   248
wenzelm@20128
   249
in
wenzelm@20128
   250
wenzelm@26256
   251
fun exn_message e =
wenzelm@26256
   252
  let
wenzelm@26256
   253
    val detailed = ! debug;
wenzelm@26256
   254
wenzelm@26256
   255
    fun exn_msg _ (CONTEXT (ctxt, exn)) = exn_msg (SOME ctxt) exn
wenzelm@26256
   256
      | exn_msg ctxt (Exn.EXCEPTIONS (exns, "")) = cat_lines (map (exn_msg ctxt) exns)
wenzelm@26256
   257
      | exn_msg ctxt (Exn.EXCEPTIONS (exns, msg)) = cat_lines (map (exn_msg ctxt) exns @ [msg])
wenzelm@26293
   258
      | exn_msg ctxt (EXCURSION_FAIL (exn, loc)) =
wenzelm@26293
   259
          exn_msg ctxt exn ^ Markup.markup Markup.location ("\n" ^ loc)
wenzelm@26256
   260
      | exn_msg _ TERMINATE = "Exit."
wenzelm@26256
   261
      | exn_msg _ Interrupt = "Interrupt."
wenzelm@26256
   262
      | exn_msg _ TimeLimit.TimeOut = "Timeout."
wenzelm@26256
   263
      | exn_msg _ TOPLEVEL_ERROR = "Error."
wenzelm@26256
   264
      | exn_msg _ (SYS_ERROR msg) = "## SYSTEM ERROR ##\n" ^ msg
wenzelm@26256
   265
      | exn_msg _ (ERROR msg) = msg
wenzelm@26256
   266
      | exn_msg _ (Fail msg) = raised "Fail" [msg]
wenzelm@26256
   267
      | exn_msg _ (THEORY (msg, thys)) =
wenzelm@26256
   268
          raised "THEORY" (msg :: (if detailed then map Context.str_of_thy thys else []))
wenzelm@26256
   269
      | exn_msg _ (Syntax.AST (msg, asts)) = raised "AST" (msg ::
wenzelm@26256
   270
            (if detailed then map (Pretty.string_of o Syntax.pretty_ast) asts else []))
wenzelm@26256
   271
      | exn_msg ctxt (TYPE (msg, Ts, ts)) = raised "TYPE" (msg ::
wenzelm@26256
   272
            (if detailed then
wenzelm@26256
   273
              if_context ctxt Syntax.string_of_typ Ts @ if_context ctxt Syntax.string_of_term ts
wenzelm@26256
   274
             else []))
wenzelm@26256
   275
      | exn_msg ctxt (TERM (msg, ts)) = raised "TERM" (msg ::
wenzelm@26256
   276
            (if detailed then if_context ctxt Syntax.string_of_term ts else []))
wenzelm@26256
   277
      | exn_msg ctxt (THM (msg, i, thms)) = raised ("THM " ^ string_of_int i) (msg ::
wenzelm@26256
   278
            (if detailed then if_context ctxt ProofContext.string_of_thm thms else []))
wenzelm@26256
   279
      | exn_msg _ exn = raised (General.exnMessage exn) []
wenzelm@26256
   280
  in exn_msg NONE e end;
wenzelm@26256
   281
wenzelm@20128
   282
end;
wenzelm@20128
   283
wenzelm@20128
   284
wenzelm@20128
   285
(* controlled execution *)
wenzelm@20128
   286
wenzelm@20128
   287
local
wenzelm@20128
   288
wenzelm@18685
   289
fun debugging f x =
wenzelm@23940
   290
  if ! debug then exception_trace (fn () => f x)
wenzelm@18685
   291
  else f x;
wenzelm@18685
   292
wenzelm@26256
   293
fun toplevel_error f x =
wenzelm@26256
   294
  let val ctxt = try ML_Context.the_local_context () in
wenzelm@26256
   295
    f x handle exn =>
wenzelm@26256
   296
      (Output.error_msg (exn_message (exn_context ctxt exn)); raise TOPLEVEL_ERROR)
wenzelm@26256
   297
  end;
wenzelm@24055
   298
wenzelm@20128
   299
in
wenzelm@20128
   300
wenzelm@20128
   301
fun controlled_execution f =
wenzelm@20128
   302
  f
wenzelm@20128
   303
  |> debugging
wenzelm@24055
   304
  |> interruptible;
wenzelm@20128
   305
wenzelm@20128
   306
fun program f =
wenzelm@24055
   307
 (f
wenzelm@24055
   308
  |> debugging
wenzelm@24055
   309
  |> toplevel_error) ();
wenzelm@20128
   310
wenzelm@20128
   311
end;
wenzelm@20128
   312
wenzelm@5828
   313
wenzelm@27601
   314
(* node transactions -- maintaining stable checkpoints *)
wenzelm@7022
   315
wenzelm@6689
   316
local
wenzelm@6689
   317
wenzelm@26624
   318
fun is_draft_theory (Theory (gthy, _)) = Context.is_draft (Context.theory_of gthy)
wenzelm@26624
   319
  | is_draft_theory _ = false;
wenzelm@26624
   320
wenzelm@27601
   321
fun is_stale state = Context.is_stale (theory_of state) handle UNDEF => false;
wenzelm@27601
   322
wenzelm@26624
   323
fun stale_error NONE = SOME (ERROR "Stale theory encountered after successful execution!")
wenzelm@26624
   324
  | stale_error some = some;
wenzelm@16815
   325
wenzelm@27576
   326
fun map_theory f (Theory (gthy, ctxt)) =
wenzelm@27576
   327
      Theory (Context.mapping f (LocalTheory.raw_theory f) gthy, ctxt)
wenzelm@27576
   328
  | map_theory _ node = node;
wenzelm@6689
   329
wenzelm@6689
   330
in
wenzelm@6689
   331
wenzelm@27601
   332
fun apply_transaction pos f (node, exit) =
wenzelm@20128
   333
  let
wenzelm@27576
   334
    val _ = is_draft_theory node andalso error "Illegal draft theory in toplevel state";
wenzelm@27576
   335
    val cont_node = reset_presentation node;
wenzelm@27576
   336
    val back_node = map_theory (Theory.checkpoint o Theory.copy) cont_node;
wenzelm@27601
   337
    fun state_error e nd = (State (SOME (nd, exit), SOME (node, exit)), e);
wenzelm@6689
   338
wenzelm@20128
   339
    val (result, err) =
wenzelm@20128
   340
      cont_node
wenzelm@27576
   341
      |> controlled_execution f
wenzelm@26624
   342
      |> map_theory Theory.checkpoint
wenzelm@26624
   343
      |> state_error NONE
wenzelm@26624
   344
      handle exn => state_error (SOME exn) cont_node;
wenzelm@26624
   345
wenzelm@26624
   346
    val (result', err') =
wenzelm@26624
   347
      if is_stale result then state_error (stale_error err) back_node
wenzelm@26624
   348
      else (result, err);
wenzelm@20128
   349
  in
wenzelm@26624
   350
    (case err' of
wenzelm@26624
   351
      NONE => result'
wenzelm@26624
   352
    | SOME exn => raise FAILURE (result', exn))
wenzelm@20128
   353
  end;
wenzelm@6689
   354
wenzelm@6689
   355
end;
wenzelm@6689
   356
wenzelm@6689
   357
wenzelm@6689
   358
(* primitive transitions *)
wenzelm@6689
   359
wenzelm@5828
   360
datatype trans =
wenzelm@27576
   361
  Init of string * (bool -> theory) * (theory -> unit) | (*theory name, init, exit*)
wenzelm@27576
   362
  Exit |                                         (*formal exit of theory -- without committing*)
wenzelm@27576
   363
  CommitExit |                                   (*exit and commit final theory*)
wenzelm@27576
   364
  Keep of bool -> state -> unit |                (*peek at state*)
wenzelm@27576
   365
  Transaction of bool -> node -> node;           (*node transaction*)
wenzelm@21958
   366
wenzelm@6689
   367
local
wenzelm@5828
   368
wenzelm@27576
   369
fun apply_tr int _ (Init (_, f, exit)) (State (NONE, _)) =
wenzelm@27576
   370
      State (SOME (Theory (Context.Theory (Theory.checkpoint (f int)), NONE), exit), NONE)
wenzelm@27603
   371
  | apply_tr _ _ Exit (State (prev as SOME (Theory (Context.Theory _, _), _), _)) =
wenzelm@27603
   372
      State (NONE, prev)
wenzelm@27601
   373
  | apply_tr _ _ CommitExit (State (NONE, SOME (Theory (Context.Theory thy, _), exit))) =
wenzelm@27576
   374
      (controlled_execution exit thy; toplevel)
wenzelm@27576
   375
  | apply_tr int _ (Keep f) state =
wenzelm@27576
   376
      controlled_execution (fn x => tap (f int) x) state
wenzelm@27576
   377
  | apply_tr int pos (Transaction f) (State (SOME state, _)) =
wenzelm@27601
   378
      apply_transaction pos (fn x => f int x) state
wenzelm@23363
   379
  | apply_tr _ _ _ _ = raise UNDEF;
wenzelm@5828
   380
wenzelm@23363
   381
fun apply_union _ _ [] state = raise FAILURE (state, UNDEF)
wenzelm@23363
   382
  | apply_union int pos (tr :: trs) state =
wenzelm@23363
   383
      apply_tr int pos tr state
wenzelm@23363
   384
        handle UNDEF => apply_union int pos trs state
wenzelm@23363
   385
          | FAILURE (alt_state, UNDEF) => apply_union int pos trs alt_state
wenzelm@6689
   386
          | exn as FAILURE _ => raise exn
wenzelm@6689
   387
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   388
wenzelm@6689
   389
in
wenzelm@6689
   390
wenzelm@23363
   391
fun apply_trans int pos trs state = (apply_union int pos trs state, NONE)
skalberg@15531
   392
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   393
wenzelm@6689
   394
end;
wenzelm@5828
   395
wenzelm@5828
   396
wenzelm@5828
   397
(* datatype transition *)
wenzelm@5828
   398
wenzelm@5828
   399
datatype transition = Transition of
wenzelm@26621
   400
 {name: string,              (*command name*)
wenzelm@26621
   401
  pos: Position.T,           (*source position*)
wenzelm@26621
   402
  int_only: bool,            (*interactive-only*)
wenzelm@26621
   403
  print: bool,               (*print result state*)
wenzelm@26621
   404
  no_timing: bool,           (*suppress timing*)
wenzelm@26621
   405
  trans: trans list};        (*primitive transitions (union)*)
wenzelm@5828
   406
wenzelm@26621
   407
fun make_transition (name, pos, int_only, print, no_timing, trans) =
wenzelm@26621
   408
  Transition {name = name, pos = pos, int_only = int_only, print = print, no_timing = no_timing,
wenzelm@26621
   409
    trans = trans};
wenzelm@5828
   410
wenzelm@26621
   411
fun map_transition f (Transition {name, pos, int_only, print, no_timing, trans}) =
wenzelm@26621
   412
  make_transition (f (name, pos, int_only, print, no_timing, trans));
wenzelm@5828
   413
wenzelm@27441
   414
val empty = make_transition ("", Position.none, false, false, false, []);
wenzelm@5828
   415
wenzelm@5828
   416
wenzelm@5828
   417
(* diagnostics *)
wenzelm@5828
   418
wenzelm@27441
   419
fun init_of (Transition {trans = [Init (name, _, _)], ...}) = SOME name
wenzelm@27441
   420
  | init_of _ = NONE;
wenzelm@27441
   421
wenzelm@27427
   422
fun name_of (Transition {name, ...}) = name;
wenzelm@27427
   423
fun str_of (Transition {name, pos, ...}) = quote name ^ Position.str_of pos;
wenzelm@5828
   424
wenzelm@27427
   425
fun command_msg msg tr = msg ^ "command " ^ str_of tr;
wenzelm@5828
   426
fun at_command tr = command_msg "At " tr ^ ".";
wenzelm@5828
   427
wenzelm@5828
   428
fun type_error tr state =
wenzelm@18685
   429
  ERROR (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
wenzelm@5828
   430
wenzelm@5828
   431
wenzelm@5828
   432
(* modify transitions *)
wenzelm@5828
   433
wenzelm@26621
   434
fun name nm = map_transition (fn (_, pos, int_only, print, no_timing, trans) =>
wenzelm@26621
   435
  (nm, pos, int_only, print, no_timing, trans));
wenzelm@9010
   436
wenzelm@26621
   437
fun position pos = map_transition (fn (name, _, int_only, print, no_timing, trans) =>
wenzelm@26621
   438
  (name, pos, int_only, print, no_timing, trans));
wenzelm@5828
   439
wenzelm@26621
   440
fun interactive int_only = map_transition (fn (name, pos, _, print, no_timing, trans) =>
wenzelm@26621
   441
  (name, pos, int_only, print, no_timing, trans));
wenzelm@14923
   442
wenzelm@26621
   443
val no_timing = map_transition (fn (name, pos, int_only, print, _, trans) =>
wenzelm@26621
   444
  (name, pos, int_only, print, true, trans));
wenzelm@17363
   445
wenzelm@26621
   446
fun add_trans tr = map_transition (fn (name, pos, int_only, print, no_timing, trans) =>
wenzelm@26621
   447
  (name, pos, int_only, print, no_timing, trans @ [tr]));
wenzelm@16607
   448
wenzelm@26621
   449
val print = map_transition (fn (name, pos, int_only, _, no_timing, trans) =>
wenzelm@26621
   450
  (name, pos, int_only, true, no_timing, trans));
wenzelm@5828
   451
wenzelm@5828
   452
wenzelm@21007
   453
(* basic transitions *)
wenzelm@5828
   454
wenzelm@27576
   455
fun init_theory name f exit = add_trans (Init (name, f, exit));
wenzelm@6689
   456
val exit = add_trans Exit;
wenzelm@7612
   457
val keep' = add_trans o Keep;
wenzelm@27601
   458
fun transaction f = add_trans (Transaction f);
wenzelm@5828
   459
wenzelm@7612
   460
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   461
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   462
wenzelm@21007
   463
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@21007
   464
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@21007
   465
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@15668
   466
wenzelm@21007
   467
wenzelm@21007
   468
(* theory transitions *)
wenzelm@15668
   469
wenzelm@27601
   470
fun generic_theory f = transaction (fn _ =>
wenzelm@26491
   471
  (fn Theory (gthy, _) => Theory (f gthy, NONE)
wenzelm@26491
   472
    | _ => raise UNDEF));
wenzelm@26491
   473
wenzelm@27601
   474
fun theory' f = transaction (fn int =>
wenzelm@20963
   475
  (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (f int thy), NONE)
wenzelm@20963
   476
    | _ => raise UNDEF));
wenzelm@20963
   477
wenzelm@20963
   478
fun theory f = theory' (K f);
wenzelm@20963
   479
wenzelm@27601
   480
fun begin_local_theory begin f = transaction (fn _ =>
wenzelm@20963
   481
  (fn Theory (Context.Theory thy, _) =>
wenzelm@20963
   482
        let
wenzelm@20985
   483
          val lthy = f thy;
wenzelm@21294
   484
          val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
wenzelm@21294
   485
        in Theory (gthy, SOME lthy) end
wenzelm@20963
   486
    | _ => raise UNDEF));
wenzelm@17076
   487
wenzelm@27601
   488
val end_local_theory = transaction (fn _ =>
wenzelm@21294
   489
  (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
wenzelm@21007
   490
    | _ => raise UNDEF));
wenzelm@21007
   491
wenzelm@21007
   492
local
wenzelm@21007
   493
wenzelm@27601
   494
fun local_theory_presentation loc f g = transaction (fn int =>
wenzelm@21294
   495
  (fn Theory (gthy, _) =>
wenzelm@21294
   496
        let
wenzelm@21294
   497
          val finish = loc_finish loc gthy;
wenzelm@25960
   498
          val lthy' = f (loc_begin loc gthy);
wenzelm@21294
   499
        in Theory (finish lthy', SOME lthy') end
wenzelm@20963
   500
    | _ => raise UNDEF) #> tap (g int));
wenzelm@15668
   501
wenzelm@21007
   502
in
wenzelm@21007
   503
wenzelm@20963
   504
fun local_theory loc f = local_theory_presentation loc f (K I);
wenzelm@20963
   505
fun present_local_theory loc g = local_theory_presentation loc I g;
wenzelm@18955
   506
wenzelm@21007
   507
end;
wenzelm@21007
   508
wenzelm@21007
   509
wenzelm@21007
   510
(* proof transitions *)
wenzelm@21007
   511
wenzelm@27601
   512
fun end_proof f = transaction (fn int =>
wenzelm@24795
   513
  (fn Proof (prf, (finish, _)) =>
wenzelm@27564
   514
        let val state = ProofNode.current prf in
wenzelm@21007
   515
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   516
            let
wenzelm@21007
   517
              val ctxt' = f int state;
wenzelm@21007
   518
              val gthy' = finish ctxt';
wenzelm@21007
   519
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   520
          else raise UNDEF
wenzelm@21007
   521
        end
wenzelm@27564
   522
    | SkipProof (0, (gthy, _)) => Theory (gthy, NONE)
wenzelm@21007
   523
    | _ => raise UNDEF));
wenzelm@21007
   524
wenzelm@21294
   525
local
wenzelm@21294
   526
wenzelm@27601
   527
fun begin_proof init finish = transaction (fn int =>
wenzelm@21294
   528
  (fn Theory (gthy, _) =>
wenzelm@21294
   529
    let
berghofe@24453
   530
      val prf = init int gthy;
wenzelm@21294
   531
      val schematic = Proof.schematic_goal prf;
wenzelm@21294
   532
    in
wenzelm@21294
   533
      if ! skip_proofs andalso schematic then
wenzelm@21294
   534
        warning "Cannot skip proof of schematic goal statement"
wenzelm@21294
   535
      else ();
wenzelm@21294
   536
      if ! skip_proofs andalso not schematic then
wenzelm@27564
   537
        SkipProof (0, (finish gthy (Proof.global_skip_proof int prf), gthy))
wenzelm@27564
   538
      else Proof (ProofNode.init prf, (finish gthy, gthy))
wenzelm@21294
   539
    end
wenzelm@21294
   540
  | _ => raise UNDEF));
wenzelm@21294
   541
wenzelm@21294
   542
in
wenzelm@21294
   543
wenzelm@24780
   544
fun local_theory_to_proof' loc f = begin_proof
wenzelm@25960
   545
  (fn int => fn gthy => f int (loc_begin loc gthy))
wenzelm@24780
   546
  (loc_finish loc);
wenzelm@24780
   547
berghofe@24453
   548
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   549
wenzelm@21294
   550
fun theory_to_proof f = begin_proof
wenzelm@24780
   551
  (K (fn Context.Theory thy => f thy | _ => raise UNDEF))
wenzelm@24780
   552
  (K (Context.Theory o ProofContext.theory_of));
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@27601
   561
fun present_proof f = transaction (fn int =>
wenzelm@27564
   562
  (fn Proof (prf, x) => Proof (ProofNode.apply I prf, x)
wenzelm@27564
   563
    | skip as SkipProof _ => skip
wenzelm@21177
   564
    | _ => raise UNDEF) #> tap (f int));
wenzelm@21177
   565
wenzelm@27601
   566
fun proofs' f = transaction (fn int =>
wenzelm@27564
   567
  (fn Proof (prf, x) => Proof (ProofNode.applys (f int) prf, x)
wenzelm@27564
   568
    | skip as SkipProof _ => skip
wenzelm@16815
   569
    | _ => raise UNDEF));
wenzelm@15668
   570
wenzelm@17904
   571
fun proof' f = proofs' (Seq.single 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@17904
   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@25960
   597
(* thread position *)
wenzelm@25799
   598
wenzelm@25960
   599
fun setmp_thread_position (Transition {pos, ...}) f x =
wenzelm@25819
   600
  Position.setmp_thread_data pos f x;
wenzelm@25799
   601
wenzelm@27606
   602
fun status tr m =
wenzelm@27606
   603
  setmp_thread_position tr (fn () => Output.status (Markup.markup m "")) ();
wenzelm@27606
   604
wenzelm@26602
   605
fun error_msg tr exn_info =
wenzelm@26602
   606
  setmp_thread_position tr (fn () => Output.error_msg (exn_message (EXCURSION_FAIL exn_info))) ();
wenzelm@26602
   607
wenzelm@25799
   608
wenzelm@5828
   609
(* apply transitions *)
wenzelm@5828
   610
wenzelm@6664
   611
local
wenzelm@6664
   612
wenzelm@25799
   613
fun app int (tr as Transition {trans, pos, int_only, print, no_timing, ...}) =
wenzelm@25819
   614
  setmp_thread_position tr (fn state =>
wenzelm@25799
   615
    let
wenzelm@26621
   616
      val _ = if not int andalso int_only then warning (command_msg "Interactive-only " tr) else ();
wenzelm@16682
   617
wenzelm@25799
   618
      fun do_timing f x = (warning (command_msg "" tr); timeap f x);
wenzelm@25799
   619
      fun do_profiling f x = profile (! profiling) f x;
wenzelm@25799
   620
wenzelm@26256
   621
      val (result, status) =
wenzelm@25799
   622
         state |> (apply_trans int pos trans
wenzelm@25799
   623
          |> (if ! profiling > 0 andalso not no_timing then do_profiling else I)
wenzelm@25799
   624
          |> (if ! profiling > 0 orelse ! timing andalso not no_timing then do_timing else I));
wenzelm@26256
   625
wenzelm@26621
   626
      val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
wenzelm@26256
   627
    in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);
wenzelm@6664
   628
wenzelm@6664
   629
in
wenzelm@5828
   630
wenzelm@26602
   631
fun transition int tr st =
wenzelm@26256
   632
  let val ctxt = try context_of st in
wenzelm@26256
   633
    (case app int tr st of
wenzelm@26256
   634
      (_, SOME TERMINATE) => NONE
wenzelm@26256
   635
    | (state', SOME (EXCURSION_FAIL exn_info)) => SOME (state', SOME exn_info)
wenzelm@26256
   636
    | (state', SOME exn) => SOME (state', SOME (exn_context ctxt exn, at_command tr))
wenzelm@26256
   637
    | (state', NONE) => SOME (state', NONE))
wenzelm@26256
   638
  end;
wenzelm@6664
   639
wenzelm@6664
   640
end;
wenzelm@5828
   641
wenzelm@5828
   642
wenzelm@27601
   643
(* commit final exit *)
wenzelm@27601
   644
wenzelm@27601
   645
val commit_exit =
wenzelm@27601
   646
  name "end" empty
wenzelm@27601
   647
  |> add_trans CommitExit
wenzelm@27601
   648
  |> imperative (fn () => warning "Expected to find finished theory");
wenzelm@27601
   649
wenzelm@27601
   650
wenzelm@17076
   651
(* excursion: toplevel -- apply transformers/presentation -- toplevel *)
wenzelm@5828
   652
wenzelm@6664
   653
local
wenzelm@6664
   654
wenzelm@27576
   655
fun no_pr _ _ _ = ();
wenzelm@5828
   656
wenzelm@27601
   657
fun excur (tr, pr) (st, res) =
wenzelm@27576
   658
  (case transition (! interact) tr st of
wenzelm@27576
   659
    SOME (st', NONE) =>
wenzelm@27601
   660
      (st', setmp_thread_position tr (fn () => pr st st' res) () handle exn =>
wenzelm@27576
   661
        raise EXCURSION_FAIL (exn, "Presentation failed\n" ^ at_command tr))
wenzelm@27576
   662
  | SOME (st', SOME exn_info) => raise EXCURSION_FAIL exn_info
wenzelm@27576
   663
  | NONE => raise EXCURSION_FAIL (TERMINATE, at_command tr));
wenzelm@27576
   664
wenzelm@27601
   665
fun excurs trs st_res =
wenzelm@27601
   666
  let val (st', res') = fold excur trs st_res in
wenzelm@27601
   667
    if is_toplevel st' then (excur (commit_exit, no_pr) (st', ()); res')
wenzelm@27601
   668
    else error "Unfinished development at end of input"
wenzelm@27601
   669
  end;
wenzelm@17076
   670
wenzelm@6664
   671
in
wenzelm@6664
   672
wenzelm@17076
   673
fun present_excursion trs res =
wenzelm@27601
   674
  excurs trs (toplevel, res) handle exn => error (exn_message exn);
wenzelm@9134
   675
wenzelm@17076
   676
fun excursion trs = present_excursion (map (rpair no_pr) trs) ();
wenzelm@7062
   677
wenzelm@6664
   678
end;
wenzelm@6664
   679
wenzelm@5828
   680
end;