src/Pure/Isar/method.ML
author wenzelm
Sun Oct 14 20:09:59 2001 +0200 (2001-10-14)
changeset 11765 4c45eb23ef68
parent 11731 1a0c1ef86518
child 11785 3087d6f19adc
permissions -rw-r--r--
atomize_tac etc. moved to object_logic.ML;
wenzelm@5824
     1
(*  Title:      Pure/Isar/method.ML
wenzelm@5824
     2
    ID:         $Id$
wenzelm@5824
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8807
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@5824
     5
wenzelm@5824
     6
Proof methods.
wenzelm@5824
     7
*)
wenzelm@5824
     8
wenzelm@5824
     9
signature BASIC_METHOD =
wenzelm@5824
    10
sig
wenzelm@11731
    11
  val trace_rules: bool ref
wenzelm@5824
    12
  val print_methods: theory -> unit
wenzelm@5824
    13
  val Method: bstring -> (Args.src -> Proof.context -> Proof.method) -> string -> unit
wenzelm@5824
    14
end;
wenzelm@5824
    15
wenzelm@5824
    16
signature METHOD =
wenzelm@5824
    17
sig
wenzelm@5824
    18
  include BASIC_METHOD
wenzelm@11731
    19
  val trace: thm list -> unit
wenzelm@8153
    20
  val print_global_rules: theory -> unit
wenzelm@8153
    21
  val print_local_rules: Proof.context -> unit
wenzelm@8153
    22
  val dest_global: theory attribute
wenzelm@8153
    23
  val elim_global: theory attribute
wenzelm@8153
    24
  val intro_global: theory attribute
wenzelm@9938
    25
  val rule_del_global: theory attribute
wenzelm@8153
    26
  val dest_local: Proof.context attribute
wenzelm@8153
    27
  val elim_local: Proof.context attribute
wenzelm@8153
    28
  val intro_local: Proof.context attribute
wenzelm@9938
    29
  val rule_del_local: Proof.context attribute
wenzelm@6091
    30
  val METHOD: (thm list -> tactic) -> Proof.method
wenzelm@8372
    31
  val METHOD_CASES:
wenzelm@8372
    32
    (thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq) -> Proof.method
wenzelm@9706
    33
  val SIMPLE_METHOD: tactic -> Proof.method
wenzelm@9706
    34
  val SIMPLE_METHOD': ((int -> tactic) -> tactic) -> (int -> tactic) -> Proof.method
wenzelm@5824
    35
  val fail: Proof.method
wenzelm@5824
    36
  val succeed: Proof.method
wenzelm@8167
    37
  val defer: int option -> Proof.method
wenzelm@8167
    38
  val prefer: int -> Proof.method
wenzelm@7419
    39
  val insert_tac: thm list -> int -> tactic
wenzelm@7574
    40
  val insert: thm list -> Proof.method
wenzelm@7555
    41
  val insert_facts: Proof.method
wenzelm@7601
    42
  val unfold: thm list -> Proof.method
wenzelm@7419
    43
  val fold: thm list -> Proof.method
wenzelm@7419
    44
  val multi_resolve: thm list -> thm -> thm Seq.seq
wenzelm@7419
    45
  val multi_resolves: thm list -> thm list -> thm Seq.seq
wenzelm@6091
    46
  val rule_tac: thm list -> thm list -> int -> tactic
wenzelm@10309
    47
  val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic
wenzelm@6091
    48
  val rule: thm list -> Proof.method
wenzelm@10744
    49
  val erule: int -> thm list -> Proof.method
wenzelm@10744
    50
  val drule: int -> thm list -> Proof.method
wenzelm@10744
    51
  val frule: int -> thm list -> Proof.method
wenzelm@8195
    52
  val this: Proof.method
wenzelm@7555
    53
  val assumption: Proof.context -> Proof.method
wenzelm@8351
    54
  val set_tactic: (Proof.context -> thm list -> tactic) -> unit
wenzelm@8351
    55
  val tactic: string -> Proof.context -> Proof.method
wenzelm@5916
    56
  exception METHOD_FAIL of (string * Position.T) * exn
wenzelm@5824
    57
  val method: theory -> Args.src -> Proof.context -> Proof.method
wenzelm@9539
    58
  val add_method: bstring * (Args.src -> Proof.context -> Proof.method) * string
wenzelm@9539
    59
    -> theory -> theory
wenzelm@5824
    60
  val add_methods: (bstring * (Args.src -> Proof.context -> Proof.method) * string) list
wenzelm@5824
    61
    -> theory -> theory
wenzelm@5884
    62
  val syntax: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
wenzelm@8282
    63
    Args.src -> Proof.context -> Proof.context * 'a
