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