src/Pure/Isar/method.ML
author wenzelm
Tue Oct 16 17:47:23 2012 +0200 (2012-10-16)
changeset 49865 eeaf1ec7eac2
parent 49846 8fae089f5a0c
child 49866 619acbd72664
permissions -rw-r--r--
clarified defer/prefer: more specific errors;
wenzelm@5824
     1
(*  Title:      Pure/Isar/method.ML
wenzelm@5824
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5824
     3
wenzelm@17110
     4
Isar proof methods.
wenzelm@5824
     5
*)
wenzelm@5824
     6
wenzelm@5824
     7
signature METHOD =
wenzelm@5824
     8
sig
wenzelm@30508
     9
  type method
wenzelm@32193
    10
  val apply: (Proof.context -> method) -> Proof.context -> thm list -> cases_tactic
wenzelm@18227
    11
  val RAW_METHOD_CASES: (thm list -> cases_tactic) -> method
wenzelm@17110
    12
  val RAW_METHOD: (thm list -> tactic) -> method
wenzelm@18227
    13
  val METHOD_CASES: (thm list -> cases_tactic) -> method
wenzelm@17110
    14
  val METHOD: (thm list -> tactic) -> method
wenzelm@17110
    15
  val fail: method
wenzelm@17110
    16
  val succeed: method
wenzelm@17110
    17
  val insert_tac: thm list -> int -> tactic
wenzelm@17110
    18
  val insert: thm list -> method
wenzelm@17110
    19
  val insert_facts: method
wenzelm@17110
    20
  val SIMPLE_METHOD: tactic -> method
wenzelm@21592
    21
  val SIMPLE_METHOD': (int -> tactic) -> method
wenzelm@21592
    22
  val SIMPLE_METHOD'': ((int -> tactic) -> tactic) -> (int -> tactic) -> method
wenzelm@20289
    23
  val cheating: bool -> Proof.context -> method
wenzelm@17110
    24
  val intro: thm list -> method
wenzelm@17110
    25
  val elim: thm list -> method
wenzelm@20289
    26
  val unfold: thm list -> Proof.context -> method
wenzelm@20289
    27
  val fold: thm list -> Proof.context -> method
wenzelm@17110
    28
  val atomize: bool -> method
wenzelm@17110
    29
  val this: method
wenzelm@20289
    30
  val fact: thm list -> Proof.context -> method
wenzelm@30234
    31
  val assm_tac: Proof.context -> int -> tactic
wenzelm@30567
    32
  val all_assm_tac: Proof.context -> tactic
wenzelm@20289
    33
  val assumption: Proof.context -> method
wenzelm@46466
    34
  val rule_trace: bool Config.T
wenzelm@20289
    35
  val trace: Proof.context -> thm list -> unit
wenzelm@6091
    36
  val rule_tac: thm list -> thm list -> int -> tactic
wenzelm@20289
    37
  val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic
haftmann@25270
    38
  val intros_tac: thm list -> thm list -> tactic
ballarin@36093
    39
  val try_intros_tac: thm list -> thm list -> tactic
wenzelm@17110
    40
  val rule: thm list -> method
wenzelm@17110
    41
  val erule: int -> thm list -> method
wenzelm@17110
    42
  val drule: int -> thm list -> method
wenzelm@17110
    43
  val frule: int -> thm list -> method
wenzelm@27235
    44
  val set_tactic: (thm list -> tactic) -> Proof.context -> Proof.context
wenzelm@26385
    45
  val tactic: string * Position.T -> Proof.context -> method
wenzelm@27235
    46
  val raw_tactic: string * Position.T -> Proof.context -> method
wenzelm@27729
    47
  type src = Args.src
wenzelm@5824
    48
  datatype text =
wenzelm@32193
    49
    Basic of Proof.context -> method |
wenzelm@15703
    50
    Source of src |
wenzelm@20030
    51
    Source_i of src |
wenzelm@5824
    52
    Then of text list |
