src/Pure/Thy/thy_output.ML
changeset 22124 27b674312b2f
child 23272 25c84ca5e9a9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/Thy/thy_output.ML	Fri Jan 19 22:10:35 2007 +0100
     1.3 @@ -0,0 +1,532 @@
     1.4 +(*  Title:      Pure/Thy/thy_output.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Markus Wenzel, TU Muenchen
     1.7 +
     1.8 +Theory document output.
     1.9 +*)
    1.10 +
    1.11 +signature THY_OUTPUT =
    1.12 +sig
    1.13 +  val display: bool ref
    1.14 +  val quotes: bool ref
    1.15 +  val indent: int ref
    1.16 +  val source: bool ref
    1.17 +  val add_commands: (string * (Args.src -> Toplevel.node option -> string)) list -> unit
    1.18 +  val add_options: (string * (string -> (unit -> string) -> unit -> string)) list -> unit
    1.19 +  val print_antiquotations: unit -> unit
    1.20 +  val boolean: string -> bool
    1.21 +  val integer: string -> int
    1.22 +  val args: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) ->
    1.23 +    (Args.src -> Proof.context -> 'a -> string) -> Args.src -> Toplevel.node option -> string
    1.24 +  datatype markup = Markup | MarkupEnv | Verbatim
    1.25 +  val modes: string list ref
    1.26 +  val eval_antiquote: Scan.lexicon -> Toplevel.node option -> string * Position.T -> string
    1.27 +  val present_thy: Scan.lexicon -> (string -> string list) -> (markup -> string -> bool) ->
    1.28 +    Toplevel.transition list -> (OuterLex.token, 'a) Source.source -> Buffer.T
    1.29 +  val output_list: (Proof.context -> 'a -> Pretty.T) -> Args.src ->
    1.30 +    Proof.context -> 'a list -> string
    1.31 +  val output: (Proof.context -> 'a -> Pretty.T) -> Args.src -> Proof.context -> 'a -> string
    1.32 +end;
    1.33 +
    1.34 +structure ThyOutput: THY_OUTPUT =
    1.35 +struct
    1.36 +
    1.37 +structure T = OuterLex;
    1.38 +structure P = OuterParse;
    1.39 +
    1.40 +
    1.41 +(** global options **)
    1.42 +
    1.43 +val locale = ref "";
    1.44 +val display = ref false;
    1.45 +val quotes = ref false;
    1.46 +val indent = ref 0;
    1.47 +val source = ref false;
    1.48 +val break = ref false;
    1.49 +
    1.50 +
    1.51 +
    1.52 +(** maintain global commands **)
    1.53 +
    1.54 +local
    1.55 +
    1.56 +val global_commands =
    1.57 +  ref (Symtab.empty: (Args.src -> Toplevel.node option -> string) Symtab.table);
    1.58 +
    1.59 +val global_options =
    1.60 +  ref (Symtab.empty: (string -> (unit -> string) -> unit -> string) Symtab.table);
    1.61 +
    1.62 +fun add_item kind (name, x) tab =
    1.63 + (if not (Symtab.defined tab name) then ()
    1.64 +  else warning ("Redefined text antiquotation " ^ kind ^ ": " ^ quote name);
    1.65 +  Symtab.update (name, x) tab);
    1.66 +
    1.67 +in
    1.68 +
    1.69 +val add_commands = Library.change global_commands o fold (add_item "command");
    1.70 +val add_options = Library.change global_options o fold (add_item "option");
    1.71 +
    1.72 +fun command src =
    1.73 +  let val ((name, _), pos) = Args.dest_src src in
    1.74 +    (case Symtab.lookup (! global_commands) name of
    1.75 +      NONE => error ("Unknown text antiquotation command: " ^ quote name ^ Position.str_of pos)
    1.76 +    | SOME f => transform_failure (curry Antiquote.ANTIQUOTE_FAIL (name, pos)) (f src))
    1.77 +  end;
    1.78 +
    1.79 +fun option (name, s) f () =
    1.80 +  (case Symtab.lookup (! global_options) name of
    1.81 +    NONE => error ("Unknown text antiquotation option: " ^ quote name)
    1.82 +  | SOME opt => opt s f ());
    1.83 +
    1.84 +fun options [] f = f
    1.85 +  | options (opt :: opts) f = option opt (options opts f);
    1.86 +
    1.87 +
    1.88 +fun print_antiquotations () =
    1.89 + [Pretty.big_list "text antiquotation commands:"
    1.90 +    (map Pretty.str (Symtab.keys (! global_commands))),
    1.91 +  Pretty.big_list "text antiquotation options:" (map Pretty.str (Symtab.keys (! global_options)))]
    1.92 + |> Pretty.chunks |> Pretty.writeln;
    1.93 +
    1.94 +end;
    1.95 +
    1.96 +
    1.97 +
    1.98 +(** syntax of antiquotations **)
    1.99 +
   1.100 +(* option values *)
   1.101 +
   1.102 +fun boolean "" = true
   1.103 +  | boolean "true" = true
   1.104 +  | boolean "false" = false
   1.105 +  | boolean s = error ("Bad boolean value: " ^ quote s);
   1.106 +
   1.107 +fun integer s =
   1.108 +  let
   1.109 +    fun int ss =
   1.110 +      (case Library.read_int ss of (i, []) => i
   1.111 +      | _ => error ("Bad integer value: " ^ quote s));
   1.112 +  in (case Symbol.explode s of "-" :: ss => ~ (int ss) | ss => int ss) end;
   1.113 +
   1.114 +
   1.115 +(* args syntax *)
   1.116 +
   1.117 +fun syntax scan = Args.context_syntax "text antiquotation" scan;
   1.118 +
   1.119 +fun args scan f src node : string =
   1.120 +  let
   1.121 +    val loc = if ! locale = "" then NONE else SOME (! locale);
   1.122 +    val (x, ctxt) = syntax scan src (Toplevel.presentation_context node loc);
   1.123 +  in f src ctxt x end;
   1.124 +
   1.125 +
   1.126 +(* outer syntax *)
   1.127 +
   1.128 +local
   1.129 +
   1.130 +val property = P.xname -- Scan.optional (P.$$$ "=" |-- P.!!! P.xname) "";
   1.131 +val properties = Scan.optional (P.$$$ "[" |-- P.!!! (P.enum "," property --| P.$$$ "]")) [];
   1.132 +
   1.133 +in
   1.134 +
   1.135 +val antiq = P.!!! (P.position P.xname -- properties -- P.arguments --| Scan.ahead P.eof)
   1.136 +  >> (fn (((x, pos), y), z) => (y, Args.src ((x, z), pos)));
   1.137 +
   1.138 +end;
   1.139 +
   1.140 +
   1.141 +(* eval_antiquote *)
   1.142 +
   1.143 +val modes = ref ([]: string list);
   1.144 +
   1.145 +fun eval_antiquote lex node (str, pos) =
   1.146 +  let
   1.147 +    fun expand (Antiquote.Text s) = s
   1.148 +      | expand (Antiquote.Antiq x) =
   1.149 +          let val (opts, src) = Antiquote.scan_arguments lex antiq x in
   1.150 +            options opts (fn () => command src node) ();  (*preview errors!*)
   1.151 +            Library.setmp print_mode (! modes @ Latex.modes @ ! print_mode)
   1.152 +              (Output.no_warnings (options opts (fn () => command src node))) ()
   1.153 +          end;
   1.154 +    val ants = Antiquote.scan_antiquotes (str, pos);
   1.155 +  in
   1.156 +    if is_none node andalso exists Antiquote.is_antiq ants then
   1.157 +      error ("Cannot expand text antiquotations at top-level" ^ Position.str_of pos)
   1.158 +    else implode (map expand ants)
   1.159 +  end;
   1.160 +
   1.161 +
   1.162 +
   1.163 +(** present theory source **)
   1.164 +
   1.165 +(*NB: arranging white space around command spans is a black art.*)
   1.166 +
   1.167 +(* presentation tokens *)
   1.168 +
   1.169 +datatype token =
   1.170 +    NoToken
   1.171 +  | BasicToken of T.token
   1.172 +  | MarkupToken of string * (string * Position.T)
   1.173 +  | MarkupEnvToken of string * (string * Position.T)
   1.174 +  | VerbatimToken of string * Position.T;
   1.175 +
   1.176 +fun output_token lex state =
   1.177 +  let
   1.178 +    val eval = eval_antiquote lex (try Toplevel.node_of state)
   1.179 +  in
   1.180 +    fn NoToken => ""
   1.181 +     | BasicToken tok => Latex.output_basic tok
   1.182 +     | MarkupToken (cmd, txt) => Latex.output_markup cmd (eval txt)
   1.183 +     | MarkupEnvToken (cmd, txt) => Latex.output_markup_env cmd (eval txt)
   1.184 +     | VerbatimToken txt => Latex.output_verbatim (eval txt)
   1.185 +  end;
   1.186 +
   1.187 +fun basic_token pred (BasicToken tok) = pred tok
   1.188 +  | basic_token _ _ = false;
   1.189 +
   1.190 +val improper_token = basic_token (not o T.is_proper);
   1.191 +val comment_token = basic_token T.is_comment;
   1.192 +val blank_token = basic_token T.is_blank;
   1.193 +val newline_token = basic_token T.is_newline;
   1.194 +
   1.195 +
   1.196 +(* command spans *)
   1.197 +
   1.198 +type command = string * Position.T * string list;   (*name, position, tags*)
   1.199 +type source = (token * (string * int)) list;        (*token, markup flag, meta-comment depth*)
   1.200 +
   1.201 +datatype span = Span of command * (source * source * source * source) * bool;
   1.202 +
   1.203 +fun make_span cmd src =
   1.204 +  let
   1.205 +    fun take_newline (tok :: toks) =
   1.206 +          if newline_token (fst tok) then ([tok], toks, true)
   1.207 +          else ([], tok :: toks, false)
   1.208 +      | take_newline [] = ([], [], false);
   1.209 +    val (((src_prefix, src_main), src_suffix1), (src_suffix2, src_appendix, newline)) =
   1.210 +      src
   1.211 +      |> take_prefix (improper_token o fst)
   1.212 +      ||>> take_suffix (improper_token o fst)
   1.213 +      ||>> take_prefix (comment_token o fst)
   1.214 +      ||> take_newline;
   1.215 +  in Span (cmd, (src_prefix, src_main, src_suffix1 @ src_suffix2, src_appendix), newline) end;
   1.216 +
   1.217 +
   1.218 +(* present spans *)
   1.219 +
   1.220 +local
   1.221 +
   1.222 +fun err_bad_nesting pos =
   1.223 +  error ("Bad nesting of commands in presentation" ^ pos);
   1.224 +
   1.225 +fun edge which f (x: string option, y) =
   1.226 +  if x = y then I
   1.227 +  else (case which (x, y) of NONE => I | SOME txt => Buffer.add (f txt));
   1.228 +
   1.229 +val begin_tag = edge #2 Latex.begin_tag;
   1.230 +val end_tag = edge #1 Latex.end_tag;
   1.231 +fun open_delim delim e = edge #2 Latex.begin_delim e #> delim #> edge #2 Latex.end_delim e;
   1.232 +fun close_delim delim e = edge #1 Latex.begin_delim e #> delim #> edge #1 Latex.end_delim e;
   1.233 +
   1.234 +in
   1.235 +
   1.236 +fun present_span lex default_tags span state state'
   1.237 +    (tag_stack, active_tag, newline, buffer, present_cont) =
   1.238 +  let
   1.239 +    val present = fold (fn (tok, (flag, 0)) =>
   1.240 +        Buffer.add (output_token lex state' tok)
   1.241 +        #> Buffer.add flag
   1.242 +      | _ => I);
   1.243 +
   1.244 +    val Span ((cmd_name, cmd_pos, cmd_tags), srcs, span_newline) = span;
   1.245 +
   1.246 +    val (tag, tags) = tag_stack;
   1.247 +    val tag' = try hd (fold OuterKeyword.update_tags cmd_tags (the_list tag));
   1.248 +
   1.249 +    val active_tag' =
   1.250 +      if is_some tag' then tag'
   1.251 +      else if cmd_name = "end" andalso not (Toplevel.is_toplevel state') then NONE
   1.252 +      else try hd (default_tags cmd_name);
   1.253 +    val edge = (active_tag, active_tag');
   1.254 +
   1.255 +    val newline' =
   1.256 +      if is_none active_tag' then span_newline else newline;
   1.257 +
   1.258 +    val nesting = Toplevel.level state' - Toplevel.level state;
   1.259 +    val tag_stack' =
   1.260 +      if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack
   1.261 +      else if nesting >= 0 then (tag', replicate nesting tag @ tags)
   1.262 +      else
   1.263 +        (case Library.drop (~ nesting - 1, tags) of
   1.264 +          tgs :: tgss => (tgs, tgss)
   1.265 +        | [] => err_bad_nesting (Position.str_of cmd_pos));
   1.266 +
   1.267 +    val buffer' =
   1.268 +      buffer
   1.269 +      |> end_tag edge
   1.270 +      |> close_delim (fst present_cont) edge
   1.271 +      |> snd present_cont
   1.272 +      |> open_delim (present (#1 srcs)) edge
   1.273 +      |> begin_tag edge
   1.274 +      |> present (#2 srcs);
   1.275 +    val present_cont' =
   1.276 +      if newline then (present (#3 srcs), present (#4 srcs))
   1.277 +      else (I, present (#3 srcs) #> present (#4 srcs));
   1.278 +  in (tag_stack', active_tag', newline', buffer', present_cont') end;
   1.279 +
   1.280 +fun present_trailer ((_, tags), active_tag, _, buffer, present_cont) =
   1.281 +  if not (null tags) then err_bad_nesting " at end of theory"
   1.282 +  else
   1.283 +    buffer
   1.284 +    |> end_tag (active_tag, NONE)
   1.285 +    |> close_delim (fst present_cont) (active_tag, NONE)
   1.286 +    |> snd present_cont;
   1.287 +
   1.288 +end;
   1.289 +
   1.290 +
   1.291 +(* present_thy *)
   1.292 +
   1.293 +datatype markup = Markup | MarkupEnv | Verbatim;
   1.294 +
   1.295 +local
   1.296 +
   1.297 +val space_proper =
   1.298 +  Scan.one T.is_blank -- Scan.many T.is_comment -- Scan.one T.is_proper;
   1.299 +
   1.300 +val is_improper = not o (T.is_proper orf T.is_begin_ignore orf T.is_end_ignore);
   1.301 +val improper = Scan.many is_improper;
   1.302 +val improper_end = Scan.repeat (Scan.unless space_proper (Scan.one is_improper));
   1.303 +val blank_end = Scan.repeat (Scan.unless space_proper (Scan.one T.is_blank));
   1.304 +
   1.305 +val opt_newline = Scan.option (Scan.one T.is_newline);
   1.306 +
   1.307 +val ignore =
   1.308 +  Scan.depend (fn d => opt_newline |-- Scan.one T.is_begin_ignore
   1.309 +    >> pair (d + 1)) ||
   1.310 +  Scan.depend (fn d => Scan.one T.is_end_ignore --|
   1.311 +    (if d = 0 then Scan.fail_with (K "Bad nesting of meta-comments") else opt_newline)
   1.312 +    >> pair (d - 1));
   1.313 +
   1.314 +val tag = (improper -- P.$$$ "%" -- improper) |-- P.!!! (P.tag_name --| blank_end);
   1.315 +
   1.316 +val locale =
   1.317 +  Scan.option ((P.$$$ "(" -- improper -- P.$$$ "in") |--
   1.318 +    P.!!! (improper |-- P.xname --| (improper -- P.$$$ ")")));
   1.319 +
   1.320 +in
   1.321 +
   1.322 +fun present_thy lex default_tags is_markup trs src =
   1.323 +  let
   1.324 +    (* tokens *)
   1.325 +
   1.326 +    val ignored = Scan.state --| ignore
   1.327 +      >> (fn d => (NONE, (NoToken, ("", d))));
   1.328 +
   1.329 +    fun markup mark mk flag = Scan.peek (fn d =>
   1.330 +      improper |-- P.position (Scan.one (T.is_kind T.Command andf is_markup mark o T.val_of)) --
   1.331 +      Scan.repeat tag --
   1.332 +      P.!!!! ((improper -- locale -- improper) |-- P.position P.text --| improper_end)
   1.333 +      >> (fn (((tok, pos), tags), txt) =>
   1.334 +        let val name = T.val_of tok
   1.335 +        in (SOME (name, pos, tags), (mk (name, txt), (flag, d))) end));
   1.336 +
   1.337 +    val command = Scan.peek (fn d =>
   1.338 +      P.position (Scan.one (T.is_kind T.Command)) --
   1.339 +      Scan.repeat tag
   1.340 +      >> (fn ((tok, pos), tags) =>
   1.341 +        let val name = T.val_of tok
   1.342 +        in (SOME (name, pos, tags), (BasicToken tok, (Latex.markup_false, d))) end));
   1.343 +
   1.344 +    val cmt = Scan.peek (fn d =>
   1.345 +      P.$$$ "--" |-- P.!!!! (improper |-- P.position P.text)
   1.346 +      >> (fn txt => (NONE, (MarkupToken ("cmt", txt), ("", d)))));
   1.347 +
   1.348 +    val other = Scan.peek (fn d =>
   1.349 +       Scan.one T.not_eof >> (fn tok => (NONE, (BasicToken tok, ("", d)))));
   1.350 +
   1.351 +    val token =
   1.352 +      ignored ||
   1.353 +      markup Markup MarkupToken Latex.markup_true ||
   1.354 +      markup MarkupEnv MarkupEnvToken Latex.markup_true ||
   1.355 +      markup Verbatim (VerbatimToken o #2) "" ||
   1.356 +      command || cmt || other;
   1.357 +
   1.358 +
   1.359 +    (* spans *)
   1.360 +
   1.361 +    val stopper =
   1.362 +      ((NONE, (BasicToken (#1 T.stopper), ("", 0))),
   1.363 +        fn (_, (BasicToken x, _)) => #2 T.stopper x | _ => false);
   1.364 +
   1.365 +    val cmd = Scan.one (is_some o fst);
   1.366 +    val non_cmd = Scan.one (is_none o fst andf not o #2 stopper) >> #2;
   1.367 +
   1.368 +    val comments = Scan.many (comment_token o fst o snd);
   1.369 +    val blank = Scan.one (blank_token o fst o snd);
   1.370 +    val newline = Scan.one (newline_token o fst o snd);
   1.371 +    val before_cmd =
   1.372 +      Scan.option (newline -- comments) --
   1.373 +      Scan.option (newline -- comments) --
   1.374 +      Scan.option (blank -- comments) -- cmd;
   1.375 +
   1.376 +    val span =
   1.377 +      Scan.repeat non_cmd -- cmd --
   1.378 +        Scan.repeat (Scan.unless before_cmd non_cmd) --
   1.379 +        Scan.option (newline >> (single o snd))
   1.380 +      >> (fn (((toks1, (cmd, tok2)), toks3), tok4) =>
   1.381 +          make_span (the cmd) (toks1 @ (tok2 :: (toks3 @ the_default [] tok4))));
   1.382 +
   1.383 +    val spans =
   1.384 +      src
   1.385 +      |> Source.filter (not o T.is_semicolon)
   1.386 +      |> Source.source' 0 T.stopper (Scan.error (Scan.bulk token)) NONE
   1.387 +      |> Source.source stopper (Scan.error (Scan.bulk span)) NONE
   1.388 +      |> Source.exhaust;
   1.389 +  in
   1.390 +    if length trs = length spans then
   1.391 +      ((NONE, []), NONE, true, Buffer.empty, (I, I))
   1.392 +      |> Toplevel.present_excursion (trs ~~ map (present_span lex default_tags) spans)
   1.393 +      |> present_trailer
   1.394 +    else error "Messed-up outer syntax for presentation"
   1.395 +  end;
   1.396 +
   1.397 +end;
   1.398 +
   1.399 +
   1.400 +
   1.401 +(** setup default output **)
   1.402 +
   1.403 +(* options *)
   1.404 +
   1.405 +val _ = add_options
   1.406 + [("show_types", Library.setmp Syntax.show_types o boolean),
   1.407 +  ("show_sorts", Library.setmp Syntax.show_sorts o boolean),
   1.408 +  ("show_structs", Library.setmp show_structs o boolean),
   1.409 +  ("show_question_marks", Library.setmp show_question_marks o boolean),
   1.410 +  ("long_names", Library.setmp NameSpace.long_names o boolean),
   1.411 +  ("short_names", Library.setmp NameSpace.short_names o boolean),
   1.412 +  ("unique_names", Library.setmp NameSpace.unique_names o boolean),
   1.413 +  ("eta_contract", Library.setmp Syntax.eta_contract o boolean),
   1.414 +  ("locale", Library.setmp locale),
   1.415 +  ("display", Library.setmp display o boolean),
   1.416 +  ("break", Library.setmp break o boolean),
   1.417 +  ("quotes", Library.setmp quotes o boolean),
   1.418 +  ("mode", fn s => fn f => fn () => Library.setmp print_mode (s :: ! print_mode) f ()),
   1.419 +  ("margin", Pretty.setmp_margin o integer),
   1.420 +  ("indent", Library.setmp indent o integer),
   1.421 +  ("source", Library.setmp source o boolean),
   1.422 +  ("goals_limit", Library.setmp goals_limit o integer)];
   1.423 +
   1.424 +
   1.425 +(* basic pretty printing *)
   1.426 +
   1.427 +val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
   1.428 +
   1.429 +fun tweak_line s =
   1.430 +  if ! display then s else Symbol.strip_blanks s;
   1.431 +
   1.432 +val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
   1.433 +
   1.434 +fun pretty_term ctxt = ProofContext.pretty_term ctxt o ProofContext.revert_skolems ctxt;
   1.435 +fun pretty_term_abbrev ctxt =
   1.436 +  ProofContext.pretty_term_abbrev ctxt o ProofContext.revert_skolems ctxt;
   1.437 +
   1.438 +fun pretty_term_typ ctxt t =
   1.439 +  ProofContext.pretty_term ctxt (TypeInfer.constrain t (Term.fastype_of t));
   1.440 +
   1.441 +fun pretty_term_typeof ctxt = ProofContext.pretty_typ ctxt o Term.fastype_of;
   1.442 +
   1.443 +fun pretty_term_const ctxt t =
   1.444 +  if Term.is_Const t then pretty_term ctxt t
   1.445 +  else error ("Logical constant expected: " ^ ProofContext.string_of_term ctxt t);
   1.446 +
   1.447 +fun pretty_abbrev ctxt t =
   1.448 +  let
   1.449 +    fun err () = error ("Abbreviated constant expected: " ^ ProofContext.string_of_term ctxt t);
   1.450 +    val (head, args) = Term.strip_comb t;
   1.451 +    val (c, T) = Term.dest_Const head handle TERM _ => err ();
   1.452 +    val (U, (u, _)) = Consts.the_abbreviation (ProofContext.consts_of ctxt) c
   1.453 +      handle TYPE _ => err ();
   1.454 +    val t' = Term.betapplys (Envir.expand_atom T (U, u), args);
   1.455 +  in pretty_term_abbrev ctxt (Logic.mk_equals (t, t')) end;
   1.456 +
   1.457 +fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
   1.458 +
   1.459 +fun pretty_term_style ctxt (name, t) =
   1.460 +  pretty_term ctxt (TermStyle.the_style (ProofContext.theory_of ctxt) name ctxt t);
   1.461 +
   1.462 +fun pretty_thm_style ctxt (name, th) =
   1.463 +  pretty_term_style ctxt (name, Thm.full_prop_of th);
   1.464 +
   1.465 +fun pretty_prf full ctxt thms =
   1.466 +  Pretty.chunks (map (ProofContext.pretty_proof_of ctxt full) thms);
   1.467 +
   1.468 +fun pretty_theory ctxt name =
   1.469 +  (Theory.requires (ProofContext.theory_of ctxt) name "presentation"; Pretty.str name);
   1.470 +
   1.471 +
   1.472 +(* Isar output *)
   1.473 +
   1.474 +fun output_list pretty src ctxt xs =
   1.475 +  map (pretty ctxt) xs        (*always pretty in order to exhibit errors!*)
   1.476 +  |> (if ! source then K [pretty_text (str_of_source src)] else I)
   1.477 +  |> (if ! quotes then map Pretty.quote else I)
   1.478 +  |> (if ! display then
   1.479 +    map (Output.output o Pretty.string_of o Pretty.indent (! indent))
   1.480 +    #> space_implode "\\isasep\\isanewline%\n"
   1.481 +    #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   1.482 +  else
   1.483 +    map (Output.output o (if ! break then Pretty.string_of else Pretty.str_of))
   1.484 +    #> space_implode "\\isasep\\isanewline%\n"
   1.485 +    #> enclose "\\isa{" "}");
   1.486 +
   1.487 +fun output pretty src ctxt = output_list pretty src ctxt o single;
   1.488 +
   1.489 +fun proof_state node =
   1.490 +  (case Option.map Toplevel.proof_node node of
   1.491 +    SOME (SOME prf) => ProofHistory.current prf
   1.492 +  | _ => error "No proof state");
   1.493 +
   1.494 +fun output_goals main_goal src node = args (Scan.succeed ()) (output (fn _ => fn _ =>
   1.495 +  Pretty.chunks (Proof.pretty_goals main_goal (proof_state node)))) src node;
   1.496 +
   1.497 +fun ml_val txt = "fn _ => (" ^ txt ^ ");";
   1.498 +fun ml_type txt = "val _ = NONE : (" ^ txt ^ ") option;";
   1.499 +fun ml_struct txt = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
   1.500 +
   1.501 +fun output_ml ml src ctxt txt =
   1.502 + (ML_Context.use_mltext (ml txt) false (SOME (Context.Proof ctxt));
   1.503 +  (if ! source then str_of_source src else txt)
   1.504 +  |> (if ! quotes then quote else I)
   1.505 +  |> (if ! display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
   1.506 +  else
   1.507 +    split_lines
   1.508 +    #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
   1.509 +    #> space_implode "\\isasep\\isanewline%\n"));
   1.510 +
   1.511 +
   1.512 +(* commands *)
   1.513 +
   1.514 +val _ = add_commands
   1.515 + [("thm", args Attrib.thms (output_list pretty_thm)),
   1.516 +  ("thm_style", args (Scan.lift Args.liberal_name -- Attrib.thm) (output pretty_thm_style)),
   1.517 +  ("prop", args Args.prop (output pretty_term)),
   1.518 +  ("term", args Args.term (output pretty_term)),
   1.519 +  ("term_style", args (Scan.lift Args.liberal_name -- Args.term) (output pretty_term_style)),
   1.520 +  ("term_type", args Args.term (output pretty_term_typ)),
   1.521 +  ("typeof", args Args.term (output pretty_term_typeof)),
   1.522 +  ("const", args Args.term (output pretty_term_const)),
   1.523 +  ("abbrev", args Args.term_abbrev (output pretty_abbrev)),
   1.524 +  ("typ", args Args.typ_abbrev (output ProofContext.pretty_typ)),
   1.525 +  ("text", args (Scan.lift Args.name) (output (K pretty_text))),
   1.526 +  ("goals", output_goals true),
   1.527 +  ("subgoals", output_goals false),
   1.528 +  ("prf", args Attrib.thms (output (pretty_prf false))),
   1.529 +  ("full_prf", args Attrib.thms (output (pretty_prf true))),
   1.530 +  ("theory", args (Scan.lift Args.name) (output pretty_theory)),
   1.531 +  ("ML", args (Scan.lift Args.name) (output_ml ml_val)),
   1.532 +  ("ML_type", args (Scan.lift Args.name) (output_ml ml_type)),
   1.533 +  ("ML_struct", args (Scan.lift Args.name) (output_ml ml_struct))];
   1.534 +
   1.535 +end;