src/Pure/Isar/isar_cmd.ML
author ballarin
Thu Dec 11 18:30:26 2008 +0100 (2008-12-11)
changeset 29223 e09c53289830
parent 29006 abe0f11cfa4e
child 29230 155f6c110dfc
permissions -rw-r--r--
Conversion of HOL-Main and ZF to new locales.
     1 (*  Title:      Pure/Isar/isar_cmd.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Derived Isar commands.
     6 *)
     7 
     8 signature ISAR_CMD =
     9 sig
    10   val generic_setup: string * Position.T -> theory -> theory
    11   val parse_ast_translation: bool * (string * Position.T) -> theory -> theory
    12   val parse_translation: bool * (string * Position.T) -> theory -> theory
    13   val print_translation: bool * (string * Position.T) -> theory -> theory
    14   val typed_print_translation: bool * (string * Position.T) -> theory -> theory
    15   val print_ast_translation: bool * (string * Position.T) -> theory -> theory
    16   val oracle: bstring -> SymbolPos.text * Position.T -> theory -> theory
    17   val add_axioms: ((Binding.T * string) * Attrib.src list) list -> theory -> theory
    18   val add_defs: (bool * bool) * ((Binding.T * string) * Attrib.src list) list -> theory -> theory
    19   val declaration: string * Position.T -> local_theory -> local_theory
    20   val simproc_setup: string -> string list -> string * Position.T -> string list ->
    21     local_theory -> local_theory
    22   val hide_names: bool -> string * xstring list -> theory -> theory
    23   val have: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    24   val hence: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    25   val show: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    26   val thus: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    27   val qed: Method.text option -> Toplevel.transition -> Toplevel.transition
    28   val terminal_proof: Method.text * Method.text option ->
    29     Toplevel.transition -> Toplevel.transition
    30   val default_proof: Toplevel.transition -> Toplevel.transition
    31   val immediate_proof: Toplevel.transition -> Toplevel.transition
    32   val done_proof: Toplevel.transition -> Toplevel.transition
    33   val skip_proof: Toplevel.transition -> Toplevel.transition
    34   val init_theory: string * string list * (string * bool) list ->
    35     Toplevel.transition -> Toplevel.transition
    36   val welcome: Toplevel.transition -> Toplevel.transition
    37   val exit: Toplevel.transition -> Toplevel.transition
    38   val quit: Toplevel.transition -> Toplevel.transition
    39   val pr: string list * (int option * int option) -> Toplevel.transition -> Toplevel.transition
    40   val disable_pr: Toplevel.transition -> Toplevel.transition
    41   val enable_pr: Toplevel.transition -> Toplevel.transition
    42   val ml_diag: bool -> string * Position.T -> Toplevel.transition -> Toplevel.transition
    43   val cd: Path.T -> Toplevel.transition -> Toplevel.transition
    44   val pwd: Toplevel.transition -> Toplevel.transition
    45   val display_drafts: Path.T list -> Toplevel.transition -> Toplevel.transition
    46   val print_drafts: Path.T list -> Toplevel.transition -> Toplevel.transition
    47   val pretty_setmargin: int -> Toplevel.transition -> Toplevel.transition
    48   val print_context: Toplevel.transition -> Toplevel.transition
    49   val print_theory: bool -> Toplevel.transition -> Toplevel.transition
    50   val print_syntax: Toplevel.transition -> Toplevel.transition
    51   val print_abbrevs: Toplevel.transition -> Toplevel.transition
    52   val print_facts: Toplevel.transition -> Toplevel.transition
    53   val print_configs: Toplevel.transition -> Toplevel.transition
    54   val print_theorems: Toplevel.transition -> Toplevel.transition
    55   val print_locales: Toplevel.transition -> Toplevel.transition
    56   val print_locale: bool * xstring -> Toplevel.transition -> Toplevel.transition
    57   val print_registrations: bool -> string -> Toplevel.transition -> Toplevel.transition
    58   val print_attributes: Toplevel.transition -> Toplevel.transition
    59   val print_simpset: Toplevel.transition -> Toplevel.transition
    60   val print_rules: Toplevel.transition -> Toplevel.transition
    61   val print_trans_rules: Toplevel.transition -> Toplevel.transition
    62   val print_methods: Toplevel.transition -> Toplevel.transition
    63   val print_antiquotations: Toplevel.transition -> Toplevel.transition
    64   val class_deps: Toplevel.transition -> Toplevel.transition
    65   val thy_deps: Toplevel.transition -> Toplevel.transition
    66   val thm_deps: (Facts.ref * Attrib.src list) list -> Toplevel.transition -> Toplevel.transition
    67   val find_theorems: (int option * bool) * (bool * string FindTheorems.criterion) list
    68     -> Toplevel.transition -> Toplevel.transition
    69   val unused_thms: (string list * string list option) option ->
    70     Toplevel.transition -> Toplevel.transition
    71   val print_binds: Toplevel.transition -> Toplevel.transition
    72   val print_cases: Toplevel.transition -> Toplevel.transition
    73   val print_stmts: string list * (Facts.ref * Attrib.src list) list
    74     -> Toplevel.transition -> Toplevel.transition
    75   val print_thms: string list * (Facts.ref * Attrib.src list) list
    76     -> Toplevel.transition -> Toplevel.transition
    77   val print_prfs: bool -> string list * (Facts.ref * Attrib.src list) list option
    78     -> Toplevel.transition -> Toplevel.transition
    79   val print_prop: (string list * string) -> Toplevel.transition -> Toplevel.transition
    80   val print_term: (string list * string) -> Toplevel.transition -> Toplevel.transition
    81   val print_type: (string list * string) -> Toplevel.transition -> Toplevel.transition
    82   val header_markup: SymbolPos.text * Position.T -> Toplevel.transition -> Toplevel.transition
    83   val local_theory_markup: xstring option * (SymbolPos.text * Position.T) ->
    84     Toplevel.transition -> Toplevel.transition
    85   val proof_markup: SymbolPos.text * Position.T -> Toplevel.transition -> Toplevel.transition
    86 end;
    87 
    88 structure IsarCmd: ISAR_CMD =
    89 struct
    90 
    91 
    92 (** theory declarations **)
    93 
    94 (* generic_setup *)
    95 
    96 fun generic_setup (txt, pos) =
    97   ML_Context.expression pos "val setup: theory -> theory" "Context.map_theory setup" txt
    98   |> Context.theory_map;
    99 
   100 
   101 (* translation functions *)
   102 
   103 local
   104 
   105 fun advancedT false = ""
   106   | advancedT true = "Proof.context -> ";
   107 
   108 fun advancedN false = ""
   109   | advancedN true = "advanced_";
   110 
   111 in
   112 
   113 fun parse_ast_translation (a, (txt, pos)) =
   114   txt |> ML_Context.expression pos
   115     ("val parse_ast_translation: (string * (" ^ advancedT a ^
   116       "Syntax.ast list -> Syntax.ast)) list")
   117     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns (parse_ast_translation, [], [], []))")
   118   |> Context.theory_map;
   119 
   120 fun parse_translation (a, (txt, pos)) =
   121   txt |> ML_Context.expression pos
   122     ("val parse_translation: (string * (" ^ advancedT a ^
   123       "term list -> term)) list")
   124     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], parse_translation, [], []))")
   125   |> Context.theory_map;
   126 
   127 fun print_translation (a, (txt, pos)) =
   128   txt |> ML_Context.expression pos
   129     ("val print_translation: (string * (" ^ advancedT a ^
   130       "term list -> term)) list")
   131     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], [], print_translation, []))")
   132   |> Context.theory_map;
   133 
   134 fun print_ast_translation (a, (txt, pos)) =
   135   txt |> ML_Context.expression pos
   136     ("val print_ast_translation: (string * (" ^ advancedT a ^
   137       "Syntax.ast list -> Syntax.ast)) list")
   138     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], [], [], print_ast_translation))")
   139   |> Context.theory_map;
   140 
   141 fun typed_print_translation (a, (txt, pos)) =
   142   txt |> ML_Context.expression pos
   143     ("val typed_print_translation: (string * (" ^ advancedT a ^
   144       "bool -> typ -> term list -> term)) list")
   145     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfunsT typed_print_translation)")
   146   |> Context.theory_map;
   147 
   148 end;
   149 
   150 
   151 (* oracles *)
   152 
   153 fun oracle name (oracle_txt, pos) =
   154   let
   155     val oracle = SymbolPos.content (SymbolPos.explode (oracle_txt, pos));
   156     val txt =
   157       "local\n\
   158       \  val name = " ^ quote name ^ ";\n\
   159       \  val oracle = " ^ oracle ^ ";\n\
   160       \in\n\
   161       \  val " ^ name ^ " = snd (Context.>>> (Context.map_theory_result (Thm.add_oracle (name, oracle))));\n\
   162       \end;\n";
   163   in Context.theory_map (ML_Context.exec (fn () => ML_Context.eval false pos txt)) end;
   164 
   165 
   166 (* axioms *)
   167 
   168 fun add_axms f args thy =
   169   f (map (fn ((b, ax), srcs) => ((Binding.base_name b, ax), map (Attrib.attribute thy) srcs)) args) thy;
   170 
   171 val add_axioms = add_axms (snd oo PureThy.add_axioms_cmd);
   172 
   173 fun add_defs ((unchecked, overloaded), args) =
   174   add_axms
   175     (snd oo (if unchecked then PureThy.add_defs_unchecked_cmd else PureThy.add_defs_cmd) overloaded) args;
   176 
   177 
   178 (* declarations *)
   179 
   180 fun declaration (txt, pos) =
   181   txt |> ML_Context.expression pos
   182     "val declaration: Morphism.declaration"
   183     "Context.map_proof (LocalTheory.declaration declaration)"
   184   |> Context.proof_map;
   185 
   186 
   187 (* simprocs *)
   188 
   189 fun simproc_setup name lhss (proc, pos) identifier =
   190   ML_Context.expression pos
   191     "val proc: Morphism.morphism -> Simplifier.simpset -> cterm -> thm option"
   192   ("Context.map_proof (Simplifier.def_simproc {name = " ^ ML_Syntax.print_string name ^ ", \
   193     \lhss = " ^ ML_Syntax.print_strings lhss ^ ", proc = proc, \
   194     \identifier = Library.maps ML_Context.thms " ^ ML_Syntax.print_strings identifier ^ "})") proc
   195   |> Context.proof_map;
   196 
   197 
   198 (* hide names *)
   199 
   200 val hide_kinds =
   201  [("class", (Sign.intern_class, can o Sign.certify_class, Sign.hide_class)),
   202   ("type", (Sign.intern_type, Sign.declared_tyname, Sign.hide_type)),
   203   ("const", (Sign.intern_const, Sign.declared_const, Sign.hide_const)),
   204   ("fact", (PureThy.intern_fact, PureThy.defined_fact, PureThy.hide_fact))];
   205 
   206 fun hide_names b (kind, xnames) thy =
   207   (case AList.lookup (op =) hide_kinds kind of
   208     SOME (intern, check, hide) =>
   209       let
   210         val names = map (intern thy) xnames;
   211         val bads = filter_out (check thy) names;
   212       in
   213         if null bads then fold (hide b) names thy
   214         else error ("Attempt to hide undeclared item(s): " ^ commas_quote bads)
   215       end
   216   | NONE => error ("Bad name space specification: " ^ quote kind));
   217 
   218 
   219 (* goals *)
   220 
   221 fun goal opt_chain goal stmt int =
   222   opt_chain #> goal NONE (K Seq.single) stmt int;
   223 
   224 val have = goal I Proof.have;
   225 val hence = goal Proof.chain Proof.have;
   226 val show = goal I Proof.show;
   227 val thus = goal Proof.chain Proof.show;
   228 
   229 
   230 (* local endings *)
   231 
   232 fun local_qed m = Toplevel.proofs (Proof.local_qed (m, true));
   233 val local_terminal_proof = Toplevel.proofs o Proof.local_terminal_proof;
   234 val local_default_proof = Toplevel.proofs Proof.local_default_proof;
   235 val local_immediate_proof = Toplevel.proofs Proof.local_immediate_proof;
   236 val local_done_proof = Toplevel.proofs Proof.local_done_proof;
   237 val local_skip_proof = Toplevel.proofs' Proof.local_skip_proof;
   238 
   239 val skip_local_qed = Toplevel.skip_proof (fn i => if i > 1 then i - 1 else raise Toplevel.UNDEF);
   240 
   241 
   242 (* global endings *)
   243 
   244 fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
   245 val global_terminal_proof = Toplevel.end_proof o K o Proof.global_terminal_proof;
   246 val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
   247 val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
   248 val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
   249 val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
   250 
   251 val skip_global_qed = Toplevel.skip_proof_to_theory (fn n => n = 1);
   252 
   253 
   254 (* common endings *)
   255 
   256 fun qed m = local_qed m o global_qed m o skip_local_qed o skip_global_qed;
   257 fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
   258 val default_proof = local_default_proof o global_default_proof;
   259 val immediate_proof = local_immediate_proof o global_immediate_proof;
   260 val done_proof = local_done_proof o global_done_proof;
   261 val skip_proof = local_skip_proof o global_skip_proof;
   262 
   263 
   264 (* init and exit *)
   265 
   266 fun init_theory (name, imports, uses) =
   267   Toplevel.init_theory name (ThyInfo.begin_theory name imports (map (apfst Path.explode) uses))
   268     (fn thy =>
   269       if ThyInfo.check_known_thy (Context.theory_name thy)
   270       then ThyInfo.end_theory thy else ());
   271 
   272 val welcome = Toplevel.imperative (writeln o Session.welcome);
   273 
   274 val exit = Toplevel.keep (fn state =>
   275  (Context.set_thread_data (try Toplevel.generic_theory_of state);
   276   raise Toplevel.TERMINATE));
   277 
   278 val quit = Toplevel.imperative quit;
   279 
   280 
   281 (* print state *)
   282 
   283 fun set_limit _ NONE = ()
   284   | set_limit r (SOME n) = r := n;
   285 
   286 fun pr (modes, (lim1, lim2)) = Toplevel.keep (fn state =>
   287   (set_limit goals_limit lim1; set_limit ProofContext.prems_limit lim2; Toplevel.quiet := false;
   288     PrintMode.with_modes modes (Toplevel.print_state true) state));
   289 
   290 val disable_pr = Toplevel.imperative (fn () => Toplevel.quiet := true);
   291 val enable_pr = Toplevel.imperative (fn () => Toplevel.quiet := false);
   292 
   293 
   294 (* diagnostic ML evaluation *)
   295 
   296 fun ml_diag verbose (txt, pos) = Toplevel.keep (fn state =>
   297   (ML_Context.eval_in
   298     (Option.map Context.proof_of (try Toplevel.generic_theory_of state)) verbose pos txt));
   299 
   300 
   301 (* current working directory *)
   302 
   303 fun cd path = Toplevel.imperative (fn () => (File.cd path));
   304 val pwd = Toplevel.imperative (fn () => writeln (Path.implode (File.pwd ())));
   305 
   306 
   307 (* present draft files *)
   308 
   309 fun display_drafts files = Toplevel.imperative (fn () =>
   310   let val outfile = File.shell_path (Present.drafts (getenv "ISABELLE_DOC_FORMAT") files)
   311   in File.isabelle_tool ("display -c " ^ outfile ^ " &"); () end);
   312 
   313 fun print_drafts files = Toplevel.imperative (fn () =>
   314   let val outfile = File.shell_path (Present.drafts "ps" files)
   315   in File.isabelle_tool ("print -c " ^ outfile); () end);
   316 
   317 
   318 (* pretty_setmargin *)
   319 
   320 fun pretty_setmargin n = Toplevel.imperative (fn () => Pretty.setmargin n);
   321 
   322 
   323 (* print parts of theory and proof context *)
   324 
   325 val print_context = Toplevel.keep Toplevel.print_state_context;
   326 
   327 fun print_theory verbose = Toplevel.unknown_theory o
   328   Toplevel.keep (Pretty.writeln o ProofDisplay.pretty_full_theory verbose o Toplevel.theory_of);
   329 
   330 val print_syntax = Toplevel.unknown_context o
   331   Toplevel.keep (ProofContext.print_syntax o Toplevel.context_of);
   332 
   333 val print_abbrevs = Toplevel.unknown_context o
   334   Toplevel.keep (ProofContext.print_abbrevs o Toplevel.context_of);
   335 
   336 val print_facts = Toplevel.unknown_context o Toplevel.keep (fn state =>
   337   ProofContext.setmp_verbose
   338     ProofContext.print_lthms (Toplevel.context_of state));
   339 
   340 val print_configs = Toplevel.unknown_context o Toplevel.keep (fn state =>
   341   Attrib.print_configs (Toplevel.context_of state));
   342 
   343 val print_theorems_proof = Toplevel.keep (fn state =>
   344   ProofContext.setmp_verbose
   345     ProofContext.print_lthms (Proof.context_of (Toplevel.proof_of state)));
   346 
   347 val print_theorems_theory = Toplevel.keep (fn state =>
   348   Toplevel.theory_of state |>
   349   (case Option.map Toplevel.theory_node (Toplevel.previous_node_of state) of
   350     SOME (SOME prev_thy) => ProofDisplay.print_theorems_diff (Context.theory_of prev_thy)
   351   | _ => ProofDisplay.print_theorems));
   352 
   353 val print_theorems = Toplevel.unknown_context o print_theorems_theory o print_theorems_proof;
   354 
   355 val print_locales = Toplevel.unknown_theory o
   356   Toplevel.keep (NewLocale.print_locales o Toplevel.theory_of);
   357 
   358 fun print_locale (show_facts, name) = Toplevel.unknown_theory o
   359   Toplevel.keep (fn state =>
   360     NewLocale.print_locale (Toplevel.theory_of state) show_facts name);
   361 
   362 fun print_registrations show_wits name = Toplevel.unknown_context o
   363   Toplevel.keep (Toplevel.node_case
   364       (Context.cases (Locale.print_registrations show_wits name o ProofContext.init)
   365         (Locale.print_registrations show_wits name))
   366     (Locale.print_registrations show_wits name o Proof.context_of));
   367 
   368 val print_attributes = Toplevel.unknown_theory o
   369   Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
   370 
   371 val print_simpset = Toplevel.unknown_context o
   372   Toplevel.keep (Pretty.writeln o Simplifier.pretty_ss o Simplifier.local_simpset_of o Toplevel.context_of);
   373 
   374 val print_rules = Toplevel.unknown_context o
   375   Toplevel.keep (ContextRules.print_rules o Toplevel.context_of);
   376 
   377 val print_trans_rules = Toplevel.unknown_context o
   378   Toplevel.keep (Calculation.print_rules o Toplevel.context_of);
   379 
   380 val print_methods = Toplevel.unknown_theory o
   381   Toplevel.keep (Method.print_methods o Toplevel.theory_of);
   382 
   383 val print_antiquotations = Toplevel.imperative ThyOutput.print_antiquotations;
   384 
   385 val thy_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
   386   let
   387     val thy = Toplevel.theory_of state;
   388     val all_thys = sort ThyInfo.thy_ord (thy :: Theory.ancestors_of thy);
   389     val gr = all_thys |> map (fn node =>
   390       let
   391         val name = Context.theory_name node;
   392         val parents = map Context.theory_name (Theory.parents_of node);
   393         val dir = Present.session_name node;
   394         val unfold = not (ThyInfo.known_thy name andalso ThyInfo.is_finished name);
   395       in {name = name, ID = name, parents = parents, dir = dir, unfold = unfold, path = ""} end);
   396   in Present.display_graph gr end);
   397 
   398 val class_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
   399   let
   400     val thy = Toplevel.theory_of state;
   401     val {classes = (space, algebra), ...} = Type.rep_tsig (Sign.tsig_of thy);
   402     val {classes, ...} = Sorts.rep_algebra algebra;
   403     fun entry (c, (i, (_, cs))) =
   404       (i, {name = NameSpace.extern space c, ID = c, parents = cs,
   405             dir = "", unfold = true, path = ""});
   406     val gr =
   407       Graph.fold (cons o entry) classes []
   408       |> sort (int_ord o pairself #1) |> map #2;
   409   in Present.display_graph gr end);
   410 
   411 
   412 (* retrieve theorems *)
   413 
   414 fun thm_deps args = Toplevel.unknown_theory o Toplevel.keep (fn state =>
   415   ThmDeps.thm_deps (Proof.get_thmss (Toplevel.enter_proof_body state) args));
   416 
   417 fun find_theorems ((opt_lim, rem_dups), spec) =
   418   Toplevel.unknown_theory o Toplevel.keep (fn state =>
   419   let
   420     val proof_state = Toplevel.enter_proof_body state;
   421     val ctxt = Proof.context_of proof_state;
   422     val opt_goal = try Proof.get_goal proof_state |> Option.map (Thm.prop_of o #2 o #2);
   423   in FindTheorems.print_theorems ctxt opt_goal opt_lim rem_dups spec end);
   424 
   425 
   426 (* find unused theorems *)
   427 
   428 fun unused_thms opt_range = Toplevel.keep (fn state =>
   429   let
   430     val thy = Toplevel.theory_of state;
   431     val ctxt = Toplevel.context_of state;
   432     fun pretty_thm (a, th) = ProofContext.pretty_fact ctxt (a, [th]);
   433   in
   434     ThmDeps.unused_thms
   435       (case opt_range of
   436         NONE => (Theory.parents_of thy, [thy])
   437       | SOME (xs, NONE) => (map ThyInfo.get_theory xs, [thy])
   438       | SOME (xs, SOME ys) => (map ThyInfo.get_theory xs, map ThyInfo.get_theory ys))
   439     |> map pretty_thm |> Pretty.chunks |> Pretty.writeln
   440   end);
   441 
   442 
   443 (* print proof context contents *)
   444 
   445 val print_binds = Toplevel.unknown_context o Toplevel.keep (fn state =>
   446   ProofContext.setmp_verbose ProofContext.print_binds (Toplevel.context_of state));
   447 
   448 val print_cases = Toplevel.unknown_context o Toplevel.keep (fn state =>
   449   ProofContext.setmp_verbose ProofContext.print_cases (Toplevel.context_of state));
   450 
   451 
   452 (* print theorems, terms, types etc. *)
   453 
   454 local
   455 
   456 fun string_of_stmts state args =
   457   Proof.get_thmss state args
   458   |> map (Element.pretty_statement (Proof.context_of state) Thm.theoremK)
   459   |> Pretty.chunks2 |> Pretty.string_of;
   460 
   461 fun string_of_thms state args =
   462   Pretty.string_of (ProofContext.pretty_thms (Proof.context_of state)
   463     (Proof.get_thmss state args));
   464 
   465 fun string_of_prfs full state arg =
   466   Pretty.string_of (case arg of
   467       NONE =>
   468         let
   469           val (ctxt, (_, thm)) = Proof.get_goal state;
   470           val thy = ProofContext.theory_of ctxt;
   471           val prf = Thm.proof_of thm;
   472           val prop = Thm.full_prop_of thm;
   473           val prf' = Proofterm.rewrite_proof_notypes ([], []) prf
   474         in
   475           ProofSyntax.pretty_proof ctxt
   476             (if full then Reconstruct.reconstruct_proof thy prop prf' else prf')
   477         end
   478     | SOME args => Pretty.chunks
   479         (map (ProofSyntax.pretty_proof_of (Proof.context_of state) full)
   480           (Proof.get_thmss state args)));
   481 
   482 fun string_of_prop state s =
   483   let
   484     val ctxt = Proof.context_of state;
   485     val prop = Syntax.read_prop ctxt s;
   486     val ctxt' = Variable.auto_fixes prop ctxt;
   487   in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
   488 
   489 fun string_of_term state s =
   490   let
   491     val ctxt = Proof.context_of state;
   492     val t = Syntax.read_term ctxt s;
   493     val T = Term.type_of t;
   494     val ctxt' = Variable.auto_fixes t ctxt;
   495   in
   496     Pretty.string_of
   497       (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
   498         Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
   499   end;
   500 
   501 fun string_of_type state s =
   502   let
   503     val ctxt = Proof.context_of state;
   504     val T = Syntax.read_typ ctxt s;
   505   in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end;
   506 
   507 fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
   508   PrintMode.with_modes modes (fn () =>
   509     writeln (string_of (Toplevel.enter_proof_body state) arg)) ());
   510 
   511 in
   512 
   513 val print_stmts = print_item string_of_stmts;
   514 val print_thms = print_item string_of_thms;
   515 val print_prfs = print_item o string_of_prfs;
   516 val print_prop = print_item string_of_prop;
   517 val print_term = print_item string_of_term;
   518 val print_type = print_item string_of_type;
   519 
   520 end;
   521 
   522 
   523 (* markup commands *)
   524 
   525 fun check_text (txt, pos) opt_node =
   526  (Position.report Markup.doc_source pos;
   527   ignore (ThyOutput.eval_antiquote (#1 (OuterKeyword.get_lexicons ())) opt_node (txt, pos)));
   528 
   529 fun header_markup txt = Toplevel.keep (fn state =>
   530   if Toplevel.is_toplevel state then check_text txt NONE
   531   else raise Toplevel.UNDEF);
   532 
   533 fun local_theory_markup (loc, txt) = Toplevel.present_local_theory loc (check_text txt o SOME);
   534 fun proof_markup txt = Toplevel.present_proof (check_text txt o SOME);
   535 
   536 end;