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