{-# LANGUAGE TypeFamilies   #-}

{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998


Desugaring list comprehensions, monad comprehensions and array comprehensions
-}

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          -- the monadery used in the desugarer
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.Utils.Panic.Plain
import GHC.Tc.Utils.TcType
import GHC.Data.List.SetOps( getNth )

{-
List comprehensions may be desugared in one of two ways: ``ordinary''
(as you would expect if you read SLPJ's book) and ``with foldr/build
turned on'' (if you read Gill {\em et al.}'s paper on the subject).

There will be at least one ``qualifier'' in the input.
-}

dsListComp :: [ExprLStmt GhcTc]
           -> Type              -- Type of entire list
           -> DsM CoreExpr
dsListComp :: [ExprLStmt GhcTc] -> Type -> DsM CoreExpr
dsListComp [ExprLStmt GhcTc]
lquals Type
res_ty = do
    DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
    let quals :: [Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
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 :: Type
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 Bool -> Bool
not (GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_EnableRewriteRules DynFlags
dflags) Bool -> Bool -> Bool
|| GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_IgnoreInterfacePragmas DynFlags
dflags
       -- Either rules are switched off, or we are ignoring what there are;
       -- Either way foldr/build won't happen, so use the more efficient
       -- Wadler-style desugaring
       Bool -> Bool -> Bool
|| [Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))] -> Bool
forall {idL} {idR} {body}. [StmtLR idL idR body] -> Bool
isParallelComp [Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
quals
       -- Foldr-style desugaring can't handle parallel list comprehensions
        then [ExprStmt GhcTc] -> CoreExpr -> DsM CoreExpr
deListComp [ExprStmt GhcTc]
[Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr 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 (\(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)
             -- Foldr/build should be enabled, so desugar
             -- into foldrs and builds

  where
    -- We must test for ParStmt anywhere, not just at the head, because an extension
    -- to list comprehensions would be to add brackets to specify the associativity
    -- of qualifier lists. This is really easy to do by adding extra ParStmts into the
    -- mix of possibly a single element in length, so we do this to leave the possibility open
    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


-- This function lets you desugar a inner list comprehension and a list of the binders
-- of that comprehension that we need in the outer comprehension into such an expression
-- and the type of the elements that it outputs (tuples of binders)
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]
[Id]
bndrs
             list_ty :: Type
list_ty          = Type -> Type
mkListTy Type
bndrs_tuple_type

             -- really use original bndrs below!
       ; CoreExpr
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 a an. a -> LocatedAn an 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

       ; (CoreExpr, Type) -> DsM (CoreExpr, Type)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr
expr, Type
bndrs_tuple_type) }

-- This function factors out commonality between the desugaring strategies for GroupStmt.
-- Given such a statement it gives you back an expression representing how to compute the transformed
-- list and the tuple that you need to bind from that list in order to proceed with your desugaring
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
mkBigCoreTupTy [Type]
to_bndrs_tys

    -- Desugar an inner comprehension which outputs a list of tuples of the "from" binders
    (CoreExpr
expr', 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
NoExtField
noExtField [ExprLStmt GhcTc]
stmts
                                                        [IdP GhcTc]
[Id]
from_bndrs SyntaxExpr GhcTc
forall (p :: Pass). IsPass p => SyntaxExpr (GhcPass p)
noSyntaxExpr)

    -- Work out what arguments should be supplied to that expression: i.e. is an extraction
    -- function required? If so, create that desugared function and add to arguments
    CoreExpr
usingExpr' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
using
    [CoreExpr]
usingArgs' <- case Maybe (LHsExpr GhcTc)
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 { 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 a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [CoreExpr
lam', CoreExpr
expr'] }

    -- Create an unzip function for the appropriate arity and element types and find "map"
    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

    -- Generate the expressions to build the grouped list
    let -- First we apply the grouping function to the inner list
        inner_list_expr' :: CoreExpr
inner_list_expr' = CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
usingExpr' [CoreExpr]
usingArgs'
        -- Then we map our "unzip" across it to turn the lists of tuples into tuples of lists
        -- We make sure we instantiate the type variable "a" to be a list of "from" tuples and
        -- the "b" to be a tuple of "to" lists!
        -- Then finally we bind the unzip function around that expression
        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') ->
                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' ]

    -- Build a pattern that ensures the consumer binds into the NEW binders,
    -- which hold lists rather than single values
    let pat :: LPat GhcTc
pat = [Id] -> LPat GhcTc
mkBigLHsVarPatTupId [Id]
to_bndrs  -- NB: no '!
    (CoreExpr, GenLocated SrcSpanAnnA (Pat GhcTc))
-> IOEnv
     (Env DsGblEnv DsLclEnv)
     (CoreExpr, GenLocated SrcSpanAnnA (Pat GhcTc))
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr
bound_unzipped_inner_list_expr', LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
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"

{-
************************************************************************
*                                                                      *
*           Ordinary desugaring of list comprehensions                 *
*                                                                      *
************************************************************************

Just as in Phil's chapter~7 in SLPJ, using the rules for
optimally-compiled list comprehensions.  This is what Kevin followed
as well, and I quite happily do the same.  The TQ translation scheme
transforms a list of qualifiers (either boolean expressions or
generators) into a single expression which implements the list
comprehension.  Because we are generating 2nd-order polymorphic
lambda-calculus, calls to NIL and CONS must be applied to a type
argument, as well as their usual value arguments.
\begin{verbatim}
TE << [ e | qs ] >>  =  TQ << [ e | qs ] ++ Nil (typeOf e) >>

(Rule C)
TQ << [ e | ] ++ L >> = Cons (typeOf e) TE <<e>> TE <<L>>

(Rule B)
TQ << [ e | b , qs ] ++ L >> =
    if TE << b >> then TQ << [ e | qs ] ++ L >> else TE << L >>

(Rule A')
TQ << [ e | p <- L1, qs ]  ++  L2 >> =
  letrec
    h = \ u1 ->
          case u1 of
            []        ->  TE << L2 >>
            (u2 : u3) ->
                  (( \ TE << p >> -> ( TQ << [e | qs]  ++  (h u3) >> )) u2)
                    [] (h u3)
  in
    h ( TE << L1 >> )

"h", "u1", "u2", and "u3" are new variables.
\end{verbatim}

@deListComp@ is the TQ translation scheme.  Roughly speaking, @dsExpr@
is the TE translation scheme.  Note that we carry around the @L@ list
already desugared.  @dsListComp@ does the top TE rule mentioned above.

To the above, we add an additional rule to deal with parallel list
comprehensions.  The translation goes roughly as follows:
     [ e | p1 <- e11, let v1 = e12, p2 <- e13
         | q1 <- e21, let v2 = e22, q2 <- e23]
     =>
     [ e | ((x1, .., xn), (y1, ..., ym)) <-
               zip [(x1,..,xn) | p1 <- e11, let v1 = e12, p2 <- e13]
                   [(y1,..,ym) | q1 <- e21, let v2 = e22, q2 <- e23]]
where (x1, .., xn) are the variables bound in p1, v1, p2
      (y1, .., ym) are the variables bound in q1, v2, q2

In the translation below, the ParStmt branch translates each parallel branch
into a sub-comprehension, and desugars each independently.  The resulting lists
are fed to a zip function, we create a binding for all the variables bound in all
the comprehensions, and then we hand things off the desugarer for bindings.
The zip function is generated here a) because it's small, and b) because then we
don't have to deal with arbitrary limits on the number of zip functions in the
prelude, nor which library the zip function came from.
The introduced tuples are Boxed, but only because I couldn't get it to work
with the Unboxed variety.
-}

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
  =     -- Figure 7.4, SLPJ, p 135, rule C above
    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 { CoreExpr
core_body <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
body
       ; CoreExpr -> DsM CoreExpr
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> CoreExpr -> CoreExpr -> CoreExpr
mkConsExpr ((() :: Constraint) => CoreExpr -> Type
CoreExpr -> Type
exprType CoreExpr
core_body) CoreExpr
core_body CoreExpr
list) }

        -- Non-last: must be a guard
deListComp (BodyStmt XBodyStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
guard SyntaxExpr GhcTc
_ SyntaxExpr GhcTc
_ : [ExprStmt GhcTc]
quals) CoreExpr
list = do  -- rule B above
    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
    CoreExpr -> DsM CoreExpr
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
mkIfThenElse CoreExpr
core_guard CoreExpr
core_rest CoreExpr
list)

-- [e | let B, qs] = let B in [e | qs]
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 LPat GhcTc
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 -- rule A' above
    CoreExpr
core_list1 <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr 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 <- (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 ([CoreExpr]
exps, [Type]
qual_tys) = [(CoreExpr, Type)] -> ([CoreExpr], [Type])
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

        -- Deal with [e | pat <- zip l1 .. ln] in example above
       ; LPat GhcTc
-> CoreExpr -> [ExprStmt 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))
                    [ExprStmt GhcTc]
quals CoreExpr
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 is the pattern ((x1,..,xn), (y1,..,ym)) in the example above
        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 (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 = (() :: Constraint) => CoreExpr -> Type
CoreExpr -> Type
exprType CoreExpr
core_list1       -- two names, same thing

        -- u1_ty is a [alpha] type, and u2_ty = alpha
    let u2_ty :: Type
u2_ty = LPat GhcTc -> Type
hsLPatType LPat GhcTc
pat

    let res_ty :: Type
res_ty = (() :: Constraint) => CoreExpr -> Type
CoreExpr -> Type
exprType CoreExpr
core_list2
        h_ty :: Type
h_ty   = Type
u1_ty (() :: Constraint) => Type -> Type -> Type
Type -> Type -> Type
`mkVisFunTyMany` Type
res_ty

       -- no representation polymorphism here, as list comprehensions
       -- don't work with RebindableSyntax. NB: These are *not* monad comps.
    [Id
h, Id
u1, Id
u2, Id
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]

    -- the "fail" value ...
    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 <- [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 (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
u2) (HsStmtContext GhcRn -> HsMatchContext GhcRn
forall p. HsStmtContext p -> HsMatchContext p
StmtCtxt (HsDoFlavour -> HsStmtContext GhcRn
forall p. HsDoFlavour -> HsStmtContext p
HsDoStmt HsDoFlavour
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
                   [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]
                        -- Increasing order of tag

    CoreExpr -> DsM CoreExpr
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
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)

{-
************************************************************************
*                                                                      *
*           Foldr/Build desugaring of list comprehensions              *
*                                                                      *
************************************************************************

@dfListComp@ are the rules used with foldr/build turned on:

\begin{verbatim}
TE[ e | ]            c n = c e n
TE[ e | b , q ]      c n = if b then TE[ e | q ] c n else n
TE[ e | p <- l , q ] c n = let
                                f = \ x b -> case x of
                                                  p -> TE[ e | q ] c b
                                                  _ -> b
                           in
                           foldr f n l
\end{verbatim}
-}

dfListComp :: Id -> Id            -- 'c' and 'n'
           -> [ExprStmt GhcTc]    -- the rest of the qual's
           -> 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 { CoreExpr
core_body <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
body
       ; CoreExpr -> DsM CoreExpr
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
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]) }

        -- Non-last: must be a guard
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
    CoreExpr -> DsM CoreExpr
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
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 Id
c_id Id
n_id (LetStmt XLetStmt GhcTc GhcTc (LHsExpr GhcTc)
_ HsLocalBindsLR GhcTc GhcTc
binds : [ExprStmt GhcTc]
quals) = do
    -- new in 1.3, local bindings
    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
    -- Anyway, we bind the newly grouped list via the generic binding function
    Id
-> Id -> (LPat GhcTc, CoreExpr) -> [ExprStmt GhcTc] -> DsM CoreExpr
dfBindComp Id
c_id Id
n_id (LPat GhcTc
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
    -- evaluate the two lists
    CoreExpr
core_list1 <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
list1

    -- Do the rest of the work in the generic binding builder
    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]
_) = 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
_ (ApplicativeStmt {} : [ExprStmt GhcTc]
_) =
  String -> DsM CoreExpr
forall a. HasCallStack => String -> a
panic String
"dfListComp ApplicativeStmt"

dfBindComp :: Id -> Id             -- 'c' and 'n'
           -> (LPat GhcTc, CoreExpr)
           -> [ExprStmt GhcTc]     -- the rest of the qual's
           -> 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
    -- find the required type
    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

    -- create some new local id's
    Id
b <- Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy Type
b_ty
    Id
x <- Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy Type
x_ty

    -- build rest of the comprehension
    CoreExpr
core_rest <- Id -> Id -> [ExprStmt GhcTc] -> DsM CoreExpr
dfListComp Id
c_id Id
b [ExprStmt GhcTc]
quals

    -- build the pattern match
    CoreExpr
core_expr <- CoreExpr
-> HsMatchContext GhcRn
-> LPat GhcTc
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimply (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
x) (HsStmtContext GhcRn -> HsMatchContext GhcRn
forall p. HsStmtContext p -> HsMatchContext p
StmtCtxt (HsDoFlavour -> HsStmtContext GhcRn
forall p. HsDoFlavour -> HsStmtContext p
HsDoStmt HsDoFlavour
ListComp))
                LPat GhcTc
pat CoreExpr
core_rest (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
b)

    -- now build the outermost foldr, and return
    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

{-
************************************************************************
*                                                                      *
\subsection[DsFunGeneration]{Generation of zip/unzip functions for use in desugaring}
*                                                                      *
************************************************************************
-}

mkZipBind :: [Type] -> DsM (Id, CoreExpr)
-- mkZipBind [t1, t2]
-- = (zip, \as1:[t1] as2:[t2]
--         -> case as1 of
--              [] -> []
--              (a1:as'1) -> case as2 of
--                              [] -> []
--                              (a2:as'2) -> (a1, a2) : zip as'1 as'2)]

mkZipBind :: [Type] -> DsM (Id, CoreExpr)
mkZipBind [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)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy)  [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)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy)  [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)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy)  [Type]
elt_list_tys

    Id
zip_fn <- Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy 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 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)

    (Id, CoreExpr) -> DsM (Id, CoreExpr)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
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
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]
                        -- Increasing order of tag


