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