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