src/Pure/Isar/isar.ML
author wenzelm
Thu, 10 Jul 2008 20:02:55 +0200
changeset 27530 df14c9cbd21d
parent 27529 6a5ccbb1bca0
child 27533 85bbd045ac3e
permissions -rw-r--r--
export init_point; added kill, kill_proof; misc tuning;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/isar.ML
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
     4
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
     5
The global Isabelle/Isar state and main read-eval-print loop.
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
     6
*)
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
     7
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
     8
signature ISAR =
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
     9
sig
27530
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
    10
  val init_point: unit -> unit
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    11
  val state: unit -> Toplevel.state
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    12
  val exn: unit -> (exn * string) option
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    13
  val context: unit -> Proof.context
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    14
  val goal: unit -> thm
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    15
  val >> : Toplevel.transition -> bool
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    16
  val >>> : Toplevel.transition list -> unit
27529
6a5ccbb1bca0 added Isar.linear_undo;
wenzelm
parents: 27528
diff changeset
    17
  val linear_undo: int -> unit
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
    18
  val undo: int -> unit
27530
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
    19
  val kill: unit -> unit
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
    20
  val kill_proof: unit -> unit
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    21
  val crashes: exn list ref
26643
99f5407c05ef Isar.toplevel_loop: separate init/welcome flag;
wenzelm
parents: 26606
diff changeset
    22
  val toplevel_loop: {init: bool, welcome: bool, sync: bool, secure: bool} -> unit
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    23
  val loop: unit -> unit
26606
379596d12f25 replaced Isar loop variants by generic toplevel_loop;
wenzelm
parents: 26605
diff changeset
    24
  val main: unit -> unit
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    25
end;
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    26
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    27
structure Isar: ISAR =
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    28
struct
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    29
27432
c5ec309c6de8 replaced datatype kind by OuterKeyword.category;
wenzelm
parents: 27428
diff changeset
    30
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    31
(** individual toplevel commands **)
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    32
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    33
(* unique identification *)
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    34
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    35
type id = string;
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    36
val no_id : id = "";
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    37
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    38
fun identify tr =
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    39
  (case Toplevel.get_id tr of
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    40
    SOME id => (id, tr)
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    41
  | NONE =>
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    42
      let val id = "isabelle:" ^ serial_string ()
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    43
      in (id, Toplevel.put_id id tr) end);
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    44
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    45
27438
9b2427cc234e command: always keep transition, not just as initial status;
wenzelm
parents: 27432
diff changeset
    46
(* command category *)
9b2427cc234e command: always keep transition, not just as initial status;
wenzelm
parents: 27432
diff changeset
    47
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
    48
datatype category = Empty | Theory | Proof | Diag | Control;
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    49
27438
9b2427cc234e command: always keep transition, not just as initial status;
wenzelm
parents: 27432
diff changeset
    50
fun category_of tr =
9b2427cc234e command: always keep transition, not just as initial status;
wenzelm
parents: 27432
diff changeset
    51
  let val name = Toplevel.name_of tr in
9b2427cc234e command: always keep transition, not just as initial status;
wenzelm
parents: 27432
diff changeset
    52
    if name = "" then Empty
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
    53
    else if OuterKeyword.is_theory name then Theory
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
    54
    else if OuterKeyword.is_proof name then Proof
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
    55
    else if OuterKeyword.is_diag name then Diag
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
    56
    else Control
27438
9b2427cc234e command: always keep transition, not just as initial status;
wenzelm
parents: 27432
diff changeset
    57
  end;
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    58
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
    59
val is_theory = fn Theory => true | _ => false;
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
    60
val is_proper = fn Theory => true | Proof => true | _ => false;
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
    61
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    62
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    63
(* datatype command *)
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    64
27438
9b2427cc234e command: always keep transition, not just as initial status;
wenzelm
parents: 27432
diff changeset
    65
