src/Pure/Thy/thy_output.ML
author wenzelm
Sun Mar 08 21:12:37 2009 +0100 (2009-03-08)
changeset 30368 1a2a54f910c9
parent 30367 ee8841b1b981
child 30381 a59d792d0ccf
permissions -rw-r--r--
added (raw_)antiquotation -- simplified wrapper for defining output commands;
     1 (*  Title:      Pure/Thy/thy_output.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Theory document output.
     5 *)
     6 
     7 signature THY_OUTPUT =
     8 sig
     9   val display: bool ref
    10   val quotes: bool ref
    11   val indent: int ref
    12   val source: bool ref
    13   val break: bool 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 raw_antiquotation: string -> (Args.src -> Toplevel.state ->
    22     Context.generic * Args.T list -> string * (Context.generic * Args.T list)) -> unit
    23   val antiquotation: string -> (Args.src -> Proof.context ->
    24     Context.generic * Args.T list -> string * (Context.generic * Args.T list)) -> unit
    25   val args: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) ->
    26     (Args.src -> Proof.context -> 'a -> string) -> Args.src -> Toplevel.state -> string
    27   datatype markup = Markup | MarkupEnv | Verbatim
    28   val modes: string list ref
    29   val eval_antiquote: Scan.lexicon -> Toplevel.state -> SymbolPos.text * Position.T -> string
    30   val present_thy: Scan.lexicon -> (string -> string list) -> (markup -> string -> bool) ->
    31     (Toplevel.transition * Toplevel.state) list -> (OuterLex.token, 'a) Source.source -> Buffer.T
    32   val str_of_source: Args.src -> string
    33   val pretty_text: string -> Pretty.T
    34   val pretty_term: Proof.context -> term -> Pretty.T
    35   val pretty_thm: Proof.context -> thm -> Pretty.T
    36   val output_list: (Proof.context -> 'a -> Pretty.T) -> Args.src ->
    37     Proof.context -> 'a list -> string
    38   val output: (Proof.context -> 'a -> Pretty.T) -> Args.src -> Proof.context -> 'a -> string
    39 end;
    40 
    41 structure ThyOutput: THY_OUTPUT =
    42 struct
    43 
    44 structure T = OuterLex;
    45 structure P = OuterParse;
    46 
    47 
    48 (** global options **)
    49 
    50 val locale = ref "";
    51 val display = ref false;
    52 val quotes = ref false;
    53 val indent = ref 0;
    54 val source = ref false;
    55 val break = ref false;
    56 
    57 
    58 
    59 (** maintain global commands **)
    60 
    61 local
    62 
    63 val global_commands =
    64   ref (Symtab.empty: (Args.src -> Toplevel.state -> string) Symtab.table);
    65 
    66 val global_options =
    67   ref (Symtab.empty: (string -> (unit -> string) -> unit -> string) Symtab.table);
    68 
    69 fun add_item kind (name, x) tab =
    70  (if not (Symtab.defined tab name) then ()
    71   else warning ("Redefined document antiquotation " ^ kind ^ ": " ^ quote name);
    72   Symtab.update (name, x) tab);
    73 
    74 in
    75 
    76 fun add_commands xs = CRITICAL (fn () => change global_commands (fold (add_item "command") xs));
    77 fun add_options xs = CRITICAL (fn () => change global_options (fold (add_item "option") xs));
    78 
    79 fun defined_command name = Symtab.defined (! global_commands) name;
    80 fun defined_option name = Symtab.defined (! global_options) name;
    81 
    82 fun command src =
    83   let val ((name, _), pos) = Args.dest_src src in
    84     (case Symtab.lookup (! global_commands) name of
    85       NONE => error ("Unknown document antiquotation: " ^ quote name ^ Position.str_of pos)
    86     | SOME f =>
    87        (Position.report (Markup.doc_antiq name) pos;
    88         (fn state => f src state handle ERROR msg =>
    89           cat_error msg ("The error(s) above occurred in document antiquotation: " ^
    90             quote name ^ Position.str_of pos))))
    91   end;
    92 
    93 fun option (name, s) f () =
    94   (case Symtab.lookup (! global_options) name of
    95     NONE => error ("Unknown document antiquotation option: " ^ quote name)
    96   | SOME opt => opt s f ());
    97 
    98 fun options [] f = f
    99   | options (opt :: opts) f = option opt (options opts f);
   100 
   101 
   102 fun print_antiquotations () =
   103  [Pretty.big_list "document antiquotation commands:"
   104     (map Pretty.str (sort_strings (Symtab.keys (! global_commands)))),
   105   Pretty.big_list "document antiquotation options:"
   106     (map Pretty.str (sort_strings (Symtab.keys (! global_options))))]
   107  |> Pretty.chunks |> Pretty.writeln;
   108 
   109 end;
   110 
   111 
   112 
   113 (** syntax of antiquotations **)
   114 
   115 (* option values *)
   116 
   117 fun boolean "" = true
   118   | boolean "true" = true
   119   | boolean "false" = false
   120   | boolean s = error ("Bad boolean value: " ^ quote s);
   121 
   122 fun integer s =
   123   let
   124     fun int ss =
   125       (case Library.read_int ss of (i, []) => i
   126       | _ => error ("Bad integer value: " ^ quote s));
   127   in (case Symbol.explode s of "-" :: ss => ~ (int ss) | ss => int ss) end;
   128 
   129 
   130 (* args syntax *)
   131 
   132 fun syntax scan = Args.context_syntax "document antiquotation" scan;
   133 
   134 fun raw_antiquotation name scan =
   135   add_commands [(name, fn src => fn state =>
   136     #1 (syntax (scan src state) src (Toplevel.presentation_context_of state NONE)))];
   137 
   138 fun antiquotation name scan =
   139   raw_antiquotation name (fn src => fn state =>
   140     scan src (Toplevel.presentation_context_of state NONE));
   141 
   142 fun args scan f src state : string =
   143   let
   144     val loc = if ! locale = "" then NONE else SOME (! locale);
   145     val (x, ctxt) = syntax scan src (Toplevel.presentation_context_of state loc);
   146   in f src ctxt x end;
   147 
   148 
   149 (* outer syntax *)
   150 
   151 local
   152 
   153 val property = P.xname -- Scan.optional (P.$$$ "=" |-- P.!!! P.xname) "";
   154 val properties = Scan.optional (P.$$$ "[" |-- P.!!! (P.enum "," property --| P.$$$ "]")) [];
   155 
   156 in
   157 
   158 val antiq = P.!!! (P.position P.xname -- properties -- Args.parse --| Scan.ahead P.eof)
   159   >> (fn (((x, pos), y), z) => (y, Args.src ((x, z), pos)));
   160 
   161 end;
   162 
   163 
   164 (* eval_antiquote *)
   165 
   166 val modes = ref ([]: string list);
   167 
   168 fun eval_antiquote lex state (txt, pos) =
   169   let
   170     fun expand (Antiquote.Text s) = s
   171       | expand (Antiquote.Antiq x) =
   172           let val (opts, src) = Antiquote.read_antiq lex antiq x in
   173             options opts (fn () => command src state) ();  (*preview errors!*)
   174             PrintMode.with_modes (! modes @ Latex.modes)
   175               (Output.no_warnings (options opts (fn () => command src state))) ()
   176           end
   177       | expand (Antiquote.Open _) = ""
   178       | expand (Antiquote.Close _) = "";
   179     val ants = Antiquote.read (SymbolPos.explode (txt, pos), pos);
   180   in
   181     if Toplevel.is_toplevel state andalso exists Antiquote.is_antiq ants then
   182       error ("Unknown context -- cannot expand document antiquotations" ^ Position.str_of pos)
   183     else implode (map expand ants)
   184   end;
   185 
   186 
   187 
   188 (** present theory source **)
   189 
   190 (*NB: arranging white space around command spans is a black art.*)
   191 
   192 (* presentation tokens *)
   193 
   194 datatype token =
   195     NoToken
   196   | BasicToken of T.token
   197   | MarkupToken of string * (string * Position.T)
   198   | MarkupEnvToken of string * (string * Position.T)
   199   | VerbatimToken of string * Position.T;
   200 
   201 fun output_token lex state =
   202   let val eval = eval_antiquote lex state in
   203     fn NoToken => ""
   204      | BasicToken tok => Latex.output_basic tok
   205      | MarkupToken (cmd, txt) => Latex.output_markup cmd (eval txt)
   206      | MarkupEnvToken (cmd, txt) => Latex.output_markup_env cmd (eval txt)
   207      | VerbatimToken txt => Latex.output_verbatim (eval txt)
   208   end;
   209 
   210 fun basic_token pred (BasicToken tok) = pred tok
   211   | basic_token _ _ = false;
   212 
   213 val improper_token = basic_token (not o T.is_proper);
   214 val comment_token = basic_token T.is_comment;
   215 val blank_token = basic_token T.is_blank;
   216 val newline_token = basic_token T.is_newline;
   217 
   218 
   219 (* command spans *)
   220 
   221 type command = string * Position.T * string list;   (*name, position, tags*)
   222 type source = (token * (string * int)) list;        (*token, markup flag, meta-comment depth*)
   223 
   224 datatype span = Span of command * (source * source * source * source) * bool;
   225 
   226 fun make_span cmd src =
   227   let
   228     fun take_newline (tok :: toks) =
   229           if newline_token (fst tok) then ([tok], toks, true)
   230           else ([], tok :: toks, false)
   231       | take_newline [] = ([], [], false);
   232     val (((src_prefix, src_main), src_suffix1), (src_suffix2, src_appendix, newline)) =
   233       src
   234       |> take_prefix (improper_token o fst)
   235       ||>> take_suffix (improper_token o fst)
   236       ||>> take_prefix (comment_token o fst)
   237       ||> take_newline;
   238   in Span (cmd, (src_prefix, src_main, src_suffix1 @ src_suffix2, src_appendix), newline) end;
   239 
   240 
   241 (* present spans *)
   242 
   243 local
   244 
   245 fun err_bad_nesting pos =
   246   error ("Bad nesting of commands in presentation" ^ pos);
   247 
   248 fun edge which f (x: string option, y) =
   249   if x = y then I
   250   else (case which (x, y) of NONE => I | SOME txt => Buffer.add (f txt));
   251 
   252 val begin_tag = edge #2 Latex.begin_tag;
   253 val end_tag = edge #1 Latex.end_tag;
   254 fun open_delim delim e = edge #2 Latex.begin_delim e #> delim #> edge #2 Latex.end_delim e;
   255 fun close_delim delim e = edge #1 Latex.begin_delim e #> delim #> edge #1 Latex.end_delim e;
   256 
   257 in
   258 
   259 fun present_span lex default_tags span state state'
   260     (tag_stack, active_tag, newline, buffer, present_cont) =
   261   let
   262     val present = fold (fn (tok, (flag, 0)) =>
   263         Buffer.add (output_token lex state' tok)
   264         #> Buffer.add flag
   265       | _ => I);
   266 
   267     val Span ((cmd_name, cmd_pos, cmd_tags), srcs, span_newline) = span;
   268 
   269     val (tag, tags) = tag_stack;
   270     val tag' = try hd (fold OuterKeyword.update_tags cmd_tags (the_list tag));
   271 
   272     val active_tag' =
   273       if is_some tag' then tag'
   274       else if cmd_name = "end" andalso not (Toplevel.is_toplevel state') then NONE
   275       else try hd (default_tags cmd_name);
   276     val edge = (active_tag, active_tag');
   277 
   278     val newline' =
   279       if is_none active_tag' then span_newline else newline;
   280 
   281     val nesting = Toplevel.level state' - Toplevel.level state;
   282     val tag_stack' =
   283       if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack
   284       else if nesting >= 0 then (tag', replicate nesting tag @ tags)
   285       else
   286         (case Library.drop (~ nesting - 1, tags) of
   287           tgs :: tgss => (tgs, tgss)
   288         | [] => err_bad_nesting (Position.str_of cmd_pos));
   289 
   290     val buffer' =
   291       buffer
   292       |> end_tag edge
   293       |> close_delim (fst present_cont) edge
   294       |> snd present_cont
   295       |> open_delim (present (#1 srcs)) edge
   296       |> begin_tag edge
   297       |> present (#2 srcs);
   298     val present_cont' =
   299       if newline then (present (#3 srcs), present (#4 srcs))
   300       else (I, present (#3 srcs) #> present (#4 srcs));
   301   in (tag_stack', active_tag', newline', buffer', present_cont') end;
   302 
   303 fun present_trailer ((_, tags), active_tag, _, buffer, present_cont) =
   304   if not (null tags) then err_bad_nesting " at end of theory"
   305   else
   306     buffer
   307     |> end_tag (active_tag, NONE)
   308     |> close_delim (fst present_cont) (active_tag, NONE)
   309     |> snd present_cont;
   310 
   311 end;
   312 
   313 
   314 (* present_thy *)
   315 
   316 datatype markup = Markup | MarkupEnv | Verbatim;
   317 
   318 local
   319 
   320 val space_proper =
   321   Scan.one T.is_blank -- Scan.many T.is_comment -- Scan.one T.is_proper;
   322 
   323 val is_improper = not o (T.is_proper orf T.is_begin_ignore orf T.is_end_ignore);
   324 val improper = Scan.many is_improper;
   325 val improper_end = Scan.repeat (Scan.unless space_proper (Scan.one is_improper));
   326 val blank_end = Scan.repeat (Scan.unless space_proper (Scan.one T.is_blank));
   327 
   328 val opt_newline = Scan.option (Scan.one T.is_newline);
   329 
   330 val ignore =
   331   Scan.depend (fn d => opt_newline |-- Scan.one T.is_begin_ignore
   332     >> pair (d + 1)) ||
   333   Scan.depend (fn d => Scan.one T.is_end_ignore --|
   334     (if d = 0 then Scan.fail_with (K "Bad nesting of meta-comments") else opt_newline)
   335     >> pair (d - 1));
   336 
   337 val tag = (improper -- P.$$$ "%" -- improper) |-- P.!!! (P.tag_name --| blank_end);
   338 
   339 val locale =
   340   Scan.option ((P.$$$ "(" -- improper -- P.$$$ "in") |--
   341     P.!!! (improper |-- P.xname --| (improper -- P.$$$ ")")));
   342 
   343 in
   344 
   345 fun present_thy lex default_tags is_markup command_results src =
   346   let
   347     (* tokens *)
   348 
   349     val ignored = Scan.state --| ignore
   350       >> (fn d => (NONE, (NoToken, ("", d))));
   351 
   352     fun markup mark mk flag = Scan.peek (fn d =>
   353       improper |-- P.position (Scan.one (T.is_kind T.Command andf is_markup mark o T.content_of)) --
   354       Scan.repeat tag --
   355       P.!!!! ((improper -- locale -- improper) |-- P.doc_source --| improper_end)
   356       >> (fn (((tok, pos), tags), txt) =>
   357         let val name = T.content_of tok
   358         in (SOME (name, pos, tags), (mk (name, txt), (flag, d))) end));
   359 
   360     val command = Scan.peek (fn d =>
   361       P.position (Scan.one (T.is_kind T.Command)) --
   362       Scan.repeat tag
   363       >> (fn ((tok, pos), tags) =>
   364         let val name = T.content_of tok
   365         in (SOME (name, pos, tags), (BasicToken tok, (Latex.markup_false, d))) end));
   366 
   367     val cmt = Scan.peek (fn d =>
   368       P.$$$ "--" |-- P.!!!! (improper |-- P.doc_source)
   369       >> (fn txt => (NONE, (MarkupToken ("cmt", txt), ("", d)))));
   370 
   371     val other = Scan.peek (fn d =>
   372        P.not_eof >> (fn tok => (NONE, (BasicToken tok, ("", d)))));
   373 
   374     val token =
   375       ignored ||
   376       markup Markup MarkupToken Latex.markup_true ||
   377       markup MarkupEnv MarkupEnvToken Latex.markup_true ||
   378       markup Verbatim (VerbatimToken o #2) "" ||
   379       command || cmt || other;
   380 
   381 
   382     (* spans *)
   383 
   384     val is_eof = fn (_, (BasicToken x, _)) => T.is_eof x | _ => false;
   385     val stopper = Scan.stopper (K (NONE, (BasicToken T.eof, ("", 0)))) is_eof;
   386 
   387     val cmd = Scan.one (is_some o fst);
   388     val non_cmd = Scan.one (is_none o fst andf not o is_eof) >> #2;
   389 
   390     val comments = Scan.many (comment_token o fst o snd);
   391     val blank = Scan.one (blank_token o fst o snd);
   392     val newline = Scan.one (newline_token o fst o snd);
   393     val before_cmd =
   394       Scan.option (newline -- comments) --
   395       Scan.option (newline -- comments) --
   396       Scan.option (blank -- comments) -- cmd;
   397 
   398     val span =
   399       Scan.repeat non_cmd -- cmd --
   400         Scan.repeat (Scan.unless before_cmd non_cmd) --
   401         Scan.option (newline >> (single o snd))
   402       >> (fn (((toks1, (cmd, tok2)), toks3), tok4) =>
   403           make_span (the cmd) (toks1 @ (tok2 :: (toks3 @ the_default [] tok4))));
   404 
   405     val spans =
   406       src
   407       |> Source.filter (not o T.is_semicolon)
   408       |> Source.source' 0 T.stopper (Scan.error (Scan.bulk token)) NONE
   409       |> Source.source stopper (Scan.error (Scan.bulk span)) NONE
   410       |> Source.exhaust;
   411 
   412 
   413     (* present commands *)
   414 
   415     fun present_command tr span st st' =
   416       Toplevel.setmp_thread_position tr (present_span lex default_tags span st st');
   417 
   418     fun present _ [] = I
   419       | present st (((tr, st'), span) :: rest) = present_command tr span st st' #> present st' rest;
   420   in
   421     if length command_results = length spans then
   422       ((NONE, []), NONE, true, Buffer.empty, (I, I))
   423       |> present Toplevel.toplevel (command_results ~~ spans)
   424       |> present_trailer
   425     else error "Messed-up outer syntax for presentation"
   426   end;
   427 
   428 end;
   429 
   430 
   431 
   432 (** setup default output **)
   433 
   434 (* options *)
   435 
   436 val _ = add_options
   437  [("show_types", Library.setmp Syntax.show_types o boolean),
   438   ("show_sorts", Library.setmp Syntax.show_sorts o boolean),
   439   ("show_structs", Library.setmp show_structs o boolean),
   440   ("show_question_marks", Library.setmp show_question_marks o boolean),
   441   ("long_names", Library.setmp NameSpace.long_names o boolean),
   442   ("short_names", Library.setmp NameSpace.short_names o boolean),
   443   ("unique_names", Library.setmp NameSpace.unique_names o boolean),
   444   ("eta_contract", Library.setmp Syntax.eta_contract o boolean),
   445   ("locale", Library.setmp locale),
   446   ("display", Library.setmp display o boolean),
   447   ("break", Library.setmp break o boolean),
   448   ("quotes", Library.setmp quotes o boolean),
   449   ("mode", fn s => fn f => PrintMode.with_modes [s] f),
   450   ("margin", Pretty.setmp_margin o integer),
   451   ("indent", Library.setmp indent o integer),
   452   ("source", Library.setmp source o boolean),
   453   ("goals_limit", Library.setmp goals_limit o integer)];
   454 
   455 
   456 (* basic pretty printing *)
   457 
   458 val str_of_source = space_implode " " o map T.unparse o #2 o #1 o Args.dest_src;
   459 
   460 fun tweak_line s =
   461   if ! display then s else Symbol.strip_blanks s;
   462 
   463 val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
   464 
   465 fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
   466 
   467 fun pretty_term_abbrev ctxt t = ProofContext.pretty_term_abbrev (Variable.auto_fixes t ctxt) t;
   468 
   469 fun pretty_term_typ ctxt t =
   470   Syntax.pretty_term ctxt (TypeInfer.constrain (Term.fastype_of t) t);
   471 
   472 fun pretty_term_typeof ctxt = Syntax.pretty_typ ctxt o Term.fastype_of;
   473 
   474 fun pretty_const ctxt c =
   475   let
   476     val t = Const (c, Consts.type_scheme (ProofContext.consts_of ctxt) c)
   477       handle TYPE (msg, _, _) => error msg;
   478     val ([t'], _) = Variable.import_terms true [t] ctxt;
   479   in pretty_term ctxt t' end;
   480 
   481 fun pretty_abbrev ctxt s =
   482   let
   483     val t = Syntax.parse_term ctxt s |> singleton (ProofContext.standard_infer_types ctxt);
   484     fun err () = error ("Abbreviated constant expected: " ^ Syntax.string_of_term ctxt t);
   485     val (head, args) = Term.strip_comb t;
   486     val (c, T) = Term.dest_Const head handle TERM _ => err ();
   487     val (U, u) = Consts.the_abbreviation (ProofContext.consts_of ctxt) c
   488       handle TYPE _ => err ();
   489     val t' = Term.betapplys (Envir.expand_atom T (U, u), args);
   490   in pretty_term_abbrev ctxt (Logic.mk_equals (t, t')) end;
   491 
   492 fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
   493 
   494 fun pretty_term_style ctxt (name, t) =
   495   pretty_term ctxt (TermStyle.the_style (ProofContext.theory_of ctxt) name ctxt t);
   496 
   497 fun pretty_thm_style ctxt (name, th) =
   498   pretty_term_style ctxt (name, Thm.full_prop_of th);
   499 
   500 fun pretty_prf full ctxt thms =
   501   Pretty.chunks (map (ProofSyntax.pretty_proof_of ctxt full) thms);
   502 
   503 fun pretty_theory ctxt name =
   504   (Theory.requires (ProofContext.theory_of ctxt) name "presentation"; Pretty.str name);
   505 
   506 
   507 (* Isar output *)
   508 
   509 fun output_list pretty src ctxt xs =
   510   map (pretty ctxt) xs        (*always pretty in order to exhibit errors!*)
   511   |> (if ! source then K [pretty_text (str_of_source src)] else I)
   512   |> (if ! quotes then map Pretty.quote else I)
   513   |> (if ! display then
   514     map (Output.output o Pretty.string_of o Pretty.indent (! indent))
   515     #> space_implode "\\isasep\\isanewline%\n"
   516     #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   517   else
   518     map (Output.output o (if ! break then Pretty.string_of else Pretty.str_of))
   519     #> space_implode "\\isasep\\isanewline%\n"
   520     #> enclose "\\isa{" "}");
   521 
   522 fun output pretty src ctxt = output_list pretty src ctxt o single;
   523 
   524 fun proof_state state =
   525   (case try Toplevel.proof_of state of
   526     SOME prf => prf
   527   | _ => error "No proof state");
   528 
   529 fun output_goals main_goal src state = args (Scan.succeed ()) (output (fn _ => fn _ =>
   530   Pretty.chunks (Proof.pretty_goals main_goal (proof_state state)))) src state;
   531 
   532 fun ml_val txt = "fn _ => (" ^ txt ^ ");";
   533 fun ml_type txt = "val _ = NONE : (" ^ txt ^ ") option;";
   534 fun ml_struct txt = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
   535 
   536 fun output_ml ml _ ctxt (txt, pos) =
   537  (ML_Context.eval_in (SOME ctxt) false pos (ml txt);
   538   SymbolPos.content (SymbolPos.explode (txt, pos))
   539   |> (if ! quotes then quote else I)
   540   |> (if ! display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
   541   else
   542     split_lines
   543     #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
   544     #> space_implode "\\isasep\\isanewline%\n"));
   545 
   546 
   547 (* embedded lemmas *)
   548 
   549 fun pretty_lemma ctxt (prop, methods) =
   550   let
   551     val _ = ctxt
   552       |> Proof.theorem_i NONE (K I) [[(prop, [])]]
   553       |> Proof.global_terminal_proof methods;
   554   in pretty_term ctxt prop end;
   555 
   556 val embedded_lemma =
   557   args (Args.prop -- Scan.lift (Args.$$$ "by" |-- Method.parse -- Scan.option Method.parse))
   558     (output pretty_lemma o (fn ((a, arg :: _), p) => (Args.src ((a, [arg]), p))) o Args.dest_src);
   559 
   560 
   561 (* commands *)
   562 
   563 val _ = OuterKeyword.keyword "by";
   564 
   565 val _ = add_commands
   566  [("thm", args Attrib.thms (output_list pretty_thm)),
   567   ("thm_style", args (Scan.lift Args.liberal_name -- Attrib.thm) (output pretty_thm_style)),
   568   ("prop", args Args.prop (output pretty_term)),
   569   ("lemma", embedded_lemma),
   570   ("term", args Args.term (output pretty_term)),
   571   ("term_style", args (Scan.lift Args.liberal_name -- Args.term) (output pretty_term_style)),
   572   ("term_type", args Args.term (output pretty_term_typ)),
   573   ("typeof", args Args.term (output pretty_term_typeof)),
   574   ("const", args Args.const_proper (output pretty_const)),
   575   ("abbrev", args (Scan.lift Args.name_source) (output pretty_abbrev)),
   576   ("typ", args Args.typ_abbrev (output Syntax.pretty_typ)),
   577   ("text", args (Scan.lift Args.name) (output (K pretty_text))),
   578   ("goals", output_goals true),
   579   ("subgoals", output_goals false),
   580   ("prf", args Attrib.thms (output (pretty_prf false))),
   581   ("full_prf", args Attrib.thms (output (pretty_prf true))),
   582   ("theory", args (Scan.lift Args.name) (output pretty_theory)),
   583   ("ML", args (Scan.lift Args.name_source_position) (output_ml ml_val)),
   584   ("ML_type", args (Scan.lift Args.name_source_position) (output_ml ml_type)),
   585   ("ML_struct", args (Scan.lift Args.name_source_position) (output_ml ml_struct))];
   586 
   587 end;