src/Pure/Isar/method.ML
author wenzelm
Mon Jul 12 22:27:20 1999 +0200 (1999-07-12)
changeset 6981 eaade7e398a7
parent 6951 13399b560e4e
child 7130 a17f7b5ac40f
permissions -rw-r--r--
export assumption_tac;
local qeds: print rule;
same_tac: actually insert rules, !! bind vars;
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@6775
    17
  val multi_resolve: thm list -> thm -> thm Seq.seq
wenzelm@6500
    18
  val FINISHED: tactic -> tactic
wenzelm@6091
    19
  val METHOD: (thm list -> tactic) -> Proof.method
wenzelm@5824
    20
  val METHOD0: tactic -> Proof.method
wenzelm@5824
    21
  val fail: Proof.method
wenzelm@5824
    22
  val succeed: Proof.method
wenzelm@6500
    23
  val same_tac: thm list -> int -> tactic
wenzelm@6108
    24
  val same: Proof.method
wenzelm@6981
    25
  val assumption_tac: thm list -> int -> tactic
wenzelm@5824
    26
  val assumption: Proof.method
wenzelm@5824
    27
  val forward_chain: thm list -> thm list -> thm Seq.seq
wenzelm@6091
    28
  val rule_tac: thm list -> thm list -> int -> tactic
wenzelm@6091
    29
  val rule: thm list -> Proof.method
wenzelm@5916
    30
  exception METHOD_FAIL of (string * Position.T) * exn
wenzelm@5824
    31
  val method: theory -> Args.src -> Proof.context -> Proof.method
wenzelm@5824
    32
  val add_methods: (bstring * (Args.src -> Proof.context -> Proof.method) * string) list
wenzelm@5824
    33
    -> theory -> theory
wenzelm@5884
    34
  val syntax: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
wenzelm@5884
    35
    Proof.context -> Args.src -> Proof.context * 'a
wenzelm@5884
    36
  val no_args: Proof.method -> Args.src -> Proof.context -> Proof.method