mkUnzipBind :: TransForm -> [Type] -> DsM (Maybe (Id, CoreExpr))
-- mkUnzipBind [t1, t2]
-- = (unzip, \ys :: [(t1, t2)] -> foldr (\ax :: (t1, t2) axs :: ([t1], [t2])
--     -> case ax of
--      (x1, x2) -> case axs of
--                (xs1, xs2) -> (x1 : xs1, x2 : xs2))
--      ([], [])
--      ys)
--
-- We use foldr here in all cases, even if rules are turned off, because we may as well!
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    -- No unzipping for ThenForm
mkUnzipBind TransForm
_ [Type]
elt_tys
  = do { Id
ax  <- Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy Type
elt_tuple_ty
       ; Id
axs <- Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy Type
elt_list_tuple_ty
       ; Id
ys  <- Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy 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)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy) [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)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy) [Type]
elt_list_tys

       ; Id
unzip_fn <- Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy Type
unzip_fn_ty

       ; [UniqSupply
us1, 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)
forall (m :: * -> *) a. Monad m => [m a] -> m [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 -> CoreExpr -> CoreExpr
mkBigTupleCase UniqSupply
us1 [Id]
xss CoreExpr
tupled_concat_expression (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
axs)
             folder_body_outer_case :: CoreExpr
folder_body_outer_case = UniqSupply -> [Id] -> CoreExpr -> CoreExpr -> CoreExpr
mkBigTupleCase UniqSupply
us2 [Id]
xs CoreExpr
folder_body_inner_case (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 a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
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 (() :: Constraint) => 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

-- Translation for monad comprehensions

-- Entry point for monad comprehension desugaring
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. SrcSpanAnn' 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 { CoreExpr
body' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
body
       ; SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
ret_op [CoreExpr
body'] }

--   [ .. | let binds, stmts ]
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 }

--   [ .. | a <- m, stmts ]
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)
XBindStmtTc
xbs) (XBindStmtTc -> Maybe (SyntaxExpr GhcTc)
xbstc_failOp XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
XBindStmtTc
xbs) (XBindStmtTc -> Type
xbstc_boundResultType XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
XBindStmtTc
xbs) [ExprLStmt GhcTc]
stmts }