datatype status =
9b2427cc234e command: always keep transition, not just as initial status;
wenzelm
parents: 27432
diff changeset
    66
  Initial |
27518
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
    67
  Result of Toplevel.state * (exn * string) option;
27438
9b2427cc234e command: always keep transition, not just as initial status;
wenzelm
parents: 27432
diff changeset
    68
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    69
datatype command = Command of
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
    70
 {category: category,
27438
9b2427cc234e command: always keep transition, not just as initial status;
wenzelm
parents: 27432
diff changeset
    71
  transition: Toplevel.transition,
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    72
  status: status};
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    73
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
    74
fun make_command (category, transition, status) =
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
    75
  Command {category = category, transition = transition, status = status};
27438
9b2427cc234e command: always keep transition, not just as initial status;
wenzelm
parents: 27432
diff changeset
    76
9b2427cc234e command: always keep transition, not just as initial status;
wenzelm
parents: 27432
diff changeset
    77
val empty_command =
27518
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
    78
  make_command (Empty, Toplevel.empty, Result (Toplevel.toplevel, NONE));
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    79
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
    80
fun map_command f (Command {category, transition, status}) =
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
    81
  make_command (f (category, transition, status));
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    82
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
    83
fun map_status f = map_command (fn (category, transition, status) =>
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
    84
  (category, transition, f status));
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    85
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
    86
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
    87
(* global collection of identified commands *)
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    88
27518
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
    89
fun err_dup id = sys_error ("Duplicate command " ^ quote id);
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
    90
fun err_undef id = sys_error ("Unknown command " ^ quote id);
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
    91
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    92
local
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    93
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
    94
val empty_commands = Graph.empty: command Graph.T;
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    95
val global_commands = ref empty_commands;
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    96
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    97
in
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
    98
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
    99
fun change_commands f = NAMED_CRITICAL "Isar" (fn () => change global_commands f)
27518
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   100
  handle Graph.DUP id => err_dup id | Graph.UNDEF id => err_undef id;
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   101
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   102
fun init_commands () = change_commands (K empty_commands);
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   103
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   104
fun the_command id =
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   105
  let val Command cmd =
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   106
    if id = no_id then empty_command
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   107
    else (Graph.get_node (! global_commands) id handle Graph.UNDEF _ => err_undef id)
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   108
  in cmd end;
