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