#include "HsVersions.h"
-import HsSyn ( HsType(..), HsTyVarBndr(..),
+import HsSyn ( HsType(..), HsTyVarBndr(..), HsTyOp(..),
Sig(..), HsPred(..), pprParendHsType, HsTupCon(..), hsTyVarNames )
import RnHsSyn ( RenamedHsType, RenamedHsPred, RenamedContext, RenamedSig, extractHsTyVars )
import TcHsSyn ( TcId )
import TcType ( Type, Kind, SourceType(..), ThetaType, TyVarDetails(..),
TcTyVar, TcKind, TcThetaType, TcTauType,
mkTyVarTy, mkTyVarTys, mkFunTy,
- hoistForAllTys, zipFunTys,
+ zipFunTys, mkForAllTys, mkFunTys, tcEqType, isPredTy,
mkSigmaTy, mkPredTy, mkGenTyConApp, mkTyConApp, mkAppTys,
liftedTypeKind, unliftedTypeKind, mkArrowKind,
- mkArrowKinds, tcSplitFunTy_maybe
+ mkArrowKinds, tcSplitFunTy_maybe, tcSplitForAllTys
)
import Inst ( Inst, InstOrigin(..), newMethodWithGivenTy, instToId )
import Class ( classTyCon )
import Name ( Name )
import NameSet
+import Subst ( deShadowTy )
import TysWiredIn ( mkListTy, mkPArrTy, mkTupleTy, genUnitTyCon )
import BasicTypes ( Boxity(..) )
import SrcLoc ( SrcLoc )
import Util ( lengthIs )
import Outputable
-
+import List ( nubBy )
\end{code}
kcTypeType ty2 `thenTc_`
returnTc liftedTypeKind
-kcHsType (HsNumTy _) -- The unit type for generics
- = returnTc liftedTypeKind
+kcHsType (HsOpTy ty1 HsArrow ty2)
+ = kcTypeType ty1 `thenTc_`
+ kcTypeType ty2 `thenTc_`
+ returnTc liftedTypeKind
-kcHsType ty@(HsOpTy ty1 op ty2)
+kcHsType ty@(HsOpTy ty1 (HsTyOp op) ty2)
= kcTyVar op `thenTc` \ op_kind ->
kcHsType ty1 `thenTc` \ ty1_kind ->
kcHsType ty2 `thenTc` \ ty2_kind ->
tcAddErrCtxt (appKindCtxt (ppr ty)) $
kcAppKind op_kind ty1_kind `thenTc` \ op_kind' ->
kcAppKind op_kind' ty2_kind
+
+kcHsType (HsParTy ty) -- Skip parentheses markers
+ = kcHsType ty
+kcHsType (HsNumTy _) -- The unit type for generics
+ = returnTc liftedTypeKind
+
kcHsType (HsPredTy pred)
= kcHsPred pred `thenTc_`
returnTc liftedTypeKind
tc_type ty2 `thenTc` \ tau_ty2 ->
returnTc (mkFunTy tau_ty1 tau_ty2)
-tc_type (HsNumTy n)
- = ASSERT(n== 1)
- returnTc (mkTyConApp genUnitTyCon [])
+tc_type (HsOpTy ty1 HsArrow ty2)
+ = tc_type ty1 `thenTc` \ tau_ty1 ->
+ tc_type ty2 `thenTc` \ tau_ty2 ->
+ returnTc (mkFunTy tau_ty1 tau_ty2)
-tc_type (HsOpTy ty1 op ty2)
+tc_type (HsOpTy ty1 (HsTyOp op) ty2)
= tc_type ty1 `thenTc` \ tau_ty1 ->
tc_type ty2 `thenTc` \ tau_ty2 ->
tc_fun_type op [tau_ty1,tau_ty2]
+tc_type (HsParTy ty) -- Remove the parentheses markers
+ = tc_type ty
+
+tc_type (HsNumTy n)
+ = ASSERT(n== 1)
+ returnTc (mkTyConApp genUnitTyCon [])
+
tc_type (HsAppTy ty1 ty2) = tc_app ty1 [ty2]
tc_type (HsPredTy pred)
\end{code}
+%************************************************************************
+%* *
+\subsection{Errors and contexts}
+%* *
+%************************************************************************
+
+
+\begin{code}
+hoistForAllTys :: Type -> Type
+-- Used for user-written type signatures only
+-- Move all the foralls and constraints to the top
+-- e.g. T -> forall a. a ==> forall a. T -> a
+-- T -> (?x::Int) -> Int ==> (?x::Int) -> T -> Int
+--
+-- We want to 'look through' type synonyms when doing this
+-- so it's better done on the Type than the HsType
+
+hoistForAllTys ty
+ = let
+ no_shadow_ty = deShadowTy ty
+ -- Running over ty with an empty substitution gives it the
+ -- no-shadowing property. This is important. For example:
+ -- type Foo r = forall a. a -> r
+ -- foo :: Foo (Foo ())
+ -- Here the hoisting should give
+ -- foo :: forall a a1. a -> a1 -> ()
+ --
+ -- What about type vars that are lexically in scope in the envt?
+ -- We simply rely on them having a different unique to any
+ -- binder in 'ty'. Otherwise we'd have to slurp the in-scope-tyvars
+ -- out of the envt, which is boring and (I think) not necessary.
+ in
+ case hoist no_shadow_ty of
+ (tvs, theta, body) -> mkForAllTys tvs (mkFunTys (nubBy tcEqType theta) body)
+ -- The 'nubBy' eliminates duplicate constraints,
+ -- notably implicit parameters
+ where
+ hoist ty
+ | (tvs1, body_ty) <- tcSplitForAllTys ty,
+ not (null tvs1)
+ = case hoist body_ty of
+ (tvs2,theta,tau) -> (tvs1 ++ tvs2, theta, tau)
+
+ | Just (arg, res) <- tcSplitFunTy_maybe ty
+ = let
+ arg' = hoistForAllTys arg -- Don't forget to apply hoist recursively
+ in -- to the argument type
+ if (isPredTy arg') then
+ case hoist res of
+ (tvs,theta,tau) -> (tvs, arg':theta, tau)
+ else
+ case hoist res of
+ (tvs,theta,tau) -> (tvs, theta, mkFunTy arg' tau)
+
+ | otherwise = ([], [], ty)
+\end{code}
+
%************************************************************************
%* *