
wenzelm@48929: (*  Title:      Pure/Pure.thy
wenzelm@48929:     Author:     Makarius
wenzelm@48929: 
wenzelm@62944: The Pure theory, with definitions of Isar commands and some lemmas.
wenzelm@48929: *)
wenzelm@48929: 
wenzelm@48638: theory Pure
wenzelm@62859: keywords
wenzelm@67446:     "!!" "!" "+" ":" ";" "<" "<=" "==" "=>" "?" "[" "\<comment>" "\<equiv>" "\<leftharpoondown>" "\<rightharpoonup>" "\<rightleftharpoons>"
wenzelm@66194:     "\<subseteq>" "]" "attach" "binder" "in" "infix" "infixl" "infixr" "is" "open" "output"
wenzelm@66194:     "overloaded" "pervasive" "premises" "structure" "unchecked"
wenzelm@63441:   and "private" "qualified" :: before_command
ballarin@67740:   and "assumes" "constrains" "defines" "fixes" "for" "if" "includes" "notes" "rewrites"
wenzelm@66194:     "obtains" "shows" "when" "where" "|" :: quasi_command
wenzelm@58999:   and "text" "txt" :: document_body
wenzelm@58999:   and "text_raw" :: document_raw
wenzelm@63579:   and "default_sort" :: thy_decl
wenzelm@57506:   and "typedecl" "type_synonym" "nonterminal" "judgment"
wenzelm@62169:     "consts" "syntax" "no_syntax" "translations" "no_translations"
wenzelm@55385:     "definition" "abbreviation" "type_notation" "no_type_notation" "notation"
wenzelm@66248:     "no_notation" "axiomatization" "alias" "type_alias" "lemmas" "declare"
wenzelm@48641:     "hide_class" "hide_type" "hide_const" "hide_fact" :: thy_decl
wenzelm@67281:   and "external_file" "bibtex_file" :: thy_load
wenzelm@62849:   and "ML_file" "ML_file_debug" "ML_file_no_debug" :: thy_load % "ML"
wenzelm@60957:   and "SML_file" "SML_file_debug" "SML_file_no_debug" :: thy_load % "ML"
wenzelm@68276:   and "SML_import" "SML_export" "ML_export" :: thy_decl % "ML"
wenzelm@48641:   and "ML_prf" :: prf_decl % "proof"  (* FIXME % "ML" ?? *)
wenzelm@48641:   and "ML_val" "ML_command" :: diag % "ML"
wenzelm@63579:   and "simproc_setup" :: thy_decl % "ML"
wenzelm@48641:   and "setup" "local_setup" "attribute_setup" "method_setup"
wenzelm@55762:     "declaration" "syntax_declaration"
wenzelm@48641:     "parse_ast_translation" "parse_translation" "print_translation"
wenzelm@48641:     "typed_print_translation" "print_ast_translation" "oracle" :: thy_decl % "ML"
wenzelm@63273:   and "bundle" :: thy_decl_block
wenzelm@63282:   and "unbundle" :: thy_decl
wenzelm@48641:   and "include" "including" :: prf_decl
wenzelm@48641:   and "print_bundles" :: diag
wenzelm@59901:   and "context" "locale" "experiment" :: thy_decl_block
wenzelm@51224:   and "interpret" :: prf_goal % "proof"
haftmann@61890:   and "interpretation" "global_interpretation" "sublocale" :: thy_goal
wenzelm@58800:   and "class" :: thy_decl_block
wenzelm@48641:   and "subclass" :: thy_goal
wenzelm@58800:   and "instantiation" :: thy_decl_block
wenzelm@48641:   and "instance" :: thy_goal
wenzelm@58800:   and "overloading" :: thy_decl_block
wenzelm@48641:   and "code_datatype" :: thy_decl
wenzelm@61338:   and "theorem" "lemma" "corollary" "proposition" :: thy_goal
wenzelm@61337:   and "schematic_goal" :: thy_goal
wenzelm@58800:   and "notepad" :: thy_decl_block
wenzelm@50128:   and "have" :: prf_goal % "proof"
wenzelm@63579:   and "hence" :: prf_goal % "proof"
wenzelm@50128:   and "show" :: prf_asm_goal % "proof"
wenzelm@63579:   and "thus" :: prf_asm_goal % "proof"
wenzelm@48641:   and "then" "from" "with" :: prf_chain % "proof"
wenzelm@60371:   and "note" :: prf_decl % "proof"
wenzelm@60371:   and "supply" :: prf_script % "proof"
wenzelm@60371:   and "using" "unfolding" :: prf_decl % "proof"
wenzelm@67119:   and "fix" "assume" "presume" "define" :: prf_asm % "proof"
wenzelm@60448:   and "consider" :: prf_goal % "proof"
wenzelm@53371:   and "obtain" :: prf_asm_goal % "proof"
wenzelm@60624:   and "guess" :: prf_script_asm_goal % "proof"
wenzelm@48641:   and "let" "write" :: prf_decl % "proof"
wenzelm@48641:   and "case" :: prf_asm % "proof"
wenzelm@48641:   and "{" :: prf_open % "proof"
wenzelm@48641:   and "}" :: prf_close % "proof"
wenzelm@60694:   and "next" :: next_block % "proof"
wenzelm@48641:   and "qed" :: qed_block % "proof"
wenzelm@62312:   and "by" ".." "." "sorry" "\<proof>" :: "qed" % "proof"
wenzelm@53571:   and "done" :: "qed_script" % "proof"
wenzelm@48641:   and "oops" :: qed_global % "proof"
wenzelm@50128:   and "defer" "prefer" "apply" :: prf_script % "proof"
wenzelm@63579:   and "apply_end" :: prf_script % "proof"
wenzelm@60624:   and "subgoal" :: prf_script_goal % "proof"
wenzelm@48641:   and "proof" :: prf_block % "proof"
wenzelm@48641:   and "also" "moreover" :: prf_decl % "proof"
wenzelm@48641:   and "finally" "ultimately" :: prf_chain % "proof"
wenzelm@48641:   and "back" :: prf_script % "proof"
wenzelm@61252:   and "help" "print_commands" "print_options" "print_context" "print_theory"
wenzelm@61252:     "print_definitions" "print_syntax" "print_abbrevs" "print_defn_rules"
wenzelm@48641:     "print_theorems" "print_locales" "print_classes" "print_locale"
wenzelm@48641:     "print_interps" "print_dependencies" "print_attributes"
wenzelm@48641:     "print_simpset" "print_rules" "print_trans_rules" "print_methods"
wenzelm@56069:     "print_antiquotations" "print_ML_antiquotations" "thy_deps"
wenzelm@58845:     "locale_deps" "class_deps" "thm_deps" "print_term_bindings"
wenzelm@57415:     "print_facts" "print_cases" "print_statement" "thm" "prf" "full_prf"
wenzelm@57415:     "prop" "term" "typ" "print_codesetup" "unused_thms" :: diag
wenzelm@67263:   and "print_state" :: diag
wenzelm@48646:   and "welcome" :: diag
wenzelm@68505:   and "end" :: thy_end
wenzelm@63579:   and "realizers" :: thy_decl
wenzelm@63579:   and "realizability" :: thy_decl
wenzelm@56797:   and "extract_type" "extract" :: thy_decl
wenzelm@48646:   and "find_theorems" "find_consts" :: diag
wenzelm@57886:   and "named_theorems" :: thy_decl
wenzelm@67013: abbrevs "===>" = "===>"  (*prevent replacement of very long arrows*)
wenzelm@67013:   and "--->" = "\<midarrow>\<rightarrow>"
wenzelm@67724:   and "hence" "thus" "default_sort" "simproc_setup" "apply_end" "realizers" "realizability" = ""
wenzelm@67013:   and "hence" = "then have"
wenzelm@67013:   and "thus" = "then show"
wenzelm@48638: begin
wenzelm@15803: 
wenzelm@62849: section \<open>Isar commands\<close>
wenzelm@62849: 
wenzelm@67281: subsection \<open>Other files\<close>
wenzelm@66757: 
wenzelm@66757: ML \<open>
wenzelm@67281: local
wenzelm@67281:   val _ =
wenzelm@67281:     Outer_Syntax.command \<^command_keyword>\<open>external_file\<close> "formal dependency on external file"
wenzelm@69262:       (Resources.provide_parse_files "external_file" >> (fn files => Toplevel.theory (#2 o files)));
wenzelm@67281: 
wenzelm@67281:   val _ =
wenzelm@67281:     Outer_Syntax.command \<^command_keyword>\<open>bibtex_file\<close> "check bibtex database file in Prover IDE"
wenzelm@69262:       (Resources.provide_parse_files "bibtex_file" >> (fn files =>
wenzelm@69262:         Toplevel.theory (fn thy =>
wenzelm@67281:           let
wenzelm@69262:             val ([{lines, pos, ...}], thy') = files thy;
wenzelm@69262:             val _ = Bibtex.check_database_output pos (cat_lines lines);
wenzelm@69262:           in thy' end)));
wenzelm@67281: in end\<close>
wenzelm@66757: 
wenzelm@66757: 
wenzelm@62856: subsection \<open>Embedded ML text\<close>
wenzelm@62849: 
wenzelm@62856: ML \<open>
wenzelm@62849: local
wenzelm@62849: 
wenzelm@67147: val semi = Scan.option \<^keyword>\<open>;\<close>;
wenzelm@62902: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>ML_file\<close> "read and evaluate Isabelle/ML file"
wenzelm@62902:     (Resources.parse_files "ML_file" --| semi >> ML_File.ML NONE);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>ML_file_debug\<close>
wenzelm@62849:     "read and evaluate Isabelle/ML file (with debugger information)"
wenzelm@62902:     (Resources.parse_files "ML_file_debug" --| semi >> ML_File.ML (SOME true));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>ML_file_no_debug\<close>
wenzelm@62849:     "read and evaluate Isabelle/ML file (no debugger information)"
wenzelm@62902:     (Resources.parse_files "ML_file_no_debug" --| semi >> ML_File.ML (SOME false));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>SML_file\<close> "read and evaluate Standard ML file"
wenzelm@62902:     (Resources.parse_files "SML_file" --| semi >> ML_File.SML NONE);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>SML_file_debug\<close>
wenzelm@62849:     "read and evaluate Standard ML file (with debugger information)"
wenzelm@62902:     (Resources.parse_files "SML_file_debug" --| semi >> ML_File.SML (SOME true));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>SML_file_no_debug\<close>
wenzelm@62849:     "read and evaluate Standard ML file (no debugger information)"
wenzelm@62902:     (Resources.parse_files "SML_file_no_debug" --| semi >> ML_File.SML (SOME false));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>SML_export\<close> "evaluate SML within Isabelle/ML environment"
wenzelm@62849:     (Parse.ML_source >> (fn source =>
wenzelm@62849:       let
wenzelm@62849:         val flags: ML_Compiler.flags =
wenzelm@68820:           {environment = ML_Env.SML_export, redirect = false, verbose = true,
wenzelm@62902:             debug = NONE, writeln = writeln, warning = warning};
wenzelm@62849:       in
wenzelm@62849:         Toplevel.theory
wenzelm@62849:           (Context.theory_map (ML_Context.exec (fn () => ML_Context.eval_source flags source)))
wenzelm@62849:       end));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>SML_import\<close> "evaluate Isabelle/ML within SML environment"
wenzelm@62849:     (Parse.ML_source >> (fn source =>
wenzelm@62849:       let
wenzelm@62849:         val flags: ML_Compiler.flags =
wenzelm@68820:           {environment = ML_Env.SML_import, redirect = false, verbose = true,
wenzelm@62902:             debug = NONE, writeln = writeln, warning = warning};
wenzelm@62849:       in
wenzelm@62849:         Toplevel.generic_theory
wenzelm@62849:           (ML_Context.exec (fn () => ML_Context.eval_source flags source) #>
wenzelm@62849:             Local_Theory.propagate_ml_env)
wenzelm@62849:       end));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@68276:   Outer_Syntax.command ("ML_export", \<^here>)
wenzelm@68276:     "ML text within theory or local theory, and export to bootstrap environment"
wenzelm@68276:     (Parse.ML_source >> (fn source =>
wenzelm@68276:       Toplevel.generic_theory (fn context =>
wenzelm@68276:         context
wenzelm@68826:         |> Config.put_generic ML_Env.ML_environment ML_Env.Isabelle
wenzelm@68816:         |> Config.put_generic ML_Env.ML_write_global true
wenzelm@68276:         |> ML_Context.exec (fn () =>
wenzelm@68276:             ML_Context.eval_source (ML_Compiler.verbose true ML_Compiler.flags) source)
wenzelm@68827:         |> Config.restore_generic ML_Env.ML_write_global context
wenzelm@68827:         |> Config.restore_generic ML_Env.ML_environment context
wenzelm@68276:         |> Local_Theory.propagate_ml_env)));
wenzelm@68276: 
wenzelm@68276: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>ML_prf\<close> "ML text within proof"
wenzelm@62849:     (Parse.ML_source >> (fn source =>
wenzelm@62849:       Toplevel.proof (Proof.map_context (Context.proof_map
wenzelm@62849:         (ML_Context.exec (fn () =>
wenzelm@62849:             ML_Context.eval_source (ML_Compiler.verbose true ML_Compiler.flags) source))) #>
wenzelm@62849:           Proof.propagate_ml_env)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>ML_val\<close> "diagnostic ML text"
wenzelm@62849:     (Parse.ML_source >> Isar_Cmd.ml_diag true);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>ML_command\<close> "diagnostic ML text (silent)"
wenzelm@62849:     (Parse.ML_source >> Isar_Cmd.ml_diag false);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>setup\<close> "ML setup for global theory"
wenzelm@62849:     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.setup));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>local_setup\<close> "ML setup for local theory"
wenzelm@62849:     (Parse.ML_source >> Isar_Cmd.local_setup);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>oracle\<close> "declare oracle"
wenzelm@67147:     (Parse.range Parse.name -- (\<^keyword>\<open>=\<close> |-- Parse.ML_source) >>
wenzelm@62856:       (fn (x, y) => Toplevel.theory (Isar_Cmd.oracle x y)));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>attribute_setup\<close> "define attribute in ML"
wenzelm@62849:     (Parse.position Parse.name --
wenzelm@67147:         Parse.!!! (\<^keyword>\<open>=\<close> |-- Parse.ML_source -- Scan.optional Parse.text "")
wenzelm@62849:       >> (fn (name, (txt, cmt)) => Attrib.attribute_setup name txt cmt));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>method_setup\<close> "define proof method in ML"
wenzelm@62849:     (Parse.position Parse.name --
wenzelm@67147:         Parse.!!! (\<^keyword>\<open>=\<close> |-- Parse.ML_source -- Scan.optional Parse.text "")
wenzelm@62849:       >> (fn (name, (txt, cmt)) => Method.method_setup name txt cmt));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>declaration\<close> "generic ML declaration"
wenzelm@62849:     (Parse.opt_keyword "pervasive" -- Parse.ML_source
wenzelm@62849:       >> (fn (pervasive, txt) => Isar_Cmd.declaration {syntax = false, pervasive = pervasive} txt));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>syntax_declaration\<close> "generic ML syntax declaration"
wenzelm@62849:     (Parse.opt_keyword "pervasive" -- Parse.ML_source
wenzelm@62849:       >> (fn (pervasive, txt) => Isar_Cmd.declaration {syntax = true, pervasive = pervasive} txt));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>simproc_setup\<close> "define simproc in ML"
wenzelm@62849:     (Parse.position Parse.name --
wenzelm@67147:       (\<^keyword>\<open>(\<close> |-- Parse.enum1 "|" Parse.term --| \<^keyword>\<open>)\<close> --| \<^keyword>\<open>=\<close>) --
wenzelm@62913:       Parse.ML_source >> (fn ((a, b), c) => Isar_Cmd.simproc_setup a b c));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62856: 
wenzelm@62856: subsection \<open>Theory commands\<close>
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Sorts and types\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>default_sort\<close>
wenzelm@62856:     "declare default sort for explicit type variables"
wenzelm@62856:     (Parse.sort >> (fn s => fn lthy => Local_Theory.set_defsort (Syntax.read_sort lthy s) lthy));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>typedecl\<close> "type declaration"
wenzelm@62856:     (Parse.type_args -- Parse.binding -- Parse.opt_mixfix
wenzelm@62856:       >> (fn ((args, a), mx) =>
wenzelm@62856:           Typedecl.typedecl {final = true} (a, map (rpair dummyS) args, mx) #> snd));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>type_synonym\<close> "declare type abbreviation"
wenzelm@62856:     (Parse.type_args -- Parse.binding --
wenzelm@67147:       (\<^keyword>\<open>=\<close> |-- Parse.!!! (Parse.typ -- Parse.opt_mixfix'))
wenzelm@62856:       >> (fn ((args, a), (rhs, mx)) => snd o Typedecl.abbrev_cmd (a, args, mx) rhs));
wenzelm@62856: 
wenzelm@62856: in end\<close>
wenzelm@62856: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Consts\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>judgment\<close> "declare object-logic judgment"
wenzelm@62856:     (Parse.const_binding >> (Toplevel.theory o Object_Logic.add_judgment_cmd));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>consts\<close> "declare constants"
wenzelm@62856:     (Scan.repeat1 Parse.const_binding >> (Toplevel.theory o Sign.add_consts_cmd));
wenzelm@62856: 
wenzelm@62856: in end\<close>
wenzelm@62856: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Syntax and translations\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>nonterminal\<close>
wenzelm@62856:     "declare syntactic type constructors (grammar nonterminal symbols)"
wenzelm@62856:     (Parse.and_list1 Parse.binding >> (Toplevel.theory o Sign.add_nonterminals_global));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>syntax\<close> "add raw syntax clauses"
wenzelm@62856:     (Parse.syntax_mode -- Scan.repeat1 Parse.const_decl
wenzelm@62856:       >> (Toplevel.theory o uncurry Sign.add_syntax_cmd));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>no_syntax\<close> "delete raw syntax clauses"
wenzelm@62856:     (Parse.syntax_mode -- Scan.repeat1 Parse.const_decl
wenzelm@62856:       >> (Toplevel.theory o uncurry Sign.del_syntax_cmd));
wenzelm@62856: 
wenzelm@62856: val trans_pat =
wenzelm@62856:   Scan.optional
wenzelm@67147:     (\<^keyword>\<open>(\<close> |-- Parse.!!! (Parse.inner_syntax Parse.name --| \<^keyword>\<open>)\<close>)) "logic"
wenzelm@62856:     -- Parse.inner_syntax Parse.string;
wenzelm@62856: 
wenzelm@62856: fun trans_arrow toks =
wenzelm@67147:   ((\<^keyword>\<open>\<rightharpoonup>\<close> || \<^keyword>\<open>=>\<close>) >> K Syntax.Parse_Rule ||
wenzelm@67147:     (\<^keyword>\<open>\<leftharpoondown>\<close> || \<^keyword>\<open><=\<close>) >> K Syntax.Print_Rule ||
wenzelm@67147:     (\<^keyword>\<open>\<rightleftharpoons>\<close> || \<^keyword>\<open>==\<close>) >> K Syntax.Parse_Print_Rule) toks;
wenzelm@62856: 
wenzelm@62856: val trans_line =
wenzelm@62856:   trans_pat -- Parse.!!! (trans_arrow -- trans_pat)
wenzelm@62856:     >> (fn (left, (arr, right)) => arr (left, right));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>translations\<close> "add syntax translation rules"
wenzelm@62856:     (Scan.repeat1 trans_line >> (Toplevel.theory o Isar_Cmd.translations));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>no_translations\<close> "delete syntax translation rules"
wenzelm@62856:     (Scan.repeat1 trans_line >> (Toplevel.theory o Isar_Cmd.no_translations));
wenzelm@62856: 
wenzelm@62856: in end\<close>
wenzelm@62856: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Translation functions\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>parse_ast_translation\<close>
wenzelm@62849:     "install parse ast translation functions"
wenzelm@62849:     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.parse_ast_translation));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>parse_translation\<close>
wenzelm@62849:     "install parse translation functions"
wenzelm@62849:     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.parse_translation));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_translation\<close>
wenzelm@62849:     "install print translation functions"
wenzelm@62849:     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.print_translation));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>typed_print_translation\<close>
wenzelm@62849:     "install typed print translation functions"
wenzelm@62849:     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.typed_print_translation));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_ast_translation\<close>
wenzelm@62849:     "install print ast translation functions"
wenzelm@62849:     (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.print_ast_translation));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Specifications\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.local_theory' \<^command_keyword>\<open>definition\<close> "constant definition"
wenzelm@63180:     (Scan.option Parse_Spec.constdecl -- (Parse_Spec.opt_thm_name ":" -- Parse.prop) --
wenzelm@63180:       Parse_Spec.if_assumes -- Parse.for_fixes >> (fn (((decl, spec), prems), params) =>
wenzelm@63180:         #2 oo Specification.definition_cmd decl params prems spec));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.local_theory' \<^command_keyword>\<open>abbreviation\<close> "constant abbreviation"
wenzelm@63180:     (Parse.syntax_mode -- Scan.option Parse_Spec.constdecl -- Parse.prop -- Parse.for_fixes
wenzelm@63180:       >> (fn (((mode, decl), spec), params) => Specification.abbreviation_cmd mode decl params spec));
wenzelm@62856: 
wenzelm@63178: val axiomatization =
wenzelm@63178:   Parse.and_list1 (Parse_Spec.thm_name ":" -- Parse.prop) --
wenzelm@63178:   Parse_Spec.if_assumes -- Parse.for_fixes >> (fn ((a, b), c) => (c, b, a));
wenzelm@63178: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>axiomatization\<close> "axiomatic constant specification"
wenzelm@63285:     (Scan.optional Parse.vars [] --
wenzelm@63178:       Scan.optional (Parse.where_ |-- Parse.!!! axiomatization) ([], [], [])
wenzelm@63178:       >> (fn (a, (b, c, d)) => Toplevel.theory (#2 o Specification.axiomatization_cmd a b c d)));
wenzelm@62856: 
wenzelm@66248: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>alias\<close> "name-space alias for constant"
wenzelm@67147:     (Parse.binding -- (Parse.!!! \<^keyword>\<open>=\<close> |-- Parse.position Parse.name)
wenzelm@66248:       >> Specification.alias_cmd);
wenzelm@66248: 
wenzelm@66248: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>type_alias\<close> "name-space alias for type constructor"
wenzelm@67147:     (Parse.binding -- (Parse.!!! \<^keyword>\<open>=\<close> |-- Parse.position Parse.name)
wenzelm@66248:       >> Specification.type_alias_cmd);
wenzelm@66248: 
wenzelm@62856: in end\<close>
wenzelm@62856: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Notation\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>type_notation\<close>
wenzelm@62856:     "add concrete syntax for type constructors"
wenzelm@62856:     (Parse.syntax_mode -- Parse.and_list1 (Parse.type_const -- Parse.mixfix)
wenzelm@62856:       >> (fn (mode, args) => Specification.type_notation_cmd true mode args));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>no_type_notation\<close>
wenzelm@62856:     "delete concrete syntax for type constructors"
wenzelm@62856:     (Parse.syntax_mode -- Parse.and_list1 (Parse.type_const -- Parse.mixfix)
wenzelm@62856:       >> (fn (mode, args) => Specification.type_notation_cmd false mode args));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>notation\<close>
wenzelm@62856:     "add concrete syntax for constants / fixed variables"
wenzelm@62856:     (Parse.syntax_mode -- Parse.and_list1 (Parse.const -- Parse.mixfix)
wenzelm@62856:       >> (fn (mode, args) => Specification.notation_cmd true mode args));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>no_notation\<close>
wenzelm@62856:     "delete concrete syntax for constants / fixed variables"
wenzelm@62856:     (Parse.syntax_mode -- Parse.and_list1 (Parse.const -- Parse.mixfix)
wenzelm@62856:       >> (fn (mode, args) => Specification.notation_cmd false mode args));
wenzelm@62856: 
wenzelm@62856: in end\<close>
wenzelm@62856: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Theorems\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62856: 
wenzelm@63094: val long_keyword =
wenzelm@63094:   Parse_Spec.includes >> K "" ||
wenzelm@63094:   Parse_Spec.long_statement_keyword;
wenzelm@63094: 
wenzelm@63094: val long_statement =
wenzelm@63352:   Scan.optional (Parse_Spec.opt_thm_name ":" --| Scan.ahead long_keyword) Binding.empty_atts --
wenzelm@63094:   Scan.optional Parse_Spec.includes [] -- Parse_Spec.long_statement
wenzelm@63094:     >> (fn ((binding, includes), (elems, concl)) => (true, binding, includes, elems, concl));
wenzelm@63094: 
wenzelm@63094: val short_statement =
wenzelm@63094:   Parse_Spec.statement -- Parse_Spec.if_statement -- Parse.for_fixes
wenzelm@63094:     >> (fn ((shows, assumes), fixes) =>
wenzelm@63352:       (false, Binding.empty_atts, [], [Element.Fixes fixes, Element.Assumes assumes],
wenzelm@63094:         Element.Shows shows));
wenzelm@63094: 
wenzelm@63094: fun theorem spec schematic descr =
wenzelm@63094:   Outer_Syntax.local_theory_to_proof' spec ("state " ^ descr)
wenzelm@63094:     ((long_statement || short_statement) >> (fn (long, binding, includes, elems, concl) =>
wenzelm@63094:       ((if schematic then Specification.schematic_theorem_cmd else Specification.theorem_cmd)
wenzelm@63094:         long Thm.theoremK NONE (K I) binding includes elems concl)));
wenzelm@63094: 
wenzelm@67147: val _ = theorem \<^command_keyword>\<open>theorem\<close> false "theorem";
wenzelm@67147: val _ = theorem \<^command_keyword>\<open>lemma\<close> false "lemma";
wenzelm@67147: val _ = theorem \<^command_keyword>\<open>corollary\<close> false "corollary";
wenzelm@67147: val _ = theorem \<^command_keyword>\<open>proposition\<close> false "proposition";
wenzelm@67147: val _ = theorem \<^command_keyword>\<open>schematic_goal\<close> true "schematic goal";
wenzelm@63094: 
wenzelm@63094: in end\<close>
wenzelm@63094: 
wenzelm@63094: ML \<open>
wenzelm@63094: local
wenzelm@63094: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.local_theory' \<^command_keyword>\<open>lemmas\<close> "define theorems"
wenzelm@62856:     (Parse_Spec.name_facts -- Parse.for_fixes >>
wenzelm@62856:       (fn (facts, fixes) => #2 oo Specification.theorems_cmd Thm.theoremK facts fixes));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.local_theory' \<^command_keyword>\<open>declare\<close> "declare theorems"
wenzelm@62969:     (Parse.and_list1 Parse.thms1 -- Parse.for_fixes
wenzelm@62856:       >> (fn (facts, fixes) =>
wenzelm@63352:           #2 oo Specification.theorems_cmd "" [(Binding.empty_atts, flat facts)] fixes));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>named_theorems\<close>
wenzelm@62856:     "declare named collection of theorems"
wenzelm@62856:     (Parse.and_list1 (Parse.binding -- Scan.optional Parse.text "") >>
wenzelm@62856:       fold (fn (b, descr) => snd o Named_Theorems.declare b descr));
wenzelm@62856: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62849: 
wenzelm@62856: subsubsection \<open>Hide names\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62856: 
wenzelm@62856: fun hide_names command_keyword what hide parse prep =
wenzelm@62856:   Outer_Syntax.command command_keyword ("hide " ^ what ^ " from name space")
wenzelm@62856:     ((Parse.opt_keyword "open" >> not) -- Scan.repeat1 parse >> (fn (fully, args) =>
wenzelm@62856:       (Toplevel.theory (fn thy =>
wenzelm@62856:         let val ctxt = Proof_Context.init_global thy
wenzelm@62856:         in fold (hide fully o prep ctxt) args thy end))));
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   hide_names \<^command_keyword>\<open>hide_class\<close> "classes" Sign.hide_class Parse.class
wenzelm@62856:     Proof_Context.read_class;
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   hide_names \<^command_keyword>\<open>hide_type\<close> "types" Sign.hide_type Parse.type_const
wenzelm@62856:     ((#1 o dest_Type) oo Proof_Context.read_type_name {proper = true, strict = false});
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   hide_names \<^command_keyword>\<open>hide_const\<close> "consts" Sign.hide_const Parse.const
wenzelm@62856:     ((#1 o dest_Const) oo Proof_Context.read_const {proper = true, strict = false});
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   hide_names \<^command_keyword>\<open>hide_fact\<close> "facts" Global_Theory.hide_fact
wenzelm@62969:     (Parse.position Parse.name) (Global_Theory.check_fact o Proof_Context.theory_of);
wenzelm@62856: 
wenzelm@62856: in end\<close>
wenzelm@62856: 
wenzelm@62856: 
wenzelm@62856: subsection \<open>Bundled declarations\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.maybe_begin_local_theory \<^command_keyword>\<open>bundle\<close>
wenzelm@63273:     "define bundle of declarations"
wenzelm@67147:     ((Parse.binding --| \<^keyword>\<open>=\<close>) -- Parse.thms1 -- Parse.for_fixes
wenzelm@63273:       >> (uncurry Bundle.bundle_cmd))
wenzelm@63273:     (Parse.binding --| Parse.begin >> Bundle.init);
wenzelm@63270: 
wenzelm@63270: val _ =
wenzelm@67147:   Outer_Syntax.local_theory \<^command_keyword>\<open>unbundle\<close>
wenzelm@63282:     "activate declarations from bundle in local theory"
wenzelm@63282:     (Scan.repeat1 (Parse.position Parse.name) >> Bundle.unbundle_cmd);
wenzelm@63282: 
wenzelm@63282: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>include\<close>
wenzelm@63282:     "activate declarations from bundle in proof body"
wenzelm@62969:     (Scan.repeat1 (Parse.position Parse.name) >> (Toplevel.proof o Bundle.include_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>including\<close>
wenzelm@63282:     "activate declarations from bundle in goal refinement"
wenzelm@62969:     (Scan.repeat1 (Parse.position Parse.name) >> (Toplevel.proof o Bundle.including_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_bundles\<close>
wenzelm@62849:     "print bundles of declarations"
wenzelm@62849:     (Parse.opt_bang >> (fn b => Toplevel.keep (Bundle.print_bundles b o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62856: 
wenzelm@62856: subsection \<open>Local theory specifications\<close>
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Specification context\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>context\<close> "begin local theory context"
wenzelm@62969:     ((Parse.position Parse.name >> (fn name =>
wenzelm@62849:         Toplevel.begin_local_theory true (Named_Target.begin name)) ||
wenzelm@62849:       Scan.optional Parse_Spec.includes [] -- Scan.repeat Parse_Spec.context_element
wenzelm@62849:         >> (fn (incls, elems) => Toplevel.open_target (#2 o Bundle.context_cmd incls elems)))
wenzelm@62849:       --| Parse.begin);
wenzelm@62849: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>end\<close> "end context"
wenzelm@62856:     (Scan.succeed
wenzelm@62856:       (Toplevel.exit o Toplevel.end_local_theory o Toplevel.close_target o
wenzelm@62856:         Toplevel.end_proof (K Proof.end_notepad)));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62856: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Locales and interpretation\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val locale_val =
wenzelm@62849:   Parse_Spec.locale_expression --
wenzelm@67147:     Scan.optional (\<^keyword>\<open>+\<close> |-- Parse.!!! (Scan.repeat1 Parse_Spec.context_element)) [] ||
wenzelm@62849:   Scan.repeat1 Parse_Spec.context_element >> pair ([], []);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>locale\<close> "define named specification context"
wenzelm@62849:     (Parse.binding --
wenzelm@67147:       Scan.optional (\<^keyword>\<open>=\<close> |-- Parse.!!! locale_val) (([], []), []) -- Parse.opt_begin
wenzelm@62849:       >> (fn ((name, (expr, elems)), begin) =>
wenzelm@62849:           Toplevel.begin_local_theory begin
wenzelm@62849:             (Expression.add_locale_cmd name Binding.empty expr elems #> snd)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>experiment\<close> "open private specification context"
wenzelm@62849:     (Scan.repeat Parse_Spec.context_element --| Parse.begin
wenzelm@62849:       >> (fn elems =>
wenzelm@62849:           Toplevel.begin_local_theory true (Experiment.experiment_cmd elems #> snd)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>interpret\<close>
wenzelm@62849:     "prove interpretation of locale expression in proof context"
ballarin@67740:     (Parse.!!! Parse_Spec.locale_expression >> (fn expr =>
ballarin@67777:       Toplevel.proof (Interpretation.interpret_cmd expr)));
wenzelm@62849: 
wenzelm@62849: val interpretation_args_with_defs =
wenzelm@62849:   Parse.!!! Parse_Spec.locale_expression --
wenzelm@67147:     (Scan.optional (\<^keyword>\<open>defines\<close> |-- Parse.and_list1 (Parse_Spec.opt_thm_name ":"
ballarin@67777:       -- ((Parse.binding -- Parse.opt_mixfix') --| \<^keyword>\<open>=\<close> -- Parse.term))) ([]));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.local_theory_to_proof \<^command_keyword>\<open>global_interpretation\<close>
wenzelm@62849:     "prove interpretation of locale expression into global theory"
ballarin@67777:     (interpretation_args_with_defs >> (fn (expr, defs) =>
ballarin@67777:       Interpretation.global_interpretation_cmd expr defs));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>sublocale\<close>
wenzelm@62849:     "prove sublocale relation between a locale and a locale expression"
wenzelm@67147:     ((Parse.position Parse.name --| (\<^keyword>\<open>\<subseteq>\<close> || \<^keyword>\<open><\<close>) --
ballarin@67777:       interpretation_args_with_defs >> (fn (loc, (expr, defs)) =>
ballarin@67777:         Toplevel.theory_to_proof (Interpretation.global_sublocale_cmd loc expr defs)))
ballarin@67777:     || interpretation_args_with_defs >> (fn (expr, defs) =>
ballarin@67777:         Toplevel.local_theory_to_proof NONE NONE (Interpretation.sublocale_cmd expr defs)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>interpretation\<close>
wenzelm@62849:     "prove interpretation of locale expression in local theory or into global theory"
ballarin@67740:     (Parse.!!! Parse_Spec.locale_expression >> (fn expr =>
wenzelm@62856:       Toplevel.local_theory_to_proof NONE NONE
ballarin@67777:         (Interpretation.isar_interpretation_cmd expr)));
wenzelm@62856: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62849: 
wenzelm@62856: subsubsection \<open>Type classes\<close>
wenzelm@62849: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val class_val =
wenzelm@62849:   Parse_Spec.class_expression --
wenzelm@67147:     Scan.optional (\<^keyword>\<open>+\<close> |-- Parse.!!! (Scan.repeat1 Parse_Spec.context_element)) [] ||
wenzelm@62849:   Scan.repeat1 Parse_Spec.context_element >> pair [];
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>class\<close> "define type class"
wenzelm@67147:    (Parse.binding -- Scan.optional (\<^keyword>\<open>=\<close> |-- class_val) ([], []) -- Parse.opt_begin
wenzelm@62849:     >> (fn ((name, (supclasses, elems)), begin) =>
wenzelm@62849:         Toplevel.begin_local_theory begin
wenzelm@62849:           (Class_Declaration.class_cmd name supclasses elems #> snd)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.local_theory_to_proof \<^command_keyword>\<open>subclass\<close> "prove a subclass relation"
wenzelm@62849:     (Parse.class >> Class_Declaration.subclass_cmd);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>instantiation\<close> "instantiate and prove type arity"
wenzelm@62849:    (Parse.multi_arity --| Parse.begin
wenzelm@62849:      >> (fn arities => Toplevel.begin_local_theory true (Class.instantiation_cmd arities)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>instance\<close> "prove type arity or subclass relation"
wenzelm@62849:   ((Parse.class --
wenzelm@67147:     ((\<^keyword>\<open>\<subseteq>\<close> || \<^keyword>\<open><\<close>) |-- Parse.!!! Parse.class) >> Class.classrel_cmd ||
wenzelm@62849:     Parse.multi_arity >> Class.instance_arity_cmd) >> Toplevel.theory_to_proof ||
wenzelm@62849:     Scan.succeed (Toplevel.local_theory_to_proof NONE NONE (Class.instantiation_instance I)));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Arbitrary overloading\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>overloading\<close> "overloaded definitions"
wenzelm@67147:    (Scan.repeat1 (Parse.name --| (\<^keyword>\<open>==\<close> || \<^keyword>\<open>\<equiv>\<close>) -- Parse.term --
wenzelm@67147:       Scan.optional (\<^keyword>\<open>(\<close> |-- (\<^keyword>\<open>unchecked\<close> >> K false) --| \<^keyword>\<open>)\<close>) true
wenzelm@62849:       >> Scan.triple1) --| Parse.begin
wenzelm@62849:    >> (fn operations => Toplevel.begin_local_theory true (Overloading.overloading_cmd operations)));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62849: 
wenzelm@62856: subsection \<open>Proof commands\<close>
wenzelm@62849: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.local_theory_to_proof \<^command_keyword>\<open>notepad\<close> "begin proof context"
wenzelm@62849:     (Parse.begin >> K Proof.begin_notepad);
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Statements\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val structured_statement =
wenzelm@62849:   Parse_Spec.statement -- Parse_Spec.cond_statement -- Parse.for_fixes
wenzelm@62849:     >> (fn ((shows, (strict, assumes)), fixes) => (strict, fixes, assumes, shows));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>have\<close> "state local goal"
wenzelm@62849:     (structured_statement >> (fn (a, b, c, d) =>
wenzelm@62849:       Toplevel.proof' (fn int => Proof.have_cmd a NONE (K I) b c d int #> #2)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>show\<close> "state local goal, to refine pending subgoals"
wenzelm@62849:     (structured_statement >> (fn (a, b, c, d) =>
wenzelm@62849:       Toplevel.proof' (fn int => Proof.show_cmd a NONE (K I) b c d int #> #2)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>hence\<close> "old-style alias of \"then have\""
wenzelm@62849:     (structured_statement >> (fn (a, b, c, d) =>
wenzelm@62849:       Toplevel.proof' (fn int => Proof.chain #> Proof.have_cmd a NONE (K I) b c d int #> #2)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>thus\<close> "old-style alias of  \"then show\""
wenzelm@62849:     (structured_statement >> (fn (a, b, c, d) =>
wenzelm@62849:       Toplevel.proof' (fn int => Proof.chain #> Proof.show_cmd a NONE (K I) b c d int #> #2)));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Local facts\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62969: val facts = Parse.and_list1 Parse.thms1;
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>then\<close> "forward chaining"
wenzelm@62849:     (Scan.succeed (Toplevel.proof Proof.chain));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>from\<close> "forward chaining from given facts"
wenzelm@62849:     (facts >> (Toplevel.proof o Proof.from_thmss_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>with\<close> "forward chaining from given and current facts"
wenzelm@62849:     (facts >> (Toplevel.proof o Proof.with_thmss_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>note\<close> "define facts"
wenzelm@62849:     (Parse_Spec.name_facts >> (Toplevel.proof o Proof.note_thmss_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>supply\<close> "define facts during goal refinement (unstructured)"
wenzelm@62849:     (Parse_Spec.name_facts >> (Toplevel.proof o Proof.supply_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>using\<close> "augment goal facts"
wenzelm@62849:     (facts >> (Toplevel.proof o Proof.using_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>unfolding\<close> "unfold definitions in goal and facts"
wenzelm@62849:     (facts >> (Toplevel.proof o Proof.unfolding_cmd));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Proof context\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62856: 
wenzelm@62856: val structured_statement =
wenzelm@62856:   Parse_Spec.statement -- Parse_Spec.if_statement' -- Parse.for_fixes
wenzelm@62856:     >> (fn ((shows, assumes), fixes) => (fixes, assumes, shows));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>fix\<close> "fix local variables (Skolem constants)"
wenzelm@63285:     (Parse.vars >> (Toplevel.proof o Proof.fix_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>assume\<close> "assume propositions"
wenzelm@62856:     (structured_statement >> (fn (a, b, c) => Toplevel.proof (Proof.assume_cmd a b c)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>presume\<close> "assume propositions, to be established later"
wenzelm@62856:     (structured_statement >> (fn (a, b, c) => Toplevel.proof (Proof.presume_cmd a b c)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>define\<close> "local definition (non-polymorphic)"
wenzelm@63285:     ((Parse.vars --| Parse.where_) -- Parse_Spec.statement -- Parse.for_fixes
wenzelm@63039:       >> (fn ((a, b), c) => Toplevel.proof (Proof.define_cmd a c b)));
wenzelm@63039: 
wenzelm@63039: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>consider\<close> "state cases rule"
wenzelm@62849:     (Parse_Spec.obtains >> (Toplevel.proof' o Obtain.consider_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>obtain\<close> "generalized elimination"
wenzelm@63285:     (Parse.parbinding -- Scan.optional (Parse.vars --| Parse.where_) [] -- structured_statement
wenzelm@63059:       >> (fn ((a, b), (c, d, e)) => Toplevel.proof' (Obtain.obtain_cmd a b c d e)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>guess\<close> "wild guessing (unstructured)"
wenzelm@63285:     (Scan.optional Parse.vars [] >> (Toplevel.proof' o Obtain.guess_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>let\<close> "bind text variables"
wenzelm@67147:     (Parse.and_list1 (Parse.and_list1 Parse.term -- (\<^keyword>\<open>=\<close> |-- Parse.term))
wenzelm@62849:       >> (Toplevel.proof o Proof.let_bind_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>write\<close> "add concrete syntax for constants / fixed variables"
wenzelm@62856:     (Parse.syntax_mode -- Parse.and_list1 (Parse.const -- Parse.mixfix)
wenzelm@62849:     >> (fn (mode, args) => Toplevel.proof (Proof.write_cmd mode args)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>case\<close> "invoke local context"
wenzelm@62849:     (Parse_Spec.opt_thm_name ":" --
wenzelm@67147:       (\<^keyword>\<open>(\<close> |--
wenzelm@62969:         Parse.!!! (Parse.position Parse.name -- Scan.repeat (Parse.maybe Parse.binding)
wenzelm@67147:           --| \<^keyword>\<open>)\<close>) ||
wenzelm@62969:         Parse.position Parse.name >> rpair []) >> (Toplevel.proof o Proof.case_cmd));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Proof structure\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>{\<close> "begin explicit proof block"
wenzelm@62849:     (Scan.succeed (Toplevel.proof Proof.begin_block));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>}\<close> "end explicit proof block"
wenzelm@62849:     (Scan.succeed (Toplevel.proof Proof.end_block));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>next\<close> "enter next proof block"
wenzelm@62849:     (Scan.succeed (Toplevel.proof Proof.next_block));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>End proof\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>qed\<close> "conclude proof"
wenzelm@62849:     (Scan.option Method.parse >> (fn m =>
wenzelm@62849:      (Option.map Method.report m;
wenzelm@62849:       Isar_Cmd.qed m)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>by\<close> "terminal backward proof"
wenzelm@62849:     (Method.parse -- Scan.option Method.parse >> (fn (m1, m2) =>
wenzelm@62849:      (Method.report m1;
wenzelm@62849:       Option.map Method.report m2;
wenzelm@62849:       Isar_Cmd.terminal_proof (m1, m2))));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>..\<close> "default proof"
wenzelm@62849:     (Scan.succeed Isar_Cmd.default_proof);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>.\<close> "immediate proof"
wenzelm@62849:     (Scan.succeed Isar_Cmd.immediate_proof);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>done\<close> "done proof"
wenzelm@62849:     (Scan.succeed Isar_Cmd.done_proof);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>sorry\<close> "skip proof (quick-and-dirty mode only!)"
wenzelm@62849:     (Scan.succeed Isar_Cmd.skip_proof);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>\<proof>\<close> "dummy proof (quick-and-dirty mode only!)"
wenzelm@62849:     (Scan.succeed Isar_Cmd.skip_proof);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>oops\<close> "forget proof"
wenzelm@68876:     (Scan.succeed Toplevel.forget_proof);
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Proof steps\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>defer\<close> "shuffle internal proof state"
wenzelm@62849:     (Scan.optional Parse.nat 1 >> (Toplevel.proof o Proof.defer));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>prefer\<close> "shuffle internal proof state"
wenzelm@62849:     (Parse.nat >> (Toplevel.proof o Proof.prefer));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>apply\<close> "initial goal refinement step (unstructured)"
wenzelm@62849:     (Method.parse >> (fn m => (Method.report m; Toplevel.proofs (Proof.apply m))));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>apply_end\<close> "terminal goal refinement step (unstructured)"
wenzelm@62849:     (Method.parse >> (fn m => (Method.report m; Toplevel.proofs (Proof.apply_end m))));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>proof\<close> "backward proof step"
wenzelm@62849:     (Scan.option Method.parse >> (fn m =>
wenzelm@63513:       (Option.map Method.report m;
wenzelm@63513:        Toplevel.proof (fn state =>
wenzelm@63513:          let
wenzelm@63513:           val state' = state |> Proof.proof m |> Seq.the_result "";
wenzelm@63513:           val _ =
wenzelm@63513:             Output.information
wenzelm@63513:               (Proof_Context.print_cases_proof (Proof.context_of state) (Proof.context_of state'));
wenzelm@63513:         in state' end))))
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62849: 
wenzelm@62856: subsubsection \<open>Subgoal focus\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62849: local
wenzelm@62849: 
wenzelm@62849: val opt_fact_binding =
wenzelm@62849:   Scan.optional (Parse.binding -- Parse.opt_attribs || Parse.attribs >> pair Binding.empty)
wenzelm@63352:     Binding.empty_atts;
wenzelm@62849: 
wenzelm@62849: val for_params =
wenzelm@62849:   Scan.optional
wenzelm@67147:     (\<^keyword>\<open>for\<close> |--
wenzelm@62849:       Parse.!!! ((Scan.option Parse.dots >> is_some) --
wenzelm@62849:         (Scan.repeat1 (Parse.position (Parse.maybe Parse.name)))))
wenzelm@62849:     (false, []);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>subgoal\<close>
wenzelm@62849:     "focus on first subgoal within backward refinement"
wenzelm@67147:     (opt_fact_binding -- (Scan.option (\<^keyword>\<open>premises\<close> |-- Parse.!!! opt_fact_binding)) --
wenzelm@62849:       for_params >> (fn ((a, b), c) =>
wenzelm@62849:         Toplevel.proofs (Seq.make_results o Seq.single o #2 o Subgoal.subgoal_cmd a b c)));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62849: 
wenzelm@62856: subsubsection \<open>Calculation\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val calculation_args =
wenzelm@67147:   Scan.option (\<^keyword>\<open>(\<close> |-- Parse.!!! ((Parse.thms1 --| \<^keyword>\<open>)\<close>)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>also\<close> "combine calculation and current facts"
wenzelm@62849:     (calculation_args >> (Toplevel.proofs' o Calculation.also_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>finally\<close>
wenzelm@62849:     "combine calculation and current facts, exhibit result"
wenzelm@62849:     (calculation_args >> (Toplevel.proofs' o Calculation.finally_cmd));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>moreover\<close> "augment calculation by current facts"
wenzelm@62849:     (Scan.succeed (Toplevel.proof' Calculation.moreover));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>ultimately\<close>
wenzelm@62849:     "augment calculation by current facts, exhibit result"
wenzelm@62849:     (Scan.succeed (Toplevel.proof' Calculation.ultimately));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_trans_rules\<close> "print transitivity rules"
wenzelm@62849:     (Scan.succeed (Toplevel.keep (Calculation.print_rules o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62856: 
wenzelm@62856: subsubsection \<open>Proof navigation\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: fun report_back () =
wenzelm@64677:   Output.report [Markup.markup (Markup.bad ()) "Explicit backtracking"];
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>back\<close> "explicit backtracking of proof command"
wenzelm@62849:     (Scan.succeed
wenzelm@62849:      (Toplevel.actual_proof (fn prf => (report_back (); Proof_Node.back prf)) o
wenzelm@62849:       Toplevel.skip_proof report_back));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62849: 
wenzelm@62856: subsection \<open>Diagnostic commands (for interactive mode only)\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val opt_modes =
wenzelm@67147:   Scan.optional (\<^keyword>\<open>(\<close> |-- Parse.!!! (Scan.repeat1 Parse.name --| \<^keyword>\<open>)\<close>)) [];
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>help\<close>
wenzelm@62849:     "retrieve outer syntax commands according to name patterns"
wenzelm@62849:     (Scan.repeat Parse.name >>
wenzelm@62849:       (fn pats => Toplevel.keep (fn st => Outer_Syntax.help (Toplevel.theory_of st) pats)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_commands\<close> "print outer syntax commands"
wenzelm@62849:     (Scan.succeed (Toplevel.keep (Outer_Syntax.print_commands o Toplevel.theory_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_options\<close> "print configuration options"
wenzelm@62849:     (Parse.opt_bang >> (fn b => Toplevel.keep (Attrib.print_options b o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_context\<close>
wenzelm@62849:     "print context of local theory target"
wenzelm@62849:     (Scan.succeed (Toplevel.keep (Pretty.writeln_chunks o Toplevel.pretty_context)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_theory\<close>
wenzelm@62849:     "print logical theory contents"
wenzelm@62849:     (Parse.opt_bang >> (fn b =>
wenzelm@62849:       Toplevel.keep (Pretty.writeln o Proof_Display.pretty_theory b o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_definitions\<close>
wenzelm@62849:     "print dependencies of definitional theory content"
wenzelm@62849:     (Parse.opt_bang >> (fn b =>
wenzelm@62849:       Toplevel.keep (Pretty.writeln o Proof_Display.pretty_definitions b o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_syntax\<close>
wenzelm@62849:     "print inner syntax of context"
wenzelm@62849:     (Scan.succeed (Toplevel.keep (Proof_Context.print_syntax o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_defn_rules\<close>
wenzelm@62849:     "print definitional rewrite rules of context"
wenzelm@62849:     (Scan.succeed (Toplevel.keep (Local_Defs.print_rules o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_abbrevs\<close>
wenzelm@62849:     "print constant abbreviations of context"
wenzelm@62849:     (Parse.opt_bang >> (fn b =>
wenzelm@62849:       Toplevel.keep (Proof_Context.print_abbrevs b o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_theorems\<close>
wenzelm@62849:     "print theorems of local theory or proof context"
wenzelm@62849:     (Parse.opt_bang >> (fn b =>
wenzelm@62849:       Toplevel.keep (Pretty.writeln o Pretty.chunks o Isar_Cmd.pretty_theorems b)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_locales\<close>
wenzelm@62849:     "print locales of this theory"
wenzelm@69059:     (Parse.opt_bang >> (fn verbose =>
wenzelm@69059:       Toplevel.keep (fn state =>
wenzelm@69059:         let val thy = Toplevel.theory_of state
wenzelm@69059:         in Pretty.writeln (Locale.pretty_locales thy verbose) end)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_classes\<close>
wenzelm@62849:     "print classes of this theory"
wenzelm@62849:     (Scan.succeed (Toplevel.keep (Class.print_classes o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_locale\<close>
wenzelm@62849:     "print locale of this theory"
wenzelm@69057:     (Parse.opt_bang -- Parse.position Parse.name >> (fn (show_facts, raw_name) =>
wenzelm@69057:       Toplevel.keep (fn state =>
wenzelm@69057:         let
wenzelm@69057:           val thy = Toplevel.theory_of state;
wenzelm@69057:           val name = Locale.check thy raw_name;
wenzelm@69057:         in Pretty.writeln (Locale.pretty_locale thy show_facts name) end)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_interps\<close>
wenzelm@62849:     "print interpretations of locale for this theory or proof context"
wenzelm@69059:     (Parse.position Parse.name >> (fn raw_name =>
wenzelm@69059:       Toplevel.keep (fn state =>
wenzelm@69059:         let
wenzelm@69059:           val ctxt = Toplevel.context_of state;
wenzelm@69059:           val thy = Toplevel.theory_of state;
wenzelm@69059:           val name = Locale.check thy raw_name;
wenzelm@69059:         in Pretty.writeln (Locale.pretty_registrations ctxt name) end)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_dependencies\<close>
wenzelm@62849:     "print dependencies of locale expression"
wenzelm@69057:     (Parse.opt_bang -- Parse_Spec.locale_expression >> (fn (clean, expr) =>
wenzelm@69057:       Toplevel.keep (fn state =>
wenzelm@69057:         Pretty.writeln (Expression.pretty_dependencies (Toplevel.context_of state) clean expr))));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_attributes\<close>
wenzelm@62849:     "print attributes of this theory"
wenzelm@62849:     (Parse.opt_bang >> (fn b => Toplevel.keep (Attrib.print_attributes b o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_simpset\<close>
wenzelm@62849:     "print context of Simplifier"
wenzelm@62849:     (Parse.opt_bang >> (fn b =>
wenzelm@62849:       Toplevel.keep (Pretty.writeln o Simplifier.pretty_simpset b o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_rules\<close> "print intro/elim rules"
wenzelm@62849:     (Scan.succeed (Toplevel.keep (Context_Rules.print_rules o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_methods\<close> "print methods of this theory"
wenzelm@62849:     (Parse.opt_bang >> (fn b => Toplevel.keep (Method.print_methods b o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_antiquotations\<close>
wenzelm@62849:     "print document antiquotations"
wenzelm@62849:     (Parse.opt_bang >> (fn b =>
wenzelm@67386:       Toplevel.keep (Document_Antiquotation.print_antiquotations b o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_ML_antiquotations\<close>
wenzelm@62849:     "print ML antiquotations"
wenzelm@62849:     (Parse.opt_bang >> (fn b =>
wenzelm@62849:       Toplevel.keep (ML_Context.print_antiquotations b o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>locale_deps\<close> "visualize locale dependencies"
wenzelm@62849:     (Scan.succeed
wenzelm@62849:       (Toplevel.keep (Toplevel.theory_of #> (fn thy =>
wenzelm@62849:         Locale.pretty_locale_deps thy
wenzelm@62849:         |> map (fn {name, parents, body} =>
wenzelm@62849:           ((name, Graph_Display.content_node (Locale.extern thy name) [body]), parents))
wenzelm@62849:         |> Graph_Display.display_graph_old))));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_term_bindings\<close>
wenzelm@62849:     "print term bindings of proof context"
wenzelm@62849:     (Scan.succeed
wenzelm@62849:       (Toplevel.keep
wenzelm@62849:         (Pretty.writeln_chunks o Proof_Context.pretty_term_bindings o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_facts\<close> "print facts of proof context"
wenzelm@62849:     (Parse.opt_bang >> (fn b =>
wenzelm@62849:       Toplevel.keep (Proof_Context.print_local_facts b o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_cases\<close> "print cases of proof context"
wenzelm@62849:     (Scan.succeed
wenzelm@62849:       (Toplevel.keep (Pretty.writeln_chunks o Proof_Context.pretty_cases o Toplevel.context_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_statement\<close>
wenzelm@62849:     "print theorems as long statements"
wenzelm@62969:     (opt_modes -- Parse.thms1 >> Isar_Cmd.print_stmts);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>thm\<close> "print theorems"
wenzelm@62969:     (opt_modes -- Parse.thms1 >> Isar_Cmd.print_thms);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>prf\<close> "print proof terms of theorems"
wenzelm@62969:     (opt_modes -- Scan.option Parse.thms1 >> Isar_Cmd.print_prfs false);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>full_prf\<close> "print full proof terms of theorems"
wenzelm@62969:     (opt_modes -- Scan.option Parse.thms1 >> Isar_Cmd.print_prfs true);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>prop\<close> "read and print proposition"
wenzelm@62849:     (opt_modes -- Parse.term >> Isar_Cmd.print_prop);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>term\<close> "read and print term"
wenzelm@62849:     (opt_modes -- Parse.term >> Isar_Cmd.print_term);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>typ\<close> "read and print type"
wenzelm@67147:     (opt_modes -- (Parse.typ -- Scan.option (\<^keyword>\<open>::\<close> |-- Parse.!!! Parse.sort))
wenzelm@62849:       >> Isar_Cmd.print_type);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_codesetup\<close> "print code generator setup"
wenzelm@62849:     (Scan.succeed (Toplevel.keep (Code.print_codesetup o Toplevel.theory_of)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>print_state\<close>
wenzelm@62849:     "print current proof state (if present)"
wenzelm@62849:     (opt_modes >> (fn modes =>
wenzelm@62849:       Toplevel.keep (Print_Mode.with_modes modes (Output.state o Toplevel.string_of_state))));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>welcome\<close> "print welcome message"
wenzelm@62849:     (Scan.succeed (Toplevel.keep (fn _ => writeln (Session.welcome ()))));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62849: 
wenzelm@62856: subsection \<open>Dependencies\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62849: local
wenzelm@62856: 
wenzelm@62856: val theory_bounds =
wenzelm@62969:   Parse.position Parse.theory_name >> single ||
wenzelm@67147:   (\<^keyword>\<open>(\<close> |-- Parse.enum "|" (Parse.position Parse.theory_name) --| \<^keyword>\<open>)\<close>);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>thy_deps\<close> "visualize theory dependencies"
wenzelm@62849:     (Scan.option theory_bounds -- Scan.option theory_bounds >>
wenzelm@62849:       (fn args => Toplevel.keep (fn st => Thy_Deps.thy_deps_cmd (Toplevel.context_of st) args)));
wenzelm@62849: 
wenzelm@62849: 
wenzelm@62856: val class_bounds =
wenzelm@62856:   Parse.sort >> single ||
wenzelm@67147:   (\<^keyword>\<open>(\<close> |-- Parse.enum "|" Parse.sort --| \<^keyword>\<open>)\<close>);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>class_deps\<close> "visualize class dependencies"
wenzelm@62849:     (Scan.option class_bounds -- Scan.option class_bounds >> (fn args =>
wenzelm@62849:       Toplevel.keep (fn st => Class_Deps.class_deps_cmd (Toplevel.context_of st) args)));
wenzelm@62849: 
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>thm_deps\<close> "visualize theorem dependencies"
wenzelm@62969:     (Parse.thms1 >> (fn args =>
wenzelm@62849:       Toplevel.keep (fn st =>
wenzelm@62849:         Thm_Deps.thm_deps (Toplevel.theory_of st)
wenzelm@62849:           (Attrib.eval_thms (Toplevel.context_of st) args))));
wenzelm@62849: 
wenzelm@62856: 
wenzelm@62856: val thy_names =
wenzelm@62969:   Scan.repeat1 (Scan.unless Parse.minus (Parse.position Parse.theory_name));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>unused_thms\<close> "find unused theorems"
wenzelm@62849:     (Scan.option ((thy_names --| Parse.minus) -- Scan.option thy_names) >> (fn opt_range =>
wenzelm@62849:         Toplevel.keep (fn st =>
wenzelm@62849:           let
wenzelm@62849:             val thy = Toplevel.theory_of st;
wenzelm@62849:             val ctxt = Toplevel.context_of st;
wenzelm@62849:             fun pretty_thm (a, th) = Proof_Context.pretty_fact ctxt (a, [th]);
wenzelm@68482:             val check = Theory.check {long = false} ctxt;
wenzelm@62849:           in
wenzelm@62849:             Thm_Deps.unused_thms
wenzelm@62849:               (case opt_range of
wenzelm@62849:                 NONE => (Theory.parents_of thy, [thy])
wenzelm@62849:               | SOME (xs, NONE) => (map check xs, [thy])
wenzelm@62849:               | SOME (xs, SOME ys) => (map check xs, map check ys))
wenzelm@62849:             |> map pretty_thm |> Pretty.writeln_chunks
wenzelm@62849:           end)));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62849: 
wenzelm@62856: subsubsection \<open>Find consts and theorems\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>find_consts\<close>
wenzelm@62849:     "find constants by name / type patterns"
wenzelm@62849:     (Find_Consts.query_parser >> (fn spec =>
wenzelm@62849:       Toplevel.keep (fn st =>
wenzelm@62849:         Pretty.writeln (Find_Consts.pretty_consts (Toplevel.context_of st) spec))));
wenzelm@62849: 
wenzelm@62856: val options =
wenzelm@62856:   Scan.optional
wenzelm@62856:     (Parse.$$$ "(" |--
wenzelm@62856:       Parse.!!! (Scan.option Parse.nat --
wenzelm@62856:         Scan.optional (Parse.reserved "with_dups" >> K false) true --| Parse.$$$ ")"))
wenzelm@62856:     (NONE, true);
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>find_theorems\<close>
wenzelm@62849:     "find theorems meeting specified criteria"
wenzelm@62849:     (options -- Find_Theorems.query_parser >> (fn ((opt_lim, rem_dups), spec) =>
wenzelm@62849:       Toplevel.keep (fn st =>
wenzelm@62849:         Pretty.writeln
wenzelm@62849:           (Find_Theorems.pretty_theorems (Find_Theorems.proof_state st) opt_lim rem_dups spec))));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62849: 
wenzelm@62856: subsection \<open>Code generation\<close>
wenzelm@62849: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62856: 
wenzelm@62856: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>code_datatype\<close>
wenzelm@62856:     "define set of code datatype constructors"
haftmann@66251:     (Scan.repeat1 Parse.term >> (Toplevel.theory o Code.declare_datatype_cmd));
wenzelm@62856: 
wenzelm@62856: in end\<close>
wenzelm@62856: 
wenzelm@62856: 
wenzelm@62856: subsection \<open>Extraction of programs from proofs\<close>
wenzelm@62856: 
wenzelm@62856: ML \<open>
wenzelm@62856: local
wenzelm@62849: 
wenzelm@62849: val parse_vars = Scan.optional (Parse.$$$ "(" |-- Parse.list1 Parse.name --| Parse.$$$ ")") [];
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>realizers\<close>
wenzelm@62849:     "specify realizers for primitive axioms / theorems, together with correctness proof"
wenzelm@62969:     (Scan.repeat1 (Parse.name -- parse_vars --| Parse.$$$ ":" -- Parse.string -- Parse.string) >>
wenzelm@62849:      (fn xs => Toplevel.theory (fn thy => Extraction.add_realizers
wenzelm@62849:        (map (fn (((a, vs), s1), s2) => (Global_Theory.get_thm thy a, (vs, s1, s2))) xs) thy)));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>realizability\<close>
wenzelm@62849:     "add equations characterizing realizability"
wenzelm@62849:     (Scan.repeat1 Parse.string >> (Toplevel.theory o Extraction.add_realizes_eqns));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>extract_type\<close>
wenzelm@62849:     "add equations characterizing type of extracted program"
wenzelm@62849:     (Scan.repeat1 Parse.string >> (Toplevel.theory o Extraction.add_typeof_eqns));
wenzelm@62849: 
wenzelm@62849: val _ =
wenzelm@67147:   Outer_Syntax.command \<^command_keyword>\<open>extract\<close> "extract terms from proofs"
wenzelm@62969:     (Scan.repeat1 (Parse.name -- parse_vars) >> (fn xs => Toplevel.theory (fn thy =>
wenzelm@62849:       Extraction.extract (map (apfst (Global_Theory.get_thm thy)) xs) thy)));
wenzelm@62849: 
wenzelm@62856: in end\<close>
wenzelm@62849: 
wenzelm@62849: 
wenzelm@62944: section \<open>Auxiliary lemmas\<close>
wenzelm@20627: 
wenzelm@58611: subsection \<open>Meta-level connectives in assumptions\<close>
wenzelm@15803: 
wenzelm@15803: lemma meta_mp:
wenzelm@58612:   assumes "PROP P \<Longrightarrow> PROP Q" and "PROP P"
wenzelm@15803:   shows "PROP Q"
wenzelm@58612:     by (rule \<open>PROP P \<Longrightarrow> PROP Q\<close> [OF \<open>PROP P\<close>])
wenzelm@15803: 
nipkow@23432: lemmas meta_impE = meta_mp [elim_format]
nipkow@23432: 
wenzelm@15803: lemma meta_spec:
wenzelm@58612:   assumes "\<And>x. PROP P x"
wenzelm@26958:   shows "PROP P x"
wenzelm@58612:     by (rule \<open>\<And>x. PROP P x\<close>)
wenzelm@15803: 
wenzelm@15803: lemmas meta_allE = meta_spec [elim_format]
wenzelm@15803: 
wenzelm@26570: lemma swap_params:
wenzelm@58612:   "(\<And>x y. PROP P x y) \<equiv> (\<And>y x. PROP P x y)" ..
wenzelm@26570: 
wenzelm@18466: 
wenzelm@58611: subsection \<open>Meta-level conjunction\<close>
wenzelm@18466: 
wenzelm@18466: lemma all_conjunction:
wenzelm@58612:   "(\<And>x. PROP A x &&& PROP B x) \<equiv> ((\<And>x. PROP A x) &&& (\<And>x. PROP B x))"
wenzelm@18466: proof
wenzelm@58612:   assume conj: "\<And>x. PROP A x &&& PROP B x"
wenzelm@58612:   show "(\<And>x. PROP A x) &&& (\<And>x. PROP B x)"
wenzelm@19121:   proof -
wenzelm@18466:     fix x
wenzelm@26958:     from conj show "PROP A x" by (rule conjunctionD1)
wenzelm@26958:     from conj show "PROP B x" by (rule conjunctionD2)
wenzelm@18466:   qed
wenzelm@18466: next
wenzelm@58612:   assume conj: "(\<And>x. PROP A x) &&& (\<And>x. PROP B x)"
wenzelm@18466:   fix x
wenzelm@28856:   show "PROP A x &&& PROP B x"
wenzelm@19121:   proof -
wenzelm@26958:     show "PROP A x" by (rule conj [THEN conjunctionD1, rule_format])
wenzelm@26958:     show "PROP B x" by (rule conj [THEN conjunctionD2, rule_format])
wenzelm@18466:   qed
wenzelm@18466: qed
wenzelm@18466: 
wenzelm@19121: lemma imp_conjunction:
wenzelm@58612:   "(PROP A \<Longrightarrow> PROP B &&& PROP C) \<equiv> ((PROP A \<Longrightarrow> PROP B) &&& (PROP A \<Longrightarrow> PROP C))"
wenzelm@18836: proof
wenzelm@58612:   assume conj: "PROP A \<Longrightarrow> PROP B &&& PROP C"
wenzelm@58612:   show "(PROP A \<Longrightarrow> PROP B) &&& (PROP A \<Longrightarrow> PROP C)"
wenzelm@19121:   proof -
wenzelm@18466:     assume "PROP A"
wenzelm@58611:     from conj [OF \<open>PROP A\<close>] show "PROP B" by (rule conjunctionD1)
wenzelm@58611:     from conj [OF \<open>PROP A\<close>] show "PROP C" by (rule conjunctionD2)
wenzelm@18466:   qed
wenzelm@18466: next
wenzelm@58612:   assume conj: "(PROP A \<Longrightarrow> PROP B) &&& (PROP A \<Longrightarrow> PROP C)"
wenzelm@18466:   assume "PROP A"
wenzelm@28856:   show "PROP B &&& PROP C"
wenzelm@19121:   proof -
wenzelm@58611:     from \<open>PROP A\<close> show "PROP B" by (rule conj [THEN conjunctionD1])
wenzelm@58611:     from \<open>PROP A\<close> show "PROP C" by (rule conj [THEN conjunctionD2])
wenzelm@18466:   qed
wenzelm@18466: qed
wenzelm@18466: 
wenzelm@18466: lemma conjunction_imp:
wenzelm@58612:   "(PROP A &&& PROP B \<Longrightarrow> PROP C) \<equiv> (PROP A \<Longrightarrow> PROP B \<Longrightarrow> PROP C)"
wenzelm@18466: proof
wenzelm@58612:   assume r: "PROP A &&& PROP B \<Longrightarrow> PROP C"
wenzelm@22933:   assume ab: "PROP A" "PROP B"
wenzelm@22933:   show "PROP C"
wenzelm@22933:   proof (rule r)
wenzelm@28856:     from ab show "PROP A &&& PROP B" .
wenzelm@22933:   qed
wenzelm@18466: next
wenzelm@58612:   assume r: "PROP A \<Longrightarrow> PROP B \<Longrightarrow> PROP C"
wenzelm@28856:   assume conj: "PROP A &&& PROP B"
wenzelm@18466:   show "PROP C"
wenzelm@18466:   proof (rule r)
wenzelm@19121:     from conj show "PROP A" by (rule conjunctionD1)
wenzelm@19121:     from conj show "PROP B" by (rule conjunctionD2)
wenzelm@18466:   qed
wenzelm@18466: qed
wenzelm@18466: 
wenzelm@68816: declare [[ML_write_global = false]]
wenzelm@68816: 
wenzelm@48638: end




