5829
|
1 |
(* Title: Pure/Isar/outer_syntax.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
The global Isabelle/Isar outer syntax.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature BASIC_OUTER_SYNTAX =
|
|
9 |
sig
|
|
10 |
val main: unit -> unit
|
5883
|
11 |
val loop: unit -> unit
|
6860
|
12 |
val sync_main: unit -> unit
|
|
13 |
val sync_loop: unit -> unit
|
5829
|
14 |
val help: unit -> unit
|
|
15 |
end;
|
|
16 |
|
|
17 |
signature OUTER_SYNTAX =
|
|
18 |
sig
|
|
19 |
include BASIC_OUTER_SYNTAX
|
6722
|
20 |
structure Keyword:
|
|
21 |
sig
|
|
22 |
val control: string
|
|
23 |
val diag: string
|
|
24 |
val thy_begin: string
|
7104
|
25 |
val thy_switch: string
|
6722
|
26 |
val thy_end: string
|
|
27 |
val thy_heading: string
|
|
28 |
val thy_decl: string
|
|
29 |
val thy_goal: string
|
|
30 |
val qed: string
|
6733
|
31 |
val qed_block: string
|
6722
|
32 |
val prf_goal: string
|
|
33 |
val prf_block: string
|
|
34 |
val prf_chain: string
|
|
35 |
val prf_decl: string
|
6868
|
36 |
val prf_asm: string
|
7676
|
37 |
val prf_asm_goal: string
|
6722
|
38 |
val prf_script: string
|
|
39 |
val kinds: string list
|
|
40 |
end
|
5829
|
41 |
type token
|
|
42 |
type parser
|
6722
|
43 |
val command: string -> string -> string ->
|
6373
|
44 |
(token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
|
6722
|
45 |
val improper_command: string -> string -> string ->
|
6373
|
46 |
(token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
|
7026
|
47 |
val dest_keywords: unit -> string list
|
|
48 |
val dest_parsers: unit -> (string * string * string * bool) list
|
5883
|
49 |
val print_outer_syntax: unit -> unit
|
7367
|
50 |
val print_help: Toplevel.transition -> Toplevel.transition
|
5829
|
51 |
val add_keywords: string list -> unit
|
|
52 |
val add_parsers: parser list -> unit
|
7683
|
53 |
val token_source: (string, 'a) Source.source * Position.T -> (OuterLex.token,
|
|
54 |
Position.T * (Symbol.symbol, (string, 'a) Source.source) Source.source) Source.source
|
6247
|
55 |
val theory_header: token list -> (string * string list * (string * bool) list) * token list
|
7192
|
56 |
val deps_thy: string -> Path.T -> string list * Path.T list
|
6199
|
57 |
val load_thy: string -> bool -> bool -> Path.T -> unit
|
7333
|
58 |
val isar: bool -> bool -> Toplevel.isar
|
5829
|
59 |
end;
|
|
60 |
|
|
61 |
structure OuterSyntax: OUTER_SYNTAX =
|
|
62 |
struct
|
|
63 |
|
6860
|
64 |
structure P = OuterParse;
|
|
65 |
|
5829
|
66 |
|
|
67 |
(** outer syntax **)
|
|
68 |
|
6722
|
69 |
(* command keyword classification *)
|
|
70 |
|
|
71 |
structure Keyword =
|
|
72 |
struct
|
|
73 |
val control = "control";
|
|
74 |
val diag = "diag";
|
|
75 |
val thy_begin = "theory-begin";
|
7104
|
76 |
val thy_switch = "theory-switch";
|
6722
|
77 |
val thy_end = "theory-end";
|
|
78 |
val thy_heading = "theory-heading";
|
|
79 |
val thy_decl = "theory-decl";
|
|
80 |
val thy_goal = "theory-goal";
|
|
81 |
val qed = "qed";
|
6733
|
82 |
val qed_block = "qed-block";
|
6722
|
83 |
val prf_goal = "proof-goal";
|
|
84 |
val prf_block = "proof-block";
|
|
85 |
val prf_chain = "proof-chain";
|
|
86 |
val prf_decl = "proof-decl";
|
6868
|
87 |
val prf_asm = "proof-asm";
|
7676
|
88 |
val prf_asm_goal = "proof-asm-goal";
|
6722
|
89 |
val prf_script = "proof-script";
|
|
90 |
|
7104
|
91 |
val kinds = [control, diag, thy_begin, thy_switch, thy_end, thy_heading, thy_decl, thy_goal,
|
7676
|
92 |
qed, qed_block, prf_goal, prf_block, prf_chain, prf_decl, prf_asm, prf_asm_goal, prf_script];
|
6722
|
93 |
end;
|
|
94 |
|
|
95 |
|
5829
|
96 |
(* parsers *)
|
|
97 |
|
|
98 |
type token = OuterLex.token;
|
|
99 |
type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
|
|
100 |
|
|
101 |
datatype parser =
|
6722
|
102 |
Parser of string * (string * string) * bool * parser_fn;
|
5829
|
103 |
|
6722
|
104 |
fun parser int_only name comment kind parse = Parser (name, (comment, kind), int_only, parse);
|
5829
|
105 |
|
|
106 |
|
|
107 |
(* parse command *)
|
|
108 |
|
6860
|
109 |
local
|
6199
|
110 |
|
5829
|
111 |
fun command_body cmd (name, _) =
|
7026
|
112 |
(case cmd name of
|
|
113 |
Some (int_only, parse) => P.!!! (Scan.prompt (name ^ "# ") (parse >> pair int_only))
|
|
114 |
| None => sys_error ("no parser for outer syntax command " ^ quote name));
|
6860
|
115 |
|
|
116 |
fun terminator false = Scan.succeed ()
|
|
117 |
| terminator true = P.group "terminator" (Scan.option P.sync -- P.$$$ ";" >> K ());
|
|
118 |
|
|
119 |
in
|
5829
|
120 |
|
6860
|
121 |
fun command term cmd =
|
|
122 |
P.$$$ ";" >> K None ||
|
|
123 |
P.sync >> K None ||
|
7026
|
124 |
(P.position P.command :-- command_body cmd) --| terminator term
|
6860
|
125 |
>> (fn ((name, pos), (int_only, f)) =>
|
|
126 |
Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
|
|
127 |
Toplevel.interactive int_only |> f));
|
5829
|
128 |
|
6199
|
129 |
end;
|
|
130 |
|
5829
|
131 |
|
|
132 |
|
|
133 |
(** global syntax state **)
|
|
134 |
|
7026
|
135 |
local
|
|
136 |
|
|
137 |
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
|
6722
|
138 |
val global_parsers = ref (Symtab.empty: ((string * string) * (bool * parser_fn)) Symtab.table);
|
5952
|
139 |
|
7026
|
140 |
fun change_lexicons f =
|
|
141 |
let val lexs = f (! global_lexicons) in
|
|
142 |
(case (op inter_string) (pairself Scan.dest_lexicon lexs) of
|
|
143 |
[] => global_lexicons := lexs
|
|
144 |
| bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
|
|
145 |
end;
|
5829
|
146 |
|
7026
|
147 |
fun change_parsers f = global_parsers := f (! global_parsers);
|
6722
|
148 |
|
7026
|
149 |
in
|
|
150 |
|
|
151 |
(* get current lexers / parsers *)
|
|
152 |
|
|
153 |
(*Note: the syntax for files is statically determined at the very
|
|
154 |
beginning; for interactive processing it may change dynamically.*)
|
|
155 |
|
|
156 |
fun get_lexicons () = ! global_lexicons;
|
|
157 |
fun get_parsers () = ! global_parsers;
|
|
158 |
fun get_parser () = apsome snd o curry Symtab.lookup (! global_parsers);
|
5829
|
159 |
|
|
160 |
|
|
161 |
(* augment syntax *)
|
|
162 |
|
7026
|
163 |
fun add_keywords keywords = change_lexicons (apfst (fn lex =>
|
|
164 |
(Scan.extend_lexicon lex (map Symbol.explode keywords))));
|
5829
|
165 |
|
|
166 |
fun add_parser (tab, Parser (name, comment, int_only, parse)) =
|
|
167 |
(if is_none (Symtab.lookup (tab, name)) then ()
|
|
168 |
else warning ("Redefined outer syntax command " ^ quote name);
|
|
169 |
Symtab.update ((name, (comment, (int_only, parse))), tab));
|
|
170 |
|
|
171 |
fun add_parsers parsers =
|
7026
|
172 |
(change_parsers (fn tab => foldl add_parser (tab, parsers));
|
|
173 |
change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex
|
|
174 |
(map (fn Parser (name, _, _, _) => Symbol.explode name) parsers))));
|
|
175 |
|
|
176 |
end;
|
5829
|
177 |
|
|
178 |
|
7026
|
179 |
(* print syntax *)
|
|
180 |
|
|
181 |
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
|
|
182 |
|
|
183 |
fun dest_parsers () =
|
|
184 |
map (fn (name, ((cmt, kind), (int_only, _))) => (name, cmt, kind, int_only))
|
|
185 |
(Symtab.dest (get_parsers ()));
|
5829
|
186 |
|
7026
|
187 |
fun print_outer_syntax () =
|
|
188 |
let
|
|
189 |
fun pretty_cmd (name, comment, _, _) =
|
|
190 |
Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
|
|
191 |
val (int_cmds, cmds) = partition #4 (dest_parsers ());
|
|
192 |
in
|
|
193 |
Pretty.writeln (Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())));
|
|
194 |
Pretty.writeln (Pretty.big_list "proper commands:" (map pretty_cmd cmds));
|
|
195 |
Pretty.writeln (Pretty.big_list "improper commands (interactive-only):"
|
|
196 |
(map pretty_cmd int_cmds))
|
|
197 |
end;
|
5829
|
198 |
|
7367
|
199 |
val print_help =
|
|
200 |
Toplevel.keep (fn state =>
|
7613
|
201 |
let val opt_thy = try Toplevel.theory_of state in
|
|
202 |
print_outer_syntax ();
|
|
203 |
Method.help_methods opt_thy;
|
|
204 |
Attrib.help_attributes opt_thy
|
|
205 |
end);
|
7367
|
206 |
|
5829
|
207 |
|
|
208 |
|
|
209 |
(** read theory **)
|
|
210 |
|
6247
|
211 |
(* theory keyword *)
|
|
212 |
|
|
213 |
val theoryN = "theory";
|
|
214 |
val theory_keyword = OuterParse.$$$ theoryN;
|
|
215 |
|
|
216 |
|
7683
|
217 |
(* sources *)
|
5829
|
218 |
|
6860
|
219 |
local
|
5829
|
220 |
|
6860
|
221 |
val no_terminator =
|
|
222 |
Scan.unless (P.$$$ ";") (Scan.one (OuterLex.not_sync andf OuterLex.not_eof));
|
|
223 |
|
|
224 |
val recover = Scan.prompt "recover# " (Scan.repeat no_terminator);
|
|
225 |
|
|
226 |
in
|
5829
|
227 |
|
6860
|
228 |
fun source term do_recover cmd src =
|
5829
|
229 |
src
|
6860
|
230 |
|> Source.source OuterLex.stopper
|
|
231 |
(Scan.bulk (fn xs => OuterParse.!!! (command term (cmd ())) xs))
|
|
232 |
(if do_recover then Some recover else None)
|
5829
|
233 |
|> Source.mapfilter I;
|
|
234 |
|
6860
|
235 |
end;
|
|
236 |
|
7683
|
237 |
fun token_source (src, pos) =
|
|
238 |
src
|
|
239 |
|> Symbol.source false
|
|
240 |
|> OuterLex.source false (K (get_lexicons ())) pos;
|
|
241 |
|
|
242 |
fun filter_proper src =
|
|
243 |
src
|
|
244 |
|> Source.filter OuterLex.is_proper;
|
|
245 |
|
5829
|
246 |
|
|
247 |
(* detect header *)
|
|
248 |
|
7026
|
249 |
fun scan_header get_lex scan (src, pos) =
|
5829
|
250 |
src
|
|
251 |
|> Symbol.source false
|
7026
|
252 |
|> OuterLex.source false (fn () => (get_lex (), Scan.empty_lexicon)) pos
|
7683
|
253 |
|> filter_proper
|
6199
|
254 |
|> Source.source OuterLex.stopper (Scan.single scan) None
|
5829
|
255 |
|> (fst o the o Source.get_single);
|
|
256 |
|
6247
|
257 |
val check_header_lexicon = Scan.make_lexicon [Symbol.explode theoryN];
|
5829
|
258 |
|
6199
|
259 |
fun is_old_theory src =
|
6247
|
260 |
is_none (scan_header (K check_header_lexicon) (Scan.option theory_keyword) src);
|
6199
|
261 |
|
|
262 |
|
|
263 |
(* deps_thy --- inspect theory header *)
|
|
264 |
|
6247
|
265 |
val header_lexicon =
|
|
266 |
Scan.make_lexicon (map Symbol.explode ["(", ")", "+", ":", "=", "files", theoryN]);
|
6199
|
267 |
|
6860
|
268 |
local
|
6199
|
269 |
|
7026
|
270 |
val file_name =
|
|
271 |
(P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;
|
6247
|
272 |
|
|
273 |
val theory_head =
|
6860
|
274 |
(P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name) --
|
|
275 |
Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [])
|
6247
|
276 |
>> (fn ((A, Bs), files) => (A, Bs, files));
|
|
277 |
|
6860
|
278 |
in
|
|
279 |
|
|
280 |
val theory_header = theory_head --| (Scan.ahead P.eof || P.$$$ ":");
|
|
281 |
val only_header = theory_keyword |-- theory_head --| Scan.ahead P.eof;
|
|
282 |
val new_header = theory_keyword |-- P.!!! theory_header;
|
6199
|
283 |
|
|
284 |
val old_header =
|
6860
|
285 |
P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))
|
6247
|
286 |
>> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
|
6199
|
287 |
|
|
288 |
end;
|
5829
|
289 |
|
7192
|
290 |
fun deps_thy name path =
|
6199
|
291 |
let
|
6641
|
292 |
val src = File.source path;
|
6247
|
293 |
val (name', parents, files) =
|
6199
|
294 |
(*Note: old style headers dynamically depend on the current lexicon :-( *)
|
7212
|
295 |
if is_old_theory src then scan_header ThySyn.get_lexicon (Scan.error old_header) src
|
6247
|
296 |
else scan_header (K header_lexicon) (Scan.error new_header) src;
|
6199
|
297 |
|
|
298 |
val ml_path = ThyLoad.ml_path name;
|
7192
|
299 |
val ml_file = if is_none (ThyLoad.check_file ml_path) then [] else [ml_path];
|
6199
|
300 |
in
|
|
301 |
if name <> name' then
|
|
302 |
error ("Filename " ^ quote (Path.pack path) ^ " does not match theory name " ^ quote name)
|
6247
|
303 |
else (parents, map (Path.unpack o #1) files @ ml_file)
|
6199
|
304 |
end;
|
|
305 |
|
|
306 |
|
|
307 |
(* load_thy --- read text (including header) *)
|
|
308 |
|
6247
|
309 |
fun try_ml_file name ml time =
|
6199
|
310 |
let
|
|
311 |
val path = ThyLoad.ml_path name;
|
7243
|
312 |
val tr = Toplevel.imperative (fn () => ThyInfo.may_load_file ml time path);
|
6247
|
313 |
val tr_name = if time then "time_use" else "use";
|
6199
|
314 |
in
|
7243
|
315 |
if is_none (ThyLoad.check_file path) then ()
|
7062
|
316 |
else Toplevel.excursion_error [Toplevel.empty |> Toplevel.name tr_name |> tr]
|
6199
|
317 |
end;
|
|
318 |
|
7683
|
319 |
fun parse_thy src_pos =
|
6247
|
320 |
let
|
7683
|
321 |
val lex_src = filter_proper (token_source src_pos);
|
6247
|
322 |
val only_head =
|
|
323 |
lex_src
|
|
324 |
|> Source.source OuterLex.stopper (Scan.single (Scan.option only_header)) None
|
|
325 |
|> (fst o the o Source.get_single);
|
|
326 |
in
|
|
327 |
(case only_head of
|
|
328 |
None =>
|
|
329 |
lex_src
|
6860
|
330 |
|> source false false (K (get_parser ()))
|
6247
|
331 |
|> Source.exhaust
|
|
332 |
| Some spec =>
|
|
333 |
[Toplevel.empty |> Toplevel.name theoryN |> IsarThy.theory spec,
|
|
334 |
Toplevel.empty |> Toplevel.name "end" |> Toplevel.exit])
|
|
335 |
end;
|
5829
|
336 |
|
6247
|
337 |
fun run_thy name path =
|
7683
|
338 |
let
|
|
339 |
val (src, pos) = File.source path;
|
|
340 |
val is_old = is_old_theory (src, pos);
|
|
341 |
in
|
|
342 |
Present.theory_source is_old name (src, pos);
|
|
343 |
if is_old then ThySyn.load_thy name (Source.exhaust src)
|
7062
|
344 |
else Toplevel.excursion_error (parse_thy (src, pos))
|
6247
|
345 |
end;
|
6199
|
346 |
|
|
347 |
fun load_thy name ml time path =
|
6247
|
348 |
(if time then
|
|
349 |
timeit (fn () =>
|
|
350 |
(writeln ("\n**** Starting theory " ^ quote name ^ " ****");
|
|
351 |
setmp Goals.proof_timing true (run_thy name) path;
|
|
352 |
writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
|
|
353 |
else run_thy name path;
|
|
354 |
Context.context (ThyInfo.get_theory name);
|
|
355 |
try_ml_file name ml time);
|
5829
|
356 |
|
|
357 |
|
|
358 |
(* interactive source of state transformers *)
|
|
359 |
|
7333
|
360 |
fun isar term no_pos =
|
5829
|
361 |
Source.tty
|
|
362 |
|> Symbol.source true
|
7604
|
363 |
|> OuterLex.source true get_lexicons
|
|
364 |
(if no_pos then Position.none else Position.line_name 1 "stdin")
|
7683
|
365 |
|> filter_proper
|
6860
|
366 |
|> source term true get_parser;
|
5829
|
367 |
|
|
368 |
|
|
369 |
|
|
370 |
(** the read-eval-print loop **)
|
|
371 |
|
5923
|
372 |
(* main loop *)
|
|
373 |
|
7333
|
374 |
fun gen_loop term no_pos =
|
|
375 |
(Context.reset_context ();
|
|
376 |
Toplevel.loop (isar term no_pos));
|
5829
|
377 |
|
7333
|
378 |
fun gen_main term no_pos =
|
5829
|
379 |
(Toplevel.set_state Toplevel.toplevel;
|
|
380 |
ml_prompts "ML> " "ML# ";
|
6199
|
381 |
writeln (Session.welcome ());
|
7333
|
382 |
gen_loop term no_pos);
|
6860
|
383 |
|
7333
|
384 |
fun main () = gen_main false false;
|
|
385 |
fun loop () = gen_loop false false;
|
|
386 |
fun sync_main () = gen_main true true;
|
|
387 |
fun sync_loop () = gen_loop true true;
|
5829
|
388 |
|
|
389 |
|
|
390 |
(* help *)
|
|
391 |
|
|
392 |
fun help () =
|
|
393 |
writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
|
5883
|
394 |
\invoke 'loop();' to enter the Isar loop.");
|
5829
|
395 |
|
|
396 |
|
6373
|
397 |
(*final declarations of this structure!*)
|
6685
|
398 |
val command = parser false;
|
|
399 |
val improper_command = parser true;
|
|
400 |
|
6373
|
401 |
|
5829
|
402 |
end;
|
|
403 |
|
6199
|
404 |
(*setup theory syntax dependent operations*)
|
|
405 |
ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
|
|
406 |
ThyLoad.load_thy_fn := OuterSyntax.load_thy;
|
|
407 |
structure ThyLoad: THY_LOAD = ThyLoad;
|
|
408 |
|
5829
|
409 |
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
|
|
410 |
open BasicOuterSyntax;
|