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