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