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