{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
module GHC.HsToCore.ListComp ( dsListComp, dsMonadComp ) where
#include "HsVersions.h"
import GHC.Prelude
import {-# SOURCE #-} GHC.HsToCore.Expr ( dsExpr, dsLExpr, dsLExprNoLP, dsLocalBinds, dsSyntaxExpr )
import GHC.Hs
import GHC.Tc.Utils.Zonk
import GHC.Core
import GHC.Core.Make
import GHC.HsToCore.Monad
import GHC.HsToCore.Utils
import GHC.Driver.Session
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 )
import GHC.Utils.Misc
dsListComp :: [ExprLStmt GhcTc]
-> Type
-> DsM CoreExpr
dsListComp :: [ExprLStmt GhcTc] -> Type -> DsM CoreExpr
dsListComp [ExprLStmt GhcTc]
lquals Type
res_ty = do
DynFlags
dflags <- forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let quals :: [Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
quals = forall a b. (a -> b) -> [a] -> [b]
map forall l e. GenLocated l e -> e
unLoc [ExprLStmt GhcTc]
lquals
elt_ty :: Type
elt_ty = case Type -> [Type]
tcTyConAppArgs Type
res_ty of
[Type
elt_ty] -> Type
elt_ty
[Type]
_ -> forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"dsListComp" (forall a. Outputable a => a -> SDoc
ppr Type
res_ty SDoc -> SDoc -> SDoc
$$ forall a. Outputable a => a -> SDoc
ppr [ExprLStmt GhcTc]
lquals)
if Bool -> Bool
not (GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_EnableRewriteRules DynFlags
dflags) Bool -> Bool -> Bool
|| GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_IgnoreInterfacePragmas DynFlags
dflags
Bool -> Bool -> Bool
|| forall {idL} {idR} {body}. [StmtLR idL idR body] -> Bool
isParallelComp [Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
quals
then [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deListComp [Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
quals (Type -> CoreExpr
mkNilExpr Type
elt_ty)
else forall (m :: * -> *).
(MonadFail m, MonadThings m, MonadUnique m) =>
Type -> ((Id, Type) -> (Id, Type) -> m CoreExpr) -> m CoreExpr
mkBuildExpr Type
elt_ty (\(Id
c, Type
_) (Id
n, Type
_) -> Id -> Id -> [ExprStmt GhcTc] -> DsM CoreExpr
dfListComp Id
c Id
n [Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
quals)
where
isParallelComp :: [StmtLR idL idR body] -> Bool
isParallelComp = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any 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
mkBigCoreVarTupTy [IdP GhcTc]
bndrs
list_ty :: Type
list_ty = Type -> Type
mkListTy Type
bndrs_tuple_type
; CoreExpr
expr <- [ExprLStmt GhcTc] -> Type -> DsM CoreExpr
dsListComp ([ExprLStmt GhcTc]
stmts forall a. [a] -> [a] -> [a]
++ [forall a an. a -> LocatedAn an a
noLocA forall a b. (a -> b) -> a -> b
$ 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]
bndrs)]) Type
list_ty
; forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr
expr, Type
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) = forall a b. [(a, b)] -> ([a], [b])
unzip [(IdP GhcTc, IdP GhcTc)]
binderMap
let from_bndrs_tys :: [Type]
from_bndrs_tys = forall a b. (a -> b) -> [a] -> [b]
map Id -> Type
idType [Id]
from_bndrs
to_bndrs_tys :: [Type]
to_bndrs_tys = 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
mkBigCoreTupTy [Type]
to_bndrs_tys
(CoreExpr
expr', Type
from_tup_ty) <- ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type)
dsInnerListComp (forall idL idR.
XParStmtBlock idL idR
-> [ExprLStmt idL]
-> [IdP idR]
-> SyntaxExpr idR
-> ParStmtBlock idL idR
ParStmtBlock NoExtField
noExtField [ExprLStmt GhcTc]
stmts
[Id]
from_bndrs forall (p :: Pass). IsPass p => SyntaxExpr (GhcPass p)
noSyntaxExpr)
CoreExpr
usingExpr' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
using
[CoreExpr]
usingArgs' <- case Maybe (LHsExpr GhcTc)
by of
Maybe (LHsExpr GhcTc)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return [CoreExpr
expr']
Just LHsExpr GhcTc
by_e -> do { CoreExpr
by_e' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
by_e
; CoreExpr
lam' <- [Id] -> CoreExpr -> DsM CoreExpr
matchTuple [Id]
from_bndrs CoreExpr
by_e'
; forall (m :: * -> *) a. Monad m => a -> m a
return [CoreExpr
lam', CoreExpr
expr'] }
Maybe (Id, CoreExpr)
unzip_stuff' <- TransForm -> [Type] -> DsM (Maybe (Id, CoreExpr))
mkUnzipBind TransForm
form [Type]
from_bndrs_tys
Id
map_id <- Name -> DsM Id
dsLookupGlobalId Name
mapName
let
inner_list_expr' :: CoreExpr
inner_list_expr' = forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
usingExpr' [CoreExpr]
usingArgs'
bound_unzipped_inner_list_expr' :: CoreExpr
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') ->
forall b. Bind b -> Expr b -> Expr b
Let (forall b. [(b, Expr b)] -> Bind b
Rec [(Id
unzip_fn', CoreExpr
unzip_rhs')]) forall a b. (a -> b) -> a -> b
$
forall b. Expr b -> [Expr b] -> Expr b
mkApps (forall b. Id -> Expr b
Var Id
map_id) forall a b. (a -> b) -> a -> b
$
[ forall b. Type -> Expr b
Type (Type -> Type
mkListTy Type
from_tup_ty)
, forall b. Type -> Expr b
Type Type
to_bndrs_tup_ty
, forall b. Id -> Expr b
Var Id
unzip_fn'
, CoreExpr
inner_list_expr' ]
Type -> SDoc -> DsM ()
dsNoLevPoly (HasDebugCallStack => Int -> Type -> Type
tcFunResultTyN (forall (t :: * -> *) a. Foldable t => t a -> Int
length [CoreExpr]
usingArgs') (CoreExpr -> Type
exprType CoreExpr
usingExpr'))
(String -> SDoc
text String
"In the result of a" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (String -> SDoc
text String
"using") SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"function:" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcTc
using)
let pat :: LPat GhcTc
pat = [Id] -> LPat GhcTc
mkBigLHsVarPatTupId [Id]
to_bndrs
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr
bound_unzipped_inner_list_expr', LPat GhcTc
pat)
dsTransStmt ExprStmt GhcTc
_ = forall a. String -> a
panic String
"dsTransStmt: Not given a TransStmt"
deListComp :: [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deListComp :: [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deListComp [] CoreExpr
_ = forall a. String -> a
panic String
"deListComp"
deListComp (LastStmt XLastStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
body Maybe Bool
_ SyntaxExpr GhcTc
_ : [ExprStmt GhcTc]
quals) CoreExpr
list
=
ASSERT( null quals )
do { CoreExpr
core_body <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
body
; forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> CoreExpr -> CoreExpr -> CoreExpr
mkConsExpr (CoreExpr -> Type
exprType CoreExpr
core_body) CoreExpr
core_body CoreExpr
list) }
deListComp (BodyStmt XBodyStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
guard SyntaxExpr GhcTc
_ SyntaxExpr GhcTc
_ : [ExprStmt GhcTc]
quals) CoreExpr
list = do
CoreExpr
core_guard <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
guard
CoreExpr
core_rest <- [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deListComp [ExprStmt GhcTc]
quals CoreExpr
list
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
mkIfThenElse CoreExpr
core_guard CoreExpr
core_rest CoreExpr
list)
deListComp (LetStmt XLetStmt GhcTc GhcTc (LHsExpr GhcTc)
_ HsLocalBindsLR GhcTc GhcTc
binds : [ExprStmt GhcTc]
quals) CoreExpr
list = do
CoreExpr
core_rest <- [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deListComp [ExprStmt GhcTc]
quals CoreExpr
list
HsLocalBindsLR GhcTc GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds HsLocalBindsLR GhcTc GhcTc
binds CoreExpr
core_rest
deListComp (stmt :: ExprStmt GhcTc
stmt@(TransStmt {}) : [ExprStmt GhcTc]
quals) CoreExpr
list = do
(CoreExpr
inner_list_expr, GenLocated SrcSpanAnnA (Pat GhcTc)
pat) <- ExprStmt GhcTc -> DsM (CoreExpr, LPat GhcTc)
dsTransStmt ExprStmt GhcTc
stmt
LPat GhcTc
-> CoreExpr -> [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deBindComp GenLocated SrcSpanAnnA (Pat GhcTc)
pat CoreExpr
inner_list_expr [ExprStmt GhcTc]
quals CoreExpr
list
deListComp (BindStmt XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LPat GhcTc
pat LHsExpr GhcTc
list1 : [ExprStmt GhcTc]
quals) CoreExpr
core_list2 = do
CoreExpr
core_list1 <- LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
list1
LPat GhcTc
-> CoreExpr -> [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deBindComp LPat GhcTc
pat CoreExpr
core_list1 [ExprStmt GhcTc]
quals CoreExpr
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 { [(CoreExpr, Type)]
exps_and_qual_tys <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type)
dsInnerListComp [ParStmtBlock GhcTc GhcTc]
stmtss_w_bndrs
; let ([CoreExpr]
exps, [Type]
qual_tys) = forall a b. [(a, b)] -> ([a], [b])
unzip [(CoreExpr, Type)]
exps_and_qual_tys
; (Id
zip_fn, CoreExpr
zip_rhs) <- [Type] -> DsM (Id, CoreExpr)
mkZipBind [Type]
qual_tys
; LPat GhcTc
-> CoreExpr -> [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deBindComp LPat GhcTc
pat (forall b. Bind b -> Expr b -> Expr b
Let (forall b. [(b, Expr b)] -> Bind b
Rec [(Id
zip_fn, CoreExpr
zip_rhs)]) (forall b. Expr b -> [Expr b] -> Expr b
mkApps (forall b. Id -> Expr b
Var Id
zip_fn) [CoreExpr]
exps))
[ExprStmt GhcTc]
quals CoreExpr
list }
where
bndrs_s :: [[Id]]
bndrs_s = [[IdP GhcTc]
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 [GenLocated SrcSpanAnnA (Pat GhcTc)]
pats
pats :: [GenLocated SrcSpanAnnA (Pat GhcTc)]
pats = forall a b. (a -> b) -> [a] -> [b]
map [Id] -> LPat GhcTc
mkBigLHsVarPatTupId [[Id]]
bndrs_s
deListComp (RecStmt {} : [ExprStmt GhcTc]
_) CoreExpr
_ = forall a. String -> a
panic String
"deListComp RecStmt"
deListComp (ApplicativeStmt {} : [ExprStmt GhcTc]
_) CoreExpr
_ =
forall a. 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 = 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 = CoreExpr -> Type
exprType CoreExpr
core_list2
h_ty :: Type
h_ty = Type
u1_ty Type -> Type -> Type
`mkVisFunTyMany` Type
res_ty
[Id
h, Id
u1, Id
u2, Id
u3] <- [Scaled Type] -> DsM [Id]
newSysLocalsDs forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Scaled a
unrestricted [Type
h_ty, Type
u1_ty, Type
u2_ty, Type
u3_ty]
let
core_fail :: CoreExpr
core_fail = forall b. Expr b -> Expr b -> Expr b
App (forall b. Id -> Expr b
Var Id
h) (forall b. Id -> Expr b
Var Id
u3)
letrec_body :: CoreExpr
letrec_body = forall b. Expr b -> Expr b -> Expr b
App (forall b. Id -> Expr b
Var Id
h) CoreExpr
core_list1
CoreExpr
rest_expr <- [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deListComp [ExprStmt GhcTc]
quals CoreExpr
core_fail
CoreExpr
core_match <- CoreExpr
-> HsMatchContext GhcRn
-> LPat GhcTc
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimply (forall b. Id -> Expr b
Var Id
u2) (forall p. HsStmtContext p -> HsMatchContext p
StmtCtxt forall p. HsStmtContext p
ListComp) LPat GhcTc
pat CoreExpr
rest_expr CoreExpr
core_fail
let
rhs :: CoreExpr
rhs = forall b. b -> Expr b -> Expr b
Lam Id
u1 forall a b. (a -> b) -> a -> b
$
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case (forall b. Id -> Expr b
Var Id
u1) Id
u1 Type
res_ty
[forall b. AltCon -> [b] -> Expr b -> Alt b
Alt (DataCon -> AltCon
DataAlt DataCon
nilDataCon) [] CoreExpr
core_list2
,forall b. AltCon -> [b] -> Expr b -> Alt b
Alt (DataCon -> AltCon
DataAlt DataCon
consDataCon) [Id
u2, Id
u3] CoreExpr
core_match]
forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. Bind b -> Expr b -> Expr b
Let (forall b. [(b, Expr b)] -> Bind b
Rec [(Id
h, CoreExpr
rhs)]) CoreExpr
letrec_body)
dfListComp :: Id -> Id
-> [ExprStmt GhcTc]
-> DsM CoreExpr
dfListComp :: Id -> Id -> [ExprStmt GhcTc] -> DsM CoreExpr
dfListComp Id
_ Id
_ [] = forall a. 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)
= ASSERT( null quals )
do { CoreExpr
core_body <- LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
body
; forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. Expr b -> [Expr b] -> Expr b
mkApps (forall b. Id -> Expr b
Var Id
c_id) [CoreExpr
core_body, forall b. Id -> Expr b
Var Id
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
CoreExpr
core_guard <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
guard
CoreExpr
core_rest <- Id -> Id -> [ExprStmt GhcTc] -> DsM CoreExpr
dfListComp Id
c_id Id
n_id [ExprStmt GhcTc]
quals
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
mkIfThenElse CoreExpr
core_guard CoreExpr
core_rest (forall b. Id -> Expr b
Var Id
n_id))
dfListComp Id
c_id Id
n_id (LetStmt XLetStmt GhcTc GhcTc (LHsExpr GhcTc)
_ HsLocalBindsLR GhcTc GhcTc
binds : [ExprStmt GhcTc]
quals) = do
CoreExpr
core_rest <- Id -> Id -> [ExprStmt GhcTc] -> DsM CoreExpr
dfListComp Id
c_id Id
n_id [ExprStmt GhcTc]
quals
HsLocalBindsLR GhcTc GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds HsLocalBindsLR GhcTc GhcTc
binds CoreExpr
core_rest
dfListComp Id
c_id Id
n_id (stmt :: ExprStmt GhcTc
stmt@(TransStmt {}) : [ExprStmt GhcTc]
quals) = do
(CoreExpr
inner_list_expr, GenLocated SrcSpanAnnA (Pat GhcTc)
pat) <- ExprStmt GhcTc -> DsM (CoreExpr, LPat GhcTc)
dsTransStmt ExprStmt GhcTc
stmt
Id
-> Id -> (LPat GhcTc, CoreExpr) -> [ExprStmt GhcTc] -> DsM CoreExpr
dfBindComp Id
c_id Id
n_id (GenLocated SrcSpanAnnA (Pat GhcTc)
pat, CoreExpr
inner_list_expr) [ExprStmt GhcTc]
quals
dfListComp Id
c_id Id
n_id (BindStmt XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LPat GhcTc
pat LHsExpr GhcTc
list1 : [ExprStmt GhcTc]
quals) = do
CoreExpr
core_list1 <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
list1
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
dfListComp Id
_ Id
_ (ParStmt {} : [ExprStmt GhcTc]
_) = forall a. String -> a
panic String
"dfListComp ParStmt"
dfListComp Id
_ Id
_ (RecStmt {} : [ExprStmt GhcTc]
_) = forall a. String -> a
panic String
"dfListComp RecStmt"
dfListComp Id
_ Id
_ (ApplicativeStmt {} : [ExprStmt GhcTc]
_) =
forall a. 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
Id
b <- Type -> Type -> DsM Id
newSysLocalDs Type
Many Type
b_ty
Id
x <- Type -> Type -> DsM Id
newSysLocalDs Type
Many Type
x_ty
CoreExpr
core_rest <- Id -> Id -> [ExprStmt GhcTc] -> DsM CoreExpr
dfListComp Id
c_id Id
b [ExprStmt GhcTc]
quals
CoreExpr
core_expr <- CoreExpr
-> HsMatchContext GhcRn
-> LPat GhcTc
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimply (forall b. Id -> Expr b
Var Id
x) (forall p. HsStmtContext p -> HsMatchContext p
StmtCtxt forall p. HsStmtContext p
ListComp)
LPat GhcTc
pat CoreExpr
core_rest (forall b. Id -> Expr b
Var Id
b)
forall (m :: * -> *).
MonadThings m =>
Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr -> m CoreExpr
mkFoldrExpr Type
x_ty Type
b_ty (forall b. [b] -> Expr b -> Expr b
mkLams [Id
x, Id
b] CoreExpr
core_expr) (forall b. Id -> Expr b
Var Id
n_id) CoreExpr
core_list1
mkZipBind :: [Type] -> DsM (Id, CoreExpr)
mkZipBind :: [Type] -> DsM (Id, CoreExpr)
mkZipBind [Type]
elt_tys = do
[Id]
ass <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Type -> Type -> DsM Id
newSysLocalDs Type
Many) [Type]
elt_list_tys
[Id]
as' <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Type -> Type -> DsM Id
newSysLocalDs Type
Many) [Type]
elt_tys
[Id]
as's <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Type -> Type -> DsM Id
newSysLocalDs Type
Many) [Type]
elt_list_tys
Id
zip_fn <- Type -> Type -> DsM Id
newSysLocalDs Type
Many Type
zip_fn_ty
let inner_rhs :: CoreExpr
inner_rhs = Type -> CoreExpr -> CoreExpr -> CoreExpr
mkConsExpr Type
elt_tuple_ty
([Id] -> CoreExpr
mkBigCoreVarTup [Id]
as')
(forall b. Expr b -> [Id] -> Expr b
mkVarApps (forall b. Id -> Expr b
Var Id
zip_fn) [Id]
as's)
zip_body :: CoreExpr
zip_body = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Id, Id, Id) -> CoreExpr -> CoreExpr
mk_case CoreExpr
inner_rhs (forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Id]
ass [Id]
as' [Id]
as's)
forall (m :: * -> *) a. Monad m => a -> m a
return (Id
zip_fn, forall b. [b] -> Expr b -> Expr b
mkLams [Id]
ass CoreExpr
zip_body)
where
elt_list_tys :: [Type]
elt_list_tys = forall a b. (a -> b) -> [a] -> [b]
map Type -> Type
mkListTy [Type]
elt_tys
elt_tuple_ty :: Type
elt_tuple_ty = [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
= forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case (forall b. Id -> Expr b
Var Id
as) Id
as Type
elt_tuple_list_ty
[ forall b. AltCon -> [b] -> Expr b -> Alt b
Alt (DataCon -> AltCon
DataAlt DataCon
nilDataCon) [] (Type -> CoreExpr
mkNilExpr Type
elt_tuple_ty)
, 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]
_
= forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
mkUnzipBind TransForm
_ [Type]
elt_tys
= do { Id
ax <- Type -> Type -> DsM Id
newSysLocalDs Type
Many Type
elt_tuple_ty
; Id
axs <- Type -> Type -> DsM Id
newSysLocalDs Type
Many Type
elt_list_tuple_ty
; Id
ys <- Type -> Type -> DsM Id
newSysLocalDs Type
Many Type
elt_tuple_list_ty
; [Id]
xs <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Type -> Type -> DsM Id
newSysLocalDs Type
Many) [Type]
elt_tys
; [Id]
xss <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Type -> Type -> DsM Id
newSysLocalDs Type
Many) [Type]
elt_list_tys
; Id
unzip_fn <- Type -> Type -> DsM Id
newSysLocalDs Type
Many Type
unzip_fn_ty
; [UniqSupply
us1, UniqSupply
us2] <- forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply, forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply]
; let nil_tuple :: CoreExpr
nil_tuple = [CoreExpr] -> CoreExpr
mkBigCoreTup (forall a b. (a -> b) -> [a] -> [b]
map Type -> CoreExpr
mkNilExpr [Type]
elt_tys)
concat_expressions :: [CoreExpr]
concat_expressions = forall a b. (a -> b) -> [a] -> [b]
map (Type, CoreExpr, CoreExpr) -> CoreExpr
mkConcatExpression (forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Type]
elt_tys (forall a b. (a -> b) -> [a] -> [b]
map forall b. Id -> Expr b
Var [Id]
xs) (forall a b. (a -> b) -> [a] -> [b]
map forall b. Id -> Expr b
Var [Id]
xss))
tupled_concat_expression :: CoreExpr
tupled_concat_expression = [CoreExpr] -> CoreExpr
mkBigCoreTup [CoreExpr]
concat_expressions
folder_body_inner_case :: CoreExpr
folder_body_inner_case = UniqSupply -> [Id] -> CoreExpr -> Id -> CoreExpr -> CoreExpr
mkTupleCase UniqSupply
us1 [Id]
xss CoreExpr
tupled_concat_expression Id
axs (forall b. Id -> Expr b
Var Id
axs)
folder_body_outer_case :: CoreExpr
folder_body_outer_case = UniqSupply -> [Id] -> CoreExpr -> Id -> CoreExpr -> CoreExpr
mkTupleCase UniqSupply
us2 [Id]
xs CoreExpr
folder_body_inner_case Id
ax (forall b. Id -> Expr b
Var Id
ax)
folder_body :: CoreExpr
folder_body = forall b. [b] -> Expr b -> Expr b
mkLams [Id
ax, Id
axs] CoreExpr
folder_body_outer_case
; CoreExpr
unzip_body <- forall (m :: * -> *).
MonadThings m =>
Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr -> m CoreExpr
mkFoldrExpr Type
elt_tuple_ty Type
elt_list_tuple_ty CoreExpr
folder_body CoreExpr
nil_tuple (forall b. Id -> Expr b
Var Id
ys)
; forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (Id
unzip_fn, forall b. [b] -> Expr b -> Expr b
mkLams [Id
ys] CoreExpr
unzip_body)) }
where
elt_tuple_ty :: Type
elt_tuple_ty = [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 = 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
mkBigCoreTupTy [Type]
elt_list_tys
unzip_fn_ty :: Type
unzip_fn_ty = Type
elt_tuple_list_ty 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 [] = forall a. String -> a
panic String
"dsMcStmts"
dsMcStmts ((L SrcSpanAnnA
loc Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
stmt) : [ExprLStmt GhcTc]
lstmts) = forall ann a. SrcSpanAnn' ann -> DsM a -> DsM a
putSrcSpanDsA SrcSpanAnnA
loc (ExprStmt GhcTc -> [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmt 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
= ASSERT( null stmts )
do { CoreExpr
body' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
body
; SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
ret_op [CoreExpr
body'] }
dsMcStmt (LetStmt XLetStmt GhcTc GhcTc (LHsExpr GhcTc)
_ HsLocalBindsLR GhcTc GhcTc
binds) [ExprLStmt GhcTc]
stmts
= do { CoreExpr
rest <- [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts [ExprLStmt GhcTc]
stmts
; HsLocalBindsLR GhcTc GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds HsLocalBindsLR GhcTc GhcTc
binds CoreExpr
rest }
dsMcStmt (BindStmt XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
xbs LPat GhcTc
pat LHsExpr GhcTc
rhs) [ExprLStmt GhcTc]
stmts
= do { CoreExpr
rhs' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
rhs
; LPat GhcTc
-> CoreExpr
-> SyntaxExpr GhcTc
-> Maybe (SyntaxExpr GhcTc)
-> Type
-> [ExprLStmt GhcTc]
-> DsM CoreExpr
dsMcBindStmt LPat GhcTc
pat CoreExpr
rhs' (XBindStmtTc -> SyntaxExpr GhcTc
xbstc_bindOp XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
xbs) (XBindStmtTc -> Maybe (SyntaxExpr GhcTc)
xbstc_failOp XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
xbs) (XBindStmtTc -> Type
xbstc_boundResultType XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
xbs) [ExprLStmt GhcTc]
stmts }
dsMcStmt (BodyStmt XBodyStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
exp SyntaxExpr GhcTc
then_exp SyntaxExpr GhcTc
guard_exp) [ExprLStmt GhcTc]
stmts
= do { CoreExpr
exp' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
exp
; CoreExpr
rest <- [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts [ExprLStmt GhcTc]
stmts
; CoreExpr
guard_exp' <- SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
guard_exp [CoreExpr
exp']
; SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
then_exp [CoreExpr
guard_exp', CoreExpr
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) = forall a b. [(a, b)] -> ([a], [b])
unzip [(IdP GhcTc, IdP GhcTc)]
bndrs
; let from_bndr_tys :: [Type]
from_bndr_tys = forall a b. (a -> b) -> [a] -> [b]
map Id -> Type
idType [Id]
from_bndrs
; CoreExpr
expr' <- [ExprLStmt GhcTc] -> [Id] -> SyntaxExpr GhcTc -> DsM CoreExpr
dsInnerMonadComp [ExprLStmt GhcTc]
stmts [Id]
from_bndrs SyntaxExpr GhcTc
return_op
; CoreExpr
usingExpr' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
using
; [CoreExpr]
usingArgs' <- case Maybe (LHsExpr GhcTc)
by of
Maybe (LHsExpr GhcTc)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return [CoreExpr
expr']
Just LHsExpr GhcTc
by_e -> do { CoreExpr
by_e' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
by_e
; CoreExpr
lam' <- [Id] -> CoreExpr -> DsM CoreExpr
matchTuple [Id]
from_bndrs CoreExpr
by_e'
; forall (m :: * -> *) a. Monad m => a -> m a
return [CoreExpr
lam', CoreExpr
expr'] }
; let tup_n_ty' :: Type
tup_n_ty' = [Id] -> Type
mkBigCoreVarTupTy [Id]
to_bndrs
; CoreExpr
body <- [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts [ExprLStmt GhcTc]
stmts_rest
; Id
n_tup_var' <- Type -> Type -> DsM Id
newSysLocalDsNoLP Type
Many XTransStmt GhcTc GhcTc (LHsExpr GhcTc)
n_tup_ty'
; Id
tup_n_var' <- Type -> Type -> DsM Id
newSysLocalDs Type
Many Type
tup_n_ty'
; CoreExpr
tup_n_expr' <- TransForm -> HsExpr GhcTc -> Id -> [Type] -> DsM CoreExpr
mkMcUnzipM TransForm
form HsExpr GhcTc
fmap_op Id
n_tup_var' [Type]
from_bndr_tys
; UniqSupply
us <- forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply
; let rhs' :: CoreExpr
rhs' = forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
usingExpr' [CoreExpr]
usingArgs'
body' :: CoreExpr
body' = UniqSupply -> [Id] -> CoreExpr -> Id -> CoreExpr -> CoreExpr
mkTupleCase UniqSupply
us [Id]
to_bndrs CoreExpr
body Id
tup_n_var' CoreExpr
tup_n_expr'
; SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
bind_op [CoreExpr
rhs', forall b. b -> Expr b -> Expr b
Lam Id
n_tup_var' CoreExpr
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 { [(CoreExpr, Type)]
exps_w_tys <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type)
ds_inner [ParStmtBlock GhcTc GhcTc]
blocks
; CoreExpr
mzip_op' <- HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
mzip_op
; let
pats :: [GenLocated SrcSpanAnnA (Pat GhcTc)]
pats = [ [Id] -> LPat GhcTc
mkBigLHsVarPatTupId [IdP GhcTc]
bs | ParStmtBlock XParStmtBlock GhcTc GhcTc
_ [ExprLStmt GhcTc]
_ [IdP GhcTc]
bs SyntaxExpr GhcTc
_ <- [ParStmtBlock GhcTc GhcTc]
blocks]
pat :: GenLocated SrcSpanAnnA (Pat GhcTc)
pat = 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 [GenLocated SrcSpanAnnA (Pat GhcTc)
p1, GenLocated SrcSpanAnnA (Pat GhcTc)
p2]) [GenLocated SrcSpanAnnA (Pat GhcTc)]
pats
(CoreExpr
rhs, Type
_) = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 (\(CoreExpr
e1,Type
t1) (CoreExpr
e2,Type
t2) ->
(forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
mzip_op' [forall b. Type -> Expr b
Type Type
t1, forall b. Type -> Expr b
Type Type
t2, CoreExpr
e1, CoreExpr
e2],
[Type] -> Type
mkBoxedTupleTy [Type
t1,Type
t2]))
[(CoreExpr, Type)]
exps_w_tys
; LPat GhcTc
-> CoreExpr
-> SyntaxExpr GhcTc
-> Maybe (SyntaxExpr GhcTc)
-> Type
-> [ExprLStmt GhcTc]
-> DsM CoreExpr
dsMcBindStmt GenLocated SrcSpanAnnA (Pat GhcTc)
pat CoreExpr
rhs SyntaxExpr GhcTc
bind_op forall a. Maybe a
Nothing XParStmt GhcTc GhcTc (LHsExpr GhcTc)
bind_ty [ExprLStmt GhcTc]
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 { CoreExpr
exp <- [ExprLStmt GhcTc] -> [Id] -> SyntaxExpr GhcTc -> DsM CoreExpr
dsInnerMonadComp [ExprLStmt GhcTc]
stmts [IdP GhcTc]
bndrs SyntaxExpr GhcTc
return_op
; forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr
exp, [Id] -> Type
mkBigCoreVarTupTy [IdP GhcTc]
bndrs) }
dsMcStmt ExprStmt GhcTc
stmt [ExprLStmt GhcTc]
_ = forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"dsMcStmt: unexpected stmt" (forall a. Outputable a => a -> SDoc
ppr ExprStmt GhcTc
stmt)
matchTuple :: [Id] -> CoreExpr -> DsM CoreExpr
matchTuple :: [Id] -> CoreExpr -> DsM CoreExpr
matchTuple [Id]
ids CoreExpr
body
= do { UniqSupply
us <- forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply
; Id
tup_id <- Type -> Type -> DsM Id
newSysLocalDs Type
Many ([Id] -> Type
mkBigCoreVarTupTy [Id]
ids)
; forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. b -> Expr b -> Expr b
Lam Id
tup_id forall a b. (a -> b) -> a -> b
$ UniqSupply -> [Id] -> CoreExpr -> Id -> CoreExpr -> CoreExpr
mkTupleCase UniqSupply
us [Id]
ids CoreExpr
body Id
tup_id (forall b. Id -> Expr b
Var Id
tup_id)) }
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 { CoreExpr
body <- [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts [ExprLStmt GhcTc]
stmts
; Id
var <- Type -> LPat GhcTc -> DsM Id
selectSimpleMatchVarL Type
Many LPat GhcTc
pat
; MatchResult CoreExpr
match <- Id
-> Maybe CoreExpr
-> HsMatchContext GhcRn
-> LPat GhcTc
-> Type
-> MatchResult CoreExpr
-> DsM (MatchResult CoreExpr)
matchSinglePatVar Id
var forall a. Maybe a
Nothing (forall p. HsStmtContext p -> HsMatchContext p
StmtCtxt (forall p. Maybe ModuleName -> HsStmtContext p
DoExpr forall a. Maybe a
Nothing)) LPat GhcTc
pat
Type
res1_ty (CoreExpr -> MatchResult CoreExpr
cantFailMatchResult CoreExpr
body)
; CoreExpr
match_code <- HsStmtContext GhcRn
-> LPat GhcTc
-> MatchResult CoreExpr
-> Maybe (SyntaxExpr GhcTc)
-> DsM CoreExpr
dsHandleMonadicFailure (forall p. HsStmtContext p
MonadComp :: HsStmtContext GhcRn) LPat GhcTc
pat MatchResult CoreExpr
match Maybe (SyntaxExpr GhcTc)
fail_op
; SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
bind_op [CoreExpr
rhs', forall b. b -> Expr b -> Expr b
Lam Id
var CoreExpr
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]
stmts forall a. [a] -> [a] -> [a]
++
[forall a an. a -> LocatedAn an a
noLocA (forall idL idR body.
XLastStmt idL idR body
-> body -> Maybe Bool -> SyntaxExpr idR -> StmtLR idL idR body
LastStmt NoExtField
noExtField ([Id] -> LHsExpr GhcTc
mkBigLHsVarTupId [Id]
bndrs) 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]
_
= forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. Id -> Expr b
Var Id
ys)
mkMcUnzipM TransForm
_ HsExpr GhcTc
fmap_op Id
ys [Type]
elt_tys
= do { CoreExpr
fmap_op' <- HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
fmap_op
; [Id]
xs <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Type -> Type -> DsM Id
newSysLocalDs Type
Many) [Type]
elt_tys
; let tup_ty :: Type
tup_ty = [Type] -> Type
mkBigCoreTupTy [Type]
elt_tys
; Id
tup_xs <- Type -> Type -> DsM Id
newSysLocalDs Type
Many Type
tup_ty
; let mk_elt :: Int -> CoreExpr
mk_elt Int
i = forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
fmap_op'
[ forall b. Type -> Expr b
Type Type
tup_ty, forall b. Type -> Expr b
Type (forall a. Outputable a => [a] -> Int -> a
getNth [Type]
elt_tys Int
i)
, Int -> CoreExpr
mk_sel Int
i, forall b. Id -> Expr b
Var Id
ys]
mk_sel :: Int -> CoreExpr
mk_sel Int
n = forall b. b -> Expr b -> Expr b
Lam Id
tup_xs forall a b. (a -> b) -> a -> b
$
[Id] -> Id -> Id -> CoreExpr -> CoreExpr
mkTupleSelector [Id]
xs (forall a. Outputable a => [a] -> Int -> a
getNth [Id]
xs Int
n) Id
tup_xs (forall b. Id -> Expr b
Var Id
tup_xs)
; forall (m :: * -> *) a. Monad m => a -> m a
return ([CoreExpr] -> CoreExpr
mkBigCoreTup (forall a b. (a -> b) -> [a] -> [b]
map Int -> CoreExpr
mk_elt [Int
0..forall (t :: * -> *) a. Foldable t => t a -> Int
length [Type]
elt_tys forall a. Num a => a -> a -> a
- Int
1])) }