module TcGenGenerics (canDoGenerics, canDoGenerics1,
GenericKind(..),
gen_Generic_binds, get_gen1_constrained_tys) where
import HsSyn
import Type
import TcType
import TcGenDeriv
import DataCon
import TyCon
import FamInstEnv ( FamInst, FamFlavor(..), mkSingleCoAxiom )
import FamInst
import Module ( Module, moduleName, moduleNameFS
, moduleUnitId, unitIdFS )
import IfaceEnv ( newGlobalBinder )
import Name hiding ( varName )
import RdrName
import BasicTypes
import TysPrim
import TysWiredIn
import PrelNames
import TcEnv
import TcRnMonad
import HscTypes
import ErrUtils( Validity(..), andValid )
import SrcLoc
import Bag
import VarEnv
import VarSet (elemVarSet, partitionVarSet)
import Outputable
import FastString
import Util
import Control.Monad (mplus)
import Data.List (zip4)
import Data.Maybe (isJust)
#include "HsVersions.h"
gen_Generic_binds :: GenericKind -> TyCon -> Type -> Module
-> TcM (LHsBinds RdrName, FamInst)
gen_Generic_binds gk tc inst_ty mod = do
repTyInsts <- tc_mkRepFamInsts gk tc inst_ty mod
return (mkBindsRep gk tc, repTyInsts)
get_gen1_constrained_tys :: TyVar -> Type -> [Type]
get_gen1_constrained_tys argVar
= argTyFold argVar $ ArgTyAlg { ata_rec0 = const []
, ata_par1 = [], ata_rec1 = const []
, ata_comp = (:) }
canDoGenerics :: TyCon -> Validity
canDoGenerics tc
= mergeErrors (
(if (not (null (tyConStupidTheta tc)))
then (NotValid (tc_name <+> text "must not have a datatype context"))
else IsValid)
: (map bad_con (tyConDataCons tc)))
where
tc_name = ppr $ case tyConFamInst_maybe tc of
Just (ptc, _) -> ptc
_ -> tc
bad_con dc = if (any bad_arg_type (dataConOrigArgTys dc))
then (NotValid (ppr dc <+> text
"must not have exotic unlifted or polymorphic arguments"))
else (if (not (isVanillaDataCon dc))
then (NotValid (ppr dc <+> text "must be a vanilla data constructor"))
else IsValid)
bad_arg_type ty = (isUnliftedType ty && not (allowedUnliftedTy ty))
|| not (isTauTy ty)
allowedUnliftedTy :: Type -> Bool
allowedUnliftedTy = isJust . unboxedRepRDRs
mergeErrors :: [Validity] -> Validity
mergeErrors [] = IsValid
mergeErrors (NotValid s:t) = case mergeErrors t of
IsValid -> NotValid s
NotValid s' -> NotValid (s <> text ", and" $$ s')
mergeErrors (IsValid : t) = mergeErrors t
data Check_for_CanDoGenerics1 = CCDG1
{ _ccdg1_hasParam :: Bool
, _ccdg1_errors :: Validity
}
canDoGenerics1 :: TyCon -> Validity
canDoGenerics1 rep_tc =
canDoGenerics rep_tc `andValid` additionalChecks
where
additionalChecks
| null (tyConTyVars rep_tc) = NotValid $
text "Data type" <+> quotes (ppr rep_tc)
<+> text "must have some type parameters"
| otherwise = mergeErrors $ concatMap check_con data_cons
data_cons = tyConDataCons rep_tc
check_con con = case check_vanilla con of
j@(NotValid {}) -> [j]
IsValid -> _ccdg1_errors `map` foldDataConArgs (ft_check con) con
bad :: DataCon -> SDoc -> SDoc
bad con msg = text "Constructor" <+> quotes (ppr con) <+> msg
check_vanilla :: DataCon -> Validity
check_vanilla con | isVanillaDataCon con = IsValid
| otherwise = NotValid (bad con existential)
bmzero = CCDG1 False IsValid
bmbad con s = CCDG1 True $ NotValid $ bad con s
bmplus (CCDG1 b1 m1) (CCDG1 b2 m2) = CCDG1 (b1 || b2) (m1 `andValid` m2)
ft_check :: DataCon -> FFoldType Check_for_CanDoGenerics1
ft_check con = FT
{ ft_triv = bmzero
, ft_var = caseVar, ft_co_var = caseVar
, ft_tup = \_ components -> if any _ccdg1_hasParam (init components)
then bmbad con wrong_arg
else foldr bmplus bmzero components
, ft_fun = \dom rng ->
if _ccdg1_hasParam dom
then bmbad con wrong_arg
else bmplus dom rng
, ft_ty_app = \_ arg -> arg
, ft_bad_app = bmbad con wrong_arg
, ft_forall = \_ body -> body
}
where
caseVar = CCDG1 True IsValid
existential = text "must not have existential arguments"
wrong_arg = text "applies a type to an argument involving the last parameter"
$$ text "but the applied type is not of kind * -> *"
type US = Int
type Alt = (LPat RdrName, LHsExpr RdrName)
data GenericKind = Gen0 | Gen1
data GenericKind_ = Gen0_ | Gen1_ TyVar
data GenericKind_DC = Gen0_DC | Gen1_DC TyVar
forgetArgVar :: GenericKind_DC -> GenericKind
forgetArgVar Gen0_DC = Gen0
forgetArgVar Gen1_DC{} = Gen1
gk2gkDC :: GenericKind_ -> DataCon -> GenericKind_DC
gk2gkDC Gen0_ _ = Gen0_DC
gk2gkDC Gen1_{} d = Gen1_DC $ last $ dataConUnivTyVars d
mkBindsRep :: GenericKind -> TyCon -> LHsBinds RdrName
mkBindsRep gk tycon =
unitBag (mkRdrFunBind (L loc from01_RDR) from_matches)
`unionBags`
unitBag (mkRdrFunBind (L loc to01_RDR) to_matches)
where
from_matches = [mkSimpleHsAlt pat rhs | (pat,rhs) <- from_alts]
to_matches = [mkSimpleHsAlt pat rhs | (pat,rhs) <- to_alts ]
loc = srcLocSpan (getSrcLoc tycon)
datacons = tyConDataCons tycon
(from01_RDR, to01_RDR) = case gk of
Gen0 -> (from_RDR, to_RDR)
Gen1 -> (from1_RDR, to1_RDR)
from_alts, to_alts :: [Alt]
(from_alts, to_alts) = mkSum gk_ (1 :: US) tycon datacons
where gk_ = case gk of
Gen0 -> Gen0_
Gen1 -> ASSERT(length tyvars >= 1)
Gen1_ (last tyvars)
where tyvars = tyConTyVars tycon
tc_mkRepFamInsts :: GenericKind
-> TyCon
-> Type
-> Module
-> TcM (FamInst)
tc_mkRepFamInsts gk tycon inst_ty mod =
do {
fam_tc <- case gk of
Gen0 -> tcLookupTyCon repTyConName
Gen1 -> tcLookupTyCon rep1TyConName
; fam_envs <- tcGetFamInstEnvs
; let mbFamInst = tyConFamInst_maybe tycon
ptc = maybe tycon fst mbFamInst
(_, inst_args, _) = tcLookupDataFamInst fam_envs ptc $ snd
$ tcSplitTyConApp inst_ty
; let
(tyvars, gk_) = case gk of
Gen0 -> (all_tyvars, Gen0_)
Gen1 -> ASSERT(not $ null all_tyvars)
(init all_tyvars, Gen1_ $ last all_tyvars)
where all_tyvars = tyConTyVars tycon
; repTy <- tc_mkRepTy gk_ tycon
; rep_name <- let mkGen = case gk of Gen0 -> mkGenR; Gen1 -> mkGen1R
in newGlobalBinder mod (mkGen (nameOccName (tyConName tycon)))
(nameSrcSpan (tyConName tycon))
; let env = zipTyEnv tyvars inst_args
in_scope = mkInScopeSet (tyCoVarsOfType inst_ty)
subst = mkTvSubst in_scope env
repTy' = substTy subst repTy
tcv_set' = tyCoVarsOfType inst_ty
(tv_set', cv_set') = partitionVarSet isTyVar tcv_set'
tvs' = varSetElemsWellScoped tv_set'
cvs' = varSetElemsWellScoped cv_set'
axiom = mkSingleCoAxiom Nominal rep_name tvs' cvs'
fam_tc [inst_ty] repTy'
; newFamInst SynFamilyInst axiom }
data ArgTyAlg a = ArgTyAlg
{ ata_rec0 :: (Type -> a)
, ata_par1 :: a, ata_rec1 :: (Type -> a)
, ata_comp :: (Type -> a -> a)
}
argTyFold :: forall a. TyVar -> ArgTyAlg a -> Type -> a
argTyFold argVar (ArgTyAlg {ata_rec0 = mkRec0,
ata_par1 = mkPar1, ata_rec1 = mkRec1,
ata_comp = mkComp}) =
\t -> maybe (mkRec0 t) id $ go t where
go :: Type ->
Maybe a
go t = isParam `mplus` isApp where
isParam = do
t' <- getTyVar_maybe t
Just $ if t' == argVar then mkPar1
else mkRec0 t
isApp = do
(phi, beta) <- tcSplitAppTy_maybe t
let interesting = argVar `elemVarSet` exactTyCoVarsOfType beta
if not interesting then Nothing
else
if Just argVar == getTyVar_maybe beta then Just $ mkRec1 phi
else mkComp phi `fmap` go beta
tc_mkRepTy ::
GenericKind_
-> TyCon
-> TcM Type
tc_mkRepTy gk_ tycon =
do
d1 <- tcLookupTyCon d1TyConName
c1 <- tcLookupTyCon c1TyConName
s1 <- tcLookupTyCon s1TyConName
rec0 <- tcLookupTyCon rec0TyConName
rec1 <- tcLookupTyCon rec1TyConName
par1 <- tcLookupTyCon par1TyConName
u1 <- tcLookupTyCon u1TyConName
v1 <- tcLookupTyCon v1TyConName
plus <- tcLookupTyCon sumTyConName
times <- tcLookupTyCon prodTyConName
comp <- tcLookupTyCon compTyConName
uAddr <- tcLookupTyCon uAddrTyConName
uChar <- tcLookupTyCon uCharTyConName
uDouble <- tcLookupTyCon uDoubleTyConName
uFloat <- tcLookupTyCon uFloatTyConName
uInt <- tcLookupTyCon uIntTyConName
uWord <- tcLookupTyCon uWordTyConName
let tcLookupPromDataCon = fmap promoteDataCon . tcLookupDataCon
md <- tcLookupPromDataCon metaDataDataConName
mc <- tcLookupPromDataCon metaConsDataConName
ms <- tcLookupPromDataCon metaSelDataConName
pPrefix <- tcLookupPromDataCon prefixIDataConName
pInfix <- tcLookupPromDataCon infixIDataConName
pLA <- tcLookupPromDataCon leftAssociativeDataConName
pRA <- tcLookupPromDataCon rightAssociativeDataConName
pNA <- tcLookupPromDataCon notAssociativeDataConName
pSUpk <- tcLookupPromDataCon sourceUnpackDataConName
pSNUpk <- tcLookupPromDataCon sourceNoUnpackDataConName
pNSUpkness <- tcLookupPromDataCon noSourceUnpackednessDataConName
pSLzy <- tcLookupPromDataCon sourceLazyDataConName
pSStr <- tcLookupPromDataCon sourceStrictDataConName
pNSStrness <- tcLookupPromDataCon noSourceStrictnessDataConName
pDLzy <- tcLookupPromDataCon decidedLazyDataConName
pDStr <- tcLookupPromDataCon decidedStrictDataConName
pDUpk <- tcLookupPromDataCon decidedUnpackDataConName
fix_env <- getFixityEnv
let mkSum' a b = mkTyConApp plus [a,b]
mkProd a b = mkTyConApp times [a,b]
mkComp a b = mkTyConApp comp [a,b]
mkRec0 a = mkBoxTy uAddr uChar uDouble uFloat uInt uWord rec0 a
mkRec1 a = mkTyConApp rec1 [a]
mkPar1 = mkTyConTy par1
mkD a = mkTyConApp d1 [ metaDataTy, sumP (tyConDataCons a) ]
mkC a = mkTyConApp c1 [ metaConsTy a
, prod (dataConInstOrigArgTys a
. mkTyVarTys . tyConTyVars $ tycon)
(dataConSrcBangs a)
(dataConImplBangs a)
(dataConFieldLabels a)]
mkS mlbl su ss ib a = mkTyConApp s1 [metaSelTy mlbl su ss ib, a]
sumP [] = mkTyConTy v1
sumP l = foldBal mkSum' . map mkC $ l
prod :: [Type] -> [HsSrcBang] -> [HsImplBang] -> [FieldLabel] -> Type
prod [] _ _ _ = mkTyConTy u1
prod l sb ib fl = foldBal mkProd
[ ASSERT(null fl || length fl > j)
arg t sb' ib' (if null fl
then Nothing
else Just (fl !! j))
| (t,sb',ib',j) <- zip4 l sb ib [0..] ]
arg :: Type -> HsSrcBang -> HsImplBang -> Maybe FieldLabel -> Type
arg t (HsSrcBang _ su ss) ib fl = mkS fl su ss ib $ case gk_ of
Gen0_ -> mkRec0 t
Gen1_ argVar -> argPar argVar t
where
argPar argVar = argTyFold argVar $ ArgTyAlg
{ata_rec0 = mkRec0, ata_par1 = mkPar1,
ata_rec1 = mkRec1, ata_comp = mkComp}
tyConName_user = case tyConFamInst_maybe tycon of
Just (ptycon, _) -> tyConName ptycon
Nothing -> tyConName tycon
dtName = mkStrLitTy . occNameFS . nameOccName $ tyConName_user
mdName = mkStrLitTy . moduleNameFS . moduleName
. nameModule . tyConName $ tycon
pkgName = mkStrLitTy . unitIdFS . moduleUnitId
. nameModule . tyConName $ tycon
isNT = mkTyConTy $ if isNewTyCon tycon
then promotedTrueDataCon
else promotedFalseDataCon
ctName = mkStrLitTy . occNameFS . nameOccName . dataConName
ctFix c
| dataConIsInfix c
= case lookupFixity fix_env (dataConName c) of
Fixity _ n InfixL -> buildFix n pLA
Fixity _ n InfixR -> buildFix n pRA
Fixity _ n InfixN -> buildFix n pNA
| otherwise = mkTyConTy pPrefix
buildFix n assoc = mkTyConApp pInfix [ mkTyConTy assoc
, mkNumLitTy (fromIntegral n)]
isRec c = mkTyConTy $ if length (dataConFieldLabels c) > 0
then promotedTrueDataCon
else promotedFalseDataCon
selName = mkStrLitTy . flLabel
mbSel Nothing = mkTyConApp promotedNothingDataCon [typeSymbolKind]
mbSel (Just s) = mkTyConApp promotedJustDataCon
[typeSymbolKind, selName s]
metaDataTy = mkTyConApp md [dtName, mdName, pkgName, isNT]
metaConsTy c = mkTyConApp mc [ctName c, ctFix c, isRec c]
metaSelTy mlbl su ss ib =
mkTyConApp ms [mbSel mlbl, pSUpkness, pSStrness, pDStrness]
where
pSUpkness = mkTyConTy $ case su of
SrcUnpack -> pSUpk
SrcNoUnpack -> pSNUpk
NoSrcUnpack -> pNSUpkness
pSStrness = mkTyConTy $ case ss of
SrcLazy -> pSLzy
SrcStrict -> pSStr
NoSrcStrict -> pNSStrness
pDStrness = mkTyConTy $ case ib of
HsLazy -> pDLzy
HsStrict -> pDStr
HsUnpack{} -> pDUpk
return (mkD tycon)
mkBoxTy :: TyCon
-> TyCon
-> TyCon
-> TyCon
-> TyCon
-> TyCon
-> TyCon
-> Type
-> Type
mkBoxTy uAddr uChar uDouble uFloat uInt uWord rec0 ty
| ty `eqType` addrPrimTy = mkTyConTy uAddr
| ty `eqType` charPrimTy = mkTyConTy uChar
| ty `eqType` doublePrimTy = mkTyConTy uDouble
| ty `eqType` floatPrimTy = mkTyConTy uFloat
| ty `eqType` intPrimTy = mkTyConTy uInt
| ty `eqType` wordPrimTy = mkTyConTy uWord
| otherwise = mkTyConApp rec0 [ty]
mkSum :: GenericKind_
-> US
-> TyCon
-> [DataCon]
-> ([Alt],
[Alt])
mkSum _ _ tycon [] = ([from_alt], [to_alt])
where
from_alt = (nlWildPat, mkM1_E (makeError errMsgFrom))
to_alt = (mkM1_P nlWildPat, makeError errMsgTo)
makeError s = nlHsApp (nlHsVar error_RDR) (nlHsLit (mkHsString s))
tyConStr = occNameString (nameOccName (tyConName tycon))
errMsgFrom = "No generic representation for empty datatype " ++ tyConStr
errMsgTo = "No values for empty datatype " ++ tyConStr
mkSum gk_ us _ datacons =
unzip [ mk1Sum (gk2gkDC gk_ d) us i (length datacons) d
| (d,i) <- zip datacons [1..] ]
mk1Sum :: GenericKind_DC
-> US
-> Int
-> Int
-> DataCon
-> (Alt,
Alt)
mk1Sum gk_ us i n datacon = (from_alt, to_alt)
where
gk = forgetArgVar gk_
argTys = dataConOrigArgTys datacon
n_args = dataConSourceArity datacon
datacon_varTys = zip (map mkGenericLocal [us .. us+n_args1]) argTys
datacon_vars = map fst datacon_varTys
us' = us + n_args
datacon_rdr = getRdrName datacon
from_alt = (nlConVarPat datacon_rdr datacon_vars, from_alt_rhs)
from_alt_rhs = mkM1_E (genLR_E i n (mkProd_E gk_ us' datacon_varTys))
to_alt = ( mkM1_P (genLR_P i n (mkProd_P gk us' datacon_varTys))
, to_alt_rhs
)
to_alt_rhs = case gk_ of
Gen0_DC -> nlHsVarApps datacon_rdr datacon_vars
Gen1_DC argVar -> nlHsApps datacon_rdr $ map argTo datacon_varTys
where
argTo (var, ty) = converter ty `nlHsApp` nlHsVar var where
converter = argTyFold argVar $ ArgTyAlg
{ata_rec0 = nlHsVar . unboxRepRDR,
ata_par1 = nlHsVar unPar1_RDR,
ata_rec1 = const $ nlHsVar unRec1_RDR,
ata_comp = \_ cnv -> (nlHsVar fmap_RDR `nlHsApp` cnv)
`nlHsCompose` nlHsVar unComp1_RDR}
genLR_P :: Int -> Int -> LPat RdrName -> LPat RdrName
genLR_P i n p
| n == 0 = error "impossible"
| n == 1 = p
| i <= div n 2 = nlConPat l1DataCon_RDR [genLR_P i (div n 2) p]
| otherwise = nlConPat r1DataCon_RDR [genLR_P (im) (nm) p]
where m = div n 2
genLR_E :: Int -> Int -> LHsExpr RdrName -> LHsExpr RdrName
genLR_E i n e
| n == 0 = error "impossible"
| n == 1 = e
| i <= div n 2 = nlHsVar l1DataCon_RDR `nlHsApp` genLR_E i (div n 2) e
| otherwise = nlHsVar r1DataCon_RDR `nlHsApp` genLR_E (im) (nm) e
where m = div n 2
mkProd_E :: GenericKind_DC
-> US
-> [(RdrName, Type)]
-> LHsExpr RdrName
mkProd_E _ _ [] = mkM1_E (nlHsVar u1DataCon_RDR)
mkProd_E gk_ _ varTys = mkM1_E (foldBal prod appVars)
where
appVars = map (wrapArg_E gk_) varTys
prod a b = prodDataCon_RDR `nlHsApps` [a,b]
wrapArg_E :: GenericKind_DC -> (RdrName, Type) -> LHsExpr RdrName
wrapArg_E Gen0_DC (var, ty) = mkM1_E $
boxRepRDR ty `nlHsVarApps` [var]
wrapArg_E (Gen1_DC argVar) (var, ty) = mkM1_E $
converter ty `nlHsApp` nlHsVar var
where converter = argTyFold argVar $ ArgTyAlg
{ata_rec0 = nlHsVar . boxRepRDR,
ata_par1 = nlHsVar par1DataCon_RDR,
ata_rec1 = const $ nlHsVar rec1DataCon_RDR,
ata_comp = \_ cnv -> nlHsVar comp1DataCon_RDR `nlHsCompose`
(nlHsVar fmap_RDR `nlHsApp` cnv)}
boxRepRDR :: Type -> RdrName
boxRepRDR = maybe k1DataCon_RDR fst . unboxedRepRDRs
unboxRepRDR :: Type -> RdrName
unboxRepRDR = maybe unK1_RDR snd . unboxedRepRDRs
unboxedRepRDRs :: Type -> Maybe (RdrName, RdrName)
unboxedRepRDRs ty
| ty `eqType` addrPrimTy = Just (uAddrDataCon_RDR, uAddrHash_RDR)
| ty `eqType` charPrimTy = Just (uCharDataCon_RDR, uCharHash_RDR)
| ty `eqType` doublePrimTy = Just (uDoubleDataCon_RDR, uDoubleHash_RDR)
| ty `eqType` floatPrimTy = Just (uFloatDataCon_RDR, uFloatHash_RDR)
| ty `eqType` intPrimTy = Just (uIntDataCon_RDR, uIntHash_RDR)
| ty `eqType` wordPrimTy = Just (uWordDataCon_RDR, uWordHash_RDR)
| otherwise = Nothing
mkProd_P :: GenericKind
-> US
-> [(RdrName, Type)]
-> LPat RdrName
mkProd_P _ _ [] = mkM1_P (nlNullaryConPat u1DataCon_RDR)
mkProd_P gk _ varTys = mkM1_P (foldBal prod appVars)
where
appVars = unzipWith (wrapArg_P gk) varTys
prod a b = prodDataCon_RDR `nlConPat` [a,b]
wrapArg_P :: GenericKind -> RdrName -> Type -> LPat RdrName
wrapArg_P Gen0 v ty = mkM1_P (boxRepRDR ty `nlConVarPat` [v])
wrapArg_P Gen1 v _ = m1DataCon_RDR `nlConVarPat` [v]
mkGenericLocal :: US -> RdrName
mkGenericLocal u = mkVarUnqual (mkFastString ("g" ++ show u))
mkM1_E :: LHsExpr RdrName -> LHsExpr RdrName
mkM1_E e = nlHsVar m1DataCon_RDR `nlHsApp` e
mkM1_P :: LPat RdrName -> LPat RdrName
mkM1_P p = m1DataCon_RDR `nlConPat` [p]
nlHsCompose :: LHsExpr RdrName -> LHsExpr RdrName -> LHsExpr RdrName
nlHsCompose x y = compose_RDR `nlHsApps` [x, y]
foldBal :: (a -> a -> a) -> [a] -> a
foldBal op = foldBal' op (error "foldBal: empty list")
foldBal' :: (a -> a -> a) -> a -> [a] -> a
foldBal' _ x [] = x
foldBal' _ _ [y] = y
foldBal' op x l = let (a,b) = splitAt (length l `div` 2) l
in foldBal' op x a `op` foldBal' op x b