wenzelm@8351
    64
  val simple_args: (Args.T list -> 'a * Args.T list)
wenzelm@8351
    65
    -> ('a -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
wenzelm@7555
    66
  val ctxt_args: (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
wenzelm@5884
    67
  val no_args: Proof.method -> Args.src -> Proof.context -> Proof.method
wenzelm@7268
    68
  type modifier
wenzelm@7601
    69
  val sectioned_args: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
wenzelm@7268
    70
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@9864
    71
    ('a -> Proof.context -> 'b) -> Args.src -> Proof.context -> 'b
wenzelm@7601
    72
  val bang_sectioned_args:
wenzelm@7601
    73
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@9864
    74
    (thm list -> Proof.context -> 'a) -> Args.src -> Proof.context -> 'a
wenzelm@9777
    75
  val bang_sectioned_args':
wenzelm@9777
    76
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@9777
    77
    (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
wenzelm@9864
    78
    ('a -> thm list -> Proof.context -> 'b) -> Args.src -> Proof.context -> 'b
wenzelm@7601
    79
  val only_sectioned_args:
wenzelm@7601
    80
    (Args.T list -> modifier * Args.T list) list ->
wenzelm@9864
    81
    (Proof.context -> 'a) -> Args.src -> Proof.context -> 'a
wenzelm@9864
    82
  val thms_ctxt_args: (thm list -> Proof.context -> 'a) -> Args.src -> Proof.context -> 'a
wenzelm@9864
    83
  val thms_args: (thm list -> 'a) -> Args.src -> Proof.context -> 'a
wenzelm@9864
    84
  val thm_args: (thm -> 'a) -> Args.src -> Proof.context -> 'a
wenzelm@5824
    85
  datatype text =
wenzelm@5824
    86
    Basic of (Proof.context -> Proof.method) |
wenzelm@5824
    87
    Source of Args.src |
wenzelm@5824
    88
    Then of text list |
wenzelm@5824
    89
    Orelse of text list |
wenzelm@5824
    90
    Try of text |
wenzelm@5824
    91
    Repeat1 of text
wenzelm@5824
    92
  val refine: text -> Proof.state -> Proof.state Seq.seq
wenzelm@8238
    93
  val refine_end: text -> Proof.state -> Proof.state Seq.seq
wenzelm@5824
    94
  val proof: text option -> Proof.state -> Proof.state Seq.seq
wenzelm@8966
    95
  val local_qed: bool -> text option
wenzelm@6981
    96
    -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
wenzelm@6736
    97
    -> Proof.state -> Proof.state Seq.seq
wenzelm@6981
    98
  val local_terminal_proof: text * text option
wenzelm@6981
    99
    -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
wenzelm@6736
   100
    -> Proof.state -> Proof.state Seq.seq
wenzelm@6981
   101
  val local_default_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
wenzelm@6736
   102
    -> Proof.state -> Proof.state Seq.seq
wenzelm@8966
   103
  val local_immediate_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
wenzelm@8966
   104
    -> Proof.state -> Proof.state Seq.seq
wenzelm@8966
   105
  val local_done_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
wenzelm@8966
   106
    -> Proof.state -> Proof.state Seq.seq
wenzelm@8966
   107
  val global_qed: bool -> text option
wenzelm@8966
   108
    -> Proof.state -> theory * {kind: string, name: string, thm: thm}
wenzelm@6934
   109
  val global_terminal_proof: text * text option
wenzelm@6934
   110
    -> Proof.state -> theory * {kind: string, name: string, thm: thm}
wenzelm@8966
   111
  val global_default_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
wenzelm@6532
   112
  val global_immediate_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
wenzelm@8966
   113
  val global_done_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
wenzelm@9539
   114
  val goal_args: (Args.T list -> 'a * Args.T list) -> ('a -> int -> tactic)
wenzelm@9539
   115
    -> Args.src -> Proof.context -> Proof.method
wenzelm@9539
   116
  val goal_args': (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))
wenzelm@9539
   117
    -> ('a -> int -> tactic) -> Args.src -> Proof.context -> Proof.method
wenzelm@5824
   118
  val setup: (theory -> theory) list
wenzelm@5824
   119
end;
wenzelm@5824
   120
wenzelm@5824
   121
structure Method: METHOD =
wenzelm@5824
   122
struct
wenzelm@5824
   123
wenzelm@5824
   124
wenzelm@11731
   125
(** tracing *)
wenzelm@11731
   126
wenzelm@11731
   127
val trace_rules = ref false;
wenzelm@11731
   128
wenzelm@11731
   129
fun trace rules =
wenzelm@11731
   130
  if not (! trace_rules) then ()
wenzelm@11731
   131
  else Pretty.writeln (Pretty.big_list "rules:" (map Display.pretty_thm rules));
wenzelm@11731
   132
wenzelm@11731
   133
wenzelm@11731
   134
wenzelm@8153
   135
(** global and local rule data **)
wenzelm@8153
   136
wenzelm@10008
   137
local
wenzelm@10008
   138
  fun prt_rules kind sg ths =
wenzelm@10008
   139
    Pretty.writeln (Pretty.big_list ("standard " ^ kind ^ " rules:")
wenzelm@10008
   140
      (map (Display.pretty_thm_sg sg) ths));
wenzelm@10008
   141
in
wenzelm@10008
   142
  fun print_rules sg (intro, elim) =
wenzelm@10008
   143
    (prt_rules "introduction" sg intro; prt_rules "elimination" sg elim);
wenzelm@10008
   144
end;
wenzelm@8153
   145
wenzelm@8153
   146
wenzelm@8153
   147
(* theory data kind 'Isar/rules' *)
wenzelm@8153
   148
wenzelm@8153
   149
structure GlobalRulesArgs =
wenzelm@8153
   150
struct
wenzelm@8153
   151
  val name = "Isar/rules";
wenzelm@8153
   152
  type T = thm list * thm list;
wenzelm@8153
   153
wenzelm@8153
   154
  val empty = ([], []);
wenzelm@8153
   155
  val copy = I;
wenzelm@8153
   156
  val prep_ext = I;
wenzelm@8153
   157
  fun merge ((intro1, elim1), (intro2, elim2)) =
wenzelm@9418
   158
    (Drule.merge_rules (intro1, intro2), Drule.merge_rules (elim1, elim2));
wenzelm@10008
   159
  val print = print_rules;
wenzelm@8153
   160
end;
wenzelm@8153
   161
wenzelm@8153
   162
structure GlobalRules = TheoryDataFun(GlobalRulesArgs);
wenzelm@8153
   163
val print_global_rules = GlobalRules.print;
wenzelm@8153
   164
wenzelm@8153
   165
wenzelm@8153
   166
(* proof data kind 'Isar/rules' *)
wenzelm@8153
   167
wenzelm@8153
   168
structure LocalRulesArgs =
wenzelm@8153
   169
struct
wenzelm@8153
   170
  val name = "Isar/rules";
wenzelm@8153
   171
  type T = thm list * thm list;
wenzelm@8153
   172
wenzelm@8153
   173
  val init = GlobalRules.get;
wenzelm@10008
   174
  val print = print_rules o ProofContext.sign_of;
wenzelm@8153
   175
end;
wenzelm@8153
   176
wenzelm@8153
   177
structure LocalRules = ProofDataFun(LocalRulesArgs);
wenzelm@8153
   178
val print_local_rules = LocalRules.print;
wenzelm@8153
   179
wenzelm@8153
   180
wenzelm@8153
   181
wenzelm@8153
   182
(** attributes **)
wenzelm@8153
   183
wenzelm@8153
   184
(* add rules *)
wenzelm@8153
   185
wenzelm@8153
   186
local
wenzelm@8153
   187
wenzelm@9938
   188
fun add_dest th (intro, elim) = (intro, Drule.add_rules [Tactic.make_elim th] elim);
wenzelm@9938
   189
fun add_elim th (intro, elim) = (intro, Drule.add_rules [th] elim);
wenzelm@9938
   190
fun add_intro th (intro, elim) = (Drule.add_rules [th] intro, elim);
wenzelm@8153
   191
wenzelm@9938
   192
fun del_rule th (intro, elim) =
wenzelm@9938
   193
  let
wenzelm@9938
   194
    val th' = Tactic.make_elim th;
wenzelm@9938
   195
    val del = Drule.del_rules [th'] o Drule.del_rules [th];
wenzelm@9938
   196
  in (del intro, del elim) end;
wenzelm@8153
   197
wenzelm@9938
   198
fun mk_att f g (x, th) = (f (g th) x, th);
wenzelm@8153
   199
wenzelm@8153
   200
in
wenzelm@8153
   201
wenzelm@8153
   202
val dest_global = mk_att GlobalRules.map add_dest;
wenzelm@8153
   203
val elim_global = mk_att GlobalRules.map add_elim;
wenzelm@8153
   204
val intro_global = mk_att GlobalRules.map add_intro;
wenzelm@9938
   205
val rule_del_global = mk_att GlobalRules.map del_rule;
wenzelm@8153
   206
wenzelm@8153
   207
val dest_local = mk_att LocalRules.map add_dest;
wenzelm@8153
   208
val elim_local = mk_att LocalRules.map add_elim;
wenzelm@8153
   209
val intro_local = mk_att LocalRules.map add_intro;
wenzelm@9938
   210
val rule_del_local = mk_att LocalRules.map del_rule;
wenzelm@9938
   211
wenzelm@10034
   212
fun del_args att = Attrib.syntax (Scan.lift Args.del >> K att);
wenzelm@8153
   213
wenzelm@8153
   214
end;
wenzelm@8153
   215
wenzelm@8153
   216
wenzelm@8153
   217
(* concrete syntax *)
wenzelm@8153
   218
wenzelm@8153
   219
val rule_atts =
wenzelm@9900
   220
 [("dest", (Attrib.no_args dest_global, Attrib.no_args dest_local),
wenzelm@9900
   221
    "declaration of destruction rule"),
wenzelm@9900
   222
  ("elim", (Attrib.no_args elim_global, Attrib.no_args elim_local),
wenzelm@9900
   223
    "declaration of elimination rule"),
wenzelm@9900
   224
  ("intro", (Attrib.no_args intro_global, Attrib.no_args intro_local),
wenzelm@9900
   225
    "declaration of introduction rule"),
wenzelm@9938
   226
  ("rule", (del_args rule_del_global, del_args rule_del_local),
wenzelm@9900
   227
    "remove declaration of elim/intro rule")];
wenzelm@8153
   228
wenzelm@8153
   229
wenzelm@8153
   230
wenzelm@5824
   231
(** proof methods **)
wenzelm@5824
   232
wenzelm@8372
   233
(* make methods *)
wenzelm@5824
   234
wenzelm@6849
   235
val METHOD = Proof.method;
wenzelm@8372
   236
val METHOD_CASES = Proof.method_cases;
wenzelm@8372
   237
wenzelm@5824
   238
wenzelm@5824
   239
(* primitive *)
wenzelm@5824
   240
wenzelm@5824
   241
val fail = METHOD (K no_tac);
wenzelm@5824
   242
val succeed = METHOD (K all_tac);
wenzelm@5824
   243
wenzelm@5824
   244
wenzelm@8167
   245
(* shuffle *)
wenzelm@8167
   246
wenzelm@8240
   247
fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1)));
wenzelm@8167
   248
fun defer opt_i = METHOD (K (Tactic.defer_tac (if_none opt_i 1)));
wenzelm@8167
   249
wenzelm@8167
   250
wenzelm@7419
   251
(* insert *)
wenzelm@7419
   252
wenzelm@7419
   253
local
wenzelm@5824
   254
wenzelm@6981
   255
fun cut_rule_tac raw_rule =
wenzelm@6981
   256
  let
wenzelm@6981
   257
    val rule = Drule.forall_intr_vars raw_rule;
wenzelm@6981
   258
    val revcut_rl = Drule.incr_indexes_wrt [] [] [] [rule] Drule.revcut_rl;
wenzelm@7555
   259
  in Tactic.rtac (rule COMP revcut_rl) end;
wenzelm@6981
   260
wenzelm@7419
   261
in
wenzelm@5824
   262
wenzelm@7419
   263
fun insert_tac [] i = all_tac
wenzelm@7419
   264
  | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
wenzelm@6981
   265
wenzelm@7555
   266
val insert_facts = METHOD (ALLGOALS o insert_tac);
wenzelm@7664
   267
fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
wenzelm@7419
   268
wenzelm@7419
   269
end;
wenzelm@5824
   270
wenzelm@5824
   271
wenzelm@9706
   272
(* simple methods *)
wenzelm@9706
   273
wenzelm@9706
   274
fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac);
wenzelm@9706
   275
fun SIMPLE_METHOD' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac));
wenzelm@9706
   276
wenzelm@9706
   277
wenzelm@7601
   278
(* unfold / fold definitions *)
wenzelm@6532
   279
wenzelm@10821
   280
fun unfold thms = SIMPLE_METHOD (CHANGED_PROP (rewrite_goals_tac thms));
wenzelm@10821
   281
fun fold thms = SIMPLE_METHOD (CHANGED_PROP (fold_goals_tac thms));
wenzelm@9484
   282
wenzelm@9484
   283
wenzelm@7419
   284
(* multi_resolve *)
wenzelm@7419
   285
wenzelm@7419
   286
local
wenzelm@7419
   287
wenzelm@7419
   288
fun res th i rule =
wenzelm@7419
   289
  Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty;
wenzelm@7419
   290
wenzelm@7419
   291
fun multi_res _ [] rule = Seq.single rule
wenzelm@7419
   292
  | multi_res i (th :: ths) rule = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths rule));
wenzelm@7419
   293
wenzelm@7419
   294
in
wenzelm@7419
   295
wenzelm@7419
   296
val multi_resolve = multi_res 1;
wenzelm@8372
   297
fun multi_resolves facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules));
wenzelm@7419
   298
wenzelm@7419
   299
end;
wenzelm@7419
   300
wenzelm@7419
   301
wenzelm@10744
   302
(* basic rules *)
wenzelm@8372
   303
wenzelm@7419
   304
local
wenzelm@5824
   305
wenzelm@10541
   306
fun gen_rule_tac tac rules [] i st = tac rules i st
wenzelm@10541
   307
  | gen_rule_tac tac erules facts i st =
wenzelm@10541
   308
      Seq.flat (Seq.map (fn rule => (tac o single) rule i st) (multi_resolves facts erules));
wenzelm@7130
   309
wenzelm@10744
   310
fun gen_arule_tac tac j rules facts =
wenzelm@10744
   311
  EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac);
wenzelm@10744
   312
wenzelm@10309
   313
fun gen_some_rule_tac tac arg_rules ctxt facts =
wenzelm@8153
   314
  let val rules =
wenzelm@8153
   315
    if not (null arg_rules) then arg_rules
wenzelm@8153
   316
    else if null facts then #1 (LocalRules.get ctxt)
wenzelm@8153
   317
    else op @ (LocalRules.get ctxt);
wenzelm@11731
   318
  in trace rules; tac rules facts end;
wenzelm@10309
   319
wenzelm@10744
   320
fun meth tac x = METHOD (HEADGOAL o tac x);
wenzelm@10744
   321
fun meth' tac x y = METHOD (HEADGOAL o tac x y);
wenzelm@8220
   322
wenzelm@7419
   323
in
wenzelm@7419
   324
wenzelm@10744
   325
val rule_tac = gen_rule_tac Tactic.resolve_tac;
wenzelm@10744
   326
val rule = meth rule_tac;
wenzelm@10744
   327
val some_rule_tac = gen_some_rule_tac rule_tac;
wenzelm@10744
   328
val some_rule = meth' some_rule_tac;
wenzelm@10744
   329
wenzelm@10744
   330
val erule = meth' (gen_arule_tac Tactic.eresolve_tac);
wenzelm@10744
   331
val drule = meth' (gen_arule_tac Tactic.dresolve_tac);
wenzelm@10744
   332
val frule = meth' (gen_arule_tac Tactic.forward_tac);
wenzelm@5824
   333
wenzelm@7419
   334
end;
wenzelm@7419
   335
wenzelm@7419
   336
wenzelm@8195
   337
(* this *)
wenzelm@8195
   338
wenzelm@8671
   339
val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac));
wenzelm@8195
   340
