+ Constraints and evidence
+%* *
+%************************************************************************
+
+\begin{code}
+zonkEvTerm :: ZonkEnv -> EvTerm -> TcM EvTerm
+zonkEvTerm env (EvId v) = ASSERT2( isId v, ppr v )
+ return (EvId (zonkIdOcc env v))
+zonkEvTerm env (EvCoercion co) = do { co' <- zonkTcTypeToType env co
+ ; return (EvCoercion co') }
+zonkEvTerm env (EvCast v co) = ASSERT( isId v)
+ do { co' <- zonkTcTypeToType env co
+ ; return (EvCast (zonkIdOcc env v) co') }
+zonkEvTerm env (EvSuperClass d n) = return (EvSuperClass (zonkIdOcc env d) n)
+zonkEvTerm env (EvDFunApp df tys tms)
+ = do { tys' <- zonkTcTypeToTypes env tys
+ ; let tms' = map (zonkEvVarOcc env) tms
+ ; return (EvDFunApp (zonkIdOcc env df) tys' tms') }
+
+zonkTcEvBinds :: ZonkEnv -> TcEvBinds -> TcM (ZonkEnv, TcEvBinds)
+zonkTcEvBinds env (TcEvBinds var) = do { (env', bs') <- zonkEvBindsVar env var
+ ; return (env', EvBinds bs') }
+zonkTcEvBinds env (EvBinds bs) = do { (env', bs') <- zonkEvBinds env bs
+ ; return (env', EvBinds bs') }
+
+zonkEvBindsVar :: ZonkEnv -> EvBindsVar -> TcM (ZonkEnv, Bag EvBind)
+zonkEvBindsVar env (EvBindsVar ref _) = do { bs <- readMutVar ref
+ ; zonkEvBinds env (evBindMapBinds bs) }
+
+zonkEvBinds :: ZonkEnv -> Bag EvBind -> TcM (ZonkEnv, Bag EvBind)
+zonkEvBinds env binds
+ = fixM (\ ~( _, new_binds) -> do
+ { let env1 = extendZonkEnv env (collect_ev_bndrs new_binds)
+ ; binds' <- mapBagM (zonkEvBind env1) binds
+ ; return (env1, binds') })
+ where
+ collect_ev_bndrs :: Bag EvBind -> [EvVar]
+ collect_ev_bndrs = foldrBag add []
+ add (EvBind var _) vars = var : vars
+
+zonkEvBind :: ZonkEnv -> EvBind -> TcM EvBind
+zonkEvBind env (EvBind var term)
+ = do { var' <- zonkEvBndr env var
+ ; term' <- zonkEvTerm env term
+ ; return (EvBind var' term') }
+\end{code}
+
+%************************************************************************
+%* *
+ Zonking types