{-# LANGUAGE TypeFamilies #-}
module GHC.Tc.Gen.Default ( tcDefaults ) where
import GHC.Prelude
import GHC.Hs
import GHC.Core.Class
import GHC.Core.Type ( typeKind )
import GHC.Types.Var( tyVarKind )
import GHC.Tc.Errors.Types
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.Env
import GHC.Tc.Gen.HsType
import GHC.Tc.Utils.Zonk
import GHC.Tc.Solver
import GHC.Tc.Validity
import GHC.Tc.Utils.TcType
import GHC.Builtin.Names
import GHC.Types.Error
import GHC.Types.SrcLoc
import GHC.Utils.Outputable
import GHC.Utils.Panic
import qualified GHC.LanguageExtensions as LangExt
tcDefaults :: [LDefaultDecl GhcRn]
-> TcM (Maybe [Type])
tcDefaults :: [LDefaultDecl GhcRn] -> TcM (Maybe [Type])
tcDefaults []
= TcM (Maybe [Type])
getDeclaredDefaultTys
tcDefaults [L SrcSpanAnnA
_ (DefaultDecl XCDefaultDecl GhcRn
_ [])]
= forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just [])
tcDefaults [L SrcSpanAnnA
locn (DefaultDecl XCDefaultDecl GhcRn
_ [LHsType GhcRn]
mono_tys)]
= forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
locn) forall a b. (a -> b) -> a -> b
$
forall a. SDoc -> TcM a -> TcM a
addErrCtxt SDoc
defaultDeclCtxt forall a b. (a -> b) -> a -> b
$
do { Bool
ovl_str <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.OverloadedStrings
; Bool
ext_deflt <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.ExtendedDefaultRules
; Class
num_class <- Name -> TcM Class
tcLookupClass Name
numClassName
; [Class]
deflt_str <- if Bool
ovl_str
then forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Name -> TcM Class
tcLookupClass [Name
isStringClassName]
else forall (m :: * -> *) a. Monad m => a -> m a
return []
; [Class]
deflt_interactive <- if Bool
ext_deflt
then forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Name -> TcM Class
tcLookupClass [Name]
interactiveClassNames
else forall (m :: * -> *) a. Monad m => a -> m a
return []
; let deflt_clss :: [Class]
deflt_clss = Class
num_class forall a. a -> [a] -> [a]
: [Class]
deflt_str forall a. [a] -> [a] -> [a]
++ [Class]
deflt_interactive
; [Type]
tau_tys <- forall a b. (a -> TcRn b) -> [a] -> TcRn [b]
mapAndReportM ([Class] -> LHsType GhcRn -> TcM Type
tc_default_ty [Class]
deflt_clss) [LHsType GhcRn]
mono_tys
; forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just [Type]
tau_tys) }
tcDefaults decls :: [LDefaultDecl GhcRn]
decls@(L SrcSpanAnnA
locn (DefaultDecl XCDefaultDecl GhcRn
_ [LHsType GhcRn]
_) : [LDefaultDecl GhcRn]
_)
= forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
locn) forall a b. (a -> b) -> a -> b
$
forall a. TcRnMessage -> TcM a
failWithTc ([LDefaultDecl GhcRn] -> TcRnMessage
dupDefaultDeclErr [LDefaultDecl GhcRn]
decls)
tc_default_ty :: [Class] -> LHsType GhcRn -> TcM Type
tc_default_ty :: [Class] -> LHsType GhcRn -> TcM Type
tc_default_ty [Class]
deflt_clss LHsType GhcRn
hs_ty
= do { Type
ty <- forall a. String -> TcM a -> TcM a
solveEqualities String
"tc_default_ty" forall a b. (a -> b) -> a -> b
$
LHsType GhcRn -> TcM Type
tcInferLHsType LHsType GhcRn
hs_ty
; Type
ty <- Type -> TcM Type
zonkTcTypeToType Type
ty
; UserTypeCtxt -> Type -> TcM ()
checkValidType UserTypeCtxt
DefaultDeclCtxt Type
ty
; [Bool]
oks <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Type -> Class -> TcM Bool
check_instance Type
ty) [Class]
deflt_clss
; Bool -> TcRnMessage -> TcM ()
checkTc (forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Bool]
oks) (Type -> [Class] -> TcRnMessage
TcRnBadDefaultType Type
ty [Class]
deflt_clss)
; forall (m :: * -> *) a. Monad m => a -> m a
return Type
ty }
check_instance :: Type -> Class -> TcM Bool
check_instance :: Type -> Class -> TcM Bool
check_instance Type
ty Class
cls
| [TyVar
cls_tv] <- Class -> [TyVar]
classTyVars Class
cls
, TyVar -> Type
tyVarKind TyVar
cls_tv HasDebugCallStack => Type -> Type -> Bool
`tcEqType` HasDebugCallStack => Type -> Type
typeKind Type
ty
= [Type] -> TcM Bool
simplifyDefault [Class -> [Type] -> Type
mkClassPred Class
cls [Type
ty]]
| Bool
otherwise
= forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
defaultDeclCtxt :: SDoc
defaultDeclCtxt :: SDoc
defaultDeclCtxt = String -> SDoc
text String
"When checking the types in a default declaration"
dupDefaultDeclErr :: [LDefaultDecl GhcRn] -> TcRnMessage
dupDefaultDeclErr :: [LDefaultDecl GhcRn] -> TcRnMessage
dupDefaultDeclErr (L SrcSpanAnnA
_ (DefaultDecl XCDefaultDecl GhcRn
_ [LHsType GhcRn]
_) : [LDefaultDecl GhcRn]
dup_things)
= [LDefaultDecl GhcRn] -> TcRnMessage
TcRnMultipleDefaultDeclarations [LDefaultDecl GhcRn]
dup_things
dupDefaultDeclErr [] = forall a. String -> a
panic String
"dupDefaultDeclErr []"