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