src/Pure/Isar/toplevel.ML
author wenzelm
Wed Jul 06 20:00:41 2005 +0200 (2005-07-06)
changeset 16729 24c5c94aa967
parent 16682 154a84e1a4f7
child 16815 13d20ed9086c
permissions -rw-r--r--
debug: exception_trace;
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@16490
    10
  datatype node = Theory of theory | Proof of ProofHistory.T | SkipProof of int History.T * theory
wenzelm@5828
    11
  type state
wenzelm@5828
    12
  val toplevel: state
wenzelm@7732
    13
  val is_toplevel: state -> bool
wenzelm@6689
    14
  val prompt_state_default: state -> string
wenzelm@6689
    15
  val prompt_state_fn: (state -> string) ref
wenzelm@7308
    16
  val print_state_context: state -> unit
wenzelm@8886
    17
  val print_state_default: bool -> state -> unit
wenzelm@8886
    18
  val print_state_fn: (bool -> state -> unit) ref
wenzelm@8886
    19
  val print_state: bool -> state -> unit
wenzelm@12423
    20
  val pretty_state: bool -> state -> Pretty.T list
wenzelm@6689
    21
  exception UNDEF
wenzelm@6689
    22
  val node_history_of: state -> node History.T
wenzelm@5828
    23
  val node_of: state -> node
wenzelm@6664
    24
  val node_case: (theory -> 'a) -> (Proof.state -> 'a) -> state -> 'a
wenzelm@5828
    25
  val theory_of: state -> theory
wenzelm@16452
    26
  val sign_of: state -> theory    (*obsolete*)
wenzelm@7501
    27
  val context_of: state -> Proof.context
wenzelm@5828
    28
  val proof_of: state -> Proof.state
wenzelm@9453
    29
  val enter_forward_proof: state -> Proof.state
wenzelm@16682
    30
  val quiet: bool ref
wenzelm@16682
    31
  val debug: bool ref
wenzelm@16682
    32
  val timing: bool ref
wenzelm@16682
    33
  val profiling: int ref
wenzelm@5828
    34
  exception TERMINATE
wenzelm@5990
    35
  exception RESTART
wenzelm@16682
    36
  type transition
wenzelm@6689
    37
  val undo_limit: bool -> int option
wenzelm@5828
    38
  val empty: transition
wenzelm@14923
    39
  val name_of: transition -> string
wenzelm@14923
    40
  val source_of: transition -> OuterLex.token list option
wenzelm@5828
    41
  val name: string -> transition -> transition
wenzelm@5828
    42
  val position: Position.T -> transition -> transition
wenzelm@14923
    43
  val source: OuterLex.token list -> transition -> transition
wenzelm@5828
    44
  val interactive: bool -> transition -> transition
wenzelm@5828
    45
  val print: transition -> transition
wenzelm@16607
    46
  val print': string -> transition -> transition
wenzelm@9010
    47
  val no_timing: transition -> transition
wenzelm@5828
    48
  val reset: transition -> transition
wenzelm@8465
    49
  val init: (bool -> node) -> (node -> unit) -> (node -> unit) -> transition -> transition
wenzelm@6689
    50
  val exit: transition -> transition
wenzelm@6689
    51
  val kill: transition -> transition
wenzelm@5828
    52
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    53
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@6689
    54
  val history: (node History.T -> node History.T) -> transition -> transition
wenzelm@5828
    55
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@7105
    56
  val init_theory: (bool -> theory) -> (theory -> unit) -> (theory -> unit)
wenzelm@6689
    57
    -> transition -> transition
berghofe@15237
    58
  val skip_proofs: bool ref
wenzelm@5828
    59
  val theory: (theory -> theory) -> transition -> transition
wenzelm@7612
    60
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@6689
    61
  val theory_to_proof: (bool -> theory -> ProofHistory.T) -> transition -> transition
wenzelm@5828
    62
  val proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition
wenzelm@6689
    63
  val proof': (bool -> ProofHistory.T -> ProofHistory.T) -> transition -> transition
