src/Pure/Isar/isar_syn.ML
author wenzelm
Mon Nov 14 16:52:19 2011 +0100 (2011-11-14 ago)
changeset 45488 6d71d9e52369
parent 45134 9b02f6665fc8
child 45600 1bbbac9a0cb0
permissions -rw-r--r--
pass positions for named targets, for formal links in the document model;
     1 (*  Title:      Pure/Isar/isar_syn.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Isar/Pure outer syntax.
     5 *)
     6 
     7 structure Isar_Syn: sig end =
     8 struct
     9 
    10 (** keywords **)
    11 
    12 (*keep keywords consistent with the parsers, otherwise be prepared for
    13   unexpected errors*)
    14 
    15 val _ = List.app Keyword.keyword
    16  ["!!", "!", "%", "(", ")", "+", ",", "--", ":", "::", ";", "<", "<=",
    17   "=", "==", "=>", "?", "[", "\\<equiv>", "\\<leftharpoondown>",
    18   "\\<rightharpoonup>", "\\<rightleftharpoons>", "\\<subseteq>", "]",
    19   "advanced", "and", "assumes", "attach", "begin", "binder",
    20   "constrains", "defines", "fixes", "for", "identifier", "if",
    21   "imports", "in", "infix", "infixl", "infixr", "is",
    22   "notes", "obtains", "open", "output", "overloaded", "pervasive",
    23   "shows", "structure", "unchecked", "uses", "where", "|"];
    24 
    25 
    26 
    27 (** init and exit **)
    28 
    29 val _ =
    30   Outer_Syntax.command "theory" "begin theory" (Keyword.tag_theory Keyword.thy_begin)
    31     (Thy_Header.args >> (fn (name, imports, uses) =>
    32       Toplevel.print o
    33         Toplevel.init_theory
    34           (fn () =>
    35             Thy_Info.toplevel_begin_theory (Thy_Load.get_master_path ())
    36               name imports (map (apfst Path.explode) uses))));
    37 
    38 val _ =
    39   Outer_Syntax.command "end" "end (local) theory" (Keyword.tag_theory Keyword.thy_end)
    40     (Scan.succeed
    41       (Toplevel.exit o Toplevel.end_local_theory o Toplevel.end_proof (K Proof.end_notepad)));
    42 
    43 
    44 
    45 (** markup commands **)
    46 
    47 val _ = Outer_Syntax.markup_command Thy_Output.Markup "header" "theory header" Keyword.diag
    48   (Parse.doc_source >> Isar_Cmd.header_markup);
    49 
    50 val _ = Outer_Syntax.markup_command Thy_Output.Markup "chapter" "chapter heading"
    51   Keyword.thy_heading (Parse.opt_target -- Parse.doc_source >> Isar_Cmd.local_theory_markup);
    52 
    53 val _ = Outer_Syntax.markup_command Thy_Output.Markup "section" "section heading"
    54   Keyword.thy_heading (Parse.opt_target -- Parse.doc_source >> Isar_Cmd.local_theory_markup);
    55 
    56 val _ = Outer_Syntax.markup_command Thy_Output.Markup "subsection" "subsection heading"
    57   Keyword.thy_heading (Parse.opt_target -- Parse.doc_source >> Isar_Cmd.local_theory_markup);
    58 
    59 val _ =
    60   Outer_Syntax.markup_command Thy_Output.Markup "subsubsection" "subsubsection heading"
    61   Keyword.thy_heading (Parse.opt_target -- Parse.doc_source >> Isar_Cmd.local_theory_markup);
    62 
    63 val _ = Outer_Syntax.markup_command Thy_Output.MarkupEnv "text" "formal comment (theory)"
    64   Keyword.thy_decl (Parse.opt_target -- Parse.doc_source >> Isar_Cmd.local_theory_markup);
    65 
    66 val _ = Outer_Syntax.markup_command Thy_Output.Verbatim "text_raw" "raw document preparation text"
    67   Keyword.thy_decl (Parse.opt_target -- Parse.doc_source >> Isar_Cmd.local_theory_markup);
    68 
    69 val _ = Outer_Syntax.markup_command Thy_Output.Markup "sect" "formal comment (proof)"
    70   (Keyword.tag_proof Keyword.prf_heading) (Parse.doc_source >> Isar_Cmd.proof_markup);
    71 
    72 val _ = Outer_Syntax.markup_command Thy_Output.Markup "subsect" "formal comment (proof)"
    73   (Keyword.tag_proof Keyword.prf_heading) (Parse.doc_source >> Isar_Cmd.proof_markup);
    74 
    75 val _ = Outer_Syntax.markup_command Thy_Output.Markup "subsubsect" "formal comment (proof)"
    76   (Keyword.tag_proof Keyword.prf_heading) (Parse.doc_source >> Isar_Cmd.proof_markup);
    77 
    78 val _ = Outer_Syntax.markup_command Thy_Output.MarkupEnv "txt" "formal comment (proof)"
    79   (Keyword.tag_proof Keyword.prf_decl) (Parse.doc_source >> Isar_Cmd.proof_markup);
    80 
    81 val _ = Outer_Syntax.markup_command Thy_Output.Verbatim "txt_raw"
    82   "raw document preparation text (proof)" (Keyword.tag_proof Keyword.prf_decl)
    83   (Parse.doc_source >> Isar_Cmd.proof_markup);
    84 
    85 
    86 
    87 (** theory commands **)
    88 
    89 (* classes and sorts *)
    90 
    91 val _ =
    92   Outer_Syntax.command "classes" "declare type classes" Keyword.thy_decl
    93     (Scan.repeat1 (Parse.binding -- Scan.optional ((Parse.$$$ "\\<subseteq>" || Parse.$$$ "<") |--
    94         Parse.!!! (Parse.list1 Parse.xname)) [])
    95       >> (Toplevel.theory o fold AxClass.axiomatize_class_cmd));
    96 
    97 val _ =
    98   Outer_Syntax.command "classrel" "state inclusion of type classes (axiomatic!)" Keyword.thy_decl
    99     (Parse.and_list1 (Parse.xname -- ((Parse.$$$ "\\<subseteq>" || Parse.$$$ "<")
   100         |-- Parse.!!! Parse.xname))
   101     >> (Toplevel.theory o AxClass.axiomatize_classrel_cmd));
   102 
   103 val _ =
   104   Outer_Syntax.local_theory "default_sort" "declare default sort for explicit type variables"
   105     Keyword.thy_decl
   106     (Parse.sort >> (fn s => fn lthy => Local_Theory.set_defsort (Syntax.read_sort lthy s) lthy));
   107 
   108 
   109 (* types *)
   110 
   111 val _ =
   112   Outer_Syntax.local_theory "typedecl" "type declaration" Keyword.thy_decl
   113     (Parse.type_args -- Parse.binding -- Parse.opt_mixfix
   114       >> (fn ((args, a), mx) => Typedecl.typedecl (a, map (rpair dummyS) args, mx) #> snd));
   115 
   116 val type_abbrev =
   117   Parse.type_args -- Parse.binding --
   118     (Parse.$$$ "=" |-- Parse.!!! (Parse.typ -- Parse.opt_mixfix'));
   119 
   120 val _ =
   121   Outer_Syntax.local_theory "type_synonym" "declare type abbreviation" Keyword.thy_decl
   122     (type_abbrev >> (fn ((args, a), (rhs, mx)) => snd o Typedecl.abbrev_cmd (a, args, mx) rhs));
   123 
   124 val _ =
   125   Outer_Syntax.command "nonterminal"
   126     "declare syntactic type constructors (grammar nonterminal symbols)" Keyword.thy_decl
   127     (Parse.and_list1 Parse.binding >> (Toplevel.theory o Sign.add_nonterminals_global));
   128 
   129 val _ =
   130   Outer_Syntax.command "arities" "state type arities (axiomatic!)" Keyword.thy_decl
   131     (Scan.repeat1 Parse.arity >> (Toplevel.theory o fold AxClass.axiomatize_arity_cmd));
   132 
   133 
   134 (* consts and syntax *)
   135 
   136 val _ =
   137   Outer_Syntax.command "judgment" "declare object-logic judgment" Keyword.thy_decl
   138     (Parse.const_binding >> (Toplevel.theory o Object_Logic.add_judgment_cmd));
   139 
   140 val _ =
   141   Outer_Syntax.command "consts" "declare constants" Keyword.thy_decl
   142     (Scan.repeat1 Parse.const_binding >> (Toplevel.theory o Sign.add_consts));
   143 
   144 val opt_overloaded = Parse.opt_keyword "overloaded";
   145 
   146 val _ =
   147   Outer_Syntax.command "finalconsts" "declare constants as final" Keyword.thy_decl
   148     (opt_overloaded -- Scan.repeat1 Parse.term >> (uncurry (Toplevel.theory oo Theory.add_finals)));
   149 
   150 val mode_spec =
   151   (Parse.$$$ "output" >> K ("", false)) ||
   152     Parse.name -- Scan.optional (Parse.$$$ "output" >> K false) true;
   153 
   154 val opt_mode =
   155   Scan.optional (Parse.$$$ "(" |-- Parse.!!! (mode_spec --| Parse.$$$ ")")) Syntax.mode_default;
   156 
   157 val _ =
   158   Outer_Syntax.command "syntax" "declare syntactic constants" Keyword.thy_decl
   159     (opt_mode -- Scan.repeat1 Parse.const_decl >> (Toplevel.theory o uncurry Sign.add_modesyntax));
   160 
   161 val _ =
   162   Outer_Syntax.command "no_syntax" "delete syntax declarations" Keyword.thy_decl
   163     (opt_mode -- Scan.repeat1 Parse.const_decl >> (Toplevel.theory o uncurry Sign.del_modesyntax));
   164 
   165 
   166 (* translations *)
   167 
   168 val trans_pat =
   169   Scan.optional
   170     (Parse.$$$ "(" |-- Parse.!!! (Parse.xname --| Parse.$$$ ")")) "logic"
   171     -- Parse.inner_syntax Parse.string;
   172 
   173 fun trans_arrow toks =
   174   ((Parse.$$$ "\\<rightharpoonup>" || Parse.$$$ "=>") >> K Syntax.Parse_Rule ||
   175     (Parse.$$$ "\\<leftharpoondown>" || Parse.$$$ "<=") >> K Syntax.Print_Rule ||
   176     (Parse.$$$ "\\<rightleftharpoons>" || Parse.$$$ "==") >> K Syntax.Parse_Print_Rule) toks;
   177 
   178 val trans_line =
   179   trans_pat -- Parse.!!! (trans_arrow -- trans_pat)
   180     >> (fn (left, (arr, right)) => arr (left, right));
   181 
   182 val _ =
   183   Outer_Syntax.command "translations" "declare syntax translation rules" Keyword.thy_decl
   184     (Scan.repeat1 trans_line >> (Toplevel.theory o Isar_Cmd.translations));
   185 
   186 val _ =
   187   Outer_Syntax.command "no_translations" "remove syntax translation rules" Keyword.thy_decl
   188     (Scan.repeat1 trans_line >> (Toplevel.theory o Isar_Cmd.no_translations));
   189 
   190 
   191 (* axioms and definitions *)
   192 
   193 val _ =
   194   Outer_Syntax.command "axioms" "state arbitrary propositions (axiomatic!)" Keyword.thy_decl
   195     (Scan.repeat1 Parse_Spec.spec >>
   196       (fn spec => Toplevel.theory (fn thy =>
   197         (legacy_feature "Old 'axioms' command -- use 'axiomatization' instead";
   198           Isar_Cmd.add_axioms spec thy))));
   199 
   200 val opt_unchecked_overloaded =
   201   Scan.optional (Parse.$$$ "(" |-- Parse.!!!
   202     (((Parse.$$$ "unchecked" >> K true) -- Scan.optional (Parse.$$$ "overloaded" >> K true) false ||
   203       Parse.$$$ "overloaded" >> K (false, true)) --| Parse.$$$ ")")) (false, false);
   204 
   205 val _ =
   206   Outer_Syntax.command "defs" "define constants" Keyword.thy_decl
   207     (opt_unchecked_overloaded --
   208       Scan.repeat1 (Parse_Spec.thm_name ":" -- Parse.prop >> (fn ((x, y), z) => ((x, z), y)))
   209       >> (Toplevel.theory o Isar_Cmd.add_defs));
   210 
   211 
   212 (* constant definitions and abbreviations *)
   213 
   214 val _ =
   215   Outer_Syntax.local_theory' "definition" "constant definition" Keyword.thy_decl
   216     (Parse_Spec.constdef >> (fn args => #2 oo Specification.definition_cmd args));
   217 
   218 val _ =
   219   Outer_Syntax.local_theory' "abbreviation" "constant abbreviation" Keyword.thy_decl
   220     (opt_mode -- (Scan.option Parse_Spec.constdecl -- Parse.prop)
   221       >> (fn (mode, args) => Specification.abbreviation_cmd mode args));
   222 
   223 val _ =
   224   Outer_Syntax.local_theory "type_notation" "add concrete syntax for type constructors"
   225     Keyword.thy_decl
   226     (opt_mode -- Parse.and_list1 (Parse.type_const -- Parse.mixfix)
   227       >> (fn (mode, args) => Specification.type_notation_cmd true mode args));
   228 
   229 val _ =
   230   Outer_Syntax.local_theory "no_type_notation" "delete concrete syntax for type constructors"
   231     Keyword.thy_decl
   232     (opt_mode -- Parse.and_list1 (Parse.type_const -- Parse.mixfix)
   233       >> (fn (mode, args) => Specification.type_notation_cmd false mode args));
   234 
   235 val _ =
   236   Outer_Syntax.local_theory "notation" "add concrete syntax for constants / fixed variables"
   237     Keyword.thy_decl
   238     (opt_mode -- Parse.and_list1 (Parse.const -- Parse_Spec.locale_mixfix)
   239       >> (fn (mode, args) => Specification.notation_cmd true mode args));
   240 
   241 val _ =
   242   Outer_Syntax.local_theory "no_notation" "delete concrete syntax for constants / fixed variables"
   243     Keyword.thy_decl
   244     (opt_mode -- Parse.and_list1 (Parse.const -- Parse_Spec.locale_mixfix)
   245       >> (fn (mode, args) => Specification.notation_cmd false mode args));
   246 
   247 
   248 (* constant specifications *)
   249 
   250 val _ =
   251   Outer_Syntax.command "axiomatization" "axiomatic constant specification" Keyword.thy_decl
   252     (Scan.optional Parse.fixes [] --
   253       Scan.optional (Parse.where_ |-- Parse.!!! (Parse.and_list1 Parse_Spec.specs)) []
   254       >> (fn (x, y) => Toplevel.theory (#2 o Specification.axiomatization_cmd x y)));
   255 
   256 
   257 (* theorems *)
   258 
   259 fun theorems kind =
   260   Parse_Spec.name_facts >> (fn args => #2 oo Specification.theorems_cmd kind args);
   261 
   262 val _ =
   263   Outer_Syntax.local_theory' "theorems" "define theorems" Keyword.thy_decl (theorems Thm.theoremK);
   264 
   265 val _ =
   266   Outer_Syntax.local_theory' "lemmas" "define lemmas" Keyword.thy_decl (theorems Thm.lemmaK);
   267 
   268 val _ =
   269   Outer_Syntax.local_theory' "declare" "declare theorems" Keyword.thy_decl
   270     (Parse.and_list1 Parse_Spec.xthms1
   271       >> (fn args => #2 oo Specification.theorems_cmd "" [(Attrib.empty_binding, flat args)]));
   272 
   273 
   274 (* name space entry path *)
   275 
   276 fun hide_names name hide what =
   277   Outer_Syntax.command name ("hide " ^ what ^ " from name space") Keyword.thy_decl
   278     ((Parse.opt_keyword "open" >> not) -- Scan.repeat1 Parse.xname >>
   279       (Toplevel.theory o uncurry hide));
   280 
   281 val _ = hide_names "hide_class" Isar_Cmd.hide_class "classes";
   282 val _ = hide_names "hide_type" Isar_Cmd.hide_type "types";
   283 val _ = hide_names "hide_const" Isar_Cmd.hide_const "constants";
   284 val _ = hide_names "hide_fact" Isar_Cmd.hide_fact "facts";
   285 
   286 
   287 (* use ML text *)
   288 
   289 val _ =
   290   Outer_Syntax.command "use" "ML text from file" (Keyword.tag_ml Keyword.thy_decl)
   291     (Parse.path >> (fn path => Toplevel.generic_theory (Thy_Load.exec_ml path)));
   292 
   293 val _ =
   294   Outer_Syntax.command "ML" "ML text within theory or local theory"
   295     (Keyword.tag_ml Keyword.thy_decl)
   296     (Parse.ML_source >> (fn (txt, pos) =>
   297       Toplevel.generic_theory
   298         (ML_Context.exec (fn () => ML_Context.eval_text true pos txt) #>
   299           Local_Theory.propagate_ml_env)));
   300 
   301 val _ =
   302   Outer_Syntax.command "ML_prf" "ML text within proof" (Keyword.tag_proof Keyword.prf_decl)
   303     (Parse.ML_source >> (fn (txt, pos) =>
   304       Toplevel.proof (Proof.map_context (Context.proof_map
   305         (ML_Context.exec (fn () => ML_Context.eval_text true pos txt))) #> Proof.propagate_ml_env)));
   306 
   307 val _ =
   308   Outer_Syntax.command "ML_val" "diagnostic ML text" (Keyword.tag_ml Keyword.diag)
   309     (Parse.ML_source >> Isar_Cmd.ml_diag true);
   310 
   311 val _ =
   312   Outer_Syntax.command "ML_command" "diagnostic ML text (silent)" (Keyword.tag_ml Keyword.diag)
   313     (Parse.ML_source >> (Toplevel.no_timing oo Isar_Cmd.ml_diag false));
   314 
   315 val _ =
   316   Outer_Syntax.command "setup" "ML theory setup" (Keyword.tag_ml Keyword.thy_decl)
   317     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.global_setup));
   318 
   319 val _ =
   320   Outer_Syntax.local_theory "local_setup" "ML local theory setup" (Keyword.tag_ml Keyword.thy_decl)
   321     (Parse.ML_source >> Isar_Cmd.local_setup);
   322 
   323 val _ =
   324   Outer_Syntax.command "attribute_setup" "define attribute in ML" (Keyword.tag_ml Keyword.thy_decl)
   325     (Parse.position Parse.name --
   326         Parse.!!! (Parse.$$$ "=" |-- Parse.ML_source -- Scan.optional Parse.text "")
   327       >> (fn (name, (txt, cmt)) => Toplevel.theory (Attrib.attribute_setup name txt cmt)));
   328 
   329 val _ =
   330   Outer_Syntax.command "method_setup" "define proof method in ML" (Keyword.tag_ml Keyword.thy_decl)
   331     (Parse.position Parse.name --
   332         Parse.!!! (Parse.$$$ "=" |-- Parse.ML_source -- Scan.optional Parse.text "")
   333       >> (fn (name, (txt, cmt)) => Toplevel.theory (Method.method_setup name txt cmt)));
   334 
   335 val _ =
   336   Outer_Syntax.local_theory "declaration" "generic ML declaration"
   337     (Keyword.tag_ml Keyword.thy_decl)
   338     (Parse.opt_keyword "pervasive" -- Parse.ML_source
   339       >> (fn (pervasive, txt) => Isar_Cmd.declaration {syntax = false, pervasive = pervasive} txt));
   340 
   341 val _ =
   342   Outer_Syntax.local_theory "syntax_declaration" "generic ML declaration"
   343     (Keyword.tag_ml Keyword.thy_decl)
   344     (Parse.opt_keyword "pervasive" -- Parse.ML_source
   345       >> (fn (pervasive, txt) => Isar_Cmd.declaration {syntax = true, pervasive = pervasive} txt));
   346 
   347 val _ =
   348   Outer_Syntax.local_theory "simproc_setup" "define simproc in ML" (Keyword.tag_ml Keyword.thy_decl)
   349     (Parse.position Parse.name --
   350       (Parse.$$$ "(" |-- Parse.enum1 "|" Parse.term --| Parse.$$$ ")" --| Parse.$$$ "=") --
   351       Parse.ML_source -- Scan.optional (Parse.$$$ "identifier" |-- Scan.repeat1 Parse.xname) []
   352     >> (fn (((a, b), c), d) => Isar_Cmd.simproc_setup a b c d));
   353 
   354 
   355 (* translation functions *)
   356 
   357 val trfun = Parse.opt_keyword "advanced" -- Parse.ML_source;
   358 
   359 val _ =
   360   Outer_Syntax.command "parse_ast_translation" "install parse ast translation functions"
   361     (Keyword.tag_ml Keyword.thy_decl)
   362     (trfun >> (Toplevel.theory o Isar_Cmd.parse_ast_translation));
   363 
   364 val _ =
   365   Outer_Syntax.command "parse_translation" "install parse translation functions"
   366     (Keyword.tag_ml Keyword.thy_decl)
   367     (trfun >> (Toplevel.theory o Isar_Cmd.parse_translation));
   368 
   369 val _ =
   370   Outer_Syntax.command "print_translation" "install print translation functions"
   371     (Keyword.tag_ml Keyword.thy_decl)
   372     (trfun >> (Toplevel.theory o Isar_Cmd.print_translation));
   373 
   374 val _ =
   375   Outer_Syntax.command "typed_print_translation" "install typed print translation functions"
   376     (Keyword.tag_ml Keyword.thy_decl)
   377     (trfun >> (Toplevel.theory o Isar_Cmd.typed_print_translation));
   378 
   379 val _ =
   380   Outer_Syntax.command "print_ast_translation" "install print ast translation functions"
   381     (Keyword.tag_ml Keyword.thy_decl)
   382     (trfun >> (Toplevel.theory o Isar_Cmd.print_ast_translation));
   383 
   384 
   385 (* oracles *)
   386 
   387 val _ =
   388   Outer_Syntax.command "oracle" "declare oracle" (Keyword.tag_ml Keyword.thy_decl)
   389     (Parse.position Parse.name -- (Parse.$$$ "=" |-- Parse.ML_source) >>
   390       (fn (x, y) => Toplevel.theory (Isar_Cmd.oracle x y)));
   391 
   392 
   393 (* local theories *)
   394 
   395 val _ =
   396   Outer_Syntax.command "context" "enter local theory context" Keyword.thy_decl
   397     (Parse.position Parse.name --| Parse.begin >> (fn name =>
   398       Toplevel.print o Toplevel.begin_local_theory true (Named_Target.context_cmd name)));
   399 
   400 
   401 (* locales *)
   402 
   403 val locale_val =
   404   Parse_Spec.locale_expression false --
   405     Scan.optional (Parse.$$$ "+" |-- Parse.!!! (Scan.repeat1 Parse_Spec.context_element)) [] ||
   406   Scan.repeat1 Parse_Spec.context_element >> pair ([], []);
   407 
   408 val _ =
   409   Outer_Syntax.command "locale" "define named proof context" Keyword.thy_decl
   410     (Parse.binding --
   411       Scan.optional (Parse.$$$ "=" |-- Parse.!!! locale_val) (([], []), []) -- Parse.opt_begin
   412       >> (fn ((name, (expr, elems)), begin) =>
   413           (begin ? Toplevel.print) o Toplevel.begin_local_theory begin
   414             (Expression.add_locale_cmd I name Binding.empty expr elems #> snd)));
   415 
   416 fun parse_interpretation_arguments mandatory =
   417   Parse.!!! (Parse_Spec.locale_expression mandatory) --
   418     Scan.optional
   419       (Parse.where_ |-- Parse.and_list1 (Parse_Spec.opt_thm_name ":" -- Parse.prop)) [];
   420 
   421 val _ =
   422   Outer_Syntax.command "sublocale"
   423     "prove sublocale relation between a locale and a locale expression" Keyword.thy_goal
   424     (Parse.xname --| (Parse.$$$ "\\<subseteq>" || Parse.$$$ "<") --
   425       parse_interpretation_arguments false
   426       >> (fn (loc, (expr, equations)) =>
   427           Toplevel.print o Toplevel.theory_to_proof (Expression.sublocale_cmd I loc expr equations)));
   428 
   429 val _ =
   430   Outer_Syntax.command "interpretation"
   431     "prove interpretation of locale expression in theory" Keyword.thy_goal
   432     (parse_interpretation_arguments true
   433       >> (fn (expr, equations) => Toplevel.print o
   434           Toplevel.theory_to_proof (Expression.interpretation_cmd expr equations)));
   435 
   436 val _ =
   437   Outer_Syntax.command "interpret"
   438     "prove interpretation of locale expression in proof context"
   439     (Keyword.tag_proof Keyword.prf_goal)
   440     (parse_interpretation_arguments true
   441       >> (fn (expr, equations) => Toplevel.print o
   442           Toplevel.proof' (Expression.interpret_cmd expr equations)));
   443 
   444 
   445 (* classes *)
   446 
   447 val class_val =
   448   Parse_Spec.class_expr --
   449     Scan.optional (Parse.$$$ "+" |-- Parse.!!! (Scan.repeat1 Parse_Spec.context_element)) [] ||
   450   Scan.repeat1 Parse_Spec.context_element >> pair [];
   451 
   452 val _ =
   453   Outer_Syntax.command "class" "define type class" Keyword.thy_decl
   454    (Parse.binding -- Scan.optional (Parse.$$$ "=" |-- class_val) ([], []) -- Parse.opt_begin
   455     >> (fn ((name, (supclasses, elems)), begin) =>
   456         (begin ? Toplevel.print) o Toplevel.begin_local_theory begin
   457           (Class_Declaration.class_cmd I name supclasses elems #> snd)));
   458 
   459 val _ =
   460   Outer_Syntax.local_theory_to_proof "subclass" "prove a subclass relation" Keyword.thy_goal
   461     (Parse.xname >> Class_Declaration.subclass_cmd I);
   462 
   463 val _ =
   464   Outer_Syntax.command "instantiation" "instantiate and prove type arity" Keyword.thy_decl
   465    (Parse.multi_arity --| Parse.begin
   466      >> (fn arities => Toplevel.print o
   467          Toplevel.begin_local_theory true (Class.instantiation_cmd arities)));
   468 
   469 val _ =
   470   Outer_Syntax.command "instance" "prove type arity or subclass relation" Keyword.thy_goal
   471   ((Parse.xname -- ((Parse.$$$ "\\<subseteq>" || Parse.$$$ "<") |-- Parse.!!! Parse.xname)
   472         >> Class.classrel_cmd ||
   473     Parse.multi_arity >> Class.instance_arity_cmd)
   474     >> (Toplevel.print oo Toplevel.theory_to_proof) ||
   475     Scan.succeed
   476       (Toplevel.print o Toplevel.local_theory_to_proof NONE (Class.instantiation_instance I)));
   477 
   478 
   479 (* arbitrary overloading *)
   480 
   481 val _ =
   482   Outer_Syntax.command "overloading" "overloaded definitions" Keyword.thy_decl
   483    (Scan.repeat1 (Parse.name --| (Parse.$$$ "\\<equiv>" || Parse.$$$ "==") -- Parse.term --
   484       Scan.optional (Parse.$$$ "(" |-- (Parse.$$$ "unchecked" >> K false) --| Parse.$$$ ")") true
   485       >> Parse.triple1) --| Parse.begin
   486    >> (fn operations => Toplevel.print o
   487          Toplevel.begin_local_theory true (Overloading.overloading_cmd operations)));
   488 
   489 
   490 (* code generation *)
   491 
   492 val _ =
   493   Outer_Syntax.command "code_datatype" "define set of code datatype constructors" Keyword.thy_decl
   494     (Scan.repeat1 Parse.term >> (Toplevel.theory o Code.add_datatype_cmd));
   495 
   496 
   497 
   498 (** proof commands **)
   499 
   500 (* statements *)
   501 
   502 fun gen_theorem schematic kind =
   503   Outer_Syntax.local_theory_to_proof'
   504     (if schematic then "schematic_" ^ kind else kind)
   505     ("state " ^ (if schematic then "schematic " ^ kind else kind))
   506     (if schematic then Keyword.thy_schematic_goal else Keyword.thy_goal)
   507     (Scan.optional (Parse_Spec.opt_thm_name ":" --|
   508       Scan.ahead (Parse_Spec.locale_keyword || Parse_Spec.statement_keyword)) Attrib.empty_binding --
   509       Parse_Spec.general_statement >> (fn (a, (elems, concl)) =>
   510         ((if schematic then Specification.schematic_theorem_cmd else Specification.theorem_cmd)
   511           kind NONE (K I) a elems concl)));
   512 
   513 val _ = gen_theorem false Thm.theoremK;
   514 val _ = gen_theorem false Thm.lemmaK;
   515 val _ = gen_theorem false Thm.corollaryK;
   516 val _ = gen_theorem true Thm.theoremK;
   517 val _ = gen_theorem true Thm.lemmaK;
   518 val _ = gen_theorem true Thm.corollaryK;
   519 
   520 val _ =
   521   Outer_Syntax.local_theory_to_proof "notepad"
   522     "Isar proof state as formal notepad, without any result" Keyword.thy_decl
   523     (Parse.begin >> K Proof.begin_notepad);
   524 
   525 val _ =
   526   Outer_Syntax.command "have" "state local goal"
   527     (Keyword.tag_proof Keyword.prf_goal)
   528     (Parse_Spec.statement >> ((Toplevel.print oo Toplevel.proof') o Isar_Cmd.have));
   529 
   530 val _ =
   531   Outer_Syntax.command "hence" "abbreviates \"then have\""
   532     (Keyword.tag_proof Keyword.prf_goal)
   533     (Parse_Spec.statement >> ((Toplevel.print oo Toplevel.proof') o Isar_Cmd.hence));
   534 
   535 val _ =
   536   Outer_Syntax.command "show" "state local goal, solving current obligation"
   537     (Keyword.tag_proof Keyword.prf_asm_goal)
   538     (Parse_Spec.statement >> ((Toplevel.print oo Toplevel.proof') o Isar_Cmd.show));
   539 
   540 val _ =
   541   Outer_Syntax.command "thus" "abbreviates \"then show\""
   542     (Keyword.tag_proof Keyword.prf_asm_goal)
   543     (Parse_Spec.statement >> ((Toplevel.print oo Toplevel.proof') o Isar_Cmd.thus));
   544 
   545 
   546 (* facts *)
   547 
   548 val facts = Parse.and_list1 Parse_Spec.xthms1;
   549 
   550 val _ =
   551   Outer_Syntax.command "then" "forward chaining"
   552     (Keyword.tag_proof Keyword.prf_chain)
   553     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.chain));
   554 
   555 val _ =
   556   Outer_Syntax.command "from" "forward chaining from given facts"
   557     (Keyword.tag_proof Keyword.prf_chain)
   558     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.from_thmss_cmd)));
   559 
   560 val _ =
   561   Outer_Syntax.command "with" "forward chaining from given and current facts"
   562     (Keyword.tag_proof Keyword.prf_chain)
   563     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.with_thmss_cmd)));
   564 
   565 val _ =
   566   Outer_Syntax.command "note" "define facts"
   567     (Keyword.tag_proof Keyword.prf_decl)
   568     (Parse_Spec.name_facts >> (Toplevel.print oo (Toplevel.proof o Proof.note_thmss_cmd)));
   569 
   570 val _ =
   571   Outer_Syntax.command "using" "augment goal facts"
   572     (Keyword.tag_proof Keyword.prf_decl)
   573     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.using_cmd)));
   574 
   575 val _ =
   576   Outer_Syntax.command "unfolding" "unfold definitions in goal and facts"
   577     (Keyword.tag_proof Keyword.prf_decl)
   578     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.unfolding_cmd)));
   579 
   580 
   581 (* proof context *)
   582 
   583 val _ =
   584   Outer_Syntax.command "fix" "fix local variables (Skolem constants)"
   585     (Keyword.tag_proof Keyword.prf_asm)
   586     (Parse.fixes >> (Toplevel.print oo (Toplevel.proof o Proof.fix_cmd)));
   587 
   588 val _ =
   589   Outer_Syntax.command "assume" "assume propositions"
   590     (Keyword.tag_proof Keyword.prf_asm)
   591     (Parse_Spec.statement >> (Toplevel.print oo (Toplevel.proof o Proof.assume_cmd)));
   592 
   593 val _ =
   594   Outer_Syntax.command "presume" "assume propositions, to be established later"
   595     (Keyword.tag_proof Keyword.prf_asm)
   596     (Parse_Spec.statement >> (Toplevel.print oo (Toplevel.proof o Proof.presume_cmd)));
   597 
   598 val _ =
   599   Outer_Syntax.command "def" "local definition"
   600     (Keyword.tag_proof Keyword.prf_asm)
   601     (Parse.and_list1
   602       (Parse_Spec.opt_thm_name ":" --
   603         ((Parse.binding -- Parse.opt_mixfix) --
   604           ((Parse.$$$ "\\<equiv>" || Parse.$$$ "==") |-- Parse.!!! Parse.termp)))
   605     >> (Toplevel.print oo (Toplevel.proof o Proof.def_cmd)));
   606 
   607 val _ =
   608   Outer_Syntax.command "obtain" "generalized existence"
   609     (Keyword.tag_proof Keyword.prf_asm_goal)
   610     (Parse.parname -- Scan.optional (Parse.fixes --| Parse.where_) [] -- Parse_Spec.statement
   611       >> (fn ((x, y), z) => Toplevel.print o Toplevel.proof' (Obtain.obtain_cmd x y z)));
   612 
   613 val _ =
   614   Outer_Syntax.command "guess" "wild guessing (unstructured)"
   615     (Keyword.tag_proof Keyword.prf_asm_goal)
   616     (Scan.optional Parse.fixes [] >> (Toplevel.print oo (Toplevel.proof' o Obtain.guess_cmd)));
   617 
   618 val _ =
   619   Outer_Syntax.command "let" "bind text variables"
   620     (Keyword.tag_proof Keyword.prf_decl)
   621     (Parse.and_list1 (Parse.and_list1 Parse.term -- (Parse.$$$ "=" |-- Parse.term))
   622       >> (Toplevel.print oo (Toplevel.proof o Proof.let_bind_cmd)));
   623 
   624 val _ =
   625   Outer_Syntax.command "write" "add concrete syntax for constants / fixed variables"
   626     (Keyword.tag_proof Keyword.prf_decl)
   627     (opt_mode -- Parse.and_list1 (Parse.const -- Parse_Spec.locale_mixfix)
   628     >> (fn (mode, args) => Toplevel.print o Toplevel.proof (Proof.write_cmd mode args)));
   629 
   630 val case_spec =
   631   (Parse.$$$ "(" |--
   632     Parse.!!! (Parse.xname -- Scan.repeat1 (Parse.maybe Parse.binding) --| Parse.$$$ ")") ||
   633     Parse.xname >> rpair []) -- Parse_Spec.opt_attribs >> Parse.triple1;
   634 
   635 val _ =
   636   Outer_Syntax.command "case" "invoke local context"
   637     (Keyword.tag_proof Keyword.prf_asm)
   638     (case_spec >> (Toplevel.print oo (Toplevel.proof o Proof.invoke_case_cmd)));
   639 
   640 
   641 (* proof structure *)
   642 
   643 val _ =
   644   Outer_Syntax.command "{" "begin explicit proof block"
   645     (Keyword.tag_proof Keyword.prf_open)
   646     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.begin_block));
   647 
   648 val _ =
   649   Outer_Syntax.command "}" "end explicit proof block"
   650     (Keyword.tag_proof Keyword.prf_close)
   651     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.end_block));
   652 
   653 val _ =
   654   Outer_Syntax.command "next" "enter next proof block"
   655     (Keyword.tag_proof Keyword.prf_block)
   656     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.next_block));
   657 
   658 
   659 (* end proof *)
   660 
   661 val _ =
   662   Outer_Syntax.command "qed" "conclude (sub-)proof"
   663     (Keyword.tag_proof Keyword.qed_block)
   664     (Scan.option Method.parse >> Isar_Cmd.qed);
   665 
   666 val _ =
   667   Outer_Syntax.command "by" "terminal backward proof"
   668     (Keyword.tag_proof Keyword.qed)
   669     (Method.parse -- Scan.option Method.parse >> Isar_Cmd.terminal_proof);
   670 
   671 val _ =
   672   Outer_Syntax.command ".." "default proof"
   673     (Keyword.tag_proof Keyword.qed)
   674     (Scan.succeed Isar_Cmd.default_proof);
   675 
   676 val _ =
   677   Outer_Syntax.command "." "immediate proof"
   678     (Keyword.tag_proof Keyword.qed)
   679     (Scan.succeed Isar_Cmd.immediate_proof);
   680 
   681 val _ =
   682   Outer_Syntax.command "done" "done proof"
   683     (Keyword.tag_proof Keyword.qed)
   684     (Scan.succeed Isar_Cmd.done_proof);
   685 
   686 val _ =
   687   Outer_Syntax.command "sorry" "skip proof (quick-and-dirty mode only!)"
   688     (Keyword.tag_proof Keyword.qed)
   689     (Scan.succeed Isar_Cmd.skip_proof);
   690 
   691 val _ =
   692   Outer_Syntax.command "oops" "forget proof"
   693     (Keyword.tag_proof Keyword.qed_global)
   694     (Scan.succeed Toplevel.forget_proof);
   695 
   696 
   697 (* proof steps *)
   698 
   699 val _ =
   700   Outer_Syntax.command "defer" "shuffle internal proof state"
   701     (Keyword.tag_proof Keyword.prf_script)
   702     (Scan.option Parse.nat >> (Toplevel.print oo (Toplevel.proofs o Proof.defer)));
   703 
   704 val _ =
   705   Outer_Syntax.command "prefer" "shuffle internal proof state"
   706     (Keyword.tag_proof Keyword.prf_script)
   707     (Parse.nat >> (Toplevel.print oo (Toplevel.proofs o Proof.prefer)));
   708 
   709 val _ =
   710   Outer_Syntax.command "apply" "initial refinement step (unstructured)"
   711     (Keyword.tag_proof Keyword.prf_script)
   712     (Method.parse >> (Toplevel.print oo (Toplevel.proofs o Proof.apply)));
   713 
   714 val _ =
   715   Outer_Syntax.command "apply_end" "terminal refinement (unstructured)"
   716     (Keyword.tag_proof Keyword.prf_script)
   717     (Method.parse >> (Toplevel.print oo (Toplevel.proofs o Proof.apply_end)));
   718 
   719 val _ =
   720   Outer_Syntax.command "proof" "backward proof"
   721     (Keyword.tag_proof Keyword.prf_block)
   722     (Scan.option Method.parse >> (fn m => Toplevel.print o
   723       Toplevel.actual_proof (Proof_Node.applys (Proof.proof m)) o
   724       Toplevel.skip_proof (fn i => i + 1)));
   725 
   726 
   727 (* calculational proof commands *)
   728 
   729 val calc_args =
   730   Scan.option (Parse.$$$ "(" |-- Parse.!!! ((Parse_Spec.xthms1 --| Parse.$$$ ")")));
   731 
   732 val _ =
   733   Outer_Syntax.command "also" "combine calculation and current facts"
   734     (Keyword.tag_proof Keyword.prf_decl)
   735     (calc_args >> (Toplevel.proofs' o Calculation.also_cmd));
   736 
   737 val _ =
   738   Outer_Syntax.command "finally" "combine calculation and current facts, exhibit result"
   739     (Keyword.tag_proof Keyword.prf_chain)
   740     (calc_args >> (Toplevel.proofs' o Calculation.finally_cmd));
   741 
   742 val _ =
   743   Outer_Syntax.command "moreover" "augment calculation by current facts"
   744     (Keyword.tag_proof Keyword.prf_decl)
   745     (Scan.succeed (Toplevel.proof' Calculation.moreover));
   746 
   747 val _ =
   748   Outer_Syntax.command "ultimately" "augment calculation by current facts, exhibit result"
   749     (Keyword.tag_proof Keyword.prf_chain)
   750     (Scan.succeed (Toplevel.proof' Calculation.ultimately));
   751 
   752 
   753 (* proof navigation *)
   754 
   755 val _ =
   756   Outer_Syntax.command "back" "backtracking of proof command"
   757     (Keyword.tag_proof Keyword.prf_script)
   758     (Scan.succeed (Toplevel.print o Toplevel.actual_proof Proof_Node.back o Toplevel.skip_proof I));
   759 
   760 
   761 (* nested commands *)
   762 
   763 val props_text =
   764   Scan.optional Parse.properties [] -- Parse.position Parse.string
   765   >> (fn (props, (str, pos)) =>
   766       (Position.of_properties (Position.default_properties pos props), str));
   767 
   768 val _ =
   769   Outer_Syntax.improper_command "Isabelle.command" "nested Isabelle command" Keyword.control
   770     (props_text :|-- (fn (pos, str) =>
   771       (case Outer_Syntax.parse pos str of
   772         [tr] => Scan.succeed (K tr)
   773       | _ => Scan.fail_with (K (fn () => "exactly one command expected")))
   774       handle ERROR msg => Scan.fail_with (K (fn () => msg))));
   775 
   776 
   777 
   778 (** diagnostic commands (for interactive mode only) **)
   779 
   780 val opt_modes =
   781   Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Scan.repeat1 Parse.xname --| Parse.$$$ ")")) [];
   782 
   783 val opt_bang = Scan.optional (Parse.$$$ "!" >> K true) false;
   784 
   785 val _ =
   786   Outer_Syntax.improper_command "pretty_setmargin" "change default margin for pretty printing"
   787     Keyword.diag (Parse.nat >>
   788       (fn n => Toplevel.no_timing o Toplevel.imperative (fn () => Pretty.margin_default := n)));
   789 
   790 val _ =
   791   Outer_Syntax.improper_command "help" "print outer syntax commands" Keyword.diag
   792     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative Outer_Syntax.print_outer_syntax));
   793 
   794 val _ =
   795   Outer_Syntax.improper_command "print_commands" "print outer syntax commands" Keyword.diag
   796     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative Outer_Syntax.print_outer_syntax));
   797 
   798 val _ =
   799   Outer_Syntax.improper_command "print_configs" "print configuration options" Keyword.diag
   800     (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_configs));
   801 
   802 val _ =
   803   Outer_Syntax.improper_command "print_context" "print theory context name" Keyword.diag
   804     (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_context));
   805 
   806 val _ =
   807   Outer_Syntax.improper_command "print_theory" "print logical theory contents (verbose!)"
   808     Keyword.diag (opt_bang >> (Toplevel.no_timing oo Isar_Cmd.print_theory));
   809 
   810 val _ =
   811   Outer_Syntax.improper_command "print_syntax" "print inner syntax of context (verbose!)"
   812     Keyword.diag (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_syntax));
   813 
   814 val _ =
   815   Outer_Syntax.improper_command "print_abbrevs" "print constant abbreviation of context"
   816     Keyword.diag (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_abbrevs));
   817 
   818 val _ =
   819   Outer_Syntax.improper_command "print_theorems"
   820       "print theorems of local theory or proof context" Keyword.diag
   821     (opt_bang >> (Toplevel.no_timing oo Isar_Cmd.print_theorems));
   822 
   823 val _ =
   824   Outer_Syntax.improper_command "print_locales" "print locales of this theory" Keyword.diag
   825     (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_locales));
   826 
   827 val _ =
   828   Outer_Syntax.improper_command "print_classes" "print classes of this theory" Keyword.diag
   829     (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o
   830       Toplevel.keep (Class.print_classes o Toplevel.context_of)));
   831 
   832 val _ =
   833   Outer_Syntax.improper_command "print_locale" "print locale of this theory" Keyword.diag
   834     (opt_bang -- Parse.xname >> (Toplevel.no_timing oo Isar_Cmd.print_locale));
   835 
   836 val _ =
   837   Outer_Syntax.improper_command "print_interps"
   838     "print interpretations of locale for this theory or proof context" Keyword.diag
   839     (Parse.xname >> (Toplevel.no_timing oo Isar_Cmd.print_registrations));
   840 
   841 val _ =
   842   Outer_Syntax.improper_command "print_dependencies"
   843     "print dependencies of locale expression" Keyword.diag
   844     (opt_bang -- Parse_Spec.locale_expression true >>
   845       (Toplevel.no_timing oo Isar_Cmd.print_dependencies));
   846 
   847 val _ =
   848   Outer_Syntax.improper_command "print_attributes" "print attributes of this theory" Keyword.diag
   849     (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_attributes));
   850 
   851 val _ =
   852   Outer_Syntax.improper_command "print_simpset" "print context of Simplifier" Keyword.diag
   853     (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_simpset));
   854 
   855 val _ =
   856   Outer_Syntax.improper_command "print_rules" "print intro/elim rules" Keyword.diag
   857     (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_rules));
   858 
   859 val _ =
   860   Outer_Syntax.improper_command "print_trans_rules" "print transitivity rules" Keyword.diag
   861     (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_trans_rules));
   862 
   863 val _ =
   864   Outer_Syntax.improper_command "print_methods" "print methods of this theory" Keyword.diag
   865     (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_methods));
   866 
   867 val _ =
   868   Outer_Syntax.improper_command "print_antiquotations" "print antiquotations (global)" Keyword.diag
   869     (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_antiquotations));
   870 
   871 val _ =
   872   Outer_Syntax.improper_command "thy_deps" "visualize theory dependencies"
   873     Keyword.diag (Scan.succeed (Toplevel.no_timing o Isar_Cmd.thy_deps));
   874 
   875 val _ =
   876   Outer_Syntax.improper_command "class_deps" "visualize class dependencies"
   877     Keyword.diag (Scan.succeed (Toplevel.no_timing o Isar_Cmd.class_deps));
   878 
   879 val _ =
   880   Outer_Syntax.improper_command "thm_deps" "visualize theorem dependencies"
   881     Keyword.diag (Parse_Spec.xthms1 >> (Toplevel.no_timing oo Isar_Cmd.thm_deps));
   882 
   883 val _ =
   884   Outer_Syntax.improper_command "print_binds" "print term bindings of proof context" Keyword.diag
   885     (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_binds));
   886 
   887 val _ =
   888   Outer_Syntax.improper_command "print_facts" "print facts of proof context" Keyword.diag
   889     (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_facts));
   890 
   891 val _ =
   892   Outer_Syntax.improper_command "print_cases" "print cases of proof context" Keyword.diag
   893     (Scan.succeed (Toplevel.no_timing o Isar_Cmd.print_cases));
   894 
   895 val _ =
   896   Outer_Syntax.improper_command "print_statement" "print theorems as long statements" Keyword.diag
   897     (opt_modes -- Parse_Spec.xthms1 >> (Toplevel.no_timing oo Isar_Cmd.print_stmts));
   898 
   899 val _ =
   900   Outer_Syntax.improper_command "thm" "print theorems" Keyword.diag
   901     (opt_modes -- Parse_Spec.xthms1 >> (Toplevel.no_timing oo Isar_Cmd.print_thms));
   902 
   903 val _ =
   904   Outer_Syntax.improper_command "prf" "print proof terms of theorems" Keyword.diag
   905     (opt_modes -- Scan.option Parse_Spec.xthms1
   906       >> (Toplevel.no_timing oo Isar_Cmd.print_prfs false));
   907 
   908 val _ =
   909   Outer_Syntax.improper_command "full_prf" "print full proof terms of theorems" Keyword.diag
   910     (opt_modes -- Scan.option Parse_Spec.xthms1 >> (Toplevel.no_timing oo Isar_Cmd.print_prfs true));
   911 
   912 val _ =
   913   Outer_Syntax.improper_command "prop" "read and print proposition" Keyword.diag
   914     (opt_modes -- Parse.term >> (Toplevel.no_timing oo Isar_Cmd.print_prop));
   915 
   916 val _ =
   917   Outer_Syntax.improper_command "term" "read and print term" Keyword.diag
   918     (opt_modes -- Parse.term >> (Toplevel.no_timing oo Isar_Cmd.print_term));
   919 
   920 val _ =
   921   Outer_Syntax.improper_command "typ" "read and print type" Keyword.diag
   922     (opt_modes -- Parse.typ >> (Toplevel.no_timing oo Isar_Cmd.print_type));
   923 
   924 val _ =
   925   Outer_Syntax.improper_command "print_codesetup" "print code generator setup" Keyword.diag
   926     (Scan.succeed
   927       (Toplevel.no_timing o Toplevel.unknown_theory o Toplevel.keep
   928         (Code.print_codesetup o Toplevel.theory_of)));
   929 
   930 val _ =
   931   Outer_Syntax.improper_command "unused_thms" "find unused theorems" Keyword.diag
   932     (Scan.option ((Scan.repeat1 (Scan.unless Parse.minus Parse.name) --| Parse.minus) --
   933        Scan.option (Scan.repeat1 (Scan.unless Parse.minus Parse.name))) >>
   934          (Toplevel.no_timing oo Isar_Cmd.unused_thms));
   935 
   936 
   937 
   938 (** system commands (for interactive mode only) **)
   939 
   940 val _ =
   941   Outer_Syntax.improper_command "cd" "change current working directory" Keyword.control
   942     (Parse.path >> (fn path => Toplevel.no_timing o Toplevel.imperative (fn () => File.cd path)));
   943 
   944 val _ =
   945   Outer_Syntax.improper_command "pwd" "print current working directory" Keyword.diag
   946     (Scan.succeed (Toplevel.no_timing o
   947       Toplevel.imperative (fn () => writeln (Path.print (File.pwd ())))));
   948 
   949 val _ =
   950   Outer_Syntax.improper_command "use_thy" "use theory file" Keyword.control
   951     (Parse.name >> (fn name =>
   952       Toplevel.no_timing o Toplevel.imperative (fn () => Thy_Info.use_thy name)));
   953 
   954 val _ =
   955   Outer_Syntax.improper_command "remove_thy" "remove theory from loader database" Keyword.control
   956     (Parse.name >> (fn name =>
   957       Toplevel.no_timing o Toplevel.imperative (fn () => Thy_Info.remove_thy name)));
   958 
   959 val _ =
   960   Outer_Syntax.improper_command "kill_thy" "kill theory -- try to remove from loader database"
   961     Keyword.control (Parse.name >> (fn name =>
   962       Toplevel.no_timing o Toplevel.imperative (fn () => Thy_Info.kill_thy name)));
   963 
   964 val _ =
   965   Outer_Syntax.improper_command "display_drafts" "display raw source files with symbols"
   966     Keyword.diag (Scan.repeat1 Parse.path >> (Toplevel.no_timing oo Isar_Cmd.display_drafts));
   967 
   968 val _ =
   969   Outer_Syntax.improper_command "print_drafts" "print raw source files with symbols"
   970     Keyword.diag (Scan.repeat1 Parse.path >> (Toplevel.no_timing oo Isar_Cmd.print_drafts));
   971 
   972 val _ =  (* FIXME tty only *)
   973   Outer_Syntax.improper_command "pr" "print current proof state (if present)" Keyword.diag
   974     (opt_modes -- Scan.option Parse.nat >> (fn (modes, limit) =>
   975       Toplevel.no_timing o Toplevel.keep (fn state =>
   976        (case limit of NONE => () | SOME n => Goal_Display.goals_limit_default := n;
   977         Toplevel.quiet := false;
   978         Print_Mode.with_modes modes (Toplevel.print_state true) state))));
   979 
   980 val _ =
   981   Outer_Syntax.improper_command "disable_pr" "disable printing of toplevel state" Keyword.control
   982     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative (fn () => Toplevel.quiet := true)));
   983 
   984 val _ =
   985   Outer_Syntax.improper_command "enable_pr" "enable printing of toplevel state" Keyword.control
   986     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative (fn () => Toplevel.quiet := false)));
   987 
   988 val _ =
   989   Outer_Syntax.improper_command "commit" "commit current session to ML database" Keyword.control
   990     (Parse.opt_unit >> K (Toplevel.no_timing o Toplevel.imperative Secure.commit));
   991 
   992 val _ =
   993   Outer_Syntax.improper_command "quit" "quit Isabelle" Keyword.control
   994     (Parse.opt_unit >> (Toplevel.no_timing oo K (Toplevel.imperative quit)));
   995 
   996 val _ =
   997   Outer_Syntax.improper_command "exit" "exit Isar loop" Keyword.control
   998     (Scan.succeed
   999       (Toplevel.no_timing o Toplevel.keep (fn state =>
  1000         (Context.set_thread_data (try Toplevel.generic_theory_of state);
  1001           raise Runtime.TERMINATE))));
  1002 
  1003 end;
  1004