src/Pure/Isar/toplevel.ML
author wenzelm
Wed Jul 13 16:07:37 2005 +0200 (2005-07-13 ago)
changeset 16815 13d20ed9086c
parent 16729 24c5c94aa967
child 17076 c7effdf2e2e2
permissions -rw-r--r--
added print_state_hook;
renamed proof'' to actual_proof;
tuned;
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@5828
     5
The Isabelle/Isar toplevel.
wenzelm@5828
     6
*)
wenzelm@5828
     7
wenzelm@5828
     8
signature TOPLEVEL =
wenzelm@5828
     9
sig
wenzelm@16815
    10
  datatype node =
wenzelm@16815
    11
    Theory of theory | Proof of ProofHistory.T | SkipProof of int History.T * theory
wenzelm@5828
    12
  type state
wenzelm@5828
    13
  val toplevel: state
wenzelm@7732
    14
  val is_toplevel: state -> bool
wenzelm@6689
    15
  exception UNDEF
wenzelm@6689
    16
  val node_history_of: state -> node History.T
wenzelm@5828
    17
  val node_of: state -> node
wenzelm@6664
    18
  val node_case: (theory -> 'a) -> (Proof.state -> 'a) -> state -> 'a
wenzelm@5828
    19
  val theory_of: state -> theory
wenzelm@16452
    20
  val sign_of: state -> theory    (*obsolete*)
wenzelm@7501
    21
  val context_of: state -> Proof.context
wenzelm@5828
    22
  val proof_of: state -> Proof.state
wenzelm@9453
    23
  val enter_forward_proof: state -> Proof.state
wenzelm@16815
    24
  val prompt_state_default: state -> string
wenzelm@16815
    25
  val prompt_state_fn: (state -> string) ref
wenzelm@16815
    26
  val print_state_context: state -> unit
wenzelm@16815
    27
  val print_state_default: bool -> state -> unit
wenzelm@16815
    28
  val print_state_hook: (bool -> state -> unit) -> unit
wenzelm@16815
    29
  val print_state_fn: (bool -> state -> unit) ref
wenzelm@16815
    30
  val print_state: bool -> state -> unit
wenzelm@16815
    31
  val pretty_state: bool -> state -> Pretty.T list
wenzelm@16682
    32
  val quiet: bool ref
wenzelm@16682
    33
  val debug: bool ref
wenzelm@16682
    34
  val timing: bool ref
wenzelm@16682
    35
  val profiling: int ref
wenzelm@16815
    36
  val skip_proofs: bool ref
wenzelm@5828
    37
  exception TERMINATE
wenzelm@5990
    38
  exception RESTART
wenzelm@16682
    39
  type transition
wenzelm@6689
    40
  val undo_limit: bool -> int option
wenzelm@5828
    41
  val empty: transition
wenzelm@14923
    42
  val name_of: transition -> string
wenzelm@14923
    43
  val source_of: transition -> OuterLex.token list option
wenzelm@5828
    44
  val name: string -> transition -> transition
wenzelm@5828
    45
  val position: Position.T -> transition -> transition
wenzelm@14923
    46
  val source: OuterLex.token list -> transition -> transition
wenzelm@5828
    47
  val interactive: bool -> transition -> transition
wenzelm@5828
    48
  val print: transition -> transition
wenzelm@16607
    49
  val print': string -> transition -> transition
wenzelm@9010
    50
  val no_timing: transition -> transition
wenzelm@5828
    51
  val reset: transition -> transition
wenzelm@8465
    52
  val init: (bool -> node) -> (node -> unit) -> (node -> unit) -> transition -> transition
wenzelm@6689
    53
  val exit: transition -> transition
wenzelm@6689
    54
  val kill: transition -> transition
wenzelm@5828
    55
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    56
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@6689
    57
  val history: (node History.T -> node History.T) -> transition -> transition
wenzelm@5828
    58
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@7105
    59
  val init_theory: (bool -> theory) -> (theory -> unit) -> (theory -> unit)
wenzelm@6689
    60
    -> transition -> transition
wenzelm@5828
    61
  val theory: (theory -> theory) -> transition -> transition
wenzelm@7612
    62
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@6689
    63
  val theory_to_proof: (bool -> theory -> ProofHistory.T) -> transition -> transition
wenzelm@5828
    64
  val proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition
wenzelm@6689
    65
  val proof': (bool -> ProofHistory.T -> ProofHistory.T) -> transition -> transition
wenzelm@16815
    66
  val actual_proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition
wenzelm@16815
    67
  val skip_proof: (int History.T -> int History.T) -> transition -> transition
wenzelm@5828
    68
  val proof_to_theory: (ProofHistory.T -> theory) -> transition -> transition
wenzelm@12315
    69
  val proof_to_theory': (bool -> ProofHistory.T -> theory) -> transition -> transition
berghofe@15237
    70
  val skip_proof_to_theory: (int History.T -> bool) -> transition -> transition
wenzelm@9512
    71
  val unknown_theory: transition -> transition
wenzelm@9512
    72
  val unknown_proof: transition -> transition
