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