src/Provers/eqsubst.ML
author wenzelm
Wed Apr 04 23:29:33 2007 +0200 (2007-04-04)
changeset 22596 d0d2af4db18f
parent 22578 b0eb5652f210
child 22727 473c7f67c64f
permissions -rw-r--r--
rep_thm/cterm/ctyp: removed obsolete sign field;
dixon@15538
     1
(*  Title:      Provers/eqsubst.ML
wenzelm@16434
     2
    ID:         $Id$
wenzelm@18598
     3
    Author:     Lucas Dixon, University of Edinburgh, lucas.dixon@ed.ac.uk
paulson@15481
     4
wenzelm@18598
     5
A proof method to perform a substiution using an equation.
wenzelm@18598
     6
*)
dixon@15538
     7
wenzelm@18591
     8
signature EQSUBST =
paulson@15481
     9
sig
dixon@19871
    10
  (* a type abriviation for match information *)
dixon@19871
    11
  type match =
dixon@19871
    12
       ((indexname * (sort * typ)) list (* type instantiations *)
dixon@19871
    13
        * (indexname * (typ * term)) list) (* term instantiations *)
dixon@19871
    14
       * (string * typ) list (* fake named type abs env *)
dixon@19871
    15
       * (string * typ) list (* type abs env *)
dixon@19871
    16
       * term (* outer term *)
dixon@19871
    17
dixon@19871
    18
  type searchinfo =
dixon@19871
    19
       theory
dixon@19871
    20
       * int (* maxidx *)
dixon@19871
    21
       * Zipper.T (* focusterm to search under *)
dixon@19871
    22
dixon@19871
    23
    exception eqsubst_occL_exp of
dixon@19871
    24
       string * int list * Thm.thm list * int * Thm.thm
dixon@19871
    25
    
dixon@19871
    26
    (* low level substitution functions *)
dixon@19871
    27
    val apply_subst_in_asm :
dixon@19871
    28
       int ->
dixon@19871
    29
       Thm.thm ->
dixon@19871
    30
       Thm.thm ->