wenzelm@8195
   341
wenzelm@8195
   342
(* assumption *)
wenzelm@7555
   343
wenzelm@10378
   344
fun asm_tac ths =
wenzelm@10378
   345
  foldr (op APPEND') (map (fn th => Tactic.rtac th THEN_ALL_NEW assume_tac) ths, K no_tac);
wenzelm@10378
   346
wenzelm@10405
   347
fun assm_tac ctxt =
wenzelm@10405
   348
  assume_tac APPEND'
wenzelm@10405
   349
  asm_tac (ProofContext.prems_of ctxt) APPEND'
wenzelm@10405
   350
  Tactic.rtac Drule.reflexive_thm;
wenzelm@7419
   351
wenzelm@7555
   352
fun assumption_tac ctxt [] = assm_tac ctxt
wenzelm@10378
   353
  | assumption_tac _ [fact] = asm_tac [fact]
wenzelm@7555
   354
  | assumption_tac _ _ = K no_tac;
wenzelm@7419
   355
wenzelm@8671
   356
fun assumption ctxt = METHOD (HEADGOAL o assumption_tac ctxt);
wenzelm@7419
   357
wenzelm@7419
   358
wenzelm@9539
   359
(* res_inst_tac etc. *)
wenzelm@8238
   360
wenzelm@9539
   361
(*Note: insts refer to the implicit (!!) goal context; use at your own risk*)
wenzelm@9565
   362
fun gen_res_inst _ tac (quant, ([], thms)) =
wenzelm@9565
   363
      METHOD (fn facts => (quant (insert_tac facts THEN' tac thms)))
wenzelm@9565
   364
  | gen_res_inst tac _ (quant, (insts, [thm])) =
wenzelm@9565
   365
      METHOD (fn facts => (quant (insert_tac facts THEN' tac insts thm)))
wenzelm@9565
   366
  | gen_res_inst _ _ _ = error "Cannot have instantiations with multiple rules";
wenzelm@8238
   367
wenzelm@9565
   368
val res_inst = gen_res_inst Tactic.res_inst_tac Tactic.resolve_tac;
wenzelm@9565
   369
val eres_inst = gen_res_inst Tactic.eres_inst_tac Tactic.eresolve_tac;
wenzelm@9565
   370
val dres_inst = gen_res_inst Tactic.dres_inst_tac Tactic.dresolve_tac;
wenzelm@9565
   371
val forw_inst = gen_res_inst Tactic.forw_inst_tac Tactic.forward_tac;
wenzelm@9565
   372
val cut_inst = gen_res_inst Tactic.cut_inst_tac Tactic.cut_facts_tac;
wenzelm@8238
   373
wenzelm@8238
   374
wenzelm@8329
   375
(* simple Prolog interpreter *)
wenzelm@8329
   376
wenzelm@8329
   377
fun prolog_tac rules facts =
wenzelm@8329
   378
  DEPTH_SOLVE_1 (HEADGOAL (Tactic.assume_tac APPEND' Tactic.resolve_tac (facts @ rules)));
wenzelm@8329
   379
wenzelm@8329
   380
val prolog = METHOD o prolog_tac;
wenzelm@8329
   381
wenzelm@8329
   382
wenzelm@8351
   383
(* ML tactics *)
wenzelm@8351
   384
wenzelm@8351
   385
val tactic_ref = ref ((fn _ => raise Match): Proof.context -> thm list -> tactic);
wenzelm@8351
   386
fun set_tactic f = tactic_ref := f;
wenzelm@8351
   387
wenzelm@8351
   388
fun tactic txt ctxt = METHOD (fn facts =>
wenzelm@9631
   389
  (Context.use_mltext
wenzelm@9631
   390
    ("let fun tactic (ctxt: PureIsar.Proof.context) (facts: thm list) : tactic = \
wenzelm@9631
   391
     \let val thm = PureIsar.ProofContext.get_thm_closure ctxt\n\
wenzelm@9631
   392
     \  and thms = PureIsar.ProofContext.get_thms_closure ctxt in\n"
wenzelm@9631
   393
     ^ txt ^
wenzelm@9631
   394
     "\nend in PureIsar.Method.set_tactic tactic end")
wenzelm@9631
   395
    false None;
wenzelm@9631
   396
    Context.setmp (Some (ProofContext.theory_of ctxt)) (! tactic_ref ctxt) facts));
wenzelm@8351
   397
wenzelm@8351
   398
wenzelm@5824
   399
wenzelm@5824
   400
(** methods theory data **)
wenzelm@5824
   401
wenzelm@5824
   402
(* data kind 'Isar/methods' *)
wenzelm@5824
   403
wenzelm@5824
   404
structure MethodsDataArgs =
wenzelm@5824
   405
struct
wenzelm@5824
   406
  val name = "Isar/methods";
wenzelm@5824
   407
  type T =
wenzelm@5824
   408
    {space: NameSpace.T,
wenzelm@5824
   409
     meths: (((Args.src -> Proof.context -> Proof.method) * string) * stamp) Symtab.table};
wenzelm@5824
   410
wenzelm@5824
   411
  val empty = {space = NameSpace.empty, meths = Symtab.empty};
wenzelm@6546
   412
  val copy = I;
wenzelm@5824
   413
  val prep_ext = I;
wenzelm@5824
   414
  fun merge ({space = space1, meths = meths1}, {space = space2, meths = meths2}) =
wenzelm@5824
   415
    {space = NameSpace.merge (space1, space2),
wenzelm@5824
   416
      meths = Symtab.merge eq_snd (meths1, meths2) handle Symtab.DUPS dups =>
wenzelm@5824
   417
        error ("Attempt to merge different versions of methods " ^ commas_quote dups)};
wenzelm@5824
   418
wenzelm@9222
   419
  fun print _ {space, meths} =
wenzelm@5824
   420
    let
wenzelm@5824
   421
      fun prt_meth (name, ((_, comment), _)) = Pretty.block
wenzelm@6849
   422
        [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@5824
   423
    in
wenzelm@8720
   424
      [Pretty.big_list "methods:" (map prt_meth (NameSpace.cond_extern_table space meths))]
wenzelm@9222
   425
      |> Pretty.chunks |> Pretty.writeln
wenzelm@5824
   426
    end;
wenzelm@5824
   427
end;
wenzelm@5824
   428
wenzelm@5824
   429
structure MethodsData = TheoryDataFun(MethodsDataArgs);
wenzelm@5824
   430
val print_methods = MethodsData.print;
wenzelm@7611
   431
wenzelm@5824
   432
wenzelm@5824
   433
(* get methods *)
wenzelm@5824
   434
wenzelm@5916
   435
exception METHOD_FAIL of (string * Position.T) * exn;
wenzelm@5916
   436
wenzelm@5824
   437
fun method thy =
wenzelm@5824
   438
  let
wenzelm@5824
   439
    val {space, meths} = MethodsData.get thy;
wenzelm@5824
   440
wenzelm@5884
   441
    fun meth src =
wenzelm@5884
   442
      let
wenzelm@5884
   443
        val ((raw_name, _), pos) = Args.dest_src src;
wenzelm@5884
   444
        val name = NameSpace.intern space raw_name;
wenzelm@5884
   445
      in
wenzelm@5824
   446
        (case Symtab.lookup (meths, name) of
wenzelm@5824
   447
          None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
wenzelm@5916
   448
        | Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
wenzelm@5824
   449
      end;
wenzelm@5824
   450
  in meth end;
wenzelm@5824
   451
wenzelm@5824
   452
wenzelm@9194
   453
(* add_method(s) *)
wenzelm@5824
   454
wenzelm@5824
   455
fun add_methods raw_meths thy =
wenzelm@5824
   456
  let
wenzelm@5824
   457
    val full = Sign.full_name (Theory.sign_of thy);
wenzelm@5824
   458
    val new_meths =
wenzelm@5824
   459
      map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths;
wenzelm@5824
   460
wenzelm@5824
   461
    val {space, meths} = MethodsData.get thy;
wenzelm@5824
   462
    val space' = NameSpace.extend (space, map fst new_meths);
wenzelm@5824
   463
    val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups =>
wenzelm@5824
   464
      error ("Duplicate declaration of method(s) " ^ commas_quote dups);
wenzelm@5824
   465
  in
wenzelm@5824
   466
    thy |> MethodsData.put {space = space', meths = meths'}
wenzelm@5824
   467
  end;
wenzelm@5824
   468
wenzelm@9194
   469
val add_method = add_methods o Library.single;
wenzelm@9194
   470
wenzelm@5824
   471
(*implicit version*)
wenzelm@5824
   472
fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]);
wenzelm@5824
   473
wenzelm@5824
   474
wenzelm@5884
   475
wenzelm@5884
   476
(** method syntax **)
wenzelm@5824
   477
wenzelm@5884
   478
(* basic *)
wenzelm@5884
   479
wenzelm@5884
   480
fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
wenzelm@5884
   481
  Args.syntax "method" scan;
wenzelm@5824
   482
wenzelm@8351
   483
fun simple_args scan f src ctxt : Proof.method =
wenzelm@8351
   484
  #2 (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt);
wenzelm@8351
   485
wenzelm@7555
   486
fun ctxt_args (f: Proof.context -> Proof.method) src ctxt =
wenzelm@8282
   487
  #2 (syntax (Scan.succeed (f ctxt)) src ctxt);
wenzelm@7555
   488
wenzelm@7555
   489
fun no_args m = ctxt_args (K m);
wenzelm@5884
   490
wenzelm@5884
   491
wenzelm@8351
   492
wenzelm@5884
   493
(* sections *)
wenzelm@5824
   494
wenzelm@7268
   495
type modifier = (Proof.context -> Proof.context) * Proof.context attribute;
wenzelm@7268
   496
wenzelm@7268
   497
local
wenzelm@7268
   498
wenzelm@8381
   499
fun sect ss = Scan.first (map Scan.lift ss);
wenzelm@5884
   500
fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
wenzelm@5884
   501
fun thmss ss = Scan.repeat (thms ss) >> flat;
wenzelm@5884
   502
wenzelm@7268
   503
fun apply (f, att) (ctxt, ths) = Thm.applys_attributes ((f ctxt, ths), [att]);
wenzelm@5824
   504
wenzelm@7268
   505
fun section ss = (sect ss -- thmss ss) :-- (fn (m, ths) => Scan.depend (fn ctxt =>
wenzelm@7268
   506
  Scan.succeed (apply m (ctxt, ths)))) >> #2;
wenzelm@5884
   507
wenzelm@7601
   508
fun sectioned args ss = args -- Scan.repeat (section ss);
wenzelm@5884
   509
wenzelm@7268
   510
in
wenzelm@5824
   511
wenzelm@5884
   512
fun sectioned_args args ss f src ctxt =
wenzelm@8282
   513
  let val (ctxt', (x, _)) = syntax (sectioned args ss) src ctxt
wenzelm@5921
   514
  in f x ctxt' end;
wenzelm@5884
   515
wenzelm@7601
   516
fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f;
wenzelm@9777
   517
fun bang_sectioned_args' ss scan f =
wenzelm@9777
   518
  sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f);
wenzelm@7601
   519
fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f);
wenzelm@7268
   520
wenzelm@8093
   521
fun thms_ctxt_args f = sectioned_args (thmss []) [] f;
wenzelm@8093
   522
fun thms_args f = thms_ctxt_args (K o f);
wenzelm@9706
   523
fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected");
wenzelm@5824
   524
wenzelm@7268
   525
end;
wenzelm@7268
   526
wenzelm@5824
   527
wenzelm@9539
   528
(* tactic syntax *)
wenzelm@8238
   529
wenzelm@10744
   530
fun nat_thms_args f = uncurry f oo
wenzelm@10744
   531
  (#2 oo syntax (Scan.lift (Scan.optional (Args.parens Args.nat) 0) -- Attrib.local_thmss));
wenzelm@10744
   532
wenzelm@8238
   533
val insts =
wenzelm@9539
   534
  Scan.optional
wenzelm@9565
   535
    (Args.enum1 "and" (Scan.lift (Args.name -- (Args.$$$ "=" |-- Args.!!! Args.name))) --|
wenzelm@9565
   536
      Scan.lift (Args.$$$ "in")) [] -- Attrib.local_thmss;
wenzelm@8238
   537
wenzelm@8537
   538
fun inst_args f = f oo (#2 oo syntax (Args.goal_spec HEADGOAL -- insts));
wenzelm@8537
   539
wenzelm@8537
   540
wenzelm@9539
   541
fun goal_args' args tac = #2 oo syntax (Args.goal_spec HEADGOAL -- args >>
wenzelm@9706
   542
  (fn (quant, s) => SIMPLE_METHOD' quant (tac s)));
wenzelm@8537
   543
wenzelm@9539
   544
fun goal_args args tac = goal_args' (Scan.lift args) tac;
wenzelm@8238
   545
wenzelm@8238
   546
wenzelm@5824
   547
wenzelm@5824
   548
(** method text **)
wenzelm@5824
   549
wenzelm@5824
   550
(* datatype text *)
wenzelm@5824
   551
wenzelm@5824
   552
datatype text =
wenzelm@5824
   553
  Basic of (Proof.context -> Proof.method) |
wenzelm@5824
   554
  Source of Args.src |
wenzelm@5824
   555
  Then of text list |
wenzelm@5824
   556
  Orelse of text list |
wenzelm@5824
   557
  Try of text |
wenzelm@5824
   558
  Repeat1 of text;
wenzelm@5824
   559
wenzelm@5824
   560
wenzelm@5824
   561
(* refine *)
wenzelm@5824
   562
wenzelm@8238
   563
fun gen_refine f text state =
wenzelm@5824
   564
  let
wenzelm@5824
   565
    val thy = Proof.theory_of state;
wenzelm@5824
   566
wenzelm@8238
   567
    fun eval (Basic mth) = f mth
wenzelm@8238
   568
      | eval (Source src) = f (method thy src)
wenzelm@5824
   569
      | eval (Then txts) = Seq.EVERY (map eval txts)
wenzelm@5824
   570
      | eval (Orelse txts) = Seq.FIRST (map eval txts)
wenzelm@5824
   571
      | eval (Try txt) = Seq.TRY (eval txt)
wenzelm@5824
   572
      | eval (Repeat1 txt) = Seq.REPEAT1 (eval txt);
wenzelm@5824
   573
  in eval text state end;
wenzelm@5824
   574
wenzelm@8238
   575
val refine = gen_refine Proof.refine;
wenzelm@8238
   576
val refine_end = gen_refine Proof.refine_end;
wenzelm@6404
   577
wenzelm@5824
   578
wenzelm@6404
   579
(* structured proof steps *)
wenzelm@5824
   580
wenzelm@7506
   581
val default_text = Source (Args.src (("default", []), Position.none));
wenzelm@8195
   582
val this_text = Basic (K this);
wenzelm@9706
   583
val done_text = Basic (K (SIMPLE_METHOD all_tac));
wenzelm@7555
   584
wenzelm@8966
   585
fun close_text asm = Basic (fn ctxt => METHOD (K
wenzelm@8966
   586
  (FILTER Thm.no_prems ((if asm then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac))));
wenzelm@8966
   587
wenzelm@8966
   588
fun finish_text asm None = close_text asm
wenzelm@8966
   589
  | finish_text asm (Some txt) = Then [txt, close_text asm];
wenzelm@6872
   590
wenzelm@5824
   591
fun proof opt_text state =
wenzelm@5824
   592
  state
wenzelm@5824
   593
  |> Proof.assert_backward
wenzelm@6404
   594
  |> refine (if_none opt_text default_text)
wenzelm@8242
   595
  |> Seq.map (Proof.goal_facts (K []))
wenzelm@5824
   596
  |> Seq.map Proof.enter_forward;
wenzelm@5824
   597
wenzelm@8966
   598
fun local_qed asm opt_text = Proof.local_qed (refine (finish_text asm opt_text));
wenzelm@8966
   599
fun local_terminal_proof (text, opt_text) pr =
wenzelm@8966
   600
  Seq.THEN (proof (Some text), local_qed true opt_text pr);
wenzelm@8966
   601
val local_default_proof = local_terminal_proof (default_text, None);
wenzelm@8195
   602
val local_immediate_proof = local_terminal_proof (this_text, None);
wenzelm@8966
   603
fun local_done_proof pr = Seq.THEN (proof (Some done_text), local_qed false None pr);
wenzelm@5824
   604
wenzelm@6872
   605
wenzelm@8966
   606
fun global_qeds asm opt_text = Proof.global_qed (refine (finish_text asm opt_text));
wenzelm@5824
   607
wenzelm@8966
   608
fun global_qed asm opt_text state =
wenzelm@6872
   609
  state
wenzelm@8966
   610
  |> global_qeds asm opt_text
wenzelm@6872
   611
  |> Proof.check_result "Failed to finish proof" state
wenzelm@6872
   612
  |> Seq.hd;
wenzelm@6872
   613
wenzelm@8966
   614
fun global_term_proof asm (text, opt_text) state =
wenzelm@6872
   615
  state
wenzelm@6872
   616
  |> proof (Some text)
wenzelm@6872
   617
  |> Proof.check_result "Terminal proof method failed" state
wenzelm@8966
   618
  |> (Seq.flat o Seq.map (global_qeds asm opt_text))
wenzelm@6872
   619
  |> Proof.check_result "Failed to finish proof (after successful terminal method)" state
wenzelm@6872
   620
  |> Seq.hd;
wenzelm@6872
   621
wenzelm@8966
   622
val global_terminal_proof = global_term_proof true;
wenzelm@6934
   623
val global_default_proof = global_terminal_proof (default_text, None);
wenzelm@8966
   624
val global_immediate_proof = global_terminal_proof (this_text, None);
wenzelm@8966
   625
val global_done_proof = global_term_proof false (done_text, None);
wenzelm@5824
   626
wenzelm@5824
   627
wenzelm@5824
   628
(** theory setup **)
wenzelm@5824
   629
wenzelm@9539
   630
(* misc tactic emulations *)
wenzelm@9539
   631
wenzelm@9539
   632
val subgoal_meth = goal_args (Scan.repeat1 Args.name) Tactic.subgoals_tac;
wenzelm@9539
   633
val thin_meth = goal_args Args.name Tactic.thin_tac;
wenzelm@9539
   634
val rename_meth = goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac;
wenzelm@9631
   635
val rotate_meth = goal_args (Scan.optional Args.int 1) Tactic.rotate_tac;
wenzelm@9539
   636
wenzelm@9539
   637
wenzelm@5824
   638
(* pure_methods *)
wenzelm@5824
   639
wenzelm@5824
   640
val pure_methods =
wenzelm@5824
   641
 [("fail", no_args fail, "force failure"),
wenzelm@5824
   642
  ("succeed", no_args succeed, "succeed"),
wenzelm@9587
   643
  ("-", no_args insert_facts, "do nothing (insert current facts only)"),
wenzelm@9539
   644
  ("insert", thms_args insert, "insert theorems, ignoring facts (improper)"),
wenzelm@7601
   645
  ("unfold", thms_args unfold, "unfold definitions"),
wenzelm@7601
   646
  ("fold", thms_args fold, "fold definitions"),
wenzelm@8153
   647
  ("rule", thms_ctxt_args some_rule, "apply some rule"),
wenzelm@10744
   648
  ("erule", nat_thms_args erule, "apply rule in elimination manner (improper)"),
wenzelm@10744
   649
  ("drule", nat_thms_args drule, "apply rule in destruct manner (improper)"),
wenzelm@10744
   650
  ("frule", nat_thms_args frule, "apply rule in forward manner (improper)"),
wenzelm@8195
   651
  ("this", no_args this, "apply current facts as rules"),
wenzelm@8238
   652
  ("assumption", ctxt_args assumption, "proof by assumption, preferring facts"),
wenzelm@9539
   653
  ("rule_tac", inst_args res_inst, "apply rule (dynamic instantiation!)"),
wenzelm@9539
   654
  ("erule_tac", inst_args eres_inst, "apply rule in elimination manner (dynamic instantiation!)"),
wenzelm@9539
   655
  ("drule_tac", inst_args dres_inst, "apply rule in destruct manner (dynamic instantiation!)"),
wenzelm@9539
   656
  ("frule_tac", inst_args forw_inst, "apply rule in forward manner (dynamic instantiation!)"),
wenzelm@9539
   657
  ("cut_tac", inst_args cut_inst, "cut rule (dynamic instantiation!)"),
wenzelm@9565
   658
  ("subgoal_tac", subgoal_meth, "insert subgoal (dynamic instantiation!)"),
wenzelm@9565
   659
  ("thin_tac", thin_meth, "remove premise (dynamic instantiation!)"),
wenzelm@9631
   660
  ("rename_tac", rename_meth, "rename parameters of goal (dynamic instantiation!)"),
wenzelm@9631
   661
  ("rotate_tac", rotate_meth, "rotate assumptions of goal"),
wenzelm@8351
   662
  ("prolog", thms_args prolog, "simple prolog interpreter"),
wenzelm@8351
   663
  ("tactic", simple_args Args.name tactic, "ML tactic as proof method")];
wenzelm@5824
   664
wenzelm@5824
   665
wenzelm@5824
   666
(* setup *)
wenzelm@5824
   667
wenzelm@8153
   668
val setup =
wenzelm@8153
   669
 [GlobalRules.init, LocalRules.init, Attrib.add_attributes rule_atts,
wenzelm@8153
   670
  MethodsData.init, add_methods pure_methods];
wenzelm@5824
   671
wenzelm@5824
   672
wenzelm@5824
   673
end;
wenzelm@5824
   674
wenzelm@5824
   675
wenzelm@5824
   676
structure BasicMethod: BASIC_METHOD = Method;
wenzelm@5824
   677
open BasicMethod;