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