src/HOL/Library/reflection.ML
author hoelzl
Wed Jun 03 15:48:54 2009 +0200 (2009-06-03)
changeset 31412 f2e6b6526092
parent 31387 c4a3c3e9dc8e
child 31794 71af1fd6a5e4
permissions -rw-r--r--
Converted reification to use fold_map instead of Library.foldl_map. Use antiquotations.
haftmann@29650
     1
(*  Title:      HOL/Library/reflection.ML
haftmann@29650
     2
    Author:     Amine Chaieb, TU Muenchen
wenzelm@20319
     3
wenzelm@20319
     4
A trial for automatical reification.
wenzelm@20319
     5
*)
wenzelm@20319
     6
wenzelm@29269
     7
signature REFLECTION =
wenzelm@29269
     8
sig
wenzelm@20319
     9
  val genreify_tac: Proof.context -> thm list -> term option -> int -> tactic
chaieb@23648
    10
  val reflection_tac: Proof.context -> thm list -> thm list -> term option -> int -> tactic
haftmann@21878
    11
  val gen_reflection_tac: Proof.context -> (cterm -> thm)
chaieb@23648
    12
    -> thm list -> thm list -> term option -> int -> tactic
wenzelm@20319
    13
end;
wenzelm@20319
    14
wenzelm@29269
    15
structure Reflection : REFLECTION =
wenzelm@29269
    16
struct
wenzelm@20319
    17
haftmann@29650
    18
val ext2 = @{thm ext2};
haftmann@29650
    19
val nth_Cons_0 = @{thm nth_Cons_0};
haftmann@29650
    20
val nth_Cons_Suc = @{thm nth_Cons_Suc};
wenzelm@21669
    21
chaieb@20374
    22
  (* Make a congruence rule out of a defining equation for the interpretation *)
chaieb@20374
    23
  (* th is one defining equation of f, i.e.
chaieb@20374
    24
     th is "f (Cp ?t1 ... ?tn) = P(f ?t1, .., f ?tn)" *)
chaieb@20374
    25
  (* Cp is a constructor pattern and P is a pattern *)
chaieb@20374
    26
chaieb@20374
    27
  (* The result is:
chaieb@20374
    28
      [|?A1 = f ?t1 ; .. ; ?An= f ?tn |] ==> P (?A1, .., ?An) = f (Cp ?t1 .. ?tn) *)
chaieb@20374
    29
  (*  + the a list of names of the A1 .. An, Those are fresh in the ctxt*)
chaieb@20374
    30
hoelzl@31386
    31
fun mk_congeq ctxt fs th =
hoelzl@31386
    32
  let
hoelzl@31386
    33
   val (f as Const(fN,fT)) = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq
haftmann@29650
    34
     |> fst |> strip_comb |> fst
chaieb@20374
    35
   val thy = ProofContext.theory_of ctxt
chaieb@20374
    36
   val cert = Thm.cterm_of thy
