wenzelm@22124: (* Title: Pure/Thy/thy_output.ML wenzelm@22124: Author: Markus Wenzel, TU Muenchen wenzelm@22124: wenzelm@30390: Theory document output with antiquotations. wenzelm@22124: *) wenzelm@22124: wenzelm@22124: signature THY_OUTPUT = wenzelm@22124: sig wenzelm@38767: val display_default: bool Unsynchronized.ref wenzelm@38767: val quotes_default: bool Unsynchronized.ref wenzelm@38767: val indent_default: int Unsynchronized.ref wenzelm@38767: val source_default: bool Unsynchronized.ref wenzelm@38767: val break_default: bool Unsynchronized.ref wenzelm@38767: val display: bool Config.T wenzelm@38767: val quotes: bool Config.T wenzelm@38767: val indent: int Config.T wenzelm@38767: val source: bool Config.T wenzelm@38767: val break: bool Config.T wenzelm@38766: val add_wrapper: ((unit -> string) -> unit -> string) -> Proof.context -> Proof.context wenzelm@38766: val add_option: string -> (string -> Proof.context -> Proof.context) -> unit wenzelm@26893: val defined_command: string -> bool wenzelm@26893: val defined_option: string -> bool wenzelm@22124: val print_antiquotations: unit -> unit wenzelm@22124: val boolean: string -> bool wenzelm@22124: val integer: string -> int wenzelm@30513: val antiquotation: string -> 'a context_parser -> wenzelm@38766: ({source: Args.src, state: Toplevel.state, context: Proof.context} -> 'a -> string) -> unit wenzelm@22124: datatype markup = Markup | MarkupEnv | Verbatim wenzelm@32738: val modes: string list Unsynchronized.ref wenzelm@30573: val eval_antiquote: Scan.lexicon -> Toplevel.state -> Symbol_Pos.text * Position.T -> string wenzelm@28427: val present_thy: Scan.lexicon -> (string -> string list) -> (markup -> string -> bool) -> wenzelm@36959: (Toplevel.transition * Toplevel.state) list -> (Token.T, 'a) Source.source -> Buffer.T wenzelm@38767: val pretty_text: Proof.context -> string -> Pretty.T wenzelm@28644: val pretty_term: Proof.context -> term -> Pretty.T wenzelm@28644: val pretty_thm: Proof.context -> thm -> Pretty.T wenzelm@30390: val str_of_source: Args.src -> string wenzelm@38767: val maybe_pretty_source: (Proof.context -> 'a -> Pretty.T) -> Proof.context -> wenzelm@38767: Args.src -> 'a list -> Pretty.T list wenzelm@38767: val output: Proof.context -> Pretty.T list -> string wenzelm@22124: end; wenzelm@22124: wenzelm@37216: structure Thy_Output: THY_OUTPUT = wenzelm@22124: struct wenzelm@22124: wenzelm@22124: (** global options **) wenzelm@22124: wenzelm@38767: val display_default = Unsynchronized.ref false; wenzelm@38767: val quotes_default = Unsynchronized.ref false; wenzelm@38767: val indent_default = Unsynchronized.ref 0; wenzelm@38767: val source_default = Unsynchronized.ref false; wenzelm@38767: val break_default = Unsynchronized.ref false; wenzelm@38767: wenzelm@38767: val (display, setup_display) = Attrib.config_bool "thy_output_display" (fn _ => ! display_default); wenzelm@38767: val (quotes, setup_quotes) = Attrib.config_bool "thy_output_quotes" (fn _ => ! quotes_default); wenzelm@38767: val (indent, setup_indent) = Attrib.config_int "thy_output_indent" (fn _ => ! indent_default); wenzelm@38767: val (source, setup_source) = Attrib.config_bool "thy_output_source" (fn _ => ! source_default); wenzelm@38767: val (break, setup_break) = Attrib.config_bool "thy_output_break" (fn _ => ! break_default); wenzelm@38767: wenzelm@38767: val _ = Context.>> (Context.map_theory wenzelm@38767: (setup_display #> setup_quotes #> setup_indent #> setup_source #> setup_break)); wenzelm@22124: wenzelm@22124: wenzelm@38766: structure Wrappers = Proof_Data wenzelm@38766: ( wenzelm@38766: type T = ((unit -> string) -> unit -> string) list; wenzelm@38766: fun init _ = []; wenzelm@38766: ); wenzelm@38766: wenzelm@38766: fun add_wrapper wrapper = Wrappers.map (cons wrapper); wenzelm@38766: wenzelm@38766: val wrap = Wrappers.get #> fold (fn wrapper => fn f => wrapper f); wenzelm@38766: wenzelm@38766: wenzelm@22124: wenzelm@30390: (** maintain global antiquotations **) wenzelm@22124: wenzelm@22124: local wenzelm@22124: wenzelm@22124: val global_commands = wenzelm@38766: Unsynchronized.ref wenzelm@38766: (Symtab.empty: (Args.src -> Toplevel.state -> Proof.context -> string) Symtab.table); wenzelm@22124: wenzelm@22124: val global_options = wenzelm@38766: Unsynchronized.ref (Symtab.empty: (string -> Proof.context -> Proof.context) Symtab.table); wenzelm@22124: wenzelm@38766: fun add_item kind name item tab = wenzelm@22124: (if not (Symtab.defined tab name) then () wenzelm@27897: else warning ("Redefined document antiquotation " ^ kind ^ ": " ^ quote name); wenzelm@38766: Symtab.update (name, item) tab); wenzelm@22124: wenzelm@22124: in wenzelm@22124: wenzelm@38766: fun add_command name cmd = wenzelm@38766: CRITICAL (fn () => Unsynchronized.change global_commands (add_item "command" name cmd)); wenzelm@38766: fun add_option name opt = wenzelm@38766: CRITICAL (fn () => Unsynchronized.change global_options (add_item "option" name opt)); wenzelm@22124: wenzelm@26893: fun defined_command name = Symtab.defined (! global_commands) name; wenzelm@26893: fun defined_option name = Symtab.defined (! global_options) name; wenzelm@26893: wenzelm@22124: fun command src = wenzelm@22124: let val ((name, _), pos) = Args.dest_src src in wenzelm@22124: (case Symtab.lookup (! global_commands) name of wenzelm@30317: NONE => error ("Unknown document antiquotation: " ^ quote name ^ Position.str_of pos) wenzelm@30317: | SOME f => wenzelm@30317: (Position.report (Markup.doc_antiq name) pos; wenzelm@38766: (fn state => fn ctxt => f src state ctxt handle ERROR msg => wenzelm@30317: cat_error msg ("The error(s) above occurred in document antiquotation: " ^ wenzelm@30317: quote name ^ Position.str_of pos)))) wenzelm@22124: end; wenzelm@22124: wenzelm@38766: fun option (name, s) ctxt = wenzelm@22124: (case Symtab.lookup (! global_options) name of wenzelm@27897: NONE => error ("Unknown document antiquotation option: " ^ quote name) wenzelm@38766: | SOME opt => opt s ctxt); wenzelm@22124: wenzelm@22124: wenzelm@22124: fun print_antiquotations () = wenzelm@27897: [Pretty.big_list "document antiquotation commands:" wenzelm@23272: (map Pretty.str (sort_strings (Symtab.keys (! global_commands)))), wenzelm@27897: Pretty.big_list "document antiquotation options:" wenzelm@23272: (map Pretty.str (sort_strings (Symtab.keys (! global_options))))] wenzelm@22124: |> Pretty.chunks |> Pretty.writeln; wenzelm@22124: wenzelm@22124: end; wenzelm@22124: wenzelm@38766: fun antiquotation name scan out = wenzelm@38766: add_command name wenzelm@38766: (fn src => fn state => fn ctxt => wenzelm@38766: let val (x, ctxt') = Args.context_syntax "document antiquotation" scan src ctxt wenzelm@38766: in out {source = src, state = state, context = ctxt'} x end); wenzelm@30390: wenzelm@22124: wenzelm@22124: wenzelm@22124: (** syntax of antiquotations **) wenzelm@22124: wenzelm@22124: (* option values *) wenzelm@22124: wenzelm@22124: fun boolean "" = true wenzelm@22124: | boolean "true" = true wenzelm@22124: | boolean "false" = false wenzelm@22124: | boolean s = error ("Bad boolean value: " ^ quote s); wenzelm@22124: wenzelm@22124: fun integer s = wenzelm@22124: let wenzelm@22124: fun int ss = wenzelm@22124: (case Library.read_int ss of (i, []) => i wenzelm@22124: | _ => error ("Bad integer value: " ^ quote s)); wenzelm@22124: in (case Symbol.explode s of "-" :: ss => ~ (int ss) | ss => int ss) end; wenzelm@22124: wenzelm@22124: wenzelm@22124: (* outer syntax *) wenzelm@22124: wenzelm@22124: local wenzelm@22124: wenzelm@36950: val property = wenzelm@36950: Parse.xname -- Scan.optional (Parse.$$$ "=" |-- Parse.!!! Parse.xname) ""; wenzelm@36950: wenzelm@36950: val properties = wenzelm@36950: Scan.optional (Parse.$$$ "[" |-- Parse.!!! (Parse.enum "," property --| Parse.$$$ "]")) []; wenzelm@22124: wenzelm@22124: in wenzelm@22124: wenzelm@36950: val antiq = wenzelm@36950: Parse.!!! (Parse.position Parse.xname -- properties -- Args.parse --| Scan.ahead Parse.eof) wenzelm@22124: >> (fn (((x, pos), y), z) => (y, Args.src ((x, z), pos))); wenzelm@22124: wenzelm@22124: end; wenzelm@22124: wenzelm@22124: wenzelm@22124: (* eval_antiquote *) wenzelm@22124: wenzelm@32738: val modes = Unsynchronized.ref ([]: string list); wenzelm@22124: wenzelm@30367: fun eval_antiquote lex state (txt, pos) = wenzelm@22124: let wenzelm@30589: fun expand (Antiquote.Text ss) = Symbol_Pos.content ss wenzelm@30683: | expand (Antiquote.Antiq (ss, (pos, _))) = wenzelm@38766: let wenzelm@38766: val (opts, src) = Token.read_antiq lex antiq (ss, pos); wenzelm@38832: fun cmd ctxt = wrap ctxt (fn () => command src state ctxt) (); wenzelm@38832: val preview_ctxt = fold option opts (Toplevel.presentation_context_of state); wenzelm@38832: val print_ctxt = Context_Position.set_visible false preview_ctxt; wenzelm@38832: val _ = cmd preview_ctxt; wenzelm@38832: in Print_Mode.with_modes (! modes @ Latex.modes) (fn () => cmd print_ctxt) () end wenzelm@27344: | expand (Antiquote.Open _) = "" wenzelm@27344: | expand (Antiquote.Close _) = ""; wenzelm@30642: val ants = Antiquote.read (Symbol_Pos.explode (txt, pos), pos); wenzelm@22124: in wenzelm@30635: if Toplevel.is_toplevel state andalso not (forall Antiquote.is_text ants) then wenzelm@27897: error ("Unknown context -- cannot expand document antiquotations" ^ Position.str_of pos) wenzelm@22124: else implode (map expand ants) wenzelm@22124: end; wenzelm@22124: wenzelm@22124: wenzelm@22124: wenzelm@22124: (** present theory source **) wenzelm@22124: wenzelm@22124: (*NB: arranging white space around command spans is a black art.*) wenzelm@22124: wenzelm@22124: (* presentation tokens *) wenzelm@22124: wenzelm@22124: datatype token = wenzelm@22124: NoToken wenzelm@36959: | BasicToken of Token.T wenzelm@22124: | MarkupToken of string * (string * Position.T) wenzelm@22124: | MarkupEnvToken of string * (string * Position.T) wenzelm@22124: | VerbatimToken of string * Position.T; wenzelm@22124: wenzelm@22124: fun output_token lex state = wenzelm@30367: let val eval = eval_antiquote lex state in wenzelm@22124: fn NoToken => "" wenzelm@22124: | BasicToken tok => Latex.output_basic tok wenzelm@22124: | MarkupToken (cmd, txt) => Latex.output_markup cmd (eval txt) wenzelm@22124: | MarkupEnvToken (cmd, txt) => Latex.output_markup_env cmd (eval txt) wenzelm@22124: | VerbatimToken txt => Latex.output_verbatim (eval txt) wenzelm@22124: end; wenzelm@22124: wenzelm@22124: fun basic_token pred (BasicToken tok) = pred tok wenzelm@22124: | basic_token _ _ = false; wenzelm@22124: wenzelm@36959: val improper_token = basic_token (not o Token.is_proper); wenzelm@36959: val comment_token = basic_token Token.is_comment; wenzelm@36959: val blank_token = basic_token Token.is_blank; wenzelm@36959: val newline_token = basic_token Token.is_newline; wenzelm@22124: wenzelm@22124: wenzelm@22124: (* command spans *) wenzelm@22124: wenzelm@22124: type command = string * Position.T * string list; (*name, position, tags*) wenzelm@22124: type source = (token * (string * int)) list; (*token, markup flag, meta-comment depth*) wenzelm@22124: wenzelm@22124: datatype span = Span of command * (source * source * source * source) * bool; wenzelm@22124: wenzelm@22124: fun make_span cmd src = wenzelm@22124: let wenzelm@22124: fun take_newline (tok :: toks) = wenzelm@22124: if newline_token (fst tok) then ([tok], toks, true) wenzelm@22124: else ([], tok :: toks, false) wenzelm@22124: | take_newline [] = ([], [], false); wenzelm@22124: val (((src_prefix, src_main), src_suffix1), (src_suffix2, src_appendix, newline)) = wenzelm@22124: src wenzelm@22124: |> take_prefix (improper_token o fst) wenzelm@22124: ||>> take_suffix (improper_token o fst) wenzelm@22124: ||>> take_prefix (comment_token o fst) wenzelm@22124: ||> take_newline; wenzelm@22124: in Span (cmd, (src_prefix, src_main, src_suffix1 @ src_suffix2, src_appendix), newline) end; wenzelm@22124: wenzelm@22124: wenzelm@22124: (* present spans *) wenzelm@22124: wenzelm@22124: local wenzelm@22124: wenzelm@22124: fun err_bad_nesting pos = wenzelm@22124: error ("Bad nesting of commands in presentation" ^ pos); wenzelm@22124: wenzelm@22124: fun edge which f (x: string option, y) = wenzelm@22124: if x = y then I wenzelm@22124: else (case which (x, y) of NONE => I | SOME txt => Buffer.add (f txt)); wenzelm@22124: wenzelm@22124: val begin_tag = edge #2 Latex.begin_tag; wenzelm@22124: val end_tag = edge #1 Latex.end_tag; wenzelm@22124: fun open_delim delim e = edge #2 Latex.begin_delim e #> delim #> edge #2 Latex.end_delim e; wenzelm@22124: fun close_delim delim e = edge #1 Latex.begin_delim e #> delim #> edge #1 Latex.end_delim e; wenzelm@22124: wenzelm@22124: in wenzelm@22124: wenzelm@22124: fun present_span lex default_tags span state state' wenzelm@22124: (tag_stack, active_tag, newline, buffer, present_cont) = wenzelm@22124: let wenzelm@22124: val present = fold (fn (tok, (flag, 0)) => wenzelm@22124: Buffer.add (output_token lex state' tok) wenzelm@22124: #> Buffer.add flag wenzelm@22124: | _ => I); wenzelm@22124: wenzelm@22124: val Span ((cmd_name, cmd_pos, cmd_tags), srcs, span_newline) = span; wenzelm@22124: wenzelm@22124: val (tag, tags) = tag_stack; wenzelm@36950: val tag' = try hd (fold Keyword.update_tags cmd_tags (the_list tag)); wenzelm@22124: wenzelm@22124: val active_tag' = wenzelm@22124: if is_some tag' then tag' wenzelm@22124: else if cmd_name = "end" andalso not (Toplevel.is_toplevel state') then NONE wenzelm@22124: else try hd (default_tags cmd_name); wenzelm@22124: val edge = (active_tag, active_tag'); wenzelm@22124: wenzelm@22124: val newline' = wenzelm@22124: if is_none active_tag' then span_newline else newline; wenzelm@22124: wenzelm@22124: val nesting = Toplevel.level state' - Toplevel.level state; wenzelm@22124: val tag_stack' = wenzelm@22124: if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack wenzelm@22124: else if nesting >= 0 then (tag', replicate nesting tag @ tags) wenzelm@22124: else haftmann@33957: (case drop (~ nesting - 1) tags of wenzelm@22124: tgs :: tgss => (tgs, tgss) wenzelm@22124: | [] => err_bad_nesting (Position.str_of cmd_pos)); wenzelm@22124: wenzelm@22124: val buffer' = wenzelm@22124: buffer wenzelm@22124: |> end_tag edge wenzelm@22124: |> close_delim (fst present_cont) edge wenzelm@22124: |> snd present_cont wenzelm@22124: |> open_delim (present (#1 srcs)) edge wenzelm@22124: |> begin_tag edge wenzelm@22124: |> present (#2 srcs); wenzelm@22124: val present_cont' = wenzelm@22124: if newline then (present (#3 srcs), present (#4 srcs)) wenzelm@22124: else (I, present (#3 srcs) #> present (#4 srcs)); wenzelm@22124: in (tag_stack', active_tag', newline', buffer', present_cont') end; wenzelm@22124: wenzelm@22124: fun present_trailer ((_, tags), active_tag, _, buffer, present_cont) = wenzelm@22124: if not (null tags) then err_bad_nesting " at end of theory" wenzelm@22124: else wenzelm@22124: buffer wenzelm@22124: |> end_tag (active_tag, NONE) wenzelm@22124: |> close_delim (fst present_cont) (active_tag, NONE) wenzelm@22124: |> snd present_cont; wenzelm@22124: wenzelm@22124: end; wenzelm@22124: wenzelm@22124: wenzelm@28427: (* present_thy *) wenzelm@22124: wenzelm@22124: datatype markup = Markup | MarkupEnv | Verbatim; wenzelm@22124: wenzelm@22124: local wenzelm@22124: wenzelm@22124: val space_proper = wenzelm@36959: Scan.one Token.is_blank -- Scan.many Token.is_comment -- Scan.one Token.is_proper; wenzelm@22124: wenzelm@36959: val is_improper = not o (Token.is_proper orf Token.is_begin_ignore orf Token.is_end_ignore); wenzelm@22124: val improper = Scan.many is_improper; wenzelm@22124: val improper_end = Scan.repeat (Scan.unless space_proper (Scan.one is_improper)); wenzelm@36959: val blank_end = Scan.repeat (Scan.unless space_proper (Scan.one Token.is_blank)); wenzelm@22124: wenzelm@36959: val opt_newline = Scan.option (Scan.one Token.is_newline); wenzelm@22124: wenzelm@22124: val ignore = wenzelm@36959: Scan.depend (fn d => opt_newline |-- Scan.one Token.is_begin_ignore wenzelm@22124: >> pair (d + 1)) || wenzelm@36959: Scan.depend (fn d => Scan.one Token.is_end_ignore --| wenzelm@22124: (if d = 0 then Scan.fail_with (K "Bad nesting of meta-comments") else opt_newline) wenzelm@22124: >> pair (d - 1)); wenzelm@22124: wenzelm@36950: val tag = (improper -- Parse.$$$ "%" -- improper) |-- Parse.!!! (Parse.tag_name --| blank_end); wenzelm@22124: wenzelm@22124: val locale = wenzelm@36950: Scan.option ((Parse.$$$ "(" -- improper -- Parse.$$$ "in") |-- wenzelm@36950: Parse.!!! (improper |-- Parse.xname --| (improper -- Parse.$$$ ")"))); wenzelm@22124: wenzelm@22124: in wenzelm@22124: wenzelm@28427: fun present_thy lex default_tags is_markup command_results src = wenzelm@22124: let wenzelm@22124: (* tokens *) wenzelm@22124: wenzelm@22124: val ignored = Scan.state --| ignore wenzelm@22124: >> (fn d => (NONE, (NoToken, ("", d)))); wenzelm@22124: wenzelm@22124: fun markup mark mk flag = Scan.peek (fn d => wenzelm@36950: improper |-- wenzelm@36959: Parse.position wenzelm@36959: (Scan.one (Token.is_kind Token.Command andf is_markup mark o Token.content_of)) -- wenzelm@22124: Scan.repeat tag -- wenzelm@36950: Parse.!!!! ((improper -- locale -- improper) |-- Parse.doc_source --| improper_end) wenzelm@22124: >> (fn (((tok, pos), tags), txt) => wenzelm@36959: let val name = Token.content_of tok wenzelm@22124: in (SOME (name, pos, tags), (mk (name, txt), (flag, d))) end)); wenzelm@22124: wenzelm@22124: val command = Scan.peek (fn d => wenzelm@36959: Parse.position (Scan.one (Token.is_kind Token.Command)) -- wenzelm@22124: Scan.repeat tag wenzelm@22124: >> (fn ((tok, pos), tags) => wenzelm@36959: let val name = Token.content_of tok wenzelm@22124: in (SOME (name, pos, tags), (BasicToken tok, (Latex.markup_false, d))) end)); wenzelm@22124: wenzelm@22124: val cmt = Scan.peek (fn d => wenzelm@36950: Parse.$$$ "--" |-- Parse.!!!! (improper |-- Parse.doc_source) wenzelm@22124: >> (fn txt => (NONE, (MarkupToken ("cmt", txt), ("", d))))); wenzelm@22124: wenzelm@22124: val other = Scan.peek (fn d => wenzelm@36950: Parse.not_eof >> (fn tok => (NONE, (BasicToken tok, ("", d))))); wenzelm@22124: wenzelm@22124: val token = wenzelm@22124: ignored || wenzelm@22124: markup Markup MarkupToken Latex.markup_true || wenzelm@22124: markup MarkupEnv MarkupEnvToken Latex.markup_true || wenzelm@22124: markup Verbatim (VerbatimToken o #2) "" || wenzelm@22124: command || cmt || other; wenzelm@22124: wenzelm@22124: wenzelm@22124: (* spans *) wenzelm@22124: wenzelm@36959: val is_eof = fn (_, (BasicToken x, _)) => Token.is_eof x | _ => false; wenzelm@36959: val stopper = Scan.stopper (K (NONE, (BasicToken Token.eof, ("", 0)))) is_eof; wenzelm@22124: wenzelm@22124: val cmd = Scan.one (is_some o fst); wenzelm@27732: val non_cmd = Scan.one (is_none o fst andf not o is_eof) >> #2; wenzelm@22124: wenzelm@22124: val comments = Scan.many (comment_token o fst o snd); wenzelm@22124: val blank = Scan.one (blank_token o fst o snd); wenzelm@22124: val newline = Scan.one (newline_token o fst o snd); wenzelm@22124: val before_cmd = wenzelm@22124: Scan.option (newline -- comments) -- wenzelm@22124: Scan.option (newline -- comments) -- wenzelm@22124: Scan.option (blank -- comments) -- cmd; wenzelm@22124: wenzelm@22124: val span = wenzelm@22124: Scan.repeat non_cmd -- cmd -- wenzelm@22124: Scan.repeat (Scan.unless before_cmd non_cmd) -- wenzelm@22124: Scan.option (newline >> (single o snd)) wenzelm@22124: >> (fn (((toks1, (cmd, tok2)), toks3), tok4) => wenzelm@22124: make_span (the cmd) (toks1 @ (tok2 :: (toks3 @ the_default [] tok4)))); wenzelm@22124: wenzelm@22124: val spans = wenzelm@22124: src wenzelm@36959: |> Source.filter (not o Token.is_semicolon) wenzelm@36959: |> Source.source' 0 Token.stopper (Scan.error (Scan.bulk token)) NONE wenzelm@22124: |> Source.source stopper (Scan.error (Scan.bulk span)) NONE wenzelm@22124: |> Source.exhaust; wenzelm@28427: wenzelm@28427: wenzelm@28427: (* present commands *) wenzelm@28427: wenzelm@28427: fun present_command tr span st st' = wenzelm@28427: Toplevel.setmp_thread_position tr (present_span lex default_tags span st st'); wenzelm@28427: wenzelm@28427: fun present _ [] = I wenzelm@28427: | present st (((tr, st'), span) :: rest) = present_command tr span st st' #> present st' rest; wenzelm@22124: in wenzelm@28427: if length command_results = length spans then wenzelm@22124: ((NONE, []), NONE, true, Buffer.empty, (I, I)) wenzelm@28427: |> present Toplevel.toplevel (command_results ~~ spans) wenzelm@22124: |> present_trailer wenzelm@22124: else error "Messed-up outer syntax for presentation" wenzelm@22124: end; wenzelm@22124: wenzelm@22124: end; wenzelm@22124: wenzelm@22124: wenzelm@22124: wenzelm@22124: (** setup default output **) wenzelm@22124: wenzelm@22124: (* options *) wenzelm@22124: wenzelm@39134: val _ = add_option "show_types" (Config.put show_types o boolean); wenzelm@39134: val _ = add_option "show_sorts" (Config.put show_sorts o boolean); wenzelm@39127: val _ = add_option "show_structs" (Config.put show_structs o boolean); wenzelm@38980: val _ = add_option "show_question_marks" (Config.put show_question_marks o boolean); wenzelm@38766: val _ = add_option "long_names" (add_wrapper o setmp_CRITICAL Name_Space.long_names o boolean); wenzelm@38766: val _ = add_option "short_names" (add_wrapper o setmp_CRITICAL Name_Space.short_names o boolean); wenzelm@38766: val _ = add_option "unique_names" (add_wrapper o setmp_CRITICAL Name_Space.unique_names o boolean); wenzelm@39128: val _ = add_option "eta_contract" (Config.put eta_contract o boolean); wenzelm@38767: val _ = add_option "display" (Config.put display o boolean); wenzelm@38767: val _ = add_option "break" (Config.put break o boolean); wenzelm@38767: val _ = add_option "quotes" (Config.put quotes o boolean); wenzelm@38766: val _ = add_option "mode" (add_wrapper o Print_Mode.with_modes o single); wenzelm@38766: val _ = add_option "margin" (add_wrapper o setmp_CRITICAL Pretty.margin_default o integer); wenzelm@38767: val _ = add_option "indent" (Config.put indent o integer); wenzelm@38767: val _ = add_option "source" (Config.put source o boolean); wenzelm@39125: val _ = add_option "goals_limit" (Config.put Goal_Display.goals_limit o integer); wenzelm@22124: wenzelm@22124: wenzelm@22124: (* basic pretty printing *) wenzelm@22124: wenzelm@38767: fun tweak_line ctxt s = wenzelm@38767: if Config.get ctxt display then s else Symbol.strip_blanks s; wenzelm@22124: wenzelm@38767: fun pretty_text ctxt = wenzelm@38767: Pretty.chunks o map Pretty.str o map (tweak_line ctxt) o Library.split_lines; wenzelm@22124: wenzelm@26710: fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t; wenzelm@24920: haftmann@32898: fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of; haftmann@32898: haftmann@32898: fun pretty_term_style ctxt (style, t) = haftmann@32898: pretty_term ctxt (style t); wenzelm@22124: haftmann@32898: fun pretty_thm_style ctxt (style, th) = haftmann@32898: pretty_term ctxt (style (Thm.full_prop_of th)); wenzelm@22124: haftmann@32898: fun pretty_term_typ ctxt (style, t) = haftmann@32898: let val t' = style t wenzelm@39288: in pretty_term ctxt (Type.constraint (Term.fastype_of t') t') end; haftmann@32898: haftmann@32898: fun pretty_term_typeof ctxt (style, t) = haftmann@32898: Syntax.pretty_typ ctxt (Term.fastype_of (style t)); wenzelm@22124: wenzelm@25373: fun pretty_const ctxt c = wenzelm@25373: let wenzelm@25373: val t = Const (c, Consts.type_scheme (ProofContext.consts_of ctxt) c) wenzelm@25373: handle TYPE (msg, _, _) => error msg; wenzelm@25373: val ([t'], _) = Variable.import_terms true [t] ctxt; wenzelm@25373: in pretty_term ctxt t' end; wenzelm@22124: wenzelm@25407: fun pretty_abbrev ctxt s = wenzelm@22124: let wenzelm@25407: val t = Syntax.parse_term ctxt s |> singleton (ProofContext.standard_infer_types ctxt); wenzelm@24920: fun err () = error ("Abbreviated constant expected: " ^ Syntax.string_of_term ctxt t); wenzelm@22124: val (head, args) = Term.strip_comb t; wenzelm@22124: val (c, T) = Term.dest_Const head handle TERM _ => err (); wenzelm@25054: val (U, u) = Consts.the_abbreviation (ProofContext.consts_of ctxt) c wenzelm@22124: handle TYPE _ => err (); wenzelm@22124: val t' = Term.betapplys (Envir.expand_atom T (U, u), args); haftmann@32898: val eq = Logic.mk_equals (t, t'); haftmann@32898: val ctxt' = Variable.auto_fixes eq ctxt; haftmann@32898: in ProofContext.pretty_term_abbrev ctxt' eq end; wenzelm@22124: haftmann@39305: fun pretty_class ctxt = haftmann@39305: Pretty.str o Type.extern_class (ProofContext.tsig_of ctxt) o ProofContext.read_class ctxt; haftmann@39305: haftmann@39309: fun pretty_type ctxt s = haftmann@39309: let haftmann@39309: val tsig = ProofContext.tsig_of ctxt; haftmann@39309: val _ = ProofContext.read_type_name ctxt false s; haftmann@39309: in (Pretty.str o Type.extern_type tsig o Type.intern_type tsig) s end; haftmann@39305: wenzelm@33388: fun pretty_prf full ctxt = Proof_Syntax.pretty_proof_of ctxt full; wenzelm@22124: wenzelm@22124: fun pretty_theory ctxt name = wenzelm@22124: (Theory.requires (ProofContext.theory_of ctxt) name "presentation"; Pretty.str name); wenzelm@22124: wenzelm@22124: wenzelm@30390: (* default output *) wenzelm@30390: wenzelm@36959: val str_of_source = space_implode " " o map Token.unparse o #2 o #1 o Args.dest_src; wenzelm@22124: wenzelm@38767: fun maybe_pretty_source pretty ctxt src xs = wenzelm@38767: map (pretty ctxt) xs (*always pretty in order to exhibit errors!*) wenzelm@38767: |> (if Config.get ctxt source then K [pretty_text ctxt (str_of_source src)] else I); wenzelm@30390: wenzelm@38767: fun output ctxt prts = wenzelm@30390: prts wenzelm@38767: |> (if Config.get ctxt quotes then map Pretty.quote else I) wenzelm@38767: |> (if Config.get ctxt display then wenzelm@38767: map (Output.output o Pretty.string_of o Pretty.indent (Config.get ctxt indent)) wenzelm@22124: #> space_implode "\\isasep\\isanewline%\n" wenzelm@22124: #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" wenzelm@22124: else wenzelm@38767: map (Output.output o (if Config.get ctxt break then Pretty.string_of else Pretty.str_of)) wenzelm@22124: #> space_implode "\\isasep\\isanewline%\n" wenzelm@22124: #> enclose "\\isa{" "}"); wenzelm@22124: wenzelm@30390: wenzelm@30390: wenzelm@30390: (** concrete antiquotations **) wenzelm@30390: wenzelm@30390: (* basic entities *) wenzelm@30390: wenzelm@30390: local wenzelm@30390: wenzelm@30390: fun basic_entities name scan pretty = antiquotation name scan wenzelm@38767: (fn {source, context, ...} => output context o maybe_pretty_source pretty context source); wenzelm@30390: haftmann@32890: fun basic_entities_style name scan pretty = antiquotation name scan haftmann@32890: (fn {source, context, ...} => fn (style, xs) => wenzelm@38767: output context wenzelm@38767: (maybe_pretty_source (fn ctxt => fn x => pretty ctxt (style, x)) context source xs)); haftmann@32890: wenzelm@30390: fun basic_entity name scan = basic_entities name (scan >> single); wenzelm@30390: wenzelm@30390: in wenzelm@30390: haftmann@32890: val _ = basic_entities_style "thm" (Term_Style.parse -- Attrib.thms) pretty_thm_style; haftmann@32890: val _ = basic_entity "prop" (Term_Style.parse -- Args.prop) pretty_term_style; haftmann@32890: val _ = basic_entity "term" (Term_Style.parse -- Args.term) pretty_term_style; haftmann@32898: val _ = basic_entity "term_type" (Term_Style.parse -- Args.term) pretty_term_typ; haftmann@32898: val _ = basic_entity "typeof" (Term_Style.parse -- Args.term) pretty_term_typeof; wenzelm@35399: val _ = basic_entity "const" (Args.const_proper false) pretty_const; wenzelm@30390: val _ = basic_entity "abbrev" (Scan.lift Args.name_source) pretty_abbrev; wenzelm@30390: val _ = basic_entity "typ" Args.typ_abbrev Syntax.pretty_typ; haftmann@39305: val _ = basic_entity "class" (Scan.lift Args.name) pretty_class; haftmann@39305: val _ = basic_entity "type" (Scan.lift Args.name) pretty_type; wenzelm@38767: val _ = basic_entity "text" (Scan.lift Args.name) pretty_text; wenzelm@30390: val _ = basic_entities "prf" Attrib.thms (pretty_prf false); wenzelm@30390: val _ = basic_entities "full_prf" Attrib.thms (pretty_prf true); wenzelm@30390: val _ = basic_entity "theory" (Scan.lift Args.name) pretty_theory; wenzelm@30390: haftmann@32898: val _ = basic_entities_style "thm_style" (Term_Style.parse_bare -- Attrib.thms) pretty_thm_style; haftmann@32898: val _ = basic_entity "term_style" (Term_Style.parse_bare -- Args.term) pretty_term_style; haftmann@32898: wenzelm@30390: end; wenzelm@30390: wenzelm@30390: wenzelm@30390: (* goal state *) wenzelm@30390: wenzelm@30390: local wenzelm@22124: wenzelm@30367: fun proof_state state = wenzelm@30367: (case try Toplevel.proof_of state of wenzelm@30367: SOME prf => prf wenzelm@22124: | _ => error "No proof state"); wenzelm@22124: wenzelm@30390: fun goal_state name main_goal = antiquotation name (Scan.succeed ()) wenzelm@39125: (fn {state, context = ctxt, ...} => fn () => output ctxt wenzelm@39129: [Pretty.chunks wenzelm@39129: (Proof.pretty_goals main_goal (Proof.map_context (K ctxt) (proof_state state)))]); wenzelm@22124: wenzelm@30390: in wenzelm@30390: wenzelm@30390: val _ = goal_state "goals" true; wenzelm@30390: val _ = goal_state "subgoals" false; wenzelm@30390: wenzelm@30390: end; wenzelm@22124: wenzelm@22124: wenzelm@30390: (* embedded lemma *) wenzelm@27522: wenzelm@36950: val _ = Keyword.keyword "by"; wenzelm@27381: wenzelm@30390: val _ = antiquotation "lemma" wenzelm@30390: (Args.prop -- Scan.lift (Args.$$$ "by" |-- Method.parse -- Scan.option Method.parse)) wenzelm@30390: (fn {source, context, ...} => fn (prop, methods) => wenzelm@30390: let wenzelm@30390: val prop_src = wenzelm@30390: (case Args.dest_src source of ((a, arg :: _), pos) => Args.src ((a, [arg]), pos)); wenzelm@30390: val _ = context wenzelm@36323: |> Proof.theorem NONE (K I) [[(prop, [])]] wenzelm@30390: |> Proof.global_terminal_proof methods; wenzelm@38767: in output context (maybe_pretty_source pretty_term context prop_src [prop]) end); wenzelm@30390: wenzelm@30390: wenzelm@30390: (* ML text *) wenzelm@30390: wenzelm@30390: local wenzelm@30390: wenzelm@30390: fun ml_text name ml = antiquotation name (Scan.lift Args.name_source_position) wenzelm@30390: (fn {context, ...} => fn (txt, pos) => wenzelm@37198: (ML_Context.eval_in (SOME context) false pos (ml pos txt); wenzelm@30573: Symbol_Pos.content (Symbol_Pos.explode (txt, pos)) wenzelm@38767: |> (if Config.get context quotes then quote else I) wenzelm@38767: |> (if Config.get context display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}" wenzelm@30390: else wenzelm@30390: split_lines wenzelm@30390: #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|") wenzelm@30390: #> space_implode "\\isasep\\isanewline%\n"))); wenzelm@30390: wenzelm@37198: fun ml_enclose bg en pos txt = wenzelm@37198: ML_Lex.read Position.none bg @ ML_Lex.read pos txt @ ML_Lex.read Position.none en; wenzelm@37198: wenzelm@30390: in wenzelm@30390: wenzelm@37198: val _ = ml_text "ML" (ml_enclose "fn _ => (" ");"); wenzelm@37198: val _ = ml_text "ML_type" (ml_enclose "val _ = NONE : (" ") option;"); wenzelm@37198: val _ = ml_text "ML_struct" (ml_enclose "functor XXX() = struct structure XX = " " end;"); wenzelm@37198: wenzelm@37198: val _ = ml_text "ML_functor" (* FIXME formal treatment of functor name (!?) *) wenzelm@37198: (fn pos => fn txt => wenzelm@37198: ML_Lex.read Position.none ("ML_Env.check_functor " ^ wenzelm@37198: ML_Syntax.print_string (Symbol_Pos.content (Symbol_Pos.explode (txt, pos))))); wenzelm@37198: wenzelm@37198: val _ = ml_text "ML_text" (K (K [])); wenzelm@22124: wenzelm@22124: end; wenzelm@30390: wenzelm@30390: end;