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