src/Pure/Pure.thy
author wenzelm
Thu Mar 14 16:55:06 2019 +0100 (5 weeks ago)
changeset 69913 ca515cf61651
parent 69891 def3ec9cdb7e
child 70009 435fb018e8ee
permissions -rw-r--r--
more specific keyword kinds;
wenzelm@48929
     1
(*  Title:      Pure/Pure.thy
wenzelm@48929
     2
    Author:     Makarius
wenzelm@48929
     3
wenzelm@62944
     4
The Pure theory, with definitions of Isar commands and some lemmas.
wenzelm@48929
     5
*)
wenzelm@48929
     6
wenzelm@48638
     7
theory Pure
wenzelm@62859
     8
keywords
wenzelm@67446
     9
    "!!" "!" "+" ":" ";" "<" "<=" "==" "=>" "?" "[" "\<comment>" "\<equiv>" "\<leftharpoondown>" "\<rightharpoonup>" "\<rightleftharpoons>"
wenzelm@69891
    10
    "\<subseteq>" "]" "binder" "in" "infix" "infixl" "infixr" "is" "open" "output"
wenzelm@66194
    11
    "overloaded" "pervasive" "premises" "structure" "unchecked"
wenzelm@63441
    12
  and "private" "qualified" :: before_command
ballarin@67740
    13
  and "assumes" "constrains" "defines" "fixes" "for" "if" "includes" "notes" "rewrites"
wenzelm@66194
    14
    "obtains" "shows" "when" "where" "|" :: quasi_command
wenzelm@58999
    15
  and "text" "txt" :: document_body
wenzelm@58999
    16
  and "text_raw" :: document_raw
wenzelm@63579
    17
  and "default_sort" :: thy_decl
wenzelm@69913
    18
  and "typedecl" "nonterminal" "judgment" "consts" "syntax" "no_syntax" "translations"
wenzelm@69913
    19
    "no_translations" "type_notation" "no_type_notation" "notation" "no_notation" "alias"
wenzelm@69913
    20
    "type_alias" "declare" "hide_class" "hide_type" "hide_const" "hide_fact" :: thy_decl
wenzelm@69913
    21
  and "type_synonym" "definition" "abbreviation" "lemmas" :: thy_defn
wenzelm@69913
    22
  and "axiomatization" :: thy_stmt
wenzelm@67281
    23
  and "external_file" "bibtex_file" :: thy_load
wenzelm@69381
    24
  and "generate_file" :: thy_decl
wenzelm@69662
    25
  and "export_generated_files" :: diag
wenzelm@62849
    26
  and "ML_file" "ML_file_debug" "ML_file_no_debug" :: thy_load % "ML"
wenzelm@60957
    27
  and "SML_file" "SML_file_debug" "SML_file_no_debug" :: thy_load % "ML"
wenzelm@68276
    28
  and "SML_import" "SML_export" "ML_export" :: thy_decl % "ML"
wenzelm@48641
    29
  and "ML_prf" :: prf_decl % "proof"  (* FIXME % "ML" ?? *)
wenzelm@48641
    30
  and "ML_val" "ML_command" :: diag % "ML"
wenzelm@63579
    31
  and "simproc_setup" :: thy_decl % "ML"
wenzelm@48641
    32
  and "setup" "local_setup" "attribute_setup" "method_setup"
wenzelm@55762
    33
    "declaration" "syntax_declaration"
wenzelm@48641
    34
    "parse_ast_translation" "parse_translation" "print_translation"
wenzelm@48641
    35
    "typed_print_translation" "print_ast_translation" "oracle" :: thy_decl % "ML"
wenzelm@63273
    36
  and "bundle" :: thy_decl_block
wenzelm@63282
    37
  and "unbundle" :: thy_decl
wenzelm@48641
    38
  and "include" "including" :: prf_decl
wenzelm@48641
    39
  and "print_bundles" :: diag
wenzelm@59901
    40
  and "context" "locale" "experiment" :: thy_decl_block
wenzelm@51224
    41
  and "interpret" :: prf_goal % "proof"
haftmann@61890
    42
  and "interpretation" "global_interpretation" "sublocale" :: thy_goal
wenzelm@58800
    43
  and "class" :: thy_decl_block
wenzelm@48641
    44
  and "subclass" :: thy_goal
wenzelm@58800
    45
  and "instantiation" :: thy_decl_block
wenzelm@48641
    46
  and "instance" :: thy_goal
wenzelm@58800
    47
  and "overloading" :: thy_decl_block
wenzelm@48641
    48
  and "code_datatype" :: thy_decl
wenzelm@69913
    49
  and "theorem" "lemma" "corollary" "proposition" :: thy_goal_stmt
wenzelm@69913
    50
  and "schematic_goal" :: thy_goal_stmt
wenzelm@58800
    51
  and "notepad" :: thy_decl_block
wenzelm@50128
    52
  and "have" :: prf_goal % "proof"
wenzelm@63579
    53
  and "hence" :: prf_goal % "proof"
wenzelm@50128
    54
  and "show" :: prf_asm_goal % "proof"
wenzelm@63579
    55
  and "thus" :: prf_asm_goal % "proof"
wenzelm@48641
    56
  and "then" "from" "with" :: prf_chain % "proof"
wenzelm@60371
    57
  and "note" :: prf_decl % "proof"
wenzelm@60371
    58
  and "supply" :: prf_script % "proof"
wenzelm@60371
    59
  and "using" "unfolding" :: prf_decl % "proof"
wenzelm@67119
    60
  and "fix" "assume" "presume" "define" :: prf_asm % "proof"
wenzelm@60448
    61
  and "consider" :: prf_goal % "proof"
wenzelm@53371
    62
  and "obtain" :: prf_asm_goal % "proof"
wenzelm@60624
    63
  and "guess" :: prf_script_asm_goal % "proof"
wenzelm@48641
    64
  and "let" "write" :: prf_decl % "proof"
wenzelm@48641
    65
  and "case" :: prf_asm % "proof"
wenzelm@48641
    66
  and "{" :: prf_open % "proof"
wenzelm@48641
    67
  and "}" :: prf_close % "proof"
wenzelm@60694
    68
  and "next" :: next_block % "proof"
wenzelm@48641
    69
  and "qed" :: qed_block % "proof"
wenzelm@62312
    70
  and "by" ".." "." "sorry" "\<proof>" :: "qed" % "proof"
wenzelm@53571
    71
  and "done" :: "qed_script" % "proof"
wenzelm@48641
    72
  and "oops" :: qed_global % "proof"
wenzelm@50128
    73
  and "defer" "prefer" "apply" :: prf_script % "proof"
wenzelm@63579
    74
  and "apply_end" :: prf_script % "proof"
wenzelm@60624
    75
  and "subgoal" :: prf_script_goal % "proof"
wenzelm@48641
    76
  and "proof" :: prf_block % "proof"
wenzelm@48641
    77
  and "also" "moreover" :: prf_decl % "proof"
wenzelm@48641
    78
  and "finally" "ultimately" :: prf_chain % "proof"
wenzelm@48641
    79
  and "back" :: prf_script % "proof"
wenzelm@61252
    80
  and "help" "print_commands" "print_options" "print_context" "print_theory"
wenzelm@61252
    81
    "print_definitions" "print_syntax" "print_abbrevs" "print_defn_rules"
wenzelm@48641
    82
    "print_theorems" "print_locales" "print_classes" "print_locale"
wenzelm@48641
    83
    "print_interps" "print_dependencies" "print_attributes"
wenzelm@48641
    84
    "print_simpset" "print_rules" "print_trans_rules" "print_methods"
wenzelm@56069
    85
    "print_antiquotations" "print_ML_antiquotations" "thy_deps"
wenzelm@58845
    86
    "locale_deps" "class_deps" "thm_deps" "print_term_bindings"
wenzelm@57415
    87
    "print_facts" "print_cases" "print_statement" "thm" "prf" "full_prf"
wenzelm@57415
    88
    "prop" "term" "typ" "print_codesetup" "unused_thms" :: diag
wenzelm@67263
    89
  and "print_state" :: diag
wenzelm@48646
    90
  and "welcome" :: diag
wenzelm@68505
    91
  and "end" :: thy_end
wenzelm@63579
    92
  and "realizers" :: thy_decl
wenzelm@63579
    93
  and "realizability" :: thy_decl
wenzelm@56797
    94
  and "extract_type" "extract" :: thy_decl
wenzelm@48646
    95
  and "find_theorems" "find_consts" :: diag
wenzelm@57886
    96
  and "named_theorems" :: thy_decl
wenzelm@67013
    97
abbrevs "===>" = "===>"  (*prevent replacement of very long arrows*)
wenzelm@67013
    98
  and "--->" = "\<midarrow>\<rightarrow>"
wenzelm@67724
    99
  and "hence" "thus" "default_sort" "simproc_setup" "apply_end" "realizers" "realizability" = ""
wenzelm@67013
   100
  and "hence" = "then have"
wenzelm@67013
   101
  and "thus" = "then show"
wenzelm@48638
   102
begin
wenzelm@15803
   103
wenzelm@62849
   104
section \<open>Isar commands\<close>
wenzelm@62849
   105
wenzelm@67281
   106
subsection \<open>Other files\<close>
wenzelm@66757
   107
wenzelm@66757
   108
ML \<open>
wenzelm@67281
   109
local
wenzelm@67281
   110
  val _ =
wenzelm@67281
   111
    Outer_Syntax.command \<^command_keyword>\<open>external_file\<close> "formal dependency on external file"