-- Apply `guard` to the `exp` expression
--
--   [ .. | exp, 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] }

-- Group statements desugar like this:
--
--   [| (q, then group by e using f); rest |]
--   --->  f {qt} (\qv -> e) [| q; return qv |] >>= \ n_tup ->
--         case unzip n_tup of qv' -> [| rest |]
--
-- where   variables (v1:t1, ..., vk:tk) are bound by q
--         qv = (v1, ..., vk)
--         qt = (t1, ..., tk)
--         (>>=) :: m2 a -> (a -> m3 b) -> m3 b
--         f :: forall a. (a -> t) -> m1 a -> m2 (n a)
--         n_tup :: n qt
--         unzip :: n qt -> (n t1, ..., n tk)    (needs Functor n)

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'  -- n (a,b,c)
                    , 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     -- Types ty


       -- Desugar an inner comprehension which outputs a list of tuples of the "from" binders
       ; CoreExpr
expr' <- [ExprLStmt GhcTc] -> [Id] -> SyntaxExpr GhcTc -> DsM CoreExpr
dsInnerMonadComp [ExprLStmt GhcTc]
stmts [Id]
from_bndrs SyntaxExpr GhcTc
return_op

       -- Work out what arguments should be supplied to that expression: i.e. is an extraction
       -- function required? If so, create that desugared function and add to arguments
       ; CoreExpr