wenzelm@22568
    37
   val (((_,_),[th']), ctxt') = Variable.import_thms true [th] ctxt
chaieb@20374
    38
   val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop (Thm.prop_of th'))
hoelzl@31386
    39
   fun add_fterms (t as t1 $ t2) =
wenzelm@29269
    40
       if exists (fn f => Term.could_unify (t |> strip_comb |> fst, f)) fs then insert (op aconv) t
chaieb@20564
    41
       else add_fterms t1 #> add_fterms t2
hoelzl@31386
    42
     | add_fterms (t as Abs(xn,xT,t')) =
wenzelm@29273
    43
       if exists_Const (fn (c, _) => c = fN) t then (fn _ => [t]) else (fn _ => [])
chaieb@20374
    44
     | add_fterms _ = I
chaieb@20374
    45
   val fterms = add_fterms rhs []
wenzelm@20797
    46
   val (xs, ctxt'') = Variable.variant_fixes (replicate (length fterms) "x") ctxt'
chaieb@20374
    47
   val tys = map fastype_of fterms
chaieb@20374
    48
   val vs = map Free (xs ~~ tys)
chaieb@20374
    49
   val env = fterms ~~ vs
hoelzl@31386
    50
		    (* FIXME!!!!*)
chaieb@20564
    51
   fun replace_fterms (t as t1 $ t2) =
chaieb@20374
    52
       (case AList.lookup (op aconv) env t of
chaieb@20374
    53
	    SOME v => v
chaieb@20564
    54
	  | NONE => replace_fterms t1 $ replace_fterms t2)
chaieb@20374
    55
     | replace_fterms t = (case AList.lookup (op aconv) env t of
chaieb@20374
    56
			       SOME v => v
chaieb@20374
    57
			     | NONE => t)
hoelzl@31386
    58
chaieb@20374
    59
   fun mk_def (Abs(x,xT,t),v) = HOLogic.mk_Trueprop ((HOLogic.all_const xT)$ Abs(x,xT,HOLogic.mk_eq(v$(Bound 0), t)))
chaieb@20374
    60
     | mk_def (t, v) = HOLogic.mk_Trueprop (HOLogic.mk_eq (v, t))
chaieb@23605
    61
   fun tryext x = (x RS ext2 handle THM _ =>  x)
chaieb@20374
    62
   val cong = (Goal.prove ctxt'' [] (map mk_def env)
chaieb@20374
    63
			  (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, replace_fterms rhs)))
hoelzl@31386
    64
			  (fn x => LocalDefs.unfold_tac (#context x) (map tryext (#prems x))
chaieb@20374
    65
							THEN rtac th' 1)) RS sym
hoelzl@31386
    66
hoelzl@31386
    67
   val (cong' :: vars') =
chaieb@20374
    68
       Variable.export ctxt'' ctxt (cong :: map (Drule.mk_term o cert) vs)
chaieb@20374
    69
   val vs' = map (fst o fst o Term.dest_Var o Thm.term_of o Drule.dest_term) vars'
hoelzl@31386
    70
hoelzl@31386
    71
  in  (vs', cong') end;
wenzelm@20319
    72
 (* congs is a list of pairs (P,th) where th is a theorem for *)
wenzelm@20319
    73
        (* [| f p1 = A1; ...; f pn = An|] ==> f (C p1 .. pn) = P *)
wenzelm@20319
    74
val FWD = curry (op OF);
wenzelm@20319
    75
wenzelm@20319
    76
chaieb@20374
    77
exception REIF of string;
chaieb@20374
    78
hoelzl@31412
    79
fun dest_listT (Type (@{type_name "list"}, [T])) = T;
hoelzl@29834
    80
hoelzl@31387
    81
(* This modified version of divide_and_conquer allows the threading
hoelzl@31387
    82
   of a state variable *)
hoelzl@31412
    83
fun divide_and_conquer' decomp s x =
hoelzl@31412
    84
  let val ((ys, recomb), s') = decomp s x
hoelzl@31412
    85
  in recomb (fold_map (divide_and_conquer' decomp) ys s') end;
hoelzl@31387
    86
hoelzl@31386
    87
fun rearrange congs =
hoelzl@31386
    88
  let
hoelzl@31386
    89
    fun P (_, th) =
hoelzl@31386
    90
      let val @{term "Trueprop"}$(Const ("op =",_) $l$_) = concl_of th
hoelzl@31386
    91
      in can dest_Var l end
hoelzl@31386
    92
    val (yes,no) = List.partition P congs
hoelzl@31386
    93
  in no @ yes end
hoelzl@29834
    94
hoelzl@29834
    95
fun genreif ctxt raw_eqs t =
hoelzl@31386
    96
  let
hoelzl@31412
    97
    fun index_of t bds =
hoelzl@31386
    98
      let
hoelzl@31386
    99
        val tt = HOLogic.listT (fastype_of t)
hoelzl@31386
   100
      in
hoelzl@31387
   101
       (case AList.lookup Type.could_unify bds tt of
hoelzl@31386
   102
          NONE => error "index_of : type not found in environements!"
hoelzl@31386
   103
        | SOME (tbs,tats) =>
hoelzl@31386
   104
          let
hoelzl@31386
   105
            val i = find_index_eq t tats
hoelzl@31386
   106
            val j = find_index_eq t tbs
hoelzl@31387
   107
          in (if j = ~1 then
hoelzl@31387
   108
              if i = ~1
hoelzl@31412
   109
              then (length tbs + length tats,
hoelzl@31412
   110
                    AList.update Type.could_unify (tt,(tbs,tats@[t])) bds)
hoelzl@31412
   111
              else (i, bds) else (j, bds))
hoelzl@31386
   112
          end)
hoelzl@31386
   113
      end;
hoelzl@31386
   114
hoelzl@31386
   115
    (* Generic decomp for reification : matches the actual term with the
hoelzl@31386
   116
       rhs of one cong rule. The result of the matching guides the
hoelzl@31386
   117
       proof synthesis: The matches of the introduced Variables A1 .. An are
hoelzl@31386
   118
       processed recursively
hoelzl@31386
   119
       The rest is instantiated in the cong rule,i.e. no reification is needed *)
chaieb@20564
   120
hoelzl@31386
   121
    (* da is the decomposition for atoms, ie. it returns ([],g) where g
hoelzl@31386
   122
       returns the right instance f (AtC n) = t , where AtC is the Atoms
hoelzl@31386
   123
       constructor and n is the number of the atom corresponding to t *)
hoelzl@31412
   124
    fun decomp_genreif da cgns (t,ctxt) bds =
hoelzl@31386
   125
      let
hoelzl@31386
   126
        val thy = ProofContext.theory_of ctxt
chaieb@20374
   127
        val cert = cterm_of thy
hoelzl@31412
   128
        fun tryabsdecomp (s,ctxt) bds =
hoelzl@31386
   129
          (case s of
hoelzl@31386
   130
             Abs(xn,xT,ta) => (
hoelzl@31386
   131
               let
hoelzl@31386
   132
                 val ([xn],ctxt') = Variable.variant_fixes ["x"] ctxt
hoelzl@31386
   133
                 val (xn,ta) = variant_abs (xn,xT,ta)
hoelzl@31386
   134
                 val x = Free(xn,xT)
hoelzl@31387
   135
                 val bds = (case AList.lookup Type.could_unify bds (HOLogic.listT xT)
hoelzl@31386
   136
		          of NONE => error "tryabsdecomp: Type not found in the Environement"
hoelzl@31386
   137
                           | SOME (bsT,atsT) =>
hoelzl@31387
   138
                             (AList.update Type.could_unify (HOLogic.listT xT, ((x::bsT), atsT)) bds))
hoelzl@31412
   139
               in (([(ta, ctxt')],
hoelzl@31412
   140
                    fn ([th], bds) =>
hoelzl@31412
   141
                      (hd (Variable.export ctxt' ctxt [(forall_intr (cert x) th) COMP allI]),
hoelzl@31412
   142
                       let val (bsT,asT) = the(AList.lookup Type.could_unify bds (HOLogic.listT xT))
hoelzl@31412
   143
		       in AList.update Type.could_unify (HOLogic.listT xT,(tl bsT,asT)) bds
hoelzl@31412
   144
		       end)),
hoelzl@31412
   145
                   bds)
hoelzl@31386
   146
               end)
hoelzl@31412
   147
           | _ => da (s,ctxt) bds)
hoelzl@31386
   148
      in (case cgns of
hoelzl@31412
   149
          [] => tryabsdecomp (t,ctxt) bds
hoelzl@31386
   150
        | ((vns,cong)::congs) => ((let
hoelzl@31386
   151
            val cert = cterm_of thy
hoelzl@31386
   152
            val certy = ctyp_of thy
hoelzl@31386
   153
            val (tyenv, tmenv) =
hoelzl@31386
   154
              Pattern.match thy
hoelzl@31386
   155
              ((fst o HOLogic.dest_eq o HOLogic.dest_Trueprop) (concl_of cong), t)
hoelzl@31386
   156
              (Envir.type_env (Envir.empty 0), Vartab.empty)
hoelzl@31386
   157
            val (fnvs,invs) = List.partition (fn ((vn,_),_) => vn mem vns) (Vartab.dest tmenv)
hoelzl@31386
   158
            val (fts,its) =
hoelzl@31386
   159
	      (map (snd o snd) fnvs,
hoelzl@31386
   160
               map (fn ((vn,vi),(tT,t)) => (cert(Var ((vn,vi),tT)), cert t)) invs)
hoelzl@31386
   161
	    val ctyenv = map (fn ((vn,vi),(s,ty)) => (certy (TVar((vn,vi),s)), certy ty)) (Vartab.dest tyenv)
hoelzl@31412
   162
          in ((fts ~~ (replicate (length fts) ctxt),
hoelzl@31412
   163
               Library.apfst (FWD (instantiate (ctyenv, its) cong))), bds)
hoelzl@31386
   164
          end)
hoelzl@31412
   165
        handle MATCH => decomp_genreif da congs (t,ctxt) bds))
hoelzl@31386
   166
      end;
chaieb@23648
   167
chaieb@23648
   168
 (* looks for the atoms equation and instantiates it with the right number *)
hoelzl@31412
   169
    fun mk_decompatom eqs (t,ctxt) bds = (([], fn (_, bds) =>
hoelzl@31386
   170
      let
hoelzl@31386
   171
        val tT = fastype_of t
hoelzl@31386
   172
        fun isat eq =
hoelzl@31386
   173
          let
hoelzl@31386
   174
            val rhs = eq |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd
hoelzl@31386
   175
          in exists_Const
hoelzl@31412
   176
	    (fn (n,ty) => n = @{const_name "List.nth"}
hoelzl@31386
   177
                          andalso
hoelzl@31387
   178
			  AList.defined Type.could_unify bds (domain_type ty)) rhs
hoelzl@31386
   179
            andalso Type.could_unify (fastype_of rhs, tT)
hoelzl@31386
   180
          end
hoelzl@31386
   181
hoelzl@31386
   182
        fun get_nths t acc =
hoelzl@31386
   183
          case t of
hoelzl@31412
   184
            Const(@{const_name "List.nth"},_)$vs$n => insert (fn ((a,_),(b,_)) => a aconv b) (t,(vs,n)) acc
hoelzl@31386
   185
          | t1$t2 => get_nths t1 (get_nths t2 acc)
hoelzl@31386
   186
          | Abs(_,_,t') => get_nths t'  acc
hoelzl@31386
   187
          | _ => acc
chaieb@23548
   188
hoelzl@31386
   189
        fun
hoelzl@31412
   190
           tryeqs [] bds = error "Can not find the atoms equation"
hoelzl@31412
   191
         | tryeqs (eq::eqs) bds = ((
hoelzl@31386
   192
          let
hoelzl@31386
   193
            val rhs = eq |> prop_of |> HOLogic.dest_Trueprop  |> HOLogic.dest_eq |> snd
hoelzl@31386
   194
            val nths = get_nths rhs []
hoelzl@31386
   195
            val (vss,ns) = fold_rev (fn (_,(vs,n)) => fn (vss,ns) =>
hoelzl@31386
   196
                                      (insert (op aconv) vs vss, insert (op aconv) n ns)) nths ([],[])
hoelzl@31386
   197
            val (vsns, ctxt') = Variable.variant_fixes (replicate (length vss) "vs") ctxt
hoelzl@31386
   198
            val (xns, ctxt'') = Variable.variant_fixes (replicate (length nths) "x") ctxt'
hoelzl@31386
   199
            val thy = ProofContext.theory_of ctxt''
hoelzl@31386
   200
            val cert = cterm_of thy
hoelzl@31386
   201
            val certT = ctyp_of thy
hoelzl@31386
   202
            val vsns_map = vss ~~ vsns
hoelzl@31386
   203
            val xns_map = (fst (split_list nths)) ~~ xns
hoelzl@31386
   204
            val subst = map (fn (nt, xn) => (nt, Var ((xn,0), fastype_of nt))) xns_map
hoelzl@31386
   205
            val rhs_P = subst_free subst rhs
hoelzl@31386
   206
            val (tyenv, tmenv) = Pattern.match
hoelzl@31386
   207
                              thy (rhs_P, t)
hoelzl@31386
   208
                              (Envir.type_env (Envir.empty 0), Vartab.empty)
hoelzl@31386
   209
            val sbst = Envir.subst_vars (tyenv, tmenv)
hoelzl@31386
   210
            val sbsT = Envir.typ_subst_TVars tyenv
hoelzl@31386
   211
            val subst_ty = map (fn (n,(s,t)) => (certT (TVar (n, s)), certT t))
hoelzl@31386
   212
                               (Vartab.dest tyenv)
hoelzl@31386
   213
            val tml = Vartab.dest tmenv
hoelzl@31412
   214
            val t's = map (fn xn => snd (the (AList.lookup (op =) tml (xn,0)))) xns (* FIXME : Express with sbst*)
hoelzl@31412
   215
            val (subst_ns, bds) = fold_map
hoelzl@31412
   216
                (fn (Const _ $ vs $ n, Var (xn0,T)) => fn bds =>
hoelzl@31387
   217
                  let
hoelzl@31412
   218
                    val name = snd (the (AList.lookup (op =) tml xn0))
hoelzl@31412
   219
                    val (idx, bds) = index_of name bds
hoelzl@31412
   220
                  in ((cert n, idx |> (HOLogic.mk_nat #> cert)), bds) end) subst bds
hoelzl@31386
   221
            val subst_vs =
hoelzl@31386
   222
              let
hoelzl@31386
   223
                fun ty (Const _ $ (vs as Var (vsn,lT)) $ n, Var (xn0,T)) = (certT T, certT (sbsT T))
hoelzl@31386
   224
                fun h (Const _ $ (vs as Var (vsn,lT)) $ n, Var (xn0,T)) =
hoelzl@31386
   225
                  let
hoelzl@31412
   226
                    val cns = sbst (Const(@{const_name "List.Cons"}, T --> lT --> lT))
hoelzl@31386
   227
                    val lT' = sbsT lT
hoelzl@31387
   228
                    val (bsT,asT) = the (AList.lookup Type.could_unify bds lT)
hoelzl@31412
   229
                    val vsn = the (AList.lookup (op =) vsns_map vs)
hoelzl@31386
   230
                    val cvs = cert (fold_rev (fn x => fn xs => cns$x$xs) bsT (Free (vsn, lT')))
hoelzl@31386
   231
                  in (cert vs, cvs) end
hoelzl@31386
   232
              in map h subst end
hoelzl@31386
   233
            val cts = map (fn ((vn,vi),(tT,t)) => (cert(Var ((vn,vi),tT)), cert t))
hoelzl@31386
   234
                          (fold (AList.delete (fn (((a: string),_),(b,_)) => a = b))
hoelzl@31386
   235
                                (map (fn n => (n,0)) xns) tml)
hoelzl@31386
   236
            val substt =
hoelzl@31386
   237
              let val ih = Drule.cterm_rule (Thm.instantiate (subst_ty,[]))
hoelzl@31386
   238
              in map (fn (v,t) => (ih v, ih t)) (subst_ns@subst_vs@cts)  end
hoelzl@31386
   239
            val th = (instantiate (subst_ty, substt)  eq) RS sym
hoelzl@31412
   240
          in (hd (Variable.export ctxt'' ctxt [th]), bds) end)
hoelzl@31412
   241
          handle MATCH => tryeqs eqs bds)
hoelzl@31412
   242
      in tryeqs (filter isat eqs) bds end), bds);
chaieb@20374
   243
wenzelm@20319
   244
  (* Generic reification procedure: *)
wenzelm@20319
   245
  (* creates all needed cong rules and then just uses the theorem synthesis *)
chaieb@20564
   246
hoelzl@31386
   247
    fun mk_congs ctxt raw_eqs =
hoelzl@31386
   248
      let
hoelzl@31386
   249
        val fs = fold_rev (fn eq =>
hoelzl@31386
   250
                           insert (op =) (eq |> prop_of |> HOLogic.dest_Trueprop
hoelzl@31386
   251
                           |> HOLogic.dest_eq |> fst |> strip_comb
hoelzl@31386
   252
                           |> fst)) raw_eqs []
hoelzl@31386
   253
        val tys = fold_rev (fn f => fold (insert (op =)) (f |> fastype_of |> binder_types |> tl)
hoelzl@31386
   254
                            ) fs []
hoelzl@31386
   255
        val (vs, ctxt') = Variable.variant_fixes (replicate (length tys) "vs") ctxt
hoelzl@31386
   256
        val thy = ProofContext.theory_of ctxt'
hoelzl@31386
   257
        val cert = cterm_of thy
hoelzl@31386
   258
        val vstys = map (fn (t,v) => (t,SOME (cert (Free(v,t)))))
hoelzl@31386
   259
                    (tys ~~ vs)
hoelzl@31386
   260
        val is_Var = can dest_Var
hoelzl@31386
   261
        fun insteq eq vs =
hoelzl@31386
   262
          let
hoelzl@31412
   263
            val subst = map (fn (v as Var(n,t)) => (cert v, (the o the) (AList.lookup (op =) vstys t)))
hoelzl@31386
   264
                        (filter is_Var vs)
hoelzl@31386
   265
          in Thm.instantiate ([],subst) eq
hoelzl@31386
   266
          end
chaieb@20564
   267
hoelzl@31387
   268
        val bds = AList.make (fn _ => ([],[])) tys
hoelzl@31386
   269
        val eqs = map (fn eq => eq |> prop_of |> HOLogic.dest_Trueprop
hoelzl@31386
   270
  	                           |> HOLogic.dest_eq |> fst |> strip_comb |> snd |> tl
hoelzl@31386
   271
                                   |> (insteq eq)) raw_eqs
hoelzl@31386
   272
        val (ps,congs) = split_list (map (mk_congeq ctxt' fs) eqs)
hoelzl@31412
   273
      in (ps ~~ (Variable.export ctxt' ctxt congs), bds)
hoelzl@31386
   274
      end
hoelzl@31386
   275
hoelzl@31412
   276
    val (congs, bds) = mk_congs ctxt raw_eqs
hoelzl@31387
   277
    val congs = rearrange congs
hoelzl@31412
   278
    val (th, bds) = divide_and_conquer' (decomp_genreif (mk_decompatom raw_eqs) congs) (t,ctxt) bds
hoelzl@31386
   279
    fun is_listVar (Var (_,t)) = can dest_listT t
hoelzl@31386
   280
         | is_listVar _ = false
hoelzl@31386
   281
    val vars = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd
hoelzl@31386
   282
	          |> strip_comb |> snd |> filter is_listVar
hoelzl@31386
   283
    val cert = cterm_of (ProofContext.theory_of ctxt)
hoelzl@31386
   284
    val cvs = map (fn (v as Var(n,t)) => (cert v,
hoelzl@31387
   285
                  the (AList.lookup Type.could_unify bds t) |> snd |> HOLogic.mk_list (dest_listT t) |> cert)) vars
hoelzl@31386
   286
    val th' = instantiate ([], cvs) th
hoelzl@31386
   287
    val t' = (fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) th'
hoelzl@31386
   288
    val th'' = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq (t, t')))
hoelzl@31386
   289
	       (fn _ => simp_tac (local_simpset_of ctxt) 1)
hoelzl@31386
   290
  in FWD trans [th'',th']
hoelzl@31386
   291
  end
wenzelm@20319
   292
chaieb@23648
   293
chaieb@23648
   294
fun genreflect ctxt conv corr_thms raw_eqs t =
hoelzl@31386
   295
  let
hoelzl@31386
   296
    val reifth = genreif ctxt raw_eqs t
hoelzl@31386
   297
    fun trytrans [] = error "No suitable correctness theorem found"
hoelzl@31386
   298
      | trytrans (th::ths) =
hoelzl@31386
   299
           (FWD trans [reifth, th RS sym] handle THM _ => trytrans ths)
hoelzl@31386
   300
    val th = trytrans corr_thms
hoelzl@31386
   301
    val ft = (Thm.dest_arg1 o Thm.dest_arg o Thm.dest_arg o cprop_of) th
hoelzl@31386
   302
    val rth = conv ft
hoelzl@31386
   303
  in simplify (HOL_basic_ss addsimps raw_eqs addsimps [nth_Cons_0, nth_Cons_Suc])
hoelzl@31386
   304
             (simplify (HOL_basic_ss addsimps [rth]) th)
hoelzl@31386
   305
  end
wenzelm@20319
   306
wenzelm@20319
   307
fun genreify_tac ctxt eqs to i = (fn st =>
wenzelm@20319
   308
  let
hoelzl@31412
   309
    fun P () = HOLogic.dest_Trueprop (nth (prems_of st) (i - 1))
hoelzl@29805
   310
    val t = (case to of NONE => P () | SOME x => x)
wenzelm@20319
   311
    val th = (genreif ctxt eqs t) RS ssubst
wenzelm@20319
   312
  in rtac th i st
wenzelm@20319
   313
  end);
wenzelm@20319
   314
wenzelm@20319
   315
    (* Reflection calls reification and uses the correctness *)
wenzelm@20319
   316
        (* theorem assumed to be the dead of the list *)
chaieb@23648
   317
fun gen_reflection_tac ctxt conv corr_thms raw_eqs to i = (fn st =>
haftmann@21878
   318
  let
haftmann@21878
   319
    val P = HOLogic.dest_Trueprop (nth (prems_of st) (i - 1));
haftmann@21878
   320
    val t = the_default P to;
chaieb@23648
   321
    val th = genreflect ctxt conv corr_thms raw_eqs t
haftmann@21878
   322
      RS ssubst;
nipkow@23791
   323
  in (rtac th i THEN TRY(rtac TrueI i)) st end);
haftmann@21878
   324
nipkow@23791
   325
fun reflection_tac ctxt = gen_reflection_tac ctxt Codegen.evaluation_conv;
haftmann@30969
   326
  (*FIXME why Codegen.evaluation_conv?  very specific...*)
haftmann@29650
   327
wenzelm@20797
   328
end