wenzelm@9512
    73
  val unknown_context: transition -> transition
wenzelm@5922
    74
  val exn_message: exn -> string
wenzelm@5828
    75
  val apply: bool -> transition -> state -> (state * (exn * string) option) option
berghofe@15431
    76
  val excursion_result: ((transition * (state -> state -> 'a -> 'a)) list * 'a) -> 'a
wenzelm@5828
    77
  val excursion: transition list -> unit
wenzelm@5828
    78
  val set_state: state -> unit
wenzelm@5828
    79
  val get_state: unit -> state
wenzelm@5828
    80
  val exn: unit -> (exn * string) option
wenzelm@5828
    81
  val >> : transition -> bool
wenzelm@14985
    82
  val >>> : transition list -> unit
nipkow@14091
    83
  type 'a isar
nipkow@14091
    84
  val loop: 'a isar -> unit
wenzelm@5828
    85
end;
wenzelm@5828
    86
wenzelm@6965
    87
structure Toplevel: TOPLEVEL =
wenzelm@5828
    88
struct
wenzelm@5828
    89
wenzelm@5828
    90
wenzelm@5828
    91
(** toplevel state **)
wenzelm@5828
    92
wenzelm@16815
    93
(* datatype state *)
wenzelm@5828
    94
wenzelm@5828
    95
datatype node =
wenzelm@5828
    96
  Theory of theory |
wenzelm@16815
    97
  Proof of ProofHistory.T |              (*history of proof states*)
wenzelm@16815
    98
  SkipProof of int History.T * theory;   (*history of proof depths*)
wenzelm@5828
    99
wenzelm@8465
   100
datatype state = State of (node History.T * ((node -> unit) * (node -> unit))) option;
wenzelm@5828
   101
skalberg@15531
   102
val toplevel = State NONE;
wenzelm@5828
   103
skalberg@15531
   104
fun is_toplevel (State NONE) = true
wenzelm@7732
   105
  | is_toplevel _ = false;
wenzelm@7732
   106
skalberg@15531
   107
fun str_of_state (State NONE) = "at top level"
wenzelm@16815
   108
  | str_of_state (State (SOME (node, _))) =
wenzelm@16815
   109
      (case History.current node of
wenzelm@16815
   110
        Theory _ => "in theory mode"
wenzelm@16815
   111
      | Proof _ => "in proof mode"
wenzelm@16815
   112
      | SkipProof _ => "in skipped proof mode");
wenzelm@5946
   113
wenzelm@5946
   114
wenzelm@5828
   115
(* top node *)
wenzelm@5828
   116
wenzelm@6689
   117
exception UNDEF;
wenzelm@6689
   118
skalberg@15531
   119
fun node_history_of (State NONE) = raise UNDEF
skalberg@15531
   120
  | node_history_of (State (SOME (node, _))) = node;
wenzelm@6689
   121
wenzelm@6689
   122
val node_of = History.current o node_history_of;
wenzelm@5828
   123
wenzelm@6664
   124
fun node_case f g state =
wenzelm@5828
   125
  (case node_of state of
wenzelm@5828
   126
    Theory thy => f thy
wenzelm@16815
   127
  | Proof prf => g (ProofHistory.current prf)
wenzelm@16815
   128
  | SkipProof (_, thy) => f thy);
wenzelm@5828
   129
wenzelm@6664
   130
val theory_of = node_case I Proof.theory_of;
wenzelm@16452
   131
val sign_of = theory_of;
wenzelm@7501
   132
val context_of = node_case ProofContext.init Proof.context_of;
wenzelm@6664
   133
val proof_of = node_case (fn _ => raise UNDEF) I;
wenzelm@6664
   134
wenzelm@12049
   135
val enter_forward_proof = node_case Proof.init_state Proof.enter_forward;
wenzelm@5828
   136
wenzelm@5828
   137
wenzelm@16815
   138
(* prompt state *)
wenzelm@16815
   139
wenzelm@16815
   140
fun prompt_state_default (State _) = Source.default_prompt;
wenzelm@16815
   141
wenzelm@16815
   142
val prompt_state_fn = ref prompt_state_default;
wenzelm@16815
   143
fun prompt_state state = ! prompt_state_fn state;
wenzelm@16815
   144
wenzelm@16815
   145
wenzelm@16815
   146
(* print state *)
wenzelm@16815
   147
wenzelm@16815
   148
fun pretty_context thy = [Pretty.block
wenzelm@16815
   149
  [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
wenzelm@16815
   150
    Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]];
wenzelm@16815
   151
wenzelm@16815
   152
fun pretty_state_context state =
wenzelm@16815
   153
  (case try theory_of state of NONE => []
wenzelm@16815
   154
  | SOME thy => pretty_context thy);
wenzelm@16815
   155
wenzelm@16815
   156
fun pretty_node prf_only (Theory thy) = if prf_only then [] else pretty_context thy
wenzelm@16815
   157
  | pretty_node _ (Proof prf) =
wenzelm@16815
   158
      Proof.pretty_state (ProofHistory.position prf) (ProofHistory.current prf)
wenzelm@16815
   159
  | pretty_node _ (SkipProof (h, _)) =
wenzelm@16815
   160
      [Pretty.str ("skipped proof: depth " ^ string_of_int (History.current h))];
wenzelm@16815
   161
wenzelm@16815
   162
fun pretty_state prf_only state =
wenzelm@16815
   163
  let val ref (begin_state, end_state, _) = Display.current_goals_markers in
wenzelm@16815
   164
    (case try node_of state of NONE => []
wenzelm@16815
   165
    | SOME node =>
wenzelm@16815
   166
        (if begin_state = "" then [] else [Pretty.str begin_state]) @
wenzelm@16815
   167
        pretty_node prf_only node @
wenzelm@16815
   168
        (if end_state = "" then [] else [Pretty.str end_state]))
wenzelm@16815
   169
  end;
wenzelm@16815
   170
wenzelm@16815
   171
val print_state_context = Pretty.writelns o pretty_state_context;
wenzelm@16815
   172
fun print_state_default prf_only state = Pretty.writelns (pretty_state prf_only state);
wenzelm@16815
   173
wenzelm@16815
   174
val print_state_hooks = ref ([]: (bool -> state -> unit) list);
wenzelm@16815
   175
fun print_state_hook f = change print_state_hooks (cons f);
wenzelm@16815
   176
val print_state_fn = ref print_state_default;
wenzelm@16815
   177
wenzelm@16815
   178
fun print_state prf_only state =
wenzelm@16815
   179
 (List.app (fn f => (try (fn () => f prf_only state) (); ())) (! print_state_hooks);
wenzelm@16815
   180
  ! print_state_fn prf_only state);
wenzelm@16815
   181
wenzelm@16815
   182
wenzelm@15668
   183
wenzelm@5828
   184
(** toplevel transitions **)
wenzelm@5828
   185
wenzelm@16682
   186
val quiet = ref false;
wenzelm@16682
   187
val debug = ref false;
wenzelm@16682
   188
val timing = Output.timing;
wenzelm@16682
   189
val profiling = ref 0;
wenzelm@16815
   190
val skip_proofs = ref false;
wenzelm@16682
   191
wenzelm@5828
   192
exception TERMINATE;
wenzelm@5990
   193
exception RESTART;
wenzelm@7022
   194
exception EXCURSION_FAIL of exn * string;
wenzelm@6689
   195
exception FAILURE of state * exn;
wenzelm@5828
   196
wenzelm@5828
   197
wenzelm@16815
   198
(* node transactions and recovery from stale theories *)
wenzelm@6689
   199
wenzelm@16815
   200
(*NB: proof commands should be non-destructive!*)
wenzelm@7022
   201
wenzelm@6689
   202
local
wenzelm@6689
   203
wenzelm@16452
   204
fun is_stale state = Context.is_stale (theory_of state) handle UNDEF => false;
wenzelm@6689
   205
wenzelm@16815
   206
val stale_theory = ERROR_MESSAGE "Stale theory encountered after succesful execution!";
wenzelm@16815
   207
wenzelm@16452
   208
fun checkpoint_node true (Theory thy) = Theory (Theory.checkpoint thy)
wenzelm@6689
   209
  | checkpoint_node _ node = node;
wenzelm@6689
   210
wenzelm@6689
   211
fun copy_node true (Theory thy) = Theory (Theory.copy thy)
wenzelm@6689
   212
  | copy_node _ node = node;
wenzelm@6689
   213
skalberg@15531
   214
fun return (result, NONE) = result
skalberg@15531
   215
  | return (result, SOME exn) = raise FAILURE (result, exn);
wenzelm@7022
   216
wenzelm@16729
   217
fun debug_trans f x =
wenzelm@16729
   218
  if ! debug then
wenzelm@16815
   219
    setmp Output.transform_exceptions false
wenzelm@16815
   220
      exception_trace (fn () => f x)
wenzelm@16729
   221
  else f x;
wenzelm@16729
   222
wenzelm@16729
   223
fun interruptible f x =
wenzelm@16729
   224
  let val y = ref x
wenzelm@16729
   225
  in raise_interrupt (fn () => y := f x) (); ! y end;
wenzelm@16729
   226
wenzelm@6689
   227
in
wenzelm@6689
   228
wenzelm@16815
   229
fun transaction _ _ _ (State NONE) = raise UNDEF
wenzelm@16815
   230
  | transaction int hist f (State (SOME (node, term))) =
wenzelm@6689
   231
      let
wenzelm@16046
   232
        val cont_node = History.map (checkpoint_node int) node;
wenzelm@16046
   233
        val back_node = History.map (copy_node int) cont_node;
wenzelm@16729
   234
        fun state nd = State (SOME (nd, term));
wenzelm@16729
   235
        fun normal_state nd = (state nd, NONE);
wenzelm@16729
   236
        fun error_state nd exn = (state nd, SOME exn);
wenzelm@6689
   237
wenzelm@16729
   238
        val (result, err) =
wenzelm@16729
   239
          cont_node
wenzelm@16729
   240
          |> ((fn nd => f int nd)
wenzelm@16729
   241
              |> (if hist then History.apply_copy (copy_node int) else History.map)
wenzelm@16729
   242
              |> debug_trans
wenzelm@16729
   243
              |> interruptible
wenzelm@16729
   244
              |> transform_error)
wenzelm@16729
   245
          |> normal_state
wenzelm@16729
   246
          handle exn => error_state cont_node exn;
wenzelm@6689
   247
      in
wenzelm@16729
   248
        if is_stale result
wenzelm@16729
   249
        then return (error_state back_node (if_none err stale_theory))
wenzelm@16729
   250
        else return (result, err)
wenzelm@6689
   251
      end;
wenzelm@6689
   252
wenzelm@6689
   253
end;
wenzelm@6689
   254
wenzelm@6689
   255
wenzelm@6689
   256
(* primitive transitions *)
wenzelm@6689
   257
wenzelm@16815
   258
(*NB: recovery from stale theories is provided only for theory-level
wenzelm@16815
   259
  operations via MapCurrent and AppCurrent.  Other node or state
wenzelm@16815
   260
  operations should not touch theories at all.
wenzelm@6965
   261
wenzelm@16815
   262
  Interrupts are enabled only for Keep, MapCurrent, and AppCurrent.*)
wenzelm@5828
   263
wenzelm@5828
   264
datatype trans =
wenzelm@6689
   265
  Reset |                                       (*empty toplevel*)
wenzelm@8465
   266
  Init of (bool -> node) * ((node -> unit) * (node -> unit)) |
wenzelm@8465
   267
    (*init node; provide exit/kill operation*)
wenzelm@8465
   268
  Exit |                                        (*conclude node*)
wenzelm@6689
   269
  Kill |                                        (*abort node*)
wenzelm@7612
   270
  Keep of bool -> state -> unit |               (*peek at state*)
wenzelm@6689
   271
  History of node History.T -> node History.T | (*history operation (undo etc.)*)
wenzelm@6689
   272
  MapCurrent of bool -> node -> node |          (*change node, bypassing history*)
wenzelm@6689
   273
  AppCurrent of bool -> node -> node;           (*change node, recording history*)
wenzelm@6689
   274
skalberg@15531
   275
fun undo_limit int = if int then NONE else SOME 0;
wenzelm@6689
   276
wenzelm@6689
   277
local
wenzelm@5828
   278
wenzelm@6689
   279
fun apply_tr _ Reset _ = toplevel
skalberg@15531
   280
  | apply_tr int (Init (f, term)) (State NONE) =
skalberg@15531
   281
      State (SOME (History.init (undo_limit int) (f int), term))
skalberg@15531
   282
  | apply_tr _ (Init _ ) (State (SOME _)) = raise UNDEF
skalberg@15531
   283
  | apply_tr _ Exit (State NONE) = raise UNDEF
skalberg@15531
   284
  | apply_tr _ Exit (State (SOME (node, (exit, _)))) =
skalberg@15531
   285
      (exit (History.current node); State NONE)
skalberg@15531
   286
  | apply_tr _ Kill (State NONE) = raise UNDEF
skalberg@15531
   287
  | apply_tr _ Kill (State (SOME (node, (_, kill)))) =
skalberg@15531
   288
      (kill (History.current node); State NONE)
wenzelm@12987
   289
  | apply_tr int (Keep f) state = (raise_interrupt (f int) state; state)
skalberg@15531
   290
  | apply_tr _ (History _) (State NONE) = raise UNDEF
skalberg@15531
   291
  | apply_tr _ (History f) (State (SOME (node, term))) = State (SOME (f node, term))
wenzelm@16815
   292
  | apply_tr int (MapCurrent f) state = transaction int false f state
wenzelm@16815
   293
  | apply_tr int (AppCurrent f) state = transaction int true f state;
wenzelm@5828
   294
wenzelm@6689
   295
fun apply_union _ [] state = raise FAILURE (state, UNDEF)
wenzelm@6689
   296
  | apply_union int (tr :: trs) state =
wenzelm@6689
   297
      transform_error (apply_tr int tr) state
wenzelm@6689
   298
        handle UNDEF => apply_union int trs state
wenzelm@6689
   299
          | FAILURE (alt_state, UNDEF) => apply_union int trs alt_state
wenzelm@6689
   300
          | exn as FAILURE _ => raise exn
wenzelm@6689
   301
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   302
wenzelm@6689
   303
in
wenzelm@6689
   304
skalberg@15531
   305
fun apply_trans int trs state = (apply_union int trs state, NONE)
skalberg@15531
   306
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   307
wenzelm@6689
   308
end;
wenzelm@5828
   309
wenzelm@5828
   310
wenzelm@5828
   311
(* datatype transition *)
wenzelm@5828
   312
wenzelm@5828
   313
datatype transition = Transition of
wenzelm@16815
   314
 {name: string,                        (*command name*)
wenzelm@16815
   315
  pos: Position.T,                     (*source position*)
wenzelm@16815
   316
  source: OuterLex.token list option,  (*source text*)
wenzelm@16815
   317
  int_only: bool,                      (*interactive-only*)
wenzelm@16815
   318
  print: string list,                  (*print modes (union)*)
wenzelm@16815
   319
  no_timing: bool,                     (*suppress timing*)
wenzelm@16815
   320
  trans: trans list};                  (*primitive transitions (union)*)
wenzelm@5828
   321
wenzelm@14923
   322
fun make_transition (name, pos, source, int_only, print, no_timing, trans) =
wenzelm@14923
   323
  Transition {name = name, pos = pos, source = source,
wenzelm@14923
   324
    int_only = int_only, print = print, no_timing = no_timing, trans = trans};
wenzelm@5828
   325
wenzelm@14923
   326
fun map_transition f (Transition {name, pos, source, int_only, print, no_timing, trans}) =
wenzelm@14923
   327
  make_transition (f (name, pos, source, int_only, print, no_timing, trans));
wenzelm@5828
   328
wenzelm@16607
   329
val empty = make_transition ("<unknown>", Position.none, NONE, false, [], false, []);
wenzelm@14923
   330
wenzelm@14923
   331
fun name_of (Transition {name, ...}) = name;
wenzelm@14923
   332
fun source_of (Transition {source, ...}) = source;
wenzelm@5828
   333
wenzelm@5828
   334
wenzelm@5828
   335
(* diagnostics *)
wenzelm@5828
   336
wenzelm@5828
   337
fun str_of_transition (Transition {name, pos, ...}) = quote name ^ Position.str_of pos;
wenzelm@5828
   338
wenzelm@5828
   339
fun command_msg msg tr = msg ^ "command " ^ str_of_transition tr;
wenzelm@5828
   340
fun at_command tr = command_msg "At " tr ^ ".";
wenzelm@5828
   341
wenzelm@5828
   342
fun type_error tr state =
wenzelm@6689
   343
  ERROR_MESSAGE (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
wenzelm@5828
   344
wenzelm@5828
   345
wenzelm@5828
   346
(* modify transitions *)
wenzelm@5828
   347
wenzelm@14923
   348
fun name nm = map_transition (fn (_, pos, source, int_only, print, no_timing, trans) =>
wenzelm@14923
   349
  (nm, pos, source, int_only, print, no_timing, trans));
wenzelm@5828
   350
wenzelm@14923
   351
fun position pos = map_transition (fn (name, _, source, int_only, print, no_timing, trans) =>
wenzelm@14923
   352
  (name, pos, source, int_only, print, no_timing, trans));
wenzelm@9010
   353
wenzelm@14923
   354
fun source src = map_transition (fn (name, pos, _, int_only, print, no_timing, trans) =>
skalberg@15531
   355
  (name, pos, SOME src, int_only, print, no_timing, trans));
wenzelm@5828
   356
wenzelm@14923
   357
fun interactive int_only = map_transition (fn (name, pos, source, _, print, no_timing, trans) =>
wenzelm@14923
   358
  (name, pos, source, int_only, print, no_timing, trans));
wenzelm@14923
   359
wenzelm@16607
   360
fun print' mode = map_transition (fn (name, pos, source, int_only, print, no_timing, trans) =>
wenzelm@16607
   361
  (name, pos, source, int_only, insert (op =) mode print, no_timing, trans));
wenzelm@16607
   362
wenzelm@16607
   363
val print = print' "";
wenzelm@5828
   364
wenzelm@14923
   365
val no_timing = map_transition (fn (name, pos, source, int_only, print, _, trans) =>
wenzelm@14923
   366
  (name, pos, source, int_only, print, true, trans));
wenzelm@5828
   367
wenzelm@14923
   368
fun add_trans tr = map_transition (fn (name, pos, source, int_only, print, no_timing, trans) =>
wenzelm@14923
   369
  (name, pos, source, int_only, print, no_timing, trans @ [tr]));
wenzelm@5828
   370
wenzelm@5828
   371
wenzelm@5828
   372
(* build transitions *)
wenzelm@5828
   373
wenzelm@5828
   374
val reset = add_trans Reset;
wenzelm@6689
   375
fun init f exit kill = add_trans (Init (f, (exit, kill)));
wenzelm@6689
   376
val exit = add_trans Exit;
wenzelm@6689
   377
val kill = add_trans Kill;
wenzelm@7612
   378
val keep' = add_trans o Keep;
wenzelm@6689
   379
val history = add_trans o History;
wenzelm@5828
   380
val map_current = add_trans o MapCurrent;
wenzelm@6689
   381
val app_current = add_trans o AppCurrent;
wenzelm@5828
   382
wenzelm@7612
   383
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   384
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   385
wenzelm@6689
   386
fun init_theory f exit kill =
wenzelm@8465
   387
  init (fn int => Theory (f int))
wenzelm@6689
   388
    (fn Theory thy => exit thy | _ => raise UNDEF)
wenzelm@6689
   389
    (fn Theory thy => kill thy | _ => raise UNDEF);
wenzelm@5828
   390
wenzelm@15668
   391
wenzelm@15668
   392
(* typed transitions *)
wenzelm@15668
   393
wenzelm@6689
   394
fun theory f = app_current (fn _ => (fn Theory thy => Theory (f thy) | _ => raise UNDEF));
wenzelm@7612
   395
fun theory' f = app_current (fn int => (fn Theory thy => Theory (f int thy) | _ => raise UNDEF));
wenzelm@15668
   396
berghofe@15237
   397
fun theory_to_proof f = app_current (fn int =>
berghofe@15237
   398
  (fn Theory thy =>
wenzelm@15668
   399
        if ! skip_proofs then SkipProof (History.init (undo_limit int) 0,
berghofe@15237
   400
          fst (SkipProof.global_skip_proof int (ProofHistory.current (f int thy))))
berghofe@15237
   401
        else Proof (f int thy)
berghofe@15237
   402
    | _ => raise UNDEF));
wenzelm@15668
   403
wenzelm@16815
   404
fun proof' f = map_current (fn int =>
wenzelm@16815
   405
  (fn Proof prf => Proof (f int prf)
wenzelm@16815
   406
    | SkipProof (h, thy) => SkipProof (History.apply I h, thy)   (*approximate f*)
wenzelm@16815
   407
    | _ => raise UNDEF));
