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