{-# LANGUAGE TypeFamilies #-}
module GHC.HsToCore.ListComp ( dsListComp, dsMonadComp ) where
import GHC.Prelude
import {-# SOURCE #-} GHC.HsToCore.Expr ( dsExpr, dsLExpr, dsLocalBinds, dsSyntaxExpr )
import GHC.Hs
import GHC.Hs.Syn.Type
import GHC.Core
import GHC.Core.Make
import GHC.HsToCore.Monad
import GHC.HsToCore.Utils
import GHC.Driver.DynFlags
import GHC.Core.Utils
import GHC.Types.Id
import GHC.Core.Type
import GHC.Builtin.Types
import GHC.HsToCore.Match
import GHC.Builtin.Names
import GHC.Types.SrcLoc
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Tc.Utils.TcType
import GHC.Data.List.SetOps( getNth )
dsListComp :: [ExprLStmt GhcTc]
-> Type
-> DsM CoreExpr
dsListComp :: [ExprLStmt GhcTc] -> Type -> DsM CoreExpr
dsListComp [ExprLStmt GhcTc]
lquals Type
res_ty = do
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let quals = (GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> [GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
-> [Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
forall a b. (a -> b) -> [a] -> [b]
map GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall l e. GenLocated l e -> e
unLoc [ExprLStmt GhcTc]
[GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
lquals
elt_ty = case Type -> [Type]
tcTyConAppArgs Type
res_ty of
[Type
elt_ty] -> Type
elt_ty
[Type]
_ -> String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"dsListComp" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
res_ty SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ [GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
-> SDoc
forall a. Outputable a => a -> SDoc
ppr [ExprLStmt GhcTc]
[GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
lquals)
if not (gopt Opt_EnableRewriteRules dflags) || gopt Opt_IgnoreInterfacePragmas dflags
|| isParallelComp quals
then deListComp quals (mkNilExpr elt_ty)
else mkBuildExpr elt_ty (\(Id
c, Type
_) (Id
n, Type
_) -> Id -> Id -> [ExprStmt GhcTc] -> DsM CoreExpr
dfListComp Id
c Id
n [ExprStmt GhcTc]
[Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
quals)
where
isParallelComp :: [StmtLR idL idR body] -> Bool
isParallelComp = (StmtLR idL idR body -> Bool) -> [StmtLR idL idR body] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StmtLR idL idR body -> Bool
forall {idL} {idR} {body}. StmtLR idL idR body -> Bool
isParallelStmt
isParallelStmt :: StmtLR idL idR body -> Bool
isParallelStmt (ParStmt {}) = Bool
True
isParallelStmt StmtLR idL idR body
_ = Bool
False
dsInnerListComp :: (ParStmtBlock GhcTc GhcTc) -> DsM (CoreExpr, Type)
dsInnerListComp :: ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type)
dsInnerListComp (ParStmtBlock XParStmtBlock GhcTc GhcTc
_ [ExprLStmt GhcTc]
stmts [IdP GhcTc]
bndrs SyntaxExpr GhcTc
_)
= do { let bndrs_tuple_type :: Type
bndrs_tuple_type = [Id] -> Type
HasDebugCallStack => [Id] -> Type
mkBigCoreVarTupTy [IdP GhcTc]
[Id]
bndrs
list_ty :: Type
list_ty = Type -> Type
mkListTy Type
bndrs_tuple_type
; expr <- [ExprLStmt GhcTc] -> Type -> DsM CoreExpr
dsListComp ([ExprLStmt GhcTc]
[GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
stmts [GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
-> [GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
-> [GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
forall a. [a] -> [a] -> [a]
++ [Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))))
-> Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
forall a b. (a -> b) -> a -> b
$ GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall (idR :: Pass) (bodyR :: * -> *) (idL :: Pass).
IsPass idR =>
LocatedA (bodyR (GhcPass idR))
-> StmtLR
(GhcPass idL) (GhcPass idR) (LocatedA (bodyR (GhcPass idR)))
mkLastStmt ([Id] -> LHsExpr GhcTc
mkBigLHsVarTupId [IdP GhcTc]
[Id]
bndrs)]) Type
list_ty
; return (expr, bndrs_tuple_type) }
dsTransStmt :: ExprStmt GhcTc -> DsM (CoreExpr, LPat GhcTc)
dsTransStmt :: ExprStmt GhcTc -> DsM (CoreExpr, LPat GhcTc)
dsTransStmt (TransStmt { trS_form :: forall idL idR body. StmtLR idL idR body -> TransForm
trS_form = TransForm
form, trS_stmts :: forall idL idR body. StmtLR idL idR body -> [ExprLStmt idL]
trS_stmts = [ExprLStmt GhcTc]
stmts, trS_bndrs :: forall idL idR body. StmtLR idL idR body -> [(IdP idR, IdP idR)]
trS_bndrs = [(IdP GhcTc, IdP GhcTc)]
binderMap
, trS_by :: forall idL idR body. StmtLR idL idR body -> Maybe (LHsExpr idR)
trS_by = Maybe (LHsExpr GhcTc)
by, trS_using :: forall idL idR body. StmtLR idL idR body -> LHsExpr idR
trS_using = LHsExpr GhcTc
using }) = do
let ([Id]
from_bndrs, [Id]
to_bndrs) = [(Id, Id)] -> ([Id], [Id])
forall a b. [(a, b)] -> ([a], [b])
unzip [(IdP GhcTc, IdP GhcTc)]
[(Id, Id)]
binderMap
let from_bndrs_tys :: [Type]
from_bndrs_tys = (Id -> Type) -> [Id] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Id -> Type
idType [Id]
from_bndrs
to_bndrs_tys :: [Type]
to_bndrs_tys = (Id -> Type) -> [Id] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Id -> Type
idType [Id]
to_bndrs
to_bndrs_tup_ty :: Type
to_bndrs_tup_ty = [Type] -> Type
HasDebugCallStack => [Type] -> Type
mkBigCoreTupTy [Type]
to_bndrs_tys
(expr', from_tup_ty) <- ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type)
dsInnerListComp (XParStmtBlock GhcTc GhcTc
-> [ExprLStmt GhcTc]
-> [IdP GhcTc]
-> SyntaxExpr GhcTc
-> ParStmtBlock GhcTc GhcTc
forall idL idR.
XParStmtBlock idL idR
-> [ExprLStmt idL]
-> [IdP idR]
-> SyntaxExpr idR
-> ParStmtBlock idL idR
ParStmtBlock XParStmtBlock GhcTc GhcTc
NoExtField
noExtField [ExprLStmt GhcTc]
stmts
[IdP GhcTc]
[Id]
from_bndrs SyntaxExpr GhcTc
forall (p :: Pass). IsPass p => SyntaxExpr (GhcPass p)
noSyntaxExpr)
usingExpr' <- dsLExpr using
usingArgs' <- case by of
Maybe (LHsExpr GhcTc)
Nothing -> [CoreExpr] -> IOEnv (Env DsGblEnv DsLclEnv) [CoreExpr]
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [CoreExpr
expr']
Just LHsExpr GhcTc
by_e -> do { by_e' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
by_e
; lam' <- matchTuple from_bndrs by_e'
; return [lam', expr'] }
unzip_stuff' <- mkUnzipBind form from_bndrs_tys
map_id <- dsLookupGlobalId mapName
let
inner_list_expr' = CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
usingExpr' [CoreExpr]
usingArgs'
bound_unzipped_inner_list_expr'
= case Maybe (Id, CoreExpr)
unzip_stuff' of
Maybe (Id, CoreExpr)
Nothing -> CoreExpr
inner_list_expr'
Just (Id
unzip_fn', CoreExpr
unzip_rhs') ->
Bind Id -> CoreExpr -> CoreExpr
forall b. Bind b -> Expr b -> Expr b
Let ([(Id, CoreExpr)] -> Bind Id
forall b. [(b, Expr b)] -> Bind b
Rec [(Id
unzip_fn', CoreExpr
unzip_rhs')]) (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall a b. (a -> b) -> a -> b
$
CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
map_id) ([CoreExpr] -> CoreExpr) -> [CoreExpr] -> CoreExpr
forall a b. (a -> b) -> a -> b
$
[ Type -> CoreExpr
forall b. Type -> Expr b
Type (Type -> Type
mkListTy Type
from_tup_ty)
, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
to_bndrs_tup_ty
, Id -> CoreExpr
forall b. Id -> Expr b
Var Id
unzip_fn'
, CoreExpr
inner_list_expr' ]
let pat = [Id] -> LPat GhcTc
mkBigLHsVarPatTupId [Id]
to_bndrs
return (bound_unzipped_inner_list_expr', pat)
dsTransStmt ExprStmt GhcTc
_ = String
-> IOEnv
(Env DsGblEnv DsLclEnv)
(CoreExpr, GenLocated SrcSpanAnnA (Pat GhcTc))
forall a. HasCallStack => String -> a
panic String
"dsTransStmt: Not given a TransStmt"
deListComp :: [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deListComp :: [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deListComp [] CoreExpr
_ = String -> DsM CoreExpr
forall a. HasCallStack => String -> a
panic String
"deListComp"
deListComp (LastStmt XLastStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
body Maybe Bool
_ SyntaxExpr GhcTc
_ : [ExprStmt GhcTc]
quals) CoreExpr
list
=
Bool -> DsM CoreExpr -> DsM CoreExpr
forall a. HasCallStack => Bool -> a -> a
assert ([Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ExprStmt GhcTc]
[Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
quals) (DsM CoreExpr -> DsM CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$
do { core_body <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
body
; return (mkConsExpr (exprType core_body) core_body list) }
deListComp (BodyStmt XBodyStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
guard SyntaxExpr GhcTc
_ SyntaxExpr GhcTc
_ : [ExprStmt GhcTc]
quals) CoreExpr
list = do
core_guard <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
guard
core_rest <- deListComp quals list
return (mkIfThenElse core_guard core_rest list)
deListComp (LetStmt XLetStmt GhcTc GhcTc (LHsExpr GhcTc)
_ HsLocalBindsLR GhcTc GhcTc
binds : [ExprStmt GhcTc]
quals) CoreExpr
list = do
core_rest <- [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deListComp [ExprStmt GhcTc]
quals CoreExpr
list
dsLocalBinds binds core_rest
deListComp (stmt :: ExprStmt GhcTc
stmt@(TransStmt {}) : [ExprStmt GhcTc]
quals) CoreExpr
list = do
(inner_list_expr, pat) <- ExprStmt GhcTc -> DsM (CoreExpr, LPat GhcTc)
dsTransStmt ExprStmt GhcTc
stmt
deBindComp pat inner_list_expr quals list
deListComp (BindStmt XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LPat GhcTc
pat LHsExpr GhcTc
list1 : [ExprStmt GhcTc]
quals) CoreExpr
core_list2 = do
core_list1 <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
list1
deBindComp pat core_list1 quals core_list2
deListComp (ParStmt XParStmt GhcTc GhcTc (LHsExpr GhcTc)
_ [ParStmtBlock GhcTc GhcTc]
stmtss_w_bndrs HsExpr GhcTc
_ SyntaxExpr GhcTc
_ : [ExprStmt GhcTc]
quals) CoreExpr
list
= do { exps_and_qual_tys <- (ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type))
-> [ParStmtBlock GhcTc GhcTc]
-> IOEnv (Env DsGblEnv DsLclEnv) [(CoreExpr, Type)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type)
dsInnerListComp [ParStmtBlock GhcTc GhcTc]
stmtss_w_bndrs
; let (exps, qual_tys) = unzip exps_and_qual_tys
; (zip_fn, zip_rhs) <- mkZipBind qual_tys
; deBindComp pat (Let (Rec [(zip_fn, zip_rhs)]) (mkApps (Var zip_fn) exps))
quals list }
where
bndrs_s :: [[Id]]
bndrs_s = [[IdP GhcTc]
[Id]
bs | ParStmtBlock XParStmtBlock GhcTc GhcTc
_ [ExprLStmt GhcTc]
_ [IdP GhcTc]
bs SyntaxExpr GhcTc
_ <- [ParStmtBlock GhcTc GhcTc]
stmtss_w_bndrs]
pat :: LPat GhcTc
pat = [LPat GhcTc] -> LPat GhcTc
mkBigLHsPatTupId [LPat GhcTc]
[GenLocated SrcSpanAnnA (Pat GhcTc)]
pats
pats :: [GenLocated SrcSpanAnnA (Pat GhcTc)]
pats = ([Id] -> GenLocated SrcSpanAnnA (Pat GhcTc))
-> [[Id]] -> [GenLocated SrcSpanAnnA (Pat GhcTc)]
forall a b. (a -> b) -> [a] -> [b]
map [Id] -> LPat GhcTc
[Id] -> GenLocated SrcSpanAnnA (Pat GhcTc)
mkBigLHsVarPatTupId [[Id]]
bndrs_s
deListComp (RecStmt {} : [ExprStmt GhcTc]
_) CoreExpr
_ = String -> DsM CoreExpr
forall a. HasCallStack => String -> a
panic String
"deListComp RecStmt"
deListComp (XStmtLR ApplicativeStmt {} : [ExprStmt GhcTc]
_) CoreExpr
_ =
String -> DsM CoreExpr
forall a. HasCallStack => String -> a
panic String
"deListComp ApplicativeStmt"
deBindComp :: LPat GhcTc
-> CoreExpr
-> [ExprStmt GhcTc]
-> CoreExpr
-> DsM (Expr Id)
deBindComp :: LPat GhcTc
-> CoreExpr -> [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deBindComp LPat GhcTc
pat CoreExpr
core_list1 [ExprStmt GhcTc]
quals CoreExpr
core_list2 = do
let u3_ty :: Type
u3_ty@Type
u1_ty = HasDebugCallStack => CoreExpr -> Type
CoreExpr -> Type
exprType CoreExpr
core_list1
let u2_ty :: Type
u2_ty = LPat GhcTc -> Type
hsLPatType LPat GhcTc
pat
let res_ty :: Type
res_ty = HasDebugCallStack => CoreExpr -> Type
CoreExpr -> Type
exprType CoreExpr
core_list2
h_ty :: Type
h_ty = Type
u1_ty HasDebugCallStack => Type -> Type -> Type
Type -> Type -> Type
`mkVisFunTyMany` Type
res_ty
[h, u1, u2, u3] <- [Scaled Type] -> DsM [Id]
newSysLocalsDs ([Scaled Type] -> DsM [Id]) -> [Scaled Type] -> DsM [Id]
forall a b. (a -> b) -> a -> b
$ (Type -> Scaled Type) -> [Type] -> [Scaled Type]
forall a b. (a -> b) -> [a] -> [b]
map Type -> Scaled Type
forall a. a -> Scaled a
unrestricted [Type
h_ty, Type
u1_ty, Type
u2_ty, Type
u3_ty]
let
core_fail = CoreExpr -> CoreExpr -> CoreExpr
forall b. Expr b -> Expr b -> Expr b
App (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
h) (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
u3)
letrec_body = CoreExpr -> CoreExpr -> CoreExpr
forall b. Expr b -> Expr b -> Expr b
App (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
h) CoreExpr
core_list1
rest_expr <- deListComp quals core_fail
core_match <- matchSimply (Var u2) (StmtCtxt (HsDoStmt ListComp)) ManyTy pat rest_expr core_fail
let
rhs = Id -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Id
u1 (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall a b. (a -> b) -> a -> b
$
CoreExpr -> Id -> Type -> [Alt Id] -> CoreExpr
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
u1) Id
u1 Type
res_ty
[AltCon -> [Id] -> CoreExpr -> Alt Id
forall b. AltCon -> [b] -> Expr b -> Alt b
Alt (DataCon -> AltCon
DataAlt DataCon
nilDataCon) [] CoreExpr
core_list2
,AltCon -> [Id] -> CoreExpr -> Alt Id
forall b. AltCon -> [b] -> Expr b -> Alt b
Alt (DataCon -> AltCon
DataAlt DataCon
consDataCon) [Id
u2, Id
u3] CoreExpr
core_match]
return (Let (Rec [(h, rhs)]) letrec_body)
dfListComp :: Id -> Id
-> [ExprStmt GhcTc]
-> DsM CoreExpr
dfListComp :: Id -> Id -> [ExprStmt GhcTc] -> DsM CoreExpr
dfListComp Id
_ Id
_ [] = String -> DsM CoreExpr
forall a. HasCallStack => String -> a
panic String
"dfListComp"
dfListComp Id
c_id Id
n_id (LastStmt XLastStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
body Maybe Bool
_ SyntaxExpr GhcTc
_ : [ExprStmt GhcTc]
quals)
= Bool -> DsM CoreExpr -> DsM CoreExpr
forall a. HasCallStack => Bool -> a -> a
assert ([Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ExprStmt GhcTc]
[Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
quals) (DsM CoreExpr -> DsM CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$
do { core_body <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
body
; return (mkApps (Var c_id) [core_body, Var n_id]) }
dfListComp Id
c_id Id
n_id (BodyStmt XBodyStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
guard SyntaxExpr GhcTc
_ SyntaxExpr GhcTc
_ : [ExprStmt GhcTc]
quals) = do
core_guard <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
guard
core_rest <- dfListComp c_id n_id quals
return (mkIfThenElse core_guard core_rest (Var n_id))
dfListComp Id
c_id Id
n_id (LetStmt XLetStmt GhcTc GhcTc (LHsExpr GhcTc)
_ HsLocalBindsLR GhcTc GhcTc
binds : [ExprStmt GhcTc]
quals) = do
core_rest <- Id -> Id -> [ExprStmt GhcTc] -> DsM CoreExpr
dfListComp Id
c_id Id
n_id [ExprStmt GhcTc]
quals
dsLocalBinds binds core_rest
dfListComp Id
c_id Id
n_id (stmt :: ExprStmt GhcTc
stmt@(TransStmt {}) : [ExprStmt GhcTc]
quals) = do
(inner_list_expr, pat) <- ExprStmt GhcTc -> DsM (CoreExpr, LPat GhcTc)
dsTransStmt ExprStmt GhcTc
stmt
dfBindComp c_id n_id (pat, inner_list_expr) quals
dfListComp Id
c_id Id
n_id (BindStmt XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LPat GhcTc
pat LHsExpr GhcTc
list1 : [ExprStmt GhcTc]
quals) = do
core_list1 <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
list1
dfBindComp c_id n_id (pat, core_list1) quals
dfListComp Id
_ Id
_ (ParStmt {} : [ExprStmt GhcTc]
_) = String -> DsM CoreExpr
forall a. HasCallStack => String -> a
panic String
"dfListComp ParStmt"
dfListComp Id
_ Id
_ (RecStmt {} : [ExprStmt GhcTc]
_) = String -> DsM CoreExpr
forall a. HasCallStack => String -> a
panic String
"dfListComp RecStmt"
dfListComp Id
_ Id
_ (XStmtLR ApplicativeStmt {} : [ExprStmt GhcTc]
_) =
String -> DsM CoreExpr
forall a. HasCallStack => String -> a
panic String
"dfListComp ApplicativeStmt"
dfBindComp :: Id -> Id
-> (LPat GhcTc, CoreExpr)
-> [ExprStmt GhcTc]
-> DsM CoreExpr
dfBindComp :: Id
-> Id -> (LPat GhcTc, CoreExpr) -> [ExprStmt GhcTc] -> DsM CoreExpr
dfBindComp Id
c_id Id
n_id (LPat GhcTc
pat, CoreExpr
core_list1) [ExprStmt GhcTc]
quals = do
let x_ty :: Type
x_ty = LPat GhcTc -> Type
hsLPatType LPat GhcTc
pat
let b_ty :: Type
b_ty = Id -> Type
idType Id
n_id
b <- Type -> DsM Id
newSysLocalMDs Type
b_ty
x <- newSysLocalMDs x_ty
core_rest <- dfListComp c_id b quals
core_expr <- matchSimply (Var x) (StmtCtxt (HsDoStmt ListComp)) ManyTy
pat core_rest (Var b)
mkFoldrExpr x_ty b_ty (mkLams [x, b] core_expr) (Var n_id) core_list1
mkZipBind :: [Type] -> DsM (Id, CoreExpr)
mkZipBind :: [Type] -> DsM (Id, CoreExpr)
mkZipBind [Type]
elt_tys = do
ass <- [Type] -> DsM [Id]
newSysLocalsMDs [Type]
elt_list_tys
as' <- newSysLocalsMDs elt_tys
as's <- newSysLocalsMDs elt_list_tys
zip_fn <- newSysLocalMDs zip_fn_ty
let inner_rhs = Type -> CoreExpr -> CoreExpr -> CoreExpr
mkConsExpr Type
elt_tuple_ty
([Id] -> CoreExpr
mkBigCoreVarTup [Id]
as')
(CoreExpr -> [Id] -> CoreExpr
forall b. Expr b -> [Id] -> Expr b
mkVarApps (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
zip_fn) [Id]
as's)
zip_body = ((Id, Id, Id) -> CoreExpr -> CoreExpr)
-> CoreExpr -> [(Id, Id, Id)] -> CoreExpr
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Id, Id, Id) -> CoreExpr -> CoreExpr
mk_case CoreExpr
inner_rhs ([Id] -> [Id] -> [Id] -> [(Id, Id, Id)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Id]
ass [Id]
as' [Id]
as's)
return (zip_fn, mkLams ass zip_body)
where
elt_list_tys :: [Type]
elt_list_tys = (Type -> Type) -> [Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Type -> Type
mkListTy [Type]
elt_tys
elt_tuple_ty :: Type
elt_tuple_ty = [Type] -> Type
HasDebugCallStack => [Type] -> Type
mkBigCoreTupTy [Type]
elt_tys
elt_tuple_list_ty :: Type
elt_tuple_list_ty = Type -> Type
mkListTy Type
elt_tuple_ty
zip_fn_ty :: Type
zip_fn_ty = [Type] -> Type -> Type
mkVisFunTysMany [Type]
elt_list_tys Type
elt_tuple_list_ty
mk_case :: (Id, Id, Id) -> CoreExpr -> CoreExpr
mk_case (Id
as, Id
a', Id
as') CoreExpr
rest
= CoreExpr -> Id -> Type -> [Alt Id] -> CoreExpr
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
as) Id
as Type
elt_tuple_list_ty
[ AltCon -> [Id] -> CoreExpr -> Alt Id
forall b. AltCon -> [b] -> Expr b -> Alt b
Alt (DataCon -> AltCon
DataAlt DataCon
nilDataCon) [] (Type -> CoreExpr
mkNilExpr Type
elt_tuple_ty)
, AltCon -> [Id] -> CoreExpr -> Alt Id
forall b. AltCon -> [b] -> Expr b -> Alt b
Alt (DataCon -> AltCon
DataAlt DataCon
consDataCon) [Id
a', Id
as'] CoreExpr
rest]
mkUnzipBind :: TransForm -> [Type] -> DsM (Maybe (Id, CoreExpr))
mkUnzipBind :: TransForm -> [Type] -> DsM (Maybe (Id, CoreExpr))
mkUnzipBind TransForm
ThenForm [Type]
_
= Maybe (Id, CoreExpr) -> DsM (Maybe (Id, CoreExpr))
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Id, CoreExpr)
forall a. Maybe a
Nothing
mkUnzipBind TransForm
_ [Type]
elt_tys
= do { ax <- Type -> DsM Id
newSysLocalMDs Type
elt_tuple_ty
; axs <- newSysLocalMDs elt_list_tuple_ty
; ys <- newSysLocalMDs elt_tuple_list_ty
; xs <- newSysLocalsMDs elt_tys
; xss <- newSysLocalsMDs elt_list_tys
; unzip_fn <- newSysLocalMDs unzip_fn_ty
; let nil_tuple = [CoreExpr] -> CoreExpr
mkBigCoreTup ((Type -> CoreExpr) -> [Type] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map Type -> CoreExpr
mkNilExpr [Type]
elt_tys)
concat_expressions = ((Type, CoreExpr, CoreExpr) -> CoreExpr)
-> [(Type, CoreExpr, CoreExpr)] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map (Type, CoreExpr, CoreExpr) -> CoreExpr
mkConcatExpression ([Type] -> [CoreExpr] -> [CoreExpr] -> [(Type, CoreExpr, CoreExpr)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Type]
elt_tys ((Id -> CoreExpr) -> [Id] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map Id -> CoreExpr
forall b. Id -> Expr b
Var [Id]
xs) ((Id -> CoreExpr) -> [Id] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map Id -> CoreExpr
forall b. Id -> Expr b
Var [Id]
xss))
tupled_concat_expression = [CoreExpr] -> CoreExpr
mkBigCoreTup [CoreExpr]
concat_expressions
; folder_body_inner_case <- mkBigTupleCase xss tupled_concat_expression (Var axs)
; folder_body_outer_case <- mkBigTupleCase xs folder_body_inner_case (Var ax)
; let folder_body = [Id] -> CoreExpr -> CoreExpr
forall b. [b] -> Expr b -> Expr b
mkLams [Id
ax, Id
axs] CoreExpr
folder_body_outer_case
; unzip_body <- mkFoldrExpr elt_tuple_ty elt_list_tuple_ty folder_body nil_tuple (Var ys)
; return (Just (unzip_fn, mkLams [ys] unzip_body)) }
where
elt_tuple_ty :: Type
elt_tuple_ty = [Type] -> Type
HasDebugCallStack => [Type] -> Type
mkBigCoreTupTy [Type]
elt_tys
elt_tuple_list_ty :: Type
elt_tuple_list_ty = Type -> Type
mkListTy Type
elt_tuple_ty
elt_list_tys :: [Type]
elt_list_tys = (Type -> Type) -> [Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Type -> Type
mkListTy [Type]
elt_tys
elt_list_tuple_ty :: Type
elt_list_tuple_ty = [Type] -> Type
HasDebugCallStack => [Type] -> Type
mkBigCoreTupTy [Type]
elt_list_tys
unzip_fn_ty :: Type
unzip_fn_ty = Type
elt_tuple_list_ty HasDebugCallStack => Type -> Type -> Type
Type -> Type -> Type
`mkVisFunTyMany` Type
elt_list_tuple_ty
mkConcatExpression :: (Type, CoreExpr, CoreExpr) -> CoreExpr
mkConcatExpression (Type
list_element_ty, CoreExpr
head, CoreExpr
tail) = Type -> CoreExpr -> CoreExpr -> CoreExpr
mkConsExpr Type
list_element_ty CoreExpr
head CoreExpr
tail
dsMonadComp :: [ExprLStmt GhcTc] -> DsM CoreExpr
dsMonadComp :: [ExprLStmt GhcTc] -> DsM CoreExpr
dsMonadComp [ExprLStmt GhcTc]
stmts = [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts [ExprLStmt GhcTc]
stmts
dsMcStmts :: [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts :: [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts [] = String -> DsM CoreExpr
forall a. HasCallStack => String -> a
panic String
"dsMcStmts"
dsMcStmts ((L SrcSpanAnnA
loc Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
stmt) : [ExprLStmt GhcTc]
lstmts) = SrcSpanAnnA -> DsM CoreExpr -> DsM CoreExpr
forall ann a. EpAnn ann -> DsM a -> DsM a
putSrcSpanDsA SrcSpanAnnA
loc (ExprStmt GhcTc -> [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmt ExprStmt GhcTc
Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
stmt [ExprLStmt GhcTc]
lstmts)
dsMcStmt :: ExprStmt GhcTc -> [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmt :: ExprStmt GhcTc -> [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmt (LastStmt XLastStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
body Maybe Bool
_ SyntaxExpr GhcTc
ret_op) [ExprLStmt GhcTc]
stmts
= Bool -> DsM CoreExpr -> DsM CoreExpr
forall a. HasCallStack => Bool -> a -> a
assert ([GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
-> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ExprLStmt GhcTc]
[GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
stmts) (DsM CoreExpr -> DsM CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$
do { body' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
body
; dsSyntaxExpr ret_op [body'] }
dsMcStmt (LetStmt XLetStmt GhcTc GhcTc (LHsExpr GhcTc)
_ HsLocalBindsLR GhcTc GhcTc
binds) [ExprLStmt GhcTc]
stmts
= do { rest <- [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts [ExprLStmt GhcTc]
stmts
; dsLocalBinds binds rest }
dsMcStmt (BindStmt XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
xbs LPat GhcTc
pat LHsExpr GhcTc
rhs) [ExprLStmt GhcTc]
stmts
= do { rhs' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
rhs
; dsMcBindStmt pat rhs' (xbstc_bindOp xbs) (xbstc_failOp xbs) (xbstc_boundResultType xbs) stmts }
dsMcStmt (BodyStmt XBodyStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
exp SyntaxExpr GhcTc
then_exp SyntaxExpr GhcTc
guard_exp) [ExprLStmt GhcTc]
stmts
= do { exp' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
exp
; rest <- dsMcStmts stmts
; guard_exp' <- dsSyntaxExpr guard_exp [exp']
; dsSyntaxExpr then_exp [guard_exp', rest] }
dsMcStmt (TransStmt { trS_stmts :: forall idL idR body. StmtLR idL idR body -> [ExprLStmt idL]
trS_stmts = [ExprLStmt GhcTc]
stmts, trS_bndrs :: forall idL idR body. StmtLR idL idR body -> [(IdP idR, IdP idR)]
trS_bndrs = [(IdP GhcTc, IdP GhcTc)]
bndrs
, trS_by :: forall idL idR body. StmtLR idL idR body -> Maybe (LHsExpr idR)
trS_by = Maybe (LHsExpr GhcTc)
by, trS_using :: forall idL idR body. StmtLR idL idR body -> LHsExpr idR
trS_using = LHsExpr GhcTc
using
, trS_ret :: forall idL idR body. StmtLR idL idR body -> SyntaxExpr idR
trS_ret = SyntaxExpr GhcTc
return_op, trS_bind :: forall idL idR body. StmtLR idL idR body -> SyntaxExpr idR
trS_bind = SyntaxExpr GhcTc
bind_op
, trS_ext :: forall idL idR body. StmtLR idL idR body -> XTransStmt idL idR body
trS_ext = XTransStmt GhcTc GhcTc (LHsExpr GhcTc)
n_tup_ty'
, trS_fmap :: forall idL idR body. StmtLR idL idR body -> HsExpr idR
trS_fmap = HsExpr GhcTc
fmap_op, trS_form :: forall idL idR body. StmtLR idL idR body -> TransForm
trS_form = TransForm
form }) [ExprLStmt GhcTc]
stmts_rest
= do { let ([Id]
from_bndrs, [Id]
to_bndrs) = [(Id, Id)] -> ([Id], [Id])
forall a b. [(a, b)] -> ([a], [b])
unzip [(IdP GhcTc, IdP GhcTc)]
[(Id, Id)]
bndrs
; let from_bndr_tys :: [Type]
from_bndr_tys = (Id -> Type) -> [Id] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Id -> Type
idType [Id]
from_bndrs
; expr' <- [ExprLStmt GhcTc] -> [Id] -> SyntaxExpr GhcTc -> DsM CoreExpr
dsInnerMonadComp [ExprLStmt GhcTc]
stmts [Id]
from_bndrs SyntaxExpr GhcTc
return_op
; usingExpr' <- dsLExpr using
; usingArgs' <- case by of
Maybe (LHsExpr GhcTc)
Nothing -> [CoreExpr] -> IOEnv (Env DsGblEnv DsLclEnv) [CoreExpr]
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [CoreExpr
expr']
Just LHsExpr GhcTc
by_e -> do { by_e' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
by_e
; lam' <- matchTuple from_bndrs by_e'
; return [lam', expr'] }
; body <- dsMcStmts stmts_rest
; n_tup_var' <- newSysLocalMDs n_tup_ty'
; tup_n_expr' <- mkMcUnzipM form fmap_op n_tup_var' from_bndr_tys
; let rhs' = CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
usingExpr' [CoreExpr]
usingArgs'
; body' <- mkBigTupleCase to_bndrs body tup_n_expr'
; dsSyntaxExpr bind_op [rhs', Lam n_tup_var' body'] }
dsMcStmt (ParStmt XParStmt GhcTc GhcTc (LHsExpr GhcTc)
bind_ty [ParStmtBlock GhcTc GhcTc]
blocks HsExpr GhcTc
mzip_op SyntaxExpr GhcTc
bind_op) [ExprLStmt GhcTc]
stmts_rest
= do { exps_w_tys <- (ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type))
-> [ParStmtBlock GhcTc GhcTc]
-> IOEnv (Env DsGblEnv DsLclEnv) [(CoreExpr, Type)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type)
ds_inner [ParStmtBlock GhcTc GhcTc]
blocks
; mzip_op' <- dsExpr mzip_op
; let
pats = [ [Id] -> LPat GhcTc
mkBigLHsVarPatTupId [IdP GhcTc]
[Id]
bs | ParStmtBlock XParStmtBlock GhcTc GhcTc
_ [ExprLStmt GhcTc]
_ [IdP GhcTc]
bs SyntaxExpr GhcTc
_ <- [ParStmtBlock GhcTc GhcTc]
blocks]
pat = (GenLocated SrcSpanAnnA (Pat GhcTc)
-> GenLocated SrcSpanAnnA (Pat GhcTc)
-> GenLocated SrcSpanAnnA (Pat GhcTc))
-> [GenLocated SrcSpanAnnA (Pat GhcTc)]
-> GenLocated SrcSpanAnnA (Pat GhcTc)
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 (\GenLocated SrcSpanAnnA (Pat GhcTc)
p1 GenLocated SrcSpanAnnA (Pat GhcTc)
p2 -> [LPat GhcTc] -> LPat GhcTc
mkLHsPatTup [LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
p1, LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
p2]) [GenLocated SrcSpanAnnA (Pat GhcTc)]
pats
(rhs, _) = foldr1 (\(CoreExpr
e1,Type
t1) (CoreExpr
e2,Type
t2) ->
(CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
mzip_op' [Type -> CoreExpr
forall b. Type -> Expr b
Type Type
t1, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
t2, CoreExpr
e1, CoreExpr
e2],
[Type] -> Type
mkBoxedTupleTy [Type
t1,Type
t2]))
exps_w_tys
; dsMcBindStmt pat rhs bind_op Nothing bind_ty stmts_rest }
where
ds_inner :: ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type)
ds_inner :: ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type)
ds_inner (ParStmtBlock XParStmtBlock GhcTc GhcTc
_ [ExprLStmt GhcTc]
stmts [IdP GhcTc]
bndrs SyntaxExpr GhcTc
return_op)
= do { exp <- [ExprLStmt GhcTc] -> [Id] -> SyntaxExpr GhcTc -> DsM CoreExpr
dsInnerMonadComp [ExprLStmt GhcTc]
stmts [IdP GhcTc]
[Id]
bndrs SyntaxExpr GhcTc
return_op
; return (exp, mkBigCoreVarTupTy bndrs) }
dsMcStmt stmt :: ExprStmt GhcTc
stmt@(XStmtLR ApplicativeStmt {}) [ExprLStmt GhcTc]
_ = String -> SDoc -> DsM CoreExpr
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"dsMcStmt: unexpected stmt" (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr ExprStmt GhcTc
Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
stmt)
dsMcStmt stmt :: ExprStmt GhcTc
stmt@(RecStmt {}) [ExprLStmt GhcTc]
_ = String -> SDoc -> DsM CoreExpr
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"dsMcStmt: unexpected stmt" (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr ExprStmt GhcTc
Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
stmt)
matchTuple :: [Id] -> CoreExpr -> DsM CoreExpr
matchTuple :: [Id] -> CoreExpr -> DsM CoreExpr
matchTuple [Id]
ids CoreExpr
body
= do { tup_id <- Type -> DsM Id
newSysLocalMDs ([Id] -> Type
HasDebugCallStack => [Id] -> Type
mkBigCoreVarTupTy [Id]
ids)
; tup_case <- mkBigTupleCase ids body (Var tup_id)
; return (Lam tup_id tup_case) }
dsMcBindStmt :: LPat GhcTc
-> CoreExpr
-> SyntaxExpr GhcTc
-> Maybe (SyntaxExpr GhcTc)
-> Type
-> [ExprLStmt GhcTc]
-> DsM CoreExpr
dsMcBindStmt :: LPat GhcTc
-> CoreExpr
-> SyntaxExpr GhcTc
-> Maybe (SyntaxExpr GhcTc)
-> Type
-> [ExprLStmt GhcTc]
-> DsM CoreExpr
dsMcBindStmt LPat GhcTc
pat CoreExpr
rhs' SyntaxExpr GhcTc
bind_op Maybe (SyntaxExpr GhcTc)
fail_op Type
res1_ty [ExprLStmt GhcTc]
stmts
= do { var <- Type -> LPat GhcTc -> DsM Id
selectSimpleMatchVarL Type
ManyTy LPat GhcTc
pat
; match <- matchSinglePatVar var Nothing (StmtCtxt (HsDoStmt (DoExpr Nothing))) pat
res1_ty (MR_Infallible $ dsMcStmts stmts)
; match_code <- dsHandleMonadicFailure MonadComp pat res1_ty match fail_op
; dsSyntaxExpr bind_op [rhs', Lam var match_code] }
dsInnerMonadComp :: [ExprLStmt GhcTc]
-> [Id]
-> SyntaxExpr GhcTc
-> DsM CoreExpr
dsInnerMonadComp :: [ExprLStmt GhcTc] -> [Id] -> SyntaxExpr GhcTc -> DsM CoreExpr
dsInnerMonadComp [ExprLStmt GhcTc]
stmts [Id]
bndrs SyntaxExpr GhcTc
ret_op
= [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts ([ExprLStmt GhcTc]
[GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
stmts [GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
-> [GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
-> [GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
forall a. [a] -> [a] -> [a]
++
[Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (XLastStmt GhcTc GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> GenLocated SrcSpanAnnA (HsExpr GhcTc)
-> Maybe Bool
-> SyntaxExpr GhcTc
-> Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall idL idR body.
XLastStmt idL idR body
-> body -> Maybe Bool -> SyntaxExpr idR -> StmtLR idL idR body
LastStmt XLastStmt GhcTc GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
NoExtField
noExtField ([Id] -> LHsExpr GhcTc
mkBigLHsVarTupId [Id]
bndrs) Maybe Bool
forall a. Maybe a
Nothing SyntaxExpr GhcTc
ret_op)])
mkMcUnzipM :: TransForm
-> HsExpr GhcTc
-> Id
-> [Type]
-> DsM CoreExpr
mkMcUnzipM :: TransForm -> HsExpr GhcTc -> Id -> [Type] -> DsM CoreExpr
mkMcUnzipM TransForm
ThenForm HsExpr GhcTc
_ Id
ys [Type]
_
= CoreExpr -> DsM CoreExpr
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
ys)
mkMcUnzipM TransForm
_ HsExpr GhcTc
fmap_op Id
ys [Type]
elt_tys
= do { fmap_op' <- HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
fmap_op
; xs <- newSysLocalsMDs elt_tys
; let tup_ty = [Type] -> Type
HasDebugCallStack => [Type] -> Type
mkBigCoreTupTy [Type]
elt_tys
; tup_xs <- newSysLocalMDs tup_ty
; let mk_elt Int
i = CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
fmap_op'
[ Type -> CoreExpr
forall b. Type -> Expr b
Type Type
tup_ty, Type -> CoreExpr
forall b. Type -> Expr b
Type ([Type] -> Int -> Type
forall a. Outputable a => [a] -> Int -> a
getNth [Type]
elt_tys Int
i)
, Int -> CoreExpr
mk_sel Int
i, Id -> CoreExpr
forall b. Id -> Expr b
Var Id
ys]
mk_sel Int
n = Id -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Id
tup_xs (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall a b. (a -> b) -> a -> b
$
[Id] -> Id -> Id -> CoreExpr -> CoreExpr
mkBigTupleSelector [Id]
xs ([Id] -> Int -> Id
forall a. Outputable a => [a] -> Int -> a
getNth [Id]
xs Int
n) Id
tup_xs (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
tup_xs)
; return (mkBigCoreTup (map mk_elt [0..length elt_tys - 1])) }