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