wenzelm@5824
    53
    Orelse of text list |
wenzelm@5824
    54
    Try of text |
wenzelm@19186
    55
    Repeat1 of text |
wenzelm@19186
    56
    SelectGoals of int * text
wenzelm@17857
    57
  val primitive_text: (thm -> thm) -> text
wenzelm@17857
    58
  val succeed_text: text
wenzelm@17110
    59
  val default_text: text
wenzelm@17110
    60
  val this_text: text
wenzelm@17110
    61
  val done_text: text
wenzelm@17356
    62
  val sorry_text: bool -> text
wenzelm@32193
    63
  val finish_text: text option * bool -> text
wenzelm@27729
    64
  val print_methods: theory -> unit
wenzelm@26892
    65
  val intern: theory -> xstring -> string
wenzelm@26892
    66
  val defined: theory -> string -> bool
wenzelm@20289
    67
  val method: theory -> src -> Proof.context -> method
wenzelm@20289
    68
  val method_i: theory -> src -> Proof.context -> method
wenzelm@30512
    69
  val syntax: 'a context_parser -> src -> Proof.context -> 'a * Proof.context
wenzelm@30512
    70
  val setup: binding -> (Proof.context -> method) context_parser -> string -> theory -> theory
wenzelm@30575
    71
  val method_setup: bstring * Position.T -> Symbol_Pos.text * Position.T -> string ->
wenzelm@30575
    72
    theory -> theory
wenzelm@30540
    73
  type modifier = (Proof.context -> Proof.context) * attribute
wenzelm@30540
    74
  val section: modifier parser list -> thm list context_parser
wenzelm@30540
    75
  val sections: modifier parser list -> thm list list context_parser
wenzelm@30512
    76
  val parse: text parser
wenzelm@5824
    77
end;
wenzelm@5824
    78
wenzelm@5824
    79
structure Method: METHOD =
wenzelm@5824
    80
struct
wenzelm@5824
    81
wenzelm@12324
    82
(** proof methods **)
wenzelm@12324
    83
wenzelm@17110
    84
(* datatype method *)
wenzelm@11731
    85
wenzelm@18227
    86
datatype method = Meth of thm list -> cases_tactic;
wenzelm@11731
    87
wenzelm@32193
    88
fun apply meth ctxt = let val Meth m = meth ctxt in m end;
wenzelm@11731
    89
wenzelm@17756
    90
val RAW_METHOD_CASES = Meth;
wenzelm@11731
    91
wenzelm@17110
    92
fun RAW_METHOD tac = RAW_METHOD_CASES (NO_CASES o tac);
wenzelm@12144
    93
wenzelm@17110
    94
fun METHOD_CASES tac = RAW_METHOD_CASES (fn facts =>
wenzelm@21687
    95
  Seq.THEN (ALLGOALS Goal.conjunction_tac, tac facts));
wenzelm@8372
    96
wenzelm@21687
    97
fun METHOD tac = RAW_METHOD (fn facts => ALLGOALS Goal.conjunction_tac THEN tac facts);
wenzelm@5824
    98
wenzelm@5824
    99
val fail = METHOD (K no_tac);
wenzelm@5824
   100
val succeed = METHOD (K all_tac);
wenzelm@5824
   101
wenzelm@5824
   102
wenzelm@17110
   103
(* insert facts *)
wenzelm@7419
   104
wenzelm@7419
   105
local
wenzelm@5824
   106
wenzelm@21579
   107
fun cut_rule_tac rule =
wenzelm@21579
   108
  Tactic.rtac (Drule.forall_intr_vars rule COMP_INCR revcut_rl);
wenzelm@6981
   109
wenzelm@7419
   110
in
wenzelm@5824
   111
wenzelm@7419
   112
fun insert_tac [] i = all_tac
wenzelm@7419
   113
  | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
wenzelm@6981
   114
wenzelm@7555
   115
val insert_facts = METHOD (ALLGOALS o insert_tac);
wenzelm@7664
   116
fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
wenzelm@7419
   117
wenzelm@9706
   118
fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac);
wenzelm@21592
   119
fun SIMPLE_METHOD'' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac));
wenzelm@21592
   120
val SIMPLE_METHOD' = SIMPLE_METHOD'' HEADGOAL;
wenzelm@9706
   121
wenzelm@12324
   122
end;
wenzelm@12324
   123
wenzelm@9706
   124
wenzelm@17356
   125
(* cheating *)
wenzelm@17356
   126
wenzelm@32969
   127
fun cheating int ctxt =
wenzelm@32969
   128
  if int orelse ! quick_and_dirty then
wenzelm@42360
   129
    METHOD (K (Skip_Proof.cheat_tac (Proof_Context.theory_of ctxt)))
wenzelm@32969
   130
  else error "Cheating requires quick_and_dirty mode!";
wenzelm@17356
   131
wenzelm@17356
   132
wenzelm@17110
   133
(* unfold intro/elim rules *)
wenzelm@17110
   134
wenzelm@21592
   135
fun intro ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths));
wenzelm@21592
   136
fun elim ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths));
wenzelm@17110
   137
wenzelm@17110
   138
wenzelm@12384
   139
(* unfold/fold definitions *)
wenzelm@12384
   140
wenzelm@35624
   141
fun unfold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (Local_Defs.unfold_tac ctxt ths));
wenzelm@35624
   142
fun fold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (Local_Defs.fold_tac ctxt ths));
wenzelm@6532
   143
wenzelm@12384
   144
wenzelm@12829
   145
(* atomize rule statements *)
wenzelm@12829
   146
wenzelm@35625
   147
fun atomize false = SIMPLE_METHOD' (CHANGED_PROP o Object_Logic.atomize_prems_tac)
wenzelm@35625
   148
  | atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o Object_Logic.full_atomize_tac)));
wenzelm@12829
   149
wenzelm@12829
   150
wenzelm@18039
   151
(* this -- resolve facts directly *)
wenzelm@12384
   152
wenzelm@17110
   153
val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac));
wenzelm@9484
   154
wenzelm@9484
   155
wenzelm@18039
   156
(* fact -- composition by facts from context *)
wenzelm@18039
   157
wenzelm@42360
   158
fun fact [] ctxt = SIMPLE_METHOD' (Proof_Context.some_fact_tac ctxt)
wenzelm@42360
   159
  | fact rules _ = SIMPLE_METHOD' (Proof_Context.fact_tac rules);
wenzelm@18039
   160
wenzelm@18039
   161
wenzelm@17110
   162
(* assumption *)
wenzelm@7419
   163
wenzelm@7419
   164
local
wenzelm@7419
   165
wenzelm@19778
   166
fun cond_rtac cond rule = SUBGOAL (fn (prop, i) =>
wenzelm@19778
   167
  if cond (Logic.strip_assums_concl prop)
wenzelm@19778
   168
  then Tactic.rtac rule i else no_tac);
wenzelm@7419
   169
kleing@29857
   170
in
kleing@29857
   171
wenzelm@30234
   172
fun assm_tac ctxt =
wenzelm@17110
   173
  assume_tac APPEND'
wenzelm@23349
   174
  Goal.assume_rule_tac ctxt APPEND'
wenzelm@19778
   175
  cond_rtac (can Logic.dest_equals) Drule.reflexive_thm APPEND'
wenzelm@19778
   176
  cond_rtac (can Logic.dest_term) Drule.termI;
wenzelm@17110
   177
wenzelm@49846
   178
fun all_assm_tac ctxt =
wenzelm@49846
   179
  let
wenzelm@49846
   180
    fun tac i st =
wenzelm@49846
   181
      if i > Thm.nprems_of st then all_tac st
wenzelm@49846
   182
      else ((assm_tac ctxt i THEN tac i) ORELSE tac (i + 1)) st;
