{-# LANGUAGE CPP, NamedFieldPuns #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}
module DsListComp ( dsListComp, dsMonadComp ) where
#include "HsVersions.h"
import GhcPrelude
import {-# SOURCE #-} DsExpr ( dsExpr, dsLExpr, dsLExprNoLP, dsLocalBinds, dsSyntaxExpr )
import HsSyn
import TcHsSyn
import CoreSyn
import MkCore
import DsMonad
import DsUtils
import DynFlags
import CoreUtils
import Id
import Type
import TysWiredIn
import Match
import PrelNames
import SrcLoc
import Outputable
import TcType
import ListSetOps( getNth )
import Util
dsListComp :: [ExprLStmt GhcTc]
-> Type
-> DsM CoreExpr
dsListComp :: [ExprLStmt GhcTc] -> Type -> DsM CoreExpr
dsListComp lquals :: [ExprLStmt GhcTc]
lquals res_ty :: Type
res_ty = do
DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals = (ExprLStmt GhcTc -> StmtLR GhcTc GhcTc (LHsExpr GhcTc))
-> [ExprLStmt GhcTc] -> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
forall a b. (a -> b) -> [a] -> [b]
map ExprLStmt GhcTc -> StmtLR GhcTc GhcTc (LHsExpr GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc [ExprLStmt GhcTc]
lquals
elt_ty :: Type
elt_ty = case Type -> [Type]
tcTyConAppArgs Type
res_ty of
[elt_ty :: Type
elt_ty] -> Type
elt_ty
_ -> String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic "dsListComp" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
res_ty SDoc -> SDoc -> SDoc
$$ [ExprLStmt GhcTc] -> 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
|| [StmtLR GhcTc GhcTc (LHsExpr GhcTc)] -> Bool
forall idL idR body. [StmtLR idL idR body] -> Bool
isParallelComp [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals
then [StmtLR GhcTc GhcTc (LHsExpr GhcTc)] -> CoreExpr -> DsM CoreExpr
deListComp [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals (Type -> CoreExpr
mkNilExpr Type
elt_ty)
else Type -> ((Id, Type) -> (Id, Type) -> DsM CoreExpr) -> DsM CoreExpr
forall (m :: * -> *).
(MonadFail m, MonadThings m, MonadUnique m) =>
Type -> ((Id, Type) -> (Id, Type) -> m CoreExpr) -> m CoreExpr
mkBuildExpr Type
elt_ty (\(c :: Id
c, _) (n :: Id
n, _) -> Id -> Id -> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)] -> DsM CoreExpr
dfListComp Id
c Id
n [StmtLR GhcTc GhcTc (LHsExpr 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 _ = Bool
False
dsInnerListComp :: (ParStmtBlock GhcTc GhcTc) -> DsM (CoreExpr, Type)
dsInnerListComp :: ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type)
dsInnerListComp (ParStmtBlock _ stmts :: [ExprLStmt GhcTc]
stmts bndrs :: [IdP GhcTc]
bndrs _)
= do { let bndrs_tuple_type :: Type
bndrs_tuple_type = [Id] -> Type
mkBigCoreVarTupTy [Id]
[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 [ExprLStmt GhcTc] -> [ExprLStmt GhcTc] -> [ExprLStmt GhcTc]
forall a. [a] -> [a] -> [a]
++ [SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc)
-> SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcTc -> StmtLR GhcTc GhcTc (LHsExpr GhcTc)
forall (bodyR :: * -> *) (idR :: Pass) (idL :: Pass).
Located (bodyR (GhcPass idR))
-> StmtLR
(GhcPass idL) (GhcPass idR) (Located (bodyR (GhcPass idR)))
mkLastStmt ([Id] -> LHsExpr GhcTc
mkBigLHsVarTupId [Id]
[IdP GhcTc]
bndrs)]) Type
list_ty
; (CoreExpr, Type) -> DsM (CoreExpr, Type)
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr
expr, Type
bndrs_tuple_type) }
dsInnerListComp (XParStmtBlock{}) = String -> DsM (CoreExpr, Type)
forall a. String -> a
panic "dsInnerListComp"
dsTransStmt :: ExprStmt GhcTc -> DsM (CoreExpr, LPat GhcTc)
dsTransStmt :: StmtLR GhcTc GhcTc (LHsExpr 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 (from_bndrs :: [Id]
from_bndrs, to_bndrs :: [Id]
to_bndrs) = [(Id, Id)] -> ([Id], [Id])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Id, Id)]
[(IdP GhcTc, IdP GhcTc)]
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
mkBigCoreTupTy [Type]
to_bndrs_tys
(expr' :: CoreExpr
expr', from_tup_ty :: Type
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
NoExt
noExt [ExprLStmt GhcTc]
stmts
[Id]
[IdP GhcTc]
from_bndrs SyntaxExpr GhcTc
forall (p :: Pass). SyntaxExpr (GhcPass p)
noSyntaxExpr)
CoreExpr
usingExpr' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
using
[CoreExpr]
usingArgs' <- case Maybe (LHsExpr GhcTc)
by of
Nothing -> [CoreExpr] -> IOEnv (Env DsGblEnv DsLclEnv) [CoreExpr]
forall (m :: * -> *) a. Monad m => a -> m a
return [CoreExpr
expr']
Just by_e :: 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'
; [CoreExpr] -> IOEnv (Env DsGblEnv DsLclEnv) [CoreExpr]
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' = CoreExpr -> [CoreExpr] -> CoreExpr
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
Nothing -> CoreExpr
inner_list_expr'
Just (unzip_fn' :: Id
unzip_fn', unzip_rhs' :: 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' ]
Type -> SDoc -> DsM ()
dsNoLevPoly (Arity -> Type -> Type
HasDebugCallStack => Arity -> Type -> Type
tcFunResultTyN ([CoreExpr] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [CoreExpr]
usingArgs') (CoreExpr -> Type
exprType CoreExpr
usingExpr'))
(String -> SDoc
text "In the result of a" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (String -> SDoc
text "using") SDoc -> SDoc -> SDoc
<+> String -> SDoc
text "function:" SDoc -> SDoc -> SDoc
<+> LHsExpr GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcTc
using)
let pat :: LPat GhcTc
pat = [Id] -> LPat GhcTc
mkBigLHsVarPatTupId [Id]
to_bndrs
(CoreExpr, LPat GhcTc) -> DsM (CoreExpr, LPat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr
bound_unzipped_inner_list_expr', LPat GhcTc
pat)
dsTransStmt _ = String -> DsM (CoreExpr, LPat GhcTc)
forall a. String -> a
panic "dsTransStmt: Not given a TransStmt"
deListComp :: [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deListComp :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)] -> CoreExpr -> DsM CoreExpr
deListComp [] _ = String -> DsM CoreExpr
forall a. String -> a
panic "deListComp"
deListComp (LastStmt _ body :: LHsExpr GhcTc
body _ _ : quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals) list :: CoreExpr
list
=
ASSERT( null quals )
do { CoreExpr
core_body <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
body
; CoreExpr -> DsM CoreExpr
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 _ guard :: LHsExpr GhcTc
guard _ _ : quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals) list :: CoreExpr
list = do
CoreExpr
core_guard <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
guard
CoreExpr
core_rest <- [StmtLR GhcTc GhcTc (LHsExpr GhcTc)] -> CoreExpr -> DsM CoreExpr
deListComp [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals CoreExpr
list
CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
mkIfThenElse CoreExpr
core_guard CoreExpr
core_rest CoreExpr
list)
deListComp (LetStmt _ binds :: LHsLocalBindsLR GhcTc GhcTc
binds : quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals) list :: CoreExpr
list = do
CoreExpr
core_rest <- [StmtLR GhcTc GhcTc (LHsExpr GhcTc)] -> CoreExpr -> DsM CoreExpr
deListComp [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals CoreExpr
list
LHsLocalBindsLR GhcTc GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds LHsLocalBindsLR GhcTc GhcTc
binds CoreExpr
core_rest
deListComp (stmt :: StmtLR GhcTc GhcTc (LHsExpr GhcTc)
stmt@(TransStmt {}) : quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals) list :: CoreExpr
list = do
(inner_list_expr :: CoreExpr
inner_list_expr, pat :: LPat GhcTc
pat) <- StmtLR GhcTc GhcTc (LHsExpr GhcTc) -> DsM (CoreExpr, LPat GhcTc)
dsTransStmt StmtLR GhcTc GhcTc (LHsExpr GhcTc)
stmt
LPat GhcTc
-> CoreExpr
-> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
-> CoreExpr
-> DsM CoreExpr
deBindComp LPat GhcTc
pat CoreExpr
inner_list_expr [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals CoreExpr
list
deListComp (BindStmt _ pat :: LPat GhcTc
pat list1 :: LHsExpr GhcTc
list1 _ _ : quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals) core_list2 :: CoreExpr
core_list2 = do
CoreExpr
core_list1 <- LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
list1
LPat GhcTc
-> CoreExpr
-> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
-> CoreExpr
-> DsM CoreExpr
deBindComp LPat GhcTc
pat CoreExpr
core_list1 [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals CoreExpr
core_list2
deListComp (ParStmt _ stmtss_w_bndrs :: [ParStmtBlock GhcTc GhcTc]
stmtss_w_bndrs _ _ : quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals) list :: CoreExpr
list
= do { [(CoreExpr, Type)]
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)
mapM ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type)
dsInnerListComp [ParStmtBlock GhcTc GhcTc]
stmtss_w_bndrs
; let (exps :: [CoreExpr]
exps, qual_tys :: [Type]
qual_tys) = [(CoreExpr, Type)] -> ([CoreExpr], [Type])
forall a b. [(a, b)] -> ([a], [b])
unzip [(CoreExpr, Type)]
exps_and_qual_tys
; (zip_fn :: Id
zip_fn, zip_rhs :: CoreExpr
zip_rhs) <- [Type] -> DsM (Id, CoreExpr)
mkZipBind [Type]
qual_tys
; LPat GhcTc
-> CoreExpr
-> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
-> CoreExpr
-> DsM CoreExpr
deBindComp LPat GhcTc
pat (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
zip_fn, CoreExpr
zip_rhs)]) (CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
zip_fn) [CoreExpr]
exps))
[StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals CoreExpr
list }
where
bndrs_s :: [[Id]]
bndrs_s = [[Id]
[IdP GhcTc]
bs | ParStmtBlock _ _ bs :: [IdP GhcTc]
bs _ <- [ParStmtBlock GhcTc GhcTc]
stmtss_w_bndrs]
pat :: LPat GhcTc
pat = [LPat GhcTc] -> LPat GhcTc
mkBigLHsPatTupId [LPat GhcTc]
pats
pats :: [LPat GhcTc]
pats = ([Id] -> LPat GhcTc) -> [[Id]] -> [LPat GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map [Id] -> LPat GhcTc
mkBigLHsVarPatTupId [[Id]]
bndrs_s
deListComp (RecStmt {} : _) _ = String -> DsM CoreExpr
forall a. String -> a
panic "deListComp RecStmt"
deListComp (ApplicativeStmt {} : _) _ =
String -> DsM CoreExpr
forall a. String -> a
panic "deListComp ApplicativeStmt"
deListComp (XStmtLR {} : _) _ =
String -> DsM CoreExpr
forall a. String -> a
panic "deListComp XStmtLR"
deBindComp :: OutPat GhcTc
-> CoreExpr
-> [ExprStmt GhcTc]
-> CoreExpr
-> DsM (Expr Id)
deBindComp :: LPat GhcTc
-> CoreExpr
-> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
-> CoreExpr
-> DsM CoreExpr
deBindComp pat :: LPat GhcTc
pat core_list1 :: CoreExpr
core_list1 quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals core_list2 :: 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
`mkFunTy` Type
res_ty
[h :: Id
h, u1 :: Id
u1, u2 :: Id
u2, u3 :: Id
u3] <- [Type] -> DsM [Id]
newSysLocalsDs [Type
h_ty, Type
u1_ty, Type
u2_ty, Type
u3_ty]
let
core_fail :: CoreExpr
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
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
CoreExpr
rest_expr <- [StmtLR GhcTc GhcTc (LHsExpr GhcTc)] -> CoreExpr -> DsM CoreExpr
deListComp [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals CoreExpr
core_fail
CoreExpr
core_match <- CoreExpr
-> HsMatchContext Name
-> LPat GhcTc
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimply (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
u2) (HsStmtContext Name -> HsMatchContext Name
forall id. HsStmtContext id -> HsMatchContext id
StmtCtxt HsStmtContext Name
forall id. HsStmtContext id
ListComp) LPat GhcTc
pat CoreExpr
rest_expr CoreExpr
core_fail
let
rhs :: CoreExpr
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
[(DataCon -> AltCon
DataAlt DataCon
nilDataCon, [], CoreExpr
core_list2),
(DataCon -> AltCon
DataAlt DataCon
consDataCon, [Id
u2, Id
u3], CoreExpr
core_match)]
CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (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
h, CoreExpr
rhs)]) CoreExpr
letrec_body)
dfListComp :: Id -> Id
-> [ExprStmt GhcTc]
-> DsM CoreExpr
dfListComp :: Id -> Id -> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)] -> DsM CoreExpr
dfListComp _ _ [] = String -> DsM CoreExpr
forall a. String -> a
panic "dfListComp"
dfListComp c_id :: Id
c_id n_id :: Id
n_id (LastStmt _ body :: LHsExpr GhcTc
body _ _ : quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals)
= ASSERT( null quals )
do { CoreExpr
core_body <- LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
body
; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
c_id) [CoreExpr
core_body, Id -> CoreExpr
forall b. Id -> Expr b
Var Id
n_id]) }
dfListComp c_id :: Id
c_id n_id :: Id
n_id (BodyStmt _ guard :: LHsExpr GhcTc
guard _ _ : quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals) = do
CoreExpr
core_guard <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
guard
CoreExpr
core_rest <- Id -> Id -> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)] -> DsM CoreExpr
dfListComp Id
c_id Id
n_id [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals
CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
mkIfThenElse CoreExpr
core_guard CoreExpr
core_rest (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
n_id))
dfListComp c_id :: Id
c_id n_id :: Id
n_id (LetStmt _ binds :: LHsLocalBindsLR GhcTc GhcTc
binds : quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals) = do
CoreExpr
core_rest <- Id -> Id -> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)] -> DsM CoreExpr
dfListComp Id
c_id Id
n_id [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals
LHsLocalBindsLR GhcTc GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds LHsLocalBindsLR GhcTc GhcTc
binds CoreExpr
core_rest
dfListComp c_id :: Id
c_id n_id :: Id
n_id (stmt :: StmtLR GhcTc GhcTc (LHsExpr GhcTc)
stmt@(TransStmt {}) : quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals) = do
(inner_list_expr :: CoreExpr
inner_list_expr, pat :: LPat GhcTc
pat) <- StmtLR GhcTc GhcTc (LHsExpr GhcTc) -> DsM (CoreExpr, LPat GhcTc)
dsTransStmt StmtLR GhcTc GhcTc (LHsExpr GhcTc)
stmt
Id
-> Id
-> (LPat GhcTc, CoreExpr)
-> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
-> DsM CoreExpr
dfBindComp Id
c_id Id
n_id (LPat GhcTc
pat, CoreExpr
inner_list_expr) [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals
dfListComp c_id :: Id
c_id n_id :: Id
n_id (BindStmt _ pat :: LPat GhcTc
pat list1 :: LHsExpr GhcTc
list1 _ _ : quals :: [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals) = do
CoreExpr
core_list1 <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
list1
Id
-> Id
-> (LPat GhcTc, CoreExpr)
-> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
-> DsM CoreExpr
dfBindComp Id
c_id Id
n_id (LPat GhcTc
pat, CoreExpr
core_list1) [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals
dfListComp _ _ (ParStmt {} : _) = String -> DsM CoreExpr
forall a. String -> a
panic "dfListComp ParStmt"
dfListComp _ _ (RecStmt {} : _) = String -> DsM CoreExpr
forall a. String -> a
panic "dfListComp RecStmt"
dfListComp _ _ (ApplicativeStmt {} : _) =
String -> DsM CoreExpr
forall a. String -> a
panic "dfListComp ApplicativeStmt"
dfListComp _ _ (XStmtLR {} : _) =
String -> DsM CoreExpr
forall a. String -> a
panic "dfListComp XStmtLR"
dfBindComp :: Id -> Id
-> (LPat GhcTc, CoreExpr)
-> [ExprStmt GhcTc]
-> DsM CoreExpr
dfBindComp :: Id
-> Id
-> (LPat GhcTc, CoreExpr)
-> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
-> DsM CoreExpr
dfBindComp c_id :: Id
c_id n_id :: Id
n_id (pat :: LPat GhcTc
pat, core_list1 :: CoreExpr
core_list1) quals :: [StmtLR GhcTc GhcTc (LHsExpr 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 -> DsM Id
newSysLocalDs Type
b_ty
Id
x <- Type -> DsM Id
newSysLocalDs Type
x_ty
CoreExpr
core_rest <- Id -> Id -> [StmtLR GhcTc GhcTc (LHsExpr GhcTc)] -> DsM CoreExpr
dfListComp Id
c_id Id
b [StmtLR GhcTc GhcTc (LHsExpr GhcTc)]
quals
CoreExpr
core_expr <- CoreExpr
-> HsMatchContext Name
-> LPat GhcTc
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimply (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
x) (HsStmtContext Name -> HsMatchContext Name
forall id. HsStmtContext id -> HsMatchContext id
StmtCtxt HsStmtContext Name
forall id. HsStmtContext id
ListComp)
LPat GhcTc
pat CoreExpr
core_rest (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
b)
Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr -> DsM CoreExpr
forall (m :: * -> *).
MonadThings m =>
Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr -> m CoreExpr
mkFoldrExpr Type
x_ty Type
b_ty ([Id] -> CoreExpr -> CoreExpr
forall b. [b] -> Expr b -> Expr b
mkLams [Id
x, Id
b] CoreExpr
core_expr) (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
n_id) CoreExpr
core_list1
mkZipBind :: [Type] -> DsM (Id, CoreExpr)
mkZipBind :: [Type] -> DsM (Id, CoreExpr)
mkZipBind elt_tys :: [Type]
elt_tys = do
[Id]
ass <- (Type -> DsM Id) -> [Type] -> DsM [Id]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> DsM Id
newSysLocalDs [Type]
elt_list_tys
[Id]
as' <- (Type -> DsM Id) -> [Type] -> DsM [Id]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> DsM Id
newSysLocalDs [Type]
elt_tys
[Id]
as's <- (Type -> DsM Id) -> [Type] -> DsM [Id]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> DsM Id
newSysLocalDs [Type]
elt_list_tys
Id
zip_fn <- Type -> DsM Id
newSysLocalDs Type
zip_fn_ty
let inner_rhs :: CoreExpr
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 :: CoreExpr
zip_body = ((Id, Id, Id) -> CoreExpr -> CoreExpr)
-> CoreExpr -> [(Id, Id, Id)] -> CoreExpr
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)
(Id, CoreExpr) -> DsM (Id, CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return (Id
zip_fn, [Id] -> CoreExpr -> CoreExpr
forall b. [b] -> Expr b -> Expr b
mkLams [Id]
ass CoreExpr
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
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
mkFunTys [Type]
elt_list_tys Type
elt_tuple_list_ty
mk_case :: (Id, Id, Id) -> CoreExpr -> CoreExpr
mk_case (as :: Id
as, a' :: Id
a', as' :: Id
as') rest :: 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
[(DataCon -> AltCon
DataAlt DataCon
nilDataCon, [], Type -> CoreExpr
mkNilExpr Type
elt_tuple_ty),
(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 ThenForm _
= Maybe (Id, CoreExpr) -> DsM (Maybe (Id, CoreExpr))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Id, CoreExpr)
forall a. Maybe a
Nothing
mkUnzipBind _ elt_tys :: [Type]
elt_tys
= do { Id
ax <- Type -> DsM Id
newSysLocalDs Type
elt_tuple_ty
; Id
axs <- Type -> DsM Id
newSysLocalDs Type
elt_list_tuple_ty
; Id
ys <- Type -> DsM Id
newSysLocalDs Type
elt_tuple_list_ty
; [Id]
xs <- (Type -> DsM Id) -> [Type] -> DsM [Id]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> DsM Id
newSysLocalDs [Type]
elt_tys
; [Id]
xss <- (Type -> DsM Id) -> [Type] -> DsM [Id]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> DsM Id
newSysLocalDs [Type]
elt_list_tys
; Id
unzip_fn <- Type -> DsM Id
newSysLocalDs Type
unzip_fn_ty
; [us1 :: UniqSupply
us1, us2 :: UniqSupply
us2] <- [IOEnv (Env DsGblEnv DsLclEnv) UniqSupply]
-> IOEnv (Env DsGblEnv DsLclEnv) [UniqSupply]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [IOEnv (Env DsGblEnv DsLclEnv) UniqSupply
forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply, IOEnv (Env DsGblEnv DsLclEnv) UniqSupply
forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply]
; let nil_tuple :: CoreExpr
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 :: [CoreExpr]
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
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 (Id -> CoreExpr
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 (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
ax)
folder_body :: CoreExpr
folder_body = [Id] -> CoreExpr -> CoreExpr
forall b. [b] -> Expr b -> Expr b
mkLams [Id
ax, Id
axs] CoreExpr
folder_body_outer_case
; CoreExpr
unzip_body <- Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr -> DsM CoreExpr
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 (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
ys)
; Maybe (Id, CoreExpr) -> DsM (Maybe (Id, CoreExpr))
forall (m :: * -> *) a. Monad m => a -> m a
return ((Id, CoreExpr) -> Maybe (Id, CoreExpr)
forall a. a -> Maybe a
Just (Id
unzip_fn, [Id] -> CoreExpr -> CoreExpr
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 = (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
mkBigCoreTupTy [Type]
elt_list_tys
unzip_fn_ty :: Type
unzip_fn_ty = Type
elt_tuple_list_ty Type -> Type -> Type
`mkFunTy` Type
elt_list_tuple_ty
mkConcatExpression :: (Type, CoreExpr, CoreExpr) -> CoreExpr
mkConcatExpression (list_element_ty :: Type
list_element_ty, head :: CoreExpr
head, tail :: 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 stmts :: [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. String -> a
panic "dsMcStmts"
dsMcStmts ((ExprLStmt GhcTc -> Located (SrcSpanLess (ExprLStmt GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc stmt :: SrcSpanLess (ExprLStmt GhcTc)
stmt) : lstmts :: [ExprLStmt GhcTc]
lstmts) = SrcSpan -> DsM CoreExpr -> DsM CoreExpr
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc (StmtLR GhcTc GhcTc (LHsExpr GhcTc)
-> [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmt SrcSpanLess (ExprLStmt GhcTc)
StmtLR GhcTc GhcTc (LHsExpr GhcTc)
stmt [ExprLStmt GhcTc]
lstmts)
dsMcStmt :: ExprStmt GhcTc -> [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmt :: StmtLR GhcTc GhcTc (LHsExpr GhcTc)
-> [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmt (LastStmt _ body :: LHsExpr GhcTc
body _ ret_op :: SyntaxExpr GhcTc
ret_op) stmts :: [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 _ binds :: LHsLocalBindsLR GhcTc GhcTc
binds) stmts :: [ExprLStmt GhcTc]
stmts
= do { CoreExpr
rest <- [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts [ExprLStmt GhcTc]
stmts
; LHsLocalBindsLR GhcTc GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds LHsLocalBindsLR GhcTc GhcTc
binds CoreExpr
rest }
dsMcStmt (BindStmt bind_ty :: XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
bind_ty pat :: LPat GhcTc
pat rhs :: LHsExpr GhcTc
rhs bind_op :: SyntaxExpr GhcTc
bind_op fail_op :: SyntaxExpr GhcTc
fail_op) stmts :: [ExprLStmt GhcTc]
stmts
= do { CoreExpr
rhs' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
rhs
; LPat GhcTc
-> CoreExpr
-> SyntaxExpr GhcTc
-> SyntaxExpr GhcTc
-> Type
-> [ExprLStmt GhcTc]
-> DsM CoreExpr
dsMcBindStmt LPat GhcTc
pat CoreExpr
rhs' SyntaxExpr GhcTc
bind_op SyntaxExpr GhcTc
fail_op Type
XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
bind_ty [ExprLStmt GhcTc]
stmts }
dsMcStmt (BodyStmt _ exp :: LHsExpr GhcTc
exp then_exp :: SyntaxExpr GhcTc
then_exp guard_exp :: SyntaxExpr GhcTc
guard_exp) stmts :: [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 }) stmts_rest :: [ExprLStmt GhcTc]
stmts_rest
= do { let (from_bndrs :: [Id]
from_bndrs, to_bndrs :: [Id]
to_bndrs) = [(Id, Id)] -> ([Id], [Id])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Id, Id)]
[(IdP GhcTc, IdP GhcTc)]
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
; 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
Nothing -> [CoreExpr] -> IOEnv (Env DsGblEnv DsLclEnv) [CoreExpr]
forall (m :: * -> *) a. Monad m => a -> m a
return [CoreExpr
expr']
Just by_e :: 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'
; [CoreExpr] -> IOEnv (Env DsGblEnv DsLclEnv) [CoreExpr]
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 -> DsM Id
newSysLocalDsNoLP Type
XTransStmt GhcTc GhcTc (LHsExpr GhcTc)
n_tup_ty'
; Id
tup_n_var' <- Type -> DsM Id
newSysLocalDs 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 <- IOEnv (Env DsGblEnv DsLclEnv) UniqSupply
forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply
; let rhs' :: CoreExpr
rhs' = CoreExpr -> [CoreExpr] -> CoreExpr
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', Id -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Id
n_tup_var' CoreExpr
body'] }
dsMcStmt (ParStmt bind_ty :: XParStmt GhcTc GhcTc (LHsExpr GhcTc)
bind_ty blocks :: [ParStmtBlock GhcTc GhcTc]
blocks mzip_op :: HsExpr GhcTc
mzip_op bind_op :: SyntaxExpr GhcTc
bind_op) stmts_rest :: [ExprLStmt GhcTc]
stmts_rest
= do { [(CoreExpr, Type)]
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)
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 :: [LPat GhcTc]
pats = [ [Id] -> LPat GhcTc
mkBigLHsVarPatTupId [Id]
[IdP GhcTc]
bs | ParStmtBlock _ _ bs :: [IdP GhcTc]
bs _ <- [ParStmtBlock GhcTc GhcTc]
blocks]
pat :: LPat GhcTc
pat = (LPat GhcTc -> LPat GhcTc -> LPat GhcTc)
-> [LPat GhcTc] -> LPat GhcTc
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 (\p1 :: LPat GhcTc
p1 p2 :: LPat GhcTc
p2 -> [LPat GhcTc] -> LPat GhcTc
mkLHsPatTup [LPat GhcTc
p1, LPat GhcTc
p2]) [LPat GhcTc]
pats
(rhs :: CoreExpr
rhs, _) = ((CoreExpr, Type) -> (CoreExpr, Type) -> (CoreExpr, Type))
-> [(CoreExpr, Type)] -> (CoreExpr, Type)
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 (\(e1 :: CoreExpr
e1,t1 :: Type
t1) (e2 :: CoreExpr
e2,t2 :: 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]))
[(CoreExpr, Type)]
exps_w_tys
; LPat GhcTc
-> CoreExpr
-> SyntaxExpr GhcTc
-> SyntaxExpr GhcTc
-> Type
-> [ExprLStmt GhcTc]
-> DsM CoreExpr
dsMcBindStmt LPat GhcTc
pat CoreExpr
rhs SyntaxExpr GhcTc
bind_op SyntaxExpr GhcTc
forall (p :: Pass). SyntaxExpr (GhcPass p)
noSyntaxExpr Type
XParStmt GhcTc GhcTc (LHsExpr GhcTc)
bind_ty [ExprLStmt GhcTc]
stmts_rest }
where
ds_inner :: ParStmtBlock GhcTc GhcTc -> DsM (CoreExpr, Type)
ds_inner (ParStmtBlock _ stmts :: [ExprLStmt GhcTc]
stmts bndrs :: [IdP GhcTc]
bndrs return_op :: SyntaxExpr GhcTc
return_op)
= do { CoreExpr
exp <- [ExprLStmt GhcTc] -> [Id] -> SyntaxExpr GhcTc -> DsM CoreExpr
dsInnerMonadComp [ExprLStmt GhcTc]
stmts [Id]
[IdP GhcTc]
bndrs SyntaxExpr GhcTc
return_op
; (CoreExpr, Type) -> DsM (CoreExpr, Type)
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr
exp, [Id] -> Type
mkBigCoreVarTupTy [Id]
[IdP GhcTc]
bndrs) }
ds_inner (XParStmtBlock{}) = String -> DsM (CoreExpr, Type)
forall a. String -> a
panic "dsMcStmt"
dsMcStmt stmt :: StmtLR GhcTc GhcTc (LHsExpr GhcTc)
stmt _ = String -> SDoc -> DsM CoreExpr
forall a. HasCallStack => String -> SDoc -> a
pprPanic "dsMcStmt: unexpected stmt" (StmtLR GhcTc GhcTc (LHsExpr GhcTc) -> SDoc
forall a. Outputable a => a -> SDoc
ppr StmtLR GhcTc GhcTc (LHsExpr GhcTc)
stmt)
matchTuple :: [Id] -> CoreExpr -> DsM CoreExpr
matchTuple :: [Id] -> CoreExpr -> DsM CoreExpr
matchTuple ids :: [Id]
ids body :: CoreExpr
body
= do { UniqSupply
us <- IOEnv (Env DsGblEnv DsLclEnv) UniqSupply
forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply
; Id
tup_id <- Type -> DsM Id
newSysLocalDs ([Id] -> Type
mkBigCoreVarTupTy [Id]
ids)
; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Id
tup_id (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall a b. (a -> b) -> a -> b
$ UniqSupply -> [Id] -> CoreExpr -> Id -> CoreExpr -> CoreExpr
mkTupleCase UniqSupply
us [Id]
ids CoreExpr
body Id
tup_id (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
tup_id)) }
dsMcBindStmt :: LPat GhcTc
-> CoreExpr
-> SyntaxExpr GhcTc
-> SyntaxExpr GhcTc
-> Type
-> [ExprLStmt GhcTc]
-> DsM CoreExpr
dsMcBindStmt :: LPat GhcTc
-> CoreExpr
-> SyntaxExpr GhcTc
-> SyntaxExpr GhcTc
-> Type
-> [ExprLStmt GhcTc]
-> DsM CoreExpr
dsMcBindStmt pat :: LPat GhcTc
pat rhs' :: CoreExpr
rhs' bind_op :: SyntaxExpr GhcTc
bind_op fail_op :: SyntaxExpr GhcTc
fail_op res1_ty :: Type
res1_ty stmts :: [ExprLStmt GhcTc]
stmts
= do { CoreExpr
body <- [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts [ExprLStmt GhcTc]
stmts
; Id
var <- LPat GhcTc -> DsM Id
selectSimpleMatchVarL LPat GhcTc
pat
; MatchResult
match <- Id
-> HsMatchContext Name
-> LPat GhcTc
-> Type
-> MatchResult
-> DsM MatchResult
matchSinglePatVar Id
var (HsStmtContext Name -> HsMatchContext Name
forall id. HsStmtContext id -> HsMatchContext id
StmtCtxt HsStmtContext Name
forall id. HsStmtContext id
DoExpr) LPat GhcTc
pat
Type
res1_ty (CoreExpr -> MatchResult
cantFailMatchResult CoreExpr
body)
; CoreExpr
match_code <- LPat GhcTc -> MatchResult -> SyntaxExpr GhcTc -> DsM CoreExpr
forall e.
HasSrcSpan e =>
e -> MatchResult -> SyntaxExpr GhcTc -> DsM CoreExpr
handle_failure LPat GhcTc
pat MatchResult
match SyntaxExpr GhcTc
fail_op
; SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
bind_op [CoreExpr
rhs', Id -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Id
var CoreExpr
match_code] }
where
handle_failure :: e -> MatchResult -> SyntaxExpr GhcTc -> DsM CoreExpr
handle_failure pat :: e
pat match :: MatchResult
match fail_op :: SyntaxExpr GhcTc
fail_op
| MatchResult -> Bool
matchCanFail MatchResult
match
= do { DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
; CoreExpr
fail_msg <- String -> DsM CoreExpr
forall (m :: * -> *). MonadThings m => String -> m CoreExpr
mkStringExpr (DynFlags -> e -> String
forall e. HasSrcSpan e => DynFlags -> e -> String
mk_fail_msg DynFlags
dflags e
pat)
; CoreExpr
fail_expr <- SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
fail_op [CoreExpr
fail_msg]
; MatchResult -> CoreExpr -> DsM CoreExpr
extractMatchResult MatchResult
match CoreExpr
fail_expr }
| Bool
otherwise
= MatchResult -> CoreExpr -> DsM CoreExpr
extractMatchResult MatchResult
match (String -> CoreExpr
forall a. HasCallStack => String -> a
error "It can't fail")
mk_fail_msg :: HasSrcSpan e => DynFlags -> e -> String
mk_fail_msg :: DynFlags -> e -> String
mk_fail_msg dflags :: DynFlags
dflags pat :: e
pat
= "Pattern match failure in monad comprehension at " String -> String -> String
forall a. [a] -> [a] -> [a]
++
DynFlags -> SrcSpan -> String
forall a. Outputable a => DynFlags -> a -> String
showPpr DynFlags
dflags (e -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
getLoc e
pat)
dsInnerMonadComp :: [ExprLStmt GhcTc]
-> [Id]
-> SyntaxExpr GhcTc
-> DsM CoreExpr
dsInnerMonadComp :: [ExprLStmt GhcTc] -> [Id] -> SyntaxExpr GhcTc -> DsM CoreExpr
dsInnerMonadComp stmts :: [ExprLStmt GhcTc]
stmts bndrs :: [Id]
bndrs ret_op :: SyntaxExpr GhcTc
ret_op
= [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts ([ExprLStmt GhcTc]
stmts [ExprLStmt GhcTc] -> [ExprLStmt GhcTc] -> [ExprLStmt GhcTc]
forall a. [a] -> [a] -> [a]
++
[SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XLastStmt GhcTc GhcTc (LHsExpr GhcTc)
-> LHsExpr GhcTc
-> Bool
-> SyntaxExpr GhcTc
-> StmtLR GhcTc GhcTc (LHsExpr GhcTc)
forall idL idR body.
XLastStmt idL idR body
-> body -> Bool -> SyntaxExpr idR -> StmtLR idL idR body
LastStmt XLastStmt GhcTc GhcTc (LHsExpr GhcTc)
NoExt
noExt ([Id] -> LHsExpr GhcTc
mkBigLHsVarTupId [Id]
bndrs) Bool
False SyntaxExpr GhcTc
ret_op)])
mkMcUnzipM :: TransForm
-> HsExpr GhcTcId
-> Id
-> [Type]
-> DsM CoreExpr
mkMcUnzipM :: TransForm -> HsExpr GhcTc -> Id -> [Type] -> DsM CoreExpr
mkMcUnzipM ThenForm _ ys :: Id
ys _
= CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
ys)
mkMcUnzipM _ fmap_op :: HsExpr GhcTc
fmap_op ys :: Id
ys elt_tys :: [Type]
elt_tys
= do { CoreExpr
fmap_op' <- HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
fmap_op
; [Id]
xs <- (Type -> DsM Id) -> [Type] -> DsM [Id]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> DsM Id
newSysLocalDs [Type]
elt_tys
; let tup_ty :: Type
tup_ty = [Type] -> Type
mkBigCoreTupTy [Type]
elt_tys
; Id
tup_xs <- Type -> DsM Id
newSysLocalDs Type
tup_ty
; let mk_elt :: Arity -> CoreExpr
mk_elt i :: Arity
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] -> Arity -> Type
forall a. Outputable a => [a] -> Arity -> a
getNth [Type]
elt_tys Arity
i)
, Arity -> CoreExpr
mk_sel Arity
i, Id -> CoreExpr
forall b. Id -> Expr b
Var Id
ys]
mk_sel :: Arity -> CoreExpr
mk_sel n :: Arity
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
mkTupleSelector [Id]
xs ([Id] -> Arity -> Id
forall a. Outputable a => [a] -> Arity -> a
getNth [Id]
xs Arity
n) Id
tup_xs (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
tup_xs)
; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return ([CoreExpr] -> CoreExpr
mkBigCoreTup ((Arity -> CoreExpr) -> [Arity] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map Arity -> CoreExpr
mk_elt [0..[Type] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [Type]
elt_tys Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
- 1])) }