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