src/Provers/eqsubst.ML
author dixon
Sat Feb 19 18:44:34 2005 +0100 (2005-02-19)
changeset 15538 d8edf54cc28c
parent 15486 06a32fe35ec3
child 15550 806214035275
permissions -rw-r--r--
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
paulson@15481
     1
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
dixon@15538
     2
(*  Title:      Provers/eqsubst.ML
paulson@15481
     3
    Author:     Lucas Dixon, University of Edinburgh
paulson@15481
     4
                lucas.dixon@ed.ac.uk
dixon@15538
     5
    Modified:   18 Feb 2005 - Lucas - 
paulson@15481
     6
    Created:    29 Jan 2005
paulson@15481
     7
*)
paulson@15481
     8
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
paulson@15481
     9
(*  DESCRIPTION:
paulson@15481
    10
paulson@15481
    11
    A Tactic to perform a substiution using an equation.
paulson@15481
    12
paulson@15481
    13
*)
paulson@15481
    14
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *)
paulson@15481
    15
dixon@15538
    16
(* Logic specific data stub *)
paulson@15481
    17
signature EQRULE_DATA =
paulson@15481
    18
sig
dixon@15538
    19
paulson@15481
    20
  (* to make a meta equality theorem in the current logic *)
paulson@15481
    21
  val prep_meta_eq : thm -> thm list
dixon@15538
    22
paulson@15481
    23
end;
paulson@15481
    24
dixon@15538
    25
paulson@15481
    26
(* the signature of an instance of the SQSUBST tactic *)
paulson@15481
    27
signature EQSUBST_TAC = 
paulson@15481
    28
sig
dixon@15538
    29
dixon@15538
    30
  val prep_subst_in_asm :