wenzelm@15668
   408
wenzelm@6689
   409
val proof = proof' o K;
wenzelm@16815
   410
wenzelm@16815
   411
fun actual_proof f = map_current (fn _ =>
wenzelm@16815
   412
  (fn Proof prf => Proof (f prf) | _ => raise UNDEF));
wenzelm@16815
   413
wenzelm@16815
   414
fun skip_proof f = map_current (fn _ =>
wenzelm@16815
   415
  (fn SkipProof (h, thy) => SkipProof (f h, thy) | _ => raise UNDEF));
wenzelm@15668
   416
wenzelm@12315
   417
fun proof_to_theory' f =
berghofe@15237
   418
  map_current (fn int => (fn Proof prf => Theory (f int prf)
berghofe@15237
   419
    | SkipProof (h, thy) => if History.current h = 0 then Theory thy else raise UNDEF
berghofe@15237
   420
    | _ => raise UNDEF));
wenzelm@15668
   421
wenzelm@12315
   422
val proof_to_theory = proof_to_theory' o K;
wenzelm@15668
   423
wenzelm@16815
   424
fun skip_proof_to_theory p = map_current (fn _ =>
berghofe@15237
   425
  (fn SkipProof (h, thy) => if p h then Theory thy else raise UNDEF | _ => raise UNDEF));
