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