wenzelm@5884
    37
  val sectioned_args: ((Args.T list -> Proof.context attribute * Args.T list) list ->
wenzelm@5884
    38
      Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
wenzelm@5884
    39
    (Args.T list -> Proof.context attribute * Args.T list) list ->
wenzelm@5884
    40
    ('a -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
wenzelm@5884
    41
  val default_sectioned_args: Proof.context attribute ->
wenzelm@5884
    42
    (Args.T list -> Proof.context attribute * Args.T list) list ->
wenzelm@5884
    43
    (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
wenzelm@5884
    44
  val only_sectioned_args: (Args.T list -> Proof.context attribute * Args.T list) list ->
wenzelm@5884
    45
    (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
wenzelm@6091
    46
  val thms_args: (thm list -> Proof.method) -> Args.src -> Proof.context -> Proof.method
wenzelm@5824
    47
  datatype text =
wenzelm@5824
    48
    Basic of (Proof.context -> Proof.method) |
wenzelm@5824
    49
    Source of Args.src |
wenzelm@5824
    50
    Then of text list |
wenzelm@5824
    51
    Orelse of text list |
wenzelm@5824
    52
    Try of text |
wenzelm@5824
    53
    Repeat of text |
wenzelm@5824
    54
    Repeat1 of text
wenzelm@5824
    55
  val refine: text -> Proof.state -> Proof.state Seq.seq
wenzelm@5824
    56
  val tac: text -> Proof.state -> Proof.state Seq.seq
wenzelm@5884
    57
  val then_tac: text -> Proof.state -> Proof.state Seq.seq
wenzelm@5824
    58
  val proof: text option -> Proof.state -> Proof.state Seq.seq
wenzelm@6981
    59
  val local_qed: text option
wenzelm@6981
    60
    -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
wenzelm@6736
    61
    -> Proof.state -> Proof.state Seq.seq
wenzelm@6981
    62
  val local_terminal_proof: text * text option
wenzelm@6981
    63
    -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
wenzelm@6736
    64
    -> Proof.state -> Proof.state Seq.seq
wenzelm@6981
    65
  val local_immediate_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
wenzelm@6981
    66
    -> Proof.state -> Proof.state Seq.seq
wenzelm@6981
    67
  val local_default_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
wenzelm@6736
    68
    -> Proof.state -> Proof.state Seq.seq
wenzelm@6951
    69
  val global_qed: text option -> Proof.state -> theory * {kind: string, name: string, thm: thm}
wenzelm@6934
    70
  val global_terminal_proof: text * text option
wenzelm@6934
    71
    -> Proof.state -> theory * {kind: string, name: string, thm: thm}
wenzelm@6532
    72
  val global_immediate_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
wenzelm@6532
    73
  val global_default_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
wenzelm@5824
    74
  val setup: (theory -> theory) list
wenzelm@5824
    75
end;
wenzelm@5824
    76
wenzelm@5824
    77
structure Method: METHOD =
wenzelm@5824
    78
struct
wenzelm@5824
    79
wenzelm@5824
    80
wenzelm@6500
    81
(** utils **)
wenzelm@6500
    82
wenzelm@6500
    83
val FINISHED = FILTER (equal 0 o Thm.nprems_of);
wenzelm@6500
    84
wenzelm@6500
    85
wenzelm@6500
    86
wenzelm@5824
    87
(** proof methods **)
wenzelm@5824
    88
wenzelm@5824
    89
(* method from tactic *)
wenzelm@5824
    90
wenzelm@6849
    91
val METHOD = Proof.method;
wenzelm@5824
    92
fun METHOD0 tac = METHOD (fn [] => tac | _ => error "Method may not be used with facts");
wenzelm@5824
    93
wenzelm@5824
    94
wenzelm@5824
    95
(* primitive *)
wenzelm@5824
    96
wenzelm@5824
    97
val fail = METHOD (K no_tac);
wenzelm@5824
    98
val succeed = METHOD (K all_tac);
wenzelm@5824
    99
wenzelm@5824
   100
wenzelm@6981
   101
(* same *)
wenzelm@5824
   102
wenzelm@6981
   103
fun cut_rule_tac raw_rule =
wenzelm@6981
   104
  let
wenzelm@6981
   105
    val rule = Drule.forall_intr_vars raw_rule;
wenzelm@6981
   106
    val revcut_rl = Drule.incr_indexes_wrt [] [] [] [rule] Drule.revcut_rl;
wenzelm@6981
   107
  in Tactic.rtac (rule COMP revcut_rl) THEN' Tactic.rotate_tac ~1 end;
wenzelm@6981
   108
wenzelm@6981
   109
fun same_tac [] i = all_tac
wenzelm@6981
   110
  | same_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
wenzelm@5824
   111
wenzelm@6108
   112
val same = METHOD (ALLGOALS o same_tac);
wenzelm@6981
   113
wenzelm@5824
   114
wenzelm@6981
   115
(* assumption *)
wenzelm@6981
   116
wenzelm@6981
   117
fun assumption_tac facts = same_tac facts THEN' assume_tac;
wenzelm@6981
   118
val assumption = METHOD (FIRSTGOAL o assumption_tac);
wenzelm@6500
   119
val asm_finish = METHOD (K (FINISHED (ALLGOALS assume_tac)));
wenzelm@5824
   120
wenzelm@5824
   121
wenzelm@6532
   122
(* fold / unfold definitions *)
wenzelm@6532
   123
wenzelm@6532
   124
val fold = METHOD0 o fold_goals_tac;
wenzelm@6532
   125
val unfold = METHOD0 o rewrite_goals_tac;
wenzelm@6532
   126
wenzelm@6532
   127
wenzelm@5824
   128
(* rule *)
wenzelm@5824
   129
wenzelm@5824
   130
fun res th i rule = Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty;
wenzelm@5824
   131
wenzelm@5824
   132
fun multi_resolve facts rule =
wenzelm@5824
   133
  let
wenzelm@5824
   134
    fun multi_res i [] = Seq.single rule
wenzelm@5824
   135
      | multi_res i (th :: ths) = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths))
wenzelm@5824
   136
  in multi_res 1 facts end;
wenzelm@5824
   137
wenzelm@5824
   138
fun forward_chain facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules));
wenzelm@5824
   139
wenzelm@6091
   140
fun rule_tac rules [] = resolve_tac rules
wenzelm@5824
   141
  | rule_tac erules facts =
wenzelm@5824
   142
      let
wenzelm@6091
   143
        val rules = forward_chain facts erules;
wenzelm@5824
   144
        fun tac i state = Seq.flat (Seq.map (fn rule => Tactic.rtac rule i state) rules);
wenzelm@5824
   145
      in tac end;
wenzelm@5824
   146
wenzelm@5824
   147
