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