27518
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   109
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   110
fun prev_command id =
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   111
  if id = no_id then NONE
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   112
  else
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   113
    (case Graph.imm_preds (! global_commands) id handle Graph.UNDEF _ => err_undef id of
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   114
      [] => NONE
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   115
    | [prev] => SOME prev
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   116
    | _ => sys_error ("Non-linear command dependency " ^ quote id));
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   117
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   118
end;
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   119
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   120
27518
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   121
fun the_result id =
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   122
  (case the_command id of
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   123
    {status = Result res, ...} => res
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   124
  | {transition, ...} => error ("Unfinished command " ^ Toplevel.str_of transition));
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   125
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   126
val the_state = #1 o the_result;
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   127
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   128
fun new_command prev (id, cmd) =
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   129
  change_commands (Graph.new_node (id, cmd) #> prev <> no_id ? Graph.add_edge (prev, id));
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   130
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   131
fun dispose_command id = change_commands (Graph.del_nodes [id]);
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   132
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   133
fun change_command_status id f = change_commands (Graph.map_node id (map_status f));
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   134
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   135
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   136
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   137
(** TTY interaction **)
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   138
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   139
(* global point *)
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   140
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   141
local val global_point = ref no_id in
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   142
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   143
fun change_point f = NAMED_CRITICAL "Isar" (fn () => change global_point f);
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   144
fun point () = NAMED_CRITICAL "Isar" (fn () => ! global_point);
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   145
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   146
end;
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   147
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   148
fun set_point id = change_point (K id);
27530
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   149
fun init_point () = set_point no_id;
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   150
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   151
fun point_result () = NAMED_CRITICAL "Isar" (fn () =>
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   152
  let val id = point () in (id, the_result id) end);
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   153
27518
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   154
fun state () = #1 (#2 (point_result ()));
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   155
fun exn () = #2 (#2 (point_result ()));
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   156
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   157
fun context () =
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   158
  Toplevel.context_of (state ())
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   159
    handle Toplevel.UNDEF => error "Unknown context";
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   160
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   161
fun goal () =
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   162
  #2 (#2 (Proof.get_goal (Toplevel.proof_of (state ()))))
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   163
    handle Toplevel.UNDEF => error "No goal present";
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   164
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   165
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   166
(* interactive state transformations --- NOT THREAD-SAFE! *)
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   167
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   168
nonfix >> >>>;
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   169
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   170
fun >> raw_tr =
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   171
  let
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   172
    val (id, tr) = identify raw_tr;
27518
1e5f48e01e5e misc tuning;
wenzelm
parents: 27501
diff changeset
   173
    val (prev, (prev_state, _)) = point_result ();
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   174
    val category = category_of tr;
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   175
    val _ = new_command prev (id, make_command (category, tr, Initial));
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   176
  in
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   177
    (case Toplevel.transition true tr prev_state of
27501
632ee56c2c0b global commands: explicit graph;
wenzelm
parents: 27438
diff changeset
   178
      NONE => (dispose_command id; false)
27527
02b2c9eab4e2 tty interaction: do not move point after error;
wenzelm
parents: 27524
diff changeset
   179
    | SOME (result as (_, err)) =>
02b2c9eab4e2 tty interaction: do not move point after error;
wenzelm
parents: 27524
diff changeset
   180
        (change_command_status id (K (Result result));
02b2c9eab4e2 tty interaction: do not move point after error;
wenzelm
parents: 27524
diff changeset
   181
          Option.map (Toplevel.error_msg tr) err;
02b2c9eab4e2 tty interaction: do not move point after error;
wenzelm
parents: 27524
diff changeset
   182
          if is_some err orelse category = Control then dispose_command id
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   183
          else set_point id;
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   184
          true))
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   185
  end;
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   186
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   187
fun >>> [] = ()
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   188
  | >>> (tr :: trs) = if >> tr then >>> trs else ();
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   189
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   190
27530
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   191
(* implicit navigation wrt. proper commands *)
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   192
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   193
local
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   194
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   195
fun err_undo () = error "Undo history exhausted";
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   196
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   197
fun get_prev id = the_default no_id (prev_command id);
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   198
27530
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   199
fun find_category which id =
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   200
  (case #category (the_command id) of
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   201
    Empty => err_undo ()
27530
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   202
  | category => if which category then id else find_category which (get_prev id));
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   203
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   204
fun find_begin_theory id =
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   205
  if id = no_id then err_undo ()
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   206
  else if is_some (Toplevel.init_of (#transition (the_command id))) then id
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   207
  else find_begin_theory (get_prev id);
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   208
27529
6a5ccbb1bca0 added Isar.linear_undo;
wenzelm
parents: 27528
diff changeset
   209
fun undo_command id =
27530
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   210
  (case Toplevel.init_of (#transition (the_command id)) of
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   211
    SOME name => get_prev id before ThyInfo.kill_thy name
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   212
  | NONE => get_prev id);
27529
6a5ccbb1bca0 added Isar.linear_undo;
wenzelm
parents: 27528
diff changeset
   213
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   214
in
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   215
27530
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   216
fun linear_undo n = change_point (funpow n (fn id => undo_command (find_category is_proper id)));
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   217
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   218
fun undo n = change_point (funpow n (fn id => undo_command
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   219
  (find_category (if Toplevel.is_proof (the_state id) then is_proper else is_theory) id)));
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   220
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   221
fun kill () = change_point (fn id => undo_command
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   222
  (if Toplevel.is_proof (the_state id) then find_category is_theory id else find_begin_theory id));
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   223
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   224
fun kill_proof () = change_point (fn id =>
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   225
  if Toplevel.is_proof (the_state id) then undo_command (find_category is_theory id)
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   226
  else raise Toplevel.UNDEF);
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   227
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   228
end;
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   229
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   230
26606
379596d12f25 replaced Isar loop variants by generic toplevel_loop;
wenzelm
parents: 26605
diff changeset
   231
(* toplevel loop *)
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   232
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   233
val crashes = ref ([]: exn list);
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   234
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   235
local
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   236
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   237
fun raw_loop secure src =
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   238
  let
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   239
    fun check_secure () =
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   240
      (if secure then warning "Secure loop -- cannot exit to ML" else (); secure);
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   241
    val prev = point ();
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   242
    val prev_name = Toplevel.name_of (#transition (the_command prev));
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   243
    val prompt_markup =
27524
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   244
      prev <> no_id ? Markup.markup
cd95da386e9a provide old-style undo operation (still unused);
wenzelm
parents: 27518
diff changeset
   245
        (Markup.properties [(Markup.idN, prev), (Markup.nameN, prev_name)] Markup.prompt);
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   246
  in
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   247
    (case Source.get_single (Source.set_prompt (prompt_markup Source.default_prompt) src) of
26606
379596d12f25 replaced Isar loop variants by generic toplevel_loop;
wenzelm
parents: 26605
diff changeset
   248
      NONE => if secure then quit () else ()
379596d12f25 replaced Isar loop variants by generic toplevel_loop;
wenzelm
parents: 26605
diff changeset
   249
    | SOME (tr, src') => if >> tr orelse check_secure () then raw_loop secure src' else ())
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   250
    handle exn => (Output.error_msg (Toplevel.exn_message exn) handle crash =>
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   251
      (CRITICAL (fn () => change crashes (cons crash));
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   252
        warning "Recovering after Isar toplevel crash -- see also Isar.crashes");
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   253
      raw_loop secure src)
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   254
  end;
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   255
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   256
in
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   257
27428
f92d47cdc0de explicit identification of toplevel commands, with status etc.;
wenzelm
parents: 26643
diff changeset
   258
fun toplevel_loop {init, welcome, sync, secure} =
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   259
 (Context.set_thread_data NONE;
27530
df14c9cbd21d export init_point;
wenzelm
parents: 27529
diff changeset
   260
  if init then (init_point (); init_commands ()) else ();
26643
99f5407c05ef Isar.toplevel_loop: separate init/welcome flag;
wenzelm
parents: 26606
diff changeset
   261
  if welcome then writeln (Session.welcome ()) else ();
26606
379596d12f25 replaced Isar loop variants by generic toplevel_loop;
wenzelm
parents: 26605
diff changeset
   262
  uninterruptible (fn _ => fn () => raw_loop secure (OuterSyntax.isar sync)) ());
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   263
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   264
end;
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   265
26643
99f5407c05ef Isar.toplevel_loop: separate init/welcome flag;
wenzelm
parents: 26606
diff changeset
   266
fun loop () =
99f5407c05ef Isar.toplevel_loop: separate init/welcome flag;
wenzelm
parents: 26606
diff changeset
   267
  toplevel_loop {init = false, welcome = false, sync = false, secure = Secure.is_secure ()};
27528
wenzelm
parents: 27527
diff changeset
   268
26643
99f5407c05ef Isar.toplevel_loop: separate init/welcome flag;
wenzelm
parents: 26606
diff changeset
   269
fun main () =
99f5407c05ef Isar.toplevel_loop: separate init/welcome flag;
wenzelm
parents: 26606
diff changeset
   270
  toplevel_loop {init = true, welcome = true, sync = false, secure = Secure.is_secure ()};
26605
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   271
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   272
end;
24e60e823d22 The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff changeset
   273