wenzelm@49846
   183
  in tac 1 end;
wenzelm@30567
   184
wenzelm@23349
   185
fun assumption ctxt = METHOD (HEADGOAL o
wenzelm@30234
   186
  (fn [] => assm_tac ctxt
wenzelm@23349
   187
    | [fact] => solve_tac [fact]
wenzelm@23349
   188
    | _ => K no_tac));
wenzelm@23349
   189
wenzelm@49846
   190
fun finish immed ctxt =
wenzelm@49846
   191
  METHOD (K ((if immed then all_assm_tac ctxt else all_tac) THEN flexflex_tac));
wenzelm@7419
   192
wenzelm@7419
   193
end;
wenzelm@7419
   194
wenzelm@7419
   195
wenzelm@17110
   196
(* rule etc. -- single-step refinements *)
wenzelm@12347
   197
wenzelm@42616
   198
val rule_trace = Attrib.setup_config_bool (Binding.name "rule_trace") (fn _ => false);
wenzelm@12347
   199
wenzelm@17110
   200
fun trace ctxt rules =
wenzelm@41379
   201
  if Config.get ctxt rule_trace andalso not (null rules) then
wenzelm@32091
   202
    Pretty.big_list "rules:" (map (Display.pretty_thm ctxt) rules)
wenzelm@21962
   203
    |> Pretty.string_of |> tracing
wenzelm@21962
   204
  else ();
wenzelm@12347
   205
wenzelm@12347
   206
local
wenzelm@12347
   207
wenzelm@18841
   208
fun gen_rule_tac tac rules facts =
wenzelm@18841
   209
  (fn i => fn st =>
wenzelm@18841
   210
    if null facts then tac rules i st
wenzelm@18841
   211
    else Seq.maps (fn rule => (tac o single) rule i st) (Drule.multi_resolves facts rules))
wenzelm@21687
   212
  THEN_ALL_NEW Goal.norm_hhf_tac;
wenzelm@7130
   213
wenzelm@10744
   214
fun gen_arule_tac tac j rules facts =
wenzelm@10744
   215
  EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac);
wenzelm@10744
   216
wenzelm@11785
   217
fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) =>
wenzelm@11785
   218
  let
wenzelm@11785
   219
    val rules =
wenzelm@11785
   220
      if not (null arg_rules) then arg_rules
wenzelm@33369
   221
      else flat (Context_Rules.find_rules false facts goal ctxt)
wenzelm@12055
   222
  in trace ctxt rules; tac rules facts i end);
wenzelm@10309
   223
wenzelm@10744
   224
fun meth tac x = METHOD (HEADGOAL o tac x);
wenzelm@10744
   225
fun meth' tac x y = METHOD (HEADGOAL o tac x y);
wenzelm@8220
   226
wenzelm@7419
   227
in
wenzelm@7419
   228
wenzelm@10744
   229
val rule_tac = gen_rule_tac Tactic.resolve_tac;
wenzelm@10744
   230
val rule = meth rule_tac;
wenzelm@10744
   231
val some_rule_tac = gen_some_rule_tac rule_tac;
wenzelm@10744
   232
val some_rule = meth' some_rule_tac;
wenzelm@10744
   233
wenzelm@10744
   234
val erule = meth' (gen_arule_tac Tactic.eresolve_tac);
wenzelm@10744
   235
val drule = meth' (gen_arule_tac Tactic.dresolve_tac);
wenzelm@10744
   236
val frule = meth' (gen_arule_tac Tactic.forward_tac);
wenzelm@5824
   237
wenzelm@7419
   238
end;
wenzelm@7419
   239
wenzelm@7419
   240
haftmann@25270
   241
(* intros_tac -- pervasive search spanned by intro rules *)
haftmann@25270
   242
ballarin@36093
   243
fun gen_intros_tac goals intros facts =
ballarin@36093
   244
  goals (insert_tac facts THEN'
haftmann@25270
   245
      REPEAT_ALL_NEW (resolve_tac intros))
