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