author | ballarin |
Wed, 19 Nov 2008 16:58:33 +0100 | |
changeset 28849 | 9458d7a6388a |
parent 28306 | e2f091391865 |
child 29313 | 6852248da4b4 |
permissions | -rw-r--r-- |
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 |
27616 | 10 |
type id = string |
11 |
val no_id: id |
|
12 |
val create_command: Toplevel.transition -> id |
|
27530 | 13 |
val init_point: unit -> unit |
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
14 |
val state: unit -> Toplevel.state |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
15 |
val context: unit -> Proof.context |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
16 |
val goal: unit -> thm |
27533 | 17 |
val print: unit -> unit |
27616 | 18 |
val exn: unit -> (exn * string) option |
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
19 |
val >> : Toplevel.transition -> bool |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
20 |
val >>> : Toplevel.transition list -> unit |
27529 | 21 |
val linear_undo: int -> unit |
27524 | 22 |
val undo: int -> unit |
27530 | 23 |
val kill: unit -> unit |
24 |
val kill_proof: unit -> unit |
|
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
25 |
val crashes: exn list ref |
26643 | 26 |
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
|
27 |
val loop: unit -> unit |
26606
379596d12f25
replaced Isar loop variants by generic toplevel_loop;
wenzelm
parents:
26605
diff
changeset
|
28 |
val main: unit -> unit |
27616 | 29 |
val insert_command: id -> id -> unit |
30 |
val remove_command: id -> unit |
|
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
31 |
end; |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
32 |
|
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
33 |
structure Isar: ISAR = |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
34 |
struct |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
35 |
|
27432 | 36 |
|
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
37 |
(** individual toplevel commands **) |
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
38 |
|
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
39 |
(* unique identification *) |
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
40 |
|
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
41 |
type id = string; |
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
42 |
val no_id : id = ""; |
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
43 |
|
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
44 |
fun identify tr = |
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
45 |
(case Toplevel.get_id tr of |
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
46 |
SOME id => (id, tr) |
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
47 |
| NONE => |
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
48 |
let val id = |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
49 |
if ! Toplevel.debug then "isabelle:" ^ Toplevel.name_of tr ^ serial_string () |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
50 |
else "isabelle:" ^ serial_string () |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
51 |
in (id, Toplevel.put_id id tr) end); |
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
52 |
|
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
53 |
|
27438
9b2427cc234e
command: always keep transition, not just as initial status;
wenzelm
parents:
27432
diff
changeset
|
54 |
(* command category *) |
9b2427cc234e
command: always keep transition, not just as initial status;
wenzelm
parents:
27432
diff
changeset
|
55 |
|
27524 | 56 |
datatype category = Empty | Theory | Proof | Diag | Control; |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
57 |
|
27438
9b2427cc234e
command: always keep transition, not just as initial status;
wenzelm
parents:
27432
diff
changeset
|
58 |
fun category_of tr = |
9b2427cc234e
command: always keep transition, not just as initial status;
wenzelm
parents:
27432
diff
changeset
|
59 |
let val name = Toplevel.name_of tr in |
9b2427cc234e
command: always keep transition, not just as initial status;
wenzelm
parents:
27432
diff
changeset
|
60 |
if name = "" then Empty |
27524 | 61 |
else if OuterKeyword.is_theory name then Theory |
62 |
else if OuterKeyword.is_proof name then Proof |
|
63 |
else if OuterKeyword.is_diag name then Diag |
|
64 |
else Control |
|
27438
9b2427cc234e
command: always keep transition, not just as initial status;
wenzelm
parents:
27432
diff
changeset
|
65 |
end; |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
66 |
|
27524 | 67 |
val is_theory = fn Theory => true | _ => false; |
68 |
val is_proper = fn Theory => true | Proof => true | _ => false; |
|
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
69 |
val is_regular = fn Control => false | _ => true; |
27524 | 70 |
|
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
71 |
|
27616 | 72 |
(* command status *) |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
73 |
|
27438
9b2427cc234e
command: always keep transition, not just as initial status;
wenzelm
parents:
27432
diff
changeset
|
74 |
datatype status = |
27616 | 75 |
Unprocessed | |
76 |
Running | |
|
77 |
Failed of exn * string | |
|
78 |
Finished of Toplevel.state; |
|
79 |
||
80 |
fun status_markup Unprocessed = Markup.unprocessed |
|
81 |
| status_markup Running = Markup.running |
|
82 |
| status_markup (Failed _) = Markup.failed |
|
83 |
| status_markup (Finished _) = Markup.finished; |
|
84 |
||
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
85 |
fun run int tr state = |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
86 |
(case Toplevel.transition int tr state of |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
87 |
NONE => NONE |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
88 |
| SOME (_, SOME err) => (Toplevel.error_msg tr err; SOME (Failed err)) |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
89 |
| SOME (state', NONE) => SOME (Finished state')); |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
90 |
|
27616 | 91 |
|
92 |
(* datatype command *) |
|
27438
9b2427cc234e
command: always keep transition, not just as initial status;
wenzelm
parents:
27432
diff
changeset
|
93 |
|
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
94 |
datatype command = Command of |
27501 | 95 |
{category: category, |
27438
9b2427cc234e
command: always keep transition, not just as initial status;
wenzelm
parents:
27432
diff
changeset
|
96 |
transition: Toplevel.transition, |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
97 |
status: status}; |
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
98 |
|
27501 | 99 |
fun make_command (category, transition, status) = |
100 |
Command {category = category, transition = transition, status = status}; |
|
27438
9b2427cc234e
command: always keep transition, not just as initial status;
wenzelm
parents:
27432
diff
changeset
|
101 |
|
9b2427cc234e
command: always keep transition, not just as initial status;
wenzelm
parents:
27432
diff
changeset
|
102 |
val empty_command = |
27616 | 103 |
make_command (Empty, Toplevel.empty, Finished Toplevel.toplevel); |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
104 |
|
27501 | 105 |
fun map_command f (Command {category, transition, status}) = |
106 |
make_command (f (category, transition, status)); |
|
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
107 |
|
27501 | 108 |
fun map_status f = map_command (fn (category, transition, status) => |
109 |
(category, transition, f status)); |
|
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
110 |
|
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
111 |
|
27501 | 112 |
(* global collection of identified commands *) |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
113 |
|
27518 | 114 |
fun err_dup id = sys_error ("Duplicate command " ^ quote id); |
115 |
fun err_undef id = sys_error ("Unknown command " ^ quote id); |
|
116 |
||
27616 | 117 |
local val global_commands = ref (Graph.empty: command Graph.T) in |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
118 |
|
27501 | 119 |
fun change_commands f = NAMED_CRITICAL "Isar" (fn () => change global_commands f) |
27616 | 120 |
handle Graph.DUP bad => err_dup bad | Graph.UNDEF bad => err_undef bad; |
121 |
||
122 |
fun get_commands () = NAMED_CRITICAL "Isar" (fn () => ! global_commands); |
|
27501 | 123 |
|
27616 | 124 |
end; |
125 |
||
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
126 |
fun add_edge (id1, id2) = |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
127 |
if id1 = no_id orelse id2 = no_id then I else Graph.add_edge (id1, id2); |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
128 |
|
27616 | 129 |
|
130 |
fun init_commands () = change_commands (K Graph.empty); |
|
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
131 |
|
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
132 |
fun the_command id = |
27524 | 133 |
let val Command cmd = |
134 |
if id = no_id then empty_command |
|
27616 | 135 |
else (Graph.get_node (get_commands ()) id handle Graph.UNDEF bad => err_undef bad) |
27524 | 136 |
in cmd end; |
27518 | 137 |
|
138 |
fun prev_command id = |
|
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
139 |
if id = no_id then no_id |
27518 | 140 |
else |
27616 | 141 |
(case Graph.imm_preds (get_commands ()) id handle Graph.UNDEF bad => err_undef bad of |
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
142 |
[] => no_id |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
143 |
| [prev] => prev |
27518 | 144 |
| _ => sys_error ("Non-linear command dependency " ^ quote id)); |
27501 | 145 |
|
27616 | 146 |
fun next_commands id = |
147 |
if id = no_id then [] |
|
148 |
else Graph.imm_succs (get_commands ()) id handle Graph.UNDEF bad => err_undef bad; |
|
149 |
||
150 |
fun descendant_commands ids = |
|
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
151 |
Graph.all_succs (get_commands ()) (distinct (op =) (filter_out (fn id => id = no_id) ids)) |
27616 | 152 |
handle Graph.UNDEF bad => err_undef bad; |
153 |
||
154 |
||
155 |
(* maintain status *) |
|
156 |
||
157 |
fun report_status markup id = Toplevel.status (#transition (the_command id)) markup; |
|
158 |
||
27662 | 159 |
fun update_status status id = change_commands (Graph.map_node id (map_status (K status))); |
160 |
||
161 |
fun report_update_status status id = |
|
162 |
change_commands (Graph.map_node id (map_status (fn old_status => |
|
163 |
let val markup = status_markup status |
|
164 |
in if markup <> status_markup old_status then report_status markup id else (); status end))); |
|
27501 | 165 |
|
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
166 |
|
27616 | 167 |
(* create and dispose commands *) |
27524 | 168 |
|
27616 | 169 |
fun create_command raw_tr = |
170 |
let |
|
171 |
val (id, tr) = identify raw_tr; |
|
172 |
val cmd = make_command (category_of tr, tr, Unprocessed); |
|
173 |
val _ = change_commands (Graph.new_node (id, cmd)); |
|
174 |
in id end; |
|
175 |
||
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
176 |
fun dispose_commands ids = |
27616 | 177 |
let |
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
178 |
val desc = descendant_commands ids; |
27616 | 179 |
val _ = List.app (report_status Markup.disposed) desc; |
180 |
val _ = change_commands (Graph.del_nodes desc); |
|
181 |
in () end; |
|
27606 | 182 |
|
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
183 |
|
27616 | 184 |
(* final state *) |
27501 | 185 |
|
27616 | 186 |
fun the_state id = |
187 |
(case the_command id of |
|
188 |
{status = Finished state, ...} => state |
|
189 |
| {transition, ...} => error ("Unfinished command " ^ Toplevel.str_of transition)); |
|
27501 | 190 |
|
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
191 |
|
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
192 |
|
27616 | 193 |
(** TTY model -- single-threaded **) |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
194 |
|
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
195 |
(* global point *) |
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
196 |
|
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
197 |
local val global_point = ref no_id in |
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
198 |
|
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
199 |
fun change_point f = NAMED_CRITICAL "Isar" (fn () => change global_point f); |
27524 | 200 |
fun point () = NAMED_CRITICAL "Isar" (fn () => ! global_point); |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
201 |
|
27501 | 202 |
end; |
203 |
||
27616 | 204 |
|
27524 | 205 |
fun set_point id = change_point (K id); |
27530 | 206 |
fun init_point () = set_point no_id; |
27524 | 207 |
|
27616 | 208 |
fun point_state () = NAMED_CRITICAL "Isar" (fn () => |
209 |
let val id = point () in (id, the_state id) end); |
|
27501 | 210 |
|
27616 | 211 |
fun state () = #2 (point_state ()); |
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
212 |
|
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
213 |
fun context () = |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
214 |
Toplevel.context_of (state ()) |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
215 |
handle Toplevel.UNDEF => error "Unknown context"; |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
216 |
|
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
217 |
fun goal () = |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
218 |
#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
|
219 |
handle Toplevel.UNDEF => error "No goal present"; |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
220 |
|
27533 | 221 |
fun print () = Toplevel.print_state false (state ()); |
222 |
||
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
223 |
|
27616 | 224 |
(* global failure status *) |
225 |
||
226 |
local val global_exn = ref (NONE: (exn * string) option) in |
|
227 |
||
228 |
fun set_exn err = global_exn := err; |
|
229 |
fun exn () = ! global_exn; |
|
230 |
||
231 |
end; |
|
232 |
||
233 |
||
234 |
(* interactive state transformations *) |
|
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
235 |
|
28306 | 236 |
fun op >> raw_tr = |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
237 |
let |
27616 | 238 |
val id = create_command raw_tr; |
239 |
val {category, transition = tr, ...} = the_command id; |
|
240 |
val (prev, prev_state) = point_state (); |
|
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
241 |
val _ = |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
242 |
if is_regular category |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
243 |
then (dispose_commands (next_commands prev); change_commands (add_edge (prev, id))) |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
244 |
else (); |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
245 |
in |
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
246 |
(case run true tr prev_state of |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
247 |
NONE => false |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
248 |
| SOME (status as Failed err) => (update_status status id; set_exn (SOME err); true) |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
249 |
| SOME status => |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
250 |
(update_status status id; set_exn NONE; |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
251 |
if is_regular category then set_point id else (); |
27616 | 252 |
true)) |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
253 |
end; |
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
254 |
|
28306 | 255 |
fun op >>> [] = () |
256 |
| op >>> (tr :: trs) = if op >> tr then op >>> trs else (); |
|
28300
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
257 |
|
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
258 |
|
27530 | 259 |
(* implicit navigation wrt. proper commands *) |
27524 | 260 |
|
261 |
local |
|
262 |
||
263 |
fun err_undo () = error "Undo history exhausted"; |
|
264 |
||
27530 | 265 |
fun find_category which id = |
27524 | 266 |
(case #category (the_command id) of |
267 |
Empty => err_undo () |
|
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
268 |
| category => if which category then id else find_category which (prev_command id)); |
27530 | 269 |
|
270 |
fun find_begin_theory id = |
|
271 |
if id = no_id then err_undo () |
|
272 |
else if is_some (Toplevel.init_of (#transition (the_command id))) then id |
|
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
273 |
else find_begin_theory (prev_command id); |
27524 | 274 |
|
27529 | 275 |
fun undo_command id = |
27530 | 276 |
(case Toplevel.init_of (#transition (the_command id)) of |
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
277 |
SOME name => prev_command id before ThyInfo.kill_thy name |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
278 |
| NONE => prev_command id); |
27529 | 279 |
|
27524 | 280 |
in |
281 |
||
27530 | 282 |
fun linear_undo n = change_point (funpow n (fn id => undo_command (find_category is_proper id))); |
283 |
||
284 |
fun undo n = change_point (funpow n (fn id => undo_command |
|
285 |
(find_category (if Toplevel.is_proof (the_state id) then is_proper else is_theory) id))); |
|
286 |
||
287 |
fun kill () = change_point (fn id => undo_command |
|
288 |
(if Toplevel.is_proof (the_state id) then find_category is_theory id else find_begin_theory id)); |
|
289 |
||
290 |
fun kill_proof () = change_point (fn id => |
|
291 |
if Toplevel.is_proof (the_state id) then undo_command (find_category is_theory id) |
|
292 |
else raise Toplevel.UNDEF); |
|
27524 | 293 |
|
294 |
end; |
|
295 |
||
296 |
||
26606
379596d12f25
replaced Isar loop variants by generic toplevel_loop;
wenzelm
parents:
26605
diff
changeset
|
297 |
(* toplevel loop *) |
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
298 |
|
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
299 |
val crashes = ref ([]: exn list); |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
300 |
|
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
301 |
local |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
302 |
|
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
303 |
fun raw_loop secure src = |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
304 |
let |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
305 |
fun check_secure () = |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
306 |
(if secure then warning "Secure loop -- cannot exit to ML" else (); secure); |
27524 | 307 |
val prev = point (); |
308 |
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
|
309 |
val prompt_markup = |
27524 | 310 |
prev <> no_id ? Markup.markup |
311 |
(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
|
312 |
in |
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
313 |
(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
|
314 |
NONE => if secure then quit () else () |
28306 | 315 |
| SOME (tr, src') => if op >> 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
|
316 |
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
|
317 |
(CRITICAL (fn () => change crashes (cons crash)); |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
318 |
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
|
319 |
raw_loop secure src) |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
320 |
end; |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
321 |
|
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
322 |
in |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
323 |
|
27428
f92d47cdc0de
explicit identification of toplevel commands, with status etc.;
wenzelm
parents:
26643
diff
changeset
|
324 |
fun toplevel_loop {init, welcome, sync, secure} = |
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
325 |
(Context.set_thread_data NONE; |
27530 | 326 |
if init then (init_point (); init_commands ()) else (); |
26643 | 327 |
if welcome then writeln (Session.welcome ()) else (); |
26606
379596d12f25
replaced Isar loop variants by generic toplevel_loop;
wenzelm
parents:
26605
diff
changeset
|
328 |
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
|
329 |
|
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
330 |
end; |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
331 |
|
26643 | 332 |
fun loop () = |
333 |
toplevel_loop {init = false, welcome = false, sync = false, secure = Secure.is_secure ()}; |
|
27528 | 334 |
|
26643 | 335 |
fun main () = |
336 |
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
|
337 |
|
27616 | 338 |
|
339 |
||
340 |
(** editor model **) |
|
341 |
||
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
342 |
(* run commands *) |
27616 | 343 |
|
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
344 |
fun try_run id = |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
345 |
(case try the_state (prev_command id) of |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
346 |
NONE => () |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
347 |
| SOME state => |
27622 | 348 |
(case run true (#transition (the_command id)) state of |
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
349 |
NONE => () |
27662 | 350 |
| SOME status => report_update_status status id)); |
28300
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
351 |
|
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
352 |
fun rerun_commands ids = |
27662 | 353 |
(List.app (report_update_status Unprocessed) ids; List.app try_run ids); |
27616 | 354 |
|
355 |
||
356 |
(* modify document *) |
|
357 |
||
358 |
fun insert_command prev id = NAMED_CRITICAL "Isar" (fn () => |
|
359 |
let |
|
360 |
val nexts = next_commands prev; |
|
361 |
val _ = change_commands |
|
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
362 |
(fold (fn next => Graph.del_edge (prev, next)) nexts #> add_edge (prev, id) #> |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
363 |
fold (fn next => Graph.add_edge (id, next)) nexts); |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
364 |
in descendant_commands [id] end) |> rerun_commands; |
27616 | 365 |
|
366 |
fun remove_command id = NAMED_CRITICAL "Isar" (fn () => |
|
367 |
let |
|
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
368 |
val prev = prev_command id; |
27616 | 369 |
val nexts = next_commands id; |
27620
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
370 |
val _ = change_commands |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
371 |
(fold (fn next => Graph.del_edge (id, next)) nexts #> |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
372 |
fold (fn next => add_edge (prev, next)) nexts); |
dfecac40e4be
identify: more informative id in Toplevel.debug mode;
wenzelm
parents:
27616
diff
changeset
|
373 |
in descendant_commands nexts end) |> rerun_commands; |
27616 | 374 |
|
28300
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
375 |
|
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
376 |
(* concrete syntax *) |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
377 |
|
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
378 |
local |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
379 |
|
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
380 |
structure P = OuterParse and K = OuterKeyword; |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
381 |
val op >> = Scan.>>; |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
382 |
|
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
383 |
in |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
384 |
|
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
385 |
val _ = |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
386 |
OuterSyntax.improper_command "Isar.command" "define command (Isar editor model)" K.control |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
387 |
(P.props_text >> (fn (pos, str) => |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
388 |
Toplevel.no_timing o Toplevel.imperative (fn () => |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
389 |
ignore (create_command (OuterSyntax.prepare_command pos str))))); |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
390 |
|
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
391 |
val _ = |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
392 |
OuterSyntax.improper_command "Isar.insert" "insert command (Isar editor model)" K.control |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
393 |
(P.string -- P.string >> (fn (prev, id) => |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
394 |
Toplevel.no_timing o Toplevel.imperative (fn () => insert_command prev id))); |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
395 |
|
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
396 |
val _ = |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
397 |
OuterSyntax.improper_command "Isar.remove" "remove command (Isar editor model)" K.control |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
398 |
(P.string >> (fn id => |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
399 |
Toplevel.no_timing o Toplevel.imperative (fn () => remove_command id))); |
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
400 |
|
26605
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
401 |
end; |
24e60e823d22
The global Isabelle/Isar state and main read-eval-print loop.
wenzelm
parents:
diff
changeset
|
402 |
|
28300
111fc1879250
moved Isar editor commands from isar_syn.ML to isar.ML;
wenzelm
parents:
27662
diff
changeset
|
403 |
end; |