1 (* Title: Pure/Thy/thy_output.ML
2 Author: Markus Wenzel, TU Muenchen
4 Theory document output with antiquotations.
9 val display: bool Unsynchronized.ref
10 val quotes: bool Unsynchronized.ref
11 val indent: int Unsynchronized.ref
12 val source: bool Unsynchronized.ref
13 val break: bool Unsynchronized.ref
14 val add_commands: (string * (Args.src -> Toplevel.state -> string)) list -> unit
15 val add_options: (string * (string -> (unit -> string) -> unit -> string)) list -> unit
16 val defined_command: string -> bool
17 val defined_option: string -> bool
18 val print_antiquotations: unit -> unit
19 val boolean: string -> bool
20 val integer: string -> int
21 val antiquotation: string -> 'a context_parser ->
22 ({state: Toplevel.state, source: Args.src, context: Proof.context} -> 'a -> string) -> unit
23 datatype markup = Markup | MarkupEnv | Verbatim
24 val modes: string list Unsynchronized.ref
25 val eval_antiquote: Scan.lexicon -> Toplevel.state -> Symbol_Pos.text * Position.T -> string
26 val present_thy: Scan.lexicon -> (string -> string list) -> (markup -> string -> bool) ->
27 (Toplevel.transition * Toplevel.state) list -> (OuterLex.token, 'a) Source.source -> Buffer.T
28 val pretty_text: string -> Pretty.T
29 val pretty_term: Proof.context -> term -> Pretty.T
30 val pretty_thm: Proof.context -> thm -> Pretty.T
31 val str_of_source: Args.src -> string
32 val maybe_pretty_source: ('a -> Pretty.T) -> Args.src -> 'a list -> Pretty.T list
33 val output: Pretty.T list -> string
36 structure ThyOutput: THY_OUTPUT =
39 structure T = OuterLex;
40 structure P = OuterParse;
43 (** global options **)
45 val display = Unsynchronized.ref false;
46 val quotes = Unsynchronized.ref false;
47 val indent = Unsynchronized.ref 0;
48 val source = Unsynchronized.ref false;
49 val break = Unsynchronized.ref false;
53 (** maintain global antiquotations **)
58 Unsynchronized.ref (Symtab.empty: (Args.src -> Toplevel.state -> string) Symtab.table);
61 Unsynchronized.ref (Symtab.empty: (string -> (unit -> string) -> unit -> string) Symtab.table);
63 fun add_item kind (name, x) tab =
64 (if not (Symtab.defined tab name) then ()
65 else warning ("Redefined document antiquotation " ^ kind ^ ": " ^ quote name);
66 Symtab.update (name, x) tab);
71 CRITICAL (fn () => Unsynchronized.change global_commands (fold (add_item "command") xs));
73 CRITICAL (fn () => Unsynchronized.change global_options (fold (add_item "option") xs));
75 fun defined_command name = Symtab.defined (! global_commands) name;
76 fun defined_option name = Symtab.defined (! global_options) name;
79 let val ((name, _), pos) = Args.dest_src src in
80 (case Symtab.lookup (! global_commands) name of
81 NONE => error ("Unknown document antiquotation: " ^ quote name ^ Position.str_of pos)
83 (Position.report (Markup.doc_antiq name) pos;
84 (fn state => f src state handle ERROR msg =>
85 cat_error msg ("The error(s) above occurred in document antiquotation: " ^
86 quote name ^ Position.str_of pos))))
89 fun option (name, s) f () =
90 (case Symtab.lookup (! global_options) name of
91 NONE => error ("Unknown document antiquotation option: " ^ quote name)
92 | SOME opt => opt s f ());
95 | options (opt :: opts) f = option opt (options opts f);
98 fun print_antiquotations () =
99 [Pretty.big_list "document antiquotation commands:"
100 (map Pretty.str (sort_strings (Symtab.keys (! global_commands)))),
101 Pretty.big_list "document antiquotation options:"
102 (map Pretty.str (sort_strings (Symtab.keys (! global_options))))]
103 |> Pretty.chunks |> Pretty.writeln;
107 fun antiquotation name scan out = add_commands [(name, fn src => fn state =>
108 let val (x, ctxt) = Args.context_syntax "document antiquotation"
109 scan src (Toplevel.presentation_context_of state)
110 in out {source = src, state = state, context = ctxt} x end)];
114 (** syntax of antiquotations **)
118 fun boolean "" = true
119 | boolean "true" = true
120 | boolean "false" = false
121 | boolean s = error ("Bad boolean value: " ^ quote s);
126 (case Library.read_int ss of (i, []) => i
127 | _ => error ("Bad integer value: " ^ quote s));
128 in (case Symbol.explode s of "-" :: ss => ~ (int ss) | ss => int ss) end;
135 val property = P.xname -- Scan.optional (P.$$$ "=" |-- P.!!! P.xname) "";
136 val properties = Scan.optional (P.$$$ "[" |-- P.!!! (P.enum "," property --| P.$$$ "]")) [];
140 val antiq = P.!!! (P.position P.xname -- properties -- Args.parse --| Scan.ahead P.eof)
141 >> (fn (((x, pos), y), z) => (y, Args.src ((x, z), pos)));
148 val modes = Unsynchronized.ref ([]: string list);
150 fun eval_antiquote lex state (txt, pos) =
152 fun expand (Antiquote.Text ss) = Symbol_Pos.content ss
153 | expand (Antiquote.Antiq (ss, (pos, _))) =
154 let val (opts, src) = T.read_antiq lex antiq (ss, pos) in
155 options opts (fn () => command src state) (); (*preview errors!*)
156 PrintMode.with_modes (! modes @ Latex.modes)
157 (Output.no_warnings_CRITICAL (options opts (fn () => command src state))) ()
159 | expand (Antiquote.Open _) = ""
160 | expand (Antiquote.Close _) = "";
161 val ants = Antiquote.read (Symbol_Pos.explode (txt, pos), pos);
163 if Toplevel.is_toplevel state andalso not (forall Antiquote.is_text ants) then
164 error ("Unknown context -- cannot expand document antiquotations" ^ Position.str_of pos)
165 else implode (map expand ants)
170 (** present theory source **)
172 (*NB: arranging white space around command spans is a black art.*)
174 (* presentation tokens *)
178 | BasicToken of T.token
179 | MarkupToken of string * (string * Position.T)
180 | MarkupEnvToken of string * (string * Position.T)
181 | VerbatimToken of string * Position.T;
183 fun output_token lex state =
184 let val eval = eval_antiquote lex state in
186 | BasicToken tok => Latex.output_basic tok
187 | MarkupToken (cmd, txt) => Latex.output_markup cmd (eval txt)
188 | MarkupEnvToken (cmd, txt) => Latex.output_markup_env cmd (eval txt)
189 | VerbatimToken txt => Latex.output_verbatim (eval txt)
192 fun basic_token pred (BasicToken tok) = pred tok
193 | basic_token _ _ = false;
195 val improper_token = basic_token (not o T.is_proper);
196 val comment_token = basic_token T.is_comment;
197 val blank_token = basic_token T.is_blank;
198 val newline_token = basic_token T.is_newline;
203 type command = string * Position.T * string list; (*name, position, tags*)
204 type source = (token * (string * int)) list; (*token, markup flag, meta-comment depth*)
206 datatype span = Span of command * (source * source * source * source) * bool;
208 fun make_span cmd src =
210 fun take_newline (tok :: toks) =
211 if newline_token (fst tok) then ([tok], toks, true)
212 else ([], tok :: toks, false)
213 | take_newline [] = ([], [], false);
214 val (((src_prefix, src_main), src_suffix1), (src_suffix2, src_appendix, newline)) =
216 |> take_prefix (improper_token o fst)
217 ||>> take_suffix (improper_token o fst)
218 ||>> take_prefix (comment_token o fst)
220 in Span (cmd, (src_prefix, src_main, src_suffix1 @ src_suffix2, src_appendix), newline) end;
227 fun err_bad_nesting pos =
228 error ("Bad nesting of commands in presentation" ^ pos);
230 fun edge which f (x: string option, y) =
232 else (case which (x, y) of NONE => I | SOME txt => Buffer.add (f txt));
234 val begin_tag = edge #2 Latex.begin_tag;
235 val end_tag = edge #1 Latex.end_tag;
236 fun open_delim delim e = edge #2 Latex.begin_delim e #> delim #> edge #2 Latex.end_delim e;
237 fun close_delim delim e = edge #1 Latex.begin_delim e #> delim #> edge #1 Latex.end_delim e;
241 fun present_span lex default_tags span state state'
242 (tag_stack, active_tag, newline, buffer, present_cont) =
244 val present = fold (fn (tok, (flag, 0)) =>
245 Buffer.add (output_token lex state' tok)
249 val Span ((cmd_name, cmd_pos, cmd_tags), srcs, span_newline) = span;
251 val (tag, tags) = tag_stack;
252 val tag' = try hd (fold OuterKeyword.update_tags cmd_tags (the_list tag));
255 if is_some tag' then tag'
256 else if cmd_name = "end" andalso not (Toplevel.is_toplevel state') then NONE
257 else try hd (default_tags cmd_name);
258 val edge = (active_tag, active_tag');
261 if is_none active_tag' then span_newline else newline;
263 val nesting = Toplevel.level state' - Toplevel.level state;
265 if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack
266 else if nesting >= 0 then (tag', replicate nesting tag @ tags)
268 (case drop (~ nesting - 1) tags of
269 tgs :: tgss => (tgs, tgss)
270 | [] => err_bad_nesting (Position.str_of cmd_pos));
275 |> close_delim (fst present_cont) edge
277 |> open_delim (present (#1 srcs)) edge
279 |> present (#2 srcs);
281 if newline then (present (#3 srcs), present (#4 srcs))
282 else (I, present (#3 srcs) #> present (#4 srcs));
283 in (tag_stack', active_tag', newline', buffer', present_cont') end;
285 fun present_trailer ((_, tags), active_tag, _, buffer, present_cont) =
286 if not (null tags) then err_bad_nesting " at end of theory"
289 |> end_tag (active_tag, NONE)
290 |> close_delim (fst present_cont) (active_tag, NONE)
298 datatype markup = Markup | MarkupEnv | Verbatim;
303 Scan.one T.is_blank -- Scan.many T.is_comment -- Scan.one T.is_proper;
305 val is_improper = not o (T.is_proper orf T.is_begin_ignore orf T.is_end_ignore);
306 val improper = Scan.many is_improper;
307 val improper_end = Scan.repeat (Scan.unless space_proper (Scan.one is_improper));
308 val blank_end = Scan.repeat (Scan.unless space_proper (Scan.one T.is_blank));
310 val opt_newline = Scan.option (Scan.one T.is_newline);
313 Scan.depend (fn d => opt_newline |-- Scan.one T.is_begin_ignore
315 Scan.depend (fn d => Scan.one T.is_end_ignore --|
316 (if d = 0 then Scan.fail_with (K "Bad nesting of meta-comments") else opt_newline)
319 val tag = (improper -- P.$$$ "%" -- improper) |-- P.!!! (P.tag_name --| blank_end);
322 Scan.option ((P.$$$ "(" -- improper -- P.$$$ "in") |--
323 P.!!! (improper |-- P.xname --| (improper -- P.$$$ ")")));
327 fun present_thy lex default_tags is_markup command_results src =
331 val ignored = Scan.state --| ignore
332 >> (fn d => (NONE, (NoToken, ("", d))));
334 fun markup mark mk flag = Scan.peek (fn d =>
335 improper |-- P.position (Scan.one (T.is_kind T.Command andf is_markup mark o T.content_of)) --
337 P.!!!! ((improper -- locale -- improper) |-- P.doc_source --| improper_end)
338 >> (fn (((tok, pos), tags), txt) =>
339 let val name = T.content_of tok
340 in (SOME (name, pos, tags), (mk (name, txt), (flag, d))) end));
342 val command = Scan.peek (fn d =>
343 P.position (Scan.one (T.is_kind T.Command)) --
345 >> (fn ((tok, pos), tags) =>
346 let val name = T.content_of tok
347 in (SOME (name, pos, tags), (BasicToken tok, (Latex.markup_false, d))) end));
349 val cmt = Scan.peek (fn d =>
350 P.$$$ "--" |-- P.!!!! (improper |-- P.doc_source)
351 >> (fn txt => (NONE, (MarkupToken ("cmt", txt), ("", d)))));
353 val other = Scan.peek (fn d =>
354 P.not_eof >> (fn tok => (NONE, (BasicToken tok, ("", d)))));
358 markup Markup MarkupToken Latex.markup_true ||
359 markup MarkupEnv MarkupEnvToken Latex.markup_true ||
360 markup Verbatim (VerbatimToken o #2) "" ||
361 command || cmt || other;
366 val is_eof = fn (_, (BasicToken x, _)) => T.is_eof x | _ => false;
367 val stopper = Scan.stopper (K (NONE, (BasicToken T.eof, ("", 0)))) is_eof;
369 val cmd = Scan.one (is_some o fst);
370 val non_cmd = Scan.one (is_none o fst andf not o is_eof) >> #2;
372 val comments = Scan.many (comment_token o fst o snd);
373 val blank = Scan.one (blank_token o fst o snd);
374 val newline = Scan.one (newline_token o fst o snd);
376 Scan.option (newline -- comments) --
377 Scan.option (newline -- comments) --
378 Scan.option (blank -- comments) -- cmd;
381 Scan.repeat non_cmd -- cmd --
382 Scan.repeat (Scan.unless before_cmd non_cmd) --
383 Scan.option (newline >> (single o snd))
384 >> (fn (((toks1, (cmd, tok2)), toks3), tok4) =>
385 make_span (the cmd) (toks1 @ (tok2 :: (toks3 @ the_default [] tok4))));
389 |> Source.filter (not o T.is_semicolon)
390 |> Source.source' 0 T.stopper (Scan.error (Scan.bulk token)) NONE
391 |> Source.source stopper (Scan.error (Scan.bulk span)) NONE
395 (* present commands *)
397 fun present_command tr span st st' =
398 Toplevel.setmp_thread_position tr (present_span lex default_tags span st st');
401 | present st (((tr, st'), span) :: rest) = present_command tr span st st' #> present st' rest;
403 if length command_results = length spans then
404 ((NONE, []), NONE, true, Buffer.empty, (I, I))
405 |> present Toplevel.toplevel (command_results ~~ spans)
407 else error "Messed-up outer syntax for presentation"
414 (** setup default output **)
419 [("show_types", setmp_CRITICAL Syntax.show_types o boolean),
420 ("show_sorts", setmp_CRITICAL Syntax.show_sorts o boolean),
421 ("show_structs", setmp_CRITICAL show_structs o boolean),
422 ("show_question_marks", setmp_CRITICAL show_question_marks o boolean),
423 ("long_names", setmp_CRITICAL Name_Space.long_names o boolean),
424 ("short_names", setmp_CRITICAL Name_Space.short_names o boolean),
425 ("unique_names", setmp_CRITICAL Name_Space.unique_names o boolean),
426 ("eta_contract", setmp_CRITICAL Syntax.eta_contract o boolean),
427 ("display", setmp_CRITICAL display o boolean),
428 ("break", setmp_CRITICAL break o boolean),
429 ("quotes", setmp_CRITICAL quotes o boolean),
430 ("mode", fn s => fn f => PrintMode.with_modes [s] f),
431 ("margin", setmp_CRITICAL Pretty.margin_default o integer),
432 ("indent", setmp_CRITICAL indent o integer),
433 ("source", setmp_CRITICAL source o boolean),
434 ("goals_limit", setmp_CRITICAL goals_limit o integer)];
437 (* basic pretty printing *)
440 if ! display then s else Symbol.strip_blanks s;
442 val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
444 fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
446 fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
448 fun pretty_term_style ctxt (style, t) =
449 pretty_term ctxt (style t);
451 fun pretty_thm_style ctxt (style, th) =
452 pretty_term ctxt (style (Thm.full_prop_of th));
454 fun pretty_term_typ ctxt (style, t) =
456 in pretty_term ctxt (TypeInfer.constrain (Term.fastype_of t') t') end;
458 fun pretty_term_typeof ctxt (style, t) =
459 Syntax.pretty_typ ctxt (Term.fastype_of (style t));
461 fun pretty_const ctxt c =
463 val t = Const (c, Consts.type_scheme (ProofContext.consts_of ctxt) c)
464 handle TYPE (msg, _, _) => error msg;
465 val ([t'], _) = Variable.import_terms true [t] ctxt;
466 in pretty_term ctxt t' end;
468 fun pretty_abbrev ctxt s =
470 val t = Syntax.parse_term ctxt s |> singleton (ProofContext.standard_infer_types ctxt);
471 fun err () = error ("Abbreviated constant expected: " ^ Syntax.string_of_term ctxt t);
472 val (head, args) = Term.strip_comb t;
473 val (c, T) = Term.dest_Const head handle TERM _ => err ();
474 val (U, u) = Consts.the_abbreviation (ProofContext.consts_of ctxt) c
475 handle TYPE _ => err ();
476 val t' = Term.betapplys (Envir.expand_atom T (U, u), args);
477 val eq = Logic.mk_equals (t, t');
478 val ctxt' = Variable.auto_fixes eq ctxt;
479 in ProofContext.pretty_term_abbrev ctxt' eq end;
481 fun pretty_prf full ctxt = Proof_Syntax.pretty_proof_of ctxt full;
483 fun pretty_theory ctxt name =
484 (Theory.requires (ProofContext.theory_of ctxt) name "presentation"; Pretty.str name);
489 val str_of_source = space_implode " " o map T.unparse o #2 o #1 o Args.dest_src;
491 fun maybe_pretty_source pretty src xs =
492 map pretty xs (*always pretty in order to exhibit errors!*)
493 |> (if ! source then K [pretty_text (str_of_source src)] else I);
497 |> (if ! quotes then map Pretty.quote else I)
498 |> (if ! display then
499 map (Output.output o Pretty.string_of o Pretty.indent (! indent))
500 #> space_implode "\\isasep\\isanewline%\n"
501 #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
503 map (Output.output o (if ! break then Pretty.string_of else Pretty.str_of))
504 #> space_implode "\\isasep\\isanewline%\n"
505 #> enclose "\\isa{" "}");
509 (** concrete antiquotations **)
515 fun basic_entities name scan pretty = antiquotation name scan
516 (fn {source, context, ...} => output o maybe_pretty_source (pretty context) source);
518 fun basic_entities_style name scan pretty = antiquotation name scan
519 (fn {source, context, ...} => fn (style, xs) =>
520 output (maybe_pretty_source (fn x => pretty context (style, x)) source xs));
522 fun basic_entity name scan = basic_entities name (scan >> single);
526 val _ = basic_entities_style "thm" (Term_Style.parse -- Attrib.thms) pretty_thm_style;
527 val _ = basic_entity "prop" (Term_Style.parse -- Args.prop) pretty_term_style;
528 val _ = basic_entity "term" (Term_Style.parse -- Args.term) pretty_term_style;
529 val _ = basic_entity "term_type" (Term_Style.parse -- Args.term) pretty_term_typ;
530 val _ = basic_entity "typeof" (Term_Style.parse -- Args.term) pretty_term_typeof;
531 val _ = basic_entity "const" (Args.const_proper false) pretty_const;
532 val _ = basic_entity "abbrev" (Scan.lift Args.name_source) pretty_abbrev;
533 val _ = basic_entity "typ" Args.typ_abbrev Syntax.pretty_typ;
534 val _ = basic_entity "text" (Scan.lift Args.name) (K pretty_text);
535 val _ = basic_entities "prf" Attrib.thms (pretty_prf false);
536 val _ = basic_entities "full_prf" Attrib.thms (pretty_prf true);
537 val _ = basic_entity "theory" (Scan.lift Args.name) pretty_theory;
539 val _ = basic_entities_style "thm_style" (Term_Style.parse_bare -- Attrib.thms) pretty_thm_style;
540 val _ = basic_entity "term_style" (Term_Style.parse_bare -- Args.term) pretty_term_style;
549 fun proof_state state =
550 (case try Toplevel.proof_of state of
552 | _ => error "No proof state");
554 fun goal_state name main_goal = antiquotation name (Scan.succeed ())
555 (fn {state, ...} => fn () => output
556 [Pretty.chunks (Proof.pretty_goals main_goal (proof_state state))]);
560 val _ = goal_state "goals" true;
561 val _ = goal_state "subgoals" false;
568 val _ = OuterKeyword.keyword "by";
570 val _ = antiquotation "lemma"
571 (Args.prop -- Scan.lift (Args.$$$ "by" |-- Method.parse -- Scan.option Method.parse))
572 (fn {source, context, ...} => fn (prop, methods) =>
575 (case Args.dest_src source of ((a, arg :: _), pos) => Args.src ((a, [arg]), pos));
577 |> Proof.theorem NONE (K I) [[(prop, [])]]
578 |> Proof.global_terminal_proof methods;
579 in output (maybe_pretty_source (pretty_term context) prop_src [prop]) end);
586 fun ml_text name ml = antiquotation name (Scan.lift Args.name_source_position)
587 (fn {context, ...} => fn (txt, pos) =>
588 (ML_Context.eval_in (SOME context) false pos (ml txt);
589 Symbol_Pos.content (Symbol_Pos.explode (txt, pos))
590 |> (if ! quotes then quote else I)
591 |> (if ! display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
594 #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
595 #> space_implode "\\isasep\\isanewline%\n")));
599 val _ = ml_text "ML" (fn txt => "fn _ => (" ^ txt ^ ");");
600 val _ = ml_text "ML_type" (fn txt => "val _ = NONE : (" ^ txt ^ ") option;");
601 val _ = ml_text "ML_struct" (fn txt => "functor XXX() = struct structure XX = " ^ txt ^ " end;");
602 val _ = ml_text "ML_functor" (fn txt => "ML_Env.check_functor " ^ ML_Syntax.print_string txt);
603 val _ = ml_text "ML_text" (K "");