usingExpr' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
using
       ; [CoreExpr]
usingArgs' <- case Maybe (LHsExpr GhcTc)
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 { 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 a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [CoreExpr
lam', CoreExpr
expr'] }

       -- Generate the expressions to build the grouped list
       -- Build a pattern that ensures the consumer binds into the NEW binders,
       -- which hold monads rather than single values
       ; CoreExpr
body        <- [ExprLStmt GhcTc] -> DsM CoreExpr
dsMcStmts [ExprLStmt GhcTc]
stmts_rest
       ; Id
n_tup_var'  <- Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy XTransStmt GhcTc GhcTc (LHsExpr GhcTc)
Type
n_tup_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 -> CoreExpr -> CoreExpr
mkBigTupleCase UniqSupply
us [Id]
to_bndrs CoreExpr
body 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'] }

-- Parallel statements. Use `Control.Monad.Zip.mzip` to zip parallel
-- statements, for example:
--
--   [ body | qs1 | qs2 | qs3 ]
--     ->  [ body | (bndrs1, (bndrs2, bndrs3))
--                     <- [bndrs1 | qs1] `mzip` ([bndrs2 | qs2] `mzip` [bndrs3 | qs3]) ]
--
-- where `mzip` has type
--   mzip :: forall a b. m a -> m b -> m (a,b)
-- NB: we need a polymorphic mzip because we call it several times

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  <- (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   -- Pairs (exp :: m ty, ty)
       ; CoreExpr
mzip_op'    <- HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
mzip_op

       ; let -- The pattern variables
             pats :: [GenLocated SrcSpanAnnA (Pat GhcTc)]
pats = [ [Id] -> LPat GhcTc
mkBigLHsVarPatTupId [IdP GhcTc]
[Id]
bs | ParStmtBlock XParStmtBlock GhcTc GhcTc
_ [ExprLStmt GhcTc]
_ [IdP GhcTc]
bs SyntaxExpr GhcTc
_ <- [ParStmtBlock GhcTc GhcTc]
blocks]
             -- Pattern with tuples of variables
             -- [v1,v2,v3]  =>  (v1, (v2, v3))
             pat :: GenLocated SrcSpanAnnA (Pat GhcTc)
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
             (CoreExpr
rhs, Type
_) = ((CoreExpr, Type) -> (CoreExpr, Type) -> (CoreExpr, Type))
-> [(CoreExpr, Type)] -> (CoreExpr, Type)
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
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]))
                               [(CoreExpr, Type)]