wenzelm@5828
   426
wenzelm@9512
   427
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@9512
   428
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@9512
   429
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@9512
   430
wenzelm@5828
   431
wenzelm@5828
   432
wenzelm@5828
   433
(** toplevel transactions **)
wenzelm@5828
   434
wenzelm@5828
   435
(* print exceptions *)
wenzelm@5828
   436
wenzelm@15668
   437
local
wenzelm@15668
   438
wenzelm@15668
   439
fun with_context f xs =
wenzelm@15668
   440
  (case Context.get_context () of NONE => []
wenzelm@16452
   441
  | SOME thy => map (f thy) xs);
wenzelm@15668
   442
wenzelm@15668
   443
fun raised name [] = "exception " ^ name ^ " raised"
wenzelm@15668
   444
  | raised name [msg] = "exception " ^ name ^ " raised: " ^ msg
wenzelm@15668
   445
  | raised name msgs = cat_lines (("exception " ^ name ^ " raised:") :: msgs);
wenzelm@5828
   446
wenzelm@15668
   447
fun exn_msg _ TERMINATE = "Exit."
wenzelm@15668
   448
  | exn_msg _ RESTART = "Restart."
wenzelm@15668
   449
  | exn_msg _ Interrupt = "Interrupt."
wenzelm@15668
   450
  | exn_msg _ ERROR = "ERROR."
