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