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