wenzelm@15668
   451
  | exn_msg _ (ERROR_MESSAGE msg) = msg
wenzelm@16452
   452
  | exn_msg detailed (EXCURSION_FAIL (exn, msg)) = cat_lines [exn_msg detailed exn, msg]
wenzelm@16452
   453
  | exn_msg detailed (Context.DATA_FAIL (exn, msg)) = cat_lines [exn_msg detailed exn, msg]
wenzelm@16607
   454
  | exn_msg detailed (Syntax.TRANSLATION_FAIL (exn, msg)) = cat_lines [exn_msg detailed exn, msg]
wenzelm@15668
   455
  | exn_msg false (THEORY (msg, _)) = msg
wenzelm@16452
   456
  | exn_msg true (THEORY (msg, thys)) = raised "THEORY" (msg :: map Context.str_of_thy thys)
wenzelm@15668
   457
  | exn_msg _ (ProofContext.CONTEXT (msg, _)) = msg
wenzelm@15668
   458
  | exn_msg _ (Proof.STATE (msg, _)) = msg
wenzelm@15668
   459
  | exn_msg _ (ProofHistory.FAIL msg) = msg
wenzelm@15668
   460
  | exn_msg detailed (MetaSimplifier.SIMPROC_FAIL (name, exn)) =
