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