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