wenzelm@69262
   112
      (Resources.provide_parse_files "external_file" >> (fn files => Toplevel.theory (#2 o files)));
wenzelm@67281
   113
wenzelm@67281
   114
  val _ =
wenzelm@67281
   115
    Outer_Syntax.command \<^command_keyword>\<open>bibtex_file\<close> "check bibtex database file in Prover IDE"
wenzelm@69262
   116
      (Resources.provide_parse_files "bibtex_file" >> (fn files =>
wenzelm@69262
   117
        Toplevel.theory (fn thy =>
wenzelm@67281
   118
          let
wenzelm@69262
   119
            val ([{lines, pos, ...}], thy') = files thy;
wenzelm@69262
   120
            val _ = Bibtex.check_database_output pos (cat_lines lines);
wenzelm@69262
   121
          in thy' end)));
wenzelm@69381
   122
wenzelm@69381
   123
  val _ =
wenzelm@69381
   124
    Outer_Syntax.local_theory \<^command_keyword>\<open>generate_file\<close>
wenzelm@69381
   125
      "generate source file, with antiquotations"
wenzelm@69381
   126
      (Parse.position Parse.path -- (\<^keyword>\<open>=\<close> |-- Parse.input Parse.embedded)
wenzelm@69383
   127
        >> Generated_Files.generate_file_cmd);
wenzelm@69381
   128
wenzelm@69662
   129
  val _ =
wenzelm@69662
   130
    Outer_Syntax.command \<^command_keyword>\<open>export_generated_files\<close>
wenzelm@69662
   131
      "export generated files from this or other theories"
wenzelm@69662
   132
      (Scan.repeat Parse.name_position >> (fn names =>
wenzelm@69662
   133
        Toplevel.keep (fn st =>
wenzelm@69662
   134
          let
wenzelm@69662
   135
            val ctxt = Toplevel.context_of st;
wenzelm@69662
   136
            val thy = Toplevel.theory_of st;
wenzelm@69662
   137
            val other_thys =
wenzelm@69662
   138
              if null names then [thy]
wenzelm@69662
   139
              else map (Theory.check {long = false} ctxt) names;
wenzelm@69662
   140
            val paths = Generated_Files.export_files thy other_thys;
wenzelm@69662
   141
          in
wenzelm@69662
   142
            if not (null paths) then
wenzelm@69662
   143
              (writeln (Export.message thy "");
wenzelm@69662
   144
               writeln (cat_lines (paths |> map (fn (path, pos) =>
wenzelm@69662
   145
                Markup.markup (Markup.entityN, Position.def_properties_of pos)
wenzelm@69662
   146
                  (quote (Path.implode path))))))
wenzelm@69662
   147
            else ()
wenzelm@69662
   148
          end)));
wenzelm@67281
   149
in end\<close>
wenzelm@66757
   150
wenzelm@66757
   151
wenzelm@62856
   152
subsection \<open>Embedded ML text\<close>
wenzelm@62849
   153
wenzelm@62856
   154
ML \<open>
wenzelm@62849
   155
local
wenzelm@62849
   156
wenzelm@67147
   157
val semi = Scan.option \<^keyword>\<open>;\<close>;
wenzelm@62902
   158
wenzelm@62849
   159
val _ =
wenzelm@67147
   160
  Outer_Syntax.command \<^command_keyword>\<open>ML_file\<close> "read and evaluate Isabelle/ML file"
wenzelm@62902
   161
    (Resources.parse_files "ML_file" --| semi >> ML_File.ML NONE);
wenzelm@62849
   162
wenzelm@62849
   163
val _ =
wenzelm@67147
   164
  Outer_Syntax.command \<^command_keyword>\<open>ML_file_debug\<close>
wenzelm@62849
   165
    "read and evaluate Isabelle/ML file (with debugger information)"
wenzelm@62902
   166
    (Resources.parse_files "ML_file_debug" --| semi >> ML_File.ML (SOME true));
wenzelm@62849
   167
wenzelm@62849
   168
val _ =
wenzelm@67147
   169
  Outer_Syntax.command \<^command_keyword>\<open>ML_file_no_debug\<close>
wenzelm@62849
   170
    "read and evaluate Isabelle/ML file (no debugger information)"
wenzelm@62902
   171
    (Resources.parse_files "ML_file_no_debug" --| semi >> ML_File.ML (SOME false));
wenzelm@62849
   172
wenzelm@62849
   173
val _ =
wenzelm@67147
   174
  Outer_Syntax.command \<^command_keyword>\<open>SML_file\<close> "read and evaluate Standard ML file"
wenzelm@62902
   175
    (Resources.parse_files "SML_file" --| semi >> ML_File.SML NONE);
wenzelm@62849
   176
wenzelm@62849
   177
val _ =
wenzelm@67147
   178
  Outer_Syntax.command \<^command_keyword>\<open>SML_file_debug\<close>
wenzelm@62849
   179
    "read and evaluate Standard ML file (with debugger information)"
wenzelm@62902
   180
    (Resources.parse_files "SML_file_debug" --| semi >> ML_File.SML (SOME true));
wenzelm@62849
   181
wenzelm@62849
   182
val _ =
wenzelm@67147
   183
  Outer_Syntax.command \<^command_keyword>\<open>SML_file_no_debug\<close>
wenzelm@62849
   184
    "read and evaluate Standard ML file (no debugger information)"
wenzelm@62902
   185
    (Resources.parse_files "SML_file_no_debug" --| semi >> ML_File.SML (SOME false));
wenzelm@62849
   186
wenzelm@62849
   187
val _ =
wenzelm@67147
   188
  Outer_Syntax.command \<^command_keyword>\<open>SML_export\<close> "evaluate SML within Isabelle/ML environment"
wenzelm@62849
   189
    (Parse.ML_source >> (fn source =>
wenzelm@62849
   190
      let
wenzelm@62849
   191
        val flags: ML_Compiler.flags =
wenzelm@68820
   192
          {environment = ML_Env.SML_export, redirect = false, verbose = true,
wenzelm@62902
   193
            debug = NONE, writeln = writeln, warning = warning};
wenzelm@62849
   194
      in
wenzelm@62849
   195
        Toplevel.theory
wenzelm@62849
   196
          (Context.theory_map (ML_Context.exec (fn () => ML_Context.eval_source flags source)))
wenzelm@62849
   197
      end));
wenzelm@62849
   198
wenzelm@62849
   199
val _ =
wenzelm@67147
   200
  Outer_Syntax.command \<^command_keyword>\<open>SML_import\<close> "evaluate Isabelle/ML within SML environment"
wenzelm@62849
   201
    (Parse.ML_source >> (fn source =>
wenzelm@62849
   202
      let
wenzelm@62849
   203
        val flags: ML_Compiler.flags =
wenzelm@68820
   204
          {environment = ML_Env.SML_import, redirect = false, verbose = true,
wenzelm@62902
   205
            debug = NONE, writeln = writeln, warning = warning};
wenzelm@62849
   206
      in
wenzelm@62849
   207
        Toplevel.generic_theory
wenzelm@62849
   208
          (ML_Context.exec (fn () => ML_Context.eval_source flags source) #>
wenzelm@62849
   209
            Local_Theory.propagate_ml_env)
wenzelm@62849
   210
      end));
wenzelm@62849
   211
wenzelm@62849
   212
val _ =
wenzelm@68276
   213
  Outer_Syntax.command ("ML_export", \<^here>)
wenzelm@68276
   214
    "ML text within theory or local theory, and export to bootstrap environment"
wenzelm@68276
   215
    (Parse.ML_source >> (fn source =>
wenzelm@68276
   216
      Toplevel.generic_theory (fn context =>
wenzelm@68276
   217
        context
wenzelm@68826
   218
        |> Config.put_generic ML_Env.ML_environment ML_Env.Isabelle
wenzelm@68816
   219
        |> Config.put_generic ML_Env.ML_write_global true
wenzelm@68276
   220
        |> ML_Context.exec (fn () =>
wenzelm@68276
   221
            ML_Context.eval_source (ML_Compiler.verbose true ML_Compiler.flags) source)
wenzelm@68827
   222
        |> Config.restore_generic ML_Env.ML_write_global context
wenzelm@68827
   223
        |> Config.restore_generic ML_Env.ML_environment context
wenzelm@68276
   224
        |> Local_Theory.propagate_ml_env)));
wenzelm@68276
   225
wenzelm@68276
   226
val _ =
wenzelm@67147
   227
  Outer_Syntax.command \<^command_keyword>\<open>ML_prf\<close> "ML text within proof"
wenzelm@62849
   228
    (Parse.ML_source >> (fn source =>
wenzelm@62849
   229
      Toplevel.proof (Proof.map_context (Context.proof_map
wenzelm@62849
   230
        (ML_Context.exec (fn () =>
wenzelm@62849
   231
            ML_Context.eval_source (ML_Compiler.verbose true ML_Compiler.flags) source))) #>
wenzelm@62849
   232
          Proof.propagate_ml_env)));
wenzelm@62849
   233
wenzelm@62849
   234
val _ =
wenzelm@67147
   235
  Outer_Syntax.command \<^command_keyword>\<open>ML_val\<close> "diagnostic ML text"
wenzelm@62849
   236
    (Parse.ML_source >> Isar_Cmd.ml_diag true);
wenzelm@62849
   237
wenzelm@62849
   238
val _ =
wenzelm@67147
   239
  Outer_Syntax.command \<^command_keyword>\<open>ML_command\<close> "diagnostic ML text (silent)"
wenzelm@62849
   240
    (Parse.ML_source >> Isar_Cmd.ml_diag false);
wenzelm@62849
   241
wenzelm@62849
   242
val _ =
wenzelm@67147
   243
  Outer_Syntax.command \<^command_keyword>\<open>setup\<close> "ML setup for global theory"
wenzelm@62849
   244
    (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.setup));
wenzelm@62849
   245
wenzelm@62849
   246
val _ =
wenzelm@67147
   247
  Outer_Syntax.local_theory \<^command_keyword>\<open>local_setup\<close> "ML setup for local theory"
wenzelm@62849
   248
    (Parse.ML_source >> Isar_Cmd.local_setup);
wenzelm@62849
   249
wenzelm@62849
   250
val _ =
wenzelm@67147
   251
  Outer_Syntax.command \<^command_keyword>\<open>oracle\<close> "declare oracle"
wenzelm@67147
   252
    (Parse.range Parse.name -- (\<^keyword>\<open>=\<close> |-- Parse.ML_source) >>
wenzelm@62856
   253
      (fn (x, y) => Toplevel.theory (Isar_Cmd.oracle x y)));
wenzelm@62856
   254
wenzelm@62856
   255
val _ =
wenzelm@67147
   256
  Outer_Syntax.local_theory \<^command_keyword>\<open>attribute_setup\<close> "define attribute in ML"
wenzelm@69349
   257
    (Parse.name_position --
wenzelm@67147
   258
        Parse.!!! (\<^keyword>\<open>=\<close> |-- Parse.ML_source -- Scan.optional Parse.text "")
wenzelm@62849
   259
      >> (fn (name, (txt, cmt)) => Attrib.attribute_setup name txt cmt));
wenzelm@62849
   260
wenzelm@62849
   261
val _ =
wenzelm@67147
   262
  Outer_Syntax.local_theory \<^command_keyword>\<open>method_setup\<close> "define proof method in ML"
wenzelm@69349
   263
    (Parse.name_position --
wenzelm@67147
   264
        Parse.!!! (\<^keyword>\<open>=\<close> |-- Parse.ML_source -- Scan.optional Parse.text "")
wenzelm@62849
   265
      >> (fn (name, (txt, cmt)) => Method.method_setup name txt cmt));
wenzelm@62849
   266
wenzelm@62849
   267
val _ =
wenzelm@67147
   268
  Outer_Syntax.local_theory \<^command_keyword>\<open>declaration\<close> "generic ML declaration"
wenzelm@62849
   269
    (Parse.opt_keyword "pervasive" -- Parse.ML_source
wenzelm@62849
   270
      >> (fn (pervasive, txt) => Isar_Cmd.declaration {syntax = false, pervasive = pervasive} txt));
wenzelm@62849
   271
wenzelm@62849
   272
val _ =
wenzelm@67147
   273
  Outer_Syntax.local_theory \<^command_keyword>\<open>syntax_declaration\<close> "generic ML syntax declaration"
wenzelm@62849
   274
    (Parse.opt_keyword "pervasive" -- Parse.ML_source
wenzelm@62849
   275
      >> (fn (pervasive, txt) => Isar_Cmd.declaration {syntax = true, pervasive = pervasive} txt));
wenzelm@62849
   276
wenzelm@62849
   277
val _ =
wenzelm@67147
   278
  Outer_Syntax.local_theory \<^command_keyword>\<open>simproc_setup\<close> "define simproc in ML"
wenzelm@69349
   279
    (Parse.name_position --
wenzelm@67147
   280
      (\<^keyword>\<open>(\<close> |-- Parse.enum1 "|" Parse.term --| \<^keyword>\<open>)\<close> --| \<^keyword>\<open>=\<close>) --
wenzelm@62913
   281
      Parse.ML_source >> (fn ((a, b), c) => Isar_Cmd.simproc_setup a b c));
wenzelm@62849
   282
wenzelm@62856
   283
in end\<close>
wenzelm@62849
   284
wenzelm@62856
   285
wenzelm@62856
   286
subsection \<open>Theory commands\<close>
wenzelm@62856
   287
wenzelm@62856
   288
subsubsection \<open>Sorts and types\<close>
wenzelm@62856
   289
wenzelm@62856
   290
ML \<open>
wenzelm@62856
   291
local
wenzelm@62856
   292
wenzelm@62856
   293
val _ =
wenzelm@67147
   294
  Outer_Syntax.local_theory \<^command_keyword>\<open>default_sort\<close>
wenzelm@62856
   295
    "declare default sort for explicit type variables"
wenzelm@62856
   296
    (Parse.sort >> (fn s => fn lthy => Local_Theory.set_defsort (Syntax.read_sort lthy s) lthy));
wenzelm@62856
   297
wenzelm@62856
   298
val _ =
wenzelm@67147
   299
  Outer_Syntax.local_theory \<^command_keyword>\<open>typedecl\<close> "type declaration"
wenzelm@62856
   300
    (Parse.type_args -- Parse.binding -- Parse.opt_mixfix
wenzelm@62856
   301
      >> (fn ((args, a), mx) =>
wenzelm@62856
   302
          Typedecl.typedecl {final = true} (a, map (rpair dummyS) args, mx) #> snd));
wenzelm@62856
   303
wenzelm@62856
   304
val _ =
wenzelm@67147
   305
  Outer_Syntax.local_theory \<^command_keyword>\<open>type_synonym\<close> "declare type abbreviation"
wenzelm@62856
   306
    (Parse.type_args -- Parse.binding --
wenzelm@67147
   307
      (\<^keyword>\<open>=\<close> |-- Parse.!!! (Parse.typ -- Parse.opt_mixfix'))
wenzelm@62856
   308
      >> (fn ((args, a), (rhs, mx)) => snd o Typedecl.abbrev_cmd (a, args, mx) rhs));
wenzelm@62856
   309
wenzelm@62856
   310
in end\<close>
wenzelm@62856
   311
wenzelm@62856
   312
wenzelm@62856
   313
subsubsection \<open>Consts\<close>
wenzelm@62856
   314
wenzelm@62856
   315
ML \<open>
wenzelm@62856
   316
local
wenzelm@62856
   317
wenzelm@62856
   318
val _ =
wenzelm@67147
   319
  Outer_Syntax.command \<^command_keyword>\<open>judgment\<close> "declare object-logic judgment"
wenzelm@62856
   320
    (Parse.const_binding >> (Toplevel.theory o Object_Logic.add_judgment_cmd));
wenzelm@62856
   321
wenzelm@62856
   322
val _ =
wenzelm@67147
   323
  Outer_Syntax.command \<^command_keyword>\<open>consts\<close> "declare constants"
wenzelm@62856
   324
    (Scan.repeat1 Parse.const_binding >> (Toplevel.theory o Sign.add_consts_cmd));
wenzelm@62856
   325
wenzelm@62856
   326
in end\<close>
wenzelm@62856
   327
wenzelm@62856
   328
wenzelm@62856
   329
subsubsection \<open>Syntax and translations\<close>
wenzelm@62856
   330
wenzelm@62856
   331
ML \<open>
wenzelm@62856
   332
local
wenzelm@62856
   333
wenzelm@62856
   334
val _ =
wenzelm@67147
   335
  Outer_Syntax.command \<^command_keyword>\<open>nonterminal\<close>
wenzelm@62856
   336
    "declare syntactic type constructors (grammar nonterminal symbols)"
wenzelm@62856
   337
    (Parse.and_list1 Parse.binding >> (Toplevel.theory o Sign.add_nonterminals_global));
wenzelm@62856
   338
wenzelm@62856
   339
val _ =
wenzelm@67147
   340
  Outer_Syntax.command \<^command_keyword>\<open>syntax\<close> "add raw syntax clauses"
wenzelm@62856
   341
    (Parse.syntax_mode -- Scan.repeat1 Parse.const_decl
wenzelm@62856
   342
      >> (Toplevel.theory o uncurry Sign.add_syntax_cmd));
wenzelm@62856
   343
wenzelm@62856
   344
val _ =
wenzelm@67147
   345
  Outer_Syntax.command \<^command_keyword>\<open>no_syntax\<close> "delete raw syntax clauses"
wenzelm@62856
   346
    (Parse.syntax_mode -- Scan.repeat1 Parse.const_decl
wenzelm@62856
   347
      >> (Toplevel.theory o uncurry Sign.del_syntax_cmd));
wenzelm@62856
   348
wenzelm@62856
   349
val trans_pat =
wenzelm@62856
   350
  Scan.optional
wenzelm@67147
   351
    (\<^keyword>\<open>(\<close> |-- Parse.!!! (Parse.inner_syntax Parse.name --| \<^keyword>\<open>)\<close>)) "logic"
wenzelm@62856
   352
    -- Parse.inner_syntax Parse.string;
wenzelm@62856
   353
wenzelm@62856
   354
fun trans_arrow toks =
wenzelm@67147
   355
  ((\<^keyword>\<open>\<rightharpoonup>\<close> || \<^keyword>\<open>=>\<close>) >> K Syntax.Parse_Rule ||
wenzelm@67147
   356
    (\<^keyword>\<open>\<leftharpoondown>\<close> || \<^keyword>\<open><=\<close>) >> K Syntax.Print_Rule ||
wenzelm@67147
   357
    (\<^keyword>\<open>\<rightleftharpoons>\<close> || \<^keyword>\<open>==\<close>) >> K Syntax.Parse_Print_Rule) toks;
wenzelm@62856
   358
wenzelm@62856
   359
val trans_line =
wenzelm@62856
   360
  trans_pat -- Parse.!!! (trans_arrow -- trans_pat)
wenzelm@62856
   361
    >> (fn (left, (arr, right)) => arr (left, right));
wenzelm@62856
   362
wenzelm@62856
   363
val _ =
wenzelm@67147
   364
  Outer_Syntax.command \<^command_keyword>\<open>translations\<close> "add syntax translation rules"
wenzelm@62856
   365
    (Scan.repeat1 trans_line >> (Toplevel.theory o Isar_Cmd.translations));
wenzelm@62856
   366
wenzelm@62856
   367
val _ =
wenzelm@67147
   368
  Outer_Syntax.command \<^command_keyword>\<open>no_translations\<close> "delete syntax translation rules"
wenzelm@62856
   369
    (Scan.repeat1 trans_line >> (Toplevel.theory o Isar_Cmd.no_translations));
wenzelm@62856
   370
wenzelm@62856
   371
in end\<close>
wenzelm@62856
   372
wenzelm@62856
   373
wenzelm@62856
   374
subsubsection \<open>Translation functions\<close>
wenzelm@62856
   375
wenzelm@62856
   376
ML \<open>
wenzelm@62856
   377
local
wenzelm@62849
   378
wenzelm@62849
   379
val _ =
wenzelm@67147
   380
  Outer_Syntax.command \<^command_keyword>\<open>parse_ast_translation\<close>
wenzelm@62849
   381
    "install parse ast translation functions"
wenzelm@62849
   382
    (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.parse_ast_translation));
wenzelm@62849
   383
wenzelm@62849
   384
val _ =
wenzelm@67147
   385
  Outer_Syntax.command \<^command_keyword>\<open>parse_translation\<close>
wenzelm@62849
   386
    "install parse translation functions"
wenzelm@62849
   387
    (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.parse_translation));
wenzelm@62849
   388
wenzelm@62849
   389
val _ =
wenzelm@67147
   390
  Outer_Syntax.command \<^command_keyword>\<open>print_translation\<close>
wenzelm@62849
   391
    "install print translation functions"
wenzelm@62849
   392
    (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.print_translation));
wenzelm@62849
   393
wenzelm@62849
   394
val _ =
wenzelm@67147
   395
  Outer_Syntax.command \<^command_keyword>\<open>typed_print_translation\<close>
wenzelm@62849
   396
    "install typed print translation functions"
wenzelm@62849
   397
    (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.typed_print_translation));
wenzelm@62849
   398
wenzelm@62849
   399
val _ =
wenzelm@67147
   400
  Outer_Syntax.command \<^command_keyword>\<open>print_ast_translation\<close>
wenzelm@62849
   401
    "install print ast translation functions"
wenzelm@62849
   402
    (Parse.ML_source >> (Toplevel.theory o Isar_Cmd.print_ast_translation));
wenzelm@62849
   403
wenzelm@62856
   404
in end\<close>
wenzelm@62849
   405
wenzelm@62856
   406
wenzelm@62856
   407
subsubsection \<open>Specifications\<close>
wenzelm@62856
   408
wenzelm@62856
   409
ML \<open>
wenzelm@62856
   410
local
wenzelm@62856
   411
wenzelm@62856
   412
val _ =
wenzelm@67147
   413
  Outer_Syntax.local_theory' \<^command_keyword>\<open>definition\<close> "constant definition"
wenzelm@63180
   414
    (Scan.option Parse_Spec.constdecl -- (Parse_Spec.opt_thm_name ":" -- Parse.prop) --
wenzelm@63180
   415
      Parse_Spec.if_assumes -- Parse.for_fixes >> (fn (((decl, spec), prems), params) =>
wenzelm@63180
   416
        #2 oo Specification.definition_cmd decl params prems spec));
wenzelm@62856
   417
wenzelm@62856
   418
val _ =
wenzelm@67147
   419
  Outer_Syntax.local_theory' \<^command_keyword>\<open>abbreviation\<close> "constant abbreviation"
wenzelm@63180
   420
    (Parse.syntax_mode -- Scan.option Parse_Spec.constdecl -- Parse.prop -- Parse.for_fixes
wenzelm@63180
   421
      >> (fn (((mode, decl), spec), params) => Specification.abbreviation_cmd mode decl params spec));
wenzelm@62856
   422
wenzelm@63178
   423
val axiomatization =
wenzelm@63178
   424
  Parse.and_list1 (Parse_Spec.thm_name ":" -- Parse.prop) --
wenzelm@63178
   425
  Parse_Spec.if_assumes -- Parse.for_fixes >> (fn ((a, b), c) => (c, b, a));
wenzelm@63178
   426
wenzelm@62856
   427
val _ =
wenzelm@67147
   428
  Outer_Syntax.command \<^command_keyword>\<open>axiomatization\<close> "axiomatic constant specification"
wenzelm@63285
   429
    (Scan.optional Parse.vars [] --
wenzelm@63178
   430
      Scan.optional (Parse.where_ |-- Parse.!!! axiomatization) ([], [], [])
wenzelm@63178
   431
      >> (fn (a, (b, c, d)) => Toplevel.theory (#2 o Specification.axiomatization_cmd a b c d)));
wenzelm@62856
   432
wenzelm@66248
   433
val _ =
wenzelm@67147
   434
  Outer_Syntax.local_theory \<^command_keyword>\<open>alias\<close> "name-space alias for constant"
wenzelm@69349
   435
    (Parse.binding -- (Parse.!!! \<^keyword>\<open>=\<close> |-- Parse.name_position)
wenzelm@66248
   436
      >> Specification.alias_cmd);
wenzelm@66248
   437
wenzelm@66248
   438
val _ =
wenzelm@67147
   439
  Outer_Syntax.local_theory \<^command_keyword>\<open>type_alias\<close> "name-space alias for type constructor"
wenzelm@69349
   440
    (Parse.binding -- (Parse.!!! \<^keyword>\<open>=\<close> |-- Parse.name_position)
wenzelm@66248
   441
      >> Specification.type_alias_cmd);
wenzelm@66248
   442
wenzelm@62856
   443
in end\<close>
wenzelm@62856
   444
wenzelm@62856
   445
wenzelm@62856
   446
subsubsection \<open>Notation\<close>
wenzelm@62856
   447
wenzelm@62856
   448
ML \<open>
wenzelm@62856
   449
local
wenzelm@62856
   450
wenzelm@62856
   451
val _ =
wenzelm@67147
   452
  Outer_Syntax.local_theory \<^command_keyword>\<open>type_notation\<close>
wenzelm@62856
   453
    "add concrete syntax for type constructors"
wenzelm@62856
   454
    (Parse.syntax_mode -- Parse.and_list1 (Parse.type_const -- Parse.mixfix)
wenzelm@62856
   455
      >> (fn (mode, args) => Specification.type_notation_cmd true mode args));
wenzelm@62856
   456
wenzelm@62856
   457
val _ =
wenzelm@67147
   458
  Outer_Syntax.local_theory \<^command_keyword>\<open>no_type_notation\<close>
wenzelm@62856
   459
    "delete concrete syntax for type constructors"
wenzelm@62856
   460
    (Parse.syntax_mode -- Parse.and_list1 (Parse.type_const -- Parse.mixfix)
wenzelm@62856
   461
      >> (fn (mode, args) => Specification.type_notation_cmd false mode args));
wenzelm@62856
   462
wenzelm@62856
   463
val _ =
wenzelm@67147
   464
  Outer_Syntax.local_theory \<^command_keyword>\<open>notation\<close>
wenzelm@62856
   465
    "add concrete syntax for constants / fixed variables"
wenzelm@62856
   466
    (Parse.syntax_mode -- Parse.and_list1 (Parse.const -- Parse.mixfix)
wenzelm@62856
   467
      >> (fn (mode, args) => Specification.notation_cmd true mode args));
wenzelm@62849
   468
wenzelm@62849
   469
val _ =
wenzelm@67147
   470
  Outer_Syntax.local_theory \<^command_keyword>\<open>no_notation\<close>
wenzelm@62856
   471
    "delete concrete syntax for constants / fixed variables"
wenzelm@62856
   472
    (Parse.syntax_mode -- Parse.and_list1 (Parse.const -- Parse.mixfix)
wenzelm@62856
   473
      >> (fn (mode, args) => Specification.notation_cmd false mode args));
wenzelm@62856
   474
wenzelm@62856
   475
in end\<close>
wenzelm@62856
   476
wenzelm@62856
   477
wenzelm@62856
   478
subsubsection \<open>Theorems\<close>
wenzelm@62856
   479
wenzelm@62856
   480
ML \<open>
wenzelm@62856
   481
local
wenzelm@62856
   482
wenzelm@63094
   483
val long_keyword =
wenzelm@63094
   484
  Parse_Spec.includes >> K "" ||
wenzelm@63094
   485
  Parse_Spec.long_statement_keyword;
wenzelm@63094
   486
wenzelm@63094
   487
val long_statement =
wenzelm@63352
   488
  Scan.optional (Parse_Spec.opt_thm_name ":" --| Scan.ahead long_keyword) Binding.empty_atts --
wenzelm@63094
   489
  Scan.optional Parse_Spec.includes [] -- Parse_Spec.long_statement
wenzelm@63094
   490
    >> (fn ((binding, includes), (elems, concl)) => (true, binding, includes, elems, concl));
wenzelm@63094
   491
wenzelm@63094
   492
val short_statement =
wenzelm@63094
   493
  Parse_Spec.statement -- Parse_Spec.if_statement -- Parse.for_fixes
wenzelm@63094
   494
    >> (fn ((shows, assumes), fixes) =>
wenzelm@63352
   495
      (false, Binding.empty_atts, [], [Element.Fixes fixes, Element.Assumes assumes],
wenzelm@63094
   496
        Element.Shows shows));
wenzelm@63094
   497
wenzelm@63094
   498
fun theorem spec schematic descr =
wenzelm@63094
   499
  Outer_Syntax.local_theory_to_proof' spec ("state " ^ descr)
wenzelm@63094
   500
    ((long_statement || short_statement) >> (fn (long, binding, includes, elems, concl) =>
wenzelm@63094
   501
      ((if schematic then Specification.schematic_theorem_cmd else Specification.theorem_cmd)
wenzelm@63094
   502
        long Thm.theoremK NONE (K I) binding includes elems concl)));
wenzelm@63094
   503
wenzelm@67147
   504
val _ = theorem \<^command_keyword>\<open>theorem\<close> false "theorem";
wenzelm@67147
   505
val _ = theorem \<^command_keyword>\<open>lemma\<close> false "lemma";
wenzelm@67147
   506
val _ = theorem \<^command_keyword>\<open>corollary\<close> false "corollary";
wenzelm@67147
   507
val _ = theorem \<^command_keyword>\<open>proposition\<close> false "proposition";
wenzelm@67147
   508
val _ = theorem \<^command_keyword>\<open>schematic_goal\<close> true "schematic goal";
wenzelm@63094
   509
wenzelm@63094
   510
in end\<close>
wenzelm@63094
   511
wenzelm@63094
   512
ML \<open>
wenzelm@63094
   513
local
wenzelm@63094
   514
wenzelm@62856
   515
val _ =
wenzelm@67147
   516
  Outer_Syntax.local_theory' \<^command_keyword>\<open>lemmas\<close> "define theorems"
wenzelm@62856
   517
    (Parse_Spec.name_facts -- Parse.for_fixes >>
wenzelm@62856
   518
      (fn (facts, fixes) => #2 oo Specification.theorems_cmd Thm.theoremK facts fixes));
wenzelm@62856
   519
wenzelm@62856
   520
val _ =
wenzelm@67147
   521
  Outer_Syntax.local_theory' \<^command_keyword>\<open>declare\<close> "declare theorems"
wenzelm@62969
   522
    (Parse.and_list1 Parse.thms1 -- Parse.for_fixes
wenzelm@62856
   523
      >> (fn (facts, fixes) =>
wenzelm@63352
   524
          #2 oo Specification.theorems_cmd "" [(Binding.empty_atts, flat facts)] fixes));
wenzelm@62856
   525
wenzelm@62856
   526
val _ =
wenzelm@67147
   527
  Outer_Syntax.local_theory \<^command_keyword>\<open>named_theorems\<close>
wenzelm@62856
   528
    "declare named collection of theorems"
wenzelm@62856
   529
    (Parse.and_list1 (Parse.binding -- Scan.optional Parse.text "") >>
wenzelm@62856
   530
      fold (fn (b, descr) => snd o Named_Theorems.declare b descr));
wenzelm@62856
   531
wenzelm@62856
   532
in end\<close>
wenzelm@62849
   533
wenzelm@62849
   534
wenzelm@62856
   535
subsubsection \<open>Hide names\<close>
wenzelm@62856
   536
wenzelm@62856
   537
ML \<open>
wenzelm@62856
   538
local
wenzelm@62856
   539
wenzelm@62856
   540
fun hide_names command_keyword what hide parse prep =
wenzelm@62856
   541
  Outer_Syntax.command command_keyword ("hide " ^ what ^ " from name space")
wenzelm@62856
   542
    ((Parse.opt_keyword "open" >> not) -- Scan.repeat1 parse >> (fn (fully, args) =>
wenzelm@62856
   543
      (Toplevel.theory (fn thy =>
wenzelm@62856
   544
        let val ctxt = Proof_Context.init_global thy
wenzelm@62856
   545
        in fold (hide fully o prep ctxt) args thy end))));
wenzelm@62856
   546
wenzelm@62856
   547
val _ =
wenzelm@67147
   548
  hide_names \<^command_keyword>\<open>hide_class\<close> "classes" Sign.hide_class Parse.class
wenzelm@62856
   549
    Proof_Context.read_class;
wenzelm@62856
   550
wenzelm@62856
   551
val _ =
wenzelm@67147
   552
  hide_names \<^command_keyword>\<open>hide_type\<close> "types" Sign.hide_type Parse.type_const
wenzelm@62856
   553
    ((#1 o dest_Type) oo Proof_Context.read_type_name {proper = true, strict = false});
wenzelm@62856
   554
wenzelm@62856
   555
val _ =
wenzelm@67147
   556
  hide_names \<^command_keyword>\<open>hide_const\<close> "consts" Sign.hide_const Parse.const
wenzelm@62856
   557
    ((#1 o dest_Const) oo Proof_Context.read_const {proper = true, strict = false});
wenzelm@62856
   558
wenzelm@62856
   559
val _ =
wenzelm@67147
   560
  hide_names \<^command_keyword>\<open>hide_fact\<close> "facts" Global_Theory.hide_fact
wenzelm@69349
   561
    Parse.name_position (Global_Theory.check_fact o Proof_Context.theory_of);
wenzelm@62856
   562
wenzelm@62856
   563
in end\<close>
wenzelm@62856
   564
wenzelm@62856
   565
wenzelm@62856
   566
subsection \<open>Bundled declarations\<close>
wenzelm@62856
   567
wenzelm@62856
   568
ML \<open>
wenzelm@62856
   569
local
wenzelm@62849
   570
wenzelm@62849
   571
val _ =
wenzelm@67147
   572
  Outer_Syntax.maybe_begin_local_theory \<^command_keyword>\<open>bundle\<close>
wenzelm@63273
   573
    "define bundle of declarations"
wenzelm@67147
   574
    ((Parse.binding --| \<^keyword>\<open>=\<close>) -- Parse.thms1 -- Parse.for_fixes
wenzelm@63273
   575
      >> (uncurry Bundle.bundle_cmd))
wenzelm@63273
   576
    (Parse.binding --| Parse.begin >> Bundle.init);
wenzelm@63270
   577
wenzelm@63270
   578
val _ =
wenzelm@67147
   579
  Outer_Syntax.local_theory \<^command_keyword>\<open>unbundle\<close>
wenzelm@63282
   580
    "activate declarations from bundle in local theory"
wenzelm@69349
   581
    (Scan.repeat1 Parse.name_position >> Bundle.unbundle_cmd);
wenzelm@63282
   582
wenzelm@63282
   583
val _ =
wenzelm@67147
   584
  Outer_Syntax.command \<^command_keyword>\<open>include\<close>
wenzelm@63282
   585
    "activate declarations from bundle in proof body"
wenzelm@69349
   586
    (Scan.repeat1 Parse.name_position >> (Toplevel.proof o Bundle.include_cmd));
wenzelm@62849
   587
wenzelm@62849
   588
val _ =
wenzelm@67147
   589
  Outer_Syntax.command \<^command_keyword>\<open>including\<close>
wenzelm@63282
   590
    "activate declarations from bundle in goal refinement"
wenzelm@69349
   591
    (Scan.repeat1 Parse.name_position >> (Toplevel.proof o Bundle.including_cmd));
wenzelm@62849
   592
wenzelm@62849
   593
val _ =
wenzelm@67147
   594
  Outer_Syntax.command \<^command_keyword>\<open>print_bundles\<close>
wenzelm@62849
   595
    "print bundles of declarations"
wenzelm@62849
   596
    (Parse.opt_bang >> (fn b => Toplevel.keep (Bundle.print_bundles b o Toplevel.context_of)));
wenzelm@62849
   597
wenzelm@62856
   598
in end\<close>
wenzelm@62849
   599
wenzelm@62856
   600
wenzelm@62856
   601
subsection \<open>Local theory specifications\<close>
wenzelm@62856
   602
wenzelm@62856
   603
subsubsection \<open>Specification context\<close>
wenzelm@62856
   604
wenzelm@62856
   605
ML \<open>
wenzelm@62856
   606
local
wenzelm@62849
   607
wenzelm@62849
   608
val _ =
wenzelm@67147
   609
  Outer_Syntax.command \<^command_keyword>\<open>context\<close> "begin local theory context"
wenzelm@69349
   610
    ((Parse.name_position >> (Toplevel.begin_local_theory true o Named_Target.begin) ||
wenzelm@62849
   611
      Scan.optional Parse_Spec.includes [] -- Scan.repeat Parse_Spec.context_element
wenzelm@62849
   612
        >> (fn (incls, elems) => Toplevel.open_target (#2 o Bundle.context_cmd incls elems)))
wenzelm@62849
   613
      --| Parse.begin);
wenzelm@62849
   614
wenzelm@62856
   615
val _ =
wenzelm@67147
   616
  Outer_Syntax.command \<^command_keyword>\<open>end\<close> "end context"
wenzelm@62856
   617
    (Scan.succeed
wenzelm@62856
   618
      (Toplevel.exit o Toplevel.end_local_theory o Toplevel.close_target o
wenzelm@62856
   619
        Toplevel.end_proof (K Proof.end_notepad)));
wenzelm@62849
   620
wenzelm@62856
   621
in end\<close>
wenzelm@62856
   622
wenzelm@62856
   623
wenzelm@62856
   624
subsubsection \<open>Locales and interpretation\<close>
wenzelm@62856
   625
wenzelm@62856
   626
ML \<open>
wenzelm@62856
   627
local
wenzelm@62849
   628
wenzelm@62849
   629
val locale_val =
wenzelm@62849
   630
  Parse_Spec.locale_expression --
wenzelm@67147
   631
    Scan.optional (\<^keyword>\<open>+\<close> |-- Parse.!!! (Scan.repeat1 Parse_Spec.context_element)) [] ||
wenzelm@62849
   632
  Scan.repeat1 Parse_Spec.context_element >> pair ([], []);
wenzelm@62849
   633
wenzelm@62849
   634
val _ =
wenzelm@67147
   635
  Outer_Syntax.command \<^command_keyword>\<open>locale\<close> "define named specification context"
wenzelm@62849
   636
    (Parse.binding --
wenzelm@67147
   637
      Scan.optional (\<^keyword>\<open>=\<close> |-- Parse.!!! locale_val) (([], []), []) -- Parse.opt_begin
wenzelm@62849
   638
      >> (fn ((name, (expr, elems)), begin) =>
wenzelm@62849
   639
          Toplevel.begin_local_theory begin
wenzelm@62849
   640
            (Expression.add_locale_cmd name Binding.empty expr elems #> snd)));
wenzelm@62849
   641
wenzelm@62849
   642
val _ =
wenzelm@67147
   643
  Outer_Syntax.command \<^command_keyword>\<open>experiment\<close> "open private specification context"
wenzelm@62849
   644
    (Scan.repeat Parse_Spec.context_element --| Parse.begin
wenzelm@62849
   645
      >> (fn elems =>
wenzelm@62849
   646
          Toplevel.begin_local_theory true (Experiment.experiment_cmd elems #> snd)));
wenzelm@62849
   647
wenzelm@62849
   648
val _ =
wenzelm@67147
   649
  Outer_Syntax.command \<^command_keyword>\<open>interpret\<close>
wenzelm@62849
   650
    "prove interpretation of locale expression in proof context"
ballarin@67740
   651
    (Parse.!!! Parse_Spec.locale_expression >> (fn expr =>
ballarin@67777
   652
      Toplevel.proof (Interpretation.interpret_cmd expr)));
wenzelm@62849
   653
wenzelm@62849
   654
val interpretation_args_with_defs =
wenzelm@62849
   655
  Parse.!!! Parse_Spec.locale_expression --
wenzelm@67147
   656
    (Scan.optional (\<^keyword>\<open>defines\<close> |-- Parse.and_list1 (Parse_Spec.opt_thm_name ":"
ballarin@67777
   657
      -- ((Parse.binding -- Parse.opt_mixfix') --| \<^keyword>\<open>=\<close> -- Parse.term))) ([]));
wenzelm@62849
   658
wenzelm@62849
   659
val _ =
wenzelm@67147
   660
  Outer_Syntax.local_theory_to_proof \<^command_keyword>\<open>global_interpretation\<close>
wenzelm@62849
   661
    "prove interpretation of locale expression into global theory"
ballarin@67777
   662
    (interpretation_args_with_defs >> (fn (expr, defs) =>
ballarin@67777
   663
      Interpretation.global_interpretation_cmd expr defs));
wenzelm@62849
   664
wenzelm@62849
   665
val _ =
wenzelm@67147
   666
  Outer_Syntax.command \<^command_keyword>\<open>sublocale\<close>
wenzelm@62849
   667
    "prove sublocale relation between a locale and a locale expression"
wenzelm@69349
   668
    ((Parse.name_position --| (\<^keyword>\<open>\<subseteq>\<close> || \<^keyword>\<open><\<close>) --
ballarin@67777
   669
      interpretation_args_with_defs >> (fn (loc, (expr, defs)) =>
ballarin@67777
   670
        Toplevel.theory_to_proof (Interpretation.global_sublocale_cmd loc expr defs)))
ballarin@67777
   671
    || interpretation_args_with_defs >> (fn (expr, defs) =>
ballarin@67777
   672
        Toplevel.local_theory_to_proof NONE NONE (Interpretation.sublocale_cmd expr defs)));
wenzelm@62849
   673
wenzelm@62849
   674
val _ =
wenzelm@67147
   675
  Outer_Syntax.command \<^command_keyword>\<open>interpretation\<close>
wenzelm@62849
   676
    "prove interpretation of locale expression in local theory or into global theory"
ballarin@67740
   677
    (Parse.!!! Parse_Spec.locale_expression >> (fn expr =>
wenzelm@62856
   678
      Toplevel.local_theory_to_proof NONE NONE
ballarin@67777
   679
        (Interpretation.isar_interpretation_cmd expr)));
wenzelm@62856
   680
wenzelm@62856
   681
in end\<close>
wenzelm@62849
   682
wenzelm@62849
   683
wenzelm@62856
   684
subsubsection \<open>Type classes\<close>
wenzelm@62849
   685
wenzelm@62856
   686
ML \<open>
wenzelm@62856
   687
local
wenzelm@62849
   688
wenzelm@62849
   689
val class_val =
wenzelm@62849
   690
  Parse_Spec.class_expression --
wenzelm@67147
   691
    Scan.optional (\<^keyword>\<open>+\<close> |-- Parse.!!! (Scan.repeat1 Parse_Spec.context_element)) [] ||
wenzelm@62849
   692
  Scan.repeat1 Parse_Spec.context_element >> pair [];
wenzelm@62849
   693
wenzelm@62849
   694
val _ =
wenzelm@67147
   695
  Outer_Syntax.command \<^command_keyword>\<open>class\<close> "define type class"
wenzelm@67147
   696
   (Parse.binding -- Scan.optional (\<^keyword>\<open>=\<close> |-- class_val) ([], []) -- Parse.opt_begin
wenzelm@62849
   697
    >> (fn ((name, (supclasses, elems)), begin) =>
wenzelm@62849
   698
        Toplevel.begin_local_theory begin
wenzelm@62849
   699
          (Class_Declaration.class_cmd name supclasses elems #> snd)));
wenzelm@62849
   700
wenzelm@62849
   701
val _ =
wenzelm@67147
   702
  Outer_Syntax.local_theory_to_proof \<^command_keyword>\<open>subclass\<close> "prove a subclass relation"
wenzelm@62849
   703
    (Parse.class >> Class_Declaration.subclass_cmd);
wenzelm@62849
   704
wenzelm@62849
   705
val _ =
wenzelm@67147
   706
  Outer_Syntax.command \<^command_keyword>\<open>instantiation\<close> "instantiate and prove type arity"
wenzelm@62849
   707
   (Parse.multi_arity --| Parse.begin
wenzelm@62849
   708
     >> (fn arities => Toplevel.begin_local_theory true (Class.instantiation_cmd arities)));
wenzelm@62849
   709
wenzelm@62849
   710
val _ =
wenzelm@67147
   711
  Outer_Syntax.command \<^command_keyword>\<open>instance\<close> "prove type arity or subclass relation"
wenzelm@62849
   712
  ((Parse.class --
wenzelm@67147
   713
    ((\<^keyword>\<open>\<subseteq>\<close> || \<^keyword>\<open><\<close>) |-- Parse.!!! Parse.class) >> Class.classrel_cmd ||
wenzelm@62849
   714
    Parse.multi_arity >> Class.instance_arity_cmd) >> Toplevel.theory_to_proof ||
wenzelm@62849
   715
    Scan.succeed (Toplevel.local_theory_to_proof NONE NONE (Class.instantiation_instance I)));
wenzelm@62849
   716
wenzelm@62856
   717
in end\<close>
wenzelm@62849
   718
wenzelm@62856
   719
wenzelm@62856
   720
subsubsection \<open>Arbitrary overloading\<close>
wenzelm@62856
   721
wenzelm@62856
   722
ML \<open>
wenzelm@62856
   723
local
wenzelm@62849
   724
wenzelm@62849
   725
val _ =
wenzelm@67147
   726
  Outer_Syntax.command \<^command_keyword>\<open>overloading\<close> "overloaded definitions"
wenzelm@67147
   727
   (Scan.repeat1 (Parse.name --| (\<^keyword>\<open>==\<close> || \<^keyword>\<open>\<equiv>\<close>) -- Parse.term --
wenzelm@67147
   728
      Scan.optional (\<^keyword>\<open>(\<close> |-- (\<^keyword>\<open>unchecked\<close> >> K false) --| \<^keyword>\<open>)\<close>) true
wenzelm@62849
   729
      >> Scan.triple1) --| Parse.begin
wenzelm@62849
   730
   >> (fn operations => Toplevel.begin_local_theory true (Overloading.overloading_cmd operations)));
wenzelm@62849
   731
wenzelm@62856
   732
in end\<close>
wenzelm@62849
   733
wenzelm@62849
   734
wenzelm@62856
   735
subsection \<open>Proof commands\<close>
wenzelm@62849
   736
wenzelm@62856
   737
ML \<open>
wenzelm@62856
   738
local
wenzelm@62849
   739
wenzelm@62849
   740
val _ =
wenzelm@67147
   741
  Outer_Syntax.local_theory_to_proof \<^command_keyword>\<open>notepad\<close> "begin proof context"
wenzelm@62849
   742
    (Parse.begin >> K Proof.begin_notepad);
wenzelm@62849
   743
wenzelm@62856
   744
in end\<close>
wenzelm@62849
   745
wenzelm@62856
   746
wenzelm@62856
   747
subsubsection \<open>Statements\<close>
wenzelm@62856
   748
wenzelm@62856
   749
ML \<open>
wenzelm@62856
   750
local
wenzelm@62849
   751
wenzelm@62849
   752
val structured_statement =
wenzelm@62849
   753
  Parse_Spec.statement -- Parse_Spec.cond_statement -- Parse.for_fixes
wenzelm@62849
   754
    >> (fn ((shows, (strict, assumes)), fixes) => (strict, fixes, assumes, shows));
wenzelm@62849
   755
wenzelm@62849
   756
val _ =
wenzelm@67147
   757
  Outer_Syntax.command \<^command_keyword>\<open>have\<close> "state local goal"
wenzelm@62849
   758
    (structured_statement >> (fn (a, b, c, d) =>
wenzelm@62849
   759
      Toplevel.proof' (fn int => Proof.have_cmd a NONE (K I) b c d int #> #2)));
wenzelm@62849
   760
wenzelm@62849
   761
val _ =
wenzelm@67147
   762
  Outer_Syntax.command \<^command_keyword>\<open>show\<close> "state local goal, to refine pending subgoals"
wenzelm@62849
   763
    (structured_statement >> (fn (a, b, c, d) =>
wenzelm@62849
   764
      Toplevel.proof' (fn int => Proof.show_cmd a NONE (K I) b c d int #> #2)));
wenzelm@62849
   765
wenzelm@62849
   766
val _ =
wenzelm@67147
   767
  Outer_Syntax.command \<^command_keyword>\<open>hence\<close> "old-style alias of \"then have\""
wenzelm@62849
   768
    (structured_statement >> (fn (a, b, c, d) =>
wenzelm@62849
   769
      Toplevel.proof' (fn int => Proof.chain #> Proof.have_cmd a NONE (K I) b c d int #> #2)));
wenzelm@62849
   770
wenzelm@62849
   771
val _ =
wenzelm@67147
   772
  Outer_Syntax.command \<^command_keyword>\<open>thus\<close> "old-style alias of  \"then show\""
wenzelm@62849
   773
    (structured_statement >> (fn (a, b, c, d) =>
wenzelm@62849
   774
      Toplevel.proof' (fn int => Proof.chain #> Proof.show_cmd a NONE (K I) b c d int #> #2)));
wenzelm@62849
   775
wenzelm@62856
   776
in end\<close>
wenzelm@62849
   777
wenzelm@62856
   778
wenzelm@62856
   779
subsubsection \<open>Local facts\<close>
wenzelm@62856
   780
wenzelm@62856
   781
ML \<open>
wenzelm@62856
   782
local
wenzelm@62849
   783
wenzelm@62969
   784
val facts = Parse.and_list1 Parse.thms1;
wenzelm@62849
   785
wenzelm@62849
   786
val _ =
wenzelm@67147
   787
  Outer_Syntax.command \<^command_keyword>\<open>then\<close> "forward chaining"
wenzelm@62849
   788
    (Scan.succeed (Toplevel.proof Proof.chain));
wenzelm@62849
   789
wenzelm@62849
   790
val _ =
wenzelm@67147
   791
  Outer_Syntax.command \<^command_keyword>\<open>from\<close> "forward chaining from given facts"
wenzelm@62849
   792
    (facts >> (Toplevel.proof o Proof.from_thmss_cmd));
wenzelm@62849
   793
wenzelm@62849
   794
val _ =
wenzelm@67147
   795
  Outer_Syntax.command \<^command_keyword>\<open>with\<close> "forward chaining from given and current facts"
wenzelm@62849
   796
    (facts >> (Toplevel.proof o Proof.with_thmss_cmd));
wenzelm@62849
   797
wenzelm@62849
   798
val _ =
wenzelm@67147
   799
  Outer_Syntax.command \<^command_keyword>\<open>note\<close> "define facts"
wenzelm@62849
   800
    (Parse_Spec.name_facts >> (Toplevel.proof o Proof.note_thmss_cmd));
wenzelm@62849
   801
wenzelm@62849
   802
val _ =
wenzelm@67147
   803
  Outer_Syntax.command \<^command_keyword>\<open>supply\<close> "define facts during goal refinement (unstructured)"
wenzelm@62849
   804
    (Parse_Spec.name_facts >> (Toplevel.proof o Proof.supply_cmd));
wenzelm@62849
   805
wenzelm@62849
   806
val _ =
wenzelm@67147
   807
  Outer_Syntax.command \<^command_keyword>\<open>using\<close> "augment goal facts"
wenzelm@62849
   808
    (facts >> (Toplevel.proof o Proof.using_cmd));
wenzelm@62849
   809
wenzelm@62849
   810
val _ =
wenzelm@67147
   811
  Outer_Syntax.command \<^command_keyword>\<open>unfolding\<close> "unfold definitions in goal and facts"
wenzelm@62849
   812
    (facts >> (Toplevel.proof o Proof.unfolding_cmd));
wenzelm@62849
   813
wenzelm@62856
   814
in end\<close>
wenzelm@62849
   815
wenzelm@62856
   816
wenzelm@62856
   817
subsubsection \<open>Proof context\<close>
wenzelm@62856
   818
wenzelm@62856
   819
ML \<open>
wenzelm@62856
   820
local
wenzelm@62856
   821
wenzelm@62856
   822
val structured_statement =
wenzelm@62856
   823
  Parse_Spec.statement -- Parse_Spec.if_statement' -- Parse.for_fixes
wenzelm@62856
   824
    >> (fn ((shows, assumes), fixes) => (fixes, assumes, shows));
wenzelm@62849
   825
wenzelm@62849
   826
val _ =
wenzelm@67147
   827
  Outer_Syntax.command \<^command_keyword>\<open>fix\<close> "fix local variables (Skolem constants)"
wenzelm@63285
   828
    (Parse.vars >> (Toplevel.proof o Proof.fix_cmd));
wenzelm@62849
   829
wenzelm@62849
   830
val _ =
wenzelm@67147
   831
  Outer_Syntax.command \<^command_keyword>\<open>assume\<close> "assume propositions"
wenzelm@62856
   832
    (structured_statement >> (fn (a, b, c) => Toplevel.proof (Proof.assume_cmd a b c)));
wenzelm@62849
   833
wenzelm@62849
   834
val _ =
wenzelm@67147
   835
  Outer_Syntax.command \<^command_keyword>\<open>presume\<close> "assume propositions, to be established later"
wenzelm@62856
   836
    (structured_statement >> (fn (a, b, c) => Toplevel.proof (Proof.presume_cmd a b c)));
wenzelm@62849
   837
wenzelm@62849
   838
val _ =
wenzelm@67147
   839
  Outer_Syntax.command \<^command_keyword>\<open>define\<close> "local definition (non-polymorphic)"
wenzelm@63285
   840
    ((Parse.vars --| Parse.where_) -- Parse_Spec.statement -- Parse.for_fixes
wenzelm@63039
   841
      >> (fn ((a, b), c) => Toplevel.proof (Proof.define_cmd a c b)));
wenzelm@63039
   842
wenzelm@63039
   843
val _ =
wenzelm@67147
   844
  Outer_Syntax.command \<^command_keyword>\<open>consider\<close> "state cases rule"
wenzelm@62849
   845
    (Parse_Spec.obtains >> (Toplevel.proof' o Obtain.consider_cmd));
wenzelm@62849
   846
wenzelm@62849
   847
val _ =
wenzelm@67147
   848
  Outer_Syntax.command \<^command_keyword>\<open>obtain\<close> "generalized elimination"
wenzelm@63285
   849
    (Parse.parbinding -- Scan.optional (Parse.vars --| Parse.where_) [] -- structured_statement
wenzelm@63059
   850
      >> (fn ((a, b), (c, d, e)) => Toplevel.proof' (Obtain.obtain_cmd a b c d e)));
wenzelm@62849
   851
wenzelm@62849
   852
val _ =
wenzelm@67147
   853
  Outer_Syntax.command \<^command_keyword>\<open>guess\<close> "wild guessing (unstructured)"
wenzelm@63285
   854
    (Scan.optional Parse.vars [] >> (Toplevel.proof' o Obtain.guess_cmd));
wenzelm@62849
   855
wenzelm@62849
   856
val _ =
wenzelm@67147
   857
  Outer_Syntax.command \<^command_keyword>\<open>let\<close> "bind text variables"
wenzelm@67147
   858
    (Parse.and_list1 (Parse.and_list1 Parse.term -- (\<^keyword>\<open>=\<close> |-- Parse.term))
wenzelm@62849
   859
      >> (Toplevel.proof o Proof.let_bind_cmd));
wenzelm@62849
   860
wenzelm@62849
   861
val _ =
wenzelm@67147
   862
  Outer_Syntax.command \<^command_keyword>\<open>write\<close> "add concrete syntax for constants / fixed variables"
wenzelm@62856
   863
    (Parse.syntax_mode -- Parse.and_list1 (Parse.const -- Parse.mixfix)
wenzelm@62849
   864
    >> (fn (mode, args) => Toplevel.proof (Proof.write_cmd mode args)));
wenzelm@62849
   865
wenzelm@62849
   866
val _ =
wenzelm@67147
   867
  Outer_Syntax.command \<^command_keyword>\<open>case\<close> "invoke local context"
wenzelm@62849
   868
    (Parse_Spec.opt_thm_name ":" --
wenzelm@69349
   869
      (\<^keyword>\<open>(\<close> |-- Parse.!!! (Parse.name_position -- Scan.repeat (Parse.maybe Parse.binding)
wenzelm@67147
   870
          --| \<^keyword>\<open>)\<close>) ||
wenzelm@69349
   871
        Parse.name_position >> rpair []) >> (Toplevel.proof o Proof.case_cmd));
wenzelm@62849
   872
wenzelm@62856
   873
in end\<close>
wenzelm@62849
   874
wenzelm@62856
   875
wenzelm@62856
   876
subsubsection \<open>Proof structure\<close>
wenzelm@62856
   877
wenzelm@62856
   878
ML \<open>
wenzelm@62856
   879
local
wenzelm@62849
   880
wenzelm@62849
   881
val _ =
wenzelm@67147
   882
  Outer_Syntax.command \<^command_keyword>\<open>{\<close> "begin explicit proof block"
wenzelm@62849
   883
    (Scan.succeed (Toplevel.proof Proof.begin_block));
wenzelm@62849
   884
wenzelm@62849
   885
val _ =
wenzelm@67147
   886
  Outer_Syntax.command \<^command_keyword>\<open>}\<close> "end explicit proof block"
wenzelm@62849
   887
    (Scan.succeed (Toplevel.proof Proof.end_block));
wenzelm@62849
   888
wenzelm@62849
   889
val _ =
wenzelm@67147
   890
  Outer_Syntax.command \<^command_keyword>\<open>next\<close> "enter next proof block"
wenzelm@62849
   891
    (Scan.succeed (Toplevel.proof Proof.next_block));
wenzelm@62849
   892
wenzelm@62856
   893
in end\<close>
wenzelm@62849
   894
wenzelm@62856
   895
wenzelm@62856
   896
subsubsection \<open>End proof\<close>
wenzelm@62856
   897
wenzelm@62856
   898
ML \<open>
wenzelm@62856
   899
local
wenzelm@62849
   900
wenzelm@62849
   901
val _ =
wenzelm@67147
   902
  Outer_Syntax.command \<^command_keyword>\<open>qed\<close> "conclude proof"
wenzelm@62849
   903
    (Scan.option Method.parse >> (fn m =>
wenzelm@62849
   904
     (Option.map Method.report m;
wenzelm@62849
   905
      Isar_Cmd.qed m)));
wenzelm@62849
   906
wenzelm@62849
   907
val _ =
wenzelm@67147
   908
  Outer_Syntax.command \<^command_keyword>\<open>by\<close> "terminal backward proof"
wenzelm@62849
   909
    (Method.parse -- Scan.option Method.parse >> (fn (m1, m2) =>
wenzelm@62849
   910
     (Method.report m1;
wenzelm@62849
   911
      Option.map Method.report m2;
wenzelm@62849
   912
      Isar_Cmd.terminal_proof (m1, m2))));
wenzelm@62849
   913
wenzelm@62849
   914
val _ =
wenzelm@67147
   915
  Outer_Syntax.command \<^command_keyword>\<open>..\<close> "default proof"
wenzelm@62849
   916
    (Scan.succeed Isar_Cmd.default_proof);
wenzelm@62849
   917
wenzelm@62849
   918
val _ =
wenzelm@67147
   919
  Outer_Syntax.command \<^command_keyword>\<open>.\<close> "immediate proof"
wenzelm@62849
   920
    (Scan.succeed Isar_Cmd.immediate_proof);
wenzelm@62849
   921
wenzelm@62849
   922
val _ =
wenzelm@67147
   923
  Outer_Syntax.command \<^command_keyword>\<open>done\<close> "done proof"
wenzelm@62849
   924
    (Scan.succeed Isar_Cmd.done_proof);
wenzelm@62849
   925
wenzelm@62849
   926
val _ =
wenzelm@67147
   927
  Outer_Syntax.command \<^command_keyword>\<open>sorry\<close> "skip proof (quick-and-dirty mode only!)"
wenzelm@62849
   928
    (Scan.succeed Isar_Cmd.skip_proof);
wenzelm@62849
   929
wenzelm@62849
   930
val _ =
wenzelm@67147
   931
  Outer_Syntax.command \<^command_keyword>\<open>\<proof>\<close> "dummy proof (quick-and-dirty mode only!)"
wenzelm@62849
   932
    (Scan.succeed Isar_Cmd.skip_proof);
wenzelm@62849
   933
wenzelm@62849
   934
val _ =
wenzelm@67147
   935
  Outer_Syntax.command \<^command_keyword>\<open>oops\<close> "forget proof"
wenzelm@68876
   936
    (Scan.succeed Toplevel.forget_proof);
wenzelm@62849
   937
wenzelm@62856
   938
in end\<close>
wenzelm@62849
   939
wenzelm@62856
   940
wenzelm@62856
   941
subsubsection \<open>Proof steps\<close>
wenzelm@62856
   942
wenzelm@62856
   943
ML \<open>
wenzelm@62856
   944
local
wenzelm@62849
   945
wenzelm@62849
   946
val _ =
wenzelm@67147
   947
  Outer_Syntax.command \<^command_keyword>\<open>defer\<close> "shuffle internal proof state"
wenzelm@62849
   948
    (Scan.optional Parse.nat 1 >> (Toplevel.proof o Proof.defer));
wenzelm@62849
   949
wenzelm@62849
   950
val _ =
wenzelm@67147
   951
  Outer_Syntax.command \<^command_keyword>\<open>prefer\<close> "shuffle internal proof state"
wenzelm@62849
   952
    (Parse.nat >> (Toplevel.proof o Proof.prefer));
wenzelm@62849
   953
wenzelm@62849
   954
val _ =
wenzelm@67147
   955
  Outer_Syntax.command \<^command_keyword>\<open>apply\<close> "initial goal refinement step (unstructured)"
wenzelm@62849
   956
    (Method.parse >> (fn m => (Method.report m; Toplevel.proofs (Proof.apply m))));
wenzelm@62849
   957
wenzelm@62849
   958
val _ =
wenzelm@67147
   959
  Outer_Syntax.command \<^command_keyword>\<open>apply_end\<close> "terminal goal refinement step (unstructured)"
wenzelm@62849
   960
    (Method.parse >> (fn m => (Method.report m; Toplevel.proofs (Proof.apply_end m))));
wenzelm@62849
   961
wenzelm@62849
   962
val _ =
wenzelm@67147
   963
  Outer_Syntax.command \<^command_keyword>\<open>proof\<close> "backward proof step"
wenzelm@62849
   964
    (Scan.option Method.parse >> (fn m =>
wenzelm@63513
   965
      (Option.map Method.report m;
wenzelm@63513
   966
       Toplevel.proof (fn state =>
wenzelm@63513
   967
         let
wenzelm@63513
   968
          val state' = state |> Proof.proof m |> Seq.the_result "";
wenzelm@63513
   969
          val _ =
wenzelm@63513
   970
            Output.information
wenzelm@63513
   971
              (Proof_Context.print_cases_proof (Proof.context_of state) (Proof.context_of state'));
wenzelm@63513
   972
        in state' end))))
wenzelm@62849
   973
wenzelm@62856
   974
in end\<close>
wenzelm@62849
   975
wenzelm@62849
   976
wenzelm@62856
   977
subsubsection \<open>Subgoal focus\<close>
wenzelm@62856
   978
wenzelm@62856
   979
ML \<open>
wenzelm@62849
   980
local
wenzelm@62849
   981
wenzelm@62849
   982
val opt_fact_binding =
wenzelm@62849
   983
  Scan.optional (Parse.binding -- Parse.opt_attribs || Parse.attribs >> pair Binding.empty)
wenzelm@63352
   984
    Binding.empty_atts;
wenzelm@62849
   985
wenzelm@62849
   986
val for_params =
wenzelm@62849
   987
  Scan.optional
wenzelm@67147
   988
    (\<^keyword>\<open>for\<close> |--
wenzelm@62849
   989
      Parse.!!! ((Scan.option Parse.dots >> is_some) --
wenzelm@69349
   990
        (Scan.repeat1 (Parse.maybe_position Parse.name_position))))
wenzelm@62849
   991
    (false, []);
wenzelm@62849
   992
wenzelm@62849
   993
val _ =
wenzelm@67147
   994
  Outer_Syntax.command \<^command_keyword>\<open>subgoal\<close>
wenzelm@62849
   995
    "focus on first subgoal within backward refinement"
wenzelm@67147
   996
    (opt_fact_binding -- (Scan.option (\<^keyword>\<open>premises\<close> |-- Parse.!!! opt_fact_binding)) --
wenzelm@62849
   997
      for_params >> (fn ((a, b), c) =>
wenzelm@62849
   998
        Toplevel.proofs (Seq.make_results o Seq.single o #2 o Subgoal.subgoal_cmd a b c)));
wenzelm@62849
   999
wenzelm@62856
  1000
in end\<close>
wenzelm@62849
  1001
wenzelm@62849
  1002
wenzelm@62856
  1003
subsubsection \<open>Calculation\<close>
wenzelm@62856
  1004
wenzelm@62856
  1005
ML \<open>
wenzelm@62856
  1006
local
wenzelm@62849
  1007
wenzelm@62849
  1008
val calculation_args =
wenzelm@67147
  1009
  Scan.option (\<^keyword>\<open>(\<close> |-- Parse.!!! ((Parse.thms1 --| \<^keyword>\<open>)\<close>)));
wenzelm@62849
  1010
wenzelm@62849
  1011
val _ =
wenzelm@67147
  1012
  Outer_Syntax.command \<^command_keyword>\<open>also\<close> "combine calculation and current facts"
wenzelm@62849
  1013
    (calculation_args >> (Toplevel.proofs' o Calculation.also_cmd));
wenzelm@62849
  1014
wenzelm@62849
  1015
val _ =
wenzelm@67147
  1016
  Outer_Syntax.command \<^command_keyword>\<open>finally\<close>
wenzelm@62849
  1017
    "combine calculation and current facts, exhibit result"
wenzelm@62849
  1018
    (calculation_args >> (Toplevel.proofs' o Calculation.finally_cmd));
wenzelm@62849
  1019
wenzelm@62849
  1020
val _ =
wenzelm@67147
  1021
  Outer_Syntax.command \<^command_keyword>\<open>moreover\<close> "augment calculation by current facts"
wenzelm@62849
  1022
    (Scan.succeed (Toplevel.proof' Calculation.moreover));
wenzelm@62849
  1023
wenzelm@62849
  1024
val _ =
wenzelm@67147
  1025
  Outer_Syntax.command \<^command_keyword>\<open>ultimately\<close>
wenzelm@62849
  1026
    "augment calculation by current facts, exhibit result"
wenzelm@62849
  1027
    (Scan.succeed (Toplevel.proof' Calculation.ultimately));
wenzelm@62849
  1028
wenzelm@62849
  1029
val _ =
wenzelm@67147
  1030
  Outer_Syntax.command \<^command_keyword>\<open>print_trans_rules\<close> "print transitivity rules"
wenzelm@62849
  1031
    (Scan.succeed (Toplevel.keep (Calculation.print_rules o Toplevel.context_of)));
wenzelm@62849
  1032
wenzelm@62856
  1033
in end\<close>
wenzelm@62849
  1034
wenzelm@62856
  1035
wenzelm@62856
  1036
subsubsection \<open>Proof navigation\<close>
wenzelm@62856
  1037
wenzelm@62856
  1038
ML \<open>
wenzelm@62856
  1039
local
wenzelm@62849
  1040
wenzelm@62849
  1041
fun report_back () =
wenzelm@64677
  1042
  Output.report [Markup.markup (Markup.bad ()) "Explicit backtracking"];
wenzelm@62849
  1043
wenzelm@62849
  1044
val _ =
wenzelm@67147
  1045
  Outer_Syntax.command \<^command_keyword>\<open>back\<close> "explicit backtracking of proof command"
wenzelm@62849
  1046
    (Scan.succeed
wenzelm@62849
  1047
     (Toplevel.actual_proof (fn prf => (report_back (); Proof_Node.back prf)) o
wenzelm@62849
  1048
      Toplevel.skip_proof report_back));
wenzelm@62849
  1049
wenzelm@62856
  1050
in end\<close>
wenzelm@62849
  1051
wenzelm@62849
  1052
wenzelm@62856
  1053
subsection \<open>Diagnostic commands (for interactive mode only)\<close>
wenzelm@62856
  1054
wenzelm@62856
  1055
ML \<open>
wenzelm@62856
  1056
local
wenzelm@62849
  1057
wenzelm@62849
  1058
val opt_modes =
wenzelm@67147
  1059
  Scan.optional (\<^keyword>\<open>(\<close> |-- Parse.!!! (Scan.repeat1 Parse.name --| \<^keyword>\<open>)\<close>)) [];
wenzelm@62849
  1060
wenzelm@62849
  1061
val _ =
wenzelm@67147
  1062
  Outer_Syntax.command \<^command_keyword>\<open>help\<close>
wenzelm@62849
  1063
    "retrieve outer syntax commands according to name patterns"
wenzelm@62849
  1064
    (Scan.repeat Parse.name >>
wenzelm@62849
  1065
      (fn pats => Toplevel.keep (fn st => Outer_Syntax.help (Toplevel.theory_of st) pats)));
wenzelm@62849
  1066
wenzelm@62849
  1067
val _ =
wenzelm@67147
  1068
  Outer_Syntax.command \<^command_keyword>\<open>print_commands\<close> "print outer syntax commands"
wenzelm@62849
  1069
    (Scan.succeed (Toplevel.keep (Outer_Syntax.print_commands o Toplevel.theory_of)));
wenzelm@62849
  1070
wenzelm@62849
  1071
val _ =
wenzelm@67147
  1072
  Outer_Syntax.command \<^command_keyword>\<open>print_options\<close> "print configuration options"
wenzelm@62849
  1073
    (Parse.opt_bang >> (fn b => Toplevel.keep (Attrib.print_options b o Toplevel.context_of)));
wenzelm@62849
  1074
wenzelm@62849
  1075
val _ =
wenzelm@67147
  1076
  Outer_Syntax.command \<^command_keyword>\<open>print_context\<close>
wenzelm@62849
  1077
    "print context of local theory target"
wenzelm@62849
  1078
    (Scan.succeed (Toplevel.keep (Pretty.writeln_chunks o Toplevel.pretty_context)));
wenzelm@62849
  1079
wenzelm@62849
  1080
val _ =
wenzelm@67147
  1081
  Outer_Syntax.command \<^command_keyword>\<open>print_theory\<close>
wenzelm@62849
  1082
    "print logical theory contents"
wenzelm@62849
  1083
    (Parse.opt_bang >> (fn b =>
wenzelm@62849
  1084
      Toplevel.keep (Pretty.writeln o Proof_Display.pretty_theory b o Toplevel.context_of)));
wenzelm@62849
  1085
wenzelm@62849
  1086
val _ =
wenzelm@67147
  1087
  Outer_Syntax.command \<^command_keyword>\<open>print_definitions\<close>
wenzelm@62849
  1088
    "print dependencies of definitional theory content"
wenzelm@62849
  1089
    (Parse.opt_bang >> (fn b =>
wenzelm@62849
  1090
      Toplevel.keep (Pretty.writeln o Proof_Display.pretty_definitions b o Toplevel.context_of)));
wenzelm@62849
  1091
wenzelm@62849
  1092
val _ =
wenzelm@67147
  1093
  Outer_Syntax.command \<^command_keyword>\<open>print_syntax\<close>
wenzelm@62849
  1094
    "print inner syntax of context"
wenzelm@62849
  1095
    (Scan.succeed (Toplevel.keep (Proof_Context.print_syntax o Toplevel.context_of)));
wenzelm@62849
  1096
wenzelm@62849
  1097
val _ =
wenzelm@67147
  1098
  Outer_Syntax.command \<^command_keyword>\<open>print_defn_rules\<close>
wenzelm@62849
  1099
    "print definitional rewrite rules of context"
wenzelm@62849
  1100
    (Scan.succeed (Toplevel.keep (Local_Defs.print_rules o Toplevel.context_of)));
wenzelm@62849
  1101
wenzelm@62849
  1102
val _ =
wenzelm@67147
  1103
  Outer_Syntax.command \<^command_keyword>\<open>print_abbrevs\<close>
wenzelm@62849
  1104
    "print constant abbreviations of context"
wenzelm@62849
  1105
    (Parse.opt_bang >> (fn b =>
wenzelm@62849
  1106
      Toplevel.keep (Proof_Context.print_abbrevs b o Toplevel.context_of)));
wenzelm@62849
  1107
wenzelm@62849
  1108
val _ =
wenzelm@67147
  1109
  Outer_Syntax.command \<^command_keyword>\<open>print_theorems\<close>
wenzelm@62849
  1110
    "print theorems of local theory or proof context"
wenzelm@62849
  1111
    (Parse.opt_bang >> (fn b =>
wenzelm@62849
  1112
      Toplevel.keep (Pretty.writeln o Pretty.chunks o Isar_Cmd.pretty_theorems b)));
wenzelm@62849
  1113
wenzelm@62849
  1114
val _ =
wenzelm@67147
  1115
  Outer_Syntax.command \<^command_keyword>\<open>print_locales\<close>
wenzelm@62849
  1116
    "print locales of this theory"
wenzelm@69059
  1117
    (Parse.opt_bang >> (fn verbose =>
wenzelm@69059
  1118
      Toplevel.keep (fn state =>
wenzelm@69059
  1119
        let val thy = Toplevel.theory_of state
wenzelm@69059
  1120
        in Pretty.writeln (Locale.pretty_locales thy verbose) end)));
wenzelm@62849
  1121
wenzelm@62849
  1122
val _ =
wenzelm@67147
  1123
  Outer_Syntax.command \<^command_keyword>\<open>print_classes\<close>
wenzelm@62849
  1124
    "print classes of this theory"
wenzelm@62849
  1125
    (Scan.succeed (Toplevel.keep (Class.print_classes o Toplevel.context_of)));
wenzelm@62849
  1126
wenzelm@62849
  1127
val _ =
wenzelm@67147
  1128
  Outer_Syntax.command \<^command_keyword>\<open>print_locale\<close>
wenzelm@62849
  1129
    "print locale of this theory"
wenzelm@69349
  1130
    (Parse.opt_bang -- Parse.name_position >> (fn (show_facts, raw_name) =>
wenzelm@69057
  1131
      Toplevel.keep (fn state =>
wenzelm@69057
  1132
        let
wenzelm@69057
  1133
          val thy = Toplevel.theory_of state;
wenzelm@69057
  1134
          val name = Locale.check thy raw_name;
wenzelm@69057
  1135
        in Pretty.writeln (Locale.pretty_locale thy show_facts name) end)));
wenzelm@62849
  1136
wenzelm@62849
  1137
val _ =
wenzelm@67147
  1138
  Outer_Syntax.command \<^command_keyword>\<open>print_interps\<close>
wenzelm@62849
  1139
    "print interpretations of locale for this theory or proof context"
wenzelm@69349
  1140
    (Parse.name_position >> (fn raw_name =>
wenzelm@69059
  1141
      Toplevel.keep (fn state =>
wenzelm@69059
  1142
        let
wenzelm@69059
  1143
          val ctxt = Toplevel.context_of state;
wenzelm@69059
  1144
          val thy = Toplevel.theory_of state;
wenzelm@69059
  1145
          val name = Locale.check thy raw_name;
wenzelm@69059
  1146
        in Pretty.writeln (Locale.pretty_registrations ctxt name) end)));
wenzelm@62849
  1147
wenzelm@62849
  1148
val _ =
wenzelm@67147
  1149
  Outer_Syntax.command \<^command_keyword>\<open>print_dependencies\<close>
wenzelm@62849
  1150
    "print dependencies of locale expression"
wenzelm@69057
  1151
    (Parse.opt_bang -- Parse_Spec.locale_expression >> (fn (clean, expr) =>
wenzelm@69057
  1152
      Toplevel.keep (fn state =>
wenzelm@69057
  1153
        Pretty.writeln (Expression.pretty_dependencies (Toplevel.context_of state) clean expr))));
wenzelm@62849
  1154
wenzelm@62849
  1155
val _ =
wenzelm@67147
  1156
  Outer_Syntax.command \<^command_keyword>\<open>print_attributes\<close>
wenzelm@62849
  1157
    "print attributes of this theory"
wenzelm@62849
  1158
    (Parse.opt_bang >> (fn b => Toplevel.keep (Attrib.print_attributes b o Toplevel.context_of)));
wenzelm@62849
  1159
wenzelm@62849
  1160
val _ =
wenzelm@67147
  1161
  Outer_Syntax.command \<^command_keyword>\<open>print_simpset\<close>
wenzelm@62849
  1162
    "print context of Simplifier"
wenzelm@62849
  1163
    (Parse.opt_bang >> (fn b =>
wenzelm@62849
  1164
      Toplevel.keep (Pretty.writeln o Simplifier.pretty_simpset b o Toplevel.context_of)));
wenzelm@62849
  1165
wenzelm@62849
  1166
val _ =
wenzelm@67147
  1167
  Outer_Syntax.command \<^command_keyword>\<open>print_rules\<close> "print intro/elim rules"
wenzelm@62849
  1168
    (Scan.succeed (Toplevel.keep (Context_Rules.print_rules o Toplevel.context_of)));
wenzelm@62849
  1169
wenzelm@62849
  1170
val _ =
wenzelm@67147
  1171
  Outer_Syntax.command \<^command_keyword>\<open>print_methods\<close> "print methods of this theory"
wenzelm@62849
  1172
    (Parse.opt_bang >> (fn b => Toplevel.keep (Method.print_methods b o Toplevel.context_of)));
wenzelm@62849
  1173
wenzelm@62849
  1174
val _ =
wenzelm@67147
  1175
  Outer_Syntax.command \<^command_keyword>\<open>print_antiquotations\<close>
wenzelm@62849
  1176
    "print document antiquotations"
wenzelm@62849
  1177
    (Parse.opt_bang >> (fn b =>
wenzelm@67386
  1178
      Toplevel.keep (Document_Antiquotation.print_antiquotations b o Toplevel.context_of)));
wenzelm@62849
  1179
wenzelm@62849
  1180
val _ =
wenzelm@67147
  1181
  Outer_Syntax.command \<^command_keyword>\<open>print_ML_antiquotations\<close>
wenzelm@62849
  1182
    "print ML antiquotations"
wenzelm@62849
  1183
    (Parse.opt_bang >> (fn b =>
wenzelm@62849
  1184
      Toplevel.keep (ML_Context.print_antiquotations b o Toplevel.context_of)));
wenzelm@62849
  1185
wenzelm@62849
  1186
val _ =
wenzelm@67147
  1187
  Outer_Syntax.command \<^command_keyword>\<open>locale_deps\<close> "visualize locale dependencies"
wenzelm@62849
  1188
    (Scan.succeed
wenzelm@62849
  1189
      (Toplevel.keep (Toplevel.theory_of #> (fn thy =>
wenzelm@62849
  1190
        Locale.pretty_locale_deps thy
wenzelm@62849
  1191
        |> map (fn {name, parents, body} =>
wenzelm@62849
  1192
          ((name, Graph_Display.content_node (Locale.extern thy name) [body]), parents))
wenzelm@62849
  1193
        |> Graph_Display.display_graph_old))));
wenzelm@62849
  1194
wenzelm@62849
  1195
val _ =
wenzelm@67147
  1196
  Outer_Syntax.command \<^command_keyword>\<open>print_term_bindings\<close>
wenzelm@62849
  1197
    "print term bindings of proof context"
wenzelm@62849
  1198
    (Scan.succeed
wenzelm@62849
  1199
      (Toplevel.keep
wenzelm@62849
  1200
        (Pretty.writeln_chunks o Proof_Context.pretty_term_bindings o Toplevel.context_of)));
wenzelm@62849
  1201
wenzelm@62849
  1202
val _ =
wenzelm@67147
  1203
  Outer_Syntax.command \<^command_keyword>\<open>print_facts\<close> "print facts of proof context"
wenzelm@62849
  1204
    (Parse.opt_bang >> (fn b =>
wenzelm@62849
  1205
      Toplevel.keep (Proof_Context.print_local_facts b o Toplevel.context_of)));
wenzelm@62849
  1206
wenzelm@62849
  1207
val _ =
wenzelm@67147
  1208
  Outer_Syntax.command \<^command_keyword>\<open>print_cases\<close> "print cases of proof context"
wenzelm@62849
  1209
    (Scan.succeed
wenzelm@62849
  1210
      (Toplevel.keep (Pretty.writeln_chunks o Proof_Context.pretty_cases o Toplevel.context_of)));
wenzelm@62849
  1211
wenzelm@62849
  1212
val _ =
wenzelm@67147
  1213
  Outer_Syntax.command \<^command_keyword>\<open>print_statement\<close>
wenzelm@62849
  1214
    "print theorems as long statements"
wenzelm@62969
  1215
    (opt_modes -- Parse.thms1 >> Isar_Cmd.print_stmts);
wenzelm@62849
  1216
wenzelm@62849
  1217
val _ =
wenzelm@67147
  1218
  Outer_Syntax.command \<^command_keyword>\<open>thm\<close> "print theorems"
wenzelm@62969
  1219
    (opt_modes -- Parse.thms1 >> Isar_Cmd.print_thms);
wenzelm@62849
  1220
wenzelm@62849
  1221
val _ =
wenzelm@67147
  1222
  Outer_Syntax.command \<^command_keyword>\<open>prf\<close> "print proof terms of theorems"
wenzelm@62969
  1223
    (opt_modes -- Scan.option Parse.thms1 >> Isar_Cmd.print_prfs false);
wenzelm@62849
  1224
wenzelm@62849
  1225
val _ =
wenzelm@67147
  1226
  Outer_Syntax.command \<^command_keyword>\<open>full_prf\<close> "print full proof terms of theorems"
wenzelm@62969
  1227
    (opt_modes -- Scan.option Parse.thms1 >> Isar_Cmd.print_prfs true);
wenzelm@62849
  1228
wenzelm@62849
  1229
val _ =
wenzelm@67147
  1230
  Outer_Syntax.command \<^command_keyword>\<open>prop\<close> "read and print proposition"
wenzelm@62849
  1231
    (opt_modes -- Parse.term >> Isar_Cmd.print_prop);
wenzelm@62849
  1232
wenzelm@62849
  1233
val _ =
wenzelm@67147
  1234
  Outer_Syntax.command \<^command_keyword>\<open>term\<close> "read and print term"
wenzelm@62849
  1235
    (opt_modes -- Parse.term >> Isar_Cmd.print_term);
wenzelm@62849
  1236
wenzelm@62849
  1237
val _ =
wenzelm@67147
  1238
  Outer_Syntax.command \<^command_keyword>\<open>typ\<close> "read and print type"
wenzelm@67147
  1239
    (opt_modes -- (Parse.typ -- Scan.option (\<^keyword>\<open>::\<close> |-- Parse.!!! Parse.sort))
wenzelm@62849
  1240
      >> Isar_Cmd.print_type);
wenzelm@62849
  1241
wenzelm@62849
  1242
val _ =
wenzelm@67147
  1243
  Outer_Syntax.command \<^command_keyword>\<open>print_codesetup\<close> "print code generator setup"
wenzelm@62849
  1244
    (Scan.succeed (Toplevel.keep (Code.print_codesetup o Toplevel.theory_of)));
wenzelm@62849
  1245
wenzelm@62849
  1246
val _ =
wenzelm@67147
  1247
  Outer_Syntax.command \<^command_keyword>\<open>print_state\<close>
wenzelm@62849
  1248
    "print current proof state (if present)"
wenzelm@62849
  1249
    (opt_modes >> (fn modes =>
wenzelm@62849
  1250
      Toplevel.keep (Print_Mode.with_modes modes (Output.state o Toplevel.string_of_state))));
wenzelm@62849
  1251
wenzelm@62849
  1252
val _ =
wenzelm@67147
  1253
  Outer_Syntax.command \<^command_keyword>\<open>welcome\<close> "print welcome message"
wenzelm@62849
  1254
    (Scan.succeed (Toplevel.keep (fn _ => writeln (Session.welcome ()))));
wenzelm@62849
  1255
wenzelm@62856
  1256
in end\<close>
wenzelm@62849
  1257
wenzelm@62849
  1258
wenzelm@62856
  1259
subsection \<open>Dependencies\<close>
wenzelm@62856
  1260
wenzelm@62856
  1261
ML \<open>
wenzelm@62849
  1262
local
wenzelm@62856
  1263
wenzelm@62856
  1264
val theory_bounds =
wenzelm@69349
  1265
  Parse.theory_name >> single ||
wenzelm@69349
  1266
  (\<^keyword>\<open>(\<close> |-- Parse.enum "|" Parse.theory_name --| \<^keyword>\<open>)\<close>);
wenzelm@62849
  1267
wenzelm@62849
  1268
val _ =
wenzelm@67147
  1269
  Outer_Syntax.command \<^command_keyword>\<open>thy_deps\<close> "visualize theory dependencies"
wenzelm@62849
  1270
    (Scan.option theory_bounds -- Scan.option theory_bounds >>
wenzelm@62849
  1271
      (fn args => Toplevel.keep (fn st => Thy_Deps.thy_deps_cmd (Toplevel.context_of st) args)));
wenzelm@62849
  1272
wenzelm@62849
  1273
wenzelm@62856
  1274
val class_bounds =
wenzelm@62856
  1275
  Parse.sort >> single ||
wenzelm@67147
  1276
  (\<^keyword>\<open>(\<close> |-- Parse.enum "|" Parse.sort --| \<^keyword>\<open>)\<close>);
wenzelm@62849
  1277
wenzelm@62849
  1278
val _ =
wenzelm@67147
  1279
  Outer_Syntax.command \<^command_keyword>\<open>class_deps\<close> "visualize class dependencies"
wenzelm@62849
  1280
    (Scan.option class_bounds -- Scan.option class_bounds >> (fn args =>
wenzelm@62849
  1281
      Toplevel.keep (fn st => Class_Deps.class_deps_cmd (Toplevel.context_of st) args)));
wenzelm@62849
  1282
wenzelm@62849
  1283
wenzelm@62849
  1284
val _ =
wenzelm@67147
  1285
  Outer_Syntax.command \<^command_keyword>\<open>thm_deps\<close> "visualize theorem dependencies"
wenzelm@62969
  1286
    (Parse.thms1 >> (fn args =>
wenzelm@62849
  1287
      Toplevel.keep (fn st =>
wenzelm@62849
  1288
        Thm_Deps.thm_deps (Toplevel.theory_of st)
wenzelm@62849
  1289
          (Attrib.eval_thms (Toplevel.context_of st) args))));
wenzelm@62849
  1290
wenzelm@62856
  1291
wenzelm@69349
  1292
val thy_names = Scan.repeat1 (Scan.unless Parse.minus Parse.theory_name);
wenzelm@62849
  1293
wenzelm@62849
  1294
val _ =
wenzelm@67147
  1295
  Outer_Syntax.command \<^command_keyword>\<open>unused_thms\<close> "find unused theorems"
wenzelm@62849
  1296
    (Scan.option ((thy_names --| Parse.minus) -- Scan.option thy_names) >> (fn opt_range =>
wenzelm@62849
  1297
        Toplevel.keep (fn st =>
wenzelm@62849
  1298
          let
wenzelm@62849
  1299
            val thy = Toplevel.theory_of st;
wenzelm@62849
  1300
            val ctxt = Toplevel.context_of st;
wenzelm@62849
  1301
            fun pretty_thm (a, th) = Proof_Context.pretty_fact ctxt (a, [th]);
wenzelm@68482
  1302
            val check = Theory.check {long = false} ctxt;
wenzelm@62849
  1303
          in
wenzelm@62849
  1304
            Thm_Deps.unused_thms
wenzelm@62849
  1305
              (case opt_range of
wenzelm@62849
  1306
                NONE => (Theory.parents_of thy, [thy])
wenzelm@62849
  1307
              | SOME (xs, NONE) => (map check xs, [thy])
wenzelm@62849
  1308
              | SOME (xs, SOME ys) => (map check xs, map check ys))
wenzelm@62849
  1309
            |> map pretty_thm |> Pretty.writeln_chunks
wenzelm@62849
  1310
          end)));
wenzelm@62849
  1311
wenzelm@62856
  1312
in end\<close>
wenzelm@62849
  1313
wenzelm@62849
  1314
wenzelm@62856
  1315
subsubsection \<open>Find consts and theorems\<close>
wenzelm@62856
  1316
wenzelm@62856
  1317
ML \<open>
wenzelm@62856
  1318
local
wenzelm@62849
  1319
wenzelm@62849
  1320
val _ =
wenzelm@67147
  1321
  Outer_Syntax.command \<^command_keyword>\<open>find_consts\<close>
wenzelm@62849
  1322
    "find constants by name / type patterns"
wenzelm@62849
  1323
    (Find_Consts.query_parser >> (fn spec =>
wenzelm@62849
  1324
      Toplevel.keep (fn st =>
wenzelm@62849
  1325
        Pretty.writeln (Find_Consts.pretty_consts (Toplevel.context_of st) spec))));
wenzelm@62849
  1326
wenzelm@62856
  1327
val options =
wenzelm@62856
  1328
  Scan.optional
wenzelm@62856
  1329
    (Parse.$$$ "(" |--
wenzelm@62856
  1330
      Parse.!!! (Scan.option Parse.nat --
wenzelm@62856
  1331
        Scan.optional (Parse.reserved "with_dups" >> K false) true --| Parse.$$$ ")"))
wenzelm@62856
  1332
    (NONE, true);
wenzelm@62849
  1333
wenzelm@62849
  1334
val _ =
wenzelm@67147
  1335
  Outer_Syntax.command \<^command_keyword>\<open>find_theorems\<close>
wenzelm@62849
  1336
    "find theorems meeting specified criteria"
wenzelm@62849
  1337
    (options -- Find_Theorems.query_parser >> (fn ((opt_lim, rem_dups), spec) =>
wenzelm@62849
  1338
      Toplevel.keep (fn st =>
wenzelm@62849
  1339
        Pretty.writeln
wenzelm@62849
  1340
          (Find_Theorems.pretty_theorems (Find_Theorems.proof_state st) opt_lim rem_dups spec))));
wenzelm@62849
  1341
wenzelm@62856
  1342
in end\<close>
wenzelm@62849
  1343
wenzelm@62849
  1344
wenzelm@62856
  1345
subsection \<open>Code generation\<close>
wenzelm@62849
  1346
wenzelm@62856
  1347
ML \<open>
wenzelm@62856
  1348
local
wenzelm@62856
  1349
wenzelm@62856
  1350
val _ =
wenzelm@67147
  1351
  Outer_Syntax.command \<^command_keyword>\<open>code_datatype\<close>
wenzelm@62856
  1352
    "define set of code datatype constructors"
haftmann@66251
  1353
    (Scan.repeat1 Parse.term >> (Toplevel.theory o Code.declare_datatype_cmd));
wenzelm@62856
  1354
wenzelm@62856
  1355
in end\<close>
wenzelm@62856
  1356
wenzelm@62856
  1357
wenzelm@62856
  1358
subsection \<open>Extraction of programs from proofs\<close>
wenzelm@62856
  1359
wenzelm@62856
  1360
ML \<open>
wenzelm@62856
  1361
local
wenzelm@62849
  1362
wenzelm@62849
  1363
val parse_vars = Scan.optional (Parse.$$$ "(" |-- Parse.list1 Parse.name --| Parse.$$$ ")") [];
wenzelm@62849
  1364
wenzelm@62849
  1365
val _ =
wenzelm@67147
  1366
  Outer_Syntax.command \<^command_keyword>\<open>realizers\<close>
wenzelm@62849
  1367
    "specify realizers for primitive axioms / theorems, together with correctness proof"
wenzelm@62969
  1368
    (Scan.repeat1 (Parse.name -- parse_vars --| Parse.$$$ ":" -- Parse.string -- Parse.string) >>
wenzelm@62849
  1369
     (fn xs => Toplevel.theory (fn thy => Extraction.add_realizers
wenzelm@62849
  1370
       (map (fn (((a, vs), s1), s2) => (Global_Theory.get_thm thy a, (vs, s1, s2))) xs) thy)));
wenzelm@62849
  1371
wenzelm@62849
  1372
val _ =
wenzelm@67147
  1373
  Outer_Syntax.command \<^command_keyword>\<open>realizability\<close>
wenzelm@62849
  1374
    "add equations characterizing realizability"
wenzelm@62849
  1375
    (Scan.repeat1 Parse.string >> (Toplevel.theory o Extraction.add_realizes_eqns));
wenzelm@62849
  1376
wenzelm@62849
  1377
val _ =
wenzelm@67147
  1378
  Outer_Syntax.command \<^command_keyword>\<open>extract_type\<close>
wenzelm@62849
  1379
    "add equations characterizing type of extracted program"
wenzelm@62849
  1380
    (Scan.repeat1 Parse.string >> (Toplevel.theory o Extraction.add_typeof_eqns));
wenzelm@62849
  1381
wenzelm@62849
  1382
val _ =
wenzelm@67147
  1383
  Outer_Syntax.command \<^command_keyword>\<open>extract\<close> "extract terms from proofs"
wenzelm@62969
  1384
    (Scan.repeat1 (Parse.name -- parse_vars) >> (fn xs => Toplevel.theory (fn thy =>
wenzelm@62849
  1385
      Extraction.extract (map (apfst (Global_Theory.get_thm thy)) xs) thy)));
wenzelm@62849
  1386
wenzelm@62856
  1387
in end\<close>
wenzelm@62849
  1388
wenzelm@62849
  1389
wenzelm@62944
  1390
section \<open>Auxiliary lemmas\<close>
wenzelm@20627
  1391
wenzelm@58611
  1392
subsection \<open>Meta-level connectives in assumptions\<close>
wenzelm@15803
  1393
wenzelm@15803
  1394
lemma meta_mp:
wenzelm@58612
  1395
  assumes "PROP P \<Longrightarrow> PROP Q" and "PROP P"
wenzelm@15803
  1396
  shows "PROP Q"
wenzelm@58612
  1397
    by (rule \<open>PROP P \<Longrightarrow> PROP Q\<close> [OF \<open>PROP P\<close>])
wenzelm@15803
  1398
nipkow@23432
  1399
lemmas meta_impE = meta_mp [elim_format]
nipkow@23432
  1400
wenzelm@15803
  1401
lemma meta_spec:
wenzelm@58612
  1402
  assumes "\<And>x. PROP P x"
wenzelm@26958
  1403
  shows "PROP P x"
wenzelm@58612
  1404
    by (rule \<open>\<And>x. PROP P x\<close>)
wenzelm@15803
  1405
wenzelm@15803
  1406
lemmas meta_allE = meta_spec [elim_format]
wenzelm@15803
  1407
wenzelm@26570
  1408
lemma swap_params:
wenzelm@58612
  1409
  "(\<And>x y. PROP P x y) \<equiv> (\<And>y x. PROP P x y)" ..
wenzelm@26570
  1410
wenzelm@18466
  1411
wenzelm@58611
  1412
subsection \<open>Meta-level conjunction\<close>
wenzelm@18466
  1413
wenzelm@18466
  1414
lemma all_conjunction:
wenzelm@58612
  1415
  "(\<And>x. PROP A x &&& PROP B x) \<equiv> ((\<And>x. PROP A x) &&& (\<And>x. PROP B x))"
wenzelm@18466
  1416
proof
wenzelm@58612
  1417
  assume conj: "\<And>x. PROP A x &&& PROP B x"
wenzelm@58612
  1418
  show "(\<And>x. PROP A x) &&& (\<And>x. PROP B x)"
wenzelm@19121
  1419
  proof -
wenzelm@18466
  1420
    fix x
wenzelm@26958
  1421
    from conj show "PROP A x" by (rule conjunctionD1)
wenzelm@26958
  1422
    from conj show "PROP B x" by (rule conjunctionD2)
wenzelm@18466
  1423
  qed
wenzelm@18466
  1424
next
wenzelm@58612
  1425
  assume conj: "(\<And>x. PROP A x) &&& (\<And>x. PROP B x)"
wenzelm@18466
  1426
  fix x
wenzelm@28856
  1427
  show "PROP A x &&& PROP B x"
wenzelm@19121
  1428
  proof -
wenzelm@26958
  1429
    show "PROP A x" by (rule conj [THEN conjunctionD1, rule_format])
wenzelm@26958
  1430
    show "PROP B x" by (rule conj [THEN conjunctionD2, rule_format])
wenzelm@18466
  1431
  qed
wenzelm@18466
  1432
qed
wenzelm@18466
  1433
wenzelm@19121
  1434
lemma imp_conjunction:
wenzelm@58612
  1435
  "(PROP A \<Longrightarrow> PROP B &&& PROP C) \<equiv> ((PROP A \<Longrightarrow> PROP B) &&& (PROP A \<Longrightarrow> PROP C))"
wenzelm@18836
  1436
proof
wenzelm@58612
  1437
  assume conj: "PROP A \<Longrightarrow> PROP B &&& PROP C"
wenzelm@58612
  1438
  show "(PROP A \<Longrightarrow> PROP B) &&& (PROP A \<Longrightarrow> PROP C)"
wenzelm@19121
  1439
  proof -
wenzelm@18466
  1440
    assume "PROP A"
wenzelm@58611
  1441
    from conj [OF \<open>PROP A\<close>] show "PROP B" by (rule conjunctionD1)
wenzelm@58611
  1442
    from conj [OF \<open>PROP A\<close>] show "PROP C" by (rule conjunctionD2)
wenzelm@18466
  1443
  qed
wenzelm@18466
  1444
next
wenzelm@58612
  1445
  assume conj: "(PROP A \<Longrightarrow> PROP B) &&& (PROP A \<Longrightarrow> PROP C)"
wenzelm@18466
  1446
  assume "PROP A"
wenzelm@28856
  1447
  show "PROP B &&& PROP C"
wenzelm@19121
  1448
  proof -
wenzelm@58611
  1449
    from \<open>PROP A\<close> show "PROP B" by (rule conj [THEN conjunctionD1])
wenzelm@58611
  1450
    from \<open>PROP A\<close> show "PROP C" by (rule conj [THEN conjunctionD2])
wenzelm@18466
  1451
  qed
wenzelm@18466
  1452
qed
wenzelm@18466
  1453
wenzelm@18466
  1454
lemma conjunction_imp:
wenzelm@58612
  1455
  "(PROP A &&& PROP B \<Longrightarrow> PROP C) \<equiv> (PROP A \<Longrightarrow> PROP B \<Longrightarrow> PROP C)"
wenzelm@18466
  1456
proof
wenzelm@58612
  1457
  assume r: "PROP A &&& PROP B \<Longrightarrow> PROP C"
wenzelm@22933
  1458
  assume ab: "PROP A" "PROP B"
wenzelm@22933
  1459
  show "PROP C"
wenzelm@22933
  1460
  proof (rule r)
wenzelm@28856
  1461
    from ab show "PROP A &&& PROP B" .
wenzelm@22933
  1462
  qed
wenzelm@18466
  1463
next
wenzelm@58612
  1464
  assume r: "PROP A \<Longrightarrow> PROP B \<Longrightarrow> PROP C"
wenzelm@28856
  1465
  assume conj: "PROP A &&& PROP B"
wenzelm@18466
  1466
  show "PROP C"
wenzelm@18466
  1467
  proof (rule r)
wenzelm@19121
  1468
    from conj show "PROP A" by (rule conjunctionD1)
wenzelm@19121
  1469
    from conj show "PROP B" by (rule conjunctionD2)
wenzelm@18466
  1470
  qed
wenzelm@18466
  1471
qed
wenzelm@18466
  1472
wenzelm@68816
  1473
declare [[ML_write_global = false]]
wenzelm@68816
  1474
wenzelm@48638
  1475
end