haftmann@25270
   246
    THEN Tactic.distinct_subgoals_tac;
haftmann@25270
   247
ballarin@36093
   248
val intros_tac = gen_intros_tac ALLGOALS;
ballarin@36093
   249
val try_intros_tac = gen_intros_tac TRYALL;
haftmann@25270
   250
wenzelm@37216
   251
wenzelm@8351
   252
(* ML tactics *)
wenzelm@8351
   253
wenzelm@37216
   254
structure ML_Tactic = Proof_Data
wenzelm@26472
   255
(
wenzelm@27235
   256
  type T = thm list -> tactic;
wenzelm@26472
   257
  fun init _ = undefined;
wenzelm@26472
   258
);
wenzelm@26472
   259
wenzelm@37216
   260
val set_tactic = ML_Tactic.put;
wenzelm@8351
   261
wenzelm@26472
   262
fun ml_tactic (txt, pos) ctxt =
wenzelm@26472
   263
  let
wenzelm@26472
   264
    val ctxt' = ctxt |> Context.proof_map
wenzelm@27235
   265
      (ML_Context.expression pos
wenzelm@27235
   266
        "fun tactic (facts: thm list) : tactic"
wenzelm@37198
   267
        "Context.map_proof (Method.set_tactic tactic)" (ML_Lex.read pos txt));
