Preliminary monad-comprehension patch (Trac #4370)
[ghc-hetmet.git] / compiler / deSugar / DsListComp.lhs
index 7409101..7fa7848 100644 (file)
@@ -3,35 +3,42 @@
 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
 %
 
-Desugaring list comprehensions and array comprehensions
+Desugaring list comprehensions, monad comprehensions and array comprehensions
 
 \begin{code}
-module DsListComp ( dsListComp, dsPArrComp ) where
+{-# LANGUAGE NamedFieldPuns #-}
+{-# OPTIONS -fno-warn-incomplete-patterns #-}
+-- The above warning supression flag is a temporary kludge.
+-- While working on this module you are encouraged to remove it and fix
+-- any warnings in the module. See
+--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
+-- for details
+
+module DsListComp ( dsListComp, dsPArrComp, dsMonadComp ) where
 
 #include "HsVersions.h"
 
-import {-# SOURCE #-} DsExpr ( dsLExpr, dsLocalBinds )
+import {-# SOURCE #-} DsExpr ( dsExpr, dsLExpr, dsLocalBinds )
 
-import BasicTypes
 import HsSyn
 import TcHsSyn
 import CoreSyn
+import MkCore
 
 import DsMonad         -- the monadery used in the desugarer
 import DsUtils
 
 import DynFlags
-import StaticFlags
 import CoreUtils
-import Var
+import Id
 import Type
-import TysPrim
 import TysWiredIn
 import Match
 import PrelNames
-import PrelInfo
 import SrcLoc
-import Panic
+import Outputable
+import FastString
+import TcType
 \end{code}
 
 List comprehensions may be desugared in one of two ways: ``ordinary''
@@ -45,35 +52,114 @@ dsListComp :: [LStmt Id]
           -> LHsExpr Id
           -> Type              -- Type of list elements
           -> DsM CoreExpr
-dsListComp lquals body elt_ty
-  = getDOptsDs  `thenDs` \dflags ->
-    let
-       quals = map unLoc lquals
-    in
-    if opt_RulesOff || dopt Opt_IgnoreInterfacePragmas 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
-       || isParallelComp quals
-               -- Foldr-style desugaring can't handle
-               -- parallel list comprehensions
-       then deListComp quals body (mkNilExpr elt_ty)
-
-   else                -- Foldr/build should be enabled, so desugar 
-               -- into foldrs and builds
-    newTyVarsDs [alphaTyVar]    `thenDs` \ [n_tyvar] ->
-    let
-       n_ty = mkTyVarTy n_tyvar
-        c_ty = mkFunTys [elt_ty, n_ty] n_ty
-    in
-    newSysLocalsDs [c_ty,n_ty]         `thenDs` \ [c, n] ->
-    dfListComp c n quals body          `thenDs` \ result ->
-    dsLookupGlobalId buildName `thenDs` \ build_id ->
-    returnDs (Var build_id `App` Type elt_ty 
-                          `App` mkLams [n_tyvar, c, n] result)
-
-  where isParallelComp (ParStmt bndrstmtss : _) = True
-       isParallelComp _                        = False
+dsListComp lquals body elt_ty = do 
+    dflags <- getDOptsDs
+    let quals = map unLoc lquals
+    
+    if not (dopt Opt_EnableRewriteRules dflags) || dopt Opt_IgnoreInterfacePragmas 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
+       || isParallelComp quals
+       -- Foldr-style desugaring can't handle parallel list comprehensions
+        then deListComp quals body (mkNilExpr elt_ty)
+        else mkBuildExpr elt_ty (\(c, _) (n, _) -> dfListComp c n quals body) 
+             -- 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 = any isParallelStmt
+  
+    isParallelStmt (ParStmt _ _ _ _) = True
+    isParallelStmt _                 = 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 :: ([LStmt Id], [Id]) -> DsM (CoreExpr, Type)
+dsInnerListComp (stmts, bndrs) = do
+        expr <- dsListComp stmts (mkBigLHsVarTup bndrs) bndrs_tuple_type
+        return (expr, bndrs_tuple_type)
+    where
+        bndrs_types = map idType bndrs
+        bndrs_tuple_type = mkBigCoreTupTy bndrs_types
+        
+        
+-- This function factors out commonality between the desugaring strategies for TransformStmt.
+-- 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
+dsTransformStmt :: Stmt Id -> DsM (CoreExpr, LPat Id)
+dsTransformStmt (TransformStmt stmts binders usingExpr maybeByExpr _ _)
+ = do { (expr, binders_tuple_type) <- dsInnerListComp (stmts, binders)
+      ; usingExpr' <- dsLExpr usingExpr
+    
+      ; using_args <-
+          case maybeByExpr of
+            Nothing -> return [expr]
+            Just byExpr -> do
+                byExpr' <- dsLExpr byExpr
+                
+                us <- newUniqueSupply
+                [tuple_binder] <- newSysLocalsDs [binders_tuple_type]
+                let byExprWrapper = mkTupleCase us binders byExpr' tuple_binder (Var tuple_binder)
+                
+                return [Lam tuple_binder byExprWrapper, expr]
+
+      ; let inner_list_expr = mkApps usingExpr' ((Type binders_tuple_type) : using_args)
+            pat = mkBigLHsVarPatTup binders
+      ; return (inner_list_expr, pat) }
+    
+-- 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
+dsGroupStmt :: Stmt Id -> DsM (CoreExpr, LPat Id)
+dsGroupStmt (GroupStmt stmts binderMap by using _ _ _) = do
+    let (fromBinders, toBinders) = unzip binderMap
+        
+        fromBindersTypes = map idType fromBinders
+        toBindersTypes = map idType toBinders
+        
+        toBindersTupleType = mkBigCoreTupTy toBindersTypes
+    
+    -- Desugar an inner comprehension which outputs a list of tuples of the "from" binders
+    (expr, from_tup_ty) <- dsInnerListComp (stmts, fromBinders)
+    
+    -- 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
+    usingExpr' <- dsLExpr (either id noLoc using)
+    usingArgs <- case by of
+                   Nothing   -> return [expr]
+                  Just by_e -> do { by_e' <- dsLExpr by_e
+                                   ; us <- newUniqueSupply
+                                   ; [from_tup_id] <- newSysLocalsDs [from_tup_ty]
+                                   ; let by_wrap = mkTupleCase us fromBinders by_e' 
+                                                   from_tup_id (Var from_tup_id)
+                                   ; return [Lam from_tup_id by_wrap, expr] }
+    
+    -- Create an unzip function for the appropriate arity and element types and find "map"
+    (unzip_fn, unzip_rhs) <- mkUnzipBind fromBindersTypes
+    map_id <- dsLookupGlobalId mapName
+
+    -- Generate the expressions to build the grouped list
+    let -- First we apply the grouping function to the inner list
+        inner_list_expr = mkApps usingExpr' ((Type from_tup_ty) : 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!
+        unzipped_inner_list_expr = mkApps (Var map_id) 
+            [Type (mkListTy from_tup_ty), Type toBindersTupleType, Var unzip_fn, inner_list_expr]
+        -- Then finally we bind the unzip function around that expression
+        bound_unzipped_inner_list_expr = Let (Rec [(unzip_fn, unzip_rhs)]) unzipped_inner_list_expr
+    
+    -- Build a pattern that ensures the consumer binds into the NEW binders, which hold lists rather than single values
+    let pat = mkBigLHsVarPatTup toBinders
+    return (bound_unzipped_inner_list_expr, pat)
+    
 \end{code}
 
 %************************************************************************
@@ -141,11 +227,15 @@ The introduced tuples are Boxed, but only because I couldn't get it to work
 with the Unboxed variety.
 
 \begin{code}
+
 deListComp :: [Stmt Id] -> LHsExpr Id -> CoreExpr -> DsM CoreExpr
 
-deListComp (ParStmt stmtss_w_bndrs : quals) body list
-  = mappM do_list_comp stmtss_w_bndrs  `thenDs` \ exps ->
-    mkZipBind qual_tys                 `thenDs` \ (zip_fn, zip_rhs) ->
+deListComp (ParStmt stmtss_w_bndrs _ _ _ : quals) body list
+  = do
+    exps_and_qual_tys <- mapM dsInnerListComp stmtss_w_bndrs
+    let (exps, qual_tys) = unzip exps_and_qual_tys
+    
+    (zip_fn, zip_rhs) <- mkZipBind qual_tys
 
        -- Deal with [e | pat <- zip l1 .. ln] in example above
     deBindComp pat (Let (Rec [(zip_fn, zip_rhs)]) (mkApps (Var zip_fn) exps)) 
@@ -155,116 +245,76 @@ deListComp (ParStmt stmtss_w_bndrs : quals) body list
        bndrs_s = map snd stmtss_w_bndrs
 
        -- pat is the pattern ((x1,..,xn), (y1,..,ym)) in the example above
-       pat      = mkTuplePat pats
-       pats     = map mk_hs_tuple_pat bndrs_s
-
-       -- Types of (x1,..,xn), (y1,..,yn) etc
-       qual_tys = map mk_bndrs_tys bndrs_s
-
-       do_list_comp (stmts, bndrs)
-         = dsListComp stmts (mk_hs_tuple_expr bndrs)
-                      (mk_bndrs_tys bndrs)
-
-       mk_bndrs_tys bndrs = mkCoreTupTy (map idType bndrs)
+       pat  = mkBigLHsPatTup pats
+       pats = map mkBigLHsVarPatTup bndrs_s
 
        -- Last: the one to return
-deListComp [] body list                -- Figure 7.4, SLPJ, p 135, rule C above
-  = dsLExpr body               `thenDs` \ core_body ->
-    returnDs (mkConsExpr (exprType core_body) core_body list)
+deListComp [] body list = do    -- Figure 7.4, SLPJ, p 135, rule C above
+    core_body <- dsLExpr body
+    return (mkConsExpr (exprType core_body) core_body list)
 
        -- Non-last: must be a guard
-deListComp (ExprStmt guard _ _ : quals) body list      -- rule B above
-  = dsLExpr guard                      `thenDs` \ core_guard ->
-    deListComp quals body list `thenDs` \ core_rest ->
-    returnDs (mkIfThenElse core_guard core_rest list)
+deListComp (ExprStmt guard _ _ _ : quals) body list = do  -- rule B above
+    core_guard <- dsLExpr guard
+    core_rest <- deListComp quals body list
+    return (mkIfThenElse core_guard core_rest list)
 
 -- [e | let B, qs] = let B in [e | qs]
-deListComp (LetStmt binds : quals) body list
-  = deListComp quals body list `thenDs` \ core_rest ->
+deListComp (LetStmt binds : quals) body list = do
+    core_rest <- deListComp quals body list
     dsLocalBinds binds core_rest
 
-deListComp (BindStmt pat list1 _ _ : quals) body core_list2 -- rule A' above
-  = dsLExpr list1                  `thenDs` \ core_list1 ->
+deListComp (stmt@(TransformStmt {}) : quals) body list = do
+    (inner_list_expr, pat) <- dsTransformStmt stmt
+    deBindComp pat inner_list_expr quals body list
+
+deListComp (stmt@(GroupStmt {}) : quals) body list = do
+    (inner_list_expr, pat) <- dsGroupStmt stmt
+    deBindComp pat inner_list_expr quals body list
+
+deListComp (BindStmt pat list1 _ _ : quals) body core_list2 = do -- rule A' above
+    core_list1 <- dsLExpr list1
     deBindComp pat core_list1 quals body core_list2
 \end{code}
 
 
 \begin{code}
-deBindComp pat core_list1 quals body core_list2
-  = let
-       u3_ty@u1_ty = exprType core_list1       -- two names, same thing
+deBindComp :: OutPat Id
+           -> CoreExpr
+           -> [Stmt Id]
+           -> LHsExpr Id
+           -> CoreExpr
+           -> DsM (Expr Id)
+deBindComp pat core_list1 quals body core_list2 = do
+    let
+        u3_ty@u1_ty = exprType core_list1      -- two names, same thing
 
-       -- u1_ty is a [alpha] type, and u2_ty = alpha
-       u2_ty = hsLPatType pat
+        -- u1_ty is a [alpha] type, and u2_ty = alpha
+        u2_ty = hsLPatType pat
 
-       res_ty = exprType core_list2
-       h_ty   = u1_ty `mkFunTy` res_ty
-    in
-    newSysLocalsDs [h_ty, u1_ty, u2_ty, u3_ty] `thenDs` \ [h, u1, u2, u3] ->
+        res_ty = exprType core_list2
+        h_ty   = u1_ty `mkFunTy` res_ty
+        
+    [h, u1, u2, u3] <- newSysLocalsDs [h_ty, u1_ty, u2_ty, u3_ty]
 
     -- the "fail" value ...
     let
-       core_fail   = App (Var h) (Var u3)
-       letrec_body = App (Var h) core_list1
-    in
-    deListComp quals body core_fail            `thenDs` \ rest_expr ->
-    matchSimply (Var u2) (StmtCtxt ListComp) pat
-               rest_expr core_fail             `thenDs` \ core_match ->
+        core_fail   = App (Var h) (Var u3)
+        letrec_body = App (Var h) core_list1
+        
+    rest_expr <- deListComp quals body core_fail
+    core_match <- matchSimply (Var u2) (StmtCtxt ListComp) pat rest_expr core_fail     
+    
     let
-       rhs = Lam u1 $
+        rhs = Lam u1 $
              Case (Var u1) u1 res_ty
                   [(DataAlt nilDataCon,  [],       core_list2),
                    (DataAlt consDataCon, [u2, u3], core_match)]
                        -- Increasing order of tag
-    in
-    returnDs (Let (Rec [(h, rhs)]) letrec_body)
+            
+    return (Let (Rec [(h, rhs)]) letrec_body)
 \end{code}
 
-
-\begin{code}
-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) -> (a2,a2) : zip as'1 as'2)]
-
-mkZipBind elt_tys 
-  = mappM newSysLocalDs  list_tys      `thenDs` \ ass ->
-    mappM newSysLocalDs  elt_tys       `thenDs` \ as' ->
-    mappM newSysLocalDs  list_tys      `thenDs` \ as's ->
-    newSysLocalDs zip_fn_ty            `thenDs` \ zip_fn ->
-    let 
-       inner_rhs = mkConsExpr ret_elt_ty 
-                       (mkCoreTup (map Var as'))
-                       (mkVarApps (Var zip_fn) as's)
-       zip_body  = foldr mk_case inner_rhs (zip3 ass as' as's)
-    in
-    returnDs (zip_fn, mkLams ass zip_body)
-  where
-    list_tys    = map mkListTy elt_tys
-    ret_elt_ty  = mkCoreTupTy elt_tys
-    list_ret_ty = mkListTy ret_elt_ty
-    zip_fn_ty   = mkFunTys list_tys list_ret_ty
-
-    mk_case (as, a', as') rest
-         = Case (Var as) as list_ret_ty
-                 [(DataAlt nilDataCon,  [],        mkNilExpr ret_elt_ty),
-                  (DataAlt consDataCon, [a', as'], rest)]
-                       -- Increasing order of tag
--- Helper functions that makes an HsTuple only for non-1-sized tuples
-mk_hs_tuple_expr :: [Id] -> LHsExpr Id
-mk_hs_tuple_expr []   = nlHsVar unitDataConId
-mk_hs_tuple_expr [id] = nlHsVar id
-mk_hs_tuple_expr ids  = noLoc $ ExplicitTuple [ nlHsVar i | i <- ids ] Boxed
-
-mk_hs_tuple_pat :: [Id] -> LPat Id
-mk_hs_tuple_pat bs  = mkTuplePat (map nlVarPat bs)
-\end{code}
-
-
 %************************************************************************
 %*                                                                     *
 \subsection[DsListComp-foldr-build]{Foldr/Build desugaring of list comprehensions}
@@ -285,55 +335,156 @@ TE[ e | p <- l , q ] c n = let
 \end{verbatim}
 
 \begin{code}
-dfListComp :: Id -> Id                 -- 'c' and 'n'
-          -> [Stmt Id]         -- the rest of the qual's
-          -> LHsExpr Id
-          -> DsM CoreExpr
+dfListComp :: Id -> Id -- 'c' and 'n'
+        -> [Stmt Id]   -- the rest of the qual's
+        -> LHsExpr Id
+        -> DsM CoreExpr
 
        -- Last: the one to return
-dfListComp c_id n_id [] body
-  = dsLExpr body               `thenDs` \ core_body ->
-    returnDs (mkApps (Var c_id) [core_body, Var n_id])
+dfListComp c_id n_id [] body = do
+    core_body <- dsLExpr body
+    return (mkApps (Var c_id) [core_body, Var n_id])
 
        -- Non-last: must be a guard
-dfListComp c_id n_id (ExprStmt guard _ _  : quals) body
-  = dsLExpr guard                              `thenDs` \ core_guard ->
-    dfListComp c_id n_id quals body    `thenDs` \ core_rest ->
-    returnDs (mkIfThenElse core_guard core_rest (Var n_id))
-
-dfListComp c_id n_id (LetStmt binds : quals) body
-  -- new in 1.3, local bindings
-  = dfListComp c_id n_id quals body    `thenDs` \ core_rest ->
+dfListComp c_id n_id (ExprStmt guard _ _ _  : quals) body = do
+    core_guard <- dsLExpr guard
+    core_rest <- dfListComp c_id n_id quals body
+    return (mkIfThenElse core_guard core_rest (Var n_id))
+
+dfListComp c_id n_id (LetStmt binds : quals) body = do
+    -- new in 1.3, local bindings
+    core_rest <- dfListComp c_id n_id quals body
     dsLocalBinds binds core_rest
 
-dfListComp c_id n_id (BindStmt pat list1 _ _ : quals) body
+dfListComp c_id n_id (stmt@(TransformStmt {}) : quals) body = do
+    (inner_list_expr, pat) <- dsTransformStmt stmt
+    -- Anyway, we bind the newly transformed list via the generic binding function
+    dfBindComp c_id n_id (pat, inner_list_expr) quals body
+
+dfListComp c_id n_id (stmt@(GroupStmt {}) : quals) body = do
+    (inner_list_expr, pat) <- dsGroupStmt stmt
+    -- Anyway, we bind the newly grouped list via the generic binding function
+    dfBindComp c_id n_id (pat, inner_list_expr) quals body
+    
+dfListComp c_id n_id (BindStmt pat list1 _ _ : quals) body = do
     -- evaluate the two lists
-  = dsLExpr list1                      `thenDs` \ core_list1 ->
-
+    core_list1 <- dsLExpr list1
+    
+    -- Do the rest of the work in the generic binding builder
+    dfBindComp c_id n_id (pat, core_list1) quals body
+               
+dfBindComp :: Id -> Id         -- 'c' and 'n'
+       -> (LPat Id, CoreExpr)
+          -> [Stmt Id]                 -- the rest of the qual's
+          -> LHsExpr Id
+          -> DsM CoreExpr
+dfBindComp c_id n_id (pat, core_list1) quals body = do
     -- find the required type
     let x_ty   = hsLPatType pat
-       b_ty   = idType n_id
-    in
+        b_ty   = idType n_id
 
     -- create some new local id's
-    newSysLocalsDs [b_ty,x_ty]                 `thenDs` \ [b,x] ->
+    [b, x] <- newSysLocalsDs [b_ty, x_ty]
 
     -- build rest of the comprehesion
-    dfListComp c_id b quals body               `thenDs` \ core_rest ->
+    core_rest <- dfListComp c_id b quals body
 
     -- build the pattern match
-    matchSimply (Var x) (StmtCtxt ListComp)
-               pat core_rest (Var b)           `thenDs` \ core_expr ->
+    core_expr <- matchSimply (Var x) (StmtCtxt ListComp)
+               pat core_rest (Var b)
 
     -- now build the outermost foldr, and return
-    dsLookupGlobalId foldrName         `thenDs` \ foldr_id ->
-    returnDs (
-      Var foldr_id `App` Type x_ty 
-                  `App` Type b_ty
-                  `App` mkLams [x, b] core_expr
-                  `App` Var n_id
-                  `App` core_list1
-    )
+    mkFoldrExpr x_ty b_ty (mkLams [x, b] core_expr) (Var n_id) core_list1
+\end{code}
+
+%************************************************************************
+%*                                                                     *
+\subsection[DsFunGeneration]{Generation of zip/unzip functions for use in desugaring}
+%*                                                                     *
+%************************************************************************
+
+\begin{code}
+
+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 elt_tys = do
+    ass  <- mapM newSysLocalDs  elt_list_tys
+    as'  <- mapM newSysLocalDs  elt_tys
+    as's <- mapM newSysLocalDs  elt_list_tys
+    
+    zip_fn <- newSysLocalDs zip_fn_ty
+    
+    let inner_rhs = mkConsExpr elt_tuple_ty 
+                       (mkBigCoreVarTup as')
+                       (mkVarApps (Var zip_fn) as's)
+        zip_body  = foldr mk_case inner_rhs (zip3 ass as' as's)
+    
+    return (zip_fn, mkLams ass zip_body)
+  where
+    elt_list_tys      = map mkListTy elt_tys
+    elt_tuple_ty      = mkBigCoreTupTy elt_tys
+    elt_tuple_list_ty = mkListTy elt_tuple_ty
+    
+    zip_fn_ty         = mkFunTys elt_list_tys elt_tuple_list_ty
+
+    mk_case (as, a', as') rest
+         = Case (Var as) as elt_tuple_list_ty
+                 [(DataAlt nilDataCon,  [],        mkNilExpr elt_tuple_ty),
+                  (DataAlt consDataCon, [a', as'], rest)]
+                       -- Increasing order of tag
+            
+            
+mkUnzipBind :: [Type] -> DsM (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 elt_tys = do
+    ax  <- newSysLocalDs elt_tuple_ty
+    axs <- newSysLocalDs elt_list_tuple_ty
+    ys  <- newSysLocalDs elt_tuple_list_ty
+    xs  <- mapM newSysLocalDs elt_tys
+    xss <- mapM newSysLocalDs elt_list_tys
+    
+    unzip_fn <- newSysLocalDs unzip_fn_ty
+
+    [us1, us2] <- sequence [newUniqueSupply, newUniqueSupply]
+
+    let nil_tuple = mkBigCoreTup (map mkNilExpr elt_tys)
+        
+        concat_expressions = map mkConcatExpression (zip3 elt_tys (map Var xs) (map Var xss))
+        tupled_concat_expression = mkBigCoreTup concat_expressions
+        
+        folder_body_inner_case = mkTupleCase us1 xss tupled_concat_expression axs (Var axs)
+        folder_body_outer_case = mkTupleCase us2 xs folder_body_inner_case ax (Var ax)
+        folder_body = mkLams [ax, axs] folder_body_outer_case
+        
+    unzip_body <- mkFoldrExpr elt_tuple_ty elt_list_tuple_ty folder_body nil_tuple (Var ys)
+    return (unzip_fn, mkLams [ys] unzip_body)
+  where
+    elt_tuple_ty       = mkBigCoreTupTy elt_tys
+    elt_tuple_list_ty  = mkListTy elt_tuple_ty
+    elt_list_tys       = map mkListTy elt_tys
+    elt_list_tuple_ty  = mkBigCoreTupTy elt_list_tys
+    
+    unzip_fn_ty        = elt_tuple_list_ty `mkFunTy` elt_list_tuple_ty
+            
+    mkConcatExpression (list_element_ty, head, tail) = mkConsExpr list_element_ty head tail
+            
+            
+
 \end{code}
 
 %************************************************************************
@@ -348,17 +499,40 @@ dfListComp c_id n_id (BindStmt pat list1 _ _ : quals) body
 --
 --   [:e | qss:] = <<[:e | qss:]>> () [:():]
 --
-dsPArrComp      :: [Stmt Id] 
-               -> LHsExpr Id
-               -> Type             -- Don't use; called with `undefined' below
-               -> DsM CoreExpr
-dsPArrComp qs body _  =
-  dsLookupGlobalId replicatePName                        `thenDs` \repP ->
-  let unitArray = mkApps (Var repP) [Type unitTy, 
-                                    mkIntExpr 1, 
-                                    mkCoreTup []]
-  in
-  dePArrComp qs body (mkTuplePat []) unitArray
+dsPArrComp :: [Stmt Id] 
+            -> LHsExpr Id
+            -> Type                -- Don't use; called with `undefined' below
+            -> DsM CoreExpr
+dsPArrComp [ParStmt qss _ _ _] body _  =  -- parallel comprehension
+  dePArrParComp qss body
+
+-- Special case for simple generators:
+--
+--  <<[:e' | p <- e, qs:]>> = <<[: e' | qs :]>> p e
+--
+-- if matching again p cannot fail, or else
+--
+--  <<[:e' | p <- e, qs:]>> = 
+--    <<[:e' | qs:]>> p (filterP (\x -> case x of {p -> True; _ -> False}) e)
+--
+dsPArrComp (BindStmt p e _ _ : qs) body _ = do
+    filterP <- dsLookupDPHId filterPName
+    ce <- dsLExpr e
+    let ety'ce  = parrElemType ce
+        false   = Var falseDataConId
+        true    = Var trueDataConId
+    v <- newSysLocalDs ety'ce
+    pred <- matchSimply (Var v) (StmtCtxt PArrComp) p true false
+    let gen | isIrrefutableHsPat p = ce
+            | otherwise            = mkApps (Var filterP) [Type ety'ce, mkLams [v] pred, ce]
+    dePArrComp qs body p gen
+
+dsPArrComp qs            body _  = do -- no ParStmt in `qs'
+    sglP <- dsLookupDPHId singletonPName
+    let unitArray = mkApps (Var sglP) [Type unitTy, mkCoreTup []]
+    dePArrComp qs body (noLoc $ WildPat unitTy) unitArray
+
+
 
 -- the work horse
 --
@@ -370,125 +544,135 @@ dePArrComp :: [Stmt Id]
 --
 --  <<[:e' | :]>> pa ea = mapP (\pa -> e') ea
 --
-dePArrComp [] e' pa cea =
-  dsLookupGlobalId mapPName                              `thenDs` \mapP    ->
-  let ty = parrElemType cea
-  in
-  deLambda ty pa e'                                      `thenDs` \(clam, 
-                                                                    ty'e') ->
-  returnDs $ mkApps (Var mapP) [Type ty, Type ty'e', clam, cea]
+dePArrComp [] e' pa cea = do
+    mapP <- dsLookupDPHId mapPName
+    let ty = parrElemType cea
+    (clam, ty'e') <- deLambda ty pa e'
+    return $ mkApps (Var mapP) [Type ty, Type ty'e', clam, cea]
 --
 --  <<[:e' | b, qs:]>> pa ea = <<[:e' | qs:]>> pa (filterP (\pa -> b) ea)
 --
-dePArrComp (ExprStmt b _ _ : qs) body pa cea =
-  dsLookupGlobalId filterPName                   `thenDs` \filterP  ->
-  let ty = parrElemType cea
-  in
-  deLambda ty pa b                               `thenDs` \(clam,_) ->
-  dePArrComp qs body pa (mkApps (Var filterP) [Type ty, clam, cea])
+dePArrComp (ExprStmt b _ _ _ : qs) body pa cea = do
+    filterP <- dsLookupDPHId filterPName
+    let ty = parrElemType cea
+    (clam,_) <- deLambda ty pa b
+    dePArrComp qs body pa (mkApps (Var filterP) [Type ty, clam, cea])
+
+--
+--  <<[:e' | p <- e, qs:]>> pa ea =
+--    let ef = \pa -> e
+--    in
+--    <<[:e' | qs:]>> (pa, p) (crossMap ea ef)
+--
+-- if matching again p cannot fail, or else
 --
 --  <<[:e' | p <- e, qs:]>> pa ea = 
---    let ef = filterP (\x -> case x of {p -> True; _ -> False}) e
+--    let ef = \pa -> filterP (\x -> case x of {p -> True; _ -> False}) e
 --    in
---    <<[:e' | qs:]>> (pa, p) (crossP ea ef)
---
-dePArrComp (BindStmt p e _ _ : qs) body pa cea =
-  dsLookupGlobalId filterPName                   `thenDs` \filterP ->
-  dsLookupGlobalId crossPName                    `thenDs` \crossP  ->
-  dsLExpr e                                      `thenDs` \ce      ->
-  let ty'cea = parrElemType cea
-      ty'ce  = parrElemType ce
-      false  = Var falseDataConId
-      true   = Var trueDataConId
-  in
-  newSysLocalDs ty'ce                                    `thenDs` \v       ->
-  matchSimply (Var v) (StmtCtxt PArrComp) p true false    `thenDs` \pred    ->
-  let cef    = mkApps (Var filterP) [Type ty'ce, mkLams [v] pred, ce]
-      ty'cef = ty'ce                           -- filterP preserves the type
-      pa'    = mkTuplePat [pa, p]
-  in
-  dePArrComp qs body pa' (mkApps (Var crossP) [Type ty'cea, Type ty'cef, cea, cef])
+--    <<[:e' | qs:]>> (pa, p) (crossMapP ea ef)
+--
+dePArrComp (BindStmt p e _ _ : qs) body pa cea = do
+    filterP <- dsLookupDPHId filterPName
+    crossMapP <- dsLookupDPHId crossMapPName
+    ce <- dsLExpr e
+    let ety'cea = parrElemType cea
+        ety'ce  = parrElemType ce
+        false   = Var falseDataConId
+        true    = Var trueDataConId
+    v <- newSysLocalDs ety'ce
+    pred <- matchSimply (Var v) (StmtCtxt PArrComp) p true false
+    let cef | isIrrefutableHsPat p = ce
+            | otherwise            = mkApps (Var filterP) [Type ety'ce, mkLams [v] pred, ce]
+    (clam, _) <- mkLambda ety'cea pa cef
+    let ety'cef = ety'ce                   -- filter doesn't change the element type
+        pa'     = mkLHsPatTup [pa, p]
+
+    dePArrComp qs body pa' (mkApps (Var crossMapP) 
+                                 [Type ety'cea, Type ety'cef, cea, clam])
 --
 --  <<[:e' | let ds, qs:]>> pa ea = 
 --    <<[:e' | qs:]>> (pa, (x_1, ..., x_n)) 
---                   (mapP (\v@pa -> (v, let ds in (x_1, ..., x_n))) ea)
+--                   (mapP (\v@pa -> let ds in (v, (x_1, ..., x_n))) ea)
 --  where
 --    {x_1, ..., x_n} = DV (ds)                -- Defined Variables
 --
-dePArrComp (LetStmt ds : qs) body pa cea =
-  dsLookupGlobalId mapPName                              `thenDs` \mapP    ->
-  let xs     = map unLoc (collectLocalBinders ds)
-      ty'cea = parrElemType cea
-  in
-  newSysLocalDs ty'cea                                   `thenDs` \v       ->
-  dsLocalBinds ds (mkCoreTup (map Var xs))               `thenDs` \clet    ->
-  newSysLocalDs (exprType clet)                                  `thenDs` \let'v   ->
-  let projBody = mkDsLet (NonRec let'v clet) $ 
-                mkCoreTup [Var v, Var let'v]
-      errTy    = exprType projBody
-      errMsg   = "DsListComp.dePArrComp: internal error!"
-  in
-  mkErrorAppDs pAT_ERROR_ID errTy errMsg                  `thenDs` \cerr    ->
-  matchSimply (Var v) (StmtCtxt PArrComp) pa projBody cerr`thenDs` \ccase   ->
-  let pa'    = mkTuplePat [pa, mkTuplePat (map nlVarPat xs)]
-      proj   = mkLams [v] ccase
-  in
-  dePArrComp qs body pa' (mkApps (Var mapP) [Type ty'cea, proj, cea])
+dePArrComp (LetStmt ds : qs) body pa cea = do
+    mapP <- dsLookupDPHId mapPName
+    let xs     = collectLocalBinders ds
+        ty'cea = parrElemType cea
+    v <- newSysLocalDs ty'cea
+    clet <- dsLocalBinds ds (mkCoreTup (map Var xs))
+    let'v <- newSysLocalDs (exprType clet)
+    let projBody = mkCoreLet (NonRec let'v clet) $ 
+                   mkCoreTup [Var v, Var let'v]
+        errTy    = exprType projBody
+        errMsg   = ptext (sLit "DsListComp.dePArrComp: internal error!")
+    cerr <- mkErrorAppDs pAT_ERROR_ID errTy errMsg
+    ccase <- matchSimply (Var v) (StmtCtxt PArrComp) pa projBody cerr
+    let pa'    = mkLHsPatTup [pa, mkLHsPatTup (map nlVarPat xs)]
+        proj   = mkLams [v] ccase
+    dePArrComp qs body pa' (mkApps (Var mapP) 
+                                   [Type ty'cea, Type errTy, proj, cea])
+--
+-- The parser guarantees that parallel comprehensions can only appear as
+-- singeltons qualifier lists, which we already special case in the caller.
+-- So, encountering one here is a bug.
 --
+dePArrComp (ParStmt _ _ _ _ : _) _ _ _ = 
+  panic "DsListComp.dePArrComp: malformed comprehension AST"
+
 --  <<[:e' | qs | qss:]>> pa ea = 
 --    <<[:e' | qss:]>> (pa, (x_1, ..., x_n)) 
 --                    (zipP ea <<[:(x_1, ..., x_n) | qs:]>>)
 --    where
 --      {x_1, ..., x_n} = DV (qs)
 --
-dePArrComp (ParStmt qss : qs) body pa cea = 
-  dsLookupGlobalId crossPName                          `thenDs` \crossP  ->
-  deParStmt qss                                                `thenDs` \(pQss, 
-                                                                  ceQss) ->
-  let ty'cea   = parrElemType cea
-      ty'ceQss = parrElemType ceQss
-      pa'      = mkTuplePat [pa, pQss]
-  in
-  dePArrComp qs body pa' (mkApps (Var crossP) [Type ty'cea, Type ty'ceQss, 
-                                              cea, ceQss])
+dePArrParComp :: [([LStmt Id], [Id])] -> LHsExpr Id -> DsM CoreExpr
+dePArrParComp qss body = do
+    (pQss, ceQss) <- deParStmt qss
+    dePArrComp [] body pQss ceQss
   where
     deParStmt []             =
-      -- empty parallel statement lists have not source representation
+      -- empty parallel statement lists have no source representation
       panic "DsListComp.dePArrComp: Empty parallel list comprehension"
-    deParStmt ((qs, xs):qss) =          -- first statement
-      let res_expr = mkExplicitTuple (map nlHsVar xs)
-      in
-      dsPArrComp (map unLoc qs) res_expr undefined       `thenDs` \cqs     ->
-      parStmts qss (mkTuplePat (map nlVarPat xs)) cqs
+    deParStmt ((qs, xs):qss) = do        -- first statement
+      let res_expr = mkLHsVarTuple xs
+      cqs <- dsPArrComp (map unLoc qs) res_expr undefined
+      parStmts qss (mkLHsVarPatTup xs) cqs
     ---
     parStmts []             pa cea = return (pa, cea)
-    parStmts ((qs, xs):qss) pa cea =    -- subsequent statements (zip'ed)
-      dsLookupGlobalId zipPName                                  `thenDs` \zipP    ->
-      let pa'      = mkTuplePat [pa, mkTuplePat (map nlVarPat xs)]
-         ty'cea   = parrElemType cea
-         res_expr = mkExplicitTuple (map nlHsVar xs)
-      in
-      dsPArrComp (map unLoc qs) res_expr undefined       `thenDs` \cqs     ->
+    parStmts ((qs, xs):qss) pa cea = do  -- subsequent statements (zip'ed)
+      zipP <- dsLookupDPHId zipPName
+      let pa'      = mkLHsPatTup [pa, mkLHsVarPatTup xs]
+          ty'cea   = parrElemType cea
+          res_expr = mkLHsVarTuple xs
+      cqs <- dsPArrComp (map unLoc qs) res_expr undefined
       let ty'cqs = parrElemType cqs
-         cea'   = mkApps (Var zipP) [Type ty'cea, Type ty'cqs, cea, cqs]
-      in
+          cea'   = mkApps (Var zipP) [Type ty'cea, Type ty'cqs, cea, cqs]
       parStmts qss pa' cea'
 
 -- generate Core corresponding to `\p -> e'
 --
-deLambda        :: Type                        -- type of the argument
-               -> LPat Id              -- argument pattern
-               -> LHsExpr Id           -- body
-               -> DsM (CoreExpr, Type)
-deLambda ty p e  =
-  newSysLocalDs ty                                       `thenDs` \v       ->
-  dsLExpr e                                              `thenDs` \ce      ->
-  let errTy    = exprType ce
-      errMsg   = "DsListComp.deLambda: internal error!"
-  in
-  mkErrorAppDs pAT_ERROR_ID errTy errMsg                  `thenDs` \cerr    -> 
-  matchSimply (Var v) (StmtCtxt PArrComp) p ce cerr      `thenDs` \res     ->
-  returnDs (mkLams [v] res, errTy)
+deLambda :: Type                       -- type of the argument
+         -> LPat Id                    -- argument pattern
+         -> LHsExpr Id                 -- body
+         -> DsM (CoreExpr, Type)
+deLambda ty p e =
+    mkLambda ty p =<< dsLExpr e
+
+-- generate Core for a lambda pattern match, where the body is already in Core
+--
+mkLambda :: Type                       -- type of the argument
+        -> LPat Id                     -- argument pattern
+        -> CoreExpr                    -- desugared body
+        -> DsM (CoreExpr, Type)
+mkLambda ty p ce = do
+    v <- newSysLocalDs ty
+    let errMsg = ptext (sLit "DsListComp.deLambda: internal error!")
+        ce'ty  = exprType ce
+    cerr <- mkErrorAppDs pAT_ERROR_ID ce'ty errMsg
+    res <- matchSimply (Var v) (StmtCtxt PArrComp) p ce cerr
+    return (mkLams [v] res, ce'ty)
 
 -- obtain the element type of the parallel array produced by the given Core
 -- expression
@@ -499,16 +683,342 @@ parrElemType e  =
     Just (tycon, [ty]) | tycon == parrTyCon -> ty
     _                                                    -> panic
       "DsListComp.parrElemType: not a parallel array type"
+\end{code}
+
+Translation for monad comprehensions
+
+\begin{code}
+
+-- | Keep the "context" of a monad comprehension in a small data type to avoid
+-- some boilerplate...
+data DsMonadComp = DsMonadComp
+    { mc_return :: Either (SyntaxExpr Id) (Expr CoreBndr)
+    , mc_body   :: LHsExpr Id
+    , mc_m_ty   :: Type
+    }
+
+--
+-- Entry point for monad comprehension desugaring
+--
+dsMonadComp :: [LStmt Id]       -- the statements
+            -> SyntaxExpr Id    -- the "return" function
+            -> LHsExpr Id       -- the body
+            -> Type             -- the final type
+            -> DsM CoreExpr
+dsMonadComp stmts return_op body res_ty
+  = dsMcStmts stmts (DsMonadComp (Left return_op) body m_ty)
+  where
+    (m_ty, _) = tcSplitAppTy res_ty
+
+
+dsMcStmts :: [LStmt Id]
+          -> DsMonadComp
+          -> DsM CoreExpr
+
+-- No statements left for desugaring. Desugar the body after calling "return"
+-- on it.
+dsMcStmts [] DsMonadComp { mc_return, mc_body }
+  = case mc_return of
+         Left ret   -> dsLExpr $ noLoc ret `nlHsApp` mc_body
+         Right ret' -> do
+             { body' <- dsLExpr mc_body
+             ; return $ mkApps ret' [body'] }
+
+-- Otherwise desugar each statement step by step
+dsMcStmts ((L loc stmt) : lstmts) mc
+  = putSrcSpanDs loc (dsMcStmt stmt lstmts mc)
+
+
+dsMcStmt :: Stmt Id
+         -> [LStmt Id]
+         -> DsMonadComp
+         -> DsM CoreExpr
 
--- Smart constructor for source tuple patterns
+--   [ .. | let binds, stmts ]
+dsMcStmt (LetStmt binds) stmts mc
+  = do { rest <- dsMcStmts stmts mc
+       ; dsLocalBinds binds rest }
+
+--   [ .. | a <- m, stmts ]
+dsMcStmt (BindStmt pat rhs bind_op fail_op) stmts mc
+  = do { rhs'     <- dsLExpr rhs
+       ; dsMcBindStmt pat rhs' bind_op fail_op stmts mc }
+
+-- Apply `guard` to the `exp` expression
+--
+--   [ .. | exp, stmts ]
+--
+dsMcStmt (ExprStmt exp then_exp guard_exp _) stmts mc
+  = do { exp'       <- dsLExpr exp
+       ; guard_exp' <- dsExpr guard_exp
+       ; then_exp'  <- dsExpr then_exp
+       ; rest       <- dsMcStmts stmts mc
+       ; return $ mkApps then_exp' [ mkApps guard_exp' [exp']
+                                   , rest ] }
+
+-- Transform statements desugar like this:
+--
+--   [ .. | qs, then f by e ]  ->  f (\q_v -> e) [| qs |]
+--
+-- where [| qs |] is the desugared inner monad comprehenion generated by the
+-- statements `qs`.
+dsMcStmt (TransformStmt stmts binders usingExpr maybeByExpr return_op bind_op) stmts_rest mc
+  = do { (expr, _) <- dsInnerMonadComp (stmts, binders) (mc { mc_return = Left return_op })
+       ; let binders_tuple_type = mkBigCoreTupTy $ map idType binders
+       ; usingExpr' <- dsLExpr usingExpr
+       ; using_args <- case maybeByExpr of
+            Nothing -> return [expr]
+            Just byExpr -> do
+                byExpr' <- dsLExpr byExpr
+                us <- newUniqueSupply
+                tuple_binder <- newSysLocalDs binders_tuple_type
+                let byExprWrapper = mkTupleCase us binders byExpr' tuple_binder (Var tuple_binder)
+                return [Lam tuple_binder byExprWrapper, expr]
+
+       ; let pat = mkBigLHsVarPatTup binders
+             rhs = mkApps usingExpr' ((Type binders_tuple_type) : using_args)
+
+       ; dsMcBindStmt pat rhs bind_op noSyntaxExpr stmts_rest mc }
+
+-- Group statements desugar like this:
+--
+--   [| q, then group by e using f |]  ->  (f (\q_v -> e) [| q |]) >>= (return . (unzip q_v))
 --
-mkTuplePat :: [LPat Id] -> LPat Id
-mkTuplePat [lpat] = lpat
-mkTuplePat lpats  = noLoc $ mkVanillaTuplePat lpats Boxed
+-- which is equal to
+--
+--   [| q, then group by e using f |]  ->  liftM (unzip q_v) (f (\q_v -> e) [| q |])
+--
+-- where unzip is of the form
+--
+--   unzip :: m (a,b,c,..) -> (m a,m b,m c,..)
+--   unzip m_tuple = ( liftM selN1 m_tuple
+--                   , liftM selN2 m_tuple
+--                   , .. )
+--     where selN1 (a,b,c,..) = a
+--           selN2 (a,b,c,..) = b
+--           ..
+--
+dsMcStmt (GroupStmt stmts binderMap by using return_op bind_op liftM_op) stmts_rest mc
+  = do { let (fromBinders, toBinders) = unzip binderMap
+             fromBindersTypes         = map idType fromBinders
+             fromBindersTupleTy       = mkBigCoreTupTy fromBindersTypes
+             toBindersTypes           = map idType toBinders
+             toBindersTupleTy         = mkBigCoreTupTy toBindersTypes
+             m_ty                     = mc_m_ty mc
+
+       -- Desugar an inner comprehension which outputs a list of tuples of the "from" binders
+       ; (expr, _) <- dsInnerMonadComp (stmts, fromBinders) (mc { mc_return = Left 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
+       ; usingExpr' <- dsLExpr (either id noLoc using)
+       ; usingArgs <- case by of
+                        Nothing   -> return [expr]
+                        Just by_e -> do { by_e' <- dsLExpr by_e
+                                        ; us <- newUniqueSupply
+                                        ; from_tup_id <- newSysLocalDs fromBindersTupleTy
+                                        ; let by_wrap = mkTupleCase us fromBinders by_e' 
+                                                        from_tup_id (Var from_tup_id)
+                                        ; return [Lam from_tup_id by_wrap, expr] }
+
+       -- Create an unzip function for the appropriate arity and element types
+       ; liftM_op' <- dsExpr liftM_op
+       ; (unzip_fn, unzip_rhs) <- mkMcUnzipM liftM_op' m_ty fromBindersTypes
+
+       -- Generate the expressions to build the grouped list
+
+       ; let -- First we apply the grouping function to the inner monad
+             inner_monad_expr = mkApps usingExpr' ((Type fromBindersTupleTy) : usingArgs)
+             -- Then we map our "unzip" across it to turn the "monad of tuples" into "tuples of monads"
+             -- We make sure we instantiate the type variable "a" to be a "monad of 'from' tuples" and
+             -- the "b" to be a "tuple of 'to' monads"!
+             unzipped_inner_monad_expr = mkApps liftM_op'  -- !
+                                                -- Types:
+                                                [ Type (m_ty `mkAppTy` fromBindersTupleTy), Type toBindersTupleTy
+                                                -- And arguments:
+                                                , Var unzip_fn, inner_monad_expr ]
+             -- Then finally we bind the unzip function around that expression
+             bound_unzipped_inner_monad_expr = Let (Rec [(unzip_fn, unzip_rhs)]) unzipped_inner_monad_expr
+
+       -- Build a pattern that ensures the consumer binds into the NEW binders, which hold monads
+       -- rather than single values
+       ; let pat = mkBigLHsVarPatTup toBinders
+             rhs = bound_unzipped_inner_monad_expr
+
+       ; dsMcBindStmt pat rhs bind_op noSyntaxExpr stmts_rest mc }
+
+-- Parallel statements. Use `Control.Monad.Zip.mzip` to zip parallel
+-- statements, for example:
+--
+--   [ body | qs1 | qs2 | qs3 ]
+--     ->  [ body | (bndrs1, (bndrs2, bndrs3)) <- mzip qs1 (mzip qs2 qs3) ]
+--
+-- where `mzip` is of the form
+--
+--   mzip :: m a -> m b -> m (a,b)
+--
+dsMcStmt (ParStmt pairs mzip_op bind_op return_op) stmts_rest mc
+ = do  { -- Get types for `return`
+         return_op' <- dsExpr return_op
+       ; let pairs_with_return = map (\tp@(_,b) -> (mkReturn b,tp)) pairs
+             mkReturn bndrs    = mkApps return_op' [Type (mkBigCoreTupTy (map idType bndrs))]
+
+       ; pairs' <- mapM (\(r,tp) -> dsInnerMonadComp tp mc{mc_return = Right r})
+                        pairs_with_return
+
+       ; let (exps, _qual_tys) = unzip pairs'
+             -- Types of our `Id`s are getting messed up by `dsInnerMonadComp`
+             -- so we construct them by hand:
+             qual_tys = map (mkBigCoreTupTy . map idType . snd) pairs
+
+       ; mzip_op' <- dsExpr mzip_op
+       ; (zip_fn, zip_rhs) <- mkMcZipM mzip_op' (mc_m_ty mc) qual_tys
+
+       ; let -- The pattern variables
+             vars = map (mkBigLHsVarPatTup . snd) pairs
+             -- Pattern with tuples of variables
+             -- [v1,v2,v3]  =>  (v1, (v2, v3))
+             pat = foldr (\tn tm -> mkBigLHsPatTup [tn, tm]) (last vars) (init vars)
+             rhs = Let (Rec [(zip_fn, zip_rhs)]) (mkApps (Var zip_fn) exps)
+
+       ; dsMcBindStmt pat rhs bind_op noSyntaxExpr stmts_rest mc }
+
+dsMcStmt stmt _ _ = pprPanic "dsMcStmt: unexpected stmt" (ppr stmt)
+
+
+-- general `rhs' >>= \pat -> stmts` desugaring where `rhs'` is already a
+-- desugared `CoreExpr`
+dsMcBindStmt :: LPat Id
+             -> CoreExpr        -- ^ the desugared rhs of the bind statement
+             -> SyntaxExpr Id
+             -> SyntaxExpr Id
+             -> [LStmt Id]
+             -> DsMonadComp
+             -> DsM CoreExpr
+dsMcBindStmt pat rhs' bind_op fail_op stmts mc
+  = do  { body     <- dsMcStmts stmts mc
+        ; bind_op' <- dsExpr bind_op
+        ; var      <- selectSimpleMatchVarL pat
+        ; let bind_ty = exprType bind_op'      -- rhs -> (pat -> res1) -> res2
+              res1_ty = funResultTy (funArgTy (funResultTy bind_ty))
+        ; match <- matchSinglePat (Var var) (StmtCtxt DoExpr) pat
+                                  res1_ty (cantFailMatchResult body)
+        ; match_code <- handle_failure pat match fail_op
+        ; return (mkApps bind_op' [rhs', Lam var match_code]) }
 
--- Smart constructor for source tuple expressions
+  where
+    -- In a monad comprehension expression, pattern-match failure just calls
+    -- the monadic `fail` rather than throwing an exception
+    handle_failure pat match fail_op
+      | matchCanFail match
+        = do { fail_op' <- dsExpr fail_op
+             ; fail_msg <- mkStringExpr (mk_fail_msg pat)
+             ; extractMatchResult match (App fail_op' fail_msg) }
+      | otherwise
+        = extractMatchResult match (error "It can't fail") 
+
+    mk_fail_msg :: Located e -> String
+    mk_fail_msg pat = "Pattern match failure in monad comprehension at " ++ 
+                      showSDoc (ppr (getLoc pat))
+
+-- Desugar nested monad comprehensions, for example in `then..` constructs
+dsInnerMonadComp :: ([LStmt Id], [Id])
+                 -> DsMonadComp
+                 -> DsM (CoreExpr, Type)
+dsInnerMonadComp (stmts, bndrs) DsMonadComp{ mc_return, mc_m_ty }
+  = do { expr <- dsMcStmts stmts mc'
+       ; return (expr, bndrs_tuple_type) }
+    where
+        bndrs_types      = map idType bndrs
+        bndrs_tuple_type = mkAppTy mc_m_ty $ mkBigCoreTupTy bndrs_types
+        mc'              = DsMonadComp mc_return (mkBigLHsVarTup bndrs) mc_m_ty
+
+-- 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 m [t1, t2]
+--     = (unzip_fn, \ys :: m (t1, t2) ->
+--         ( liftM (selN1 :: (t1, t2) -> t1) ys
+--         , liftM (selN2 :: (t1, t2) -> t2) ys
+--         ))
+--
+mkMcUnzipM :: CoreExpr
+           -> Type                      -- m
+           -> [Type]                    -- [a,b,c,..]
+           -> DsM (Id, CoreExpr)
+mkMcUnzipM liftM_op m_ty elt_tys
+  = do  { ys    <- newSysLocalDs monad_tuple_ty
+        ; xs    <- mapM newSysLocalDs elt_tys
+        ; scrut <- newSysLocalDs tuple_tys
+
+        ; unzip_fn <- newSysLocalDs unzip_fn_ty
+
+        ; let -- Select one Id from our tuple
+              selectExpr n = mkLams [scrut] $ mkTupleSelector xs (xs !! n) scrut (Var scrut)
+              -- Apply 'selectVar' and 'ys' to 'liftM'
+              tupleElem n = mkApps liftM_op
+                                   -- Types (m is figured out by the type checker):
+                                   -- liftM :: forall a b. (a -> b) -> m a -> m b
+                                   [ Type tuple_tys, Type (elt_tys !! n)
+                                   -- Arguments:
+                                   , selectExpr n, Var ys ]
+              -- The final expression with the big tuple
+              unzip_body = mkBigCoreTup [ tupleElem n | n <- [0..length elt_tys - 1] ]
+
+        ; return (unzip_fn, mkLams [ys] unzip_body) }
+  where monad_tys       = map (m_ty `mkAppTy`) elt_tys                  -- [m a,m b,m c,..]
+        tuple_monad_tys = mkBigCoreTupTy monad_tys                      -- (m a,m b,m c,..)
+        tuple_tys       = mkBigCoreTupTy elt_tys                        -- (a,b,c,..)
+        monad_tuple_ty  = m_ty `mkAppTy` tuple_tys                      -- m (a,b,c,..)
+        unzip_fn_ty     = monad_tuple_ty `mkFunTy` tuple_monad_tys      -- m (a,b,c,..) -> (m a,m b,m c,..)
+
+-- Generate the `mzip` function for `ParStmt` in monad comprehensions, for
+-- example:
 --
-mkExplicitTuple :: [LHsExpr id] -> LHsExpr id
-mkExplicitTuple [lexp] = lexp
-mkExplicitTuple lexps  = noLoc $ ExplicitTuple lexps Boxed
+--   mzip :: m t1
+--        -> (m t2 -> m t3 -> m (t2, t3))
+--        -> m (t1, (t2, t3))
+--
+--   mkMcZipM m [t1, t2, t3]
+--     = (zip_fn, \(q1::t1) (q2::t2) (q3::t3) ->
+--         mzip q1 (mzip q2 q3))
+--
+mkMcZipM :: CoreExpr
+         -> Type
+         -> [Type]
+         -> DsM (Id, CoreExpr)
+
+mkMcZipM mzip_op m_ty tys@(_:_:_) -- min. 2 types
+ = do  { (ids, t1, tuple_ty, zip_body) <- loop tys
+       ; zip_fn <- newSysLocalDs $
+                       (m_ty `mkAppTy` t1)
+                       `mkFunTy`
+                       (m_ty `mkAppTy` tuple_ty)
+                       `mkFunTy`
+                       (m_ty `mkAppTy` mkBigCoreTupTy [t1, tuple_ty])
+       ; return (zip_fn, mkLams ids zip_body) }
+
+ where 
+       -- loop :: [Type] -> DsM ([Id], Type, [Type], CoreExpr)
+       loop [t1, t2] = do -- last run of the `loop`
+           { ids@[a,b] <- newSysLocalsDs (map (m_ty `mkAppTy`) [t1,t2])
+           ; let zip_body = mkApps mzip_op [ Type t1, Type t2 , Var a, Var b ]
+           ; return (ids, t1, t2, zip_body) }
+
+       loop (t1:tr) = do
+           { -- Get ty, ids etc from the "inner" zip
+             (ids', t1', t2', zip_body') <- loop tr
+
+           ; a <- newSysLocalDs $ m_ty `mkAppTy` t1
+           ; let tuple_ty' = mkBigCoreTupTy [t1', t2']
+                 zip_body = mkApps mzip_op [ Type t1, Type tuple_ty', Var a, zip_body' ]
+           ; return ((a:ids'), t1, tuple_ty', zip_body) }
+
+-- This case should never happen:
+mkMcZipM _ _ tys = pprPanic "mkMcZipM: unexpected argument" (ppr tys)
+
 \end{code}