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