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.
|
6247
|
6 |
|
|
7 |
TODO:
|
|
8 |
- cleanup;
|
|
9 |
- avoid string constants;
|
5829
|
10 |
*)
|
|
11 |
|
|
12 |
signature BASIC_OUTER_SYNTAX =
|
|
13 |
sig
|
|
14 |
val main: unit -> unit
|
5883
|
15 |
val loop: unit -> unit
|
6860
|
16 |
val sync_main: unit -> unit
|
|
17 |
val sync_loop: unit -> unit
|
5829
|
18 |
val help: unit -> unit
|
|
19 |
end;
|
|
20 |
|
|
21 |
signature OUTER_SYNTAX =
|
|
22 |
sig
|
|
23 |
include BASIC_OUTER_SYNTAX
|
6722
|
24 |
structure Keyword:
|
|
25 |
sig
|
|
26 |
val control: string
|
|
27 |
val diag: string
|
|
28 |
val thy_begin: string
|
|
29 |
val thy_end: string
|
|
30 |
val thy_heading: string
|
|
31 |
val thy_decl: string
|
|
32 |
val thy_goal: string
|
|
33 |
val qed: string
|
6733
|
34 |
val qed_block: string
|
6722
|
35 |
val prf_goal: string
|
|
36 |
val prf_block: string
|
|
37 |
val prf_chain: string
|
|
38 |
val prf_decl: string
|
6868
|
39 |
val prf_asm: string
|
6722
|
40 |
val prf_script: string
|
|
41 |
val kinds: string list
|
|
42 |
end
|
5829
|
43 |
type token
|
|
44 |
type parser
|
6722
|
45 |
val command: string -> string -> string ->
|
6373
|
46 |
(token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
|
6722
|
47 |
val improper_command: string -> string -> string ->
|
6373
|
48 |
(token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
|
6722
|
49 |
val dest_syntax: unit -> string list * (string * string) list
|
5883
|
50 |
val print_outer_syntax: unit -> unit
|
5829
|
51 |
val add_keywords: string list -> unit
|
|
52 |
val add_parsers: parser list -> unit
|
6247
|
53 |
val theory_header: token list -> (string * string list * (string * bool) list) * token list
|
6199
|
54 |
val deps_thy: string -> bool -> Path.T -> string list * Path.T list
|
|
55 |
val load_thy: string -> bool -> bool -> Path.T -> unit
|
6860
|
56 |
val isar: bool -> Toplevel.isar
|
5829
|
57 |
end;
|
|
58 |
|
|
59 |
structure OuterSyntax: OUTER_SYNTAX =
|
|
60 |
struct
|
|
61 |
|
6860
|
62 |
structure P = OuterParse;
|
|
63 |
|
5829
|
64 |
|
|
65 |
(** outer syntax **)
|
|
66 |
|
6722
|
67 |
(* command keyword classification *)
|
|
68 |
|
|
69 |
structure Keyword =
|
|
70 |
struct
|
|
71 |
val control = "control";
|
|
72 |
val diag = "diag";
|
|
73 |
val thy_begin = "theory-begin";
|
|
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 |
|
|
87 |
val kinds = [control, diag, thy_begin, thy_end, thy_heading, thy_decl, thy_goal, qed,
|
6868
|
88 |
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_name cmd =
|
6860
|
108 |
P.group "command"
|
|
109 |
(P.position (Scan.one (OuterLex.keyword_pred (is_some o cmd)) >> OuterLex.val_of));
|
5829
|
110 |
|
|
111 |
fun command_body cmd (name, _) =
|
|
112 |
let val (int_only, parse) = the (cmd name)
|
6860
|
113 |
in P.!!! (Scan.prompt (name ^ "# ") (parse >> pair int_only)) end;
|
|
114 |
|
|
115 |
fun terminator false = Scan.succeed ()
|
|
116 |
| terminator true = P.group "terminator" (Scan.option P.sync -- P.$$$ ";" >> K ());
|
|
117 |
|
|
118 |
in
|
5829
|
119 |
|
6860
|
120 |
fun command term cmd =
|
|
121 |
P.$$$ ";" >> K None ||
|
|
122 |
P.sync >> K None ||
|
|
123 |
(command_name cmd :-- command_body cmd) --| terminator term
|
|
124 |
>> (fn ((name, pos), (int_only, f)) =>
|
|
125 |
Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
|
|
126 |
Toplevel.interactive int_only |> f));
|
5829
|
127 |
|
6199
|
128 |
end;
|
|
129 |
|
5829
|
130 |
|
|
131 |
|
|
132 |
(** global syntax state **)
|
|
133 |
|
|
134 |
val global_lexicon = ref Scan.empty_lexicon;
|
6722
|
135 |
val global_parsers = ref (Symtab.empty: ((string * string) * (bool * parser_fn)) Symtab.table);
|
5952
|
136 |
|
5829
|
137 |
|
|
138 |
(* print syntax *)
|
|
139 |
|
6722
|
140 |
fun dest_syntax () =
|
|
141 |
(map implode (Scan.dest_lexicon (! global_lexicon)),
|
|
142 |
map (fn (name, ((_, kind), _)) => (name, kind)) (Symtab.dest (! global_parsers)));
|
|
143 |
|
5829
|
144 |
fun print_outer_syntax () =
|
|
145 |
let
|
|
146 |
val keywords = map implode (Scan.dest_lexicon (! global_lexicon));
|
6722
|
147 |
fun pretty_cmd (name, ((comment, _), _)) =
|
5829
|
148 |
Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
|
|
149 |
val (int_cmds, cmds) = partition (#1 o #2 o #2) (Symtab.dest (! global_parsers));
|
|
150 |
in
|
6107
|
151 |
Pretty.writeln (Pretty.strs ("syntax keywords:" :: map quote keywords));
|
6095
|
152 |
Pretty.writeln (Pretty.big_list "proper commands:" (map pretty_cmd cmds));
|
|
153 |
Pretty.writeln (Pretty.big_list "improper commands (interactive-only):"
|
|
154 |
(map pretty_cmd int_cmds))
|
5829
|
155 |
end;
|
|
156 |
|
|
157 |
|
|
158 |
(* augment syntax *)
|
|
159 |
|
|
160 |
fun add_keywords keywords =
|
|
161 |
global_lexicon := Scan.extend_lexicon (! global_lexicon) (map Symbol.explode keywords);
|
|
162 |
|
|
163 |
fun add_parser (tab, Parser (name, comment, int_only, parse)) =
|
|
164 |
(if is_none (Symtab.lookup (tab, name)) then ()
|
|
165 |
else warning ("Redefined outer syntax command " ^ quote name);
|
|
166 |
Symtab.update ((name, (comment, (int_only, parse))), tab));
|
|
167 |
|
|
168 |
fun add_parsers parsers =
|
|
169 |
(global_parsers := foldl add_parser (! global_parsers, parsers);
|
|
170 |
add_keywords (map (fn Parser (name, _, _, _) => name) parsers));
|
|
171 |
|
|
172 |
|
|
173 |
(* get current lexer / parser *)
|
|
174 |
|
|
175 |
(*Note: the syntax for files is statically determined at the very
|
|
176 |
beginning; for interactive processing it may change dynamically.*)
|
|
177 |
|
|
178 |
fun get_lexicon () = ! global_lexicon;
|
|
179 |
fun get_parser () = apsome snd o curry Symtab.lookup (! global_parsers);
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
(** read theory **)
|
|
184 |
|
6247
|
185 |
(* theory keyword *)
|
|
186 |
|
|
187 |
val theoryN = "theory";
|
|
188 |
val theory_keyword = OuterParse.$$$ theoryN;
|
|
189 |
|
|
190 |
|
5829
|
191 |
(* source *)
|
|
192 |
|
6860
|
193 |
local
|
5829
|
194 |
|
6860
|
195 |
val no_terminator =
|
|
196 |
Scan.unless (P.$$$ ";") (Scan.one (OuterLex.not_sync andf OuterLex.not_eof));
|
|
197 |
|
|
198 |
val recover = Scan.prompt "recover# " (Scan.repeat no_terminator);
|
|
199 |
|
|
200 |
in
|
5829
|
201 |
|
6860
|
202 |
fun source term do_recover cmd src =
|
5829
|
203 |
src
|
6860
|
204 |
|> Source.source OuterLex.stopper
|
|
205 |
(Scan.bulk (fn xs => OuterParse.!!! (command term (cmd ())) xs))
|
|
206 |
(if do_recover then Some recover else None)
|
5829
|
207 |
|> Source.mapfilter I;
|
|
208 |
|
6860
|
209 |
end;
|
|
210 |
|
5829
|
211 |
|
|
212 |
(* detect header *)
|
|
213 |
|
6199
|
214 |
fun scan_header get_lexicon scan (src, pos) =
|
5829
|
215 |
src
|
|
216 |
|> Symbol.source false
|
6199
|
217 |
|> OuterLex.source false get_lexicon pos
|
|
218 |
|> Source.source OuterLex.stopper (Scan.single scan) None
|
5829
|
219 |
|> (fst o the o Source.get_single);
|
|
220 |
|
6247
|
221 |
val check_header_lexicon = Scan.make_lexicon [Symbol.explode theoryN];
|
5829
|
222 |
|
6199
|
223 |
fun is_old_theory src =
|
6247
|
224 |
is_none (scan_header (K check_header_lexicon) (Scan.option theory_keyword) src);
|
6199
|
225 |
|
|
226 |
fun warn_theory_style path is_old =
|
|
227 |
let
|
|
228 |
val style = if is_old then "old" else "new";
|
|
229 |
val _ = warning ("Assuming " ^ style ^ "-style theory format for " ^ quote (Path.pack path));
|
|
230 |
in is_old end;
|
|
231 |
|
|
232 |
|
|
233 |
(* deps_thy --- inspect theory header *)
|
|
234 |
|
6247
|
235 |
val header_lexicon =
|
|
236 |
Scan.make_lexicon (map Symbol.explode ["(", ")", "+", ":", "=", "files", theoryN]);
|
6199
|
237 |
|
6860
|
238 |
local
|
6199
|
239 |
|
6860
|
240 |
val file_name = (P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;
|
6247
|
241 |
|
|
242 |
val theory_head =
|
6860
|
243 |
(P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name) --
|
|
244 |
Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [])
|
6247
|
245 |
>> (fn ((A, Bs), files) => (A, Bs, files));
|
|
246 |
|
6860
|
247 |
in
|
|
248 |
|
|
249 |
val theory_header = theory_head --| (Scan.ahead P.eof || P.$$$ ":");
|
|
250 |
val only_header = theory_keyword |-- theory_head --| Scan.ahead P.eof;
|
|
251 |
val new_header = theory_keyword |-- P.!!! theory_header;
|
6199
|
252 |
|
|
253 |
val old_header =
|
6860
|
254 |
P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))
|
6247
|
255 |
>> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
|
6199
|
256 |
|
|
257 |
end;
|
5829
|
258 |
|
6199
|
259 |
fun deps_thy name ml path =
|
|
260 |
let
|
6641
|
261 |
val src = File.source path;
|
6199
|
262 |
val is_old = warn_theory_style path (is_old_theory src);
|
6247
|
263 |
val (name', parents, files) =
|
6199
|
264 |
(*Note: old style headers dynamically depend on the current lexicon :-( *)
|
|
265 |
if is_old then scan_header ThySyn.get_lexicon (Scan.error old_header) src
|
6247
|
266 |
else scan_header (K header_lexicon) (Scan.error new_header) src;
|
6199
|
267 |
|
|
268 |
val ml_path = ThyLoad.ml_path name;
|
|
269 |
val ml_file = if not ml orelse is_none (ThyLoad.check_file ml_path) then [] else [ml_path];
|
|
270 |
in
|
|
271 |
if name <> name' then
|
|
272 |
error ("Filename " ^ quote (Path.pack path) ^ " does not match theory name " ^ quote name)
|
6247
|
273 |
else (parents, map (Path.unpack o #1) files @ ml_file)
|
6199
|
274 |
end;
|
|
275 |
|
|
276 |
|
|
277 |
(* load_thy --- read text (including header) *)
|
|
278 |
|
6247
|
279 |
fun try_ml_file name ml time =
|
6199
|
280 |
let
|
|
281 |
val path = ThyLoad.ml_path name;
|
6247
|
282 |
val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
|
|
283 |
val tr_name = if time then "time_use" else "use";
|
6199
|
284 |
in
|
|
285 |
if not ml orelse is_none (ThyLoad.check_file path) then ()
|
6247
|
286 |
else Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr]
|
6199
|
287 |
end;
|
|
288 |
|
|
289 |
fun parse_thy (src, pos) =
|
6247
|
290 |
let
|
|
291 |
val lex_src =
|
|
292 |
src
|
|
293 |
|> Symbol.source false
|
|
294 |
|> OuterLex.source false (K (get_lexicon ())) pos;
|
|
295 |
val only_head =
|
|
296 |
lex_src
|
|
297 |
|> Source.source OuterLex.stopper (Scan.single (Scan.option only_header)) None
|
|
298 |
|> (fst o the o Source.get_single);
|
|
299 |
in
|
|
300 |
(case only_head of
|
|
301 |
None =>
|
|
302 |
lex_src
|
6860
|
303 |
|> source false false (K (get_parser ()))
|
6247
|
304 |
|> Source.exhaust
|
|
305 |
| Some spec =>
|
|
306 |
[Toplevel.empty |> Toplevel.name theoryN |> IsarThy.theory spec,
|
|
307 |
Toplevel.empty |> Toplevel.name "end" |> Toplevel.exit])
|
|
308 |
end;
|
5829
|
309 |
|
6247
|
310 |
fun run_thy name path =
|
6641
|
311 |
let val (src, pos) = File.source path in
|
6332
|
312 |
Present.theory_source name src;
|
6247
|
313 |
if is_old_theory (src, pos) then ThySyn.load_thy name (Source.exhaust src)
|
|
314 |
else (Toplevel.excursion (parse_thy (src, pos))
|
|
315 |
handle exn => error (Toplevel.exn_message exn))
|
|
316 |
end;
|
6199
|
317 |
|
|
318 |
fun load_thy name ml time path =
|
6247
|
319 |
(if time then
|
|
320 |
timeit (fn () =>
|
|
321 |
(writeln ("\n**** Starting theory " ^ quote name ^ " ****");
|
|
322 |
setmp Goals.proof_timing true (run_thy name) path;
|
|
323 |
writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
|
|
324 |
else run_thy name path;
|
|
325 |
Context.context (ThyInfo.get_theory name);
|
|
326 |
try_ml_file name ml time);
|
5829
|
327 |
|
|
328 |
|
|
329 |
(* interactive source of state transformers *)
|
|
330 |
|
6860
|
331 |
fun isar term =
|
5829
|
332 |
Source.tty
|
|
333 |
|> Symbol.source true
|
|
334 |
|> OuterLex.source true get_lexicon (Position.line_name 1 "stdin")
|
6860
|
335 |
|> source term true get_parser;
|
5829
|
336 |
|
|
337 |
|
|
338 |
|
|
339 |
(** the read-eval-print loop **)
|
|
340 |
|
5923
|
341 |
(* main loop *)
|
|
342 |
|
6860
|
343 |
fun gen_loop term = (Context.reset_context (); Toplevel.loop (isar term));
|
5829
|
344 |
|
6860
|
345 |
fun gen_main term =
|
5829
|
346 |
(Toplevel.set_state Toplevel.toplevel;
|
|
347 |
ml_prompts "ML> " "ML# ";
|
6199
|
348 |
writeln (Session.welcome ());
|
6860
|
349 |
gen_loop term);
|
|
350 |
|
|
351 |
fun main () = gen_main false;
|
|
352 |
fun loop () = gen_loop false;
|
|
353 |
fun sync_main () = gen_main true;
|
|
354 |
fun sync_loop () = gen_loop true;
|
5829
|
355 |
|
|
356 |
|
|
357 |
(* help *)
|
|
358 |
|
|
359 |
fun help () =
|
|
360 |
writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
|
5883
|
361 |
\invoke 'loop();' to enter the Isar loop.");
|
5829
|
362 |
|
|
363 |
|
6373
|
364 |
(*final declarations of this structure!*)
|
6685
|
365 |
val command = parser false;
|
|
366 |
val improper_command = parser true;
|
|
367 |
|
6373
|
368 |
|
5829
|
369 |
end;
|
|
370 |
|
6199
|
371 |
(*setup theory syntax dependent operations*)
|
|
372 |
ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
|
|
373 |
ThyLoad.load_thy_fn := OuterSyntax.load_thy;
|
|
374 |
structure ThyLoad: THY_LOAD = ThyLoad;
|
|
375 |
|
5829
|
376 |
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
|
|
377 |
open BasicOuterSyntax;
|