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