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