src/HOL/ex/reflection.ML
author wenzelm
Sat Sep 30 21:39:17 2006 +0200 (2006-09-30)
changeset 20797 c1f0bc7e7d80
parent 20595 db6bedfba498
child 20853 3ff5a2e05810
permissions -rw-r--r--
renamed Variable.invent_fixes to Variable.variant_fixes;
wenzelm@20319
     1
(*
wenzelm@20319
     2
    ID:         $Id$
wenzelm@20319
     3
    Author:     Amine Chaieb, TU Muenchen
wenzelm@20319
     4
wenzelm@20319
     5
A trial for automatical reification.
wenzelm@20319
     6
*)
wenzelm@20319
     7
wenzelm@20319
     8
signature REFLECTION = sig
wenzelm@20319
     9
  val genreify_tac: Proof.context -> thm list -> term option -> int -> tactic
wenzelm@20319
    10
  val reflection_tac: Proof.context -> thm list -> term option -> int -> tactic
wenzelm@20319
    11
end;
wenzelm@20319
    12
wenzelm@20319
    13
structure Reflection : REFLECTION
wenzelm@20319
    14
= struct
wenzelm@20319
    15
chaieb@20374
    16
val ext2 = thm "ext2";
chaieb@20374
    17
  (* Make a congruence rule out of a defining equation for the interpretation *)
chaieb@20374
    18
  (* th is one defining equation of f, i.e.
chaieb@20374
    19
     th is "f (Cp ?t1 ... ?tn) = P(f ?t1, .., f ?tn)" *)
chaieb@20374
    20
  (* Cp is a constructor pattern and P is a pattern *)
chaieb@20374
    21
chaieb@20374
    22
  (* The result is:
chaieb@20374
    23
      [|?A1 = f ?t1 ; .. ; ?An= f ?tn |] ==> P (?A1, .., ?An) = f (Cp ?t1 .. ?tn) *)
chaieb@20374
    24
  (*  + the a list of names of the A1 .. An, Those are fresh in the ctxt*)
chaieb@20374
    25
chaieb@20374
    26
chaieb@20374
    27
fun mk_congeq ctxt fs th = 
chaieb@20374
    28
  let 
chaieb@20564
    29
   val (f as Const(fN,fT)) = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq 
chaieb@20564
    30
				|> fst |> strip_comb |> fst
chaieb@20374
    31
   val thy = ProofContext.theory_of ctxt
chaieb@20374
    32
   val cert = Thm.cterm_of thy