wenzelm@37216
   268
  in Context.setmp_thread_data (SOME (Context.Proof ctxt)) (ML_Tactic.get ctxt') end;
wenzelm@23425
   269
wenzelm@23425
   270
fun tactic txt ctxt = METHOD (ml_tactic txt ctxt);
wenzelm@23425
   271
fun raw_tactic txt ctxt = RAW_METHOD (ml_tactic txt ctxt);
wenzelm@8351
   272
wenzelm@8351
   273
wenzelm@5824
   274
wenzelm@17110
   275
(** method syntax **)
wenzelm@17110
   276
wenzelm@17110
   277
(* method text *)
wenzelm@17110
   278
wenzelm@17110
   279
type src = Args.src;
wenzelm@5824
   280
wenzelm@17110
   281
datatype text =
wenzelm@32193
   282
  Basic of Proof.context -> method |
wenzelm@17110
   283
  Source of src |
wenzelm@20030
   284
  Source_i of src |
wenzelm@17110
   285
  Then of text list |
wenzelm@17110
   286
  Orelse of text list |
wenzelm@17110
   287
  Try of text |
wenzelm@19186
   288
  Repeat1 of text |
wenzelm@19186
   289
  SelectGoals of int * text;
wenzelm@17110
   290
wenzelm@32193
   291
fun primitive_text r = Basic (K (SIMPLE_METHOD (PRIMITIVE r)));
wenzelm@32193
   292
val succeed_text = Basic (K succeed);
wenzelm@17110
   293
val default_text = Source (Args.src (("default", []), Position.none));
wenzelm@32193
   294
val this_text = Basic (K this);
wenzelm@32193
   295
val done_text = Basic (K (SIMPLE_METHOD all_tac));
wenzelm@32193
   296
fun sorry_text int = Basic (cheating int);
wenzelm@17110
   297
wenzelm@49846
   298
fun finish_text (NONE, immed) = Basic (finish immed)
wenzelm@49846
   299
  | finish_text (SOME txt, immed) = Then [txt, Basic (finish immed)];
wenzelm@17110
   300
wenzelm@17110
   301
wenzelm@17110
   302
(* method definitions *)
wenzelm@5824
   303
wenzelm@33522
   304
structure Methods = Theory_Data
wenzelm@22846
   305
(
wenzelm@33095
   306
  type T = ((src -> Proof.context -> method) * string) Name_Space.table;
wenzelm@33159
   307
  val empty : T = Name_Space.empty_table "method";
wenzelm@16448
   308
  val extend = I;
wenzelm@33522
   309
  fun merge data : T = Name_Space.merge_tables data;
wenzelm@22846
   310
);
wenzelm@5824
   311
wenzelm@22846
   312
fun print_methods thy =
wenzelm@22846
   313
  let
wenzelm@42360
   314
    val ctxt = Proof_Context.init_global thy;
wenzelm@24116
   315
    val meths = Methods.get thy;
wenzelm@42813
   316
    fun prt_meth (name, (_, "")) = Pretty.str name
wenzelm@42813
   317
      | prt_meth (name, (_, comment)) =
wenzelm@42813
   318
          Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@22846
   319
  in
wenzelm@42358
   320
    [Pretty.big_list "methods:" (map prt_meth (Name_Space.extern_table ctxt meths))]
wenzelm@22846
   321
    |> Pretty.chunks |> Pretty.writeln
wenzelm@22846
   322
  end;
wenzelm@7611
   323
wenzelm@33092
   324
fun add_method name meth comment thy = thy
wenzelm@47005
   325
  |> Methods.map (Name_Space.define (Context.Theory thy) true (name, (meth, comment)) #> snd);
wenzelm@31304
   326
wenzelm@5824
   327
wenzelm@5824
   328
(* get methods *)
wenzelm@5824
   329
wenzelm@33095
   330
val intern = Name_Space.intern o #1 o Methods.get;
wenzelm@26892
   331
val defined = Symtab.defined o #2 o Methods.get;
wenzelm@26892
   332
wenzelm@20030
   333
fun method_i thy =
wenzelm@5824
   334
  let
wenzelm@42380
   335
    val (space, tab) = Methods.get thy;
wenzelm@5884
   336
    fun meth src =
wenzelm@20030
   337
      let val ((name, _), pos) = Args.dest_src src in
wenzelm@42380
   338
        (case Symtab.lookup tab name of
wenzelm@48992
   339
          NONE => error ("Unknown proof method: " ^ quote name ^ Position.here pos)
wenzelm@42380
   340
        | SOME (mth, _) => (Position.report pos (Name_Space.markup space name); mth src))
wenzelm@5824
   341
      end;
wenzelm@5824
   342
  in meth end;
wenzelm@5824
   343
wenzelm@45228
   344
fun method thy = method_i thy o Args.map_name (intern thy);
wenzelm@20030
   345
wenzelm@5824
   346
wenzelm@30512
   347
(* method setup *)
wenzelm@30512
   348
wenzelm@30512
   349
fun syntax scan = Args.context_syntax "method" scan;
wenzelm@30512
   350
wenzelm@31304
   351
fun setup name scan =
wenzelm@31304
   352
  add_method name
wenzelm@31304
   353
    (fn src => fn ctxt => let val (m, ctxt') = syntax scan src ctxt in m ctxt' end);
wenzelm@17356
   354
wenzelm@26385
   355
fun method_setup name (txt, pos) cmt =
wenzelm@26472
   356
  Context.theory_map (ML_Context.expression pos
wenzelm@30544
   357
    "val (name, scan, comment): binding * (Proof.context -> Proof.method) context_parser * string"
wenzelm@30544
   358
    "Context.map_theory (Method.setup name scan comment)"
wenzelm@37198
   359
    (ML_Lex.read Position.none ("(" ^ ML_Syntax.make_binding name ^ ", ") @
wenzelm@37198
   360
      ML_Lex.read pos txt @
wenzelm@37198
   361
      ML_Lex.read Position.none (", " ^ ML_Syntax.print_string cmt ^ ")")));
wenzelm@17356
   362
wenzelm@17356
   363
wenzelm@5884
   364
wenzelm@17110
   365
(** concrete syntax **)
wenzelm@5824
   366
wenzelm@5884
   367
(* sections *)
wenzelm@5824
   368
wenzelm@20289
   369
type modifier = (Proof.context -> Proof.context) * attribute;
wenzelm@7268
   370
wenzelm@7268
   371
local
wenzelm@7268
   372
wenzelm@24010
   373
fun thms ss = Scan.repeat (Scan.unless (Scan.lift (Scan.first ss)) Attrib.multi_thm) >> flat;
wenzelm@46775
   374
fun app (f, att) ths context = fold_map (Thm.apply_attribute att) ths (Context.map_proof f context);
wenzelm@5824
   375
wenzelm@30540
   376
in
wenzelm@30540
   377
wenzelm@24022
   378
fun section ss = Scan.depend (fn context => (Scan.first ss -- Scan.pass context (thms ss)) :|--
wenzelm@46775
   379
  (fn (m, ths) => Scan.succeed (swap (app m ths context))));
wenzelm@5884
   380
wenzelm@30540
   381
fun sections ss = Scan.repeat (section ss);
wenzelm@5824
   382
wenzelm@7268
   383
end;
wenzelm@7268
   384
wenzelm@5824
   385
wenzelm@30515
   386
(* extra rule methods *)
wenzelm@30515
   387
wenzelm@30515
   388
fun xrule_meth m =
wenzelm@36950
   389
  Scan.lift (Scan.optional (Args.parens Parse.nat) 0) -- Attrib.thms >>
wenzelm@30515
   390
  (fn (n, ths) => K (m n ths));
wenzelm@30515
   391
wenzelm@30515
   392
wenzelm@27813
   393
(* outer parser *)
wenzelm@27813
   394
wenzelm@27813
   395
fun is_symid_meth s =
wenzelm@36959
   396
  s <> "|" andalso s <> "?" andalso s <> "+" andalso Token.ident_or_symbolic s;
wenzelm@27813
   397
wenzelm@27813
   398
local
wenzelm@27813
   399
wenzelm@27813
   400
fun meth4 x =
wenzelm@36950
   401
 (Parse.position (Parse.xname >> rpair []) >> (Source o Args.src) ||
wenzelm@36950
   402
  Parse.$$$ "(" |-- Parse.!!! (meth0 --| Parse.$$$ ")")) x
wenzelm@27813
   403
and meth3 x =
wenzelm@36950
   404
 (meth4 --| Parse.$$$ "?" >> Try ||
wenzelm@36950
   405
  meth4 --| Parse.$$$ "+" >> Repeat1 ||
wenzelm@36950
   406
  meth4 -- (Parse.$$$ "[" |-- Scan.optional Parse.nat 1 --| Parse.$$$ "]") >> (SelectGoals o swap) ||
wenzelm@27813
   407
  meth4) x
wenzelm@27813
   408
and meth2 x =
wenzelm@36950
   409
 (Parse.position (Parse.xname -- Args.parse1 is_symid_meth) >> (Source o Args.src) ||
wenzelm@27813
   410
  meth3) x
wenzelm@36950
   411
and meth1 x = (Parse.enum1 "," meth2 >> (fn [m] => m | ms => Then ms)) x
wenzelm@36950
   412
and meth0 x = (Parse.enum1 "|" meth1 >> (fn [m] => m | ms => Orelse ms)) x;
wenzelm@27813
   413
wenzelm@27813
   414
in val parse = meth3 end;
wenzelm@27813
   415
wenzelm@27813
   416
wenzelm@18708
   417
(* theory setup *)
wenzelm@5824
   418
wenzelm@26463
   419
val _ = Context.>> (Context.map_theory
wenzelm@42616
   420
 (setup (Binding.name "fail") (Scan.succeed (K fail)) "force failure" #>
wenzelm@30515
   421
  setup (Binding.name "succeed") (Scan.succeed (K succeed)) "succeed" #>
wenzelm@30515
   422
  setup (Binding.name "-") (Scan.succeed (K insert_facts))
wenzelm@30515
   423
    "do nothing (insert current facts only)" #>
wenzelm@30515
   424
  setup (Binding.name "insert") (Attrib.thms >> (K o insert))
wenzelm@30515
   425
    "insert theorems, ignoring facts (improper)" #>
wenzelm@30515
   426
  setup (Binding.name "intro") (Attrib.thms >> (K o intro))
wenzelm@30515
   427
    "repeatedly apply introduction rules" #>
wenzelm@30515
   428
  setup (Binding.name "elim") (Attrib.thms >> (K o elim))
wenzelm@30515
   429
    "repeatedly apply elimination rules" #>
wenzelm@30515
   430
  setup (Binding.name "unfold") (Attrib.thms >> unfold_meth) "unfold definitions" #>
wenzelm@30515
   431
  setup (Binding.name "fold") (Attrib.thms >> fold_meth) "fold definitions" #>
wenzelm@30515
   432
  setup (Binding.name "atomize") (Args.mode "full" >> (K o atomize))
wenzelm@30515
   433
    "present local premises as object-level statements" #>
wenzelm@30515
   434
  setup (Binding.name "rule") (Attrib.thms >> some_rule) "apply some intro/elim rule" #>
wenzelm@30515
   435
  setup (Binding.name "erule") (xrule_meth erule) "apply rule in elimination manner (improper)" #>
wenzelm@30515
   436
  setup (Binding.name "drule") (xrule_meth drule) "apply rule in destruct manner (improper)" #>
wenzelm@30515
   437
  setup (Binding.name "frule") (xrule_meth frule) "apply rule in forward manner (improper)" #>
wenzelm@30515
   438
  setup (Binding.name "this") (Scan.succeed (K this)) "apply current facts as rules" #>
wenzelm@30515
   439
  setup (Binding.name "fact") (Attrib.thms >> fact) "composition by facts from context" #>
wenzelm@30515
   440
  setup (Binding.name "assumption") (Scan.succeed assumption)
wenzelm@30515
   441
    "proof by assumption, preferring facts" #>
wenzelm@30515
   442
  setup (Binding.name "rename_tac") (Args.goal_spec -- Scan.lift (Scan.repeat1 Args.name) >>
wenzelm@30515
   443
    (fn (quant, xs) => K (SIMPLE_METHOD'' quant (Tactic.rename_tac xs))))
wenzelm@30515
   444
    "rename parameters of goal" #>
wenzelm@36950
   445
  setup (Binding.name "rotate_tac") (Args.goal_spec -- Scan.lift (Scan.optional Parse.int 1) >>
wenzelm@30515
   446
    (fn (quant, i) => K (SIMPLE_METHOD'' quant (Tactic.rotate_tac i))))
wenzelm@30515
   447
      "rotate assumptions of goal" #>
wenzelm@37198
   448
  setup (Binding.name "tactic") (Scan.lift Args.name_source_position >> tactic)
wenzelm@30515
   449
    "ML tactic as proof method" #>
wenzelm@37198
   450
  setup (Binding.name "raw_tactic") (Scan.lift Args.name_source_position >> raw_tactic)
wenzelm@30515
   451
    "ML tactic as raw proof method"));
wenzelm@5824
   452
wenzelm@5824
   453
wenzelm@16145
   454
(*final declarations of this structure!*)
wenzelm@16145
   455
val unfold = unfold_meth;
wenzelm@16145
   456
val fold = fold_meth;
wenzelm@16145
   457
wenzelm@5824
   458
end;
wenzelm@5824
   459
wenzelm@30508
   460
val RAW_METHOD_CASES = Method.RAW_METHOD_CASES;
wenzelm@30508
   461
val RAW_METHOD = Method.RAW_METHOD;
wenzelm@30508
   462
val METHOD_CASES = Method.METHOD_CASES;
wenzelm@30508
   463
val METHOD = Method.METHOD;
wenzelm@30508
   464
val SIMPLE_METHOD = Method.SIMPLE_METHOD;
wenzelm@30508
   465
val SIMPLE_METHOD' = Method.SIMPLE_METHOD';
wenzelm@30508
   466
val SIMPLE_METHOD'' = Method.SIMPLE_METHOD'';
wenzelm@30508
   467