+
+-- Look up the representation tycon of a family instance.
+-- Return the rep tycon and the corresponding rep args
+tcLookupFamInst :: TyCon -> [Type] -> TcM (TyCon, [Type])
+tcLookupFamInst tycon tys
+ | not (isOpenTyCon tycon)
+ = return (tycon, tys)
+ | otherwise
+ = do { env <- getGblEnv
+ ; eps <- getEps
+ ; let instEnv = (eps_fam_inst_env eps, tcg_fam_inst_env env)
+ ; case lookupFamInstEnv instEnv tycon tys of
+
+ [(subst, fam_inst)] | variable_only_subst ->
+ return (rep_tc, substTyVars subst (tyConTyVars rep_tc))
+ where -- NB: assumption is that (tyConTyVars rep_tc) is in
+ -- the domain of the substitution
+ rep_tc = famInstTyCon fam_inst
+ subst_domain = varEnvElts . getTvSubstEnv $ subst
+ tvs = map (Type.getTyVar "tcLookupFamInst")
+ subst_domain
+ variable_only_subst = all Type.isTyVarTy subst_domain &&
+ sizeVarSet (mkVarSet tvs) == length tvs
+ -- renaming may have no repetitions
+
+ other -> famInstNotFound tycon tys other
+ }