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