fun rule rules = METHOD (FIRSTGOAL o rule_tac rules);
wenzelm@5824
   148
wenzelm@5824
   149
wenzelm@5824
   150
wenzelm@5824
   151
(** methods theory data **)
wenzelm@5824
   152
wenzelm@5824
   153
(* data kind 'Isar/methods' *)
wenzelm@5824
   154
wenzelm@5824
   155
structure MethodsDataArgs =
wenzelm@5824
   156
struct
wenzelm@5824
   157
  val name = "Isar/methods";
wenzelm@5824
   158
  type T =
wenzelm@5824
   159
    {space: NameSpace.T,
wenzelm@5824
   160
     meths: (((Args.src -> Proof.context -> Proof.method) * string) * stamp) Symtab.table};
wenzelm@5824
   161
wenzelm@5824
   162
  val empty = {space = NameSpace.empty, meths = Symtab.empty};
wenzelm@6546
   163
  val copy = I;
wenzelm@5824
   164
  val prep_ext = I;
wenzelm@5824
   165
  fun merge ({space = space1, meths = meths1}, {space = space2, meths = meths2}) =
wenzelm@5824
   166
    {space = NameSpace.merge (space1, space2),
wenzelm@5824
   167
      meths = Symtab.merge eq_snd (meths1, meths2) handle Symtab.DUPS dups =>
wenzelm@5824
   168
        error ("Attempt to merge different versions of methods " ^ commas_quote dups)};
wenzelm@5824
   169
wenzelm@5824
   170
  fun print _ {space, meths} =
wenzelm@5824
   171
    let
wenzelm@5824
   172
      fun prt_meth (name, ((_, comment), _)) = Pretty.block
wenzelm@6849
   173
        [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@5824
   174
    in
wenzelm@5824
   175
      Pretty.writeln (Display.pretty_name_space ("method name space", space));
wenzelm@6849
   176
      Pretty.writeln (Pretty.big_list "methods:"
wenzelm@6849
   177
        (map prt_meth (NameSpace.cond_extern_table space meths)))
wenzelm@5824
   178
    end;
wenzelm@5824
   179
end;
wenzelm@5824
   180
wenzelm@5824
   181
structure MethodsData = TheoryDataFun(MethodsDataArgs);
wenzelm@5824
   182
val print_methods = MethodsData.print;
wenzelm@5824
   183
wenzelm@5824
   184
wenzelm@5824
   185
(* get methods *)
wenzelm@5824
   186
wenzelm@5916
   187
exception METHOD_FAIL of (string * Position.T) * exn;
wenzelm@5916
   188
wenzelm@5824
   189
fun method thy =
wenzelm@5824
   190
  let
wenzelm@5824
   191
    val {space, meths} = MethodsData.get thy;
wenzelm@5824
   192
wenzelm@5884
   193
    fun meth src =
wenzelm@5884
   194
      let
wenzelm@5884
   195
        val ((raw_name, _), pos) = Args.dest_src src;
wenzelm@5884
   196
        val name = NameSpace.intern space raw_name;
wenzelm@5884
   197
      in
wenzelm@5824
   198
        (case Symtab.lookup (meths, name) of
wenzelm@5824
   199
          None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
wenzelm@5916
   200
        | Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
wenzelm@5824
   201
      end;
wenzelm@5824
   202
  in meth end;
wenzelm@5824
   203
wenzelm@5824
   204
wenzelm@5824
   205
(* add_methods *)
wenzelm@5824
   206
wenzelm@5824
   207
fun add_methods raw_meths thy =
wenzelm@5824
   208
  let
wenzelm@5824
   209
    val full = Sign.full_name (Theory.sign_of thy);
wenzelm@5824
   210
    val new_meths =
wenzelm@5824
   211
      map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths;
wenzelm@5824
   212
wenzelm@5824
   213
    val {space, meths} = MethodsData.get thy;
wenzelm@5824
   214
    val space' = NameSpace.extend (space, map fst new_meths);
wenzelm@5824
   215
    val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups =>
wenzelm@5824
   216
      error ("Duplicate declaration of method(s) " ^ commas_quote dups);
wenzelm@5824
   217
  in
wenzelm@5824
   218
    thy |> MethodsData.put {space = space', meths = meths'}
wenzelm@5824
   219
  end;
wenzelm@5824
   220
wenzelm@5824
   221
(*implicit version*)
wenzelm@5824
   222
fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]);
wenzelm@5824
   223
wenzelm@5824
   224
