+\begin{code}
+splitHsInstDeclTy
+ :: Outputable name
+ => HsType name
+ -> ([HsTyVarBndr name], HsContext name, name, [HsType name])
+ -- Split up an instance decl type, returning the pieces
+
+-- In interface files, the instance declaration head is created
+-- by HsTypes.toHsType, which does not guarantee to produce a
+-- HsForAllTy. For example, if we had the weird decl
+-- instance Foo T => Foo [T]
+-- then we'd get the instance type
+-- Foo T -> Foo [T]
+-- So when colleting the instance context, to be on the safe side
+-- we gather predicate arguments
+--
+-- For source code, the parser ensures the type will have the right shape.
+-- (e.g. see ParseUtil.checkInstType)
+
+splitHsInstDeclTy inst_ty
+ = case inst_ty of
+ HsForAllTy (Just tvs) cxt1 tau
+ -> (tvs, cxt1++cxt2, cls, tys)
+ where
+ (cxt2, cls, tys) = split_tau tau
+
+ other -> ([], cxt2, cls, tys)
+ where
+ (cxt2, cls, tys) = split_tau inst_ty
+
+ where
+ split_tau (HsFunTy (HsPredTy p) ty) = (p:ps, cls, tys)
+ where
+ (ps, cls, tys) = split_tau ty
+ split_tau (HsPredTy (HsClassP cls tys)) = ([], cls,tys)
+ split_tau other = pprPanic "splitHsInstDeclTy" (ppr inst_ty)
+\end{code}
+
+