exps_w_tys

       ; LPat GhcTc
-> CoreExpr
-> SyntaxExpr GhcTc
-> Maybe (SyntaxExpr GhcTc)
-> Type
-> [ExprLStmt GhcTc]
-> DsM CoreExpr
dsMcBindStmt LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat CoreExpr
rhs SyntaxExpr GhcTc
bind_op Maybe (SyntaxExpr GhcTc)
Maybe SyntaxExprTc
forall a. Maybe a
Nothing XParStmt GhcTc GhcTc (LHsExpr GhcTc)
Type
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]
[Id]
bndrs SyntaxExpr GhcTc
return_op
            ; (CoreExpr, Type) -> DsM (CoreExpr, Type)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr
exp, [Id] -> Type
mkBigCoreVarTupTy [IdP GhcTc]
[Id]
bndrs) }

dsMcStmt ExprStmt GhcTc
stmt [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 [a,b,c] body)
--       returns the Core term
--  \x. case x of (a,b,c) -> body
matchTuple :: [Id] -> CoreExpr -> DsM CoreExpr
matchTuple [Id]
ids CoreExpr
body
  = do { UniqSupply
us <- IOEnv (Env DsGblEnv DsLclEnv) UniqSupply
forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply
       ; Id
tup_id <- Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy ([Id] -> Type
mkBigCoreVarTupTy [Id]
ids)
       ; CoreExpr -> DsM CoreExpr
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
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 -> CoreExpr -> CoreExpr
mkBigTupleCase UniqSupply
us [Id]
ids CoreExpr
body (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
tup_id)) }