dixon@19871
    31
       (Thm.cterm list * int * 'a * Thm.thm) * match -> Thm.thm Seq.seq
dixon@19871
    32
    val apply_subst_in_concl :
dixon@19871
    33
       int ->
dixon@19871
    34
       Thm.thm ->
dixon@19871
    35
       Thm.cterm list * Thm.thm ->
dixon@19871
    36
       Thm.thm -> match -> Thm.thm Seq.seq
dixon@19871
    37
dixon@19871
    38
    (* matching/unification within zippers *)
dixon@19871
    39
    val clean_match_z :
dixon@19871
    40
       Context.theory -> Term.term -> Zipper.T -> match option
dixon@19871
    41
    val clean_unify_z :
dixon@19871
    42
       Context.theory -> int -> Term.term -> Zipper.T -> match Seq.seq
dixon@19871
    43
dixon@19871
    44
    (* skipping things in seq seq's *)
dixon@19871
    45
dixon@19871
    46
   (* skipping non-empty sub-sequences but when we reach the end
dixon@19871
    47
      of the seq, remembering how much we have left to skip. *)
dixon@19871
    48
    datatype 'a skipseq = SkipMore of int
dixon@19871
    49
      | SkipSeq of 'a Seq.seq Seq.seq;
dixon@19871
    50
dixon@19871
    51
    val skip_first_asm_occs_search :
dixon@19871
    52
       ('a -> 'b -> 'c Seq.seq Seq.seq) ->
dixon@19871
    53
       'a -> int -> 'b -> 'c skipseq
dixon@19871
    54
    val skip_first_occs_search :
dixon@19871
    55
       int -> ('a -> 'b -> 'c Seq.seq Seq.seq) -> 'a -> 'b -> 'c Seq.seq
dixon@19871
    56
    val skipto_skipseq : int -> 'a Seq.seq Seq.seq -> 'a skipseq
dixon@19871
    57
dixon@19871
    58
    (* tactics *)
dixon@19871
    59
    val eqsubst_asm_tac :
dixon@19871
    60
       Proof.context ->
dixon@19871
    61
       int list -> Thm.thm list -> int -> Thm.thm -> Thm.thm Seq.seq
dixon@19871
    62
    val eqsubst_asm_tac' :
dixon@19871
    63
       Proof.context ->
dixon@19871
    64
       (searchinfo -> int -> Term.term -> match skipseq) ->
dixon@19871
    65
       int -> Thm.thm -> int -> Thm.thm -> Thm.thm Seq.seq
dixon@19871
    66
    val eqsubst_tac :
dixon@19871
    67
       Proof.context ->
dixon@19871
    68
       int list -> Thm.thm list -> int -> Thm.thm -> Thm.thm Seq.seq
dixon@19871
    69
    val eqsubst_tac' :
dixon@19871
    70
       Proof.context ->
dixon@19871
    71
       (searchinfo -> Term.term -> match Seq.seq) 
dixon@19871
    72
       -> Thm.thm -> int -> Thm.thm -> Thm.thm Seq.seq
dixon@19871
    73
dixon@19871
    74
dixon@19871
    75
    val fakefree_badbounds :
dixon@19871
    76
       (string * Term.typ) list ->
dixon@19871
    77
       Term.term ->
dixon@19871
    78
       (string * Term.typ) list * (string * Term.typ) list * Term.term
dixon@19871
    79
dixon@19871
    80
    val mk_foo_match :
dixon@19871
    81
       (Term.term -> Term.term) ->
dixon@19871
    82
       ('a * Term.typ) list -> Term.term -> Term.term
dixon@19871
    83
dixon@19871
    84
    (* preparing substitution *)
dixon@19871
    85
    val prep_meta_eq : Proof.context -> Thm.thm -> Thm.thm list
dixon@19871
    86
    val prep_concl_subst :
dixon@19871
    87
       int -> Thm.thm -> (Thm.cterm list * Thm.thm) * searchinfo
dixon@19871
    88
    val prep_subst_in_asm :
dixon@19871
    89
       int -> Thm.thm -> int ->
dixon@19871
    90
       (Thm.cterm list * int * int * Thm.thm) * searchinfo
dixon@19871
    91
    val prep_subst_in_asms :
dixon@19871
    92
       int -> Thm.thm ->
dixon@19871
    93
       ((Thm.cterm list * int * int * Thm.thm) * searchinfo) list
dixon@19871
    94
    val prep_zipper_match :
dixon@19871
    95
       Zipper.T -> Term.term * ((string * Term.typ) list * (string * Term.typ) list * Term.term)
dixon@19871
    96
dixon@19871
    97
    (* search for substitutions *)
dixon@19871
    98
    val valid_match_start : Zipper.T -> bool
dixon@19871
    99
    val search_lr_all : Zipper.T -> Zipper.T Seq.seq
dixon@19871
   100
    val search_lr_valid : (Zipper.T -> bool) -> Zipper.T -> Zipper.T Seq.seq
dixon@19871
   101
    val searchf_lr_unify_all :
dixon@19871
   102
       searchinfo -> Term.term -> match Seq.seq Seq.seq
dixon@19871
   103
    val searchf_lr_unify_valid :
dixon@19871
   104
       searchinfo -> Term.term -> match Seq.seq Seq.seq
dixon@19871
   105
dixon@19871
   106
dixon@19871
   107
    (* syntax tools *)
dixon@19871
   108
    val ith_syntax : Args.T list -> int list * Args.T list
dixon@19871
   109
    val options_syntax : Args.T list -> bool * Args.T list
dixon@19871
   110
dixon@19871
   111
    (* Isar level hooks *)
wenzelm@20289
   112
    val eqsubst_asm_meth : Proof.context -> int list -> Thm.thm list -> Proof.method
wenzelm@20289
   113
    val eqsubst_meth : Proof.context -> int list -> Thm.thm list -> Proof.method
wenzelm@20289
   114
    val subst_meth : Method.src -> Proof.context -> Proof.method
dixon@19871
   115
    val setup : theory -> theory
dixon@19871
   116
paulson@15481
   117
end;
paulson@15481
   118
dixon@19835
   119
structure EqSubst
dixon@19871
   120
: EQSUBST
dixon@19835
   121
= struct
dixon@16004
   122
dixon@19835
   123
structure Z = Zipper;
dixon@19835
   124
dixon@19835
   125
(* changes object "=" to meta "==" which prepares a given rewrite rule *)
wenzelm@18598
   126
fun prep_meta_eq ctxt =
wenzelm@18598
   127
  let val (_, {mk_rews = {mk, ...}, ...}) = Simplifier.rep_ss (Simplifier.local_simpset_of ctxt)
wenzelm@18598
   128
  in mk #> map Drule.zero_var_indexes end;
wenzelm@18598
   129
paulson@15481
   130
dixon@15915
   131
  (* a type abriviation for match information *)
wenzelm@16978
   132
  type match =
wenzelm@16978
   133
       ((indexname * (sort * typ)) list (* type instantiations *)
wenzelm@16978
   134
        * (indexname * (typ * term)) list) (* term instantiations *)
wenzelm@16978
   135
       * (string * typ) list (* fake named type abs env *)
wenzelm@16978
   136
       * (string * typ) list (* type abs env *)
wenzelm@16978
   137
       * term (* outer term *)
dixon@15550
   138
wenzelm@16978
   139
  type searchinfo =
wenzelm@18598
   140
       theory
dixon@16004
   141
       * int (* maxidx *)
dixon@19835
   142
       * Zipper.T (* focusterm to search under *)
dixon@19835
   143
dixon@19835
   144
dixon@19835
   145
(* skipping non-empty sub-sequences but when we reach the end
dixon@19835
   146
   of the seq, remembering how much we have left to skip. *)
dixon@19835
   147
datatype 'a skipseq = SkipMore of int
dixon@19835
   148
  | SkipSeq of 'a Seq.seq Seq.seq;
dixon@19835
   149
(* given a seqseq, skip the first m non-empty seq's, note deficit *)
dixon@19835
   150
fun skipto_skipseq m s = 
dixon@19835
   151
    let 
dixon@19835
   152
      fun skip_occs n sq = 
dixon@19835
   153
          case Seq.pull sq of 
dixon@19835
   154
            NONE => SkipMore n
dixon@19835
   155
          | SOME (h,t) => 
dixon@19835
   156
            (case Seq.pull h of NONE => skip_occs n t
dixon@19835
   157
             | SOME _ => if n <= 1 then SkipSeq (Seq.cons h t)
dixon@19835
   158
                         else skip_occs (n - 1) t)
dixon@19835
   159
    in (skip_occs m s) end;
dixon@19835
   160
dixon@19835
   161
(* note: outerterm is the taget with the match replaced by a bound 
dixon@19835
   162
         variable : ie: "P lhs" beocmes "%x. P x" 
dixon@19835
   163
         insts is the types of instantiations of vars in lhs
dixon@19835
   164
         and typinsts is the type instantiations of types in the lhs
dixon@19835
   165
         Note: Final rule is the rule lifted into the ontext of the 
dixon@19835
   166
         taget thm. *)
dixon@19835
   167
fun mk_foo_match mkuptermfunc Ts t = 
dixon@19835
   168
    let 
dixon@19835
   169
      val ty = Term.type_of t
dixon@19835
   170
      val bigtype = (rev (map snd Ts)) ---> ty
dixon@19835
   171
      fun mk_foo 0 t = t
dixon@19835
   172
        | mk_foo i t = mk_foo (i - 1) (t $ (Bound (i - 1)))
dixon@19835
   173
      val num_of_bnds = (length Ts)
dixon@19835
   174
      (* foo_term = "fooabs y0 ... yn" where y's are local bounds *)
dixon@19835
   175
      val foo_term = mk_foo num_of_bnds (Bound num_of_bnds)
dixon@19835
   176
    in Abs("fooabs", bigtype, mkuptermfunc foo_term) end;
dixon@19835
   177
dixon@19835
   178
(* T is outer bound vars, n is number of locally bound vars *)
dixon@19835
   179
(* THINK: is order of Ts correct...? or reversed? *)
dixon@19835
   180
fun fakefree_badbounds Ts t = 
dixon@19835
   181
    let val (FakeTs,Ts,newnames) = 
dixon@19835
   182
            List.foldr (fn ((n,ty),(FakeTs,Ts,usednames)) => 
wenzelm@20071
   183
                           let val newname = Name.variant usednames n
dixon@19835
   184
                           in ((RWTools.mk_fake_bound_name newname,ty)::FakeTs,
dixon@19835
   185
                               (newname,ty)::Ts, 
dixon@19835
   186
                               newname::usednames) end)
dixon@19835
   187
                       ([],[],[])
dixon@19835
   188
                       Ts
dixon@19835
   189
    in (FakeTs, Ts, Term.subst_bounds (map Free FakeTs, t)) end;
dixon@19835
   190
dixon@19835
   191
(* before matching we need to fake the bound vars that are missing an
dixon@19835
   192
abstraction. In this function we additionally construct the
dixon@19835
   193
abstraction environment, and an outer context term (with the focus
dixon@19835
   194
abstracted out) for use in rewriting with RWInst.rw *)
dixon@19835
   195
fun prep_zipper_match z = 
dixon@19835
   196
    let 
dixon@19835
   197
      val t = Z.trm z  
dixon@19835
   198
      val c = Z.ctxt z
dixon@19835
   199
      val Ts = Z.C.nty_ctxt c
dixon@19835
   200
      val (FakeTs', Ts', t') = fakefree_badbounds Ts t
dixon@19835
   201
      val absterm = mk_foo_match (Z.C.apply c) Ts' t'
dixon@19835
   202
    in
dixon@19835
   203
      (t', (FakeTs', Ts', absterm))
dixon@19835
   204
    end;
dixon@19835
   205
dixon@19835
   206
(* Matching and Unification with exception handled *)
dixon@19835
   207
fun clean_match thy (a as (pat, t)) =
dixon@19835
   208
  let val (tyenv, tenv) = Pattern.match thy a (Vartab.empty, Vartab.empty)
dixon@19835
   209
  in SOME (Vartab.dest tyenv, Vartab.dest tenv)
dixon@19835
   210
  end handle Pattern.MATCH => NONE;
dixon@19835
   211
(* given theory, max var index, pat, tgt; returns Seq of instantiations *)
dixon@19835
   212
fun clean_unify sgn ix (a as (pat, tgt)) =
dixon@19835
   213
    let
dixon@19835
   214
      (* type info will be re-derived, maybe this can be cached
dixon@19835
   215
         for efficiency? *)
dixon@19835
   216
      val pat_ty = Term.type_of pat;
dixon@19835
   217
      val tgt_ty = Term.type_of tgt;
dixon@19835
   218
      (* is it OK to ignore the type instantiation info?
dixon@19835
   219
         or should I be using it? *)
dixon@19835
   220
      val typs_unify =
dixon@19835
   221
          SOME (Sign.typ_unify sgn (pat_ty, tgt_ty) (Term.Vartab.empty, ix))
dixon@19835
   222
            handle Type.TUNIFY => NONE;
dixon@19835
   223
    in
dixon@19835
   224
      case typs_unify of
dixon@19835
   225
        SOME (typinsttab, ix2) =>
dixon@19835
   226
        let
dixon@19835
   227
      (* is it right to throw away the flexes?
dixon@19835
   228
         or should I be using them somehow? *)
dixon@19835
   229
          fun mk_insts env =
dixon@19835
   230
            (Vartab.dest (Envir.type_env env),
dixon@19835
   231
             Envir.alist_of env);
dixon@19835
   232
          val initenv = Envir.Envir {asol = Vartab.empty,
dixon@19835
   233
                                     iTs = typinsttab, maxidx = ix2};
wenzelm@19861
   234
          val useq = Unify.smash_unifiers sgn [a] initenv
dixon@19835
   235
	            handle UnequalLengths => Seq.empty
dixon@19835
   236
		               | Term.TERM _ => Seq.empty;
dixon@19835
   237
          fun clean_unify' useq () =
dixon@19835
   238
              (case (Seq.pull useq) of
dixon@19835
   239
                 NONE => NONE
dixon@19835
   240
               | SOME (h,t) => SOME (mk_insts h, Seq.make (clean_unify' t)))
dixon@19835
   241
	      handle UnequalLengths => NONE
dixon@19835
   242
                   | Term.TERM _ => NONE;
dixon@19835
   243
        in
dixon@19835
   244
          (Seq.make (clean_unify' useq))
dixon@19835
   245
        end
dixon@19835
   246
      | NONE => Seq.empty
dixon@19835
   247
    end;
dixon@19835
   248
dixon@19835
   249
(* Matching and Unification for zippers *)
dixon@19835
   250
(* Note: Ts is a modified version of the original names of the outer
dixon@19835
   251
bound variables. New names have been introduced to make sure they are
dixon@19835
   252
unique w.r.t all names in the term and each other. usednames' is
dixon@19835
   253
oldnames + new names. *)
dixon@19835
   254
fun clean_match_z thy pat z = 
dixon@19835
   255
    let val (t, (FakeTs,Ts,absterm)) = prep_zipper_match z in
dixon@19835
   256
      case clean_match thy (pat, t) of 
dixon@19835
   257
        NONE => NONE 
dixon@19835
   258
      | SOME insts => SOME (insts, FakeTs, Ts, absterm) end;
dixon@19835
   259
(* ix = max var index *)
dixon@19835
   260
fun clean_unify_z sgn ix pat z = 
dixon@19835
   261
    let val (t, (FakeTs, Ts,absterm)) = prep_zipper_match z in
dixon@19835
   262
    Seq.map (fn insts => (insts, FakeTs, Ts, absterm)) 
dixon@19835
   263
            (clean_unify sgn ix (t, pat)) end;
dixon@19835
   264
dixon@15550
   265
dixon@15538
   266
(* FOR DEBUGGING...
dixon@15538
   267
type trace_subst_errT = int (* subgoal *)
wenzelm@16978
   268
        * thm (* thm with all goals *)
dixon@15538
   269
        * (Thm.cterm list (* certified free var placeholders for vars *)
wenzelm@16978
   270
           * thm)  (* trivial thm of goal concl *)
dixon@15538
   271
            (* possible matches/unifiers *)
wenzelm@16978
   272
        * thm (* rule *)
wenzelm@16978
   273
        * (((indexname * typ) list (* type instantiations *)
wenzelm@16978
   274
              * (indexname * term) list ) (* term instantiations *)
wenzelm@16978
   275
             * (string * typ) list (* Type abs env *)
wenzelm@16978
   276
             * term) (* outer term *);
dixon@15538
   277
dixon@15538
   278
val trace_subst_err = (ref NONE : trace_subst_errT option ref);
dixon@15538
   279
val trace_subst_search = ref false;
dixon@15538
   280
exception trace_subst_exp of trace_subst_errT;
dixon@19835
   281
*)
dixon@19835
   282
dixon@19835
   283
dixon@19835
   284
fun bot_left_leaf_of (l $ r) = bot_left_leaf_of l
dixon@19835
   285
  | bot_left_leaf_of (Abs(s,ty,t)) = bot_left_leaf_of t
dixon@19835
   286
  | bot_left_leaf_of x = x;
dixon@15538
   287
dixon@19975
   288
(* Avoid considering replacing terms which have a var at the head as
dixon@19975
   289
   they always succeed trivially, and uninterestingly. *)
dixon@19835
   290
fun valid_match_start z =
dixon@19835
   291
    (case bot_left_leaf_of (Z.trm z) of 
dixon@19975
   292
      Var _ => false 
dixon@19975
   293
      | _ => true);
dixon@19975
   294
dixon@15814
   295
(* search from top, left to right, then down *)
dixon@19871
   296
val search_lr_all = ZipperSearch.all_bl_ur;
paulson@15481
   297
dixon@15814
   298
(* search from top, left to right, then down *)
dixon@19871
   299
fun search_lr_valid validf =
dixon@19835
   300
    let 
dixon@19835
   301
      fun sf_valid_td_lr z = 
dixon@19835
   302
          let val here = if validf z then [Z.Here z] else [] in
dixon@19835
   303
            case Z.trm z 
dixon@19871
   304
             of _ $ _ => [Z.LookIn (Z.move_down_left z)] 
dixon@19871
   305
                         @ here 
dixon@19871
   306
                         @ [Z.LookIn (Z.move_down_right z)]
dixon@19835
   307
              | Abs _ => here @ [Z.LookIn (Z.move_down_abs z)]
dixon@19835
   308
              | _ => here
dixon@19835
   309
          end;
dixon@19835
   310
    in Z.lzy_search sf_valid_td_lr end;
dixon@15814
   311
dixon@15814
   312
(* search all unifications *)
dixon@19871
   313
fun searchf_lr_unify_all (sgn, maxidx, z) lhs =
dixon@19835
   314
    Seq.map (clean_unify_z sgn maxidx lhs) 
dixon@19871
   315
            (Z.limit_apply search_lr_all z);
paulson@15481
   316
dixon@15814
   317
(* search only for 'valid' unifiers (non abs subterms and non vars) *)
dixon@19871
   318
fun searchf_lr_unify_valid (sgn, maxidx, z) lhs  =
dixon@19835
   319
    Seq.map (clean_unify_z sgn maxidx lhs) 
dixon@19871
   320
            (Z.limit_apply (search_lr_valid valid_match_start) z);
dixon@15929
   321
dixon@15814
   322
dixon@15538
   323
(* apply a substitution in the conclusion of the theorem th *)
dixon@15538
   324
(* cfvs are certified free var placeholders for goal params *)
dixon@15538
   325
(* conclthm is a theorem of for just the conclusion *)
dixon@15538
   326
(* m is instantiation/match information *)
dixon@15538
   327
(* rule is the equation for substitution *)
wenzelm@16978
   328
fun apply_subst_in_concl i th (cfvs, conclthm) rule m =
dixon@15538
   329
    (RWInst.rw m rule conclthm)
dixon@15855
   330
      |> IsaND.unfix_frees cfvs
dixon@15915
   331
      |> RWInst.beta_eta_contract
dixon@15538
   332
      |> (fn r => Tactic.rtac r i th);
paulson@15481
   333
paulson@15481
   334
(* substitute within the conclusion of goal i of gth, using a meta
dixon@15538
   335
equation rule. Note that we assume rule has var indicies zero'd *)
wenzelm@16978
   336
fun prep_concl_subst i gth =
wenzelm@16978
   337
    let
paulson@15481
   338
      val th = Thm.incr_indexes 1 gth;
paulson@15481
   339
      val tgt_term = Thm.prop_of th;
paulson@15481
   340
wenzelm@22578
   341
      val sgn = Thm.theory_of_thm th;
paulson@15481
   342
      val ctermify = Thm.cterm_of sgn;
paulson@15481
   343
      val trivify = Thm.trivial o ctermify;
paulson@15481
   344
paulson@15481
   345
      val (fixedbody, fvs) = IsaND.fix_alls_term i tgt_term;
paulson@15481
   346
      val cfvs = rev (map ctermify fvs);
paulson@15481
   347
dixon@15538
   348
      val conclterm = Logic.strip_imp_concl fixedbody;
dixon@15538
   349
      val conclthm = trivify conclterm;
dixon@15538
   350
      val maxidx = Term.maxidx_of_term conclterm;
dixon@19835
   351
      val ft = ((Z.move_down_right (* ==> *)
dixon@19835
   352
                 o Z.move_down_left (* Trueprop *)
dixon@19835
   353
                 o Z.mktop
dixon@16004
   354
                 o Thm.prop_of) conclthm)
paulson@15481
   355
    in
dixon@16004
   356
      ((cfvs, conclthm), (sgn, maxidx, ft))
paulson@15481
   357
    end;
paulson@15481
   358
paulson@15481
   359
(* substitute using an object or meta level equality *)
wenzelm@18598
   360
fun eqsubst_tac' ctxt searchf instepthm i th =
wenzelm@16978
   361
    let
dixon@16004
   362
      val (cvfsconclthm, searchinfo) = prep_concl_subst i th;
wenzelm@18598
   363
      val stepthms = Seq.of_list (prep_meta_eq ctxt instepthm);
dixon@15538
   364
      fun rewrite_with_thm r =
dixon@15538
   365
          let val (lhs,_) = Logic.dest_equals (Thm.concl_of r);
wenzelm@18598
   366
          in searchf searchinfo lhs
wenzelm@18598
   367
             |> Seq.maps (apply_subst_in_concl i th cvfsconclthm r) end;
wenzelm@18598
   368
    in stepthms |> Seq.maps rewrite_with_thm end;
dixon@15538
   369
dixon@15538
   370
wenzelm@19047
   371
(* distinct subgoals *)
dixon@15959
   372
fun distinct_subgoals th =
wenzelm@19047
   373
  the_default th (SINGLE distinct_subgoals_tac th);
dixon@15538
   374
wenzelm@19047
   375
(* General substitution of multiple occurances using one of
dixon@15936
   376
   the given theorems*)
dixon@19835
   377
dixon@19835
   378
wenzelm@16978
   379
exception eqsubst_occL_exp of
wenzelm@16978
   380
          string * (int list) * (thm list) * int * thm;
wenzelm@16978
   381
fun skip_first_occs_search occ srchf sinfo lhs =
dixon@19835
   382
    case (skipto_skipseq occ (srchf sinfo lhs)) of
dixon@19835
   383
      SkipMore _ => Seq.empty
dixon@19835
   384
    | SkipSeq ss => Seq.flat ss;
dixon@16004
   385
wenzelm@18598
   386
fun eqsubst_tac ctxt occL thms i th =
dixon@15936
   387
    let val nprems = Thm.nprems_of th in
dixon@15936
   388
      if nprems < i then Seq.empty else
wenzelm@16978
   389
      let val thmseq = (Seq.of_list thms)
wenzelm@16978
   390
        fun apply_occ occ th =
wenzelm@18598
   391
            thmseq |> Seq.maps
dixon@19835
   392
                    (fn r => eqsubst_tac' 
dixon@19835
   393
                               ctxt 
dixon@19835
   394
                               (skip_first_occs_search
dixon@19871
   395
                                  occ searchf_lr_unify_valid) r
dixon@15936
   396
                                 (i + ((Thm.nprems_of th) - nprems))
dixon@15936
   397
                                 th);
wenzelm@16978
   398
        val sortedoccL =
dixon@16004
   399
            Library.sort (Library.rev_order o Library.int_ord) occL;
dixon@15936
   400
      in
dixon@16004
   401
        Seq.map distinct_subgoals (Seq.EVERY (map apply_occ sortedoccL) th)
dixon@15936
   402
      end
dixon@15959
   403
    end
dixon@15959
   404
    handle THM _ => raise eqsubst_occL_exp ("THM",occL,thms,i,th);
dixon@15959
   405
paulson@15481
   406
paulson@15481
   407
(* inthms are the given arguments in Isar, and treated as eqstep with
paulson@15481
   408
   the first one, then the second etc *)
wenzelm@18598
   409
fun eqsubst_meth ctxt occL inthms =
wenzelm@21588
   410
    Method.SIMPLE_METHOD' (eqsubst_tac ctxt occL inthms);
paulson@15481
   411
dixon@16004
   412
(* apply a substitution inside assumption j, keeps asm in the same place *)
wenzelm@16978
   413
fun apply_subst_in_asm i th rule ((cfvs, j, ngoalprems, pth),m) =
wenzelm@16978
   414
    let
dixon@16004
   415
      val th2 = Thm.rotate_rule (j - 1) i th; (* put premice first *)
wenzelm@16978
   416
      val preelimrule =
dixon@16004
   417
          (RWInst.rw m rule pth)
wenzelm@21708
   418
            |> (Seq.hd o prune_params_tac)
dixon@16004
   419
            |> Thm.permute_prems 0 ~1 (* put old asm first *)
dixon@16004
   420
            |> IsaND.unfix_frees cfvs (* unfix any global params *)
dixon@16004
   421
            |> RWInst.beta_eta_contract; (* normal form *)
wenzelm@16978
   422
  (*    val elimrule =
dixon@16004
   423
          preelimrule
dixon@16004
   424
            |> Tactic.make_elim (* make into elim rule *)
dixon@16004
   425
            |> Thm.lift_rule (th2, i); (* lift into context *)
dixon@16007
   426
   *)
dixon@16004
   427
    in
dixon@16004
   428
      (* ~j because new asm starts at back, thus we subtract 1 *)
dixon@16007
   429
      Seq.map (Thm.rotate_rule (~j) ((Thm.nprems_of rule) + i))
dixon@16007
   430
      (Tactic.dtac preelimrule i th2)
dixon@16007
   431
wenzelm@16978
   432
      (* (Thm.bicompose
dixon@16004
   433
                 false (* use unification *)
dixon@16004
   434
                 (true, (* elim resolution *)
dixon@16007
   435
                  elimrule, (2 + (Thm.nprems_of rule)) - ngoalprems)
dixon@16007
   436
                 i th2) *)
dixon@16004
   437
    end;
paulson@15481
   438
paulson@15481
   439
dixon@15538
   440
(* prepare to substitute within the j'th premise of subgoal i of gth,
dixon@15538
   441
using a meta-level equation. Note that we assume rule has var indicies
dixon@15538
   442
zero'd. Note that we also assume that premt is the j'th premice of
dixon@15538
   443
subgoal i of gth. Note the repetition of work done for each
dixon@15538
   444
assumption, i.e. this can be made more efficient for search over
dixon@15538
   445
multiple assumptions.  *)
wenzelm@16978
   446
fun prep_subst_in_asm i gth j =
wenzelm@16978
   447
    let
paulson@15481
   448
      val th = Thm.incr_indexes 1 gth;
paulson@15481
   449
      val tgt_term = Thm.prop_of th;
paulson@15481
   450
wenzelm@22578
   451
      val sgn = Thm.theory_of_thm th;
paulson@15481
   452
      val ctermify = Thm.cterm_of sgn;
paulson@15481
   453
      val trivify = Thm.trivial o ctermify;
paulson@15481
   454
paulson@15481
   455
      val (fixedbody, fvs) = IsaND.fix_alls_term i tgt_term;
paulson@15481
   456
      val cfvs = rev (map ctermify fvs);
paulson@15481
   457
haftmann@18011
   458
      val asmt = nth (Logic.strip_imp_prems fixedbody) (j - 1);
dixon@15538
   459
      val asm_nprems = length (Logic.strip_imp_prems asmt);
dixon@15538
   460
dixon@15538
   461
      val pth = trivify asmt;
dixon@15538
   462
      val maxidx = Term.maxidx_of_term asmt;
dixon@15538
   463
dixon@19835
   464
      val ft = ((Z.move_down_right (* trueprop *)
dixon@19835
   465
                 o Z.mktop
dixon@16004
   466
                 o Thm.prop_of) pth)
dixon@16004
   467
    in ((cfvs, j, asm_nprems, pth), (sgn, maxidx, ft)) end;
paulson@15481
   468
dixon@15538
   469
(* prepare subst in every possible assumption *)
wenzelm@16978
   470
fun prep_subst_in_asms i gth =
dixon@16004
   471
    map (prep_subst_in_asm i gth)
dixon@19835
   472
        ((fn l => Library.upto (1, length l))
dixon@16004
   473
           (Logic.prems_of_goal (Thm.prop_of gth) i));
dixon@15538
   474
dixon@15538
   475
dixon@15538
   476
(* substitute in an assumption using an object or meta level equality *)
wenzelm@18598
   477
fun eqsubst_asm_tac' ctxt searchf skipocc instepthm i th =
wenzelm@16978
   478
    let
dixon@16004
   479
      val asmpreps = prep_subst_in_asms i th;
wenzelm@18598
   480
      val stepthms = Seq.of_list (prep_meta_eq ctxt instepthm);
dixon@16004
   481
      fun rewrite_with_thm r =
dixon@16004
   482
          let val (lhs,_) = Logic.dest_equals (Thm.concl_of r)
dixon@16004
   483
            fun occ_search occ [] = Seq.empty
dixon@16004
   484
              | occ_search occ ((asminfo, searchinfo)::moreasms) =
wenzelm@16978
   485
                (case searchf searchinfo occ lhs of
dixon@19835
   486
                   SkipMore i => occ_search i moreasms
dixon@19835
   487
                 | SkipSeq ss =>
wenzelm@19861
   488
                   Seq.append (Seq.map (Library.pair asminfo) (Seq.flat ss))
wenzelm@19861
   489
                               (occ_search 1 moreasms))
dixon@16004
   490
                              (* find later substs also *)
wenzelm@16978
   491
          in
wenzelm@18598
   492
            occ_search skipocc asmpreps |> Seq.maps (apply_subst_in_asm i th r)
dixon@16004
   493
          end;
wenzelm@18598
   494
    in stepthms |> Seq.maps rewrite_with_thm end;
dixon@15538
   495
dixon@16004
   496
wenzelm@16978
   497
fun skip_first_asm_occs_search searchf sinfo occ lhs =
dixon@19835
   498
    skipto_skipseq occ (searchf sinfo lhs);
dixon@16004
   499
wenzelm@18598
   500
fun eqsubst_asm_tac ctxt occL thms i th =
wenzelm@16978
   501
    let val nprems = Thm.nprems_of th
dixon@15538
   502
    in
dixon@16004
   503
      if nprems < i then Seq.empty else
wenzelm@16978
   504
      let val thmseq = (Seq.of_list thms)
wenzelm@16978
   505
        fun apply_occ occK th =
wenzelm@18598
   506
            thmseq |> Seq.maps
wenzelm@16978
   507
                    (fn r =>
wenzelm@18598
   508
                        eqsubst_asm_tac' ctxt (skip_first_asm_occs_search
dixon@19871
   509
                                            searchf_lr_unify_valid) occK r
dixon@16004
   510
                                         (i + ((Thm.nprems_of th) - nprems))
dixon@16004
   511
                                         th);
wenzelm@16978
   512
        val sortedoccs =
dixon@16004
   513
            Library.sort (Library.rev_order o Library.int_ord) occL
dixon@16004
   514
      in
dixon@16004
   515
        Seq.map distinct_subgoals
dixon@16004
   516
                (Seq.EVERY (map apply_occ sortedoccs) th)
dixon@16004
   517
      end
dixon@16004
   518
    end
dixon@16004
   519
    handle THM _ => raise eqsubst_occL_exp ("THM",occL,thms,i,th);
paulson@15481
   520
paulson@15481
   521
(* inthms are the given arguments in Isar, and treated as eqstep with
paulson@15481
   522
   the first one, then the second etc *)
wenzelm@18598
   523
fun eqsubst_asm_meth ctxt occL inthms =
wenzelm@21588
   524
    Method.SIMPLE_METHOD' (eqsubst_asm_tac ctxt occL inthms);
paulson@15481
   525
paulson@15481
   526
(* syntax for options, given "(asm)" will give back true, without
paulson@15481
   527
   gives back false *)
paulson@15481
   528
val options_syntax =
paulson@15481
   529
    (Args.parens (Args.$$$ "asm") >> (K true)) ||
paulson@15481
   530
     (Scan.succeed false);
dixon@15936
   531
dixon@15929
   532
val ith_syntax =
dixon@15936
   533
    (Args.parens (Scan.repeat Args.nat))
dixon@15936
   534
      || (Scan.succeed [0]);
paulson@15481
   535
wenzelm@18598
   536
(* combination method that takes a flag (true indicates that subst
wenzelm@18598
   537
should be done to an assumption, false = apply to the conclusion of
wenzelm@18598
   538
the goal) as well as the theorems to use *)
wenzelm@18598
   539
fun subst_meth src =
wenzelm@18988
   540
  Method.syntax ((Scan.lift options_syntax) -- (Scan.lift ith_syntax) -- Attrib.thms) src
haftmann@21879
   541
  #> (fn (((asmflag, occL), inthms), ctxt) =>
wenzelm@18598
   542
    (if asmflag then eqsubst_asm_meth else eqsubst_meth) ctxt occL inthms);
paulson@15481
   543
wenzelm@18598
   544
wenzelm@16978
   545
val setup =
wenzelm@18833
   546
  Method.add_method ("subst", subst_meth, "single-step substitution");
paulson@15481
   547
wenzelm@16978
   548
end;