src/Pure/Isar/isar_syn.ML
author wenzelm
Tue Mar 25 13:18:10 2014 +0100 (2014-03-25 ago)
changeset 56275 600f432ab556
parent 56240 938c6c7e10eb
child 56304 40274e4f5ebf
permissions -rw-r--r--
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
     1 (*  Title:      Pure/Isar/isar_syn.ML
     2     Author:     Makarius
     3 
     4 Outer syntax for Isabelle/Pure.
     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 (* sorts *)
    77 
    78 val _ =
    79   Outer_Syntax.local_theory @{command_spec "default_sort"}
    80     "declare default sort for explicit type variables"
    81     (Parse.sort >> (fn s => fn lthy => Local_Theory.set_defsort (Syntax.read_sort lthy s) lthy));
    82 
    83 
    84 (* types *)
    85 
    86 val _ =
    87   Outer_Syntax.local_theory @{command_spec "typedecl"} "type declaration"
    88     (Parse.type_args -- Parse.binding -- Parse.opt_mixfix
    89       >> (fn ((args, a), mx) => Typedecl.typedecl (a, map (rpair dummyS) args, mx) #> snd));
    90 
    91 val _ =
    92   Outer_Syntax.local_theory @{command_spec "type_synonym"} "declare type abbreviation"
    93     (Parse.type_args -- Parse.binding --
    94       (@{keyword "="} |-- Parse.!!! (Parse.typ -- Parse.opt_mixfix'))
    95       >> (fn ((args, a), (rhs, mx)) => snd o Typedecl.abbrev_cmd (a, args, mx) rhs));
    96 
    97 val _ =
    98   Outer_Syntax.command @{command_spec "nonterminal"}
    99     "declare syntactic type constructors (grammar nonterminal symbols)"
   100     (Parse.and_list1 Parse.binding >> (Toplevel.theory o Sign.add_nonterminals_global));
   101 
   102 
   103 (* consts and syntax *)
   104 
   105 val _ =
   106   Outer_Syntax.command @{command_spec "judgment"} "declare object-logic judgment"
   107     (Parse.const_binding >> (Toplevel.theory o Object_Logic.add_judgment_cmd));
   108 
   109 val _ =
   110   Outer_Syntax.command @{command_spec "consts"} "declare constants"
   111     (Scan.repeat1 Parse.const_binding >> (Toplevel.theory o Sign.add_consts_cmd));
   112 
   113 val mode_spec =
   114   (@{keyword "output"} >> K ("", false)) ||
   115     Parse.name -- Scan.optional (@{keyword "output"} >> K false) true;
   116 
   117 val opt_mode =
   118   Scan.optional (@{keyword "("} |-- Parse.!!! (mode_spec --| @{keyword ")"})) Syntax.mode_default;
   119 
   120 val _ =
   121   Outer_Syntax.command @{command_spec "syntax"} "add raw syntax clauses"
   122     (opt_mode -- Scan.repeat1 Parse.const_decl
   123       >> (Toplevel.theory o uncurry Sign.add_syntax_cmd));
   124 
   125 val _ =
   126   Outer_Syntax.command @{command_spec "no_syntax"} "delete raw syntax clauses"
   127     (opt_mode -- Scan.repeat1 Parse.const_decl
   128       >> (Toplevel.theory o uncurry Sign.del_syntax_cmd));
   129 
   130 
   131 (* translations *)
   132 
   133 val trans_pat =
   134   Scan.optional
   135     (@{keyword "("} |-- Parse.!!! (Parse.inner_syntax Parse.xname --| @{keyword ")"})) "logic"
   136     -- Parse.inner_syntax Parse.string;
   137 
   138 fun trans_arrow toks =
   139   ((@{keyword "\<rightharpoonup>"} || @{keyword "=>"}) >> K Syntax.Parse_Rule ||
   140     (@{keyword "\<leftharpoondown>"} || @{keyword "<="}) >> K Syntax.Print_Rule ||
   141     (@{keyword "\<rightleftharpoons>"} || @{keyword "=="}) >> K Syntax.Parse_Print_Rule) toks;
   142 
   143 val trans_line =
   144   trans_pat -- Parse.!!! (trans_arrow -- trans_pat)
   145     >> (fn (left, (arr, right)) => arr (left, right));
   146 
   147 val _ =
   148   Outer_Syntax.command @{command_spec "translations"} "add syntax translation rules"
   149     (Scan.repeat1 trans_line >> (Toplevel.theory o Isar_Cmd.translations));
   150 
   151 val _ =
   152   Outer_Syntax.command @{command_spec "no_translations"} "delete syntax translation rules"
   153     (Scan.repeat1 trans_line >> (Toplevel.theory o Isar_Cmd.no_translations));
   154 
   155 
   156 (* axioms and definitions *)
   157 
   158 val opt_unchecked_overloaded =
   159   Scan.optional (@{keyword "("} |-- Parse.!!!
   160     (((@{keyword "unchecked"} >> K true) --
   161         Scan.optional (@{keyword "overloaded"} >> K true) false ||
   162       @{keyword "overloaded"} >> K (false, true)) --| @{keyword ")"})) (false, false);
   163 
   164 val _ =
   165   Outer_Syntax.command @{command_spec "defs"} "define constants"
   166     (opt_unchecked_overloaded --
   167       Scan.repeat1 (Parse_Spec.thm_name ":" -- Parse.prop >> (fn ((x, y), z) => ((x, z), y)))
   168       >> (Toplevel.theory o Isar_Cmd.add_defs));
   169 
   170 
   171 (* constant definitions and abbreviations *)
   172 
   173 val _ =
   174   Outer_Syntax.local_theory' @{command_spec "definition"} "constant definition"
   175     (Parse_Spec.constdef >> (fn args => #2 oo Specification.definition_cmd args));
   176 
   177 val _ =
   178   Outer_Syntax.local_theory' @{command_spec "abbreviation"} "constant abbreviation"
   179     (opt_mode -- (Scan.option Parse_Spec.constdecl -- Parse.prop)
   180       >> (fn (mode, args) => Specification.abbreviation_cmd mode args));
   181 
   182 val _ =
   183   Outer_Syntax.local_theory @{command_spec "type_notation"}
   184     "add concrete syntax for type constructors"
   185     (opt_mode -- Parse.and_list1 (Parse.type_const -- Parse.mixfix)
   186       >> (fn (mode, args) => Specification.type_notation_cmd true mode args));
   187 
   188 val _ =
   189   Outer_Syntax.local_theory @{command_spec "no_type_notation"}
   190     "delete concrete syntax for type constructors"
   191     (opt_mode -- Parse.and_list1 (Parse.type_const -- Parse.mixfix)
   192       >> (fn (mode, args) => Specification.type_notation_cmd false mode args));
   193 
   194 val _ =
   195   Outer_Syntax.local_theory @{command_spec "notation"}
   196     "add concrete syntax for constants / fixed variables"
   197     (opt_mode -- Parse.and_list1 (Parse.const -- Parse.mixfix)
   198       >> (fn (mode, args) => Specification.notation_cmd true mode args));
   199 
   200 val _ =
   201   Outer_Syntax.local_theory @{command_spec "no_notation"}
   202     "delete concrete syntax for constants / fixed variables"
   203     (opt_mode -- Parse.and_list1 (Parse.const -- Parse.mixfix)
   204       >> (fn (mode, args) => Specification.notation_cmd false mode args));
   205 
   206 
   207 (* constant specifications *)
   208 
   209 val _ =
   210   Outer_Syntax.command @{command_spec "axiomatization"} "axiomatic constant specification"
   211     (Scan.optional Parse.fixes [] --
   212       Scan.optional (Parse.where_ |-- Parse.!!! (Parse.and_list1 Parse_Spec.specs)) []
   213       >> (fn (x, y) => Toplevel.theory (#2 o Specification.axiomatization_cmd x y)));
   214 
   215 
   216 (* theorems *)
   217 
   218 fun theorems kind =
   219   Parse_Spec.name_facts -- Parse.for_fixes
   220     >> (fn (facts, fixes) => #2 oo Specification.theorems_cmd kind facts fixes);
   221 
   222 val _ =
   223   Outer_Syntax.local_theory' @{command_spec "theorems"} "define theorems"
   224     (theorems Thm.theoremK);
   225 
   226 val _ =
   227   Outer_Syntax.local_theory' @{command_spec "lemmas"} "define lemmas" (theorems Thm.lemmaK);
   228 
   229 val _ =
   230   Outer_Syntax.local_theory' @{command_spec "declare"} "declare theorems"
   231     (Parse.and_list1 Parse_Spec.xthms1 -- Parse.for_fixes
   232       >> (fn (facts, fixes) =>
   233           #2 oo Specification.theorems_cmd "" [(Attrib.empty_binding, flat facts)] fixes));
   234 
   235 
   236 (* hide names *)
   237 
   238 local
   239 
   240 fun hide_names command_spec what hide parse prep =
   241   Outer_Syntax.command command_spec ("hide " ^ what ^ " from name space")
   242     ((Parse.opt_keyword "open" >> not) -- Scan.repeat1 parse >> (fn (fully, args) =>
   243       (Toplevel.theory (fn thy =>
   244         let val ctxt = Proof_Context.init_global thy
   245         in fold (hide fully o prep ctxt) args thy end))));
   246 
   247 in
   248 
   249 val _ =
   250   hide_names @{command_spec "hide_class"} "classes" Sign.hide_class Parse.class
   251     Proof_Context.read_class;
   252 
   253 val _ =
   254   hide_names @{command_spec "hide_type"} "types" Sign.hide_type Parse.type_const
   255     ((#1 o dest_Type) oo Proof_Context.read_type_name {proper = true, strict = false});
   256 
   257 val _ =
   258   hide_names @{command_spec "hide_const"} "constants" Sign.hide_const Parse.const
   259     ((#1 o dest_Const) oo Proof_Context.read_const {proper = true, strict = false});
   260 
   261 val _ =
   262   hide_names @{command_spec "hide_fact"} "facts" Global_Theory.hide_fact
   263     (Parse.position Parse.xname) (Global_Theory.check_fact o Proof_Context.theory_of);
   264 
   265 end;
   266 
   267 
   268 (* use ML text *)
   269 
   270 val _ =
   271   Outer_Syntax.command @{command_spec "SML_file"} "read and evaluate Standard ML file"
   272     (Resources.provide_parse_files "SML_file" >> (fn files => Toplevel.theory (fn thy =>
   273         let
   274           val ([{lines, pos, ...}], thy') = files thy;
   275           val source = {delimited = true, text = cat_lines lines, pos = pos};
   276         in
   277           thy' |> Context.theory_map
   278             (ML_Context.exec (fn () => ML_Context.eval_source {SML = true, verbose = true} source))
   279         end)));
   280 
   281 val _ =
   282   Outer_Syntax.command @{command_spec "ML"} "ML text within theory or local theory"
   283     (Parse.ML_source >> (fn source =>
   284       Toplevel.generic_theory
   285         (ML_Context.exec (fn () => ML_Context.eval_source {SML = false, verbose = true} source) #>
   286           Local_Theory.propagate_ml_env)));
   287 
   288 val _ =
   289   Outer_Syntax.command @{command_spec "ML_prf"} "ML text within proof"
   290     (Parse.ML_source >> (fn source =>
   291       Toplevel.proof (Proof.map_context (Context.proof_map
   292         (ML_Context.exec (fn () => ML_Context.eval_source {SML = false, verbose = true} source))) #>
   293           Proof.propagate_ml_env)));
   294 
   295 val _ =
   296   Outer_Syntax.command @{command_spec "ML_val"} "diagnostic ML text"
   297     (Parse.ML_source >> Isar_Cmd.ml_diag true);
   298 
   299 val _ =
   300   Outer_Syntax.command @{command_spec "ML_command"} "diagnostic ML text (silent)"
   301     (Parse.ML_source >> Isar_Cmd.ml_diag false);
   302 
   303 val _ =
   304   Outer_Syntax.command @{command_spec "setup"} "ML theory setup"
   305     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.global_setup));
   306 
   307 val _ =
   308   Outer_Syntax.local_theory @{command_spec "local_setup"} "ML local theory setup"
   309     (Parse.ML_source >> Isar_Cmd.local_setup);
   310 
   311 val _ =
   312   Outer_Syntax.command @{command_spec "attribute_setup"} "define attribute in ML"
   313     (Parse.position Parse.name --
   314         Parse.!!! (@{keyword "="} |-- Parse.ML_source -- Scan.optional Parse.text "")
   315       >> (fn (name, (txt, cmt)) => Toplevel.theory (Attrib.attribute_setup name txt cmt)));
   316 
   317 val _ =
   318   Outer_Syntax.command @{command_spec "method_setup"} "define proof method in ML"
   319     (Parse.position Parse.name --
   320         Parse.!!! (@{keyword "="} |-- Parse.ML_source -- Scan.optional Parse.text "")
   321       >> (fn (name, (txt, cmt)) => Toplevel.theory (Method.method_setup name txt cmt)));
   322 
   323 val _ =
   324   Outer_Syntax.local_theory @{command_spec "declaration"} "generic ML declaration"
   325     (Parse.opt_keyword "pervasive" -- Parse.ML_source
   326       >> (fn (pervasive, txt) => Isar_Cmd.declaration {syntax = false, pervasive = pervasive} txt));
   327 
   328 val _ =
   329   Outer_Syntax.local_theory @{command_spec "syntax_declaration"} "generic ML syntax declaration"
   330     (Parse.opt_keyword "pervasive" -- Parse.ML_source
   331       >> (fn (pervasive, txt) => Isar_Cmd.declaration {syntax = true, pervasive = pervasive} txt));
   332 
   333 val _ =
   334   Outer_Syntax.local_theory @{command_spec "simproc_setup"} "define simproc in ML"
   335     (Parse.position Parse.name --
   336       (@{keyword "("} |-- Parse.enum1 "|" Parse.term --| @{keyword ")"} --| @{keyword "="}) --
   337       Parse.ML_source -- Scan.optional (@{keyword "identifier"} |-- Scan.repeat1 Parse.xname) []
   338     >> (fn (((a, b), c), d) => Isar_Cmd.simproc_setup a b c d));
   339 
   340 
   341 (* translation functions *)
   342 
   343 val _ =
   344   Outer_Syntax.command @{command_spec "parse_ast_translation"}
   345     "install parse ast translation functions"
   346     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.parse_ast_translation));
   347 
   348 val _ =
   349   Outer_Syntax.command @{command_spec "parse_translation"}
   350     "install parse translation functions"
   351     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.parse_translation));
   352 
   353 val _ =
   354   Outer_Syntax.command @{command_spec "print_translation"}
   355     "install print translation functions"
   356     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.print_translation));
   357 
   358 val _ =
   359   Outer_Syntax.command @{command_spec "typed_print_translation"}
   360     "install typed print translation functions"
   361     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.typed_print_translation));
   362 
   363 val _ =
   364   Outer_Syntax.command @{command_spec "print_ast_translation"}
   365     "install print ast translation functions"
   366     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.print_ast_translation));
   367 
   368 
   369 (* oracles *)
   370 
   371 val _ =
   372   Outer_Syntax.command @{command_spec "oracle"} "declare oracle"
   373     (Parse.position Parse.name -- (@{keyword "="} |-- Parse.ML_source) >>
   374       (fn (x, y) => Toplevel.theory (Isar_Cmd.oracle x y)));
   375 
   376 
   377 (* bundled declarations *)
   378 
   379 val _ =
   380   Outer_Syntax.local_theory @{command_spec "bundle"} "define bundle of declarations"
   381     ((Parse.binding --| @{keyword "="}) -- Parse_Spec.xthms1 -- Parse.for_fixes
   382       >> (uncurry Bundle.bundle_cmd));
   383 
   384 val _ =
   385   Outer_Syntax.command @{command_spec "include"}
   386     "include declarations from bundle in proof body"
   387     (Scan.repeat1 (Parse.position Parse.xname)
   388       >> (Toplevel.print oo (Toplevel.proof o Bundle.include_cmd)));
   389 
   390 val _ =
   391   Outer_Syntax.command @{command_spec "including"}
   392     "include declarations from bundle in goal refinement"
   393     (Scan.repeat1 (Parse.position Parse.xname)
   394       >> (Toplevel.print oo (Toplevel.proof o Bundle.including_cmd)));
   395 
   396 val _ =
   397   Outer_Syntax.improper_command @{command_spec "print_bundles"}
   398     "print bundles of declarations"
   399     (Scan.succeed (Toplevel.unknown_context o
   400       Toplevel.keep (Bundle.print_bundles o Toplevel.context_of)));
   401 
   402 
   403 (* local theories *)
   404 
   405 val _ =
   406   Outer_Syntax.command @{command_spec "context"} "begin local theory context"
   407     ((Parse.position Parse.xname >> (fn name =>
   408         Toplevel.print o Toplevel.begin_local_theory true (Named_Target.context_cmd name)) ||
   409       Scan.optional Parse_Spec.includes [] -- Scan.repeat Parse_Spec.context_element
   410         >> (fn (incls, elems) => Toplevel.open_target (Bundle.context_cmd incls elems)))
   411       --| Parse.begin);
   412 
   413 
   414 (* locales *)
   415 
   416 val locale_val =
   417   Parse_Spec.locale_expression false --
   418     Scan.optional (@{keyword "+"} |-- Parse.!!! (Scan.repeat1 Parse_Spec.context_element)) [] ||
   419   Scan.repeat1 Parse_Spec.context_element >> pair ([], []);
   420 
   421 val _ =
   422   Outer_Syntax.command @{command_spec "locale"} "define named proof context"
   423     (Parse.binding --
   424       Scan.optional (@{keyword "="} |-- Parse.!!! locale_val) (([], []), []) -- Parse.opt_begin
   425       >> (fn ((name, (expr, elems)), begin) =>
   426           (begin ? Toplevel.print) o Toplevel.begin_local_theory begin
   427             (Expression.add_locale_cmd I name Binding.empty expr elems #> snd)));
   428 
   429 fun interpretation_args mandatory =
   430   Parse.!!! (Parse_Spec.locale_expression mandatory) --
   431     Scan.optional
   432       (Parse.where_ |-- Parse.and_list1 (Parse_Spec.opt_thm_name ":" -- Parse.prop)) [];
   433 
   434 val _ =
   435   Outer_Syntax.command @{command_spec "sublocale"}
   436     "prove sublocale relation between a locale and a locale expression"
   437     ((Parse.position Parse.xname --| (@{keyword "\<subseteq>"} || @{keyword "<"}) --
   438       interpretation_args false >> (fn (loc, (expr, equations)) =>
   439         Toplevel.print o
   440         Toplevel.theory_to_proof (Expression.sublocale_global_cmd I loc expr equations)))
   441     || interpretation_args false >> (fn (expr, equations) =>
   442         Toplevel.print o
   443         Toplevel.local_theory_to_proof NONE (Expression.sublocale_cmd expr equations)));
   444 
   445 val _ =
   446   Outer_Syntax.command @{command_spec "interpretation"}
   447     "prove interpretation of locale expression in local theory"
   448     (interpretation_args true >> (fn (expr, equations) =>
   449       Toplevel.print o
   450       Toplevel.local_theory_to_proof NONE (Expression.interpretation_cmd expr equations)));
   451 
   452 val _ =
   453   Outer_Syntax.command @{command_spec "interpret"}
   454     "prove interpretation of locale expression in proof context"
   455     (interpretation_args true >> (fn (expr, equations) =>
   456       Toplevel.print o
   457       Toplevel.proof' (Expression.interpret_cmd expr equations)));
   458 
   459 
   460 (* classes *)
   461 
   462 val class_val =
   463   Parse_Spec.class_expression --
   464     Scan.optional (@{keyword "+"} |-- Parse.!!! (Scan.repeat1 Parse_Spec.context_element)) [] ||
   465   Scan.repeat1 Parse_Spec.context_element >> pair [];
   466 
   467 val _ =
   468   Outer_Syntax.command @{command_spec "class"} "define type class"
   469    (Parse.binding -- Scan.optional (@{keyword "="} |-- class_val) ([], []) -- Parse.opt_begin
   470     >> (fn ((name, (supclasses, elems)), begin) =>
   471         (begin ? Toplevel.print) o Toplevel.begin_local_theory begin
   472           (Class_Declaration.class_cmd I name supclasses elems #> snd)));
   473 
   474 val _ =
   475   Outer_Syntax.local_theory_to_proof @{command_spec "subclass"} "prove a subclass relation"
   476     (Parse.class >> Class_Declaration.subclass_cmd I);
   477 
   478 val _ =
   479   Outer_Syntax.command @{command_spec "instantiation"} "instantiate and prove type arity"
   480    (Parse.multi_arity --| Parse.begin
   481      >> (fn arities => Toplevel.print o
   482          Toplevel.begin_local_theory true (Class.instantiation_cmd arities)));
   483 
   484 val _ =
   485   Outer_Syntax.command @{command_spec "instance"} "prove type arity or subclass relation"
   486   ((Parse.class --
   487     ((@{keyword "\<subseteq>"} || @{keyword "<"}) |-- Parse.!!! Parse.class) >> Class.classrel_cmd ||
   488     Parse.multi_arity >> Class.instance_arity_cmd)
   489     >> (Toplevel.print oo Toplevel.theory_to_proof) ||
   490     Scan.succeed
   491       (Toplevel.print o Toplevel.local_theory_to_proof NONE (Class.instantiation_instance I)));
   492 
   493 
   494 (* arbitrary overloading *)
   495 
   496 val _ =
   497   Outer_Syntax.command @{command_spec "overloading"} "overloaded definitions"
   498    (Scan.repeat1 (Parse.name --| (@{keyword "\<equiv>"} || @{keyword "=="}) -- Parse.term --
   499       Scan.optional (@{keyword "("} |-- (@{keyword "unchecked"} >> K false) --| @{keyword ")"}) true
   500       >> Parse.triple1) --| Parse.begin
   501    >> (fn operations => Toplevel.print o
   502          Toplevel.begin_local_theory true (Overloading.overloading_cmd operations)));
   503 
   504 
   505 (* code generation *)
   506 
   507 val _ =
   508   Outer_Syntax.command @{command_spec "code_datatype"}
   509     "define set of code datatype constructors"
   510     (Scan.repeat1 Parse.term >> (Toplevel.theory o Code.add_datatype_cmd));
   511 
   512 
   513 
   514 (** proof commands **)
   515 
   516 (* statements *)
   517 
   518 fun theorem spec schematic kind =
   519   Outer_Syntax.local_theory_to_proof' spec
   520     ("state " ^ (if schematic then "schematic " ^ kind else kind))
   521     (Scan.optional (Parse_Spec.opt_thm_name ":" --|
   522       Scan.ahead (Parse_Spec.includes >> K "" ||
   523         Parse_Spec.locale_keyword || Parse_Spec.statement_keyword)) Attrib.empty_binding --
   524       Scan.optional Parse_Spec.includes [] --
   525       Parse_Spec.general_statement >> (fn ((a, includes), (elems, concl)) =>
   526         ((if schematic then Specification.schematic_theorem_cmd else Specification.theorem_cmd)
   527           kind NONE (K I) a includes elems concl)));
   528 
   529 val _ = theorem @{command_spec "theorem"} false Thm.theoremK;
   530 val _ = theorem @{command_spec "lemma"} false Thm.lemmaK;
   531 val _ = theorem @{command_spec "corollary"} false Thm.corollaryK;
   532 val _ = theorem @{command_spec "schematic_theorem"} true Thm.theoremK;
   533 val _ = theorem @{command_spec "schematic_lemma"} true Thm.lemmaK;
   534 val _ = theorem @{command_spec "schematic_corollary"} true Thm.corollaryK;
   535 
   536 val _ =
   537   Outer_Syntax.local_theory_to_proof @{command_spec "notepad"} "begin proof context"
   538     (Parse.begin >> K Proof.begin_notepad);
   539 
   540 val _ =
   541   Outer_Syntax.command @{command_spec "have"} "state local goal"
   542     (Parse_Spec.statement >> ((Toplevel.print oo Toplevel.proof') o Isar_Cmd.have));
   543 
   544 val _ =
   545   Outer_Syntax.command @{command_spec "hence"} "old-style alias of \"then have\""
   546     (Parse_Spec.statement >> ((Toplevel.print oo Toplevel.proof') o Isar_Cmd.hence));
   547 
   548 val _ =
   549   Outer_Syntax.command @{command_spec "show"}
   550     "state local goal, solving current obligation"
   551     (Parse_Spec.statement >> ((Toplevel.print oo Toplevel.proof') o Isar_Cmd.show));
   552 
   553 val _ =
   554   Outer_Syntax.command @{command_spec "thus"} "old-style alias of  \"then show\""
   555     (Parse_Spec.statement >> ((Toplevel.print oo Toplevel.proof') o Isar_Cmd.thus));
   556 
   557 
   558 (* facts *)
   559 
   560 val facts = Parse.and_list1 Parse_Spec.xthms1;
   561 
   562 val _ =
   563   Outer_Syntax.command @{command_spec "then"} "forward chaining"
   564     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.chain));
   565 
   566 val _ =
   567   Outer_Syntax.command @{command_spec "from"} "forward chaining from given facts"
   568     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.from_thmss_cmd)));
   569 
   570 val _ =
   571   Outer_Syntax.command @{command_spec "with"} "forward chaining from given and current facts"
   572     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.with_thmss_cmd)));
   573 
   574 val _ =
   575   Outer_Syntax.command @{command_spec "note"} "define facts"
   576     (Parse_Spec.name_facts >> (Toplevel.print oo (Toplevel.proof o Proof.note_thmss_cmd)));
   577 
   578 val _ =
   579   Outer_Syntax.command @{command_spec "using"} "augment goal facts"
   580     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.using_cmd)));
   581 
   582 val _ =
   583   Outer_Syntax.command @{command_spec "unfolding"} "unfold definitions in goal and facts"
   584     (facts >> (Toplevel.print oo (Toplevel.proof o Proof.unfolding_cmd)));
   585 
   586 
   587 (* proof context *)
   588 
   589 val _ =
   590   Outer_Syntax.command @{command_spec "fix"} "fix local variables (Skolem constants)"
   591     (Parse.fixes >> (Toplevel.print oo (Toplevel.proof o Proof.fix_cmd)));
   592 
   593 val _ =
   594   Outer_Syntax.command @{command_spec "assume"} "assume propositions"
   595     (Parse_Spec.statement >> (Toplevel.print oo (Toplevel.proof o Proof.assume_cmd)));
   596 
   597 val _ =
   598   Outer_Syntax.command @{command_spec "presume"} "assume propositions, to be established later"
   599     (Parse_Spec.statement >> (Toplevel.print oo (Toplevel.proof o Proof.presume_cmd)));
   600 
   601 val _ =
   602   Outer_Syntax.command @{command_spec "def"} "local definition (non-polymorphic)"
   603     (Parse.and_list1
   604       (Parse_Spec.opt_thm_name ":" --
   605         ((Parse.binding -- Parse.opt_mixfix) --
   606           ((@{keyword "\<equiv>"} || @{keyword "=="}) |-- Parse.!!! Parse.termp)))
   607     >> (Toplevel.print oo (Toplevel.proof o Proof.def_cmd)));
   608 
   609 val _ =
   610   Outer_Syntax.command @{command_spec "obtain"} "generalized elimination"
   611     (Parse.parname -- Scan.optional (Parse.fixes --| Parse.where_) [] -- Parse_Spec.statement
   612       >> (fn ((x, y), z) => Toplevel.print o Toplevel.proof' (Obtain.obtain_cmd x y z)));
   613 
   614 val _ =
   615   Outer_Syntax.command @{command_spec "guess"} "wild guessing (unstructured)"
   616     (Scan.optional Parse.fixes [] >> (Toplevel.print oo (Toplevel.proof' o Obtain.guess_cmd)));
   617 
   618 val _ =
   619   Outer_Syntax.command @{command_spec "let"} "bind text variables"
   620     (Parse.and_list1 (Parse.and_list1 Parse.term -- (@{keyword "="} |-- Parse.term))
   621       >> (Toplevel.print oo (Toplevel.proof o Proof.let_bind_cmd)));
   622 
   623 val _ =
   624   Outer_Syntax.command @{command_spec "write"} "add concrete syntax for constants / fixed variables"
   625     (opt_mode -- Parse.and_list1 (Parse.const -- Parse.mixfix)
   626     >> (fn (mode, args) => Toplevel.print o Toplevel.proof (Proof.write_cmd mode args)));
   627 
   628 val _ =
   629   Outer_Syntax.command @{command_spec "case"} "invoke local context"
   630     ((@{keyword "("} |--
   631       Parse.!!! (Parse.position Parse.xname -- Scan.repeat (Parse.maybe Parse.binding)
   632         --| @{keyword ")"}) ||
   633       Parse.position Parse.xname >> rpair []) -- Parse_Spec.opt_attribs >> (fn ((c, xs), atts) =>
   634         Toplevel.print o Toplevel.proof (Proof.invoke_case_cmd (c, xs, atts))));
   635 
   636 
   637 (* proof structure *)
   638 
   639 val _ =
   640   Outer_Syntax.command @{command_spec "{"} "begin explicit proof block"
   641     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.begin_block));
   642 
   643 val _ =
   644   Outer_Syntax.command @{command_spec "}"} "end explicit proof block"
   645     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.end_block));
   646 
   647 val _ =
   648   Outer_Syntax.command @{command_spec "next"} "enter next proof block"
   649     (Scan.succeed (Toplevel.print o Toplevel.proof Proof.next_block));
   650 
   651 
   652 (* end proof *)
   653 
   654 val _ =
   655   Outer_Syntax.command @{command_spec "qed"} "conclude proof"
   656     (Scan.option Method.parse >> (fn m => (Option.map Method.report m; Isar_Cmd.qed m)));
   657 
   658 val _ =
   659   Outer_Syntax.command @{command_spec "by"} "terminal backward proof"
   660     (Method.parse -- Scan.option Method.parse >> (fn (m1, m2) =>
   661       (Method.report m1; Option.map Method.report m2; Isar_Cmd.terminal_proof (m1, m2))));
   662 
   663 val _ =
   664   Outer_Syntax.command @{command_spec ".."} "default proof"
   665     (Scan.succeed Isar_Cmd.default_proof);
   666 
   667 val _ =
   668   Outer_Syntax.command @{command_spec "."} "immediate proof"
   669     (Scan.succeed Isar_Cmd.immediate_proof);
   670 
   671 val _ =
   672   Outer_Syntax.command @{command_spec "done"} "done proof"
   673     (Scan.succeed Isar_Cmd.done_proof);
   674 
   675 val _ =
   676   Outer_Syntax.command @{command_spec "sorry"} "skip proof (quick-and-dirty mode only!)"
   677     (Scan.succeed Isar_Cmd.skip_proof);
   678 
   679 val _ =
   680   Outer_Syntax.command @{command_spec "oops"} "forget proof"
   681     (Scan.succeed Toplevel.forget_proof);
   682 
   683 
   684 (* proof steps *)
   685 
   686 val _ =
   687   Outer_Syntax.command @{command_spec "defer"} "shuffle internal proof state"
   688     (Scan.optional Parse.nat 1 >> (fn n => Toplevel.print o Toplevel.proof (Proof.defer n)));
   689 
   690 val _ =
   691   Outer_Syntax.command @{command_spec "prefer"} "shuffle internal proof state"
   692     (Parse.nat >> (fn n => Toplevel.print o Toplevel.proof (Proof.prefer n)));
   693 
   694 val _ =
   695   Outer_Syntax.command @{command_spec "apply"} "initial refinement step (unstructured)"
   696     (Method.parse >> (fn m =>
   697       (Method.report m; Toplevel.print o Toplevel.proofs (Proof.apply_results m))));
   698 
   699 val _ =
   700   Outer_Syntax.command @{command_spec "apply_end"} "terminal refinement step (unstructured)"
   701     (Method.parse >> (fn m =>
   702       (Method.report m; Toplevel.print o Toplevel.proofs (Proof.apply_end_results m))));
   703 
   704 val _ =
   705   Outer_Syntax.command @{command_spec "proof"} "backward proof step"
   706     (Scan.option Method.parse >> (fn m =>
   707       (Option.map Method.report m;
   708         Toplevel.print o
   709         Toplevel.actual_proof (Proof_Node.applys (Proof.proof_results m)) o
   710         Toplevel.skip_proof (fn i => i + 1))));
   711 
   712 
   713 (* proof navigation *)
   714 
   715 fun report_back () =
   716   Output.report (Markup.markup Markup.bad "Explicit backtracking");
   717 
   718 val _ =
   719   Outer_Syntax.command @{command_spec "back"} "explicit backtracking of proof command"
   720     (Scan.succeed (Toplevel.print o
   721       Toplevel.actual_proof (fn prf => (report_back (); Proof_Node.back prf)) o
   722       Toplevel.skip_proof (fn h => (report_back (); h))));
   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 _ = (*Proof General legacy*)
   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.context_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_methods"} "print methods of this theory"
   854     (Scan.succeed (Toplevel.unknown_theory o
   855       Toplevel.keep (Method.print_methods o Toplevel.context_of)));
   856 
   857 val _ =
   858   Outer_Syntax.improper_command @{command_spec "print_antiquotations"}
   859     "print document antiquotations"
   860     (Scan.succeed (Toplevel.unknown_context o
   861       Toplevel.keep (Thy_Output.print_antiquotations o Toplevel.context_of)));
   862 
   863 val _ =
   864   Outer_Syntax.improper_command @{command_spec "print_ML_antiquotations"}
   865     "print ML antiquotations"
   866     (Scan.succeed (Toplevel.unknown_context o
   867       Toplevel.keep (ML_Context.print_antiquotations o Toplevel.context_of)));
   868 
   869 val _ =
   870   Outer_Syntax.improper_command @{command_spec "thy_deps"} "visualize theory dependencies"
   871     (Scan.succeed Isar_Cmd.thy_deps);
   872 
   873 val _ =
   874   Outer_Syntax.improper_command @{command_spec "locale_deps"} "visualize locale dependencies"
   875     (Scan.succeed Isar_Cmd.locale_deps);
   876 
   877 val _ =
   878   Outer_Syntax.improper_command @{command_spec "class_deps"} "visualize class dependencies"
   879     (Scan.succeed Isar_Cmd.class_deps);
   880 
   881 val _ =
   882   Outer_Syntax.improper_command @{command_spec "thm_deps"} "visualize theorem dependencies"
   883     (Parse_Spec.xthms1 >> Isar_Cmd.thm_deps);
   884 
   885 val _ =
   886   Outer_Syntax.improper_command @{command_spec "print_binds"} "print term bindings of proof context"
   887     (Scan.succeed (Toplevel.unknown_context o
   888       Toplevel.keep (Proof_Context.print_binds o Toplevel.context_of)));
   889 
   890 val _ =
   891   Outer_Syntax.improper_command @{command_spec "print_facts"} "print facts of proof context"
   892     (opt_bang >> (fn verbose => Toplevel.unknown_context o
   893       Toplevel.keep (fn st => Proof_Context.print_local_facts (Toplevel.context_of st) verbose)));
   894 
   895 val _ =
   896   Outer_Syntax.improper_command @{command_spec "print_cases"} "print cases of proof context"
   897     (Scan.succeed (Toplevel.unknown_context o
   898       Toplevel.keep (Proof_Context.print_cases o Toplevel.context_of)));
   899 
   900 val _ =
   901   Outer_Syntax.improper_command @{command_spec "print_statement"}
   902     "print theorems as long statements"
   903     (opt_modes -- Parse_Spec.xthms1 >> Isar_Cmd.print_stmts);
   904 
   905 val _ =
   906   Outer_Syntax.improper_command @{command_spec "thm"} "print theorems"
   907     (opt_modes -- Parse_Spec.xthms1 >> Isar_Cmd.print_thms);
   908 
   909 val _ =
   910   Outer_Syntax.improper_command @{command_spec "prf"} "print proof terms of theorems"
   911     (opt_modes -- Scan.option Parse_Spec.xthms1 >> Isar_Cmd.print_prfs false);
   912 
   913 val _ =
   914   Outer_Syntax.improper_command @{command_spec "full_prf"} "print full proof terms of theorems"
   915     (opt_modes -- Scan.option Parse_Spec.xthms1 >> Isar_Cmd.print_prfs true);
   916 
   917 val _ =
   918   Outer_Syntax.improper_command @{command_spec "prop"} "read and print proposition"
   919     (opt_modes -- Parse.term >> Isar_Cmd.print_prop);
   920 
   921 val _ =
   922   Outer_Syntax.improper_command @{command_spec "term"} "read and print term"
   923     (opt_modes -- Parse.term >> Isar_Cmd.print_term);
   924 
   925 val _ =
   926   Outer_Syntax.improper_command @{command_spec "typ"} "read and print type"
   927     (opt_modes -- (Parse.typ -- Scan.option (@{keyword "::"} |-- Parse.!!! Parse.sort))
   928       >> Isar_Cmd.print_type);
   929 
   930 val _ =
   931   Outer_Syntax.improper_command @{command_spec "print_codesetup"} "print code generator setup"
   932     (Scan.succeed (Toplevel.unknown_theory o
   933       Toplevel.keep (Code.print_codesetup o Toplevel.theory_of)));
   934 
   935 val _ =
   936   Outer_Syntax.improper_command @{command_spec "unused_thms"} "find unused theorems"
   937     (Scan.option ((Scan.repeat1 (Scan.unless Parse.minus Parse.name) --| Parse.minus) --
   938        Scan.option (Scan.repeat1 (Scan.unless Parse.minus Parse.name))) >> Isar_Cmd.unused_thms);
   939 
   940 
   941 
   942 (** system commands (for interactive mode only) **)
   943 
   944 val _ =
   945   Outer_Syntax.improper_command @{command_spec "cd"} "change current working directory"
   946     (Parse.path >> (fn name => Toplevel.imperative (fn () => File.cd (Path.explode name))));
   947 
   948 val _ =
   949   Outer_Syntax.improper_command @{command_spec "pwd"} "print current working directory"
   950     (Scan.succeed (Toplevel.imperative (fn () => writeln (Path.print (File.pwd ())))));
   951 
   952 val _ =
   953   Outer_Syntax.improper_command @{command_spec "use_thy"} "use theory file"
   954     (Parse.position Parse.name >>
   955       (fn name => Toplevel.imperative (fn () => Thy_Info.use_thy name)));
   956 
   957 val _ =
   958   Outer_Syntax.improper_command @{command_spec "remove_thy"} "remove theory from loader database"
   959     (Parse.name >> (fn name => Toplevel.imperative (fn () => Thy_Info.remove_thy name)));
   960 
   961 val _ =
   962   Outer_Syntax.improper_command @{command_spec "kill_thy"}
   963     "kill theory -- try to remove from loader database"
   964     (Parse.name >> (fn name => Toplevel.imperative (fn () => Thy_Info.kill_thy name)));
   965 
   966 val _ = (*partial Proof General legacy*)
   967   Outer_Syntax.improper_command @{command_spec "display_drafts"}
   968     "display raw source files with symbols"
   969     (Scan.repeat1 Parse.path >> (fn names =>
   970       Toplevel.imperative (fn () => ignore (Present.display_drafts (map Path.explode names)))));
   971 
   972 val _ =
   973   Outer_Syntax.improper_command @{command_spec "print_state"}
   974     "print current proof state (if present)"
   975     (opt_modes >> (fn modes =>
   976       Toplevel.keep (fn state =>
   977         Print_Mode.with_modes modes (Toplevel.print_state true) state)));
   978 
   979 val _ = (*Proof General legacy, e.g. for ProofGeneral-3.7.x*)
   980   Outer_Syntax.improper_command @{command_spec "pr"} "print current proof state (if present)"
   981     (opt_modes -- Scan.option Parse.nat >> (fn (modes, limit) =>
   982       Toplevel.keep (fn state =>
   983        (if Isabelle_Process.is_active () then error "Illegal TTY command" else ();
   984         case limit of NONE => () | SOME n => Options.default_put_int @{option goals_limit} n;
   985         Toplevel.quiet := false;
   986         Print_Mode.with_modes modes (Toplevel.print_state true) state))));
   987 
   988 val _ = (*Proof General legacy*)
   989   Outer_Syntax.improper_command @{command_spec "disable_pr"}
   990     "disable printing of toplevel state"
   991     (Scan.succeed (Toplevel.imperative (fn () => Toplevel.quiet := true)));
   992 
   993 val _ = (*Proof General legacy*)
   994   Outer_Syntax.improper_command @{command_spec "enable_pr"}
   995     "enable printing of toplevel state"
   996     (Scan.succeed (Toplevel.imperative (fn () => Toplevel.quiet := false)));
   997 
   998 val _ =
   999   Outer_Syntax.improper_command @{command_spec "commit"}
  1000     "commit current session to ML session image"
  1001     (Parse.opt_unit >> K (Toplevel.imperative Secure.commit));
  1002 
  1003 val _ =
  1004   Outer_Syntax.improper_command @{command_spec "quit"} "quit Isabelle"
  1005     (Parse.opt_unit >> (K (Toplevel.imperative quit)));
  1006 
  1007 val _ =
  1008   Outer_Syntax.improper_command @{command_spec "exit"} "exit Isar loop"
  1009     (Scan.succeed
  1010       (Toplevel.keep (fn state =>
  1011         (Context.set_thread_data (try Toplevel.generic_theory_of state);
  1012           raise Runtime.TERMINATE))));
  1013 
  1014 val _ =
  1015   Outer_Syntax.improper_command @{command_spec "welcome"} "print welcome message"
  1016     (Scan.succeed (Toplevel.imperative (writeln o Session.welcome)));
  1017 
  1018 
  1019 
  1020 (** raw Isar read-eval-print loop **)
  1021 
  1022 val _ =
  1023   Outer_Syntax.improper_command @{command_spec "init_toplevel"} "init toplevel point-of-interest"
  1024     (Scan.succeed (Toplevel.imperative Isar.init));
  1025 
  1026 val _ =
  1027   Outer_Syntax.improper_command @{command_spec "linear_undo"} "undo commands"
  1028     (Scan.optional Parse.nat 1 >>
  1029       (fn n => Toplevel.imperative (fn () => Isar.linear_undo n)));
  1030 
  1031 val _ =
  1032   Outer_Syntax.improper_command @{command_spec "undo"} "undo commands (skipping closed proofs)"
  1033     (Scan.optional Parse.nat 1 >>
  1034       (fn n => Toplevel.imperative (fn () => Isar.undo n)));
  1035 
  1036 val _ =
  1037   Outer_Syntax.improper_command @{command_spec "undos_proof"}
  1038     "undo commands (skipping closed proofs)"
  1039     (Scan.optional Parse.nat 1 >> (fn n =>
  1040       Toplevel.keep (fn state =>
  1041         if Toplevel.is_proof state then (Isar.undo n; Isar.print ()) else raise Toplevel.UNDEF)));
  1042 
  1043 val _ =
  1044   Outer_Syntax.improper_command @{command_spec "cannot_undo"}
  1045     "partial undo -- Proof General legacy"
  1046     (Parse.name >>
  1047       (fn "end" => Toplevel.imperative (fn () => Isar.undo 1)
  1048         | txt => Toplevel.imperative (fn () => error ("Cannot undo " ^ quote txt))));
  1049 
  1050 val _ =
  1051   Outer_Syntax.improper_command @{command_spec "kill"}
  1052     "kill partial proof or theory development"
  1053     (Scan.succeed (Toplevel.imperative Isar.kill));
  1054 
  1055 
  1056 
  1057 (** extraction of programs from proofs **)
  1058 
  1059 val parse_vars = Scan.optional (Parse.$$$ "(" |-- Parse.list1 Parse.name --| Parse.$$$ ")") [];
  1060 
  1061 val _ =
  1062   Outer_Syntax.command @{command_spec "realizers"}
  1063     "specify realizers for primitive axioms / theorems, together with correctness proof"
  1064     (Scan.repeat1 (Parse.xname -- parse_vars --| Parse.$$$ ":" -- Parse.string -- Parse.string) >>
  1065      (fn xs => Toplevel.theory (fn thy => Extraction.add_realizers
  1066        (map (fn (((a, vs), s1), s2) => (Global_Theory.get_thm thy a, (vs, s1, s2))) xs) thy)));
  1067 
  1068 val _ =
  1069   Outer_Syntax.command @{command_spec "realizability"}
  1070     "add equations characterizing realizability"
  1071     (Scan.repeat1 Parse.string >> (Toplevel.theory o Extraction.add_realizes_eqns));
  1072 
  1073 val _ =
  1074   Outer_Syntax.command @{command_spec "extract_type"}
  1075     "add equations characterizing type of extracted program"
  1076     (Scan.repeat1 Parse.string >> (Toplevel.theory o Extraction.add_typeof_eqns));
  1077 
  1078 val _ =
  1079   Outer_Syntax.command @{command_spec "extract"} "extract terms from proofs"
  1080     (Scan.repeat1 (Parse.xname -- parse_vars) >> (fn xs => Toplevel.theory (fn thy =>
  1081       Extraction.extract (map (apfst (Global_Theory.get_thm thy)) xs) thy)));
  1082 
  1083 
  1084 
  1085 (** end **)
  1086 
  1087 val _ =
  1088   Outer_Syntax.command @{command_spec "end"} "end context"
  1089     (Scan.succeed
  1090       (Toplevel.exit o Toplevel.end_local_theory o Toplevel.close_target o
  1091         Toplevel.end_proof (K Proof.end_notepad)));
  1092 
  1093 end;
  1094