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