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