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