wenzelm@15668
   461
      fail_msg detailed "simproc" ((name, Position.none), exn)
wenzelm@15668
   462
  | exn_msg detailed (Attrib.ATTRIB_FAIL info) = fail_msg detailed "attribute" info
wenzelm@15668
   463
  | exn_msg detailed (Method.METHOD_FAIL info) = fail_msg detailed "method" info
wenzelm@15668
   464
  | exn_msg detailed (Antiquote.ANTIQUOTE_FAIL info) = fail_msg detailed "antiquotation" info
wenzelm@15668
   465
  | exn_msg false (Syntax.AST (msg, _)) = raised "AST" [msg]
wenzelm@15668
   466
  | exn_msg true (Syntax.AST (msg, asts)) =
wenzelm@15668
   467
      raised "AST" (msg :: map (Pretty.string_of o Syntax.pretty_ast) asts)
wenzelm@15668
   468
  | exn_msg false (TYPE (msg, _, _)) = raised "TYPE" [msg]
wenzelm@15668
   469
  | exn_msg true (TYPE (msg, Ts, ts)) = raised "TYPE" (msg ::
wenzelm@15668
   470
        with_context Sign.string_of_typ Ts @ with_context Sign.string_of_term ts)
wenzelm@15668
   471
  | exn_msg false (TERM (msg, _)) = raised "TERM" [msg]
