src/Provers/eqsubst.ML
author dixon
Fri May 13 20:21:41 2005 +0200 (2005-05-13)
changeset 15959 366d39e95d3c
parent 15936 817ac93ee786
child 16004 031f56012483
permissions -rw-r--r--
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
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@15959
    30
  exception eqsubst_occL_exp of 
dixon@15959
    31
            string * (int list) * (Thm.thm list) * int * Thm.thm;
dixon@15959
    32
dixon@15550
    33
  type match = 
dixon@15915
    34
       ((Term.indexname * (Term.sort * Term.typ)) list (* type instantiations *)
dixon@15915
    35
        * (Term.indexname * (Term.typ * Term.term)) list) (* term instantiations *)
dixon@15814
    36
       * (string * Term.typ) list (* fake named type abs env *)
dixon@15814
    37
       * (string * Term.typ) list (* type abs env *)
dixon@15814
    38
       * Term.term (* outer term *)
dixon@15550
    39
dixon@15538
    40
  val prep_subst_in_asm :
dixon@15538
    41
      (Sign.sg (* sign for matching *)
dixon@15538
    42
       -> int (* maxidx *)
dixon@15538
    43
       -> 'a (* input object kind *)
dixon@15538
    44
       -> BasicIsaFTerm.FcTerm (* focusterm to search under *)
dixon@15538
    45
       -> 'b) (* result type *)
dixon@15538
    46
      -> int (* subgoal to subst in *)
dixon@15538
    47
      -> Thm.thm (* target theorem with subgoals *)
dixon@15538
    48
      -> int (* premise to subst in *)
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
dixon@15538
    55
  val prep_subst_in_asms :
dixon@15538
    56
      (Sign.sg -> int -> 'a -> BasicIsaFTerm.FcTerm -> 'b) 
dixon@15538
    57
      -> int (* subgoal to subst in *)
dixon@15538
    58
      -> Thm.thm (* target theorem with subgoals *)
dixon@15538
    59
      -> ((Thm.cterm list (* certified free var placeholders for vars *) 
dixon@15538
    60
          * int (* premice no. to subst *)
dixon@15538
    61
          * int (* number of assumptions of premice *)
dixon@15538
    62
          * Thm.thm) (* premice as a new theorem for forward reasoning *)
dixon@15538
    63
         * ('a -> 'b)) (* matchf *)
dixon@15538
    64
                       Seq.seq
dixon@15538
    65
dixon@15538
    66
  val apply_subst_in_asm :
dixon@15538
    67
      int (* subgoal *)
dixon@15538
    68
      -> Thm.thm (* overall theorem *)
dixon@15538
    69
      -> (Thm.cterm list (* certified free var placeholders for vars *) 
dixon@15538
    70
          * int (* assump no being subst *)
dixon@15538
    71
          * int (* num of premises of asm *) 
dixon@15538
    72
          * Thm.thm) (* premthm *)
dixon@15538
    73
      -> Thm.thm (* rule *)
dixon@15550
    74
      -> match
dixon@15538
    75
      -> Thm.thm Seq.seq
dixon@15538
    76
dixon@15538
    77
  val prep_concl_subst :
dixon@15538
    78
      (Sign.sg -> int -> 'a -> BasicIsaFTerm.FcTerm -> 'b) (* searchf *) 
dixon@15538
    79
      -> int (* subgoal *)
dixon@15538
    80
      -> Thm.thm (* overall goal theorem *)
dixon@15538
    81
      -> (Thm.cterm list * Thm.thm) * ('a -> 'b) (* (cvfs, conclthm), matchf *)
dixon@15538
    82
dixon@15538
    83
  val apply_subst_in_concl :
dixon@15538
    84
        int (* subgoal *)
dixon@15538
    85
        -> Thm.thm (* thm with all goals *)
dixon@15538
    86
        -> Thm.cterm list (* certified free var placeholders for vars *)
dixon@15538
    87
           * Thm.thm  (* trivial thm of goal concl *)
dixon@15538
    88
            (* possible matches/unifiers *)
dixon@15538
    89
        -> Thm.thm (* rule *)
dixon@15550
    90
        -> match
dixon@15538
    91
        -> Thm.thm Seq.seq (* substituted goal *)
dixon@15538
    92
dixon@15814
    93
  val searchf_tlr_unify_all : 
dixon@15814
    94
      (Sign.sg -> int ->
dixon@15814
    95
       Term.term ->
dixon@15814
    96
       BasicIsaFTerm.FcTerm ->
dixon@15929
    97
       match Seq.seq Seq.seq)
dixon@15814
    98
  val searchf_tlr_unify_valid : 
dixon@15814
    99
      (Sign.sg -> int ->
dixon@15814
   100
       Term.term ->
dixon@15814
   101
       BasicIsaFTerm.FcTerm ->
dixon@15929
   102
       match Seq.seq Seq.seq)
dixon@15814
   103
dixon@15936
   104
  val eqsubst_asm_meth : int list -> Thm.thm list -> Proof.method
dixon@15936
   105
  val eqsubst_asm_tac : int list -> Thm.thm list -> int -> Thm.thm -> Thm.thm Seq.seq
dixon@15814
   106
  val eqsubst_asm_tac' : 
dixon@15814
   107
      (Sign.sg -> int ->
dixon@15814
   108
       Term.term ->
dixon@15814
   109
       BasicIsaFTerm.FcTerm ->
dixon@15814
   110
       match Seq.seq) -> Thm.thm -> int -> Thm.thm -> Thm.thm Seq.seq
dixon@15538
   111
dixon@15936
   112
  val eqsubst_meth : int list -> Thm.thm list -> Proof.method
dixon@15936
   113
  val eqsubst_tac : int list -> Thm.thm list -> int -> Thm.thm -> Thm.thm Seq.seq
dixon@15814
   114
  val eqsubst_tac' : 
dixon@15814
   115
      (Sign.sg -> int ->
dixon@15814
   116
       Term.term ->
dixon@15814
   117
       BasicIsaFTerm.FcTerm ->
dixon@15814
   118
       match Seq.seq) -> Thm.thm -> int -> Thm.thm -> Thm.thm Seq.seq
dixon@15538
   119
dixon@15936
   120
  val meth : (bool * int list) * Thm.thm list -> Proof.context -> Proof.method
paulson@15481
   121
  val setup : (Theory.theory -> Theory.theory) list
paulson@15481
   122
end;
paulson@15481
   123
paulson@15481
   124
functor EQSubstTacFUN (structure EqRuleData : EQRULE_DATA) 
dixon@15538
   125
  : EQSUBST_TAC
paulson@15481
   126
= struct
paulson@15481
   127
dixon@15915
   128
  (* a type abriviation for match information *)
dixon@15550
   129
  type match = 
dixon@15915
   130
       ((Term.indexname * (Term.sort * Term.typ)) list (* type instantiations *)
dixon@15915
   131
        * (Term.indexname * (Term.typ * Term.term)) list) (* term instantiations *)
dixon@15915
   132
       * (string * Term.typ) list (* fake named type abs env *)
dixon@15915
   133
       * (string * Term.typ) list (* type abs env *)
dixon@15915
   134
       * Term.term (* outer term *)
dixon@15550
   135
dixon@15550
   136
dixon@15538
   137
(* FOR DEBUGGING...
dixon@15538
   138
type trace_subst_errT = int (* subgoal *)
dixon@15538
   139
        * Thm.thm (* thm with all goals *)
dixon@15538
   140
        * (Thm.cterm list (* certified free var placeholders for vars *)
dixon@15538
   141
           * Thm.thm)  (* trivial thm of goal concl *)
dixon@15538
   142
            (* possible matches/unifiers *)
dixon@15538
   143
        * Thm.thm (* rule *)
dixon@15538
   144
        * (((Term.indexname * Term.typ) list (* type instantiations *)
dixon@15538
   145
              * (Term.indexname * Term.term) list ) (* term instantiations *)
dixon@15538
   146
             * (string * Term.typ) list (* Type abs env *)
dixon@15538
   147
             * Term.term) (* outer term *);
dixon@15538
   148
dixon@15538
   149
val trace_subst_err = (ref NONE : trace_subst_errT option ref);
dixon@15538
   150
val trace_subst_search = ref false;
dixon@15538
   151
exception trace_subst_exp of trace_subst_errT;
dixon@15538
   152
 *)
dixon@15538
   153
dixon@15538
   154
(* also defined in /HOL/Tools/inductive_codegen.ML, 
dixon@15538
   155
   maybe move this to seq.ML ? *)
dixon@15538
   156
infix 5 :->;
dixon@15538
   157
fun s :-> f = Seq.flat (Seq.map f s);
dixon@15538
   158
dixon@15814
   159
(* search from top, left to right, then down *)
dixon@15814
   160
fun search_tlr_all_f f ft = 
paulson@15481
   161
    let
paulson@15481
   162
      fun maux ft = 
paulson@15481
   163
          let val t' = (IsaFTerm.focus_of_fcterm ft) 
dixon@15538
   164
            (* val _ = 
dixon@15538
   165
                if !trace_subst_search then 
dixon@15538
   166
                  (writeln ("Examining: " ^ (TermLib.string_of_term t'));
dixon@15538
   167
                   TermLib.writeterm t'; ())
dixon@15538
   168
                else (); *)
paulson@15481
   169
          in 
paulson@15481
   170
          (case t' of 
dixon@15538
   171
            (_ $ _) => Seq.append(maux (IsaFTerm.focus_left ft), 
dixon@15929
   172
                       Seq.cons(f ft, 
paulson@15481
   173
                                  maux (IsaFTerm.focus_right ft)))
dixon@15929
   174
          | (Abs _) => Seq.cons(f ft, maux (IsaFTerm.focus_abs ft))
dixon@15929
   175
          | leaf => Seq.single (f ft)) end
paulson@15481
   176
    in maux ft end;
paulson@15481
   177
dixon@15814
   178
(* search from top, left to right, then down *)
dixon@15814
   179
fun search_tlr_valid_f f ft = 
dixon@15814
   180
    let
dixon@15814
   181
      fun maux ft = 
dixon@15814
   182
          let 
dixon@15814
   183
            val hereseq = if IsaFTerm.valid_match_start ft then f ft else Seq.empty
dixon@15814
   184
          in 
dixon@15814
   185
          (case (IsaFTerm.focus_of_fcterm ft) of 
dixon@15814
   186
            (_ $ _) => Seq.append(maux (IsaFTerm.focus_left ft), 
dixon@15929
   187
                       Seq.cons(hereseq, 
dixon@15814
   188
                                  maux (IsaFTerm.focus_right ft)))
dixon@15929
   189
          | (Abs _) => Seq.cons(hereseq, maux (IsaFTerm.focus_abs ft))
dixon@15929
   190
          | leaf => Seq.single (hereseq))
dixon@15814
   191
          end
dixon@15814
   192
    in maux ft end;
dixon@15814
   193
dixon@15814
   194
(* search all unifications *)
dixon@15814
   195
fun searchf_tlr_unify_all sgn maxidx lhs  = 
paulson@15481
   196
    IsaFTerm.find_fcterm_matches 
dixon@15814
   197
      search_tlr_all_f 
paulson@15481
   198
      (IsaFTerm.clean_unify_ft sgn maxidx lhs);
paulson@15481
   199
dixon@15814
   200
(* search only for 'valid' unifiers (non abs subterms and non vars) *)
dixon@15814
   201
fun searchf_tlr_unify_valid sgn maxidx lhs  = 
dixon@15814
   202
    IsaFTerm.find_fcterm_matches 
dixon@15814
   203
      search_tlr_valid_f 
dixon@15814
   204
      (IsaFTerm.clean_unify_ft sgn maxidx lhs);
dixon@15814
   205
dixon@15929
   206
(* special tactic to skip the first "occ" occurances - ie start at the nth match *)
dixon@15929
   207
fun skip_first_occs_search occ searchf sgn i t ft = 
dixon@15929
   208
    let 
dixon@15929
   209
      fun skip_occs n sq = 
dixon@15929
   210
          if n <= 1 then sq 
dixon@15929
   211
          else
dixon@15929
   212
          (case (Seq.pull sq) of NONE => Seq.empty
dixon@15929
   213
           | SOME (h,t) => 
dixon@15929
   214
             (case Seq.pull h of NONE => skip_occs n t
dixon@15929
   215
              | SOME _ => skip_occs (n - 1) t))
dixon@15929
   216
    in Seq.flat (skip_occs occ (searchf sgn i t ft)) end;
dixon@15929
   217
dixon@15814
   218
dixon@15538
   219
(* apply a substitution in the conclusion of the theorem th *)
dixon@15538
   220
(* cfvs are certified free var placeholders for goal params *)
dixon@15538
   221
(* conclthm is a theorem of for just the conclusion *)
dixon@15538
   222
(* m is instantiation/match information *)
dixon@15538
   223
(* rule is the equation for substitution *)
dixon@15538
   224
fun apply_subst_in_concl i th (cfvs, conclthm) rule m = 
dixon@15538
   225
    (RWInst.rw m rule conclthm)
dixon@15855
   226
      |> IsaND.unfix_frees cfvs
dixon@15915
   227
      |> RWInst.beta_eta_contract
dixon@15538
   228
      |> (fn r => Tactic.rtac r i th);
paulson@15481
   229
dixon@15538
   230
(*
paulson@15481
   231
dixon@15538
   232
 |> (fn r => Thm.bicompose false (false, r, Thm.nprems_of r) i th)
dixon@15538
   233
dixon@15538
   234
*)
paulson@15481
   235
paulson@15481
   236
(* substitute within the conclusion of goal i of gth, using a meta
dixon@15538
   237
equation rule. Note that we assume rule has var indicies zero'd *)
dixon@15538
   238
fun prep_concl_subst searchf i gth = 
paulson@15481
   239
    let 
paulson@15481
   240
      val th = Thm.incr_indexes 1 gth;
paulson@15481
   241
      val tgt_term = Thm.prop_of th;
paulson@15481
   242
paulson@15481
   243
      val sgn = Thm.sign_of_thm th;
paulson@15481
   244
      val ctermify = Thm.cterm_of sgn;
paulson@15481
   245
      val trivify = Thm.trivial o ctermify;
paulson@15481
   246
paulson@15481
   247
      val (fixedbody, fvs) = IsaND.fix_alls_term i tgt_term;
paulson@15481
   248
      val cfvs = rev (map ctermify fvs);
paulson@15481
   249
dixon@15538
   250
      val conclterm = Logic.strip_imp_concl fixedbody;
dixon@15538
   251
      val conclthm = trivify conclterm;
dixon@15538
   252
      val maxidx = Term.maxidx_of_term conclterm;
paulson@15481
   253
    in
dixon@15538
   254
      ((cfvs, conclthm), 
dixon@15538
   255
       (fn lhs => searchf sgn maxidx lhs 
dixon@15538
   256
                          ((IsaFTerm.focus_right  
dixon@15538
   257
                            o IsaFTerm.focus_left
dixon@15538
   258
                            o IsaFTerm.fcterm_of_term 
dixon@15538
   259
                            o Thm.prop_of) conclthm)))
paulson@15481
   260
    end;
paulson@15481
   261
paulson@15481
   262
(* substitute using an object or meta level equality *)
dixon@15814
   263
fun eqsubst_tac' searchf instepthm i th = 
dixon@15538
   264
    let 
dixon@15538
   265
      val (cvfsconclthm, findmatchf) = 
dixon@15814
   266
          prep_concl_subst searchf i th;
dixon@15538
   267
dixon@15538
   268
      val stepthms = 
dixon@15538
   269
          Seq.map Drule.zero_var_indexes 
dixon@15538
   270
                  (Seq.of_list (EqRuleData.prep_meta_eq instepthm));
dixon@15538
   271
dixon@15538
   272
      fun rewrite_with_thm r =
dixon@15538
   273
          let val (lhs,_) = Logic.dest_equals (Thm.concl_of r);
dixon@15538
   274
          in (findmatchf lhs)
dixon@15538
   275
             :-> (apply_subst_in_concl i th cvfsconclthm r) end;
dixon@15538
   276
dixon@15538
   277
    in (stepthms :-> rewrite_with_thm) end;
dixon@15538
   278
dixon@15959
   279
(* Tactic.distinct_subgoals_tac *)
dixon@15959
   280
dixon@15959
   281
(* custom version of distinct subgoals that works with term and 
dixon@15959
   282
   type variables. Asssumes th is in beta-eta normal form. 
dixon@15959
   283
   Also, does nothing if flexflex contraints are present. *)
dixon@15959
   284
fun distinct_subgoals th =
dixon@15959
   285
    if List.null (Thm.tpairs_of th) then
dixon@15959
   286
      let val (fixes,fixedthm) = IsaND.fix_vars_and_tvars th
dixon@15959
   287
        val (fixedthconcl,cprems) = IsaND.hide_prems fixedthm
dixon@15959
   288
      in
dixon@15959
   289
        IsaND.unfix_frees_and_tfrees 
dixon@15959
   290
          fixes
dixon@15959
   291
          (Drule.implies_intr_list 
dixon@15959
   292
             (Library.gen_distinct 
dixon@15959
   293
                (fn (x, y) => Thm.term_of x = Thm.term_of y)
dixon@15959
   294
                cprems) fixedthconcl)
dixon@15959
   295
      end
dixon@15959
   296
    else th;
dixon@15538
   297
dixon@15936
   298
(* General substiuttion of multiple occurances using one of 
dixon@15936
   299
   the given theorems*)
dixon@15959
   300
exception eqsubst_occL_exp of 
dixon@15959
   301
          string * (int list) * (Thm.thm list) * int * Thm.thm;
dixon@15936
   302
fun eqsubst_occL tac occL thms i th = 
dixon@15936
   303
    let val nprems = Thm.nprems_of th in
dixon@15936
   304
      if nprems < i then Seq.empty else
dixon@15936
   305
      let val thmseq = (Seq.of_list thms) 
dixon@15936
   306
        fun apply_occ occ th = 
dixon@15936
   307
            thmseq :-> 
dixon@15936
   308
                    (fn r => tac (skip_first_occs_search 
dixon@15936
   309
                                    occ searchf_tlr_unify_valid) r
dixon@15936
   310
                                 (i + ((Thm.nprems_of th) - nprems))
dixon@15936
   311
                                 th);
dixon@15936
   312
      in
dixon@15959
   313
        Seq.map distinct_subgoals
dixon@15959
   314
                (Seq.EVERY (map apply_occ 
dixon@15959
   315
                                (Library.sort (Library.rev_order 
dixon@15959
   316
                                               o Library.int_ord) occL)) th)
dixon@15936
   317
      end
dixon@15959
   318
    end
dixon@15959
   319
    handle THM _ => raise eqsubst_occL_exp ("THM",occL,thms,i,th);
dixon@15959
   320
dixon@15959
   321
        
dixon@15936
   322
dixon@15936
   323
(* implicit argus: occL thms i th *)
dixon@15936
   324
val eqsubst_tac = eqsubst_occL eqsubst_tac';
dixon@15936
   325
paulson@15481
   326
paulson@15481
   327
(* inthms are the given arguments in Isar, and treated as eqstep with
paulson@15481
   328
   the first one, then the second etc *)
dixon@15936
   329
fun eqsubst_meth occL inthms =
paulson@15481
   330
    Method.METHOD 
dixon@15538
   331
      (fn facts =>
dixon@15936
   332
          HEADGOAL ( Method.insert_tac facts THEN' eqsubst_tac occL inthms ));
paulson@15481
   333
paulson@15481
   334
dixon@15538
   335
fun apply_subst_in_asm i th (cfvs, j, nprems, pth) rule m = 
dixon@15538
   336
    (RWInst.rw m rule pth)
dixon@15538
   337
      |> Thm.permute_prems 0 ~1
dixon@15855
   338
      |> IsaND.unfix_frees cfvs
dixon@15915
   339
      |> RWInst.beta_eta_contract
dixon@15538
   340
      |> (fn r => Tactic.dtac r i th);
dixon@15538
   341
dixon@15538
   342
(*
dixon@15538
   343
? should I be using bicompose what if we match more than one
dixon@15538
   344
assumption, even after instantiation ? (back will work, but it would
dixon@15538
   345
be nice to avoid the redudent search)
dixon@15538
   346
dixon@15538
   347
something like... 
dixon@15538
   348
 |> Thm.lift_rule (th, i)
dixon@15538
   349
 |> (fn r => Thm.bicompose false (false, r, Thm.nprems_of r - nprems) i th)
dixon@15538
   350
dixon@15538
   351
*)
paulson@15481
   352
paulson@15481
   353
dixon@15538
   354
(* prepare to substitute within the j'th premise of subgoal i of gth,
dixon@15538
   355
using a meta-level equation. Note that we assume rule has var indicies
dixon@15538
   356
zero'd. Note that we also assume that premt is the j'th premice of
dixon@15538
   357
subgoal i of gth. Note the repetition of work done for each
dixon@15538
   358
assumption, i.e. this can be made more efficient for search over
dixon@15538
   359
multiple assumptions.  *)
dixon@15538
   360
fun prep_subst_in_asm searchf i gth j = 
paulson@15481
   361
    let 
paulson@15481
   362
      val th = Thm.incr_indexes 1 gth;
paulson@15481
   363
      val tgt_term = Thm.prop_of th;
paulson@15481
   364
paulson@15481
   365
      val sgn = Thm.sign_of_thm th;
paulson@15481
   366
      val ctermify = Thm.cterm_of sgn;
paulson@15481
   367
      val trivify = Thm.trivial o ctermify;
paulson@15481
   368
paulson@15481
   369
      val (fixedbody, fvs) = IsaND.fix_alls_term i tgt_term;
paulson@15481
   370
      val cfvs = rev (map ctermify fvs);
paulson@15481
   371
dixon@15538
   372
      val asmt = Library.nth_elem(j - 1,(Logic.strip_imp_prems fixedbody));
dixon@15538
   373
      val asm_nprems = length (Logic.strip_imp_prems asmt);
dixon@15538
   374
dixon@15538
   375
      val pth = trivify asmt;
dixon@15538
   376
      val maxidx = Term.maxidx_of_term asmt;
dixon@15538
   377
paulson@15481
   378
    in
dixon@15538
   379
      ((cfvs, j, asm_nprems, pth), 
dixon@15538
   380
       (fn lhs => (searchf sgn maxidx lhs
dixon@15538
   381
                           ((IsaFTerm.focus_right 
dixon@15538
   382
                             o IsaFTerm.fcterm_of_term 
dixon@15538
   383
                             o Thm.prop_of) pth))))
paulson@15481
   384
    end;
paulson@15481
   385
dixon@15538
   386
(* prepare subst in every possible assumption *)
dixon@15538
   387
fun prep_subst_in_asms searchf i gth = 
dixon@15538
   388
    Seq.map 
dixon@15538
   389
      (prep_subst_in_asm searchf i gth)
dixon@15538
   390
      (Seq.of_list (IsaPLib.mk_num_list
dixon@15538
   391
                      (length (Logic.prems_of_goal (Thm.prop_of gth) i))));
dixon@15538
   392
dixon@15538
   393
dixon@15538
   394
(* substitute in an assumption using an object or meta level equality *)
dixon@15814
   395
fun eqsubst_asm_tac' searchf instepthm i th = 
dixon@15538
   396
    let 
dixon@15814
   397
      val asmpreps = prep_subst_in_asms searchf i th;
dixon@15538
   398
      val stepthms = 
dixon@15538
   399
          Seq.map Drule.zero_var_indexes 
dixon@15538
   400
                  (Seq.of_list (EqRuleData.prep_meta_eq instepthm))
dixon@15538
   401
dixon@15538
   402
      fun rewrite_with_thm (asminfo, findmatchf) r =
dixon@15538
   403
          let val (lhs,_) = Logic.dest_equals (Thm.concl_of r);
dixon@15538
   404
          in (findmatchf lhs)
dixon@15538
   405
             :-> (apply_subst_in_asm i th asminfo r) end;
dixon@15538
   406
    in
dixon@15538
   407
      (asmpreps :-> (fn a => stepthms :-> rewrite_with_thm a))
paulson@15481
   408
    end;
paulson@15481
   409
dixon@15936
   410
(* substitute using one of the given theorems in an assumption.
dixon@15936
   411
Implicit args: occL thms i th *)
dixon@15936
   412
val eqsubst_asm_tac = eqsubst_occL eqsubst_asm_tac'; 
dixon@15936
   413
paulson@15481
   414
paulson@15481
   415
(* inthms are the given arguments in Isar, and treated as eqstep with
paulson@15481
   416
   the first one, then the second etc *)
dixon@15936
   417
fun eqsubst_asm_meth occL inthms =
paulson@15481
   418
    Method.METHOD 
dixon@15538
   419
      (fn facts =>
dixon@15936
   420
          HEADGOAL (Method.insert_tac facts THEN' eqsubst_asm_tac occL inthms ));
paulson@15481
   421
paulson@15481
   422
(* combination method that takes a flag (true indicates that subst
paulson@15481
   423
should be done to an assumption, false = apply to the conclusion of
paulson@15481
   424
the goal) as well as the theorems to use *)
dixon@15936
   425
fun meth ((asmflag, occL), inthms) ctxt = 
dixon@15936
   426
    if asmflag then eqsubst_asm_meth occL inthms else eqsubst_meth occL inthms;
paulson@15481
   427
paulson@15481
   428
(* syntax for options, given "(asm)" will give back true, without
paulson@15481
   429
   gives back false *)
paulson@15481
   430
val options_syntax =
paulson@15481
   431
    (Args.parens (Args.$$$ "asm") >> (K true)) ||
paulson@15481
   432
     (Scan.succeed false);
dixon@15936
   433
dixon@15929
   434
val ith_syntax =
dixon@15936
   435
    (Args.parens (Scan.repeat Args.nat))
dixon@15936
   436
      || (Scan.succeed [0]);
paulson@15481
   437
paulson@15481
   438
(* method syntax, first take options, then theorems *)
paulson@15481
   439
fun meth_syntax meth src ctxt =
paulson@15481
   440
    meth (snd (Method.syntax ((Scan.lift options_syntax) 
dixon@15929
   441
                                -- (Scan.lift ith_syntax) 
paulson@15481
   442
                                -- Attrib.local_thms) src ctxt)) 
paulson@15481
   443
         ctxt;
paulson@15481
   444
paulson@15481
   445
(* setup function for adding method to theory. *)
paulson@15481
   446
val setup = 
paulson@15481
   447
    [Method.add_method ("subst", meth_syntax meth, "Substiution with an equation. Use \"(asm)\" option to substitute in an assumption.")];
paulson@15481
   448
paulson@15481
   449
end;