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