wenzelm@15668
   472
  | exn_msg true (TERM (msg, ts)) = raised "TERM" (msg :: with_context Sign.string_of_term ts)
wenzelm@15668
   473
  | exn_msg false (THM (msg, _, _)) = raised "THM" [msg]
wenzelm@15668
   474
  | exn_msg true (THM (msg, i, thms)) =
wenzelm@15668
   475
      raised ("THM " ^ string_of_int i) (msg :: map Display.string_of_thm thms)
wenzelm@15668
   476
  | exn_msg _ Option = raised "Option" []
wenzelm@15668
   477
  | exn_msg _ UnequalLengths = raised "UnequalLengths" []
wenzelm@15668
   478
  | exn_msg _ Empty = raised "Empty" []
wenzelm@15668
   479
  | exn_msg _ Subscript = raised "Subscript" []
wenzelm@16197
   480
  | exn_msg _ (Fail msg) = raised "Fail" [msg]
wenzelm@15668
   481
  | exn_msg _ exn = General.exnMessage exn
wenzelm@15668
   482
and fail_msg detailed kind ((name, pos), exn) =
wenzelm@15668
   483
  "Error in " ^ kind ^ " " ^ quote name ^ Position.str_of pos ^ ":\n" ^ exn_msg detailed exn;
ballarin@15519
   484
wenzelm@15668
   485
in
wenzelm@15668
   486
wenzelm@15668
   487
fun exn_message exn = exn_msg (! debug) exn;
wenzelm@5828
   488
skalberg@15531
   489
fun print_exn NONE = ()
skalberg@15531
   490
  | print_exn (SOME (exn, s)) = error_msg (cat_lines [exn_message exn, s]);
wenzelm@5828
   491
wenzelm@15668
   492
end;
wenzelm@15668
   493
wenzelm@5828
   494
wenzelm@5828
   495
(* apply transitions *)
wenzelm@5828
   496
wenzelm@6664
   497
local
wenzelm@6664
   498
wenzelm@9010
   499
fun app int (tr as Transition {trans, int_only, print, no_timing, ...}) state =
wenzelm@5828
   500
  let
wenzelm@16682
   501
    val _ = conditional (not int andalso int_only) (fn () =>
wenzelm@16682
   502
      warning (command_msg "Interactive-only " tr));
wenzelm@16682
   503
wenzelm@16682
   504
    fun do_timing f x = (info (command_msg "" tr); timeap f x);
wenzelm@16682
   505
    fun do_profiling f x = profile (! profiling) f x;
wenzelm@16682
   506
wenzelm@6689
   507
    val (result, opt_exn) =
wenzelm@16729
   508
       state |> (apply_trans int trans
wenzelm@16729
   509
        |> (if ! profiling > 0 then do_profiling else I)
wenzelm@16729
   510
        |> (if ! profiling > 0 orelse ! timing andalso not no_timing then do_timing else I));
wenzelm@16607
   511
    val _ = conditional (int andalso not (! quiet) andalso
wenzelm@16607
   512
        exists (fn m => m mem_string print) ("" :: ! print_mode))
wenzelm@16607
   513
      (fn () => print_state false result);
skalberg@15570
   514
  in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) opt_exn) end;
wenzelm@6664
   515
wenzelm@6664
   516
in
wenzelm@5828
   517
wenzelm@6689
   518
