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