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