{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
module GHC.Core.Make (
mkCoreLet, mkCoreLets,
mkCoreApp, mkCoreApps, mkCoreConApps,
mkCoreLams, mkWildCase, mkIfThenElse,
mkWildValBinder, mkWildEvBinder,
mkSingleAltCase,
sortQuantVars, castBottomExpr,
mkLitRubbish,
mkWordExpr,
mkIntExpr, mkIntExprInt, mkUncheckedIntExpr,
mkIntegerExpr, mkNaturalExpr,
mkFloatExpr, mkDoubleExpr,
mkCharExpr, mkStringExpr, mkStringExprFS, mkStringExprFSWith,
MkStringIds (..), getMkStringIds,
FloatBind(..), wrapFloat, wrapFloats, floatBindings,
mkCoreVarTupTy, mkCoreTup, mkCoreUnboxedTuple, mkCoreUnboxedSum,
mkCoreTupBoxity, unitExpr,
mkChunkified, chunkify,
mkBigCoreVarTup, mkBigCoreVarTupSolo,
mkBigCoreVarTupTy, mkBigCoreTupTy,
mkBigCoreTup,
mkBigTupleSelector, mkBigTupleSelectorSolo, mkBigTupleCase,
mkNilExpr, mkConsExpr, mkListExpr,
mkFoldrExpr, mkBuildExpr,
mkNothingExpr, mkJustExpr,
mkRuntimeErrorApp, mkImpossibleExpr, mkAbsentErrorApp, errorIds,
rEC_CON_ERROR_ID,
nON_EXHAUSTIVE_GUARDS_ERROR_ID, nO_METHOD_BINDING_ERROR_ID,
pAT_ERROR_ID, rEC_SEL_ERROR_ID,
tYPE_ERROR_ID, aBSENT_SUM_FIELD_ERROR_ID
) where
import GHC.Prelude
import GHC.Platform
import GHC.Types.Id
import GHC.Types.Var ( EvVar, setTyVarUnique, visArgConstraintLike )
import GHC.Types.TyThing
import GHC.Types.Id.Info
import GHC.Types.Cpr
import GHC.Types.Basic( TypeOrConstraint(..) )
import GHC.Types.Demand
import GHC.Types.Name hiding ( varName )
import GHC.Types.Literal
import GHC.Types.Unique.Supply
import GHC.Core
import GHC.Core.Utils ( exprType, mkSingleAltCase, bindNonRec )
import GHC.Core.Type
import GHC.Core.TyCo.Compare( eqType )
import GHC.Core.Coercion ( isCoVar )
import GHC.Core.DataCon ( DataCon, dataConWorkId )
import GHC.Core.Multiplicity
import GHC.Builtin.Types
import GHC.Builtin.Names
import GHC.Builtin.Types.Prim
import GHC.Utils.Outputable
import GHC.Utils.Misc
import GHC.Utils.Panic
import GHC.Utils.Panic.Plain
import GHC.Settings.Constants( mAX_TUPLE_SIZE )
import GHC.Data.FastString
import Data.List ( partition )
import Data.Char ( ord )
infixl 4 `mkCoreApp`, `mkCoreApps`
sortQuantVars :: [Var] -> [Var]
sortQuantVars :: [Id] -> [Id]
sortQuantVars [Id]
vs = [Id]
sorted_tcvs forall a. [a] -> [a] -> [a]
++ [Id]
ids
where
([Id]
tcvs, [Id]
ids) = forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (Id -> Bool
isTyVar forall (f :: * -> *). Applicative f => f Bool -> f Bool -> f Bool
<||> Id -> Bool
isCoVar) [Id]
vs
sorted_tcvs :: [Id]
sorted_tcvs = [Id] -> [Id]
scopedSort [Id]
tcvs
mkCoreLet :: CoreBind -> CoreExpr -> CoreExpr
mkCoreLet :: CoreBind -> CoreExpr -> CoreExpr
mkCoreLet (NonRec Id
bndr CoreExpr
rhs) CoreExpr
body
= HasDebugCallStack => Id -> CoreExpr -> CoreExpr -> CoreExpr
bindNonRec Id
bndr CoreExpr
rhs CoreExpr
body
mkCoreLet CoreBind
bind CoreExpr
body
= forall b. Bind b -> Expr b -> Expr b
Let CoreBind
bind CoreExpr
body
mkCoreLams :: [CoreBndr] -> CoreExpr -> CoreExpr
mkCoreLams :: [Id] -> CoreExpr -> CoreExpr
mkCoreLams = forall b. [b] -> Expr b -> Expr b
mkLams
mkCoreLets :: [CoreBind] -> CoreExpr -> CoreExpr
mkCoreLets :: [CoreBind] -> CoreExpr -> CoreExpr
mkCoreLets [CoreBind]
binds CoreExpr
body = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr CoreBind -> CoreExpr -> CoreExpr
mkCoreLet CoreExpr
body [CoreBind]
binds
mkCoreConApps :: DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps :: DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
con [CoreExpr]
args = CoreExpr -> [CoreExpr] -> CoreExpr
mkCoreApps (forall b. Id -> Expr b
Var (DataCon -> Id
dataConWorkId DataCon
con)) [CoreExpr]
args
mkCoreApps :: CoreExpr
-> [CoreExpr]
-> CoreExpr
mkCoreApps :: CoreExpr -> [CoreExpr] -> CoreExpr
mkCoreApps CoreExpr
fun [CoreExpr]
args
= forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (SDoc -> (CoreExpr, Kind) -> CoreExpr -> (CoreExpr, Kind)
mkCoreAppTyped SDoc
doc_string) (CoreExpr
fun, Kind
fun_ty) [CoreExpr]
args
where
doc_string :: SDoc
doc_string = forall a. Outputable a => a -> SDoc
ppr Kind
fun_ty forall doc. IsDoc doc => doc -> doc -> doc
$$ forall a. Outputable a => a -> SDoc
ppr CoreExpr
fun forall doc. IsDoc doc => doc -> doc -> doc
$$ forall a. Outputable a => a -> SDoc
ppr [CoreExpr]
args
fun_ty :: Kind
fun_ty = HasDebugCallStack => CoreExpr -> Kind
exprType CoreExpr
fun
mkCoreApp :: SDoc
-> CoreExpr
-> CoreExpr
-> CoreExpr
mkCoreApp :: SDoc -> CoreExpr -> CoreExpr -> CoreExpr
mkCoreApp SDoc
s CoreExpr
fun CoreExpr
arg
= forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ SDoc -> (CoreExpr, Kind) -> CoreExpr -> (CoreExpr, Kind)
mkCoreAppTyped SDoc
s (CoreExpr
fun, HasDebugCallStack => CoreExpr -> Kind
exprType CoreExpr
fun) CoreExpr
arg
mkCoreAppTyped :: SDoc -> (CoreExpr, Type) -> CoreExpr -> (CoreExpr, Type)
mkCoreAppTyped :: SDoc -> (CoreExpr, Kind) -> CoreExpr -> (CoreExpr, Kind)
mkCoreAppTyped SDoc
_ (CoreExpr
fun, Kind
fun_ty) (Type Kind
ty)
= (forall b. Expr b -> Expr b -> Expr b
App CoreExpr
fun (forall b. Kind -> Expr b
Type Kind
ty), HasDebugCallStack => Kind -> Kind -> Kind
piResultTy Kind
fun_ty Kind
ty)
mkCoreAppTyped SDoc
_ (CoreExpr
fun, Kind
fun_ty) (Coercion Coercion
co)
= (forall b. Expr b -> Expr b -> Expr b
App CoreExpr
fun (forall b. Coercion -> Expr b
Coercion Coercion
co), HasDebugCallStack => Kind -> Kind
funResultTy Kind
fun_ty)
mkCoreAppTyped SDoc
d (CoreExpr
fun, Kind
fun_ty) CoreExpr
arg
= forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr (Kind -> Bool
isFunTy Kind
fun_ty) (forall a. Outputable a => a -> SDoc
ppr CoreExpr
fun forall doc. IsDoc doc => doc -> doc -> doc
$$ forall a. Outputable a => a -> SDoc
ppr CoreExpr
arg forall doc. IsDoc doc => doc -> doc -> doc
$$ SDoc
d)
(forall b. Expr b -> Expr b -> Expr b
App CoreExpr
fun CoreExpr
arg, HasDebugCallStack => Kind -> Kind
funResultTy Kind
fun_ty)
mkWildEvBinder :: PredType -> EvVar
mkWildEvBinder :: Kind -> Id
mkWildEvBinder Kind
pred = Kind -> Kind -> Id
mkWildValBinder Kind
ManyTy Kind
pred
mkWildValBinder :: Mult -> Type -> Id
mkWildValBinder :: Kind -> Kind -> Id
mkWildValBinder Kind
w Kind
ty = Name -> Kind -> Kind -> Id
mkLocalIdOrCoVar Name
wildCardName Kind
w Kind
ty
mkWildCase :: CoreExpr
-> Scaled Type
-> Type
-> [CoreAlt]
-> CoreExpr
mkWildCase :: CoreExpr -> Scaled Kind -> Kind -> [CoreAlt] -> CoreExpr
mkWildCase CoreExpr
scrut (Scaled Kind
w Kind
scrut_ty) Kind
res_ty [CoreAlt]
alts
= forall b. Expr b -> b -> Kind -> [Alt b] -> Expr b
Case CoreExpr
scrut (Kind -> Kind -> Id
mkWildValBinder Kind
w Kind
scrut_ty) Kind
res_ty [CoreAlt]
alts
mkIfThenElse :: CoreExpr
-> CoreExpr
-> CoreExpr
-> CoreExpr
mkIfThenElse :: CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
mkIfThenElse CoreExpr
guard CoreExpr
then_expr CoreExpr
else_expr
= CoreExpr -> Scaled Kind -> Kind -> [CoreAlt] -> CoreExpr
mkWildCase CoreExpr
guard (forall a. a -> Scaled a
linear Kind
boolTy) (HasDebugCallStack => CoreExpr -> Kind
exprType CoreExpr
then_expr)
[ forall b. AltCon -> [b] -> Expr b -> Alt b
Alt (DataCon -> AltCon
DataAlt DataCon
falseDataCon) [] CoreExpr
else_expr,
forall b. AltCon -> [b] -> Expr b -> Alt b
Alt (DataCon -> AltCon
DataAlt DataCon
trueDataCon) [] CoreExpr
then_expr ]
castBottomExpr :: CoreExpr -> Type -> CoreExpr
castBottomExpr :: CoreExpr -> Kind -> CoreExpr
castBottomExpr CoreExpr
e Kind
res_ty
| Kind
e_ty Kind -> Kind -> Bool
`eqType` Kind
res_ty = CoreExpr
e
| Bool
otherwise = forall b. Expr b -> b -> Kind -> [Alt b] -> Expr b
Case CoreExpr
e (Kind -> Kind -> Id
mkWildValBinder Kind
OneTy Kind
e_ty) Kind
res_ty []
where
e_ty :: Kind
e_ty = HasDebugCallStack => CoreExpr -> Kind
exprType CoreExpr
e
mkLitRubbish :: Type -> Maybe CoreExpr
mkLitRubbish :: Kind -> Maybe CoreExpr
mkLitRubbish Kind
ty
| Bool -> Bool
not (Kind -> Bool
noFreeVarsOfType Kind
rep)
= forall a. Maybe a
Nothing
| Kind -> Bool
isCoVarType Kind
ty
= forall a. Maybe a
Nothing
| Bool
otherwise
= forall a. a -> Maybe a
Just (forall b. Literal -> Expr b
Lit (TypeOrConstraint -> Kind -> Literal
LitRubbish TypeOrConstraint
torc Kind
rep) forall b. Expr b -> [Kind] -> Expr b
`mkTyApps` [Kind
ty])
where
Just (TypeOrConstraint
torc, Kind
rep) = Kind -> Maybe (TypeOrConstraint, Kind)
sORTKind_maybe (HasDebugCallStack => Kind -> Kind
typeKind Kind
ty)
mkIntExpr :: Platform -> Integer -> CoreExpr
mkIntExpr :: Platform -> Integer -> CoreExpr
mkIntExpr Platform
platform Integer
i = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
intDataCon [forall b. Platform -> Integer -> Expr b
mkIntLit Platform
platform Integer
i]
mkUncheckedIntExpr :: Integer -> CoreExpr
mkUncheckedIntExpr :: Integer -> CoreExpr
mkUncheckedIntExpr Integer
i = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
intDataCon [forall b. Literal -> Expr b
Lit (Integer -> Literal
mkLitIntUnchecked Integer
i)]
mkIntExprInt :: Platform -> Int -> CoreExpr
mkIntExprInt :: Platform -> Int -> CoreExpr
mkIntExprInt Platform
platform Int
i = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
intDataCon [forall b. Platform -> Integer -> Expr b
mkIntLit Platform
platform (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i)]
mkWordExpr :: Platform -> Integer -> CoreExpr
mkWordExpr :: Platform -> Integer -> CoreExpr
mkWordExpr Platform
platform Integer
w = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
wordDataCon [forall b. Platform -> Integer -> Expr b
mkWordLit Platform
platform Integer
w]
mkIntegerExpr :: Platform -> Integer -> CoreExpr
mkIntegerExpr :: Platform -> Integer -> CoreExpr
mkIntegerExpr Platform
platform Integer
i
| Platform -> Integer -> Bool
platformInIntRange Platform
platform Integer
i = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
integerISDataCon [forall b. Platform -> Integer -> Expr b
mkIntLit Platform
platform Integer
i]
| Integer
i forall a. Ord a => a -> a -> Bool
< Integer
0 = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
integerINDataCon [forall b. Literal -> Expr b
Lit (Integer -> Literal
mkLitBigNat (forall a. Num a => a -> a
negate Integer
i))]
| Bool
otherwise = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
integerIPDataCon [forall b. Literal -> Expr b
Lit (Integer -> Literal
mkLitBigNat Integer
i)]
mkNaturalExpr :: Platform -> Integer -> CoreExpr
mkNaturalExpr :: Platform -> Integer -> CoreExpr
mkNaturalExpr Platform
platform Integer
w
| Platform -> Integer -> Bool
platformInWordRange Platform
platform Integer
w = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
naturalNSDataCon [forall b. Platform -> Integer -> Expr b
mkWordLit Platform
platform Integer
w]
| Bool
otherwise = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
naturalNBDataCon [forall b. Literal -> Expr b
Lit (Integer -> Literal
mkLitBigNat Integer
w)]
mkFloatExpr :: Float -> CoreExpr
mkFloatExpr :: Float -> CoreExpr
mkFloatExpr Float
f = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
floatDataCon [forall b. Float -> Expr b
mkFloatLitFloat Float
f]
mkDoubleExpr :: Double -> CoreExpr
mkDoubleExpr :: Double -> CoreExpr
mkDoubleExpr Double
d = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
doubleDataCon [forall b. Double -> Expr b
mkDoubleLitDouble Double
d]
mkCharExpr :: Char -> CoreExpr
mkCharExpr :: Char -> CoreExpr
mkCharExpr Char
c = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
charDataCon [forall b. Char -> Expr b
mkCharLit Char
c]
mkStringExpr :: MonadThings m => String -> m CoreExpr
mkStringExpr :: forall (m :: * -> *). MonadThings m => String -> m CoreExpr
mkStringExpr String
str = forall (m :: * -> *). MonadThings m => FastString -> m CoreExpr
mkStringExprFS (String -> FastString
mkFastString String
str)
mkStringExprFS :: MonadThings m => FastString -> m CoreExpr
mkStringExprFS :: forall (m :: * -> *). MonadThings m => FastString -> m CoreExpr
mkStringExprFS = forall (m :: * -> *).
Monad m =>
(Name -> m Id) -> FastString -> m CoreExpr
mkStringExprFSLookup forall (m :: * -> *). MonadThings m => Name -> m Id
lookupId
mkStringExprFSLookup :: Monad m => (Name -> m Id) -> FastString -> m CoreExpr
mkStringExprFSLookup :: forall (m :: * -> *).
Monad m =>
(Name -> m Id) -> FastString -> m CoreExpr
mkStringExprFSLookup Name -> m Id
lookupM FastString
str = do
MkStringIds
mk <- forall (m :: * -> *).
Applicative m =>
(Name -> m Id) -> m MkStringIds
getMkStringIds Name -> m Id
lookupM
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MkStringIds -> FastString -> CoreExpr
mkStringExprFSWith MkStringIds
mk FastString
str)
getMkStringIds :: Applicative m => (Name -> m Id) -> m MkStringIds
getMkStringIds :: forall (m :: * -> *).
Applicative m =>
(Name -> m Id) -> m MkStringIds
getMkStringIds Name -> m Id
lookupM = Id -> Id -> MkStringIds
MkStringIds forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> m Id
lookupM Name
unpackCStringName forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Name -> m Id
lookupM Name
unpackCStringUtf8Name
data MkStringIds = MkStringIds
{ MkStringIds -> Id
unpackCStringId :: !Id
, MkStringIds -> Id
unpackCStringUtf8Id :: !Id
}
mkStringExprFSWith :: MkStringIds -> FastString -> CoreExpr
mkStringExprFSWith :: MkStringIds -> FastString -> CoreExpr
mkStringExprFSWith MkStringIds
ids FastString
str
| FastString -> Bool
nullFS FastString
str
= Kind -> CoreExpr
mkNilExpr Kind
charTy
| forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
safeChar String
chars
= let !unpack_id :: Id
unpack_id = MkStringIds -> Id
unpackCStringId MkStringIds
ids
in forall b. Expr b -> Expr b -> Expr b
App (forall b. Id -> Expr b
Var Id
unpack_id) CoreExpr
lit
| Bool
otherwise
= let !unpack_utf8_id :: Id
unpack_utf8_id = MkStringIds -> Id
unpackCStringUtf8Id MkStringIds
ids
in forall b. Expr b -> Expr b -> Expr b
App (forall b. Id -> Expr b
Var Id
unpack_utf8_id) CoreExpr
lit
where
chars :: String
chars = FastString -> String
unpackFS FastString
str
safeChar :: Char -> Bool
safeChar Char
c = Char -> Int
ord Char
c forall a. Ord a => a -> a -> Bool
>= Int
1 Bool -> Bool -> Bool
&& Char -> Int
ord Char
c forall a. Ord a => a -> a -> Bool
<= Int
0x7F
lit :: CoreExpr
lit = forall b. Literal -> Expr b
Lit (ByteString -> Literal
LitString (FastString -> ByteString
bytesFS FastString
str))
mkCoreBoxedTuple :: HasDebugCallStack => [CoreExpr] -> CoreExpr
mkCoreBoxedTuple :: HasDebugCallStack => [CoreExpr] -> CoreExpr
mkCoreBoxedTuple [CoreExpr]
cs
= forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Kind -> Bool
tcIsLiftedTypeKind forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasDebugCallStack => Kind -> Kind
typeKind forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasDebugCallStack => CoreExpr -> Kind
exprType) [CoreExpr]
cs) (forall a. Outputable a => a -> SDoc
ppr [CoreExpr]
cs)
DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps (Boxity -> Int -> DataCon
tupleDataCon Boxity
Boxed (forall (t :: * -> *) a. Foldable t => t a -> Int
length [CoreExpr]
cs))
(forall a b. (a -> b) -> [a] -> [b]
map (forall b. Kind -> Expr b
Type forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasDebugCallStack => CoreExpr -> Kind
exprType) [CoreExpr]
cs forall a. [a] -> [a] -> [a]
++ [CoreExpr]
cs)
mkCoreUnboxedTuple :: [CoreExpr] -> CoreExpr
mkCoreUnboxedTuple :: [CoreExpr] -> CoreExpr
mkCoreUnboxedTuple [CoreExpr]
exps
= DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps (Boxity -> Int -> DataCon
tupleDataCon Boxity
Unboxed (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Kind]
tys))
(forall a b. (a -> b) -> [a] -> [b]
map (forall b. Kind -> Expr b
Type forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasDebugCallStack => Kind -> Kind
getRuntimeRep) [Kind]
tys forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall b. Kind -> Expr b
Type [Kind]
tys forall a. [a] -> [a] -> [a]
++ [CoreExpr]
exps)
where
tys :: [Kind]
tys = forall a b. (a -> b) -> [a] -> [b]
map HasDebugCallStack => CoreExpr -> Kind
exprType [CoreExpr]
exps
mkCoreTupBoxity :: Boxity -> [CoreExpr] -> CoreExpr
mkCoreTupBoxity :: Boxity -> [CoreExpr] -> CoreExpr
mkCoreTupBoxity Boxity
Boxed [CoreExpr]
exps = HasDebugCallStack => [CoreExpr] -> CoreExpr
mkCoreBoxedTuple [CoreExpr]
exps
mkCoreTupBoxity Boxity
Unboxed [CoreExpr]
exps = [CoreExpr] -> CoreExpr
mkCoreUnboxedTuple [CoreExpr]
exps
mkCoreVarTupTy :: [Id] -> Type
mkCoreVarTupTy :: [Id] -> Kind
mkCoreVarTupTy [Id]
ids = [Kind] -> Kind
mkBoxedTupleTy (forall a b. (a -> b) -> [a] -> [b]
map Id -> Kind
idType [Id]
ids)
mkCoreTup :: [CoreExpr] -> CoreExpr
mkCoreTup :: [CoreExpr] -> CoreExpr
mkCoreTup [CoreExpr
c] = CoreExpr
c
mkCoreTup [CoreExpr]
cs = HasDebugCallStack => [CoreExpr] -> CoreExpr
mkCoreBoxedTuple [CoreExpr]
cs
mkCoreUnboxedSum :: Int -> Int -> [Type] -> CoreExpr -> CoreExpr
mkCoreUnboxedSum :: Int -> Int -> [Kind] -> CoreExpr -> CoreExpr
mkCoreUnboxedSum Int
arity Int
alt [Kind]
tys CoreExpr
exp
= forall a. HasCallStack => Bool -> a -> a
assert (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Kind]
tys forall a. Eq a => a -> a -> Bool
== Int
arity) forall a b. (a -> b) -> a -> b
$
forall a. HasCallStack => Bool -> a -> a
assert (Int
alt forall a. Ord a => a -> a -> Bool
<= Int
arity) forall a b. (a -> b) -> a -> b
$
DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps (Int -> Int -> DataCon
sumDataCon Int
alt Int
arity)
(forall a b. (a -> b) -> [a] -> [b]
map (forall b. Kind -> Expr b
Type forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasDebugCallStack => Kind -> Kind
getRuntimeRep) [Kind]
tys
forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall b. Kind -> Expr b
Type [Kind]
tys
forall a. [a] -> [a] -> [a]
++ [CoreExpr
exp])
mkBigCoreVarTupSolo :: [Id] -> CoreExpr
mkBigCoreVarTupSolo :: [Id] -> CoreExpr
mkBigCoreVarTupSolo [Id
id] = HasDebugCallStack => [CoreExpr] -> CoreExpr
mkCoreBoxedTuple [forall b. Id -> Expr b
Var Id
id]
mkBigCoreVarTupSolo [Id]
ids = forall a. ([a] -> a) -> [a] -> a
mkChunkified [CoreExpr] -> CoreExpr
mkCoreTup (forall a b. (a -> b) -> [a] -> [b]
map forall b. Id -> Expr b
Var [Id]
ids)
mkBigCoreVarTup :: [Id] -> CoreExpr
mkBigCoreVarTup :: [Id] -> CoreExpr
mkBigCoreVarTup [Id]
ids = [CoreExpr] -> CoreExpr
mkBigCoreTup (forall a b. (a -> b) -> [a] -> [b]
map forall b. Id -> Expr b
Var [Id]
ids)
mkBigCoreTup :: [CoreExpr] -> CoreExpr
mkBigCoreTup :: [CoreExpr] -> CoreExpr
mkBigCoreTup [CoreExpr]
exprs = forall a. ([a] -> a) -> [a] -> a
mkChunkified [CoreExpr] -> CoreExpr
mkCoreTup (forall a b. (a -> b) -> [a] -> [b]
map CoreExpr -> CoreExpr
wrapBox [CoreExpr]
exprs)
mkBigCoreVarTupTy :: [Id] -> Type
mkBigCoreVarTupTy :: [Id] -> Kind
mkBigCoreVarTupTy [Id]
ids = [Kind] -> Kind
mkBigCoreTupTy (forall a b. (a -> b) -> [a] -> [b]
map Id -> Kind
idType [Id]
ids)
mkBigCoreTupTy :: [Type] -> Type
mkBigCoreTupTy :: [Kind] -> Kind
mkBigCoreTupTy [Kind]
tys = forall a. ([a] -> a) -> [a] -> a
mkChunkified [Kind] -> Kind
mkBoxedTupleTy forall a b. (a -> b) -> a -> b
$
forall a b. (a -> b) -> [a] -> [b]
map Kind -> Kind
boxTy [Kind]
tys
unitExpr :: CoreExpr
unitExpr :: CoreExpr
unitExpr = forall b. Id -> Expr b
Var Id
unitDataConId
wrapBox :: CoreExpr -> CoreExpr
wrapBox :: CoreExpr -> CoreExpr
wrapBox CoreExpr
e
= case forall b. Kind -> BoxingInfo b
boxingDataCon Kind
e_ty of
BoxingInfo Id
BI_NoBoxNeeded -> CoreExpr
e
BI_Box { bi_inst_con :: forall b. BoxingInfo b -> Expr b
bi_inst_con = CoreExpr
boxing_expr } -> forall b. Expr b -> Expr b -> Expr b
App CoreExpr
boxing_expr CoreExpr
e
BoxingInfo Id
BI_NoBoxAvailable -> forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"wrapBox" (forall a. Outputable a => a -> SDoc
ppr CoreExpr
e forall doc. IsDoc doc => doc -> doc -> doc
$$ forall a. Outputable a => a -> SDoc
ppr (HasDebugCallStack => CoreExpr -> Kind
exprType CoreExpr
e))
where
e_ty :: Kind
e_ty = HasDebugCallStack => CoreExpr -> Kind
exprType CoreExpr
e
boxTy :: Type -> Type
boxTy :: Kind -> Kind
boxTy Kind
ty
= case forall b. Kind -> BoxingInfo b
boxingDataCon Kind
ty of
BoxingInfo Any
BI_NoBoxNeeded -> Kind
ty
BI_Box { bi_boxed_type :: forall b. BoxingInfo b -> Kind
bi_boxed_type = Kind
box_ty } -> Kind
box_ty
BoxingInfo Any
BI_NoBoxAvailable -> forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"boxTy" (forall a. Outputable a => a -> SDoc
ppr Kind
ty)
unwrapBox :: UniqSupply -> Id -> CoreExpr
-> (UniqSupply, Id, CoreExpr)
unwrapBox :: UniqSupply -> Id -> CoreExpr -> (UniqSupply, Id, CoreExpr)
unwrapBox UniqSupply
us Id
var CoreExpr
body
= case forall b. Kind -> BoxingInfo b
boxingDataCon Kind
var_ty of
BoxingInfo Any
BI_NoBoxNeeded -> (UniqSupply
us, Id
var, CoreExpr
body)
BoxingInfo Any
BI_NoBoxAvailable -> forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"unwrapBox" (forall a. Outputable a => a -> SDoc
ppr Id
var forall doc. IsDoc doc => doc -> doc -> doc
$$ forall a. Outputable a => a -> SDoc
ppr Kind
var_ty)
BI_Box { bi_data_con :: forall b. BoxingInfo b -> DataCon
bi_data_con = DataCon
box_con, bi_boxed_type :: forall b. BoxingInfo b -> Kind
bi_boxed_type = Kind
box_ty }
-> (UniqSupply
us', Id
var', CoreExpr
body')
where
var' :: Id
var' = FastString -> Unique -> Kind -> Kind -> Id
mkSysLocal (String -> FastString
fsLit String
"uc") Unique
uniq Kind
ManyTy Kind
box_ty
body' :: CoreExpr
body' = forall b. Expr b -> b -> Kind -> [Alt b] -> Expr b
Case (forall b. Id -> Expr b
Var Id
var') Id
var' (HasDebugCallStack => CoreExpr -> Kind
exprType CoreExpr
body)
[forall b. AltCon -> [b] -> Expr b -> Alt b
Alt (DataCon -> AltCon
DataAlt DataCon
box_con) [Id
var] CoreExpr
body]
where
var_ty :: Kind
var_ty = Id -> Kind
idType Id
var
(Unique
uniq, UniqSupply
us') = UniqSupply -> (Unique, UniqSupply)
takeUniqFromSupply UniqSupply
us
mkChunkified :: ([a] -> a)
-> [a]
-> a
mkChunkified :: forall a. ([a] -> a) -> [a] -> a
mkChunkified [a] -> a
small_tuple [a]
as = [[a]] -> a
mk_big_tuple (forall a. [a] -> [[a]]
chunkify [a]
as)
where
mk_big_tuple :: [[a]] -> a
mk_big_tuple [[a]
as] = [a] -> a
small_tuple [a]
as
mk_big_tuple [[a]]
as_s = [[a]] -> a
mk_big_tuple (forall a. [a] -> [[a]]
chunkify (forall a b. (a -> b) -> [a] -> [b]
map [a] -> a
small_tuple [[a]]
as_s))
chunkify :: [a] -> [[a]]
chunkify :: forall a. [a] -> [[a]]
chunkify [a]
xs
| Int
n_xs forall a. Ord a => a -> a -> Bool
<= Int
mAX_TUPLE_SIZE = [[a]
xs]
| Bool
otherwise = forall a. [a] -> [[a]]
split [a]
xs
where
n_xs :: Int
n_xs = forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs
split :: [a] -> [[a]]
split [] = []
split [a]
xs = forall a. Int -> [a] -> [a]
take Int
mAX_TUPLE_SIZE [a]
xs forall a. a -> [a] -> [a]
: [a] -> [[a]]
split (forall a. Int -> [a] -> [a]
drop Int
mAX_TUPLE_SIZE [a]
xs)
mkBigTupleSelector, mkBigTupleSelectorSolo
:: [Id]
-> Id
-> Id
-> CoreExpr
-> CoreExpr
mkBigTupleSelector :: [Id] -> Id -> Id -> CoreExpr -> CoreExpr
mkBigTupleSelector [Id]
vars Id
the_var Id
scrut_var CoreExpr
scrut
= [[Id]] -> Id -> CoreExpr
mk_tup_sel (forall a. [a] -> [[a]]
chunkify [Id]
vars) Id
the_var
where
mk_tup_sel :: [[Id]] -> Id -> CoreExpr
mk_tup_sel [[Id]
vars] Id
the_var = [Id] -> Id -> Id -> CoreExpr -> CoreExpr
mkSmallTupleSelector [Id]
vars Id
the_var Id
scrut_var CoreExpr
scrut
mk_tup_sel [[Id]]
vars_s Id
the_var = [Id] -> Id -> Id -> CoreExpr -> CoreExpr
mkSmallTupleSelector [Id]
group Id
the_var Id
tpl_v forall a b. (a -> b) -> a -> b
$
[[Id]] -> Id -> CoreExpr
mk_tup_sel (forall a. [a] -> [[a]]
chunkify [Id]
tpl_vs) Id
tpl_v
where
tpl_tys :: [Kind]
tpl_tys = [[Kind] -> Kind
mkBoxedTupleTy (forall a b. (a -> b) -> [a] -> [b]
map Id -> Kind
idType [Id]
gp) | [Id]
gp <- [[Id]]
vars_s]
tpl_vs :: [Id]
tpl_vs = [Kind] -> [Id]
mkTemplateLocals [Kind]
tpl_tys
[(Id
tpl_v, [Id]
group)] = [(Id
tpl,[Id]
gp) | (Id
tpl,[Id]
gp) <- forall a b. HasDebugCallStack => String -> [a] -> [b] -> [(a, b)]
zipEqual String
"mkBigTupleSelector" [Id]
tpl_vs [[Id]]
vars_s,
Id
the_var forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Id]
gp ]
mkBigTupleSelectorSolo :: [Id] -> Id -> Id -> CoreExpr -> CoreExpr
mkBigTupleSelectorSolo [Id]
vars Id
the_var Id
scrut_var CoreExpr
scrut
| [Id
_] <- [Id]
vars
= [Id] -> Id -> Id -> CoreExpr -> CoreExpr
mkSmallTupleSelector1 [Id]
vars Id
the_var Id
scrut_var CoreExpr
scrut
| Bool
otherwise
= [Id] -> Id -> Id -> CoreExpr -> CoreExpr
mkBigTupleSelector [Id]
vars Id
the_var Id
scrut_var CoreExpr
scrut
mkSmallTupleSelector, mkSmallTupleSelector1
:: [Id]
-> Id
-> Id
-> CoreExpr
-> CoreExpr
mkSmallTupleSelector :: [Id] -> Id -> Id -> CoreExpr -> CoreExpr
mkSmallTupleSelector [Id
var] Id
should_be_the_same_var Id
_ CoreExpr
scrut
= forall a. HasCallStack => Bool -> a -> a
assert (Id
var forall a. Eq a => a -> a -> Bool
== Id
should_be_the_same_var) forall a b. (a -> b) -> a -> b
$
CoreExpr
scrut
mkSmallTupleSelector [Id]
vars Id
the_var Id
scrut_var CoreExpr
scrut
= [Id] -> Id -> Id -> CoreExpr -> CoreExpr
mkSmallTupleSelector1 [Id]
vars Id
the_var Id
scrut_var CoreExpr
scrut
mkSmallTupleSelector1 :: [Id] -> Id -> Id -> CoreExpr -> CoreExpr
mkSmallTupleSelector1 [Id]
vars Id
the_var Id
scrut_var CoreExpr
scrut
= forall a. HasCallStack => Bool -> a -> a
assert (forall (f :: * -> *) a. Foldable f => f a -> Bool
notNull [Id]
vars) forall a b. (a -> b) -> a -> b
$
forall b. Expr b -> b -> Kind -> [Alt b] -> Expr b
Case CoreExpr
scrut Id
scrut_var (Id -> Kind
idType Id
the_var)
[forall b. AltCon -> [b] -> Expr b -> Alt b
Alt (DataCon -> AltCon
DataAlt (Boxity -> Int -> DataCon
tupleDataCon Boxity
Boxed (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Id]
vars))) [Id]
vars (forall b. Id -> Expr b
Var Id
the_var)]
mkBigTupleCase :: UniqSupply
-> [Id]
-> CoreExpr
-> CoreExpr
-> CoreExpr
mkBigTupleCase :: UniqSupply -> [Id] -> CoreExpr -> CoreExpr -> CoreExpr
mkBigTupleCase UniqSupply
us [Id]
vars CoreExpr
body CoreExpr
scrut
= UniqSupply -> [[Id]] -> CoreExpr -> CoreExpr
mk_tuple_case UniqSupply
wrapped_us (forall a. [a] -> [[a]]
chunkify [Id]
wrapped_vars) CoreExpr
wrapped_body
where
(UniqSupply
wrapped_us, [Id]
wrapped_vars, CoreExpr
wrapped_body) = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Id -> (UniqSupply, [Id], CoreExpr) -> (UniqSupply, [Id], CoreExpr)
unwrap (UniqSupply
us,[],CoreExpr
body) [Id]
vars
scrut_ty :: Kind
scrut_ty = HasDebugCallStack => CoreExpr -> Kind
exprType CoreExpr
scrut
unwrap :: Id -> (UniqSupply, [Id], CoreExpr) -> (UniqSupply, [Id], CoreExpr)
unwrap Id
var (UniqSupply
us,[Id]
vars,CoreExpr
body)
= (UniqSupply
us', Id
var'forall a. a -> [a] -> [a]
:[Id]
vars, CoreExpr
body')
where
(UniqSupply
us', Id
var', CoreExpr
body') = UniqSupply -> Id -> CoreExpr -> (UniqSupply, Id, CoreExpr)
unwrapBox UniqSupply
us Id
var CoreExpr
body
mk_tuple_case :: UniqSupply -> [[Id]] -> CoreExpr -> CoreExpr
mk_tuple_case :: UniqSupply -> [[Id]] -> CoreExpr -> CoreExpr
mk_tuple_case UniqSupply
us [[Id]
vars] CoreExpr
body
= [Id] -> CoreExpr -> Id -> CoreExpr -> CoreExpr
mkSmallTupleCase [Id]
vars CoreExpr
body Id
scrut_var CoreExpr
scrut
where
scrut_var :: Id
scrut_var = case CoreExpr
scrut of
Var Id
v -> Id
v
CoreExpr
_ -> forall a b. (a, b) -> b
snd (UniqSupply -> Kind -> (UniqSupply, Id)
new_var UniqSupply
us Kind
scrut_ty)
mk_tuple_case UniqSupply
us [[Id]]
vars_s CoreExpr
body
= UniqSupply -> [[Id]] -> CoreExpr -> CoreExpr
mk_tuple_case UniqSupply
us' (forall a. [a] -> [[a]]
chunkify [Id]
vars') CoreExpr
body'
where
(UniqSupply
us', [Id]
vars', CoreExpr
body') = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr [Id]
-> (UniqSupply, [Id], CoreExpr) -> (UniqSupply, [Id], CoreExpr)
one_tuple_case (UniqSupply
us, [], CoreExpr
body) [[Id]]
vars_s
one_tuple_case :: [Id]
-> (UniqSupply, [Id], CoreExpr) -> (UniqSupply, [Id], CoreExpr)
one_tuple_case [Id]
chunk_vars (UniqSupply
us, [Id]
vs, CoreExpr
body)
= (UniqSupply
us', Id
scrut_varforall a. a -> [a] -> [a]
:[Id]
vs, CoreExpr
body')
where
tup_ty :: Kind
tup_ty = [Kind] -> Kind
mkBoxedTupleTy (forall a b. (a -> b) -> [a] -> [b]
map Id -> Kind
idType [Id]
chunk_vars)
(UniqSupply
us', Id
scrut_var) = UniqSupply -> Kind -> (UniqSupply, Id)
new_var UniqSupply
us Kind
tup_ty
body' :: CoreExpr
body' = [Id] -> CoreExpr -> Id -> CoreExpr -> CoreExpr
mkSmallTupleCase [Id]
chunk_vars CoreExpr
body Id
scrut_var (forall b. Id -> Expr b
Var Id
scrut_var)
new_var :: UniqSupply -> Type -> (UniqSupply, Id)
new_var :: UniqSupply -> Kind -> (UniqSupply, Id)
new_var UniqSupply
us Kind
ty = (UniqSupply
us', Id
id)
where
(Unique
uniq, UniqSupply
us') = UniqSupply -> (Unique, UniqSupply)
takeUniqFromSupply UniqSupply
us
id :: Id
id = FastString -> Unique -> Kind -> Kind -> Id
mkSysLocal (String -> FastString
fsLit String
"ds") Unique
uniq Kind
ManyTy Kind
ty
mkSmallTupleCase
:: [Id]
-> CoreExpr
-> Id
-> CoreExpr
-> CoreExpr
mkSmallTupleCase :: [Id] -> CoreExpr -> Id -> CoreExpr -> CoreExpr
mkSmallTupleCase [Id
var] CoreExpr
body Id
_scrut_var CoreExpr
scrut
= HasDebugCallStack => Id -> CoreExpr -> CoreExpr -> CoreExpr
bindNonRec Id
var CoreExpr
scrut CoreExpr
body
mkSmallTupleCase [Id]
vars CoreExpr
body Id
scrut_var CoreExpr
scrut
= forall b. Expr b -> b -> Kind -> [Alt b] -> Expr b
Case CoreExpr
scrut Id
scrut_var (HasDebugCallStack => CoreExpr -> Kind
exprType CoreExpr
body)
[forall b. AltCon -> [b] -> Expr b -> Alt b
Alt (DataCon -> AltCon
DataAlt (Boxity -> Int -> DataCon
tupleDataCon Boxity
Boxed (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Id]
vars))) [Id]
vars CoreExpr
body]
data FloatBind
= FloatLet CoreBind
| FloatCase CoreExpr Id AltCon [Var]
instance Outputable FloatBind where
ppr :: FloatBind -> SDoc
ppr (FloatLet CoreBind
b) = forall doc. IsLine doc => String -> doc
text String
"LET" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr CoreBind
b
ppr (FloatCase CoreExpr
e Id
b AltCon
c [Id]
bs) = SDoc -> Int -> SDoc -> SDoc
hang (forall doc. IsLine doc => String -> doc
text String
"CASE" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr CoreExpr
e forall doc. IsLine doc => doc -> doc -> doc
<+> forall doc. IsLine doc => String -> doc
text String
"of" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr Id
b)
Int
2 (forall a. Outputable a => a -> SDoc
ppr AltCon
c forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr [Id]
bs)
wrapFloat :: FloatBind -> CoreExpr -> CoreExpr
wrapFloat :: FloatBind -> CoreExpr -> CoreExpr
wrapFloat (FloatLet CoreBind
defns) CoreExpr
body = forall b. Bind b -> Expr b -> Expr b
Let CoreBind
defns CoreExpr
body
wrapFloat (FloatCase CoreExpr
e Id
b AltCon
con [Id]
bs) CoreExpr
body = CoreExpr -> Id -> AltCon -> [Id] -> CoreExpr -> CoreExpr
mkSingleAltCase CoreExpr
e Id
b AltCon
con [Id]
bs CoreExpr
body
wrapFloats :: [FloatBind] -> CoreExpr -> CoreExpr
wrapFloats :: [FloatBind] -> CoreExpr -> CoreExpr
wrapFloats [FloatBind]
floats CoreExpr
expr = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr FloatBind -> CoreExpr -> CoreExpr
wrapFloat CoreExpr
expr [FloatBind]
floats
bindBindings :: CoreBind -> [Var]
bindBindings :: CoreBind -> [Id]
bindBindings (NonRec Id
b CoreExpr
_) = [Id
b]
bindBindings (Rec [(Id, CoreExpr)]
bnds) = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(Id, CoreExpr)]
bnds
floatBindings :: FloatBind -> [Var]
floatBindings :: FloatBind -> [Id]
floatBindings (FloatLet CoreBind
bnd) = CoreBind -> [Id]
bindBindings CoreBind
bnd
floatBindings (FloatCase CoreExpr
_ Id
b AltCon
_ [Id]
bs) = Id
bforall a. a -> [a] -> [a]
:[Id]
bs
mkNilExpr :: Type -> CoreExpr
mkNilExpr :: Kind -> CoreExpr
mkNilExpr Kind
ty = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
nilDataCon [forall b. Kind -> Expr b
Type Kind
ty]
mkConsExpr :: Type -> CoreExpr -> CoreExpr -> CoreExpr
mkConsExpr :: Kind -> CoreExpr -> CoreExpr -> CoreExpr
mkConsExpr Kind
ty CoreExpr
hd CoreExpr
tl = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
consDataCon [forall b. Kind -> Expr b
Type Kind
ty, CoreExpr
hd, CoreExpr
tl]
mkListExpr :: Type -> [CoreExpr] -> CoreExpr
mkListExpr :: Kind -> [CoreExpr] -> CoreExpr
mkListExpr Kind
ty [CoreExpr]
xs = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Kind -> CoreExpr -> CoreExpr -> CoreExpr
mkConsExpr Kind
ty) (Kind -> CoreExpr
mkNilExpr Kind
ty) [CoreExpr]
xs
mkFoldrExpr :: MonadThings m
=> Type
-> Type
-> CoreExpr
-> CoreExpr
-> CoreExpr
-> m CoreExpr
mkFoldrExpr :: forall (m :: * -> *).
MonadThings m =>
Kind -> Kind -> CoreExpr -> CoreExpr -> CoreExpr -> m CoreExpr
mkFoldrExpr Kind
elt_ty Kind
result_ty CoreExpr
c CoreExpr
n CoreExpr
list = do
Id
foldr_id <- forall (m :: * -> *). MonadThings m => Name -> m Id
lookupId Name
foldrName
forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. Id -> Expr b
Var Id
foldr_id forall b. Expr b -> Expr b -> Expr b
`App` forall b. Kind -> Expr b
Type Kind
elt_ty
forall b. Expr b -> Expr b -> Expr b
`App` forall b. Kind -> Expr b
Type Kind
result_ty
forall b. Expr b -> Expr b -> Expr b
`App` CoreExpr
c
forall b. Expr b -> Expr b -> Expr b
`App` CoreExpr
n
forall b. Expr b -> Expr b -> Expr b
`App` CoreExpr
list)
mkBuildExpr :: (MonadFail m, MonadThings m, MonadUnique m)
=> Type
-> ((Id, Type) -> (Id, Type) -> m CoreExpr)
-> m CoreExpr
mkBuildExpr :: forall (m :: * -> *).
(MonadFail m, MonadThings m, MonadUnique m) =>
Kind -> ((Id, Kind) -> (Id, Kind) -> m CoreExpr) -> m CoreExpr
mkBuildExpr Kind
elt_ty (Id, Kind) -> (Id, Kind) -> m CoreExpr
mk_build_inside = do
Id
n_tyvar <- forall {m :: * -> *}. MonadUnique m => Id -> m Id
newTyVar Id
alphaTyVar
let n_ty :: Kind
n_ty = Id -> Kind
mkTyVarTy Id
n_tyvar
c_ty :: Kind
c_ty = [Kind] -> Kind -> Kind
mkVisFunTysMany [Kind
elt_ty, Kind
n_ty] Kind
n_ty
[Id
c, Id
n] <- forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [forall (m :: * -> *).
MonadUnique m =>
FastString -> Kind -> Kind -> m Id
mkSysLocalM (String -> FastString
fsLit String
"c") Kind
ManyTy Kind
c_ty, forall (m :: * -> *).
MonadUnique m =>
FastString -> Kind -> Kind -> m Id
mkSysLocalM (String -> FastString
fsLit String
"n") Kind
ManyTy Kind
n_ty]
CoreExpr
build_inside <- (Id, Kind) -> (Id, Kind) -> m CoreExpr
mk_build_inside (Id
c, Kind
c_ty) (Id
n, Kind
n_ty)
Id
build_id <- forall (m :: * -> *). MonadThings m => Name -> m Id
lookupId Name
buildName
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall b. Id -> Expr b
Var Id
build_id forall b. Expr b -> Expr b -> Expr b
`App` forall b. Kind -> Expr b
Type Kind
elt_ty forall b. Expr b -> Expr b -> Expr b
`App` forall b. [b] -> Expr b -> Expr b
mkLams [Id
n_tyvar, Id
c, Id
n] CoreExpr
build_inside
where
newTyVar :: Id -> m Id
newTyVar Id
tyvar_tmpl = do
Unique
uniq <- forall (m :: * -> *). MonadUnique m => m Unique
getUniqueM
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> Unique -> Id
setTyVarUnique Id
tyvar_tmpl Unique
uniq)
mkNothingExpr :: Type -> CoreExpr
mkNothingExpr :: Kind -> CoreExpr
mkNothingExpr Kind
ty = forall b. DataCon -> [Arg b] -> Arg b
mkConApp DataCon
nothingDataCon [forall b. Kind -> Expr b
Type Kind
ty]
mkJustExpr :: Type -> CoreExpr -> CoreExpr
mkJustExpr :: Kind -> CoreExpr -> CoreExpr
mkJustExpr Kind
ty CoreExpr
val = forall b. DataCon -> [Arg b] -> Arg b
mkConApp DataCon
justDataCon [forall b. Kind -> Expr b
Type Kind
ty, CoreExpr
val]
mkRuntimeErrorApp
:: Id
-> Type
-> String
-> CoreExpr
mkRuntimeErrorApp :: Id -> Kind -> String -> CoreExpr
mkRuntimeErrorApp Id
err_id Kind
res_ty String
err_msg
= forall b. Expr b -> [Expr b] -> Expr b
mkApps (forall b. Id -> Expr b
Var Id
err_id) [ forall b. Kind -> Expr b
Type (HasDebugCallStack => Kind -> Kind
getRuntimeRep Kind
res_ty)
, forall b. Kind -> Expr b
Type Kind
res_ty, CoreExpr
err_string ]
where
err_string :: CoreExpr
err_string = forall b. Literal -> Expr b
Lit (String -> Literal
mkLitString String
err_msg)
errorIds :: [Id]
errorIds :: [Id]
errorIds
= [ Id
nON_EXHAUSTIVE_GUARDS_ERROR_ID,
Id
nO_METHOD_BINDING_ERROR_ID,
Id
pAT_ERROR_ID,
Id
rEC_CON_ERROR_ID,
Id
rEC_SEL_ERROR_ID,
Id
iMPOSSIBLE_ERROR_ID, Id
iMPOSSIBLE_CONSTRAINT_ERROR_ID,
Id
aBSENT_ERROR_ID, Id
aBSENT_CONSTRAINT_ERROR_ID,
Id
aBSENT_SUM_FIELD_ERROR_ID,
Id
tYPE_ERROR_ID
]
recSelErrorName, recConErrorName, patErrorName :: Name
nonExhaustiveGuardsErrorName, noMethodBindingErrorName :: Name
typeErrorName :: Name
absentSumFieldErrorName :: Name
recSelErrorName :: Name
recSelErrorName = String -> Unique -> Id -> Name
err_nm String
"recSelError" Unique
recSelErrorIdKey Id
rEC_SEL_ERROR_ID
recConErrorName :: Name
recConErrorName = String -> Unique -> Id -> Name
err_nm String
"recConError" Unique
recConErrorIdKey Id
rEC_CON_ERROR_ID
patErrorName :: Name
patErrorName = String -> Unique -> Id -> Name
err_nm String
"patError" Unique
patErrorIdKey Id
pAT_ERROR_ID
typeErrorName :: Name
typeErrorName = String -> Unique -> Id -> Name
err_nm String
"typeError" Unique
typeErrorIdKey Id
tYPE_ERROR_ID
noMethodBindingErrorName :: Name
noMethodBindingErrorName = String -> Unique -> Id -> Name
err_nm String
"noMethodBindingError"
Unique
noMethodBindingErrorIdKey Id
nO_METHOD_BINDING_ERROR_ID
nonExhaustiveGuardsErrorName :: Name
nonExhaustiveGuardsErrorName = String -> Unique -> Id -> Name
err_nm String
"nonExhaustiveGuardsError"
Unique
nonExhaustiveGuardsErrorIdKey Id
nON_EXHAUSTIVE_GUARDS_ERROR_ID
err_nm :: String -> Unique -> Id -> Name
err_nm :: String -> Unique -> Id -> Name
err_nm String
str Unique
uniq Id
id = Module -> FastString -> Unique -> Id -> Name
mkWiredInIdName Module
cONTROL_EXCEPTION_BASE (String -> FastString
fsLit String
str) Unique
uniq Id
id
rEC_SEL_ERROR_ID, rEC_CON_ERROR_ID :: Id
pAT_ERROR_ID, nO_METHOD_BINDING_ERROR_ID, nON_EXHAUSTIVE_GUARDS_ERROR_ID :: Id
tYPE_ERROR_ID, aBSENT_SUM_FIELD_ERROR_ID :: Id
rEC_SEL_ERROR_ID :: Id
rEC_SEL_ERROR_ID = TypeOrConstraint -> Name -> Id
mkRuntimeErrorId TypeOrConstraint
TypeLike Name
recSelErrorName
rEC_CON_ERROR_ID :: Id
rEC_CON_ERROR_ID = TypeOrConstraint -> Name -> Id
mkRuntimeErrorId TypeOrConstraint
TypeLike Name
recConErrorName
pAT_ERROR_ID :: Id
pAT_ERROR_ID = TypeOrConstraint -> Name -> Id
mkRuntimeErrorId TypeOrConstraint
TypeLike Name
patErrorName
nO_METHOD_BINDING_ERROR_ID :: Id
nO_METHOD_BINDING_ERROR_ID = TypeOrConstraint -> Name -> Id
mkRuntimeErrorId TypeOrConstraint
TypeLike Name
noMethodBindingErrorName
nON_EXHAUSTIVE_GUARDS_ERROR_ID :: Id
nON_EXHAUSTIVE_GUARDS_ERROR_ID = TypeOrConstraint -> Name -> Id
mkRuntimeErrorId TypeOrConstraint
TypeLike Name
nonExhaustiveGuardsErrorName
tYPE_ERROR_ID :: Id
tYPE_ERROR_ID = TypeOrConstraint -> Name -> Id
mkRuntimeErrorId TypeOrConstraint
TypeLike Name
typeErrorName
absentSumFieldErrorName :: Name
absentSumFieldErrorName
= Module -> FastString -> Unique -> Id -> Name
mkWiredInIdName
Module
gHC_PRIM_PANIC
(String -> FastString
fsLit String
"absentSumFieldError")
Unique
absentSumFieldErrorIdKey
Id
aBSENT_SUM_FIELD_ERROR_ID
aBSENT_SUM_FIELD_ERROR_ID :: Id
aBSENT_SUM_FIELD_ERROR_ID = Name -> Id
mkExceptionId Name
absentSumFieldErrorName
mkExceptionId :: Name -> Id
mkExceptionId :: Name -> Id
mkExceptionId Name
name
= Name -> Kind -> IdInfo -> Id
mkVanillaGlobalWithInfo Name
name
([Id] -> Kind -> Kind
mkSpecForAllTys [Id
alphaTyVar] (Id -> Kind
mkTyVarTy Id
alphaTyVar))
([Demand] -> IdInfo
divergingIdInfo [] IdInfo -> CafInfo -> IdInfo
`setCafInfo` CafInfo
NoCafRefs)
divergingIdInfo :: [Demand] -> IdInfo
divergingIdInfo :: [Demand] -> IdInfo
divergingIdInfo [Demand]
arg_dmds
= IdInfo
vanillaIdInfo IdInfo -> Int -> IdInfo
`setArityInfo` Int
arity
IdInfo -> DmdSig -> IdInfo
`setDmdSigInfo` [Demand] -> Divergence -> DmdSig
mkClosedDmdSig [Demand]
arg_dmds Divergence
botDiv
IdInfo -> CprSig -> IdInfo
`setCprSigInfo` Int -> Cpr -> CprSig
mkCprSig Int
arity Cpr
botCpr
where
arity :: Int
arity = forall (t :: * -> *) a. Foldable t => t a -> Int
length [Demand]
arg_dmds
iMPOSSIBLE_ERROR_ID, iMPOSSIBLE_CONSTRAINT_ERROR_ID :: Id
iMPOSSIBLE_ERROR_ID :: Id
iMPOSSIBLE_ERROR_ID = TypeOrConstraint -> Name -> Id
mkRuntimeErrorId TypeOrConstraint
TypeLike Name
impossibleErrorName
iMPOSSIBLE_CONSTRAINT_ERROR_ID :: Id
iMPOSSIBLE_CONSTRAINT_ERROR_ID = TypeOrConstraint -> Name -> Id
mkRuntimeErrorId TypeOrConstraint
ConstraintLike Name
impossibleConstraintErrorName
impossibleErrorName, impossibleConstraintErrorName :: Name
impossibleErrorName :: Name
impossibleErrorName = String -> Unique -> Id -> Name
err_nm String
"impossibleError"
Unique
impossibleErrorIdKey Id
iMPOSSIBLE_ERROR_ID
impossibleConstraintErrorName :: Name
impossibleConstraintErrorName = String -> Unique -> Id -> Name
err_nm String
"impossibleConstraintError"
Unique
impossibleConstraintErrorIdKey Id
iMPOSSIBLE_CONSTRAINT_ERROR_ID
mkImpossibleExpr :: Type -> String -> CoreExpr
mkImpossibleExpr :: Kind -> String -> CoreExpr
mkImpossibleExpr Kind
res_ty String
str
= Id -> Kind -> String -> CoreExpr
mkRuntimeErrorApp Id
err_id Kind
res_ty String
str
where
err_id :: Id
err_id | Kind -> Bool
isConstraintLikeKind (HasDebugCallStack => Kind -> Kind
typeKind Kind
res_ty) = Id
iMPOSSIBLE_CONSTRAINT_ERROR_ID
| Bool
otherwise = Id
iMPOSSIBLE_ERROR_ID
mkAbsentErrorApp :: Type
-> String
-> CoreExpr
mkAbsentErrorApp :: Kind -> String -> CoreExpr
mkAbsentErrorApp Kind
res_ty String
err_msg
= forall b. Expr b -> [Expr b] -> Expr b
mkApps (forall b. Id -> Expr b
Var Id
err_id) [ forall b. Kind -> Expr b
Type Kind
res_ty, CoreExpr
err_string ]
where
err_id :: Id
err_id | Kind -> Bool
isConstraintLikeKind (HasDebugCallStack => Kind -> Kind
typeKind Kind
res_ty) = Id
aBSENT_CONSTRAINT_ERROR_ID
| Bool
otherwise = Id
aBSENT_ERROR_ID
err_string :: CoreExpr
err_string = forall b. Literal -> Expr b
Lit (String -> Literal
mkLitString String
err_msg)
absentErrorName, absentConstraintErrorName :: Name
absentErrorName :: Name
absentErrorName
= Module -> FastString -> Unique -> Id -> Name
mkWiredInIdName Module
gHC_PRIM_PANIC (String -> FastString
fsLit String
"absentError")
Unique
absentErrorIdKey Id
aBSENT_ERROR_ID
absentConstraintErrorName :: Name
absentConstraintErrorName
= Module -> FastString -> Unique -> Id -> Name
mkWiredInIdName Module
gHC_PRIM_PANIC (String -> FastString
fsLit String
"absentConstraintError")
Unique
absentConstraintErrorIdKey Id
aBSENT_CONSTRAINT_ERROR_ID
aBSENT_ERROR_ID, aBSENT_CONSTRAINT_ERROR_ID :: Id
aBSENT_ERROR_ID :: Id
aBSENT_ERROR_ID
= Name -> Kind -> Id
mk_runtime_error_id Name
absentErrorName Kind
absent_ty
where
absent_ty :: Kind
absent_ty = [Id] -> Kind -> Kind
mkSpecForAllTys [Id
alphaTyVar] forall a b. (a -> b) -> a -> b
$
HasDebugCallStack => Kind -> Kind -> Kind
mkVisFunTyMany Kind
addrPrimTy (Id -> Kind
mkTyVarTy Id
alphaTyVar)
aBSENT_CONSTRAINT_ERROR_ID :: Id
aBSENT_CONSTRAINT_ERROR_ID
= Name -> Kind -> Id
mk_runtime_error_id Name
absentConstraintErrorName Kind
absent_ty
where
absent_ty :: Kind
absent_ty = [Id] -> Kind -> Kind
mkSpecForAllTys [Id
alphaConstraintTyVar] forall a b. (a -> b) -> a -> b
$
HasDebugCallStack => FunTyFlag -> Kind -> Kind -> Kind -> Kind
mkFunTy FunTyFlag
visArgConstraintLike Kind
ManyTy
Kind
addrPrimTy (Id -> Kind
mkTyVarTy Id
alphaConstraintTyVar)
mkRuntimeErrorId :: TypeOrConstraint -> Name -> Id
mkRuntimeErrorId :: TypeOrConstraint -> Name -> Id
mkRuntimeErrorId TypeOrConstraint
torc Name
name = Name -> Kind -> Id
mk_runtime_error_id Name
name (TypeOrConstraint -> Kind
mkRuntimeErrorTy TypeOrConstraint
torc)
mk_runtime_error_id :: Name -> Type -> Id
mk_runtime_error_id :: Name -> Kind -> Id
mk_runtime_error_id Name
name Kind
ty
= Name -> Kind -> IdInfo -> Id
mkVanillaGlobalWithInfo Name
name Kind
ty ([Demand] -> IdInfo
divergingIdInfo [Demand
evalDmd])
mkRuntimeErrorTy :: TypeOrConstraint -> Type
mkRuntimeErrorTy :: TypeOrConstraint -> Kind
mkRuntimeErrorTy TypeOrConstraint
torc = [Id] -> Kind -> Kind
mkSpecForAllTys [Id
runtimeRep1TyVar, Id
tyvar] forall a b. (a -> b) -> a -> b
$
HasDebugCallStack => Kind -> Kind -> Kind -> Kind
mkFunctionType Kind
ManyTy Kind
addrPrimTy (Id -> Kind
mkTyVarTy Id
tyvar)
where
(Id
tyvar:[Id]
_) = [Kind] -> [Id]
mkTemplateTyVars [Kind
kind]
kind :: Kind
kind = case TypeOrConstraint
torc of
TypeOrConstraint
TypeLike -> Kind -> Kind
mkTYPEapp Kind
runtimeRep1Ty
TypeOrConstraint
ConstraintLike -> Kind -> Kind
mkCONSTRAINTapp Kind
runtimeRep1Ty