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