src/Pure/Isar/isar_cmd.ML
author wenzelm
Fri, 28 Mar 2008 00:02:54 +0100
changeset 26455 1757a6e049f4
parent 26435 bdce320cd426
child 26463 9283b4185fdf
permissions -rw-r--r--
reorganized signature of ML_Context;
     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 token_translation: string * Position.T -> theory -> theory
    17   val oracle: bstring -> string -> string * Position.T -> theory -> theory
    18   val add_axioms: ((bstring * string) * Attrib.src list) list -> theory -> theory
    19   val add_defs: (bool * bool) * ((bstring * string) * Attrib.src list) list -> theory -> theory
    20   val apply_theorems: (Facts.ref * Attrib.src list) list -> theory -> thm list * theory
    21   val apply_theorems_i: (thm list * attribute list) list -> theory -> thm list * theory
    22   val declaration: string * Position.T -> local_theory -> local_theory
    23   val simproc_setup: string -> string list -> string * Position.T -> string list ->
    24     local_theory -> local_theory
    25   val have: ((string * Attrib.src list) * (string * string list) list) list ->
    26     bool -> Proof.state -> Proof.state
    27   val hence: ((string * Attrib.src list) * (string * string list) list) list ->
    28     bool -> Proof.state -> Proof.state
    29   val show: ((string * Attrib.src list) * (string * string list) list) list ->
    30     bool -> Proof.state -> Proof.state
    31   val thus: ((string * Attrib.src list) * (string * string list) list) list ->
    32     bool -> Proof.state -> Proof.state
    33   val qed: Method.text option -> Toplevel.transition -> Toplevel.transition
    34   val terminal_proof: Method.text * Method.text option ->
    35     Toplevel.transition -> Toplevel.transition
    36   val default_proof: Toplevel.transition -> Toplevel.transition
    37   val immediate_proof: Toplevel.transition -> Toplevel.transition
    38   val done_proof: Toplevel.transition -> Toplevel.transition
    39   val skip_proof: Toplevel.transition -> Toplevel.transition
    40   val begin_theory: string -> string list -> (string * bool) list -> bool -> theory
    41   val end_theory: theory -> theory
    42   val kill_theory: string -> unit
    43   val theory: string * string list * (string * bool) list
    44     -> Toplevel.transition -> Toplevel.transition
    45   val welcome: Toplevel.transition -> Toplevel.transition
    46   val init_toplevel: Toplevel.transition -> Toplevel.transition
    47   val exit: Toplevel.transition -> Toplevel.transition
    48   val quit: Toplevel.transition -> Toplevel.transition
    49   val touch_child_thys: string -> Toplevel.transition -> Toplevel.transition
    50   val touch_thy: string -> Toplevel.transition -> Toplevel.transition
    51   val remove_thy: string -> Toplevel.transition -> Toplevel.transition
    52   val kill_thy: string -> Toplevel.transition -> Toplevel.transition
    53   val pr: string list * (int option * int option) -> Toplevel.transition -> Toplevel.transition
    54   val disable_pr: Toplevel.transition -> Toplevel.transition
    55   val enable_pr: Toplevel.transition -> Toplevel.transition
    56   val redo: Toplevel.transition -> Toplevel.transition
    57   val undos_proof: int -> Toplevel.transition -> Toplevel.transition
    58   val kill_proof_notify: (unit -> unit) -> Toplevel.transition -> Toplevel.transition
    59   val kill_proof: Toplevel.transition -> Toplevel.transition
    60   val undo_theory: Toplevel.transition -> Toplevel.transition
    61   val undo: Toplevel.transition -> Toplevel.transition
    62   val cannot_undo: string -> Toplevel.transition -> Toplevel.transition
    63   val kill: Toplevel.transition -> Toplevel.transition
    64   val back: Toplevel.transition -> Toplevel.transition
    65   val use: Path.T -> Toplevel.transition -> Toplevel.transition
    66   val use_mltext_theory: string * Position.T -> Toplevel.transition -> Toplevel.transition
    67   val use_mltext: bool -> string * Position.T -> Toplevel.transition -> Toplevel.transition
    68   val use_commit: Toplevel.transition -> Toplevel.transition
    69   val nested_command: Markup.property list -> string * Position.T -> Toplevel.transition
    70   val cd: Path.T -> Toplevel.transition -> Toplevel.transition
    71   val pwd: Toplevel.transition -> Toplevel.transition
    72   val display_drafts: Path.T list -> Toplevel.transition -> Toplevel.transition
    73   val print_drafts: Path.T list -> Toplevel.transition -> Toplevel.transition
    74   val pretty_setmargin: int -> Toplevel.transition -> Toplevel.transition
    75   val print_context: Toplevel.transition -> Toplevel.transition
    76   val print_theory: bool -> Toplevel.transition -> Toplevel.transition
    77   val print_syntax: Toplevel.transition -> Toplevel.transition
    78   val print_abbrevs: Toplevel.transition -> Toplevel.transition
    79   val print_facts: Toplevel.transition -> Toplevel.transition
    80   val print_configs: Toplevel.transition -> Toplevel.transition
    81   val print_theorems: Toplevel.transition -> Toplevel.transition
    82   val print_locales: Toplevel.transition -> Toplevel.transition
    83   val print_locale: bool * (Locale.expr * Element.context list)
    84     -> Toplevel.transition -> Toplevel.transition
    85   val print_registrations: bool -> string -> Toplevel.transition -> Toplevel.transition
    86   val print_attributes: Toplevel.transition -> Toplevel.transition
    87   val print_simpset: Toplevel.transition -> Toplevel.transition
    88   val print_rules: Toplevel.transition -> Toplevel.transition
    89   val print_trans_rules: Toplevel.transition -> Toplevel.transition
    90   val print_methods: Toplevel.transition -> Toplevel.transition
    91   val print_antiquotations: Toplevel.transition -> Toplevel.transition
    92   val class_deps: Toplevel.transition -> Toplevel.transition
    93   val thy_deps: Toplevel.transition -> Toplevel.transition
    94   val thm_deps: (Facts.ref * Attrib.src list) list -> Toplevel.transition -> Toplevel.transition
    95   val find_theorems: (int option * bool) * (bool * string FindTheorems.criterion) list
    96     -> Toplevel.transition -> Toplevel.transition
    97   val unused_thms: (string list * string list option) option ->
    98     Toplevel.transition -> Toplevel.transition
    99   val print_binds: Toplevel.transition -> Toplevel.transition
   100   val print_cases: Toplevel.transition -> Toplevel.transition
   101   val print_stmts: string list * (Facts.ref * Attrib.src list) list
   102     -> Toplevel.transition -> Toplevel.transition
   103   val print_thms: string list * (Facts.ref * Attrib.src list) list
   104     -> Toplevel.transition -> Toplevel.transition
   105   val print_prfs: bool -> string list * (Facts.ref * Attrib.src list) list option
   106     -> Toplevel.transition -> Toplevel.transition
   107   val print_prop: (string list * string) -> Toplevel.transition -> Toplevel.transition
   108   val print_term: (string list * string) -> Toplevel.transition -> Toplevel.transition
   109   val print_type: (string list * string) -> Toplevel.transition -> Toplevel.transition
   110   val add_header: string * Position.T -> Toplevel.transition -> Toplevel.transition
   111   val add_chapter: xstring option * (string * Position.T) ->
   112     Toplevel.transition -> Toplevel.transition
   113   val add_section: xstring option * (string * Position.T) ->
   114     Toplevel.transition -> Toplevel.transition
   115   val add_subsection: xstring option * (string * Position.T) ->
   116     Toplevel.transition -> Toplevel.transition
   117   val add_subsubsection: xstring option * (string * Position.T) ->
   118     Toplevel.transition -> Toplevel.transition
   119   val add_text: xstring option * (string * Position.T) ->
   120     Toplevel.transition -> Toplevel.transition
   121   val add_text_raw: string * Position.T -> Toplevel.transition -> Toplevel.transition
   122   val add_sect: string * Position.T -> Toplevel.transition -> Toplevel.transition
   123   val add_subsect: string * Position.T -> Toplevel.transition -> Toplevel.transition
   124   val add_subsubsect: string * Position.T -> Toplevel.transition -> Toplevel.transition
   125   val add_txt: string * Position.T -> Toplevel.transition -> Toplevel.transition
   126   val add_txt_raw: string * Position.T -> Toplevel.transition -> Toplevel.transition
   127 end;
   128 
   129 structure IsarCmd: ISAR_CMD =
   130 struct
   131 
   132 
   133 (** theory declarations **)
   134 
   135 (* generic_setup *)
   136 
   137 fun generic_setup (txt, pos) =
   138   ML_Context.expression pos "val setup: theory -> theory" "Context.map_theory setup" txt
   139   |> Context.theory_map;
   140 
   141 
   142 (* translation functions *)
   143 
   144 local
   145 
   146 fun advancedT false = ""
   147   | advancedT true = "Proof.context -> ";
   148 
   149 fun advancedN false = ""
   150   | advancedN true = "advanced_";
   151 
   152 in
   153 
   154 fun parse_ast_translation (a, (txt, pos)) =
   155   txt |> ML_Context.expression pos
   156     ("val parse_ast_translation: (string * (" ^ advancedT a ^
   157       "Syntax.ast list -> Syntax.ast)) list")
   158     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns (parse_ast_translation, [], [], []))")
   159   |> Context.theory_map;
   160 
   161 fun parse_translation (a, (txt, pos)) =
   162   txt |> ML_Context.expression pos
   163     ("val parse_translation: (string * (" ^ advancedT a ^
   164       "term list -> term)) list")
   165     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], parse_translation, [], []))")
   166   |> Context.theory_map;
   167 
   168 fun print_translation (a, (txt, pos)) =
   169   txt |> ML_Context.expression pos
   170     ("val print_translation: (string * (" ^ advancedT a ^
   171       "term list -> term)) list")
   172     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], [], print_translation, []))")
   173   |> Context.theory_map;
   174 
   175 fun print_ast_translation (a, (txt, pos)) =
   176   txt |> ML_Context.expression pos
   177     ("val print_ast_translation: (string * (" ^ advancedT a ^
   178       "Syntax.ast list -> Syntax.ast)) list")
   179     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], [], [], print_ast_translation))")
   180   |> Context.theory_map;
   181 
   182 fun typed_print_translation (a, (txt, pos)) =
   183   txt |> ML_Context.expression pos
   184     ("val typed_print_translation: (string * (" ^ advancedT a ^
   185       "bool -> typ -> term list -> term)) list")
   186     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfunsT typed_print_translation)")
   187   |> Context.theory_map;
   188 
   189 fun token_translation (txt, pos) =
   190   txt |> ML_Context.expression pos
   191     "val token_translation: (string * string * (string -> output * int)) list"
   192       "Context.map_theory (Sign.add_tokentrfuns token_translation)"
   193   |> Context.theory_map;
   194 
   195 end;
   196 
   197 
   198 (* oracles *)
   199 
   200 fun oracle name typ (oracle, pos) =
   201   let val txt =
   202     "local\
   203     \  type T = " ^ typ ^ ";\
   204     \  val oracle: theory -> T -> term = " ^ oracle ^ ";\n\
   205     \  val name = " ^ quote name ^ ";\n\
   206     \  exception Arg of T;\n\
   207     \  val _ = Context.>> (Theory.add_oracle (name, fn (thy, Arg x) => oracle thy x));\n\
   208     \  val thy = ML_Context.the_global_context ();\n\
   209     \  val invoke_" ^ name ^ " = Thm.invoke_oracle_i thy (Sign.full_name thy name);\n\
   210     \in\n\
   211     \  fun " ^ name ^ " thy x = invoke_" ^ name ^ " (thy, Arg x);\n\
   212     \end;\n";
   213   in Context.theory_map (ML_Context.exec (fn () => ML_Context.eval false pos txt)) end;
   214 
   215 
   216 (* axioms *)
   217 
   218 fun add_axms f args thy =
   219   f (map (fn (x, srcs) => (x, map (Attrib.attribute thy) srcs)) args) thy;
   220 
   221 val add_axioms = add_axms (snd oo PureThy.add_axioms);
   222 
   223 fun add_defs ((unchecked, overloaded), args) =
   224   add_axms
   225     (snd oo (if unchecked then PureThy.add_defs_unchecked else PureThy.add_defs) overloaded) args;
   226 
   227 
   228 (* facts *)
   229 
   230 fun apply_theorems args thy =
   231   let val facts = Attrib.map_facts (Attrib.attribute thy) [(("", []), args)]
   232   in apfst (maps snd) (PureThy.note_thmss "" facts thy) end;
   233 
   234 fun apply_theorems_i args = apfst (maps snd) o PureThy.note_thmss_i "" [(("", []), args)];
   235 
   236 
   237 (* declarations *)
   238 
   239 fun declaration (txt, pos) =
   240   txt |> ML_Context.expression pos
   241     "val declaration: Morphism.declaration"
   242     "Context.map_proof (LocalTheory.declaration declaration)"
   243   |> Context.proof_map;
   244 
   245 
   246 (* simprocs *)
   247 
   248 fun simproc_setup name lhss (proc, pos) identifier =
   249   ML_Context.expression pos
   250     "val proc: Morphism.morphism -> Simplifier.simpset -> cterm -> thm option"
   251   ("Context.map_proof (Simplifier.def_simproc {name = " ^ ML_Syntax.print_string name ^ ", \
   252     \lhss = " ^ ML_Syntax.print_strings lhss ^ ", proc = proc, \
   253     \identifier = Library.maps ML_Context.thms " ^ ML_Syntax.print_strings identifier ^ "})") proc
   254   |> Context.proof_map;
   255 
   256 
   257 (* goals *)
   258 
   259 fun goal opt_chain goal stmt int =
   260   opt_chain #> goal NONE (K Seq.single) stmt int;
   261 
   262 val have = goal I Proof.have;
   263 val hence = goal Proof.chain Proof.have;
   264 val show = goal I Proof.show;
   265 val thus = goal Proof.chain Proof.show;
   266 
   267 
   268 (* local endings *)
   269 
   270 fun local_qed m = Toplevel.proofs (Proof.local_qed (m, true));
   271 val local_terminal_proof = Toplevel.proofs o Proof.local_terminal_proof;
   272 val local_default_proof = Toplevel.proofs Proof.local_default_proof;
   273 val local_immediate_proof = Toplevel.proofs Proof.local_immediate_proof;
   274 val local_done_proof = Toplevel.proofs Proof.local_done_proof;
   275 val local_skip_proof = Toplevel.proofs' Proof.local_skip_proof;
   276 
   277 val skip_local_qed =
   278   Toplevel.skip_proof (History.apply (fn i => if i > 1 then i - 1 else raise Toplevel.UNDEF));
   279 
   280 
   281 (* global endings *)
   282 
   283 fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
   284 val global_terminal_proof = Toplevel.end_proof o K o Proof.global_terminal_proof;
   285 val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
   286 val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
   287 val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
   288 val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
   289 
   290 val skip_global_qed = Toplevel.skip_proof_to_theory (equal 1);
   291 
   292 
   293 (* common endings *)
   294 
   295 fun qed m = local_qed m o global_qed m o skip_local_qed o skip_global_qed;
   296 fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
   297 val default_proof = local_default_proof o global_default_proof;
   298 val immediate_proof = local_immediate_proof o global_immediate_proof;
   299 val done_proof = local_done_proof o global_done_proof;
   300 val skip_proof = local_skip_proof o global_skip_proof;
   301 
   302 
   303 (* init and exit *)
   304 
   305 fun begin_theory name imports uses =
   306   ThyInfo.begin_theory name imports (map (apfst Path.explode) uses);
   307 
   308 fun end_theory thy =
   309   if ThyInfo.check_known_thy (Context.theory_name thy) then ThyInfo.end_theory thy else thy;
   310 
   311 val kill_theory = ThyInfo.if_known_thy ThyInfo.remove_thy;
   312 
   313 fun theory (name, imports, uses) =
   314   Toplevel.init_theory (begin_theory name imports uses)
   315     (fn thy => (end_theory thy; ()))
   316     (kill_theory o Context.theory_name);
   317 
   318 val init_toplevel = Toplevel.imperative (fn () => raise Toplevel.RESTART);
   319 
   320 val welcome = Toplevel.imperative (writeln o Session.welcome);
   321 
   322 val exit = Toplevel.keep (fn state =>
   323  (CRITICAL (fn () => Context.set_thread_data (try Toplevel.generic_theory_of state));
   324   raise Toplevel.TERMINATE));
   325 
   326 val quit = Toplevel.imperative quit;
   327 
   328 
   329 (* touch theories *)
   330 
   331 fun touch_child_thys name = Toplevel.imperative (fn () => ThyInfo.touch_child_thys name);
   332 fun touch_thy name = Toplevel.imperative (fn () => ThyInfo.touch_thy name);
   333 fun remove_thy name = Toplevel.imperative (fn () => ThyInfo.remove_thy name);
   334 fun kill_thy name = Toplevel.imperative (fn () => kill_theory name);
   335 
   336 
   337 (* print state *)
   338 
   339 fun set_limit _ NONE = ()
   340   | set_limit r (SOME n) = r := n;
   341 
   342 fun pr (modes, (lim1, lim2)) = Toplevel.keep (fn state =>
   343   (set_limit goals_limit lim1; set_limit ProofContext.prems_limit lim2; Toplevel.quiet := false;
   344     PrintMode.with_modes modes (Toplevel.print_state true) state));
   345 
   346 val disable_pr = Toplevel.imperative (fn () => Toplevel.quiet := true);
   347 val enable_pr = Toplevel.imperative (fn () => Toplevel.quiet := false);
   348 
   349 
   350 (* history commands *)
   351 
   352 val redo =
   353   Toplevel.history History.redo o
   354   Toplevel.actual_proof ProofHistory.redo o
   355   Toplevel.skip_proof History.redo;
   356 
   357 fun undos_proof n =
   358   Toplevel.actual_proof (fn prf =>
   359     if ProofHistory.is_initial prf then raise Toplevel.UNDEF else funpow n ProofHistory.undo prf) o
   360   Toplevel.skip_proof (fn h =>
   361     if History.is_initial h then raise Toplevel.UNDEF else funpow n History.undo h);
   362 
   363 fun kill_proof_notify (f: unit -> unit) = Toplevel.history (fn hist =>
   364   if is_some (Toplevel.theory_node (History.current hist)) then raise Toplevel.UNDEF
   365   else (f (); History.undo hist));
   366 
   367 val kill_proof = kill_proof_notify (K ());
   368 
   369 val undo_theory = Toplevel.history (fn hist =>
   370   if History.is_initial hist then raise Toplevel.UNDEF else History.undo hist);
   371 
   372 val undo = Toplevel.kill o undo_theory o Toplevel.undo_exit o undos_proof 1;
   373 
   374 fun cannot_undo "end" = undo   (*ProofGeneral legacy*)
   375   | cannot_undo txt = Toplevel.imperative (fn () => error ("Cannot undo " ^ quote txt));
   376 
   377 val kill = Toplevel.kill o kill_proof;
   378 
   379 val back =
   380   Toplevel.actual_proof ProofHistory.back o
   381   Toplevel.skip_proof (History.apply I);
   382 
   383 
   384 (* use ML text *)
   385 
   386 fun use path = Toplevel.keep (fn state =>
   387   Context.setmp_thread_data (try Toplevel.generic_theory_of state) (ThyInfo.load_file false) path);
   388 
   389 (*passes changes of theory context*)
   390 fun use_mltext_theory (txt, pos) = Toplevel.theory' (fn int =>
   391   Context.theory_map (ML_Context.exec (fn () => ML_Context.eval int pos txt)));
   392 
   393 (*ignore result theory context*)
   394 fun use_mltext verbose (txt, pos) = Toplevel.keep' (fn int => fn state =>
   395   (ML_Context.eval_in (try Toplevel.generic_theory_of state) (verbose andalso int) pos txt));
   396 
   397 val use_commit = Toplevel.imperative Secure.commit;
   398 
   399 
   400 (* nested commands *)
   401 
   402 fun nested_command props (str, pos) =
   403   let val pos' = Position.of_properties (props |> Position.default_properties pos) in
   404     (case OuterSyntax.parse pos' str of
   405       [transition] => transition
   406     | _ => error "exactly one command expected")
   407   end;
   408 
   409 
   410 (* current working directory *)
   411 
   412 fun cd path = Toplevel.imperative (fn () => (File.cd path));
   413 val pwd = Toplevel.imperative (fn () => writeln (Path.implode (File.pwd ())));
   414 
   415 
   416 (* present draft files *)
   417 
   418 fun display_drafts files = Toplevel.imperative (fn () =>
   419   let val outfile = File.shell_path (Present.drafts (getenv "ISABELLE_DOC_FORMAT") files)
   420   in File.isatool ("display -c " ^ outfile ^ " &"); () end);
   421 
   422 fun print_drafts files = Toplevel.imperative (fn () =>
   423   let val outfile = File.shell_path (Present.drafts "ps" files)
   424   in File.isatool ("print -c " ^ outfile); () end);
   425 
   426 
   427 (* pretty_setmargin *)
   428 
   429 fun pretty_setmargin n = Toplevel.imperative (fn () => Pretty.setmargin n);
   430 
   431 
   432 (* print parts of theory and proof context *)
   433 
   434 val print_context = Toplevel.keep Toplevel.print_state_context;
   435 
   436 fun print_theory verbose = Toplevel.unknown_theory o
   437   Toplevel.keep (Pretty.writeln o ProofDisplay.pretty_full_theory verbose o Toplevel.theory_of);
   438 
   439 val print_syntax = Toplevel.unknown_context o
   440   Toplevel.keep (ProofContext.print_syntax o Toplevel.context_of);
   441 
   442 val print_abbrevs = Toplevel.unknown_context o
   443   Toplevel.keep (ProofContext.print_abbrevs o Toplevel.context_of);
   444 
   445 val print_facts = Toplevel.unknown_context o Toplevel.keep (fn state =>
   446   ProofContext.setmp_verbose
   447     ProofContext.print_lthms (Toplevel.context_of state));
   448 
   449 val print_configs = Toplevel.unknown_context o Toplevel.keep (fn state =>
   450   Attrib.print_configs (Toplevel.context_of state));
   451 
   452 val print_theorems_proof = Toplevel.keep (fn state =>
   453   ProofContext.setmp_verbose
   454     ProofContext.print_lthms (Proof.context_of (Toplevel.proof_of state)));
   455 
   456 val print_theorems_theory = Toplevel.keep (fn state =>
   457   Toplevel.theory_of state |>
   458   (case Option.map Toplevel.theory_node (History.previous (Toplevel.node_history_of state)) of
   459     SOME (SOME prev_thy) => ProofDisplay.print_theorems_diff (Context.theory_of prev_thy)
   460   | _ => ProofDisplay.print_theorems));
   461 
   462 val print_theorems = Toplevel.unknown_context o print_theorems_theory o print_theorems_proof;
   463 
   464 val print_locales = Toplevel.unknown_theory o
   465   Toplevel.keep (Locale.print_locales o Toplevel.theory_of);
   466 
   467 fun print_locale (show_facts, (imports, body)) = Toplevel.unknown_theory o
   468   Toplevel.keep (fn state =>
   469     Locale.print_locale (Toplevel.theory_of state) show_facts imports body);
   470 
   471 fun print_registrations show_wits name = Toplevel.unknown_context o
   472   Toplevel.keep (Toplevel.node_case
   473       (Context.cases (Locale.print_registrations show_wits name o ProofContext.init)
   474         (Locale.print_registrations show_wits name))
   475     (Locale.print_registrations show_wits name o Proof.context_of));
   476 
   477 val print_attributes = Toplevel.unknown_theory o
   478   Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
   479 
   480 val print_simpset = Toplevel.unknown_context o
   481   Toplevel.keep (Toplevel.node_case
   482     (Context.cases Simplifier.print_simpset Simplifier.print_local_simpset)
   483     (Simplifier.print_local_simpset o Proof.context_of));
   484 
   485 val print_rules = Toplevel.unknown_context o
   486   Toplevel.keep (ContextRules.print_rules o Toplevel.context_of);
   487 
   488 val print_trans_rules = Toplevel.unknown_context o
   489   Toplevel.keep (Calculation.print_rules o Toplevel.context_of);
   490 
   491 val print_methods = Toplevel.unknown_theory o
   492   Toplevel.keep (Method.print_methods o Toplevel.theory_of);
   493 
   494 val print_antiquotations = Toplevel.imperative ThyOutput.print_antiquotations;
   495 
   496 val thy_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
   497   let
   498     val thy = Toplevel.theory_of state;
   499     val all_thys = sort ThyInfo.thy_ord (thy :: Theory.ancestors_of thy);
   500     val gr = all_thys |> map (fn node =>
   501       let
   502         val name = Context.theory_name node;
   503         val parents = map Context.theory_name (Theory.parents_of node);
   504         val dir = Present.session_name node;
   505         val unfold = not (ThyInfo.known_thy name andalso ThyInfo.is_finished name);
   506       in {name = name, ID = name, parents = parents, dir = dir, unfold = unfold, path = ""} end);
   507   in Present.display_graph gr end);
   508 
   509 val class_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
   510   let
   511     val thy = Toplevel.theory_of state;
   512     val {classes = (space, algebra), ...} = Type.rep_tsig (Sign.tsig_of thy);
   513     val {classes, ...} = Sorts.rep_algebra algebra;
   514     fun entry (c, (i, (_, cs))) =
   515       (i, {name = NameSpace.extern space c, ID = c, parents = cs,
   516             dir = "", unfold = true, path = ""});
   517     val gr =
   518       Graph.fold (cons o entry) classes []
   519       |> sort (int_ord o pairself #1) |> map #2;
   520   in Present.display_graph gr end);
   521 
   522 
   523 (* retrieve theorems *)
   524 
   525 fun thm_deps args = Toplevel.unknown_theory o Toplevel.keep (fn state =>
   526   ThmDeps.thm_deps (Proof.get_thmss (Toplevel.enter_proof_body state) args));
   527 
   528 fun find_theorems ((opt_lim, rem_dups), spec) =
   529   Toplevel.unknown_theory o Toplevel.keep (fn state =>
   530   let
   531     val proof_state = Toplevel.enter_proof_body state;
   532     val ctxt = Proof.context_of proof_state;
   533     val opt_goal = try Proof.get_goal proof_state |> Option.map (Thm.prop_of o #2 o #2);
   534   in FindTheorems.print_theorems ctxt opt_goal opt_lim rem_dups spec end);
   535 
   536 
   537 (* find unused theorems *)
   538 
   539 fun unused_thms opt_range = Toplevel.keep (fn state =>
   540   let
   541     val thy = Toplevel.theory_of state;
   542     val ctxt = Toplevel.context_of state;
   543     fun pretty_thm (a, th) = ProofContext.pretty_fact ctxt (a, [th]);
   544   in
   545     ThmDeps.unused_thms
   546       (case opt_range of
   547          NONE => (NONE, [thy])
   548        | SOME (xs, NONE) => (SOME (map ThyInfo.get_theory xs), [thy])
   549        | SOME (xs, SOME ys) => (SOME (map ThyInfo.get_theory xs), map ThyInfo.get_theory ys))
   550     |> map pretty_thm |> Pretty.chunks |> Pretty.writeln
   551   end);
   552 
   553 
   554 (* print proof context contents *)
   555 
   556 val print_binds = Toplevel.unknown_context o Toplevel.keep (fn state =>
   557   ProofContext.setmp_verbose ProofContext.print_binds (Toplevel.context_of state));
   558 
   559 val print_cases = Toplevel.unknown_context o Toplevel.keep (fn state =>
   560   ProofContext.setmp_verbose ProofContext.print_cases (Toplevel.context_of state));
   561 
   562 
   563 (* print theorems, terms, types etc. *)
   564 
   565 local
   566 
   567 fun string_of_stmts state args =
   568   Proof.get_thmss state args
   569   |> map (Element.pretty_statement (Proof.context_of state) Thm.theoremK)
   570   |> Pretty.chunks2 |> Pretty.string_of;
   571 
   572 fun string_of_thms state args =
   573   Pretty.string_of (ProofContext.pretty_thms (Proof.context_of state)
   574     (Proof.get_thmss state args));
   575 
   576 fun string_of_prfs full state arg =
   577   Pretty.string_of (case arg of
   578       NONE =>
   579         let
   580           val (ctxt, (_, thm)) = Proof.get_goal state;
   581           val {thy, der = (_, prf), ...} = Thm.rep_thm thm;
   582           val prop = Thm.full_prop_of thm;
   583           val prf' = Proofterm.rewrite_proof_notypes ([], []) prf
   584         in
   585           ProofContext.pretty_proof ctxt
   586             (if full then Reconstruct.reconstruct_proof thy prop prf' else prf')
   587         end
   588     | SOME args => Pretty.chunks
   589         (map (ProofContext.pretty_proof_of (Proof.context_of state) full)
   590           (Proof.get_thmss state args)));
   591 
   592 fun string_of_prop state s =
   593   let
   594     val ctxt = Proof.context_of state;
   595     val prop = Syntax.read_prop ctxt s;
   596   in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt prop)) end;
   597 
   598 fun string_of_term state s =
   599   let
   600     val ctxt = Proof.context_of state;
   601     val t = Syntax.read_term ctxt s;
   602     val T = Term.type_of t;
   603   in
   604     Pretty.string_of
   605       (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt t), Pretty.fbrk,
   606         Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt T)])
   607   end;
   608 
   609 fun string_of_type state s =
   610   let
   611     val ctxt = Proof.context_of state;
   612     val T = Syntax.read_typ ctxt s;
   613   in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end;
   614 
   615 fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
   616   PrintMode.with_modes modes (fn () =>
   617     writeln (string_of (Toplevel.enter_proof_body state) arg)) ());
   618 
   619 in
   620 
   621 val print_stmts = print_item string_of_stmts;
   622 val print_thms = print_item string_of_thms;
   623 val print_prfs = print_item o string_of_prfs;
   624 val print_prop = print_item string_of_prop;
   625 val print_term = print_item string_of_term;
   626 val print_type = print_item string_of_type;
   627 
   628 end;
   629 
   630 
   631 (* markup commands *)
   632 
   633 fun add_header s = Toplevel.keep' (fn int => fn state =>
   634   (if Toplevel.is_toplevel state then () else raise Toplevel.UNDEF;
   635    if int then OuterSyntax.check_text s NONE else ()));
   636 
   637 local
   638 
   639 fun present _ txt true node = OuterSyntax.check_text txt (SOME node)
   640   | present f (s, _) false node = Context.setmp_thread_data
   641       (try (Toplevel.cases_node I (Context.Proof o Proof.context_of)) node) f s;
   642 
   643 fun present_local_theory f (loc, txt) = Toplevel.present_local_theory loc (present f txt);
   644 fun present_proof f txt = Toplevel.print o Toplevel.present_proof (present f txt);
   645 
   646 in
   647 
   648 val add_chapter       = present_local_theory Present.section;
   649 val add_section       = present_local_theory Present.section;
   650 val add_subsection    = present_local_theory Present.subsection;
   651 val add_subsubsection = present_local_theory Present.subsubsection;
   652 val add_text          = present_local_theory (K ());
   653 fun add_text_raw txt  = present_local_theory (K ()) (NONE, txt);
   654 val add_txt           = present_proof (K ());
   655 val add_txt_raw       = add_txt;
   656 val add_sect          = add_txt;
   657 val add_subsect       = add_txt;
   658 val add_subsubsect    = add_txt;
   659 
   660 end;
   661 
   662 end;