chaieb@20374
    33
   val (((_,_),[th']), ctxt') = Variable.import true [th] ctxt
chaieb@20374
    34
   val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop (Thm.prop_of th'))
chaieb@20564
    35
   fun add_fterms (t as t1 $ t2) = 
chaieb@20564
    36
       if exists (fn f => (t |> strip_comb |> fst) aconv f) fs then insert (op aconv) t
chaieb@20564
    37
       else add_fterms t1 #> add_fterms t2
chaieb@20374
    38
     | add_fterms (t as Abs(xn,xT,t')) = 
chaieb@20564
    39
       if (fN mem (term_consts t)) then (fn _ => [t]) else (fn _ => [])
chaieb@20374
    40
     | add_fterms _ = I
chaieb@20374
    41
   val fterms = add_fterms rhs []
wenzelm@20797
    42
   val (xs, ctxt'') = Variable.variant_fixes (replicate (length fterms) "x") ctxt'
chaieb@20374
    43
   val tys = map fastype_of fterms
chaieb@20374
    44
   val vs = map Free (xs ~~ tys)
chaieb@20374
    45
   val env = fterms ~~ vs
chaieb@20564
    46
		    (* FIXME!!!!*)	
chaieb@20564
    47
   fun replace_fterms (t as t1 $ t2) =
chaieb@20374
    48
       (case AList.lookup (op aconv) env t of
chaieb@20374
    49
	    SOME v => v
chaieb@20564
    50
	  | NONE => replace_fterms t1 $ replace_fterms t2)
chaieb@20374
    51
     | replace_fterms t = (case AList.lookup (op aconv) env t of
chaieb@20374
    52
			       SOME v => v
chaieb@20374
    53
			     | NONE => t)
chaieb@20374
    54
      
chaieb@20374
    55
   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
    56
     | mk_def (t, v) = HOLogic.mk_Trueprop (HOLogic.mk_eq (v, t))
chaieb@20374
    57
   fun tryext x = (x RS ext2 handle _ =>  x)
chaieb@20374
    58
   val cong = (Goal.prove ctxt'' [] (map mk_def env)
chaieb@20374
    59
			  (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, replace_fterms rhs)))
chaieb@20374
    60
			  (fn x => LocalDefs.unfold_tac (#context x) (map tryext (#prems x)) 
chaieb@20374
    61
							THEN rtac th' 1)) RS sym
chaieb@20374
    62
	      
chaieb@20374
    63
   val (cong' :: vars') = 
chaieb@20374
    64
       Variable.export ctxt'' ctxt (cong :: map (Drule.mk_term o cert) vs)
chaieb@20374
    65
   val vs' = map (fst o fst o Term.dest_Var o Thm.term_of o Drule.dest_term) vars'
chaieb@20374
    66
					      
chaieb@20374
    67
  in  (vs', cong') end; 
wenzelm@20319
    68
 (* congs is a list of pairs (P,th) where th is a theorem for *)
wenzelm@20319
    69
        (* [| f p1 = A1; ...; f pn = An|] ==> f (C p1 .. pn) = P *)
wenzelm@20319
    70
val FWD = curry (op OF);
wenzelm@20319
    71
wenzelm@20319
    72
 (* da is the decomposition for atoms, ie. it returns ([],g) where g
wenzelm@20319
    73
 returns the right instance f (AtC n) = t , where AtC is the Atoms
wenzelm@20319
    74
 constructor and n is the number of the atom corresponding to t *)
wenzelm@20319
    75
wenzelm@20319
    76
(* Generic decomp for reification : matches the actual term with the
wenzelm@20319
    77
rhs of one cong rule. The result of the matching guides the
wenzelm@20319
    78
proof synthesis: The matches of the introduced Variables A1 .. An are
wenzelm@20319
    79
processed recursively
wenzelm@20319
    80
 The rest is instantiated in the cong rule,i.e. no reification is needed *)
wenzelm@20319
    81
chaieb@20374
    82
exception REIF of string;
chaieb@20374
    83
chaieb@20564
    84
val bds = ref ([]: (typ * ((term list) * (term list))) list);
chaieb@20564
    85
chaieb@20564
    86
fun index_of t = 
chaieb@20564
    87
 let 
chaieb@20564
    88
  val tt = HOLogic.listT (fastype_of t)
chaieb@20564
    89
 in 
chaieb@20564
    90
  (case AList.lookup (op =) (!bds) tt of
chaieb@20564
    91
    NONE => error "index_of : type not found in environements!"
chaieb@20564
    92
  | SOME (tbs,tats) =>
chaieb@20564
    93
    let
chaieb@20564
    94
     val i = find_index_eq t tats
chaieb@20564
    95
     val j = find_index_eq t tbs 
chaieb@20564
    96
    in (if j= ~1 then 
chaieb@20564
    97
	    if i= ~1 
chaieb@20564
    98
	    then (bds := AList.update (op =) (tt,(tbs,tats@[t])) (!bds) ; 
chaieb@20564
    99
		  length tbs + length tats) 
chaieb@20564
   100
	    else i else j)
chaieb@20564
   101
    end)
chaieb@20564
   102
 end;
chaieb@20564
   103
chaieb@20564
   104
fun dest_listT (Type ("List.list", [T])) = T;
chaieb@20374
   105
chaieb@20374
   106
fun decomp_genreif da cgns (t,ctxt) =
chaieb@20564
   107
 let 
chaieb@20564
   108
  val thy = ProofContext.theory_of ctxt 
chaieb@20564
   109
  val cert = cterm_of thy
chaieb@20564
   110
  fun tryabsdecomp (s,ctxt) = 
chaieb@20564
   111
   (case s of 
chaieb@20564
   112
     Abs(xn,xT,ta) => 
chaieb@20564
   113
     (let
wenzelm@20797
   114
       val ([xn],ctxt') = Variable.variant_fixes ["x"] ctxt
chaieb@20564
   115
       val (xn,ta) = variant_abs (xn,xT,ta)
chaieb@20564
   116
       val x = Free(xn,xT)
chaieb@20564
   117
       val _ = (case AList.lookup (op =) (!bds) (HOLogic.listT xT)
chaieb@20564
   118
		 of NONE => error "tryabsdecomp: Type not found in the Environement"
chaieb@20564
   119
		  | SOME (bsT,atsT) => 
chaieb@20564
   120
		    (bds := AList.update (op =) (HOLogic.listT xT, ((x::bsT), atsT)) (!bds)))
chaieb@20564
   121
      in ([(ta, ctxt')] , 
chaieb@20564
   122
	  fn [th] => ((let val (bsT,asT) = the(AList.lookup (op =) (!bds) (HOLogic.listT xT))
chaieb@20564
   123
		       in (bds := AList.update (op =) (HOLogic.listT xT,(tl bsT,asT)) (!bds))
chaieb@20564
   124
		       end) ; 
chaieb@20564
   125
		      hd (Variable.export ctxt' ctxt [(forall_intr (cert x) th) COMP allI])))
chaieb@20374
   126
	end)
chaieb@20374
   127
    | _ => da (s,ctxt))
chaieb@20374
   128
  in 
chaieb@20374
   129
  (case cgns of 
chaieb@20374
   130
    [] => tryabsdecomp (t,ctxt)
chaieb@20374
   131
  | ((vns,cong)::congs) => ((let
chaieb@20374
   132
        val cert = cterm_of thy
chaieb@20564
   133
	val certy = ctyp_of thy
chaieb@20564
   134
        val (tyenv, tmenv) =
wenzelm@20319
   135
        Pattern.match thy
wenzelm@20319
   136
        ((fst o HOLogic.dest_eq o HOLogic.dest_Trueprop) (concl_of cong), t)
wenzelm@20319
   137
        (Envir.type_env (Envir.empty 0),Term.Vartab.empty)
wenzelm@20319
   138
        val (fnvs,invs) = List.partition (fn ((vn,_),_) => vn mem vns) (Vartab.dest tmenv)
chaieb@20564
   139
        val (fts,its) = 
chaieb@20564
   140
	    (map (snd o snd) fnvs,
chaieb@20564
   141
             map (fn ((vn,vi),(tT,t)) => (cert(Var ((vn,vi),tT)), cert t)) invs)
chaieb@20564
   142
	val ctyenv = map (fn ((vn,vi),(s,ty)) => (certy (TVar((vn,vi),s)), certy ty)) (Vartab.dest tyenv)
chaieb@20564
   143
    in (fts ~~ (replicate (length fts) ctxt), FWD (instantiate (ctyenv, its) cong))
wenzelm@20319
   144
    end)
chaieb@20564
   145
      handle MATCH => decomp_genreif da congs (t,ctxt)))
chaieb@20564
   146
  end;
wenzelm@20319
   147
          (* looks for the atoms equation and instantiates it with the right number *)
chaieb@20374
   148
chaieb@20374
   149
fun mk_decompatom eqs (t,ctxt) =
chaieb@20564
   150
 let 
chaieb@20564
   151
  val tT = fastype_of t
chaieb@20564
   152
  fun isat eq = 
chaieb@20564
   153
   let 
chaieb@20564
   154
    val rhs = eq |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd
chaieb@20564
   155
   in exists_Const 
chaieb@20564
   156
	  (fn (n,ty) => n="List.nth" 
chaieb@20564
   157
			andalso 
chaieb@20564
   158
			AList.defined (op =) (!bds) (domain_type ty)) rhs 
chaieb@20564
   159
	  andalso fastype_of rhs = tT
chaieb@20564
   160
   end
chaieb@20564
   161
  fun get_nth t = 
chaieb@20564
   162
   case t of
chaieb@20564
   163
     Const("List.nth",_)$vs$n => (t,vs,n)
chaieb@20564
   164
   | t1$t2 => (get_nth t1 handle REIF "get_nth" => get_nth t2)
chaieb@20564
   165
   | Abs(_,_,t') => get_nth t'
chaieb@20564
   166
   | _ => raise REIF "get_nth"
wenzelm@20797
   167
  val ([xn,vsn],ctxt') = Variable.variant_fixes ["x","vs"] ctxt
chaieb@20564
   168
  val thy = ProofContext.theory_of ctxt'
chaieb@20564
   169
  val cert = cterm_of thy
chaieb@20564
   170
  fun tryeqs [] = raise REIF "Can not find the atoms equation"
chaieb@20564
   171
    | tryeqs (eq::eqs) = ((
chaieb@20564
   172
      let 
chaieb@20564
   173
       val rhs = eq |> prop_of |> HOLogic.dest_Trueprop 
chaieb@20564
   174
			  |> HOLogic.dest_eq |> snd
chaieb@20564
   175
       val (nt,vs,n) = get_nth rhs
chaieb@20564
   176
       val ntT = fastype_of nt
chaieb@20564
   177
       val ntlT = HOLogic.listT ntT
chaieb@20564
   178
       val (bsT,asT) = the (AList.lookup (op =) (!bds) ntlT) 
chaieb@20564
   179
       val x = Var ((xn,0),ntT)
chaieb@20564
   180
       val rhs_P = subst_free [(nt,x)] rhs
chaieb@20564
   181
       val (_, tmenv) = Pattern.match 
chaieb@20564
   182
			    thy (rhs_P, t)
chaieb@20564
   183
			    (Envir.type_env (Envir.empty 0),Term.Vartab.empty)
chaieb@20564
   184
       val tml = Vartab.dest tmenv
chaieb@20564
   185
       val SOME (_,t') = AList.lookup (op =) tml (xn,0)
chaieb@20564
   186
       val cvs = 
chaieb@20564
   187
	   cert (foldr (fn (x,xs) => Const("List.list.Cons", ntT --> ntlT --> ntlT)$x$xs) 
chaieb@20564
   188
		       (Free(vsn,ntlT)) bsT)
chaieb@20564
   189
       val cts = map (fn ((vn,vi),(tT,t)) => (cert(Var ((vn,vi),tT)), cert t)) 
chaieb@20564
   190
		     (AList.delete (op =) (xn,0) tml)
chaieb@20564
   191
       val th = (instantiate 
chaieb@20564
   192
		     ([],
chaieb@20564
   193
		      [(cert vs, cvs),(cert n, t' |> index_of |> HOLogic.mk_nat |> cert)]
chaieb@20564
   194
		      @cts) eq) RS sym
chaieb@20564
   195
      in hd (Variable.export ctxt' ctxt [th])
chaieb@20564
   196
      end)
chaieb@20564
   197
	  handle MATCH => tryeqs eqs)
chaieb@20564
   198
 in ([], fn _ => tryeqs (filter isat eqs))
chaieb@20564
   199
 end;
chaieb@20374
   200
chaieb@20564
   201
(* 
chaieb@20564
   202
fun mk_decompatom eqs (t,ctxt) =
chaieb@20564
   203
 let 
chaieb@20564
   204
  val tT = fastype_of t
chaieb@20564
   205
  val tlT = HOLogic.listT tT
chaieb@20564
   206
  val (bsT,asT) = (the (AList.lookup (op =) (!bds) tlT) 
chaieb@20564
   207
		   handle Option => error "mk_decompatom: Type not found in the env.")
chaieb@20564
   208
  fun isateq (_$_$(Const("List.nth",_)$vs$_)) = (fastype_of vs = tlT)
chaieb@20564
   209
    | isateq _ = false
chaieb@20564
   210
 in case List.find (isateq o HOLogic.dest_Trueprop o prop_of) eqs of
chaieb@20564
   211
     NONE => raise REIF "Can not find the atoms equation"
chaieb@20564
   212
   | SOME th =>
chaieb@20564
   213
     ([],
chaieb@20564
   214
      fn ths =>
chaieb@20564
   215
        let 
wenzelm@20797
   216
         val ([x], ctxt') = Variable.variant_fixes ["vs"] ctxt
chaieb@20564
   217
	 val cert = cterm_of (ProofContext.theory_of ctxt')
chaieb@20564
   218
         val (Const("List.nth",_)$(vs as Var((vsn,vsi),_))$n) = 
chaieb@20564
   219
             (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) th
chaieb@20564
   220
         val cvs = 
chaieb@20564
   221
	     cert (foldr (fn (x,xs) => Const("List.list.Cons", tT --> tlT --> tlT)$x$xs) 
chaieb@20564
   222
			 (Free(x,tlT)) bsT)
chaieb@20564
   223
	 val th' = (instantiate ([],
chaieb@20564
   224
				 [(cert vs, cvs), 
chaieb@20564
   225
				  (cert n, cert  (HOLogic.mk_nat(index_of t)))]) th)
chaieb@20564
   226
		       RS sym
chaieb@20564
   227
        in hd (Variable.export ctxt' ctxt [th']) end)
chaieb@20564
   228
 end;
chaieb@20374
   229
*)
wenzelm@20319
   230
  (* Generic reification procedure: *)
wenzelm@20319
   231
  (* creates all needed cong rules and then just uses the theorem synthesis *)
chaieb@20564
   232
chaieb@20564
   233
fun mk_congs ctxt raw_eqs = 
chaieb@20564
   234
 let 
chaieb@20564
   235
  val fs = foldr (fn (eq,fns) => 
chaieb@20564
   236
		     (eq |> prop_of |> HOLogic.dest_Trueprop 
chaieb@20564
   237
			 |> HOLogic.dest_eq |> fst |> strip_comb 
chaieb@20564
   238
			 |> fst) ins fns) [] raw_eqs
chaieb@20564
   239
  val tys = foldr (fn (f,ts) => (f |> fastype_of |> binder_types |> split_last |> fst) 
chaieb@20564
   240
				    union ts) [] fs
chaieb@20564
   241
  val _ = bds := AList.make (fn _ => ([],[])) tys
wenzelm@20797
   242
  val (vs, ctxt') = Variable.variant_fixes (replicate (length tys) "vs") ctxt
chaieb@20564
   243
  val thy = ProofContext.theory_of ctxt'
chaieb@20564
   244
  val cert = cterm_of thy
chaieb@20564
   245
  val vstys = map (fn (t,v) => (t,SOME (cert (Free(v,t))))) 
chaieb@20564
   246
		  (tys ~~ vs)
chaieb@20564
   247
  fun insteq eq ts = 
chaieb@20564
   248
   let val itms = map (fn t => t|> (AList.lookup (op =) vstys) |> the) ts
chaieb@20564
   249
   in instantiate' [] itms eq
chaieb@20564
   250
   end
chaieb@20564
   251
  val eqs = map (fn eq => eq |> prop_of |> HOLogic.dest_Trueprop 
chaieb@20564
   252
			     |> HOLogic.dest_eq |> fst |> strip_comb |> fst |> fastype_of 
chaieb@20564
   253
			     |> binder_types |> split_last |> fst 
chaieb@20564
   254
			     |> (insteq eq)) raw_eqs
chaieb@20564
   255
  val (ps,congs) = split_list (map (mk_congeq ctxt' fs) eqs)
chaieb@20564
   256
 in  ps ~~ (Variable.export ctxt' ctxt congs)
chaieb@20564
   257
 end;
chaieb@20564
   258
wenzelm@20319
   259
fun genreif ctxt raw_eqs t =
chaieb@20564
   260
 let 
chaieb@20564
   261
  val _ = bds := []
chaieb@20564
   262
  val congs = mk_congs ctxt raw_eqs
chaieb@20564
   263
  val th = divide_and_conquer (decomp_genreif (mk_decompatom raw_eqs) congs) (t,ctxt)
chaieb@20564
   264
  val tys = th |> prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd
chaieb@20564
   265
	       |> strip_comb |> fst |> fastype_of |> strip_type |> fst
chaieb@20564
   266
	       |> split_last |> fst
chaieb@20564
   267
  val cert = cterm_of (ProofContext.theory_of ctxt)
chaieb@20564
   268
  val cvs = map (fn t => t |> (AList.lookup (op =) (!bds)) |> the |> snd 
chaieb@20564
   269
			   |> HOLogic.mk_list I (dest_listT t) |> cert |> SOME)
chaieb@20564
   270
		tys
chaieb@20564
   271
  val th' = (instantiate' [] cvs (th RS sym)) RS sym
chaieb@20564
   272
  val t' = (fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) th'
chaieb@20564
   273
  val th'' = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq (t, t')))
chaieb@20564
   274
			(fn _ => Simp_tac 1)
chaieb@20564
   275
  val _ = bds := []
chaieb@20564
   276
 in FWD trans [th'',th']
chaieb@20564
   277
 end;
wenzelm@20319
   278
wenzelm@20319
   279
fun genreflect ctxt corr_thm raw_eqs t =
wenzelm@20319
   280
    let val th = FWD trans [genreif ctxt raw_eqs t, corr_thm RS sym]
wenzelm@20319
   281
        val ft = (snd o Thm.dest_comb o snd o Thm.dest_comb o snd o Thm.dest_comb o cprop_of) th
haftmann@20595
   282
        val rth = NBE.normalization_conv ft
wenzelm@20319
   283
    in simplify (HOL_basic_ss addsimps raw_eqs addsimps [nth_Cons_0, nth_Cons_Suc])
wenzelm@20319
   284
                (simplify (HOL_basic_ss addsimps [rth]) th)
wenzelm@20319
   285
    end
wenzelm@20319
   286
wenzelm@20319
   287
fun genreify_tac ctxt eqs to i = (fn st =>
wenzelm@20319
   288
  let
wenzelm@20319
   289
    val P = HOLogic.dest_Trueprop (List.nth (prems_of st, i - 1))
wenzelm@20319
   290
    val t = (case to of NONE => P | SOME x => x)
wenzelm@20319
   291
    val th = (genreif ctxt eqs t) RS ssubst
wenzelm@20319
   292
  in rtac th i st
wenzelm@20319
   293
  end);
wenzelm@20319
   294
wenzelm@20319
   295
    (* Reflection calls reification and uses the correctness *)
wenzelm@20319
   296
        (* theorem assumed to be the dead of the list *)
wenzelm@20319
   297
 fun reflection_tac ctxt (corr_thm::raw_eqs) to i =
wenzelm@20319
   298
    (fn st =>
wenzelm@20319
   299
        let val P = (HOLogic.dest_Trueprop (List.nth (prems_of st, i - 1)))
wenzelm@20319
   300
            val t = (case to of NONE => P | SOME x => x)
wenzelm@20319
   301
            val th = (genreflect ctxt corr_thm raw_eqs t) RS ssubst
wenzelm@20319
   302
        in rtac th i st end);
wenzelm@20319
   303
wenzelm@20797
   304
end