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