-- general `rhs' >>= \pat -> stmts` desugaring where `rhs'` is already a
-- desugared `CoreExpr`
dsMcBindStmt :: LPat GhcTc
             -> CoreExpr        -- ^ the desugared rhs of the bind statement
             -> SyntaxExpr GhcTc
             -> Maybe (SyntaxExpr GhcTc)
             -> Type            -- ^ S in (>>=) :: Q -> (R -> S) -> T
             -> [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
ManyTy LPat GhcTc
pat
        ; MatchResult CoreExpr
match <- Id
-> Maybe CoreExpr
-> HsMatchContext GhcRn
-> LPat GhcTc
-> Type
-> MatchResult CoreExpr
-> DsM (MatchResult CoreExpr)
matchSinglePatVar Id
var Maybe CoreExpr
forall a. Maybe a
Nothing (HsStmtContext GhcRn -> HsMatchContext GhcRn
forall p. HsStmtContext p -> HsMatchContext p
StmtCtxt (HsDoFlavour -> HsStmtContext GhcRn
forall p. HsDoFlavour -> HsStmtContext p
HsDoStmt (Maybe ModuleName -> HsDoFlavour
DoExpr Maybe ModuleName
forall a. Maybe a
Nothing))) LPat GhcTc
pat
                                  Type
res1_ty (CoreExpr -> MatchResult CoreExpr
cantFailMatchResult CoreExpr
body)
        ; CoreExpr
match_code <- HsDoFlavour
-> LPat GhcTc
-> MatchResult CoreExpr
-> Maybe (SyntaxExpr GhcTc)
-> DsM CoreExpr
dsHandleMonadicFailure HsDoFlavour
MonadComp LPat GhcTc
pat MatchResult CoreExpr
match Maybe (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] }

-- Desugar nested monad comprehensions, for example in `then..` constructs
--    dsInnerMonadComp quals [a,b,c] ret_op
-- returns the desugaring of
--       [ (a,b,c) | quals ]

dsInnerMonadComp :: [ExprLStmt GhcTc]
                 -> [Id]               -- Return a tuple of these variables
                 -> SyntaxExpr GhcTc   -- The monomorphic "return" operator
                 -> 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 a an. a -> LocatedAn an 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)])


-- The `unzip` function for `GroupStmt` in a monad comprehensions
--
--   unzip :: m (a,b,..) -> (m a,m b,..)
--   unzip m_tuple = ( liftM selN1 m_tuple
--                   , liftM selN2 m_tuple
--                   , .. )
--
--   mkMcUnzipM fmap ys [t1, t2]
--     = ( fmap (selN1 :: (t1, t2) -> t1) ys
--       , fmap (selN2 :: (t1, t2) -> t2) ys )

mkMcUnzipM :: TransForm
           -> HsExpr GhcTc      -- fmap
           -> Id                -- Of type n (a,b,c)
           -> [Type]            -- [a,b,c]   (not representation-polymorphic)
           -> DsM CoreExpr      -- Of type (n a, n b, n c)
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) -- No unzipping to do

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       <- (Type -> DsM Id) -> [Type] -> DsM [Id]
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 (Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy) [Type]
elt_tys
       ; let tup_ty :: Type
tup_ty = [Type] -> Type
mkBigCoreTupTy [Type]
elt_tys
       ; Id
tup_xs   <- Type -> Type -> DsM Id
newSysLocalDs Type
ManyTy Type
tup_ty

       ; let mk_elt :: Int -> CoreExpr
mk_elt Int
i = CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
fmap_op'  -- fmap :: forall a b. (a -> b) -> n a -> n b
                           [ 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 -> CoreExpr
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)

       ; CoreExpr -> DsM CoreExpr
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([CoreExpr] -> CoreExpr
mkBigCoreTup ((Int -> CoreExpr) -> [Int] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map Int -> CoreExpr
mk_elt [Int
0..[Type] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Type]
elt_tys Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1])) }