berghofe@15237
    64
  val proof'': (ProofHistory.T -> ProofHistory.T) -> transition -> transition
wenzelm@5828
    65
  val proof_to_theory: (ProofHistory.T -> theory) -> transition -> transition
wenzelm@12315
    66
  val proof_to_theory': (bool -> ProofHistory.T -> theory) -> transition -> transition
berghofe@15237
    67
  val skip_proof: (int History.T -> int History.T) -> transition -> transition
berghofe@15237
    68
  val skip_proof_to_theory: (int History.T -> bool) -> transition -> transition
wenzelm@9512
    69
  val unknown_theory: transition -> transition
wenzelm@9512
    70
  val unknown_proof: transition -> transition
wenzelm@9512
    71
  val unknown_context: transition -> transition
wenzelm@5922
    72
  val exn_message: exn -> string
wenzelm@5828
    73
  val apply: bool -> transition -> state -> (state * (exn * string) option) option
berghofe@15431
    74
  val excursion_result: ((transition * (state -> state -> 'a -> 'a)) list * 'a) -> 'a
wenzelm@5828
    75
  val excursion: transition list -> unit
wenzelm@5828
    76
  val set_state: state -> unit
wenzelm@5828
    77
  val get_state: unit -> state
wenzelm@5828
    78
  val exn: unit -> (exn * string) option
wenzelm@5828
    79
  val >> : transition -> bool
wenzelm@14985
    80
  val >>> : transition list -> unit
nipkow@14091
    81
  type 'a isar
nipkow@14091
    82
  val loop: 'a isar -> unit
wenzelm@5828
    83
end;
wenzelm@5828
    84
wenzelm@6965
    85
structure Toplevel: TOPLEVEL =
wenzelm@5828
    86
struct
wenzelm@5828
    87
wenzelm@5828
    88
wenzelm@5828
    89
(** toplevel state **)
wenzelm@5828
    90
wenzelm@5828
    91
(* datatype node *)
wenzelm@5828
    92
wenzelm@5828
    93
datatype node =
wenzelm@5828
    94
  Theory of theory |
berghofe@15237
    95
  Proof of ProofHistory.T |
berghofe@15237
    96
  SkipProof of int History.T * theory;
wenzelm@5828
    97
wenzelm@6689
    98
fun str_of_node (Theory _) = "in theory mode"
berghofe@15237
    99
  | str_of_node _ = "in proof mode";
wenzelm@6689
   100
wenzelm@12423
   101
fun pretty_context thy = [Pretty.block
wenzelm@16452
   102
  [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
paulson@15633
   103
    Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]];
wenzelm@7308
   104
wenzelm@12423
   105
fun pretty_prf prf = Proof.pretty_state (ProofHistory.position prf) (ProofHistory.current prf);
wenzelm@8886
   106
wenzelm@12423
   107
fun pretty_node_context (Theory thy) = pretty_context thy
berghofe@15237
   108
  | pretty_node_context (Proof prf) = pretty_context (Proof.theory_of (ProofHistory.current prf))
berghofe@15237
   109
  | pretty_node_context _ = [];
wenzelm@7308
   110
wenzelm@12423
   111
fun pretty_node prf_only (Theory thy) = if prf_only then [] else pretty_context thy
berghofe@15237
   112
  | pretty_node _ (Proof prf) = pretty_prf prf
berghofe@15237
   113
  | pretty_node _ _ = [];
wenzelm@5828
   114
wenzelm@5828
   115
wenzelm@5828
   116
(* datatype state *)
wenzelm@5828
   117
wenzelm@8465
   118
datatype state = State of (node History.T * ((node -> unit) * (node -> unit))) option;
wenzelm@5828
   119
skalberg@15531
   120
val toplevel = State NONE;
wenzelm@5828
   121
skalberg@15531
   122
fun is_toplevel (State NONE) = true
wenzelm@7732
   123
  | is_toplevel _ = false;
wenzelm@7732
   124
skalberg@15531
   125
fun str_of_state (State NONE) = "at top level"
skalberg@15531
   126
  | str_of_state (State (SOME (node, _))) = str_of_node (History.current node);
wenzelm@6689
   127
wenzelm@6689
   128
wenzelm@6689
   129
(* prompt_state hook *)
wenzelm@6689
   130
wenzelm@8465
   131
fun prompt_state_default (State _) = Source.default_prompt;
wenzelm@6689
   132
wenzelm@6689
   133
val prompt_state_fn = ref prompt_state_default;
wenzelm@6689
   134
fun prompt_state state = ! prompt_state_fn state;
wenzelm@5828
   135
wenzelm@5828
   136
wenzelm@7308
   137
(* print state *)
wenzelm@5946
   138
skalberg@15531
   139
fun pretty_current_node _ (State NONE) = []
skalberg@15531
   140
  | pretty_current_node prt (State (SOME (node, _))) = prt (History.current node);
wenzelm@7308
   141
wenzelm@12423
   142
val print_state_context =
wenzelm@12423
   143
  Pretty.writeln o Pretty.chunks o pretty_current_node pretty_node_context;
wenzelm@5946
   144
wenzelm@12423
   145
fun pretty_state prf_only state =
wenzelm@11894
   146
  let val ref (begin_state, end_state, _) = Display.current_goals_markers in
wenzelm@12423
   147
    (case pretty_current_node (pretty_node prf_only) state of [] => []
wenzelm@12423
   148
    | prts =>
wenzelm@12423
   149
        (if begin_state = "" then [] else [Pretty.str begin_state]) @ prts @
wenzelm@12423
   150
        (if end_state = "" then [] else [Pretty.str end_state]))
wenzelm@5946
   151
  end;
wenzelm@5946
   152
wenzelm@12423
   153
fun print_state_default prf_only state = Pretty.writelns (pretty_state prf_only state);
wenzelm@5946
   154
val print_state_fn = ref print_state_default;
wenzelm@5946
   155
fun print_state state = ! print_state_fn state;
wenzelm@5946
   156
wenzelm@5946
   157
wenzelm@5828
   158
(* top node *)
wenzelm@5828
   159
wenzelm@6689
   160
exception UNDEF;
wenzelm@6689
   161
skalberg@15531
   162
fun node_history_of (State NONE) = raise UNDEF
skalberg@15531
   163
  | node_history_of (State (SOME (node, _))) = node;
wenzelm@6689
   164
wenzelm@6689
   165
val node_of = History.current o node_history_of;
wenzelm@5828
   166
wenzelm@6664
   167
fun node_case f g state =
wenzelm@5828
   168
  (case node_of state of
wenzelm@5828
   169
    Theory thy => f thy
berghofe@15237
   170
  | SkipProof (i, thy) => f thy
wenzelm@5828
   171
  | Proof prf => g (ProofHistory.current prf));
wenzelm@5828
   172
wenzelm@6664
   173
val theory_of = node_case I Proof.theory_of;
wenzelm@16452
   174
val sign_of = theory_of;
wenzelm@7501
   175
val context_of = node_case ProofContext.init Proof.context_of;
wenzelm@6664
   176
val proof_of = node_case (fn _ => raise UNDEF) I;
wenzelm@6664
   177
wenzelm@12049
   178
val enter_forward_proof = node_case Proof.init_state Proof.enter_forward;
wenzelm@5828
   179
wenzelm@5828
   180
wenzelm@15668
   181
wenzelm@5828
   182
(** toplevel transitions **)
wenzelm@5828
   183
wenzelm@16682
   184
val quiet = ref false;
wenzelm@16682
   185
val debug = ref false;
wenzelm@16682
   186
val timing = Output.timing;
wenzelm@16682
   187
val profiling = ref 0;
wenzelm@16682
   188
wenzelm@5828
   189
exception TERMINATE;
wenzelm@5990
   190
exception RESTART;
wenzelm@7022
   191
exception EXCURSION_FAIL of exn * string;
wenzelm@6689
   192
exception FAILURE of state * exn;
wenzelm@5828
   193
wenzelm@5828
   194
wenzelm@16452
   195
(* recovery from stale theories *)
wenzelm@6689
   196
wenzelm@7022
   197
(*note: proof commands should be non-destructive!*)
wenzelm@7022
   198
wenzelm@6689
   199
local
wenzelm@6689
   200
wenzelm@16452
   201
fun is_stale state = Context.is_stale (theory_of state) handle UNDEF => false;
wenzelm@6689
   202
wenzelm@16452
   203
fun checkpoint_node true (Theory thy) = Theory (Theory.checkpoint thy)
wenzelm@6689
   204
  | checkpoint_node _ node = node;
wenzelm@6689
   205
wenzelm@6689
   206
fun copy_node true (Theory thy) = Theory (Theory.copy thy)
wenzelm@6689
   207
  | copy_node _ node = node;
wenzelm@6689
   208
wenzelm@16729
   209
val stale_theory = ERROR_MESSAGE "Stale theory encountered after succesful execution!";
wenzelm@7022
   210
skalberg@15531
   211
fun return (result, NONE) = result
skalberg@15531
   212
  | return (result, SOME exn) = raise FAILURE (result, exn);
wenzelm@7022
   213
wenzelm@16729
   214
fun debug_trans f x =
wenzelm@16729
   215
  if ! debug then
wenzelm@16729
   216
    let val y = ref x in
wenzelm@16729
   217
      setmp Output.transform_exceptions false
wenzelm@16729
   218
        exception_trace (fn () => y := f x); ! y
wenzelm@16729
   219
    end
wenzelm@16729
   220
  else f x;
wenzelm@16729
   221
wenzelm@16729
   222
fun interruptible f x =
wenzelm@16729
   223
  let val y = ref x
wenzelm@16729
   224
  in raise_interrupt (fn () => y := f x) (); ! y end;
wenzelm@16729
   225
wenzelm@6689
   226
in
wenzelm@6689
   227
skalberg@15531
   228
fun node_trans _ _ _ (State NONE) = raise UNDEF
skalberg@15531
   229
  | node_trans int hist f (State (SOME (node, term))) =
wenzelm@6689
   230
      let
wenzelm@16046
   231
        val cont_node = History.map (checkpoint_node int) node;
wenzelm@16046
   232
        val back_node = History.map (copy_node int) cont_node;
wenzelm@16729
   233
        fun state nd = State (SOME (nd, term));
wenzelm@16729
   234
        fun normal_state nd = (state nd, NONE);
wenzelm@16729
   235
        fun error_state nd exn = (state nd, SOME exn);
wenzelm@6689
   236
wenzelm@16729
   237
        val (result, err) =
wenzelm@16729
   238
          cont_node
wenzelm@16729
   239
          |> ((fn nd => f int nd)
wenzelm@16729
   240
              |> (if hist then History.apply_copy (copy_node int) else History.map)
wenzelm@16729
   241
              |> debug_trans
wenzelm@16729
   242
              |> interruptible
wenzelm@16729
   243
              |> transform_error)
wenzelm@16729
   244
          |> normal_state
wenzelm@16729
   245
          handle exn => error_state cont_node exn;
wenzelm@6689
   246
      in
wenzelm@16729
   247
        if is_stale result
wenzelm@16729
   248
        then return (error_state back_node (if_none err stale_theory))
wenzelm@16729
   249
        else return (result, err)
wenzelm@6689
   250
      end;
wenzelm@6689
   251
wenzelm@6689
   252
end;
wenzelm@6689
   253
wenzelm@6689
   254
wenzelm@6689
   255
(* primitive transitions *)
wenzelm@6689
   256
wenzelm@16452
   257
(*Important note: recovery from stale theories is provided only for
wenzelm@6689
   258
  theory-level operations via MapCurrent and AppCurrent.  Other node
wenzelm@16452
   259
  or state operations should not touch theories at all.
wenzelm@6965
   260
wenzelm@6965
   261
  Also note that interrupts are enabled for Keep, MapCurrent, and
wenzelm@6965
   262
  AppCurrent only.*)
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@6689
   292
  | apply_tr int (MapCurrent f) state = node_trans int false f state
wenzelm@6689
   293
  | apply_tr int (AppCurrent f) state = node_trans 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@5828
   314
 {name: string,
wenzelm@5828
   315
  pos: Position.T,
wenzelm@14923
   316
  source: OuterLex.token list option,
wenzelm@5828
   317
  int_only: bool,
wenzelm@16607
   318
  print: string list,
wenzelm@9010
   319
  no_timing: bool,
wenzelm@5828
   320
  trans: trans list};
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@15668
   394
(*The skip_proofs flag allows proof scripts to be skipped during
wenzelm@15668
   395
  interactive proof in order to speed up processing of large
wenzelm@15668
   396
  theories. While in skipping mode, states are represented as
wenzelm@15668
   397
  SkipProof (h, thy), where h contains a counter for the number of
wenzelm@15668
   398
  open proof blocks.*)
berghofe@15237
   399
berghofe@15237
   400
val skip_proofs = ref false;
berghofe@15237
   401
wenzelm@6689
   402
fun theory f = app_current (fn _ => (fn Theory thy => Theory (f thy) | _ => raise UNDEF));
wenzelm@7612
   403
fun theory' f = app_current (fn int => (fn Theory thy => Theory (f int thy) | _ => raise UNDEF));
wenzelm@15668
   404
berghofe@15237
   405
fun theory_to_proof f = app_current (fn int =>
berghofe@15237
   406
  (fn Theory thy =>
wenzelm@15668
   407
        if ! skip_proofs then SkipProof (History.init (undo_limit int) 0,
berghofe@15237
   408
          fst (SkipProof.global_skip_proof int (ProofHistory.current (f int thy))))
berghofe@15237
   409
        else Proof (f int thy)
berghofe@15237
   410
    | _ => raise UNDEF));
wenzelm@15668
   411
berghofe@15237
   412
fun proof''' b f = map_current (fn int => (fn Proof prf => Proof (f int prf)
berghofe@15237
   413
  | SkipProof (h, thy) => if b then SkipProof (History.apply I h, thy) else raise UNDEF
berghofe@15237
   414
  | _ => raise UNDEF));
wenzelm@15668
   415
berghofe@15237
   416
val proof' = proof''' true;
wenzelm@6689
   417
val proof = proof' o K;
berghofe@15237
   418
val proof'' = proof''' false o K;
wenzelm@15668
   419
wenzelm@12315
   420
fun proof_to_theory' f =
berghofe@15237
   421
  map_current (fn int => (fn Proof prf => Theory (f int prf)
berghofe@15237
   422
    | SkipProof (h, thy) => if History.current h = 0 then Theory thy else raise UNDEF
berghofe@15237
   423
    | _ => raise UNDEF));
wenzelm@15668
   424
wenzelm@12315
   425
val proof_to_theory = proof_to_theory' o K;
wenzelm@15668
   426
berghofe@15237
   427
fun skip_proof f = map_current (fn int =>
berghofe@15237
   428
  (fn SkipProof (h, thy) => SkipProof (f h, thy) | _ => raise UNDEF));
wenzelm@15668
   429
berghofe@15237
   430
fun skip_proof_to_theory p = map_current (fn int =>
berghofe@15237
   431
  (fn SkipProof (h, thy) => if p h then Theory thy else raise UNDEF | _ => raise UNDEF));
wenzelm@5828
   432
wenzelm@9512
   433
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@9512
   434
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@9512
   435
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@9512
   436
wenzelm@5828
   437
wenzelm@5828
   438
wenzelm@5828
   439
(** toplevel transactions **)
wenzelm@5828
   440
wenzelm@5828
   441
(* print exceptions *)
wenzelm@5828
   442
wenzelm@15668
   443
local
wenzelm@15668
   444
wenzelm@15668
   445
fun with_context f xs =
wenzelm@15668
   446
  (case Context.get_context () of NONE => []
wenzelm@16452
   447
  | SOME thy => map (f thy) xs);
wenzelm@15668
   448
wenzelm@15668
   449
fun raised name [] = "exception " ^ name ^ " raised"
wenzelm@15668
   450
  | raised name [msg] = "exception " ^ name ^ " raised: " ^ msg
wenzelm@15668
   451
  | raised name msgs = cat_lines (("exception " ^ name ^ " raised:") :: msgs);
wenzelm@5828
   452
wenzelm@15668
   453
fun exn_msg _ TERMINATE = "Exit."
wenzelm@15668
   454
  | exn_msg _ RESTART = "Restart."
wenzelm@15668
   455
  | exn_msg _ Interrupt = "Interrupt."
wenzelm@15668
   456
  | exn_msg _ ERROR = "ERROR."
wenzelm@15668
   457
  | exn_msg _ (ERROR_MESSAGE msg) = msg
wenzelm@16452
   458
  | exn_msg detailed (EXCURSION_FAIL (exn, msg)) = cat_lines [exn_msg detailed exn, msg]
wenzelm@16452
   459
  | exn_msg detailed (Context.DATA_FAIL (exn, msg)) = cat_lines [exn_msg detailed exn, msg]
wenzelm@16607
   460
  | exn_msg detailed (Syntax.TRANSLATION_FAIL (exn, msg)) = cat_lines [exn_msg detailed exn, msg]
wenzelm@15668
   461
  | exn_msg false (THEORY (msg, _)) = msg
wenzelm@16452
   462
  | exn_msg true (THEORY (msg, thys)) = raised "THEORY" (msg :: map Context.str_of_thy thys)
wenzelm@15668
   463
  | exn_msg _ (ProofContext.CONTEXT (msg, _)) = msg
wenzelm@15668
   464
  | exn_msg _ (Proof.STATE (msg, _)) = msg
wenzelm@15668
   465
  | exn_msg _ (ProofHistory.FAIL msg) = msg
wenzelm@15668
   466
  | exn_msg detailed (MetaSimplifier.SIMPROC_FAIL (name, exn)) =
wenzelm@15668
   467
      fail_msg detailed "simproc" ((name, Position.none), exn)
wenzelm@15668
   468
  | exn_msg detailed (Attrib.ATTRIB_FAIL info) = fail_msg detailed "attribute" info
wenzelm@15668
   469
  | exn_msg detailed (Method.METHOD_FAIL info) = fail_msg detailed "method" info
wenzelm@15668
   470
  | exn_msg detailed (Antiquote.ANTIQUOTE_FAIL info) = fail_msg detailed "antiquotation" info
wenzelm@15668
   471
  | exn_msg false (Syntax.AST (msg, _)) = raised "AST" [msg]
wenzelm@15668
   472
  | exn_msg true (Syntax.AST (msg, asts)) =
wenzelm@15668
   473
      raised "AST" (msg :: map (Pretty.string_of o Syntax.pretty_ast) asts)
wenzelm@15668
   474
  | exn_msg false (TYPE (msg, _, _)) = raised "TYPE" [msg]
wenzelm@15668
   475
  | exn_msg true (TYPE (msg, Ts, ts)) = raised "TYPE" (msg ::
wenzelm@15668
   476
        with_context Sign.string_of_typ Ts @ with_context Sign.string_of_term ts)
wenzelm@15668
   477
  | exn_msg false (TERM (msg, _)) = raised "TERM" [msg]
wenzelm@15668
   478
  | exn_msg true (TERM (msg, ts)) = raised "TERM" (msg :: with_context Sign.string_of_term ts)
wenzelm@15668
   479
  | exn_msg false (THM (msg, _, _)) = raised "THM" [msg]
wenzelm@15668
   480
  | exn_msg true (THM (msg, i, thms)) =
wenzelm@15668
   481
      raised ("THM " ^ string_of_int i) (msg :: map Display.string_of_thm thms)
wenzelm@15668
   482
  | exn_msg _ Option = raised "Option" []
wenzelm@15668
   483
  | exn_msg _ UnequalLengths = raised "UnequalLengths" []
wenzelm@15668
   484
  | exn_msg _ Empty = raised "Empty" []
wenzelm@15668
   485
  | exn_msg _ Subscript = raised "Subscript" []
wenzelm@16197
   486
  | exn_msg _ (Fail msg) = raised "Fail" [msg]
wenzelm@15668
   487
  | exn_msg _ exn = General.exnMessage exn
wenzelm@15668
   488
and fail_msg detailed kind ((name, pos), exn) =
wenzelm@15668
   489
  "Error in " ^ kind ^ " " ^ quote name ^ Position.str_of pos ^ ":\n" ^ exn_msg detailed exn;
ballarin@15519
   490
wenzelm@15668
   491
in
wenzelm@15668
   492
wenzelm@15668
   493
fun exn_message exn = exn_msg (! debug) exn;
wenzelm@5828
   494
skalberg@15531
   495
fun print_exn NONE = ()
skalberg@15531
   496
  | print_exn (SOME (exn, s)) = error_msg (cat_lines [exn_message exn, s]);
wenzelm@5828
   497
wenzelm@15668
   498
end;
wenzelm@15668
   499
wenzelm@5828
   500
wenzelm@5828
   501
(* apply transitions *)
wenzelm@5828
   502
wenzelm@15668
   503
val quiet = ref false;
wenzelm@15668
   504
wenzelm@6664
   505
local
wenzelm@6664
   506
wenzelm@9010
   507
fun app int (tr as Transition {trans, int_only, print, no_timing, ...}) state =
wenzelm@5828
   508
  let
wenzelm@16682
   509
    val _ = conditional (not int andalso int_only) (fn () =>
wenzelm@16682
   510
      warning (command_msg "Interactive-only " tr));
wenzelm@16682
   511
wenzelm@16682
   512
    fun do_timing f x = (info (command_msg "" tr); timeap f x);
wenzelm@16682
   513
    fun do_profiling f x = profile (! profiling) f x;
wenzelm@16682
   514
wenzelm@6689
   515
    val (result, opt_exn) =
wenzelm@16729
   516
       state |> (apply_trans int trans
wenzelm@16729
   517
        |> (if ! profiling > 0 then do_profiling else I)
wenzelm@16729
   518
        |> (if ! profiling > 0 orelse ! timing andalso not no_timing then do_timing else I));
wenzelm@16607
   519
    val _ = conditional (int andalso not (! quiet) andalso
wenzelm@16607
   520
        exists (fn m => m mem_string print) ("" :: ! print_mode))
wenzelm@16607
   521
      (fn () => print_state false result);
skalberg@15570
   522
  in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) opt_exn) end;
wenzelm@6664
   523
wenzelm@6664
   524
in
wenzelm@5828
   525
wenzelm@6689
   526
fun apply int tr st =
wenzelm@6965
   527
  (case app int tr st of
skalberg@15531
   528
    (_, SOME TERMINATE) => NONE
skalberg@15531
   529
  | (_, SOME RESTART) => SOME (toplevel, NONE)
skalberg@15531
   530
  | (state', SOME (EXCURSION_FAIL exn_info)) => SOME (state', SOME exn_info)
skalberg@15531
   531
  | (state', SOME exn) => SOME (state', SOME (exn, at_command tr))
skalberg@15531
   532
  | (state', NONE) => SOME (state', NONE));
wenzelm@6664
   533
wenzelm@6664
   534
end;
wenzelm@5828
   535
wenzelm@5828
   536
wenzelm@5828
   537
(* excursion: toplevel -- apply transformers -- toplevel *)
wenzelm@5828
   538
wenzelm@6664
   539
local
wenzelm@6664
   540
wenzelm@5828
   541
fun excur [] x = x
wenzelm@9134
   542
  | excur ((tr, f) :: trs) (st, res) =
wenzelm@9134
   543
      (case apply false tr st of
skalberg@15531
   544
        SOME (st', NONE) =>
berghofe@15431
   545
          excur trs (st', transform_error (fn () => f st st' res) () handle exn =>
wenzelm@10324
   546
            raise EXCURSION_FAIL (exn, "Presentation failed\n" ^ at_command tr))
skalberg@15531
   547
      | SOME (st', SOME exn_info) => raise EXCURSION_FAIL exn_info
skalberg@15531
   548
      | NONE => raise EXCURSION_FAIL (TERMINATE, at_command tr));
wenzelm@5828
   549
wenzelm@6664
   550
in
wenzelm@6664
   551
wenzelm@9134
   552
fun excursion_result (trs, res) =
skalberg@15531
   553
  (case excur trs (State NONE, res) of
skalberg@15531
   554
    (State NONE, res') => res'
wenzelm@9134
   555
  | _ => raise ERROR_MESSAGE "Unfinished development at end of input")
wenzelm@9134
   556
  handle exn => error (exn_message exn);
wenzelm@9134
   557
wenzelm@5828
   558
fun excursion trs =
berghofe@15431
   559
  excursion_result (map (fn tr => (tr, fn _ => fn _ => fn _ => ())) trs, ());
wenzelm@7062
   560
wenzelm@6664
   561
end;
wenzelm@6664
   562
wenzelm@5828
   563
wenzelm@5828
   564
wenzelm@5828
   565
(** interactive transformations **)
wenzelm@5828
   566
wenzelm@5828
   567
(* the global state reference *)
wenzelm@5828
   568
skalberg@15531
   569
val global_state = ref (toplevel, NONE: (exn * string) option);
wenzelm@5828
   570
skalberg@15531
   571
fun set_state state = global_state := (state, NONE);
wenzelm@5828
   572
fun get_state () = fst (! global_state);
wenzelm@5828
   573
fun exn () = snd (! global_state);
wenzelm@5828
   574
wenzelm@5828
   575
wenzelm@6965
   576
(* the Isar source of transitions *)
wenzelm@6965
   577
nipkow@14091
   578
type 'a isar =
wenzelm@6965
   579
  (transition, (transition option,
wenzelm@12881
   580
    (OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token,
nipkow@14091
   581
      Position.T * (Symbol.symbol, (string, 'a) Source.source) Source.source)
wenzelm@12881
   582
          Source.source) Source.source) Source.source) Source.source) Source.source) Source.source;
wenzelm@6965
   583
wenzelm@6965
   584
wenzelm@5828
   585
(* apply transformers to global state *)
wenzelm@5828
   586
wenzelm@14985
   587
nonfix >> >>>;
wenzelm@5828
   588
wenzelm@5828
   589
fun >> tr =
wenzelm@5828
   590
  (case apply true tr (get_state ()) of
skalberg@15531
   591
    NONE => false
skalberg@15531
   592
  | SOME (state', exn_info) =>
wenzelm@5828
   593
      (global_state := (state', exn_info);
wenzelm@16729
   594
        print_exn exn_info;
wenzelm@5828
   595
        true));
wenzelm@5828
   596
wenzelm@14985
   597
fun >>> [] = ()
wenzelm@14985
   598
  | >>> (tr :: trs) = if >> tr then >>> trs else ();
wenzelm@14985
   599
wenzelm@7602
   600
(*Note: this is for Poly/ML only, we really do not intend to exhibit
wenzelm@7602
   601
  interrupts here*)
skalberg@15531
   602
fun get_interrupt src = SOME (Source.get_single src) handle Interrupt => NONE;
wenzelm@7602
   603
wenzelm@5828
   604
fun raw_loop src =
wenzelm@7602
   605
  (case get_interrupt (Source.set_prompt (prompt_state (get_state ())) src) of
skalberg@15531
   606
    NONE => (writeln "\nInterrupt."; raw_loop src)
skalberg@15531
   607
  | SOME NONE => ()
skalberg@15531
   608
  | SOME (SOME (tr, src')) => if >> tr then raw_loop src' else ());
wenzelm@5828
   609
wenzelm@12987
   610
fun loop src = ignore_interrupt raw_loop src;
wenzelm@5828
   611
wenzelm@5828
   612
end;