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