wenzelm@5884
   225
wenzelm@5884
   226
(** method syntax **)
wenzelm@5824
   227
wenzelm@5884
   228
(* basic *)
wenzelm@5884
   229
wenzelm@5884
   230
fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
wenzelm@5884
   231
  Args.syntax "method" scan;
wenzelm@5824
   232
wenzelm@5884
   233
fun no_args (x: Proof.method) src ctxt = #2 (syntax (Scan.succeed x) ctxt src);
wenzelm@5884
   234
wenzelm@5884
   235
wenzelm@5884
   236
(* sections *)
wenzelm@5824
   237
wenzelm@5884
   238
fun sect ss = Scan.first (map (fn s => Scan.lift (s --| Args.$$$ ":")) ss);
wenzelm@5884
   239
fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
wenzelm@5884
   240
fun thmss ss = Scan.repeat (thms ss) >> flat;
wenzelm@5884
   241
wenzelm@6091
   242
fun apply att (ctxt, ths) = Thm.applys_attributes ((ctxt, ths), [att]);
wenzelm@5824
   243
wenzelm@5884
   244
fun section ss = (sect ss -- thmss ss) :-- (fn (att, ths) => Scan.depend (fn ctxt =>
wenzelm@5884
   245
  Scan.succeed (apply att (ctxt, ths)))) >> #2;
wenzelm@5884
   246
wenzelm@5884
   247
fun sectioned args ss = args ss -- Scan.repeat (section ss);
wenzelm@5884
   248
wenzelm@5824
   249
wenzelm@5884
   250
fun sectioned_args args ss f src ctxt =
wenzelm@5921
   251
  let val (ctxt', (x, _)) = syntax (sectioned args ss) ctxt src
wenzelm@5921
   252
  in f x ctxt' end;
wenzelm@5884
   253
wenzelm@5884
   254
fun default_sectioned_args att ss f src ctxt =
wenzelm@5884
   255
  sectioned_args thmss ss (fn ths => fn ctxt' => f (#1 (apply att (ctxt', ths)))) src ctxt;
wenzelm@5884
   256
wenzelm@5884
   257
fun only_sectioned_args ss f = sectioned_args (K (Scan.succeed ())) ss (fn () => f);
wenzelm@5884
   258
fun thms_args f = sectioned_args thmss [] (fn ths => fn _ => f ths);
wenzelm@5824
   259
wenzelm@5824
   260
wenzelm@5824
   261
wenzelm@5824
   262
(** method text **)
wenzelm@5824
   263
wenzelm@5824
   264
(* datatype text *)
wenzelm@5824
   265
wenzelm@5824
   266
datatype text =
wenzelm@5824
   267
  Basic of (Proof.context -> Proof.method) |
wenzelm@5824
   268
  Source of Args.src |
wenzelm@5824
   269
  Then of text list |
wenzelm@5824
   270
  Orelse of text list |
wenzelm@5824
   271
  Try of text |
wenzelm@5824
   272
  Repeat of text |
wenzelm@5824
   273
  Repeat1 of text;
wenzelm@5824
   274
wenzelm@5824
   275
wenzelm@5824
   276
(* refine *)
wenzelm@5824
   277
wenzelm@5824
   278
fun refine text state =
wenzelm@5824
   279
  let
wenzelm@5824
   280
    val thy = Proof.theory_of state;
wenzelm@5824
   281
wenzelm@5824
   282
    fun eval (Basic mth) = Proof.refine mth
wenzelm@5824
   283
      | eval (Source src) = Proof.refine (method thy src)
wenzelm@5824
   284
      | eval (Then txts) = Seq.EVERY (map eval txts)
wenzelm@5824
   285
      | eval (Orelse txts) = Seq.FIRST (map eval txts)
wenzelm@5824
   286
      | eval (Try txt) = Seq.TRY (eval txt)
wenzelm@5824
   287
      | eval (Repeat txt) = Seq.REPEAT (eval txt)
wenzelm@5824
   288
      | eval (Repeat1 txt) = Seq.REPEAT1 (eval txt);
wenzelm@5824
   289
  in eval text state end;
wenzelm@5824
   290
wenzelm@6404
   291
fun named_method name = Source (Args.src ((name, []), Position.none));
wenzelm@6404
   292
wenzelm@5824
   293
wenzelm@5824
   294
(* unstructured steps *)
wenzelm@5824
   295
wenzelm@5824
   296
