- where ok :: TcType -> Bool
- -- Check that tv is not among the free variables of
- -- the type and that the type is type-family-free.
- ok (TyVarTy tv') = not (tv == tv')
- ok (TyConApp tc tys) = all ok tys && not (isSynFamilyTyCon tc)
- ok (PredTy sty) = ok_pred sty
- ok (FunTy arg res) = ok arg && ok res
- ok (AppTy fun arg) = ok fun && ok arg
- ok (ForAllTy _tv1 ty1) = ok ty1
-
- ok_pred (IParam _ ty) = ok ty
- ok_pred (ClassP _ tys) = all ok tys
- ok_pred (EqPred ty1 ty2) = ok ty1 && ok ty2
+
+ where ok :: TcType -> Maybe TcType
+ ok (TyVarTy tv') | not (tv == tv') = Just (TyVarTy tv')
+ ok this_ty@(TyConApp tc tys)
+ | not (isSynFamilyTyCon tc), Just tys' <- allMaybes (map ok tys)
+ = Just (TyConApp tc tys')
+ | isSynTyCon tc, Just ty_expanded <- tcView this_ty
+ = ok ty_expanded -- See Note [Type synonyms and the occur check]
+ ok (PredTy sty) | Just sty' <- ok_pred sty = Just (PredTy sty')
+ ok (FunTy arg res) | Just arg' <- ok arg, Just res' <- ok res
+ = Just (FunTy arg' res')
+ ok (AppTy fun arg) | Just fun' <- ok fun, Just arg' <- ok arg
+ = Just (AppTy fun' arg')
+ ok (ForAllTy tv1 ty1) | Just ty1' <- ok ty1 = Just (ForAllTy tv1 ty1')
+ -- Fall-through
+ ok _ty = Nothing
+
+ ok_pred (IParam nm ty) | Just ty' <- ok ty = Just (IParam nm ty')
+ ok_pred (ClassP cl tys)
+ | Just tys' <- allMaybes (map ok tys)
+ = Just (ClassP cl tys')
+ ok_pred (EqPred ty1 ty2)
+ | Just ty1' <- ok ty1, Just ty2' <- ok ty2
+ = Just (EqPred ty1' ty2')
+ -- Fall-through
+ ok_pred _pty = Nothing