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