src/Pure/Isar/isar_syn.ML
author haftmann
Sat Nov 14 08:45:51 2015 +0100 (2015-11-14)
changeset 61669 27ca6147e3b3
parent 61654 4a28eec739e9
child 61670 301e0b4ecd45
permissions -rw-r--r--
separate ML module for interpretation
     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 (** theory commands **)
    11 
    12 (* sorts *)
    13 
    14 val _ =
    15   Outer_Syntax.local_theory @{command_keyword default_sort}
    16     "declare default sort for explicit type variables"
    17     (Parse.sort >> (fn s => fn lthy => Local_Theory.set_defsort (Syntax.read_sort lthy s) lthy));
    18 
    19 
    20 (* types *)
    21 
    22 val _ =
    23   Outer_Syntax.local_theory @{command_keyword typedecl} "type declaration"
    24     (Parse.type_args -- Parse.binding -- Parse.opt_mixfix
    25       >> (fn ((args, a), mx) =>
    26           Typedecl.typedecl {final = true} (a, map (rpair dummyS) args, mx) #> snd));
    27 
    28 val _ =
    29   Outer_Syntax.local_theory @{command_keyword type_synonym} "declare type abbreviation"
    30     (Parse.type_args -- Parse.binding --
    31       (@{keyword "="} |-- Parse.!!! (Parse.typ -- Parse.opt_mixfix'))
    32       >> (fn ((args, a), (rhs, mx)) => snd o Typedecl.abbrev_cmd (a, args, mx) rhs));
    33 
    34 val _ =
    35   Outer_Syntax.command @{command_keyword nonterminal}
    36     "declare syntactic type constructors (grammar nonterminal symbols)"
    37     (Parse.and_list1 Parse.binding >> (Toplevel.theory o Sign.add_nonterminals_global));
    38 
    39 
    40 (* consts and syntax *)
    41 
    42 val _ =
    43   Outer_Syntax.command @{command_keyword judgment} "declare object-logic judgment"
    44     (Parse.const_binding >> (Toplevel.theory o Object_Logic.add_judgment_cmd));
    45 
    46 val _ =
    47   Outer_Syntax.command @{command_keyword consts} "declare constants"
    48     (Scan.repeat1 Parse.const_binding >> (Toplevel.theory o Sign.add_consts_cmd));
    49 
    50 val mode_spec =
    51   (@{keyword "output"} >> K ("", false)) ||
    52     Parse.name -- Scan.optional (@{keyword "output"} >> K false) true;
    53 
    54 val opt_mode =
    55   Scan.optional (@{keyword "("} |-- Parse.!!! (mode_spec --| @{keyword ")"})) Syntax.mode_default;
    56 
    57 val _ =
    58   Outer_Syntax.command @{command_keyword syntax} "add raw syntax clauses"
    59     (opt_mode -- Scan.repeat1 Parse.const_decl
    60       >> (Toplevel.theory o uncurry Sign.add_syntax_cmd));
    61 
    62 val _ =
    63   Outer_Syntax.command @{command_keyword no_syntax} "delete raw syntax clauses"
    64     (opt_mode -- Scan.repeat1 Parse.const_decl
    65       >> (Toplevel.theory o uncurry Sign.del_syntax_cmd));
    66 
    67 
    68 (* translations *)
    69 
    70 val trans_pat =
    71   Scan.optional
    72     (@{keyword "("} |-- Parse.!!! (Parse.inner_syntax Parse.xname --| @{keyword ")"})) "logic"
    73     -- Parse.inner_syntax Parse.string;
    74 
    75 fun trans_arrow toks =
    76   ((@{keyword "\<rightharpoonup>"} || @{keyword "=>"}) >> K Syntax.Parse_Rule ||
    77     (@{keyword "\<leftharpoondown>"} || @{keyword "<="}) >> K Syntax.Print_Rule ||
    78     (@{keyword "\<rightleftharpoons>"} || @{keyword "=="}) >> K Syntax.Parse_Print_Rule) toks;
    79 
    80 val trans_line =
    81   trans_pat -- Parse.!!! (trans_arrow -- trans_pat)
    82     >> (fn (left, (arr, right)) => arr (left, right));
    83 
    84 val _ =
    85   Outer_Syntax.command @{command_keyword translations} "add syntax translation rules"
    86     (Scan.repeat1 trans_line >> (Toplevel.theory o Isar_Cmd.translations));
    87 
    88 val _ =
    89   Outer_Syntax.command @{command_keyword no_translations} "delete syntax translation rules"
    90     (Scan.repeat1 trans_line >> (Toplevel.theory o Isar_Cmd.no_translations));
    91 
    92 
    93 (* axioms and definitions *)
    94 
    95 val opt_unchecked_overloaded =
    96   Scan.optional (@{keyword "("} |-- Parse.!!!
    97     (((@{keyword "unchecked"} >> K true) --
    98         Scan.optional (@{keyword "overloaded"} >> K true) false ||
    99       @{keyword "overloaded"} >> K (false, true)) --| @{keyword ")"})) (false, false);
   100 
   101 val _ =
   102   Outer_Syntax.command @{command_keyword defs} "define constants"
   103     (opt_unchecked_overloaded --
   104       Scan.repeat1 (Parse_Spec.thm_name ":" -- Parse.prop >> (fn ((x, y), z) => ((x, z), y)))
   105       >> (Toplevel.theory o Isar_Cmd.add_defs));
   106 
   107 
   108 (* constant definitions and abbreviations *)
   109 
   110 val _ =
   111   Outer_Syntax.local_theory' @{command_keyword definition} "constant definition"
   112     (Parse_Spec.constdef >> (fn args => #2 oo Specification.definition_cmd args));
   113 
   114 val _ =
   115   Outer_Syntax.local_theory' @{command_keyword abbreviation} "constant abbreviation"
   116     (opt_mode -- (Scan.option Parse_Spec.constdecl -- Parse.prop)
   117       >> (fn (mode, args) => Specification.abbreviation_cmd mode args));
   118 
   119 val _ =
   120   Outer_Syntax.local_theory @{command_keyword type_notation}
   121     "add concrete syntax for type constructors"
   122     (opt_mode -- Parse.and_list1 (Parse.type_const -- Parse.mixfix)
   123       >> (fn (mode, args) => Specification.type_notation_cmd true mode args));
   124 
   125 val _ =
   126   Outer_Syntax.local_theory @{command_keyword no_type_notation}
   127     "delete concrete syntax for type constructors"
   128     (opt_mode -- Parse.and_list1 (Parse.type_const -- Parse.mixfix)
   129       >> (fn (mode, args) => Specification.type_notation_cmd false mode args));
   130 
   131 val _ =
   132   Outer_Syntax.local_theory @{command_keyword notation}
   133     "add concrete syntax for constants / fixed variables"
   134     (opt_mode -- Parse.and_list1 (Parse.const -- Parse.mixfix)
   135       >> (fn (mode, args) => Specification.notation_cmd true mode args));
   136 
   137 val _ =
   138   Outer_Syntax.local_theory @{command_keyword no_notation}
   139     "delete concrete syntax for constants / fixed variables"
   140     (opt_mode -- Parse.and_list1 (Parse.const -- Parse.mixfix)
   141       >> (fn (mode, args) => Specification.notation_cmd false mode args));
   142 
   143 
   144 (* constant specifications *)
   145 
   146 val _ =
   147   Outer_Syntax.command @{command_keyword axiomatization} "axiomatic constant specification"
   148     (Scan.optional Parse.fixes [] --
   149       Scan.optional (Parse.where_ |-- Parse.!!! (Parse.and_list1 Parse_Spec.specs)) []
   150       >> (fn (x, y) => Toplevel.theory (#2 o Specification.axiomatization_cmd x y)));
   151 
   152 
   153 (* theorems *)
   154 
   155 val _ =
   156   Outer_Syntax.local_theory' @{command_keyword lemmas} "define theorems"
   157     (Parse_Spec.name_facts -- Parse.for_fixes >>
   158       (fn (facts, fixes) => #2 oo Specification.theorems_cmd Thm.theoremK facts fixes));
   159 
   160 val _ =
   161   Outer_Syntax.local_theory' @{command_keyword declare} "declare theorems"
   162     (Parse.and_list1 Parse.xthms1 -- Parse.for_fixes
   163       >> (fn (facts, fixes) =>
   164           #2 oo Specification.theorems_cmd "" [(Attrib.empty_binding, flat facts)] fixes));
   165 
   166 
   167 (* hide names *)
   168 
   169 local
   170 
   171 fun hide_names command_keyword what hide parse prep =
   172   Outer_Syntax.command command_keyword ("hide " ^ what ^ " from name space")
   173     ((Parse.opt_keyword "open" >> not) -- Scan.repeat1 parse >> (fn (fully, args) =>
   174       (Toplevel.theory (fn thy =>
   175         let val ctxt = Proof_Context.init_global thy
   176         in fold (hide fully o prep ctxt) args thy end))));
   177 
   178 in
   179 
   180 val _ =
   181   hide_names @{command_keyword hide_class} "classes" Sign.hide_class Parse.class
   182     Proof_Context.read_class;
   183 
   184 val _ =
   185   hide_names @{command_keyword hide_type} "types" Sign.hide_type Parse.type_const
   186     ((#1 o dest_Type) oo Proof_Context.read_type_name {proper = true, strict = false});
   187 
   188 val _ =
   189   hide_names @{command_keyword hide_const} "constants" Sign.hide_const Parse.const
   190     ((#1 o dest_Const) oo Proof_Context.read_const {proper = true, strict = false});
   191 
   192 val _ =
   193   hide_names @{command_keyword hide_fact} "facts" Global_Theory.hide_fact
   194     (Parse.position Parse.xname) (Global_Theory.check_fact o Proof_Context.theory_of);
   195 
   196 end;
   197 
   198 
   199 (* use ML text *)
   200 
   201 fun SML_file_cmd debug files = Toplevel.theory (fn thy =>
   202   let
   203     val ([{lines, pos, ...}: Token.file], thy') = files thy;
   204     val source = Input.source true (cat_lines lines) (pos, pos);
   205     val flags: ML_Compiler.flags =
   206       {SML = true, exchange = false, redirect = true, verbose = true,
   207         debug = debug, writeln = writeln, warning = warning};
   208   in
   209     thy' |> Context.theory_map
   210       (ML_Context.exec (fn () => ML_Context.eval_source flags source))
   211   end);
   212 
   213 val _ =
   214   Outer_Syntax.command @{command_keyword SML_file} "read and evaluate Standard ML file"
   215     (Resources.provide_parse_files "SML_file" >> SML_file_cmd NONE);
   216 
   217 val _ =
   218   Outer_Syntax.command @{command_keyword SML_file_debug}
   219     "read and evaluate Standard ML file (with debugger information)"
   220     (Resources.provide_parse_files "SML_file_debug" >> SML_file_cmd (SOME true));
   221 
   222 val _ =
   223   Outer_Syntax.command @{command_keyword SML_file_no_debug}
   224     "read and evaluate Standard ML file (no debugger information)"
   225     (Resources.provide_parse_files "SML_file_no_debug" >> SML_file_cmd (SOME false));
   226 
   227 val _ =
   228   Outer_Syntax.command @{command_keyword SML_export} "evaluate SML within Isabelle/ML environment"
   229     (Parse.ML_source >> (fn source =>
   230       let
   231         val flags: ML_Compiler.flags =
   232           {SML = true, exchange = true, redirect = false, verbose = true,
   233             debug = NONE, writeln = writeln, warning = warning};
   234       in
   235         Toplevel.theory
   236           (Context.theory_map (ML_Context.exec (fn () => ML_Context.eval_source flags source)))
   237       end));
   238 
   239 val _ =
   240   Outer_Syntax.command @{command_keyword SML_import} "evaluate Isabelle/ML within SML environment"
   241     (Parse.ML_source >> (fn source =>
   242       let
   243         val flags: ML_Compiler.flags =
   244           {SML = false, exchange = true, redirect = false, verbose = true,
   245             debug = NONE, writeln = writeln, warning = warning};
   246       in
   247         Toplevel.generic_theory
   248           (ML_Context.exec (fn () => ML_Context.eval_source flags source) #>
   249             Local_Theory.propagate_ml_env)
   250       end));
   251 
   252 val _ =
   253   Outer_Syntax.command @{command_keyword ML} "ML text within theory or local theory"
   254     (Parse.ML_source >> (fn source =>
   255       Toplevel.generic_theory
   256         (ML_Context.exec (fn () =>
   257             ML_Context.eval_source (ML_Compiler.verbose true ML_Compiler.flags) source) #>
   258           Local_Theory.propagate_ml_env)));
   259 
   260 val _ =
   261   Outer_Syntax.command @{command_keyword ML_prf} "ML text within proof"
   262     (Parse.ML_source >> (fn source =>
   263       Toplevel.proof (Proof.map_context (Context.proof_map
   264         (ML_Context.exec (fn () =>
   265             ML_Context.eval_source (ML_Compiler.verbose true ML_Compiler.flags) source))) #>
   266           Proof.propagate_ml_env)));
   267 
   268 val _ =
   269   Outer_Syntax.command @{command_keyword ML_val} "diagnostic ML text"
   270     (Parse.ML_source >> Isar_Cmd.ml_diag true);
   271 
   272 val _ =
   273   Outer_Syntax.command @{command_keyword ML_command} "diagnostic ML text (silent)"
   274     (Parse.ML_source >> Isar_Cmd.ml_diag false);
   275 
   276 val _ =
   277   Outer_Syntax.command @{command_keyword setup} "ML setup for global theory"
   278     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.setup));
   279 
   280 val _ =
   281   Outer_Syntax.local_theory @{command_keyword local_setup} "ML setup for local theory"
   282     (Parse.ML_source >> Isar_Cmd.local_setup);
   283 
   284 val _ =
   285   Outer_Syntax.local_theory @{command_keyword attribute_setup} "define attribute in ML"
   286     (Parse.position Parse.name --
   287         Parse.!!! (@{keyword "="} |-- Parse.ML_source -- Scan.optional Parse.text "")
   288       >> (fn (name, (txt, cmt)) => Attrib.attribute_setup name txt cmt));
   289 
   290 val _ =
   291   Outer_Syntax.local_theory @{command_keyword method_setup} "define proof method in ML"
   292     (Parse.position Parse.name --
   293         Parse.!!! (@{keyword "="} |-- Parse.ML_source -- Scan.optional Parse.text "")
   294       >> (fn (name, (txt, cmt)) => Method.method_setup name txt cmt));
   295 
   296 val _ =
   297   Outer_Syntax.local_theory @{command_keyword declaration} "generic ML declaration"
   298     (Parse.opt_keyword "pervasive" -- Parse.ML_source
   299       >> (fn (pervasive, txt) => Isar_Cmd.declaration {syntax = false, pervasive = pervasive} txt));
   300 
   301 val _ =
   302   Outer_Syntax.local_theory @{command_keyword syntax_declaration} "generic ML syntax declaration"
   303     (Parse.opt_keyword "pervasive" -- Parse.ML_source
   304       >> (fn (pervasive, txt) => Isar_Cmd.declaration {syntax = true, pervasive = pervasive} txt));
   305 
   306 val _ =
   307   Outer_Syntax.local_theory @{command_keyword simproc_setup} "define simproc in ML"
   308     (Parse.position Parse.name --
   309       (@{keyword "("} |-- Parse.enum1 "|" Parse.term --| @{keyword ")"} --| @{keyword "="}) --
   310       Parse.ML_source -- Scan.optional (@{keyword "identifier"} |-- Scan.repeat1 Parse.xname) []
   311     >> (fn (((a, b), c), d) => Isar_Cmd.simproc_setup a b c d));
   312 
   313 
   314 (* translation functions *)
   315 
   316 val _ =
   317   Outer_Syntax.command @{command_keyword parse_ast_translation}
   318     "install parse ast translation functions"
   319     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.parse_ast_translation));
   320 
   321 val _ =
   322   Outer_Syntax.command @{command_keyword parse_translation}
   323     "install parse translation functions"
   324     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.parse_translation));
   325 
   326 val _ =
   327   Outer_Syntax.command @{command_keyword print_translation}
   328     "install print translation functions"
   329     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.print_translation));
   330 
   331 val _ =
   332   Outer_Syntax.command @{command_keyword typed_print_translation}
   333     "install typed print translation functions"
   334     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.typed_print_translation));
   335 
   336 val _ =
   337   Outer_Syntax.command @{command_keyword print_ast_translation}
   338     "install print ast translation functions"
   339     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.print_ast_translation));
   340 
   341 
   342 (* oracles *)
   343 
   344 val _ =
   345   Outer_Syntax.command @{command_keyword oracle} "declare oracle"
   346     (Parse.range Parse.name -- (@{keyword "="} |-- Parse.ML_source) >>
   347       (fn (x, y) => Toplevel.theory (Isar_Cmd.oracle x y)));
   348 
   349 
   350 (* bundled declarations *)
   351 
   352 val _ =
   353   Outer_Syntax.local_theory @{command_keyword bundle} "define bundle of declarations"
   354     ((Parse.binding --| @{keyword "="}) -- Parse.xthms1 -- Parse.for_fixes
   355       >> (uncurry Bundle.bundle_cmd));
   356 
   357 val _ =
   358   Outer_Syntax.command @{command_keyword include}
   359     "include declarations from bundle in proof body"
   360     (Scan.repeat1 (Parse.position Parse.xname) >> (Toplevel.proof o Bundle.include_cmd));
   361 
   362 val _ =
   363   Outer_Syntax.command @{command_keyword including}
   364     "include declarations from bundle in goal refinement"
   365     (Scan.repeat1 (Parse.position Parse.xname) >> (Toplevel.proof o Bundle.including_cmd));
   366 
   367 val _ =
   368   Outer_Syntax.command @{command_keyword print_bundles}
   369     "print bundles of declarations"
   370     (Parse.opt_bang >> (fn b => Toplevel.keep (Bundle.print_bundles b o Toplevel.context_of)));
   371 
   372 
   373 (* local theories *)
   374 
   375 val _ =
   376   Outer_Syntax.command @{command_keyword context} "begin local theory context"
   377     ((Parse.position Parse.xname >> (fn name =>
   378         Toplevel.begin_local_theory true (Named_Target.begin name)) ||
   379       Scan.optional Parse_Spec.includes [] -- Scan.repeat Parse_Spec.context_element
   380         >> (fn (incls, elems) => Toplevel.open_target (#2 o Bundle.context_cmd incls elems)))
   381       --| Parse.begin);
   382 
   383 
   384 (* locales *)
   385 
   386 val locale_val =
   387   Parse_Spec.locale_expression --
   388     Scan.optional (@{keyword "+"} |-- Parse.!!! (Scan.repeat1 Parse_Spec.context_element)) [] ||
   389   Scan.repeat1 Parse_Spec.context_element >> pair ([], []);
   390 
   391 val _ =
   392   Outer_Syntax.command @{command_keyword locale} "define named specification context"
   393     (Parse.binding --
   394       Scan.optional (@{keyword "="} |-- Parse.!!! locale_val) (([], []), []) -- Parse.opt_begin
   395       >> (fn ((name, (expr, elems)), begin) =>
   396           Toplevel.begin_local_theory begin
   397             (Expression.add_locale_cmd name Binding.empty expr elems #> snd)));
   398 
   399 val _ =
   400   Outer_Syntax.command @{command_keyword experiment} "open private specification context"
   401     (Scan.repeat Parse_Spec.context_element --| Parse.begin
   402       >> (fn elems =>
   403           Toplevel.begin_local_theory true (Experiment.experiment_cmd elems #> snd)));
   404 
   405 val interpretation_args =
   406   Parse.!!! Parse_Spec.locale_expression --
   407     Scan.optional
   408       (@{keyword "rewrites"} |-- Parse.and_list1 (Parse_Spec.opt_thm_name ":" -- Parse.prop)) [];
   409 
   410 val _ =
   411   Outer_Syntax.command @{command_keyword sublocale}
   412     "prove sublocale relation between a locale and a locale expression"
   413     ((Parse.position Parse.xname --| (@{keyword "\<subseteq>"} || @{keyword "<"}) --
   414       interpretation_args >> (fn (loc, (expr, equations)) =>
   415         Toplevel.theory_to_proof (Interpretation.sublocale_global_cmd loc expr equations)))
   416     || interpretation_args >> (fn (expr, equations) =>
   417         Toplevel.local_theory_to_proof NONE NONE (Interpretation.sublocale_cmd expr equations)));
   418 
   419 val _ =
   420   Outer_Syntax.command @{command_keyword interpretation}
   421     "prove interpretation of locale expression in local theory"
   422     (interpretation_args >> (fn (expr, equations) =>
   423       Toplevel.local_theory_to_proof NONE NONE (Interpretation.interpretation_cmd expr equations)));
   424 
   425 val _ =
   426   Outer_Syntax.command @{command_keyword interpret}
   427     "prove interpretation of locale expression in proof context"
   428     (interpretation_args >> (fn (expr, equations) =>
   429       Toplevel.proof' (Interpretation.interpret_cmd expr equations)));
   430 
   431 
   432 (* classes *)
   433 
   434 val class_val =
   435   Parse_Spec.class_expression --
   436     Scan.optional (@{keyword "+"} |-- Parse.!!! (Scan.repeat1 Parse_Spec.context_element)) [] ||
   437   Scan.repeat1 Parse_Spec.context_element >> pair [];
   438 
   439 val _ =
   440   Outer_Syntax.command @{command_keyword class} "define type class"
   441    (Parse.binding -- Scan.optional (@{keyword "="} |-- class_val) ([], []) -- Parse.opt_begin
   442     >> (fn ((name, (supclasses, elems)), begin) =>
   443         Toplevel.begin_local_theory begin
   444           (Class_Declaration.class_cmd name supclasses elems #> snd)));
   445 
   446 val _ =
   447   Outer_Syntax.local_theory_to_proof @{command_keyword subclass} "prove a subclass relation"
   448     (Parse.class >> Class_Declaration.subclass_cmd);
   449 
   450 val _ =
   451   Outer_Syntax.command @{command_keyword instantiation} "instantiate and prove type arity"
   452    (Parse.multi_arity --| Parse.begin
   453      >> (fn arities => Toplevel.begin_local_theory true (Class.instantiation_cmd arities)));
   454 
   455 val _ =
   456   Outer_Syntax.command @{command_keyword instance} "prove type arity or subclass relation"
   457   ((Parse.class --
   458     ((@{keyword "\<subseteq>"} || @{keyword "<"}) |-- Parse.!!! Parse.class) >> Class.classrel_cmd ||
   459     Parse.multi_arity >> Class.instance_arity_cmd) >> Toplevel.theory_to_proof ||
   460     Scan.succeed (Toplevel.local_theory_to_proof NONE NONE (Class.instantiation_instance I)));
   461 
   462 
   463 (* arbitrary overloading *)
   464 
   465 val _ =
   466   Outer_Syntax.command @{command_keyword overloading} "overloaded definitions"
   467    (Scan.repeat1 (Parse.name --| (@{keyword "\<equiv>"} || @{keyword "=="}) -- Parse.term --
   468       Scan.optional (@{keyword "("} |-- (@{keyword "unchecked"} >> K false) --| @{keyword ")"}) true
   469       >> Scan.triple1) --| Parse.begin
   470    >> (fn operations => Toplevel.begin_local_theory true (Overloading.overloading_cmd operations)));
   471 
   472 
   473 (* code generation *)
   474 
   475 val _ =
   476   Outer_Syntax.command @{command_keyword code_datatype}
   477     "define set of code datatype constructors"
   478     (Scan.repeat1 Parse.term >> (Toplevel.theory o Code.add_datatype_cmd));
   479 
   480 
   481 
   482 (** proof commands **)
   483 
   484 val _ =
   485   Outer_Syntax.local_theory_to_proof @{command_keyword notepad} "begin proof context"
   486     (Parse.begin >> K Proof.begin_notepad);
   487 
   488 
   489 (* statements *)
   490 
   491 val structured_statement =
   492   Parse_Spec.statement -- Parse_Spec.cond_statement -- Parse.for_fixes
   493     >> (fn ((shows, (strict, assumes)), fixes) => (strict, fixes, assumes, shows));
   494 
   495 val structured_statement' =
   496   Parse_Spec.statement -- Parse_Spec.if_statement' -- Parse.for_fixes
   497     >> (fn ((shows, assumes), fixes) => (fixes, assumes, shows));
   498 
   499 
   500 fun theorem spec schematic descr =
   501   Outer_Syntax.local_theory_to_proof' spec
   502     ("state " ^ descr)
   503     (Scan.optional (Parse_Spec.opt_thm_name ":" --|
   504       Scan.ahead (Parse_Spec.includes >> K "" ||
   505         Parse_Spec.locale_keyword || Parse_Spec.statement_keyword)) Attrib.empty_binding --
   506       Scan.optional Parse_Spec.includes [] --
   507       Parse_Spec.general_statement >> (fn ((a, includes), (elems, concl)) =>
   508         ((if schematic then Specification.schematic_theorem_cmd else Specification.theorem_cmd)
   509           Thm.theoremK NONE (K I) a includes elems concl)));
   510 
   511 val _ = theorem @{command_keyword theorem} false "theorem";
   512 val _ = theorem @{command_keyword lemma} false "lemma";
   513 val _ = theorem @{command_keyword corollary} false "corollary";
   514 val _ = theorem @{command_keyword proposition} false "proposition";
   515 val _ = theorem @{command_keyword schematic_goal} true "schematic goal";
   516 
   517 
   518 val _ =
   519   Outer_Syntax.command @{command_keyword have} "state local goal"
   520     (structured_statement >> (fn (a, b, c, d) =>
   521       Toplevel.proof' (fn int => Proof.have_cmd a NONE (K I) b c d int #> #2)));
   522 
   523 val _ =
   524   Outer_Syntax.command @{command_keyword show} "state local goal, to refine pending subgoals"
   525     (structured_statement >> (fn (a, b, c, d) =>
   526       Toplevel.proof' (fn int => Proof.show_cmd a NONE (K I) b c d int #> #2)));
   527 
   528 val _ =
   529   Outer_Syntax.command @{command_keyword hence} "old-style alias of \"then have\""
   530     (structured_statement >> (fn (a, b, c, d) =>
   531       Toplevel.proof' (fn int => Proof.chain #> Proof.have_cmd a NONE (K I) b c d int #> #2)));
   532 
   533 val _ =
   534   Outer_Syntax.command @{command_keyword thus} "old-style alias of  \"then show\""
   535     (structured_statement >> (fn (a, b, c, d) =>
   536       Toplevel.proof' (fn int => Proof.chain #> Proof.show_cmd a NONE (K I) b c d int #> #2)));
   537 
   538 
   539 (* facts *)
   540 
   541 val facts = Parse.and_list1 Parse.xthms1;
   542 
   543 val _ =
   544   Outer_Syntax.command @{command_keyword then} "forward chaining"
   545     (Scan.succeed (Toplevel.proof Proof.chain));
   546 
   547 val _ =
   548   Outer_Syntax.command @{command_keyword from} "forward chaining from given facts"
   549     (facts >> (Toplevel.proof o Proof.from_thmss_cmd));
   550 
   551 val _ =
   552   Outer_Syntax.command @{command_keyword with} "forward chaining from given and current facts"
   553     (facts >> (Toplevel.proof o Proof.with_thmss_cmd));
   554 
   555 val _ =
   556   Outer_Syntax.command @{command_keyword note} "define facts"
   557     (Parse_Spec.name_facts >> (Toplevel.proof o Proof.note_thmss_cmd));
   558 
   559 val _ =
   560   Outer_Syntax.command @{command_keyword supply} "define facts during goal refinement (unstructured)"
   561     (Parse_Spec.name_facts >> (Toplevel.proof o Proof.supply_cmd));
   562 
   563 val _ =
   564   Outer_Syntax.command @{command_keyword using} "augment goal facts"
   565     (facts >> (Toplevel.proof o Proof.using_cmd));
   566 
   567 val _ =
   568   Outer_Syntax.command @{command_keyword unfolding} "unfold definitions in goal and facts"
   569     (facts >> (Toplevel.proof o Proof.unfolding_cmd));
   570 
   571 
   572 (* proof context *)
   573 
   574 val _ =
   575   Outer_Syntax.command @{command_keyword fix} "fix local variables (Skolem constants)"
   576     (Parse.fixes >> (Toplevel.proof o Proof.fix_cmd));
   577 
   578 val _ =
   579   Outer_Syntax.command @{command_keyword assume} "assume propositions"
   580     (structured_statement' >> (fn (a, b, c) => Toplevel.proof (Proof.assume_cmd a b c)));
   581 
   582 val _ =
   583   Outer_Syntax.command @{command_keyword presume} "assume propositions, to be established later"
   584     (structured_statement' >> (fn (a, b, c) => Toplevel.proof (Proof.presume_cmd a b c)));
   585 
   586 val _ =
   587   Outer_Syntax.command @{command_keyword 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.proof o Proof.def_cmd));
   593 
   594 val _ =
   595   Outer_Syntax.command @{command_keyword consider} "state cases rule"
   596     (Parse_Spec.obtains >> (Toplevel.proof' o Obtain.consider_cmd));
   597 
   598 val _ =
   599   Outer_Syntax.command @{command_keyword obtain} "generalized elimination"
   600     (Parse.parbinding -- Scan.optional (Parse.fixes --| Parse.where_) [] -- Parse_Spec.statement
   601       >> (fn ((x, y), z) => Toplevel.proof' (Obtain.obtain_cmd x y z)));
   602 
   603 val _ =
   604   Outer_Syntax.command @{command_keyword guess} "wild guessing (unstructured)"
   605     (Scan.optional Parse.fixes [] >> (Toplevel.proof' o Obtain.guess_cmd));
   606 
   607 val _ =
   608   Outer_Syntax.command @{command_keyword let} "bind text variables"
   609     (Parse.and_list1 (Parse.and_list1 Parse.term -- (@{keyword "="} |-- Parse.term))
   610       >> (Toplevel.proof o Proof.let_bind_cmd));
   611 
   612 val _ =
   613   Outer_Syntax.command @{command_keyword write} "add concrete syntax for constants / fixed variables"
   614     (opt_mode -- Parse.and_list1 (Parse.const -- Parse.mixfix)
   615     >> (fn (mode, args) => Toplevel.proof (Proof.write_cmd mode args)));
   616 
   617 val _ =
   618   Outer_Syntax.command @{command_keyword case} "invoke local context"
   619     (Parse_Spec.opt_thm_name ":" --
   620       (@{keyword "("} |--
   621         Parse.!!! (Parse.position Parse.xname -- Scan.repeat (Parse.maybe Parse.binding)
   622           --| @{keyword ")"}) ||
   623         Parse.position Parse.xname >> rpair []) >> (Toplevel.proof o Proof.case_cmd));
   624 
   625 
   626 (* proof structure *)
   627 
   628 val _ =
   629   Outer_Syntax.command @{command_keyword "{"} "begin explicit proof block"
   630     (Scan.succeed (Toplevel.proof Proof.begin_block));
   631 
   632 val _ =
   633   Outer_Syntax.command @{command_keyword "}"} "end explicit proof block"
   634     (Scan.succeed (Toplevel.proof Proof.end_block));
   635 
   636 val _ =
   637   Outer_Syntax.command @{command_keyword next} "enter next proof block"
   638     (Scan.succeed (Toplevel.proof Proof.next_block));
   639 
   640 
   641 (* end proof *)
   642 
   643 val _ =
   644   Outer_Syntax.command @{command_keyword qed} "conclude proof"
   645     (Scan.option Method.parse >> (fn m =>
   646      (Option.map Method.report m;
   647       Isar_Cmd.qed m)));
   648 
   649 val _ =
   650   Outer_Syntax.command @{command_keyword by} "terminal backward proof"
   651     (Method.parse -- Scan.option Method.parse >> (fn (m1, m2) =>
   652      (Method.report m1;
   653       Option.map Method.report m2;
   654       Isar_Cmd.terminal_proof (m1, m2))));
   655 
   656 val _ =
   657   Outer_Syntax.command @{command_keyword ".."} "default proof"
   658     (Scan.succeed Isar_Cmd.default_proof);
   659 
   660 val _ =
   661   Outer_Syntax.command @{command_keyword "."} "immediate proof"
   662     (Scan.succeed Isar_Cmd.immediate_proof);
   663 
   664 val _ =
   665   Outer_Syntax.command @{command_keyword done} "done proof"
   666     (Scan.succeed Isar_Cmd.done_proof);
   667 
   668 val _ =
   669   Outer_Syntax.command @{command_keyword sorry} "skip proof (quick-and-dirty mode only!)"
   670     (Scan.succeed Isar_Cmd.skip_proof);
   671 
   672 val _ =
   673   Outer_Syntax.command @{command_keyword oops} "forget proof"
   674     (Scan.succeed (Toplevel.forget_proof true));
   675 
   676 
   677 (* proof steps *)
   678 
   679 val _ =
   680   Outer_Syntax.command @{command_keyword defer} "shuffle internal proof state"
   681     (Scan.optional Parse.nat 1 >> (Toplevel.proof o Proof.defer));
   682 
   683 val _ =
   684   Outer_Syntax.command @{command_keyword prefer} "shuffle internal proof state"
   685     (Parse.nat >> (Toplevel.proof o Proof.prefer));
   686 
   687 val _ =
   688   Outer_Syntax.command @{command_keyword apply} "initial goal refinement step (unstructured)"
   689     (Method.parse >> (fn m => (Method.report m; Toplevel.proofs (Proof.apply_results m))));
   690 
   691 val _ =
   692   Outer_Syntax.command @{command_keyword apply_end} "terminal goal refinement step (unstructured)"
   693     (Method.parse >> (fn m => (Method.report m; Toplevel.proofs (Proof.apply_end_results m))));
   694 
   695 val _ =
   696   Outer_Syntax.command @{command_keyword proof} "backward proof step"
   697     (Scan.option Method.parse >> (fn m =>
   698       (Option.map Method.report m; Toplevel.proofs (Proof.proof_results m))));
   699 
   700 
   701 (* subgoal focus *)
   702 
   703 local
   704 
   705 val opt_fact_binding =
   706   Scan.optional (Parse.binding -- Parse.opt_attribs || Parse.attribs >> pair Binding.empty)
   707     Attrib.empty_binding;
   708 
   709 val for_params =
   710   Scan.optional
   711     (@{keyword "for"} |--
   712       Parse.!!! ((Scan.option Parse.dots >> is_some) --
   713         (Scan.repeat1 (Parse.position (Parse.maybe Parse.name)))))
   714     (false, []);
   715 
   716 in
   717 
   718 val _ =
   719   Outer_Syntax.command @{command_keyword subgoal}
   720     "focus on first subgoal within backward refinement"
   721     (opt_fact_binding -- (Scan.option (@{keyword "premises"} |-- Parse.!!! opt_fact_binding)) --
   722       for_params >> (fn ((a, b), c) =>
   723         Toplevel.proofs (Seq.make_results o Seq.single o #2 o Subgoal.subgoal_cmd a b c)));
   724 
   725 end;
   726 
   727 
   728 (* proof navigation *)
   729 
   730 fun report_back () =
   731   Output.report [Markup.markup Markup.bad "Explicit backtracking"];
   732 
   733 val _ =
   734   Outer_Syntax.command @{command_keyword back} "explicit backtracking of proof command"
   735     (Scan.succeed
   736      (Toplevel.actual_proof (fn prf => (report_back (); Proof_Node.back prf)) o
   737       Toplevel.skip_proof report_back));
   738 
   739 
   740 
   741 (** diagnostic commands (for interactive mode only) **)
   742 
   743 val opt_modes =
   744   Scan.optional (@{keyword "("} |-- Parse.!!! (Scan.repeat1 Parse.xname --| @{keyword ")"})) [];
   745 
   746 val _ =
   747   Outer_Syntax.command @{command_keyword help}
   748     "retrieve outer syntax commands according to name patterns"
   749     (Scan.repeat Parse.name >>
   750       (fn pats => Toplevel.keep (fn st => Outer_Syntax.help (Toplevel.theory_of st) pats)));
   751 
   752 val _ =
   753   Outer_Syntax.command @{command_keyword print_commands} "print outer syntax commands"
   754     (Scan.succeed (Toplevel.keep (Outer_Syntax.print_commands o Toplevel.theory_of)));
   755 
   756 val _ =
   757   Outer_Syntax.command @{command_keyword print_options} "print configuration options"
   758     (Parse.opt_bang >> (fn b => Toplevel.keep (Attrib.print_options b o Toplevel.context_of)));
   759 
   760 val _ =
   761   Outer_Syntax.command @{command_keyword print_context}
   762     "print context of local theory target"
   763     (Scan.succeed (Toplevel.keep (Pretty.writeln_chunks o Toplevel.pretty_context)));
   764 
   765 val _ =
   766   Outer_Syntax.command @{command_keyword print_theory}
   767     "print logical theory contents"
   768     (Parse.opt_bang >> (fn b =>
   769       Toplevel.keep (Pretty.writeln o Proof_Display.pretty_theory b o Toplevel.context_of)));
   770 
   771 val _ =
   772   Outer_Syntax.command @{command_keyword print_definitions}
   773     "print dependencies of definitional theory content"
   774     (Parse.opt_bang >> (fn b =>
   775       Toplevel.keep (Pretty.writeln o Proof_Display.pretty_definitions b o Toplevel.context_of)));
   776 
   777 val _ =
   778   Outer_Syntax.command @{command_keyword print_syntax}
   779     "print inner syntax of context"
   780     (Scan.succeed (Toplevel.keep (Proof_Context.print_syntax o Toplevel.context_of)));
   781 
   782 val _ =
   783   Outer_Syntax.command @{command_keyword print_defn_rules}
   784     "print definitional rewrite rules of context"
   785     (Scan.succeed (Toplevel.keep (Local_Defs.print_rules o Toplevel.context_of)));
   786 
   787 val _ =
   788   Outer_Syntax.command @{command_keyword print_abbrevs}
   789     "print constant abbreviations of context"
   790     (Parse.opt_bang >> (fn b =>
   791       Toplevel.keep (Proof_Context.print_abbrevs b o Toplevel.context_of)));
   792 
   793 val _ =
   794   Outer_Syntax.command @{command_keyword print_theorems}
   795     "print theorems of local theory or proof context"
   796     (Parse.opt_bang >> (fn b =>
   797       Toplevel.keep (Pretty.writeln o Pretty.chunks o Isar_Cmd.pretty_theorems b)));
   798 
   799 val _ =
   800   Outer_Syntax.command @{command_keyword print_locales}
   801     "print locales of this theory"
   802     (Parse.opt_bang >> (fn b =>
   803       Toplevel.keep (Locale.print_locales b o Toplevel.theory_of)));
   804 
   805 val _ =
   806   Outer_Syntax.command @{command_keyword print_classes}
   807     "print classes of this theory"
   808     (Scan.succeed (Toplevel.keep (Class.print_classes o Toplevel.context_of)));
   809 
   810 val _ =
   811   Outer_Syntax.command @{command_keyword print_locale}
   812     "print locale of this theory"
   813     (Parse.opt_bang -- Parse.position Parse.xname >> (fn (b, name) =>
   814       Toplevel.keep (fn state => Locale.print_locale (Toplevel.theory_of state) b name)));
   815 
   816 val _ =
   817   Outer_Syntax.command @{command_keyword print_interps}
   818     "print interpretations of locale for this theory or proof context"
   819     (Parse.position Parse.xname >> (fn name =>
   820       Toplevel.keep (fn state => Locale.print_registrations (Toplevel.context_of state) name)));
   821 
   822 val _ =
   823   Outer_Syntax.command @{command_keyword print_dependencies}
   824     "print dependencies of locale expression"
   825     (Parse.opt_bang -- Parse_Spec.locale_expression >> (fn (b, expr) =>
   826       Toplevel.keep (fn state => Expression.print_dependencies (Toplevel.context_of state) b expr)));
   827 
   828 val _ =
   829   Outer_Syntax.command @{command_keyword print_attributes}
   830     "print attributes of this theory"
   831     (Parse.opt_bang >> (fn b => Toplevel.keep (Attrib.print_attributes b o Toplevel.context_of)));
   832 
   833 val _ =
   834   Outer_Syntax.command @{command_keyword print_simpset}
   835     "print context of Simplifier"
   836     (Parse.opt_bang >> (fn b =>
   837       Toplevel.keep (Pretty.writeln o Simplifier.pretty_simpset b o Toplevel.context_of)));
   838 
   839 val _ =
   840   Outer_Syntax.command @{command_keyword print_rules} "print intro/elim rules"
   841     (Scan.succeed (Toplevel.keep (Context_Rules.print_rules o Toplevel.context_of)));
   842 
   843 val _ =
   844   Outer_Syntax.command @{command_keyword print_methods} "print methods of this theory"
   845     (Parse.opt_bang >> (fn b => Toplevel.keep (Method.print_methods b o Toplevel.context_of)));
   846 
   847 val _ =
   848   Outer_Syntax.command @{command_keyword print_antiquotations}
   849     "print document antiquotations"
   850     (Parse.opt_bang >> (fn b =>
   851       Toplevel.keep (Thy_Output.print_antiquotations b o Toplevel.context_of)));
   852 
   853 val _ =
   854   Outer_Syntax.command @{command_keyword print_ML_antiquotations}
   855     "print ML antiquotations"
   856     (Parse.opt_bang >> (fn b =>
   857       Toplevel.keep (ML_Context.print_antiquotations b o Toplevel.context_of)));
   858 
   859 val _ =
   860   Outer_Syntax.command @{command_keyword locale_deps} "visualize locale dependencies"
   861     (Scan.succeed
   862       (Toplevel.keep (Toplevel.theory_of #> (fn thy =>
   863         Locale.pretty_locale_deps thy
   864         |> map (fn {name, parents, body} =>
   865           ((name, Graph_Display.content_node (Locale.extern thy name) [body]), parents))
   866         |> Graph_Display.display_graph_old))));
   867 
   868 val _ =
   869   Outer_Syntax.command @{command_keyword print_term_bindings}
   870     "print term bindings of proof context"
   871     (Scan.succeed
   872       (Toplevel.keep
   873         (Pretty.writeln_chunks o Proof_Context.pretty_term_bindings o Toplevel.context_of)));
   874 
   875 val _ =
   876   Outer_Syntax.command @{command_keyword print_facts} "print facts of proof context"
   877     (Parse.opt_bang >> (fn b =>
   878       Toplevel.keep (Proof_Context.print_local_facts b o Toplevel.context_of)));
   879 
   880 val _ =
   881   Outer_Syntax.command @{command_keyword print_cases} "print cases of proof context"
   882     (Scan.succeed
   883       (Toplevel.keep (Pretty.writeln_chunks o Proof_Context.pretty_cases o Toplevel.context_of)));
   884 
   885 val _ =
   886   Outer_Syntax.command @{command_keyword print_statement}
   887     "print theorems as long statements"
   888     (opt_modes -- Parse.xthms1 >> Isar_Cmd.print_stmts);
   889 
   890 val _ =
   891   Outer_Syntax.command @{command_keyword thm} "print theorems"
   892     (opt_modes -- Parse.xthms1 >> Isar_Cmd.print_thms);
   893 
   894 val _ =
   895   Outer_Syntax.command @{command_keyword prf} "print proof terms of theorems"
   896     (opt_modes -- Scan.option Parse.xthms1 >> Isar_Cmd.print_prfs false);
   897 
   898 val _ =
   899   Outer_Syntax.command @{command_keyword full_prf} "print full proof terms of theorems"
   900     (opt_modes -- Scan.option Parse.xthms1 >> Isar_Cmd.print_prfs true);
   901 
   902 val _ =
   903   Outer_Syntax.command @{command_keyword prop} "read and print proposition"
   904     (opt_modes -- Parse.term >> Isar_Cmd.print_prop);
   905 
   906 val _ =
   907   Outer_Syntax.command @{command_keyword term} "read and print term"
   908     (opt_modes -- Parse.term >> Isar_Cmd.print_term);
   909 
   910 val _ =
   911   Outer_Syntax.command @{command_keyword typ} "read and print type"
   912     (opt_modes -- (Parse.typ -- Scan.option (@{keyword "::"} |-- Parse.!!! Parse.sort))
   913       >> Isar_Cmd.print_type);
   914 
   915 val _ =
   916   Outer_Syntax.command @{command_keyword print_codesetup} "print code generator setup"
   917     (Scan.succeed (Toplevel.keep (Code.print_codesetup o Toplevel.theory_of)));
   918 
   919 val _ =
   920   Outer_Syntax.command @{command_keyword print_state}
   921     "print current proof state (if present)"
   922     (opt_modes >> (fn modes =>
   923       Toplevel.keep (Print_Mode.with_modes modes (Output.state o Toplevel.string_of_state))));
   924 
   925 val _ =
   926   Outer_Syntax.command @{command_keyword welcome} "print welcome message"
   927     (Scan.succeed (Toplevel.keep (fn _ => writeln (Session.welcome ()))));
   928 
   929 val _ =
   930   Outer_Syntax.command @{command_keyword display_drafts}
   931     "display raw source files with symbols"
   932     (Scan.repeat1 Parse.path >> (fn names =>
   933       Toplevel.keep (fn _ => ignore (Present.display_drafts (map Path.explode names)))));
   934 
   935 
   936 
   937 (** extraction of programs from proofs **)
   938 
   939 val parse_vars = Scan.optional (Parse.$$$ "(" |-- Parse.list1 Parse.name --| Parse.$$$ ")") [];
   940 
   941 val _ =
   942   Outer_Syntax.command @{command_keyword realizers}
   943     "specify realizers for primitive axioms / theorems, together with correctness proof"
   944     (Scan.repeat1 (Parse.xname -- parse_vars --| Parse.$$$ ":" -- Parse.string -- Parse.string) >>
   945      (fn xs => Toplevel.theory (fn thy => Extraction.add_realizers
   946        (map (fn (((a, vs), s1), s2) => (Global_Theory.get_thm thy a, (vs, s1, s2))) xs) thy)));
   947 
   948 val _ =
   949   Outer_Syntax.command @{command_keyword realizability}
   950     "add equations characterizing realizability"
   951     (Scan.repeat1 Parse.string >> (Toplevel.theory o Extraction.add_realizes_eqns));
   952 
   953 val _ =
   954   Outer_Syntax.command @{command_keyword extract_type}
   955     "add equations characterizing type of extracted program"
   956     (Scan.repeat1 Parse.string >> (Toplevel.theory o Extraction.add_typeof_eqns));
   957 
   958 val _ =
   959   Outer_Syntax.command @{command_keyword extract} "extract terms from proofs"
   960     (Scan.repeat1 (Parse.xname -- parse_vars) >> (fn xs => Toplevel.theory (fn thy =>
   961       Extraction.extract (map (apfst (Global_Theory.get_thm thy)) xs) thy)));
   962 
   963 
   964 
   965 (** end **)
   966 
   967 val _ =
   968   Outer_Syntax.command @{command_keyword end} "end context"
   969     (Scan.succeed
   970       (Toplevel.exit o Toplevel.end_local_theory o Toplevel.close_target o
   971         Toplevel.end_proof (K Proof.end_notepad)));
   972 
   973 end;