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