fun tac text state =
wenzelm@5824
   297
  state
wenzelm@5824
   298
  |> Proof.goal_facts (K [])
wenzelm@5824
   299
  |> refine text;
wenzelm@5824
   300
wenzelm@5884
   301
fun then_tac text state =
wenzelm@5824
   302
  state
wenzelm@5824
   303
  |> Proof.goal_facts Proof.the_facts
wenzelm@5824
   304
  |> refine text;
wenzelm@5824
   305
wenzelm@5824
   306
wenzelm@6404
   307
(* structured proof steps *)
wenzelm@5824
   308
wenzelm@6404
   309
val default_text = named_method "default";
wenzelm@6404
   310
val finish_text = named_method "finish";
wenzelm@5824
   311
wenzelm@6872
   312
wenzelm@5824
   313
fun proof opt_text state =
wenzelm@5824
   314
  state
wenzelm@5824
   315
  |> Proof.assert_backward
wenzelm@6404
   316
  |> refine (if_none opt_text default_text)
wenzelm@5824
   317
  |> Seq.map Proof.enter_forward;
wenzelm@5824
   318
wenzelm@6404
   319
fun local_qed opt_text = Proof.local_qed (refine (if_none opt_text finish_text));
wenzelm@6934
   320
fun local_terminal_proof (text, opt_text) pr = Seq.THEN (proof (Some text), local_qed opt_text pr);
wenzelm@6934
   321
val local_immediate_proof = local_terminal_proof (Basic (K same), None);
wenzelm@6934
   322
val local_default_proof = local_terminal_proof (default_text, None);
wenzelm@5824
   323
wenzelm@6872
   324
wenzelm@6951
   325
fun global_qeds opt_text = Proof.global_qed (refine (if_none opt_text finish_text));
wenzelm@5824
   326
wenzelm@6951
   327
fun global_qed opt_text state =
wenzelm@6872
   328
  state
wenzelm@6951
   329
  |> global_qeds opt_text
wenzelm@6872
   330
  |> Proof.check_result "Failed to finish proof" state
wenzelm@6872
   331
  |> Seq.hd;
wenzelm@6872
   332
wenzelm@6934
   333
fun global_terminal_proof (text, opt_text) state =
wenzelm@6872
   334
  state
wenzelm@6872
   335
  |> proof (Some text)
wenzelm@6872
   336
  |> Proof.check_result "Terminal proof method failed" state
wenzelm@6951
   337
  |> (Seq.flat o Seq.map (global_qeds opt_text))
wenzelm@6872
   338
  |> Proof.check_result "Failed to finish proof (after successful terminal method)" state
wenzelm@6872
   339
  |> Seq.hd;
wenzelm@6872
   340
wenzelm@6934
   341
val global_immediate_proof = global_terminal_proof (Basic (K same), None);
wenzelm@6934
   342
val global_default_proof = global_terminal_proof (default_text, None);
wenzelm@5824
   343
wenzelm@5824
   344
wenzelm@5824
   345
wenzelm@5824
   346
(** theory setup **)
wenzelm@5824
   347
wenzelm@5824
   348
(* pure_methods *)
wenzelm@5824
   349
wenzelm@5824
   350
val pure_methods =
wenzelm@5824
   351
 [("fail", no_args fail, "force failure"),
wenzelm@5824
   352
  ("succeed", no_args succeed, "succeed"),
wenzelm@6108
   353
  ("same", no_args same, "insert facts, nothing else"),
wenzelm@5824
   354
  ("assumption", no_args assumption, "proof by assumption"),
wenzelm@5824
   355
  ("finish", no_args asm_finish, "finish proof by assumption"),
wenzelm@6532
   356
  ("fold", thms_args fold, "fold definitions"),
wenzelm@6532
   357
  ("unfold", thms_args unfold, "unfold definitions"),
wenzelm@6849
   358
  ("rule", thms_args rule, "apply some rule")];
wenzelm@5824
   359
wenzelm@5824
   360
wenzelm@5824
   361
(* setup *)
wenzelm@5824
   362
wenzelm@5824
   363
val setup = [MethodsData.init, add_methods pure_methods];
wenzelm@5824
   364
wenzelm@5824
   365
wenzelm@5824
   366
end;
wenzelm@5824
   367
wenzelm@5824
   368
wenzelm@5824
   369
structure BasicMethod: BASIC_METHOD = Method;
wenzelm@5824
   370
open BasicMethod;