summaryrefslogtreecommitdiff
path: root/compiler/typecheck/TcType.hs
diff options
context:
space:
mode:
authorSimon Peyton Jones <simonpj@microsoft.com>2018-07-25 11:35:43 +0100
committerSimon Peyton Jones <simonpj@microsoft.com>2018-07-25 12:04:30 +0100
commitc5d31df70b16dc346b5860077c8bbe585ddb7a78 (patch)
treefc62d0e68fe0b7cda4c2073e598996edc8258030 /compiler/typecheck/TcType.hs
parentf7d3054a133247cea1f488993695d3cbb941f29d (diff)
downloadhaskell-c5d31df70b16dc346b5860077c8bbe585ddb7a78.tar.gz
Treat isConstraintKind more consistently
It turned out that we were not being consistent about our use of isConstraintKind. It's delicate, because the typechecker treats Constraint and Type as /distinct/, whereas they are the /same/ in the rest of the compiler (Trac #11715). And had it wrong, which led to Trac #15412. This patch does the following: * Rename isConstraintKind to tcIsConstraintKind returnsConstraintKind to tcReturnsConstraintKind to emphasise that they use the 'tcView' view of types. * Move these functions, and some related ones (tcIsLiftedTypeKind), from Kind.hs, to group together in Type.hs, alongside isPredTy. It feels very unsatisfactory that these 'tcX' functions live in Type, but it happens because isPredTy is called later in the compiler too. But it's a consequence of the 'Constraint vs Type' dilemma.
Diffstat (limited to 'compiler/typecheck/TcType.hs')
-rw-r--r--compiler/typecheck/TcType.hs69
1 files changed, 35 insertions, 34 deletions
diff --git a/compiler/typecheck/TcType.hs b/compiler/typecheck/TcType.hs
index 01bdabebe0..79b0e7fd08 100644
--- a/compiler/typecheck/TcType.hs
+++ b/compiler/typecheck/TcType.hs
@@ -2157,6 +2157,40 @@ isImprovementPred ty
IrredPred {} -> True -- Might have equalities after reduction?
ForAllPred {} -> False
+-- | Is the equality
+-- a ~r ...a....
+-- definitely insoluble or not?
+-- a ~r Maybe a -- Definitely insoluble
+-- a ~N ...(F a)... -- Not definitely insoluble
+-- -- Perhaps (F a) reduces to Int
+-- a ~R ...(N a)... -- Not definitely insoluble
+-- -- Perhaps newtype N a = MkN Int
+-- See Note [Occurs check error] in
+-- TcCanonical for the motivation for this function.
+isInsolubleOccursCheck :: EqRel -> TcTyVar -> TcType -> Bool
+isInsolubleOccursCheck eq_rel tv ty
+ = go ty
+ where
+ go ty | Just ty' <- tcView ty = go ty'
+ go (TyVarTy tv') = tv == tv' || go (tyVarKind tv')
+ go (LitTy {}) = False
+ go (AppTy t1 t2) = case eq_rel of -- See Note [AppTy and ReprEq]
+ NomEq -> go t1 || go t2
+ ReprEq -> go t1
+ go (FunTy t1 t2) = go t1 || go t2
+ go (ForAllTy (TvBndr tv' _) inner_ty)
+ | tv' == tv = False
+ | otherwise = go (tyVarKind tv') || go inner_ty
+ go (CastTy ty _) = go ty -- ToDo: what about the coercion
+ go (CoercionTy _) = False -- ToDo: what about the coercion
+ go (TyConApp tc tys)
+ | isGenerativeTyCon tc role = any go tys
+ | otherwise = any go (drop (tyConArity tc) tys)
+ -- (a ~ F b a), where F has arity 1,
+ -- has an insoluble occurs check
+
+ role = eqRelRole eq_rel
+
{- Note [Expanding superclasses]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When we expand superclasses, we use the following algorithm:
@@ -2241,7 +2275,7 @@ the quantified variables.
************************************************************************
* *
-\subsection{Predicates}
+ Classifying types
* *
************************************************************************
-}
@@ -2343,39 +2377,6 @@ isTyVarHead _ (ForAllTy {}) = False
isTyVarHead _ (FunTy {}) = False
isTyVarHead _ (CoercionTy {}) = False
--- | Is the equality
--- a ~r ...a....
--- definitely insoluble or not?
--- a ~r Maybe a -- Definitely insoluble
--- a ~N ...(F a)... -- Not definitely insoluble
--- -- Perhaps (F a) reduces to Int
--- a ~R ...(N a)... -- Not definitely insoluble
--- -- Perhaps newtype N a = MkN Int
--- See Note [Occurs check error] in
--- TcCanonical for the motivation for this function.
-isInsolubleOccursCheck :: EqRel -> TcTyVar -> TcType -> Bool
-isInsolubleOccursCheck eq_rel tv ty
- = go ty
- where
- go ty | Just ty' <- tcView ty = go ty'
- go (TyVarTy tv') = tv == tv' || go (tyVarKind tv')
- go (LitTy {}) = False
- go (AppTy t1 t2) = case eq_rel of -- See Note [AppTy and ReprEq]
- NomEq -> go t1 || go t2
- ReprEq -> go t1
- go (FunTy t1 t2) = go t1 || go t2
- go (ForAllTy (TvBndr tv' _) inner_ty)
- | tv' == tv = False
- | otherwise = go (tyVarKind tv') || go inner_ty
- go (CastTy ty _) = go ty -- ToDo: what about the coercion
- go (CoercionTy _) = False -- ToDo: what about the coercion
- go (TyConApp tc tys)
- | isGenerativeTyCon tc role = any go tys
- | otherwise = any go (drop (tyConArity tc) tys)
- -- (a ~ F b a), where F has arity 1,
- -- has an insoluble occurs check
-
- role = eqRelRole eq_rel
{- Note [AppTy and ReprEq]
~~~~~~~~~~~~~~~~~~~~~~~~~~