fun apply int tr st =
wenzelm@6965
   519
  (case app int tr st of
skalberg@15531
   520
    (_, SOME TERMINATE) => NONE
skalberg@15531
   521
  | (_, SOME RESTART) => SOME (toplevel, NONE)
skalberg@15531
   522
  | (state', SOME (EXCURSION_FAIL exn_info)) => SOME (state', SOME exn_info)
skalberg@15531
   523
  | (state', SOME exn) => SOME (state', SOME (exn, at_command tr))
skalberg@15531
   524
  | (state', NONE) => SOME (state', NONE));
wenzelm@6664
   525
wenzelm@6664
   526
end;
wenzelm@5828
   527
wenzelm@5828
   528
wenzelm@5828
   529
(* excursion: toplevel -- apply transformers -- toplevel *)
wenzelm@5828
   530
wenzelm@6664
   531
local
wenzelm@6664
   532
wenzelm@5828
   533
fun excur [] x = x
wenzelm@9134
   534
  | excur ((tr, f) :: trs) (st, res) =
wenzelm@9134
   535
      (case apply false tr st of
skalberg@15531
   536
        SOME (st', NONE) =>
berghofe@15431
   537
          excur trs (st', transform_error (fn () => f st st' res) () handle exn =>
wenzelm@10324
   538
            raise EXCURSION_FAIL (exn, "Presentation failed\n" ^ at_command tr))
skalberg@15531
   539
      | SOME (st', SOME exn_info) => raise EXCURSION_FAIL exn_info
skalberg@15531
   540
      | NONE => raise EXCURSION_FAIL (TERMINATE, at_command tr));
wenzelm@5828
   541
wenzelm@6664
   542
in
wenzelm@6664
   543
wenzelm@9134
   544
fun excursion_result (trs, res) =
skalberg@15531
   545
  (case excur trs (State NONE, res) of
skalberg@15531
   546
    (State NONE, res') => res'
wenzelm@9134
   547
  | _ => raise ERROR_MESSAGE "Unfinished development at end of input")
wenzelm@9134
   548
  handle exn => error (exn_message exn);
wenzelm@9134
   549
wenzelm@5828
   550
fun excursion trs =
berghofe@15431
   551
  excursion_result (map (fn tr => (tr, fn _ => fn _ => fn _ => ())) trs, ());
wenzelm@7062
   552
wenzelm@6664
   553
end;
wenzelm@6664
   554
wenzelm@5828
   555
wenzelm@5828
   556
wenzelm@5828
   557
(** interactive transformations **)
wenzelm@5828
   558
wenzelm@5828
   559
(* the global state reference *)
wenzelm@5828
   560
skalberg@15531
   561
val global_state = ref (toplevel, NONE: (exn * string) option);
wenzelm@5828
   562
skalberg@15531
   563
fun set_state state = global_state := (state, NONE);
wenzelm@5828
   564
fun get_state () = fst (! global_state);
wenzelm@5828
   565
fun exn () = snd (! global_state);
wenzelm@5828
   566
wenzelm@5828
   567
wenzelm@6965
   568
(* the Isar source of transitions *)
wenzelm@6965
   569
nipkow@14091
   570
type 'a isar =
wenzelm@6965
   571
  (transition, (transition option,
wenzelm@12881
   572
    (OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token,
nipkow@14091
   573
      Position.T * (Symbol.symbol, (string, 'a) Source.source) Source.source)
wenzelm@12881
   574
          Source.source) Source.source) Source.source) Source.source) Source.source) Source.source;
wenzelm@6965
   575
wenzelm@6965
   576
wenzelm@5828
   577
(* apply transformers to global state *)
wenzelm@5828
   578
wenzelm@14985
   579
nonfix >> >>>;
wenzelm@5828
   580
wenzelm@5828
   581
fun >> tr =
wenzelm@5828
   582
  (case apply true tr (get_state ()) of
skalberg@15531
   583
    NONE => false
skalberg@15531
   584
  | SOME (state', exn_info) =>
wenzelm@5828
   585
      (global_state := (state', exn_info);
wenzelm@16729
   586
        print_exn exn_info;
wenzelm@5828
   587
        true));
wenzelm@5828
   588
wenzelm@14985
   589
fun >>> [] = ()
wenzelm@14985
   590
  | >>> (tr :: trs) = if >> tr then >>> trs else ();
wenzelm@14985
   591
wenzelm@7602
   592
(*Note: this is for Poly/ML only, we really do not intend to exhibit
wenzelm@7602
   593
  interrupts here*)
skalberg@15531
   594
fun get_interrupt src = SOME (Source.get_single src) handle Interrupt => NONE;
wenzelm@7602
   595
wenzelm@5828
   596
fun raw_loop src =
wenzelm@7602
   597
  (case get_interrupt (Source.set_prompt (prompt_state (get_state ())) src) of
skalberg@15531
   598
    NONE => (writeln "\nInterrupt."; raw_loop src)
skalberg@15531
   599
  | SOME NONE => ()
skalberg@15531
   600
  | SOME (SOME (tr, src')) => if >> tr then raw_loop src' else ());
wenzelm@5828
   601
wenzelm@12987
   602
fun loop src = ignore_interrupt raw_loop src;
wenzelm@5828
   603
wenzelm@5828
   604
end;