dixon@15538
    31
      (Sign.sg (* sign for matching *)
dixon@15538
    32
       -> int (* maxidx *)
dixon@15538
    33
       -> 'a (* input object kind *)
dixon@15538
    34
       -> BasicIsaFTerm.FcTerm (* focusterm to search under *)
dixon@15538
    35
       -> 'b) (* result type *)
dixon@15538
    36
      -> int (* subgoal to subst in *)
dixon@15538
    37
      -> Thm.thm (* target theorem with subgoals *)
dixon@15538
    38
      -> int (* premise to subst in *)
dixon@15538
    39
      -> (Thm.cterm list (* certified free var placeholders for vars *) 
dixon@15538
    40
          * int (* premice no. to subst *)
dixon@15538
    41
          * int (* number of assumptions of premice *)
dixon@15538
    42
          * Thm.thm) (* premice as a new theorem for forward reasoning *)
dixon@15538
    43
         * ('a -> 'b) (* matchf *)
dixon@15538
    44
dixon@15538
    45
  val prep_subst_in_asms :
dixon@15538
    46
      (Sign.sg -> int -> 'a -> BasicIsaFTerm.FcTerm -> 'b) 
dixon@15538
    47
      -> int (* subgoal to subst in *)
dixon@15538
    48
      -> Thm.thm (* target theorem with subgoals *)
dixon@15538
    49
      -> ((Thm.cterm list (* certified free var placeholders for vars *) 
dixon@15538
    50
          * int (* premice no. to subst *)
dixon@15538
    51
          * int (* number of assumptions of premice *)
dixon@15538
    52
          * Thm.thm) (* premice as a new theorem for forward reasoning *)
dixon@15538
    53
         * ('a -> 'b)) (* matchf *)
dixon@15538
    54
                       Seq.seq
dixon@15538
    55
dixon@15538
    56
  val apply_subst_in_asm :
dixon@15538
    57
      int (* subgoal *)
dixon@15538
    58
      -> Thm.thm (* overall theorem *)
dixon@15538
    59
      -> (Thm.cterm list (* certified free var placeholders for vars *) 
dixon@15538
    60
          * int (* assump no being subst *)
dixon@15538
    61
          * int (* num of premises of asm *) 
dixon@15538
    62
          * Thm.thm) (* premthm *)
dixon@15538
    63
      -> Thm.thm (* rule *)
dixon@15538
    64
      -> (((Term.indexname * Term.typ) list (* type instantiations *)
dixon@15538
    65
          * (Term.indexname * Term.term) list) (* term instantiations *)
dixon@15538
    66
         * (string * Term.typ) list (* type abs env *)
dixon@15538
    67
         * Term.term) (* outer term *)
dixon@15538
    68
      -> Thm.thm Seq.seq
dixon@15538
    69
dixon@15538
    70
  val prep_concl_subst :
dixon@15538
    71
      (Sign.sg -> int -> 'a -> BasicIsaFTerm.FcTerm -> 'b) (* searchf *) 
dixon@15538
    72
      -> int (* subgoal *)
dixon@15538
    73
      -> Thm.thm (* overall goal theorem *)
dixon@15538
    74
      -> (Thm.cterm list * Thm.thm) * ('a -> 'b) (* (cvfs, conclthm), matchf *)
dixon@15538
    75
dixon@15538
    76
  val apply_subst_in_concl :
dixon@15538
    77
        int (* subgoal *)
dixon@15538
    78
        -> Thm.thm (* thm with all goals *)
dixon@15538
    79
        -> Thm.cterm list (* certified free var placeholders for vars *)
dixon@15538
    80
           * Thm.thm  (* trivial thm of goal concl *)
dixon@15538
    81
            (* possible matches/unifiers *)
dixon@15538
    82
        -> Thm.thm (* rule *)
dixon@15538
    83
        -> (((Term.indexname * Term.typ) list (* type instantiations *)
dixon@15538
    84
              * (Term.indexname * Term.term) list ) (* term instantiations *)
dixon@15538
    85
             * (string * Term.typ) list (* Type abs env *)
dixon@15538
    86
             * Term.term) (* outer term *)
dixon@15538
    87
        -> Thm.thm Seq.seq (* substituted goal *)
dixon@15538
    88
paulson@15481
    89
  val eqsubst_asm_meth : Thm.thm list -> Proof.method
paulson@15481
    90
  val eqsubst_asm_tac : Thm.thm list -> int -> Thm.thm -> Thm.thm Seq.seq
paulson@15481
    91
  val eqsubst_asm_tac' : Thm.thm -> int -> Thm.thm -> Thm.thm Seq.seq
dixon@15538
    92
paulson@15481
    93
  val eqsubst_meth : Thm.thm list -> Proof.method
paulson@15481
    94
  val eqsubst_tac : Thm.thm list -> int -> Thm.thm -> Thm.thm Seq.seq
paulson@15481
    95
  val eqsubst_tac' : Thm.thm -> int -> Thm.thm -> Thm.thm Seq.seq
dixon@15538
    96
paulson@15481
    97
  val meth : bool * Thm.thm list -> Proof.context -> Proof.method
paulson@15481
    98
  val setup : (Theory.theory -> Theory.theory) list
paulson@15481
    99
end;
paulson@15481
   100
paulson@15481
   101
functor EQSubstTacFUN (structure EqRuleData : EQRULE_DATA) 
dixon@15538
   102
  : EQSUBST_TAC
paulson@15481
   103
= struct
paulson@15481
   104
dixon@15538
   105
(* FOR DEBUGGING...
dixon@15538
   106
type trace_subst_errT = int (* subgoal *)
dixon@15538
   107
        * Thm.thm (* thm with all goals *)
dixon@15538
   108
        * (Thm.cterm list (* certified free var placeholders for vars *)
dixon@15538
   109
           * Thm.thm)  (* trivial thm of goal concl *)
dixon@15538
   110
            (* possible matches/unifiers *)
dixon@15538
   111
        * Thm.thm (* rule *)
dixon@15538
   112
        * (((Term.indexname * Term.typ) list (* type instantiations *)
dixon@15538
   113
              * (Term.indexname * Term.term) list ) (* term instantiations *)
dixon@15538
   114
             * (string * Term.typ) list (* Type abs env *)
dixon@15538
   115
             * Term.term) (* outer term *);
dixon@15538
   116
dixon@15538
   117
val trace_subst_err = (ref NONE : trace_subst_errT option ref);
dixon@15538
   118
val trace_subst_search = ref false;
dixon@15538
   119
exception trace_subst_exp of trace_subst_errT;
dixon@15538
   120
 *)
dixon@15538
   121
dixon@15538
   122
(* also defined in /HOL/Tools/inductive_codegen.ML, 
dixon@15538
   123
   maybe move this to seq.ML ? *)
dixon@15538
   124
infix 5 :->;
dixon@15538
   125
fun s :-> f = Seq.flat (Seq.map f s);
dixon@15538
   126
dixon@15538
   127
(* search from the top to bottom, left to right *)
dixon@15538
   128
fun search_lr_f f ft = 
paulson@15481
   129
    let
paulson@15481
   130
      fun maux ft = 
paulson@15481
   131
          let val t' = (IsaFTerm.focus_of_fcterm ft) 
dixon@15538
   132
            (* val _ = 
dixon@15538
   133
                if !trace_subst_search then 
dixon@15538
   134
                  (writeln ("Examining: " ^ (TermLib.string_of_term t'));
dixon@15538
   135
                   TermLib.writeterm t'; ())
dixon@15538
   136
                else (); *)
paulson@15481
   137
          in 
paulson@15481
   138
          (case t' of 
dixon@15538
   139
            (_ $ _) => Seq.append(maux (IsaFTerm.focus_left ft), 
dixon@15538
   140
                       Seq.append(f ft, 
paulson@15481
   141
                                  maux (IsaFTerm.focus_right ft)))
paulson@15481
   142
          | (Abs _) => Seq.append (f ft, maux (IsaFTerm.focus_abs ft))
paulson@15481
   143
          | leaf => f ft) end
paulson@15481
   144
    in maux ft end;
paulson@15481
   145
dixon@15538
   146
fun search_for_match sgn maxidx lhs  = 
paulson@15481
   147
    IsaFTerm.find_fcterm_matches 
dixon@15538
   148
      search_lr_f 
paulson@15481
   149
      (IsaFTerm.clean_unify_ft sgn maxidx lhs);
paulson@15481
   150
dixon@15538
   151
(* apply a substitution in the conclusion of the theorem th *)
dixon@15538
   152
(* cfvs are certified free var placeholders for goal params *)
dixon@15538
   153
(* conclthm is a theorem of for just the conclusion *)
dixon@15538
   154
(* m is instantiation/match information *)
dixon@15538
   155
(* rule is the equation for substitution *)
dixon@15538
   156
fun apply_subst_in_concl i th (cfvs, conclthm) rule m = 
dixon@15538
   157
    (RWInst.rw m rule conclthm)
dixon@15538
   158
      |> IsaND.schemify_frees_to_vars cfvs
dixon@15538
   159
      |> RWInst.beta_eta_contract_tac
dixon@15538
   160
      |> (fn r => Tactic.rtac r i th);
paulson@15481
   161
dixon@15538
   162
(*
dixon@15538
   163
? is the following equivalent to rtac ? 
paulson@15481
   164
dixon@15538
   165
 |> Thm.lift_rule (th, i)
dixon@15538
   166
 |> (fn r => Thm.bicompose false (false, r, Thm.nprems_of r) i th)
dixon@15538
   167
dixon@15538
   168
*)
paulson@15481
   169
paulson@15481
   170
(* substitute within the conclusion of goal i of gth, using a meta
dixon@15538
   171
equation rule. Note that we assume rule has var indicies zero'd *)
dixon@15538
   172
fun prep_concl_subst searchf i gth = 
paulson@15481
   173
    let 
paulson@15481
   174
      val th = Thm.incr_indexes 1 gth;
paulson@15481
   175
      val tgt_term = Thm.prop_of th;
paulson@15481
   176
paulson@15481
   177
      val sgn = Thm.sign_of_thm th;
paulson@15481
   178
      val ctermify = Thm.cterm_of sgn;
paulson@15481
   179
      val trivify = Thm.trivial o ctermify;
paulson@15481
   180
paulson@15481
   181
      val (fixedbody, fvs) = IsaND.fix_alls_term i tgt_term;
paulson@15481
   182
      val cfvs = rev (map ctermify fvs);
paulson@15481
   183
dixon@15538
   184
      val conclterm = Logic.strip_imp_concl fixedbody;
dixon@15538
   185
      val conclthm = trivify conclterm;
dixon@15538
   186
      val maxidx = Term.maxidx_of_term conclterm;
paulson@15481
   187
    in
dixon@15538
   188
      ((cfvs, conclthm), 
dixon@15538
   189
       (fn lhs => searchf sgn maxidx lhs 
dixon@15538
   190
                          ((IsaFTerm.focus_right  
dixon@15538
   191
                            o IsaFTerm.focus_left
dixon@15538
   192
                            o IsaFTerm.fcterm_of_term 
dixon@15538
   193
                            o Thm.prop_of) conclthm)))
paulson@15481
   194
    end;
paulson@15481
   195
dixon@15538
   196
paulson@15481
   197
(* substitute using an object or meta level equality *)
paulson@15481
   198
fun eqsubst_tac' instepthm i th = 
dixon@15538
   199
    let 
dixon@15538
   200
      val (cvfsconclthm, findmatchf) = 
dixon@15538
   201
          prep_concl_subst search_for_match i th;
dixon@15538
   202
dixon@15538
   203
      val stepthms = 
dixon@15538
   204
          Seq.map Drule.zero_var_indexes 
dixon@15538
   205
                  (Seq.of_list (EqRuleData.prep_meta_eq instepthm));
dixon@15538
   206
dixon@15538
   207
      fun rewrite_with_thm r =
dixon@15538
   208
          let val (lhs,_) = Logic.dest_equals (Thm.concl_of r);
dixon@15538
   209
          in (findmatchf lhs)
dixon@15538
   210
             :-> (apply_subst_in_concl i th cvfsconclthm r) end;
dixon@15538
   211
dixon@15538
   212
    in (stepthms :-> rewrite_with_thm) end;
dixon@15538
   213
dixon@15538
   214
paulson@15481
   215
(* substitute using one of the given theorems *)
paulson@15481
   216
fun eqsubst_tac instepthms i th = 
dixon@15538
   217
    if Thm.nprems_of th < i then Seq.empty else
dixon@15538
   218
    (Seq.of_list instepthms) :-> (fn r => eqsubst_tac' r i th);
paulson@15481
   219
paulson@15481
   220
(* inthms are the given arguments in Isar, and treated as eqstep with
paulson@15481
   221
   the first one, then the second etc *)
paulson@15481
   222
fun eqsubst_meth inthms =
paulson@15481
   223
    Method.METHOD 
dixon@15538
   224
      (fn facts =>
dixon@15538
   225
          HEADGOAL ( Method.insert_tac facts THEN' eqsubst_tac inthms ));
paulson@15481
   226
paulson@15481
   227
dixon@15538
   228
fun apply_subst_in_asm i th (cfvs, j, nprems, pth) rule m = 
dixon@15538
   229
    (RWInst.rw m rule pth)
dixon@15538
   230
      |> Thm.permute_prems 0 ~1
dixon@15538
   231
      |> IsaND.schemify_frees_to_vars cfvs
dixon@15538
   232
      |> RWInst.beta_eta_contract_tac
dixon@15538
   233
      |> (fn r => Tactic.dtac r i th);
dixon@15538
   234
dixon@15538
   235
(*
dixon@15538
   236
? should I be using bicompose what if we match more than one
dixon@15538
   237
assumption, even after instantiation ? (back will work, but it would
dixon@15538
   238
be nice to avoid the redudent search)
dixon@15538
   239
dixon@15538
   240
something like... 
dixon@15538
   241
 |> Thm.lift_rule (th, i)
dixon@15538
   242
 |> (fn r => Thm.bicompose false (false, r, Thm.nprems_of r - nprems) i th)
dixon@15538
   243
dixon@15538
   244
*)
paulson@15481
   245
paulson@15481
   246
dixon@15538
   247
(* prepare to substitute within the j'th premise of subgoal i of gth,
dixon@15538
   248
using a meta-level equation. Note that we assume rule has var indicies
dixon@15538
   249
zero'd. Note that we also assume that premt is the j'th premice of
dixon@15538
   250
subgoal i of gth. Note the repetition of work done for each
dixon@15538
   251
assumption, i.e. this can be made more efficient for search over
dixon@15538
   252
multiple assumptions.  *)
dixon@15538
   253
fun prep_subst_in_asm searchf i gth j = 
paulson@15481
   254
    let 
paulson@15481
   255
      val th = Thm.incr_indexes 1 gth;
paulson@15481
   256
      val tgt_term = Thm.prop_of th;
paulson@15481
   257
paulson@15481
   258
      val sgn = Thm.sign_of_thm th;
paulson@15481
   259
      val ctermify = Thm.cterm_of sgn;
paulson@15481
   260
      val trivify = Thm.trivial o ctermify;
paulson@15481
   261
paulson@15481
   262
      val (fixedbody, fvs) = IsaND.fix_alls_term i tgt_term;
paulson@15481
   263
      val cfvs = rev (map ctermify fvs);
paulson@15481
   264
dixon@15538
   265
      val asmt = Library.nth_elem(j - 1,(Logic.strip_imp_prems fixedbody));
dixon@15538
   266
      val asm_nprems = length (Logic.strip_imp_prems asmt);
dixon@15538
   267
dixon@15538
   268
      val pth = trivify asmt;
dixon@15538
   269
      val maxidx = Term.maxidx_of_term asmt;
dixon@15538
   270
paulson@15481
   271
    in
dixon@15538
   272
      ((cfvs, j, asm_nprems, pth), 
dixon@15538
   273
       (fn lhs => (searchf sgn maxidx lhs
dixon@15538
   274
                           ((IsaFTerm.focus_right 
dixon@15538
   275
                             o IsaFTerm.fcterm_of_term 
dixon@15538
   276
                             o Thm.prop_of) pth))))
paulson@15481
   277
    end;
paulson@15481
   278
dixon@15538
   279
(* prepare subst in every possible assumption *)
dixon@15538
   280
fun prep_subst_in_asms searchf i gth = 
dixon@15538
   281
    Seq.map 
dixon@15538
   282
      (prep_subst_in_asm searchf i gth)
dixon@15538
   283
      (Seq.of_list (IsaPLib.mk_num_list
dixon@15538
   284
                      (length (Logic.prems_of_goal (Thm.prop_of gth) i))));
dixon@15538
   285
dixon@15538
   286
dixon@15538
   287
(* substitute in an assumption using an object or meta level equality *)
paulson@15481
   288
fun eqsubst_asm_tac' instepthm i th = 
dixon@15538
   289
    let 
dixon@15538
   290
      val asmpreps = prep_subst_in_asms search_for_match i th;
dixon@15538
   291
      val stepthms = 
dixon@15538
   292
          Seq.map Drule.zero_var_indexes 
dixon@15538
   293
                  (Seq.of_list (EqRuleData.prep_meta_eq instepthm))
dixon@15538
   294
dixon@15538
   295
      fun rewrite_with_thm (asminfo, findmatchf) r =
dixon@15538
   296
          let val (lhs,_) = Logic.dest_equals (Thm.concl_of r);
dixon@15538
   297
          in (findmatchf lhs)
dixon@15538
   298
             :-> (apply_subst_in_asm i th asminfo r) end;
dixon@15538
   299
    in
dixon@15538
   300
      (asmpreps :-> (fn a => stepthms :-> rewrite_with_thm a))
paulson@15481
   301
    end;
paulson@15481
   302
paulson@15481
   303
(* substitute using one of the given theorems *)
paulson@15481
   304
fun eqsubst_asm_tac instepthms i th = 
dixon@15538
   305
    if Thm.nprems_of th < i then Seq.empty else
dixon@15538
   306
    (Seq.of_list instepthms) :-> (fn r => eqsubst_asm_tac' r i th);
paulson@15481
   307
paulson@15481
   308
(* inthms are the given arguments in Isar, and treated as eqstep with
paulson@15481
   309
   the first one, then the second etc *)
paulson@15481
   310
fun eqsubst_asm_meth inthms =
paulson@15481
   311
    Method.METHOD 
dixon@15538
   312
      (fn facts =>
dixon@15538
   313
          HEADGOAL (Method.insert_tac facts THEN' eqsubst_asm_tac inthms ));
paulson@15481
   314
paulson@15481
   315
(* combination method that takes a flag (true indicates that subst
paulson@15481
   316
should be done to an assumption, false = apply to the conclusion of
paulson@15481
   317
the goal) as well as the theorems to use *)
paulson@15481
   318
fun meth (asmflag, inthms) ctxt = 
paulson@15481
   319
    if asmflag then eqsubst_asm_meth inthms else eqsubst_meth inthms;
paulson@15481
   320
paulson@15481
   321
(* syntax for options, given "(asm)" will give back true, without
paulson@15481
   322
   gives back false *)
paulson@15481
   323
val options_syntax =
paulson@15481
   324
    (Args.parens (Args.$$$ "asm") >> (K true)) ||
paulson@15481
   325
     (Scan.succeed false);
paulson@15481
   326
paulson@15481
   327
(* method syntax, first take options, then theorems *)
paulson@15481
   328
fun meth_syntax meth src ctxt =
paulson@15481
   329
    meth (snd (Method.syntax ((Scan.lift options_syntax) 
paulson@15481
   330
                                -- Attrib.local_thms) src ctxt)) 
paulson@15481
   331
         ctxt;
paulson@15481
   332
paulson@15481
   333
(* setup function for adding method to theory. *)
paulson@15481
   334
val setup = 
paulson@15481
   335
    [Method.add_method ("subst", meth_syntax meth, "Substiution with an equation. Use \"(asm)\" option to substitute in an assumption.")];
paulson@15481
   336
paulson@15481
   337
end;