{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}
module DsArrows ( dsProcExpr ) where
#include "HsVersions.h"
import GhcPrelude
import Match
import DsUtils
import DsMonad
import HsSyn hiding (collectPatBinders, collectPatsBinders,
collectLStmtsBinders, collectLStmtBinders,
collectStmtBinders )
import TcHsSyn
import qualified HsUtils
import {-# SOURCE #-} DsExpr ( dsExpr, dsLExpr, dsLExprNoLP, dsLocalBinds,
dsSyntaxExpr )
import TcType
import Type ( splitPiTy )
import TcEvidence
import CoreSyn
import CoreFVs
import CoreUtils
import MkCore
import DsBinds (dsHsWrapper)
import Name
import Id
import ConLike
import TysWiredIn
import BasicTypes
import PrelNames
import Outputable
import Bag
import VarSet
import SrcLoc
import ListSetOps( assocMaybe )
import Data.List
import Util
import UniqDSet
data DsCmdEnv = DsCmdEnv {
DsCmdEnv -> CoreExpr
arr_id, DsCmdEnv -> CoreExpr
compose_id, DsCmdEnv -> CoreExpr
first_id, DsCmdEnv -> CoreExpr
app_id, DsCmdEnv -> CoreExpr
choice_id, DsCmdEnv -> CoreExpr
loop_id :: CoreExpr
}
mkCmdEnv :: CmdSyntaxTable GhcTc -> DsM ([CoreBind], DsCmdEnv)
mkCmdEnv :: CmdSyntaxTable GhcTc -> DsM ([CoreBind], DsCmdEnv)
mkCmdEnv tc_meths :: CmdSyntaxTable GhcTc
tc_meths
= do { (meth_binds :: [CoreBind]
meth_binds, prs :: [(Name, Id)]
prs) <- ((Name, HsExpr GhcTc)
-> IOEnv (Env DsGblEnv DsLclEnv) (CoreBind, (Name, Id)))
-> CmdSyntaxTable GhcTc
-> IOEnv (Env DsGblEnv DsLclEnv) ([CoreBind], [(Name, Id)])
forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM (Name, HsExpr GhcTc)
-> IOEnv (Env DsGblEnv DsLclEnv) (CoreBind, (Name, Id))
forall a.
(a, HsExpr GhcTc)
-> IOEnv (Env DsGblEnv DsLclEnv) (CoreBind, (a, Id))
mk_bind CmdSyntaxTable GhcTc
tc_meths
; let the_arr_id :: Maybe Id
the_arr_id = [(Name, Id)] -> Name -> Maybe Id
forall a b. Eq a => Assoc a b -> a -> Maybe b
assocMaybe [(Name, Id)]
prs Name
arrAName
the_compose_id :: Maybe Id
the_compose_id = [(Name, Id)] -> Name -> Maybe Id
forall a b. Eq a => Assoc a b -> a -> Maybe b
assocMaybe [(Name, Id)]
prs Name
composeAName
the_first_id :: Maybe Id
the_first_id = [(Name, Id)] -> Name -> Maybe Id
forall a b. Eq a => Assoc a b -> a -> Maybe b
assocMaybe [(Name, Id)]
prs Name
firstAName
the_app_id :: Maybe Id
the_app_id = [(Name, Id)] -> Name -> Maybe Id
forall a b. Eq a => Assoc a b -> a -> Maybe b
assocMaybe [(Name, Id)]
prs Name
appAName
the_choice_id :: Maybe Id
the_choice_id = [(Name, Id)] -> Name -> Maybe Id
forall a b. Eq a => Assoc a b -> a -> Maybe b
assocMaybe [(Name, Id)]
prs Name
choiceAName
the_loop_id :: Maybe Id
the_loop_id = [(Name, Id)] -> Name -> Maybe Id
forall a b. Eq a => Assoc a b -> a -> Maybe b
assocMaybe [(Name, Id)]
prs Name
loopAName
; Int -> Maybe Id -> DsM ()
check_lev_poly 3 Maybe Id
the_arr_id
; Int -> Maybe Id -> DsM ()
check_lev_poly 5 Maybe Id
the_compose_id
; Int -> Maybe Id -> DsM ()
check_lev_poly 4 Maybe Id
the_first_id
; Int -> Maybe Id -> DsM ()
check_lev_poly 2 Maybe Id
the_app_id
; Int -> Maybe Id -> DsM ()
check_lev_poly 5 Maybe Id
the_choice_id
; Int -> Maybe Id -> DsM ()
check_lev_poly 4 Maybe Id
the_loop_id
; ([CoreBind], DsCmdEnv) -> DsM ([CoreBind], DsCmdEnv)
forall (m :: * -> *) a. Monad m => a -> m a
return ([CoreBind]
meth_binds, DsCmdEnv :: CoreExpr
-> CoreExpr
-> CoreExpr
-> CoreExpr
-> CoreExpr
-> CoreExpr
-> DsCmdEnv
DsCmdEnv {
arr_id :: CoreExpr
arr_id = Id -> CoreExpr
forall b. Id -> Expr b
Var (Maybe Id -> Name -> Id
forall a p. Outputable a => Maybe p -> a -> p
unmaybe Maybe Id
the_arr_id Name
arrAName),
compose_id :: CoreExpr
compose_id = Id -> CoreExpr
forall b. Id -> Expr b
Var (Maybe Id -> Name -> Id
forall a p. Outputable a => Maybe p -> a -> p
unmaybe Maybe Id
the_compose_id Name
composeAName),
first_id :: CoreExpr
first_id = Id -> CoreExpr
forall b. Id -> Expr b
Var (Maybe Id -> Name -> Id
forall a p. Outputable a => Maybe p -> a -> p
unmaybe Maybe Id
the_first_id Name
firstAName),
app_id :: CoreExpr
app_id = Id -> CoreExpr
forall b. Id -> Expr b
Var (Maybe Id -> Name -> Id
forall a p. Outputable a => Maybe p -> a -> p
unmaybe Maybe Id
the_app_id Name
appAName),
choice_id :: CoreExpr
choice_id = Id -> CoreExpr
forall b. Id -> Expr b
Var (Maybe Id -> Name -> Id
forall a p. Outputable a => Maybe p -> a -> p
unmaybe Maybe Id
the_choice_id Name
choiceAName),
loop_id :: CoreExpr
loop_id = Id -> CoreExpr
forall b. Id -> Expr b
Var (Maybe Id -> Name -> Id
forall a p. Outputable a => Maybe p -> a -> p
unmaybe Maybe Id
the_loop_id Name
loopAName)
}) }
where
mk_bind :: (a, HsExpr GhcTc)
-> IOEnv (Env DsGblEnv DsLclEnv) (CoreBind, (a, Id))
mk_bind (std_name :: a
std_name, expr :: HsExpr GhcTc
expr)
= do { CoreExpr
rhs <- HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
expr
; Id
id <- Type -> DsM Id
newSysLocalDs (CoreExpr -> Type
exprType CoreExpr
rhs)
; (CoreBind, (a, Id))
-> IOEnv (Env DsGblEnv DsLclEnv) (CoreBind, (a, Id))
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> CoreExpr -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec Id
id CoreExpr
rhs, (a
std_name, Id
id)) }
unmaybe :: Maybe p -> a -> p
unmaybe Nothing name :: a
name = String -> SDoc -> p
forall a. HasCallStack => String -> SDoc -> a
pprPanic "mkCmdEnv" (String -> SDoc
text "Not found:" SDoc -> SDoc -> SDoc
<+> a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
name)
unmaybe (Just id :: p
id) _ = p
id
res_type :: Type -> Type
res_type :: Type -> Type
res_type ty :: Type
ty = Type
res_ty
where
(_, res_ty :: Type
res_ty) = Type -> (TyCoBinder, Type)
splitPiTy Type
ty
check_lev_poly :: Int
-> Maybe Id -> DsM ()
check_lev_poly :: Int -> Maybe Id -> DsM ()
check_lev_poly _ Nothing = () -> DsM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
check_lev_poly arity :: Int
arity (Just id :: Id
id)
= Type -> SDoc -> DsM ()
dsNoLevPoly (Int -> (Type -> Type) -> Type -> Type
forall a. Int -> (a -> a) -> a -> a
nTimes Int
arity Type -> Type
res_type (Id -> Type
idType Id
id))
(String -> SDoc
text "In the result of the function" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
id))
do_arr :: DsCmdEnv -> Type -> Type -> CoreExpr -> CoreExpr
do_arr :: DsCmdEnv -> Type -> Type -> CoreExpr -> CoreExpr
do_arr ids :: DsCmdEnv
ids b_ty :: Type
b_ty c_ty :: Type
c_ty f :: CoreExpr
f = CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps (DsCmdEnv -> CoreExpr
arr_id DsCmdEnv
ids) [Type -> CoreExpr
forall b. Type -> Expr b
Type Type
b_ty, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
c_ty, CoreExpr
f]
do_compose :: DsCmdEnv -> Type -> Type -> Type ->
CoreExpr -> CoreExpr -> CoreExpr
do_compose :: DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_compose ids :: DsCmdEnv
ids b_ty :: Type
b_ty c_ty :: Type
c_ty d_ty :: Type
d_ty f :: CoreExpr
f g :: CoreExpr
g
= CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps (DsCmdEnv -> CoreExpr
compose_id DsCmdEnv
ids) [Type -> CoreExpr
forall b. Type -> Expr b
Type Type
b_ty, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
c_ty, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
d_ty, CoreExpr
f, CoreExpr
g]
do_first :: DsCmdEnv -> Type -> Type -> Type -> CoreExpr -> CoreExpr
do_first :: DsCmdEnv -> Type -> Type -> Type -> CoreExpr -> CoreExpr
do_first ids :: DsCmdEnv
ids b_ty :: Type
b_ty c_ty :: Type
c_ty d_ty :: Type
d_ty f :: CoreExpr
f
= CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps (DsCmdEnv -> CoreExpr
first_id DsCmdEnv
ids) [Type -> CoreExpr
forall b. Type -> Expr b
Type Type
b_ty, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
c_ty, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
d_ty, CoreExpr
f]
do_app :: DsCmdEnv -> Type -> Type -> CoreExpr
do_app :: DsCmdEnv -> Type -> Type -> CoreExpr
do_app ids :: DsCmdEnv
ids b_ty :: Type
b_ty c_ty :: Type
c_ty = CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps (DsCmdEnv -> CoreExpr
app_id DsCmdEnv
ids) [Type -> CoreExpr
forall b. Type -> Expr b
Type Type
b_ty, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
c_ty]
do_choice :: DsCmdEnv -> Type -> Type -> Type ->
CoreExpr -> CoreExpr -> CoreExpr
do_choice :: DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_choice ids :: DsCmdEnv
ids b_ty :: Type
b_ty c_ty :: Type
c_ty d_ty :: Type
d_ty f :: CoreExpr
f g :: CoreExpr
g
= CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps (DsCmdEnv -> CoreExpr
choice_id DsCmdEnv
ids) [Type -> CoreExpr
forall b. Type -> Expr b
Type Type
b_ty, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
d_ty, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
c_ty, CoreExpr
f, CoreExpr
g]
do_loop :: DsCmdEnv -> Type -> Type -> Type -> CoreExpr -> CoreExpr
do_loop :: DsCmdEnv -> Type -> Type -> Type -> CoreExpr -> CoreExpr
do_loop ids :: DsCmdEnv
ids b_ty :: Type
b_ty c_ty :: Type
c_ty d_ty :: Type
d_ty f :: CoreExpr
f
= CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps (DsCmdEnv -> CoreExpr
loop_id DsCmdEnv
ids) [Type -> CoreExpr
forall b. Type -> Expr b
Type Type
b_ty, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
d_ty, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
c_ty, CoreExpr
f]
do_premap :: DsCmdEnv -> Type -> Type -> Type ->
CoreExpr -> CoreExpr -> CoreExpr
do_premap :: DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap ids :: DsCmdEnv
ids b_ty :: Type
b_ty c_ty :: Type
c_ty d_ty :: Type
d_ty f :: CoreExpr
f g :: CoreExpr
g
= DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_compose DsCmdEnv
ids Type
b_ty Type
c_ty Type
d_ty (DsCmdEnv -> Type -> Type -> CoreExpr -> CoreExpr
do_arr DsCmdEnv
ids Type
b_ty Type
c_ty CoreExpr
f) CoreExpr
g
mkFailExpr :: HsMatchContext Id -> Type -> DsM CoreExpr
mkFailExpr :: HsMatchContext Id -> Type -> DsM CoreExpr
mkFailExpr ctxt :: HsMatchContext Id
ctxt ty :: Type
ty
= Id -> Type -> SDoc -> DsM CoreExpr
mkErrorAppDs Id
pAT_ERROR_ID Type
ty (HsMatchContext Id -> SDoc
forall id. Outputable id => HsMatchContext id -> SDoc
matchContextErrString HsMatchContext Id
ctxt)
mkFstExpr :: Type -> Type -> DsM CoreExpr
mkFstExpr :: Type -> Type -> DsM CoreExpr
mkFstExpr a_ty :: Type
a_ty b_ty :: Type
b_ty = do
Id
a_var <- Type -> DsM Id
newSysLocalDs Type
a_ty
Id
b_var <- Type -> DsM Id
newSysLocalDs Type
b_ty
Id
pair_var <- Type -> DsM Id
newSysLocalDs (Type -> Type -> Type
mkCorePairTy Type
a_ty Type
b_ty)
CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Id
pair_var
(Id -> Id -> Id -> CoreExpr -> CoreExpr
coreCasePair Id
pair_var Id
a_var Id
b_var (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
a_var)))
mkSndExpr :: Type -> Type -> DsM CoreExpr
mkSndExpr :: Type -> Type -> DsM CoreExpr
mkSndExpr a_ty :: Type
a_ty b_ty :: Type
b_ty = do
Id
a_var <- Type -> DsM Id
newSysLocalDs Type
a_ty
Id
b_var <- Type -> DsM Id
newSysLocalDs Type
b_ty
Id
pair_var <- Type -> DsM Id
newSysLocalDs (Type -> Type -> Type
mkCorePairTy Type
a_ty Type
b_ty)
CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Id
pair_var
(Id -> Id -> Id -> CoreExpr -> CoreExpr
coreCasePair Id
pair_var Id
a_var Id
b_var (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
b_var)))
coreCaseTuple :: UniqSupply -> Id -> [Id] -> CoreExpr -> CoreExpr
coreCaseTuple :: UniqSupply -> Id -> [Id] -> CoreExpr -> CoreExpr
coreCaseTuple uniqs :: UniqSupply
uniqs scrut_var :: Id
scrut_var vars :: [Id]
vars body :: CoreExpr
body
= UniqSupply -> [Id] -> CoreExpr -> Id -> CoreExpr -> CoreExpr
mkTupleCase UniqSupply
uniqs [Id]
vars CoreExpr
body Id
scrut_var (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
scrut_var)
coreCasePair :: Id -> Id -> Id -> CoreExpr -> CoreExpr
coreCasePair :: Id -> Id -> Id -> CoreExpr -> CoreExpr
coreCasePair scrut_var :: Id
scrut_var var1 :: Id
var1 var2 :: Id
var2 body :: CoreExpr
body
= 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
scrut_var) Id
scrut_var (CoreExpr -> Type
exprType CoreExpr
body)
[(DataCon -> AltCon
DataAlt (Boxity -> Int -> DataCon
tupleDataCon Boxity
Boxed 2), [Id
var1, Id
var2], CoreExpr
body)]
mkCorePairTy :: Type -> Type -> Type
mkCorePairTy :: Type -> Type -> Type
mkCorePairTy t1 :: Type
t1 t2 :: Type
t2 = [Type] -> Type
mkBoxedTupleTy [Type
t1, Type
t2]
mkCorePairExpr :: CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr :: CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr e1 :: CoreExpr
e1 e2 :: CoreExpr
e2 = [CoreExpr] -> CoreExpr
mkCoreTup [CoreExpr
e1, CoreExpr
e2]
mkCoreUnitExpr :: CoreExpr
mkCoreUnitExpr :: CoreExpr
mkCoreUnitExpr = [CoreExpr] -> CoreExpr
mkCoreTup []
envStackType :: [Id] -> Type -> Type
envStackType :: [Id] -> Type -> Type
envStackType ids :: [Id]
ids stack_ty :: Type
stack_ty = Type -> Type -> Type
mkCorePairTy ([Id] -> Type
mkBigCoreVarTupTy [Id]
ids) Type
stack_ty
splitTypeAt :: Int -> Type -> ([Type], Type)
splitTypeAt :: Int -> Type -> ([Type], Type)
splitTypeAt n :: Int
n ty :: Type
ty
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0 = ([], Type
ty)
| Bool
otherwise = case Type -> [Type]
tcTyConAppArgs Type
ty of
[t :: Type
t, ty' :: Type
ty'] -> let (ts :: [Type]
ts, ty_r :: Type
ty_r) = Int -> Type -> ([Type], Type)
splitTypeAt (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) Type
ty' in (Type
tType -> [Type] -> [Type]
forall a. a -> [a] -> [a]
:[Type]
ts, Type
ty_r)
_ -> String -> SDoc -> ([Type], Type)
forall a. HasCallStack => String -> SDoc -> a
pprPanic "splitTypeAt" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
buildEnvStack :: [Id] -> Id -> CoreExpr
buildEnvStack :: [Id] -> Id -> CoreExpr
buildEnvStack env_ids :: [Id]
env_ids stack_id :: Id
stack_id
= CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr ([Id] -> CoreExpr
mkBigCoreVarTup [Id]
env_ids) (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
stack_id)
matchEnvStack :: [Id]
-> Id
-> CoreExpr
-> DsM CoreExpr
matchEnvStack :: [Id] -> Id -> CoreExpr -> DsM CoreExpr
matchEnvStack env_ids :: [Id]
env_ids stack_id :: Id
stack_id body :: CoreExpr
body = do
UniqSupply
uniqs <- TcRnIf DsGblEnv DsLclEnv UniqSupply
forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply
Id
tup_var <- Type -> DsM Id
newSysLocalDs ([Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids)
let match_env :: CoreExpr
match_env = UniqSupply -> Id -> [Id] -> CoreExpr -> CoreExpr
coreCaseTuple UniqSupply
uniqs Id
tup_var [Id]
env_ids CoreExpr
body
Id
pair_id <- Type -> DsM Id
newSysLocalDs (Type -> Type -> Type
mkCorePairTy (Id -> Type
idType Id
tup_var) (Id -> Type
idType Id
stack_id))
CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Id
pair_id (Id -> Id -> Id -> CoreExpr -> CoreExpr
coreCasePair Id
pair_id Id
tup_var Id
stack_id CoreExpr
match_env))
matchEnv :: [Id]
-> CoreExpr
-> DsM CoreExpr
matchEnv :: [Id] -> CoreExpr -> DsM CoreExpr
matchEnv env_ids :: [Id]
env_ids body :: CoreExpr
body = do
UniqSupply
uniqs <- TcRnIf DsGblEnv DsLclEnv UniqSupply
forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply
Id
tup_id <- Type -> DsM Id
newSysLocalDs ([Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids)
CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Id
tup_id (UniqSupply -> Id -> [Id] -> CoreExpr -> CoreExpr
coreCaseTuple UniqSupply
uniqs Id
tup_id [Id]
env_ids CoreExpr
body))
matchVarStack :: [Id] -> Id -> CoreExpr -> DsM (Id, CoreExpr)
matchVarStack :: [Id] -> Id -> CoreExpr -> DsM (Id, CoreExpr)
matchVarStack [] stack_id :: Id
stack_id body :: CoreExpr
body = (Id, CoreExpr) -> DsM (Id, CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return (Id
stack_id, CoreExpr
body)
matchVarStack (param_id :: Id
param_id:param_ids :: [Id]
param_ids) stack_id :: Id
stack_id body :: CoreExpr
body = do
(tail_id :: Id
tail_id, tail_code :: CoreExpr
tail_code) <- [Id] -> Id -> CoreExpr -> DsM (Id, CoreExpr)
matchVarStack [Id]
param_ids Id
stack_id CoreExpr
body
Id
pair_id <- Type -> DsM Id
newSysLocalDs (Type -> Type -> Type
mkCorePairTy (Id -> Type
idType Id
param_id) (Id -> Type
idType Id
tail_id))
(Id, CoreExpr) -> DsM (Id, CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return (Id
pair_id, Id -> Id -> Id -> CoreExpr -> CoreExpr
coreCasePair Id
pair_id Id
param_id Id
tail_id CoreExpr
tail_code)
mkHsEnvStackExpr :: [Id] -> Id -> LHsExpr GhcTc
mkHsEnvStackExpr :: [Id] -> Id -> LHsExpr GhcTc
mkHsEnvStackExpr env_ids :: [Id]
env_ids stack_id :: Id
stack_id
= [LHsExpr GhcTc] -> LHsExpr GhcTc
forall (a :: Pass). [LHsExpr (GhcPass a)] -> LHsExpr (GhcPass a)
mkLHsTupleExpr [[IdP GhcTc] -> LHsExpr GhcTc
forall (a :: Pass). [IdP (GhcPass a)] -> LHsExpr (GhcPass a)
mkLHsVarTuple [Id]
[IdP GhcTc]
env_ids, IdP GhcTc -> LHsExpr GhcTc
forall (id :: Pass). IdP (GhcPass id) -> LHsExpr (GhcPass id)
nlHsVar Id
IdP GhcTc
stack_id]
dsProcExpr
:: LPat GhcTc
-> LHsCmdTop GhcTc
-> DsM CoreExpr
dsProcExpr :: LPat GhcTc -> LHsCmdTop GhcTc -> DsM CoreExpr
dsProcExpr pat :: LPat GhcTc
pat (LHsCmdTop GhcTc -> Located (SrcSpanLess (LHsCmdTop GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (HsCmdTop (CmdTopTc _unitTy cmd_ty ids) cmd)) = do
(meth_binds :: [CoreBind]
meth_binds, meth_ids :: DsCmdEnv
meth_ids) <- CmdSyntaxTable GhcTc -> DsM ([CoreBind], DsCmdEnv)
mkCmdEnv CmdSyntaxTable GhcTc
ids
let locals :: VarSet
locals = [Id] -> VarSet
mkVarSet (LPat GhcTc -> [Id]
collectPatBinders LPat GhcTc
pat)
(core_cmd :: CoreExpr
core_cmd, _free_vars :: DIdSet
_free_vars, env_ids :: [Id]
env_ids)
<- DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmd DsCmdEnv
meth_ids VarSet
locals Type
unitTy Type
cmd_ty LHsCmd GhcTc
cmd
let env_ty :: Type
env_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids
let env_stk_ty :: Type
env_stk_ty = Type -> Type -> Type
mkCorePairTy Type
env_ty Type
unitTy
let env_stk_expr :: CoreExpr
env_stk_expr = CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr ([Id] -> CoreExpr
mkBigCoreVarTup [Id]
env_ids) CoreExpr
mkCoreUnitExpr
CoreExpr
fail_expr <- HsMatchContext Id -> Type -> DsM CoreExpr
mkFailExpr HsMatchContext Id
forall id. HsMatchContext id
ProcExpr Type
env_stk_ty
Id
var <- LPat GhcTc -> DsM Id
selectSimpleMatchVarL LPat GhcTc
pat
CoreExpr
match_code <- CoreExpr
-> HsMatchContext Name
-> LPat GhcTc
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimply (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
var) HsMatchContext Name
forall id. HsMatchContext id
ProcExpr LPat GhcTc
pat CoreExpr
env_stk_expr CoreExpr
fail_expr
let pat_ty :: Type
pat_ty = LPat GhcTc -> Type
hsLPatType LPat GhcTc
pat
let proc_code :: CoreExpr
proc_code = DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
meth_ids Type
pat_ty Type
env_stk_ty Type
cmd_ty
(Id -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Id
var CoreExpr
match_code)
CoreExpr
core_cmd
CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return ([CoreBind] -> CoreExpr -> CoreExpr
forall b. [Bind b] -> Expr b -> Expr b
mkLets [CoreBind]
meth_binds CoreExpr
proc_code)
dsProcExpr _ _ = String -> DsM CoreExpr
forall a. String -> a
panic "dsProcExpr"
dsLCmd :: DsCmdEnv -> IdSet -> Type -> Type -> LHsCmd GhcTc -> [Id]
-> DsM (CoreExpr, DIdSet)
dsLCmd :: DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsLCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stk_ty :: Type
stk_ty res_ty :: Type
res_ty cmd :: LHsCmd GhcTc
cmd env_ids :: [Id]
env_ids
= DsCmdEnv
-> VarSet
-> Type
-> Type
-> HsCmd GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmd DsCmdEnv
ids VarSet
local_vars Type
stk_ty Type
res_ty (LHsCmd GhcTc -> SrcSpanLess (LHsCmd GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc LHsCmd GhcTc
cmd) [Id]
env_ids
dsCmd :: DsCmdEnv
-> IdSet
-> Type
-> Type
-> HsCmd GhcTc
-> [Id]
-> DsM (CoreExpr,
DIdSet)
dsCmd :: DsCmdEnv
-> VarSet
-> Type
-> Type
-> HsCmd GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stack_ty :: Type
stack_ty res_ty :: Type
res_ty
(HsCmdArrApp arrow_ty :: XCmdArrApp GhcTc
arrow_ty arrow :: LHsExpr GhcTc
arrow arg :: LHsExpr GhcTc
arg HsFirstOrderApp _)
env_ids :: [Id]
env_ids = do
let
(a_arg_ty :: Type
a_arg_ty, _res_ty' :: Type
_res_ty') = Type -> (Type, Type)
tcSplitAppTy Type
XCmdArrApp GhcTc
arrow_ty
(_a_ty :: Type
_a_ty, arg_ty :: Type
arg_ty) = Type -> (Type, Type)
tcSplitAppTy Type
a_arg_ty
CoreExpr
core_arrow <- LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
arrow
CoreExpr
core_arg <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
arg
Id
stack_id <- Type -> DsM Id
newSysLocalDs Type
stack_ty
CoreExpr
core_make_arg <- [Id] -> Id -> CoreExpr -> DsM CoreExpr
matchEnvStack [Id]
env_ids Id
stack_id CoreExpr
core_arg
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids
([Id] -> Type -> Type
envStackType [Id]
env_ids Type
stack_ty)
Type
arg_ty
Type
res_ty
CoreExpr
core_make_arg
CoreExpr
core_arrow,
CoreExpr -> DIdSet
exprFreeIdsDSet CoreExpr
core_arg DIdSet -> VarSet -> DIdSet
forall a b. UniqDSet a -> UniqSet b -> UniqDSet a
`uniqDSetIntersectUniqSet` VarSet
local_vars)
dsCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stack_ty :: Type
stack_ty res_ty :: Type
res_ty
(HsCmdArrApp arrow_ty :: XCmdArrApp GhcTc
arrow_ty arrow :: LHsExpr GhcTc
arrow arg :: LHsExpr GhcTc
arg HsHigherOrderApp _)
env_ids :: [Id]
env_ids = do
let
(a_arg_ty :: Type
a_arg_ty, _res_ty' :: Type
_res_ty') = Type -> (Type, Type)
tcSplitAppTy Type
XCmdArrApp GhcTc
arrow_ty
(_a_ty :: Type
_a_ty, arg_ty :: Type
arg_ty) = Type -> (Type, Type)
tcSplitAppTy Type
a_arg_ty
CoreExpr
core_arrow <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
arrow
CoreExpr
core_arg <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
arg
Id
stack_id <- Type -> DsM Id
newSysLocalDs Type
stack_ty
CoreExpr
core_make_pair <- [Id] -> Id -> CoreExpr -> DsM CoreExpr
matchEnvStack [Id]
env_ids Id
stack_id
(CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr CoreExpr
core_arrow CoreExpr
core_arg)
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids
([Id] -> Type -> Type
envStackType [Id]
env_ids Type
stack_ty)
(Type -> Type -> Type
mkCorePairTy Type
XCmdArrApp GhcTc
arrow_ty Type
arg_ty)
Type
res_ty
CoreExpr
core_make_pair
(DsCmdEnv -> Type -> Type -> CoreExpr
do_app DsCmdEnv
ids Type
arg_ty Type
res_ty),
([CoreExpr] -> DIdSet
exprsFreeIdsDSet [CoreExpr
core_arrow, CoreExpr
core_arg])
DIdSet -> VarSet -> DIdSet
forall a b. UniqDSet a -> UniqSet b -> UniqDSet a
`uniqDSetIntersectUniqSet` VarSet
local_vars)
dsCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stack_ty :: Type
stack_ty res_ty :: Type
res_ty (HsCmdApp _ cmd :: LHsCmd GhcTc
cmd arg :: LHsExpr GhcTc
arg) env_ids :: [Id]
env_ids = do
CoreExpr
core_arg <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
arg
let
arg_ty :: Type
arg_ty = CoreExpr -> Type
exprType CoreExpr
core_arg
stack_ty' :: Type
stack_ty' = Type -> Type -> Type
mkCorePairTy Type
arg_ty Type
stack_ty
(core_cmd :: CoreExpr
core_cmd, free_vars :: DIdSet
free_vars, env_ids' :: [Id]
env_ids')
<- DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmd DsCmdEnv
ids VarSet
local_vars Type
stack_ty' Type
res_ty LHsCmd GhcTc
cmd
Id
stack_id <- Type -> DsM Id
newSysLocalDs Type
stack_ty
Id
arg_id <- Type -> DsM Id
newSysLocalDsNoLP Type
arg_ty
let
stack' :: CoreExpr
stack' = CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
arg_id) (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
stack_id)
core_body :: CoreExpr
core_body = Id -> CoreExpr -> CoreExpr -> CoreExpr
bindNonRec Id
arg_id CoreExpr
core_arg
(CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr ([Id] -> CoreExpr
mkBigCoreVarTup [Id]
env_ids') CoreExpr
stack')
CoreExpr
core_map <- [Id] -> Id -> CoreExpr -> DsM CoreExpr
matchEnvStack [Id]
env_ids Id
stack_id CoreExpr
core_body
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids
([Id] -> Type -> Type
envStackType [Id]
env_ids Type
stack_ty)
([Id] -> Type -> Type
envStackType [Id]
env_ids' Type
stack_ty')
Type
res_ty
CoreExpr
core_map
CoreExpr
core_cmd,
DIdSet
free_vars DIdSet -> DIdSet -> DIdSet
`unionDVarSet`
(CoreExpr -> DIdSet
exprFreeIdsDSet CoreExpr
core_arg DIdSet -> VarSet -> DIdSet
forall a b. UniqDSet a -> UniqSet b -> UniqDSet a
`uniqDSetIntersectUniqSet` VarSet
local_vars))
dsCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stack_ty :: Type
stack_ty res_ty :: Type
res_ty
(HsCmdLam _ (MG { mg_alts :: forall p body. MatchGroup p body -> Located [LMatch p body]
mg_alts
= (Located [LMatch GhcTc (LHsCmd GhcTc)]
-> Located (SrcSpanLess (Located [LMatch GhcTc (LHsCmd GhcTc)]))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ [dL->L _ (Match { m_pats = pats
, m_grhss = GRHSs _ [dL->L _ (GRHS _ [] body)] _ })]) }))
env_ids :: [Id]
env_ids = do
let pat_vars :: VarSet
pat_vars = [Id] -> VarSet
mkVarSet ([LPat GhcTc] -> [Id]
collectPatsBinders [LPat GhcTc]
pats)
let
local_vars' :: VarSet
local_vars' = VarSet
pat_vars VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
local_vars
(pat_tys :: [Type]
pat_tys, stack_ty' :: Type
stack_ty') = Int -> Type -> ([Type], Type)
splitTypeAt ([LPat GhcTc] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LPat GhcTc]
pats) Type
stack_ty
(core_body :: CoreExpr
core_body, free_vars :: DIdSet
free_vars, env_ids' :: [Id]
env_ids')
<- DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmd DsCmdEnv
ids VarSet
local_vars' Type
stack_ty' Type
res_ty LHsCmd GhcTc
body
[Id]
param_ids <- (Type -> DsM Id) -> [Type] -> IOEnv (Env DsGblEnv DsLclEnv) [Id]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> DsM Id
newSysLocalDsNoLP [Type]
pat_tys
Id
stack_id' <- Type -> DsM Id
newSysLocalDs Type
stack_ty'
let
core_expr :: CoreExpr
core_expr = [Id] -> Id -> CoreExpr
buildEnvStack [Id]
env_ids' Id
stack_id'
in_ty :: Type
in_ty = [Id] -> Type -> Type
envStackType [Id]
env_ids Type
stack_ty
in_ty' :: Type
in_ty' = [Id] -> Type -> Type
envStackType [Id]
env_ids' Type
stack_ty'
CoreExpr
fail_expr <- HsMatchContext Id -> Type -> DsM CoreExpr
mkFailExpr HsMatchContext Id
forall id. HsMatchContext id
LambdaExpr Type
in_ty'
CoreExpr
match_code <- [CoreExpr]
-> HsMatchContext Name
-> [LPat GhcTc]
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimplys ((Id -> CoreExpr) -> [Id] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map Id -> CoreExpr
forall b. Id -> Expr b
Var [Id]
param_ids) HsMatchContext Name
forall id. HsMatchContext id
LambdaExpr [LPat GhcTc]
pats CoreExpr
core_expr
CoreExpr
fail_expr
(stack_id :: Id
stack_id, param_code :: CoreExpr
param_code) <- [Id] -> Id -> CoreExpr -> DsM (Id, CoreExpr)
matchVarStack [Id]
param_ids Id
stack_id' CoreExpr
match_code
CoreExpr
select_code <- [Id] -> Id -> CoreExpr -> DsM CoreExpr
matchEnvStack [Id]
env_ids Id
stack_id CoreExpr
param_code
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids Type
in_ty Type
in_ty' Type
res_ty CoreExpr
select_code CoreExpr
core_body,
DIdSet
free_vars DIdSet -> VarSet -> DIdSet
forall a b. UniqDSet a -> UniqSet b -> UniqDSet a
`uniqDSetMinusUniqSet` VarSet
pat_vars)
dsCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stack_ty :: Type
stack_ty res_ty :: Type
res_ty (HsCmdPar _ cmd :: LHsCmd GhcTc
cmd) env_ids :: [Id]
env_ids
= DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsLCmd DsCmdEnv
ids VarSet
local_vars Type
stack_ty Type
res_ty LHsCmd GhcTc
cmd [Id]
env_ids
dsCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stack_ty :: Type
stack_ty res_ty :: Type
res_ty (HsCmdIf _ mb_fun :: Maybe (SyntaxExpr GhcTc)
mb_fun cond :: LHsExpr GhcTc
cond then_cmd :: LHsCmd GhcTc
then_cmd else_cmd :: LHsCmd GhcTc
else_cmd)
env_ids :: [Id]
env_ids = do
CoreExpr
core_cond <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
cond
(core_then :: CoreExpr
core_then, fvs_then :: DIdSet
fvs_then, then_ids :: [Id]
then_ids)
<- DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmd DsCmdEnv
ids VarSet
local_vars Type
stack_ty Type
res_ty LHsCmd GhcTc
then_cmd
(core_else :: CoreExpr
core_else, fvs_else :: DIdSet
fvs_else, else_ids :: [Id]
else_ids)
<- DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmd DsCmdEnv
ids VarSet
local_vars Type
stack_ty Type
res_ty LHsCmd GhcTc
else_cmd
Id
stack_id <- Type -> DsM Id
newSysLocalDs Type
stack_ty
TyCon
either_con <- Name -> DsM TyCon
dsLookupTyCon Name
eitherTyConName
DataCon
left_con <- Name -> DsM DataCon
dsLookupDataCon Name
leftDataConName
DataCon
right_con <- Name -> DsM DataCon
dsLookupDataCon Name
rightDataConName
let mk_left_expr :: Type -> Type -> CoreExpr -> CoreExpr
mk_left_expr ty1 :: Type
ty1 ty2 :: Type
ty2 e :: CoreExpr
e = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
left_con [Type -> CoreExpr
forall b. Type -> Expr b
Type Type
ty1,Type -> CoreExpr
forall b. Type -> Expr b
Type Type
ty2, CoreExpr
e]
mk_right_expr :: Type -> Type -> CoreExpr -> CoreExpr
mk_right_expr ty1 :: Type
ty1 ty2 :: Type
ty2 e :: CoreExpr
e = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps DataCon
right_con [Type -> CoreExpr
forall b. Type -> Expr b
Type Type
ty1,Type -> CoreExpr
forall b. Type -> Expr b
Type Type
ty2, CoreExpr
e]
in_ty :: Type
in_ty = [Id] -> Type -> Type
envStackType [Id]
env_ids Type
stack_ty
then_ty :: Type
then_ty = [Id] -> Type -> Type
envStackType [Id]
then_ids Type
stack_ty
else_ty :: Type
else_ty = [Id] -> Type -> Type
envStackType [Id]
else_ids Type
stack_ty
sum_ty :: Type
sum_ty = TyCon -> [Type] -> Type
mkTyConApp TyCon
either_con [Type
then_ty, Type
else_ty]
fvs_cond :: DIdSet
fvs_cond = CoreExpr -> DIdSet
exprFreeIdsDSet CoreExpr
core_cond
DIdSet -> VarSet -> DIdSet
forall a b. UniqDSet a -> UniqSet b -> UniqDSet a
`uniqDSetIntersectUniqSet` VarSet
local_vars
core_left :: CoreExpr
core_left = Type -> Type -> CoreExpr -> CoreExpr
mk_left_expr Type
then_ty Type
else_ty
([Id] -> Id -> CoreExpr
buildEnvStack [Id]
then_ids Id
stack_id)
core_right :: CoreExpr
core_right = Type -> Type -> CoreExpr -> CoreExpr
mk_right_expr Type
then_ty Type
else_ty
([Id] -> Id -> CoreExpr
buildEnvStack [Id]
else_ids Id
stack_id)
CoreExpr
core_if <- case Maybe (SyntaxExpr GhcTc)
mb_fun of
Just fun :: SyntaxExpr GhcTc
fun -> do { CoreExpr
fun_apps <- SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
fun
[CoreExpr
core_cond, CoreExpr
core_left, CoreExpr
core_right]
; [Id] -> Id -> CoreExpr -> DsM CoreExpr
matchEnvStack [Id]
env_ids Id
stack_id CoreExpr
fun_apps }
Nothing -> [Id] -> Id -> CoreExpr -> DsM CoreExpr
matchEnvStack [Id]
env_ids Id
stack_id (CoreExpr -> DsM CoreExpr) -> CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$
CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
mkIfThenElse CoreExpr
core_cond CoreExpr
core_left CoreExpr
core_right
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids Type
in_ty Type
sum_ty Type
res_ty
CoreExpr
core_if
(DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_choice DsCmdEnv
ids Type
then_ty Type
else_ty Type
res_ty CoreExpr
core_then CoreExpr
core_else),
DIdSet
fvs_cond DIdSet -> DIdSet -> DIdSet
`unionDVarSet` DIdSet
fvs_then DIdSet -> DIdSet -> DIdSet
`unionDVarSet` DIdSet
fvs_else)
dsCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stack_ty :: Type
stack_ty res_ty :: Type
res_ty
(HsCmdCase _ exp :: LHsExpr GhcTc
exp (MG { mg_alts :: forall p body. MatchGroup p body -> Located [LMatch p body]
mg_alts = (Located [LMatch GhcTc (LHsCmd GhcTc)]
-> Located (SrcSpanLess (Located [LMatch GhcTc (LHsCmd GhcTc)]))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L l :: SrcSpan
l matches :: SrcSpanLess (Located [LMatch GhcTc (LHsCmd GhcTc)])
matches)
, mg_ext :: forall p body. MatchGroup p body -> XMG p body
mg_ext = MatchGroupTc arg_tys _
, mg_origin :: forall p body. MatchGroup p body -> Origin
mg_origin = Origin
origin }))
env_ids :: [Id]
env_ids = do
Id
stack_id <- Type -> DsM Id
newSysLocalDs Type
stack_ty
let
leaves :: [(LHsCmd GhcTc, VarSet)]
leaves = (LMatch GhcTc (LHsCmd GhcTc) -> [(LHsCmd GhcTc, VarSet)])
-> [LMatch GhcTc (LHsCmd GhcTc)] -> [(LHsCmd GhcTc, VarSet)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LMatch GhcTc (LHsCmd GhcTc) -> [(LHsCmd GhcTc, VarSet)]
forall (body :: * -> *).
LMatch GhcTc (Located (body GhcTc))
-> [(Located (body GhcTc), VarSet)]
leavesMatch [LMatch GhcTc (LHsCmd GhcTc)]
SrcSpanLess (Located [LMatch GhcTc (LHsCmd GhcTc)])
matches
make_branch :: (LHsCmd GhcTc, VarSet)
-> IOEnv (Env DsGblEnv DsLclEnv) ([LHsExpr GhcTc], Type, CoreExpr)
make_branch (leaf :: LHsCmd GhcTc
leaf, bound_vars :: VarSet
bound_vars) = do
(core_leaf :: CoreExpr
core_leaf, _fvs :: DIdSet
_fvs, leaf_ids :: [Id]
leaf_ids)
<- DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmd DsCmdEnv
ids (VarSet
bound_vars VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
local_vars) Type
stack_ty
Type
res_ty LHsCmd GhcTc
leaf
([LHsExpr GhcTc], Type, CoreExpr)
-> IOEnv (Env DsGblEnv DsLclEnv) ([LHsExpr GhcTc], Type, CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return ([[Id] -> Id -> LHsExpr GhcTc
mkHsEnvStackExpr [Id]
leaf_ids Id
stack_id],
[Id] -> Type -> Type
envStackType [Id]
leaf_ids Type
stack_ty,
CoreExpr
core_leaf)
[([LHsExpr GhcTc], Type, CoreExpr)]
branches <- ((LHsCmd GhcTc, VarSet)
-> IOEnv (Env DsGblEnv DsLclEnv) ([LHsExpr GhcTc], Type, CoreExpr))
-> [(LHsCmd GhcTc, VarSet)]
-> IOEnv
(Env DsGblEnv DsLclEnv) [([LHsExpr GhcTc], Type, CoreExpr)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (LHsCmd GhcTc, VarSet)
-> IOEnv (Env DsGblEnv DsLclEnv) ([LHsExpr GhcTc], Type, CoreExpr)
make_branch [(LHsCmd GhcTc, VarSet)]
leaves
TyCon
either_con <- Name -> DsM TyCon
dsLookupTyCon Name
eitherTyConName
DataCon
left_con <- Name -> DsM DataCon
dsLookupDataCon Name
leftDataConName
DataCon
right_con <- Name -> DsM DataCon
dsLookupDataCon Name
rightDataConName
let
left_id :: HsExpr GhcTc
left_id = XConLikeOut GhcTc -> ConLike -> HsExpr GhcTc
forall p. XConLikeOut p -> ConLike -> HsExpr p
HsConLikeOut XConLikeOut GhcTc
NoExt
noExt (DataCon -> ConLike
RealDataCon DataCon
left_con)
right_id :: HsExpr GhcTc
right_id = XConLikeOut GhcTc -> ConLike -> HsExpr GhcTc
forall p. XConLikeOut p -> ConLike -> HsExpr p
HsConLikeOut XConLikeOut GhcTc
NoExt
noExt (DataCon -> ConLike
RealDataCon DataCon
right_con)
left_expr :: Type -> Type -> LHsExpr GhcTc -> LHsExpr GhcTc
left_expr ty1 :: Type
ty1 ty2 :: Type
ty2 e :: LHsExpr GhcTc
e = SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc)
-> SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a b. (a -> b) -> a -> b
$ XApp GhcTc -> LHsExpr GhcTc -> LHsExpr GhcTc -> HsExpr GhcTc
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp XApp GhcTc
NoExt
noExt
(SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc)
-> SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a b. (a -> b) -> a -> b
$ HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
forall (id :: Pass).
HsWrapper -> HsExpr (GhcPass id) -> HsExpr (GhcPass id)
mkHsWrap ([Type] -> HsWrapper
mkWpTyApps [Type
ty1, Type
ty2]) HsExpr GhcTc
left_id ) LHsExpr GhcTc
e
right_expr :: Type -> Type -> LHsExpr GhcTc -> LHsExpr GhcTc
right_expr ty1 :: Type
ty1 ty2 :: Type
ty2 e :: LHsExpr GhcTc
e = SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc)
-> SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a b. (a -> b) -> a -> b
$ XApp GhcTc -> LHsExpr GhcTc -> LHsExpr GhcTc -> HsExpr GhcTc
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp XApp GhcTc
NoExt
noExt
(SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc)
-> SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a b. (a -> b) -> a -> b
$ HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
forall (id :: Pass).
HsWrapper -> HsExpr (GhcPass id) -> HsExpr (GhcPass id)
mkHsWrap ([Type] -> HsWrapper
mkWpTyApps [Type
ty1, Type
ty2]) HsExpr GhcTc
right_id) LHsExpr GhcTc
e
merge_branches :: ([LHsExpr GhcTc], Type, CoreExpr)
-> ([LHsExpr GhcTc], Type, CoreExpr)
-> ([LHsExpr GhcTc], Type, CoreExpr)
merge_branches (builds1 :: [LHsExpr GhcTc]
builds1, in_ty1 :: Type
in_ty1, core_exp1 :: CoreExpr
core_exp1)
(builds2 :: [LHsExpr GhcTc]
builds2, in_ty2 :: Type
in_ty2, core_exp2 :: CoreExpr
core_exp2)
= ((LHsExpr GhcTc -> LHsExpr GhcTc)
-> [LHsExpr GhcTc] -> [LHsExpr GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map (Type -> Type -> LHsExpr GhcTc -> LHsExpr GhcTc
left_expr Type
in_ty1 Type
in_ty2) [LHsExpr GhcTc]
builds1 [LHsExpr GhcTc] -> [LHsExpr GhcTc] -> [LHsExpr GhcTc]
forall a. [a] -> [a] -> [a]
++
(LHsExpr GhcTc -> LHsExpr GhcTc)
-> [LHsExpr GhcTc] -> [LHsExpr GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map (Type -> Type -> LHsExpr GhcTc -> LHsExpr GhcTc
right_expr Type
in_ty1 Type
in_ty2) [LHsExpr GhcTc]
builds2,
TyCon -> [Type] -> Type
mkTyConApp TyCon
either_con [Type
in_ty1, Type
in_ty2],
DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_choice DsCmdEnv
ids Type
in_ty1 Type
in_ty2 Type
res_ty CoreExpr
core_exp1 CoreExpr
core_exp2)
(leaves' :: [LHsExpr GhcTc]
leaves', sum_ty :: Type
sum_ty, core_choices :: CoreExpr
core_choices) = (([LHsExpr GhcTc], Type, CoreExpr)
-> ([LHsExpr GhcTc], Type, CoreExpr)
-> ([LHsExpr GhcTc], Type, CoreExpr))
-> [([LHsExpr GhcTc], Type, CoreExpr)]
-> ([LHsExpr GhcTc], Type, CoreExpr)
forall a. (a -> a -> a) -> [a] -> a
foldb ([LHsExpr GhcTc], Type, CoreExpr)
-> ([LHsExpr GhcTc], Type, CoreExpr)
-> ([LHsExpr GhcTc], Type, CoreExpr)
merge_branches [([LHsExpr GhcTc], Type, CoreExpr)]
branches
(_, matches' :: [LMatch GhcTc (LHsExpr GhcTc)]
matches') = ([LHsExpr GhcTc]
-> LMatch GhcTc (LHsCmd GhcTc)
-> ([LHsExpr GhcTc], LMatch GhcTc (LHsExpr GhcTc)))
-> [LHsExpr GhcTc]
-> [LMatch GhcTc (LHsCmd GhcTc)]
-> ([LHsExpr GhcTc], [LMatch GhcTc (LHsExpr GhcTc)])
forall (t :: * -> *) a b c.
Traversable t =>
(a -> b -> (a, c)) -> a -> t b -> (a, t c)
mapAccumL (Type
-> [LHsExpr GhcTc]
-> LMatch GhcTc (LHsCmd GhcTc)
-> ([LHsExpr GhcTc], LMatch GhcTc (LHsExpr GhcTc))
forall (body' :: * -> *) (body :: * -> *).
Type
-> [Located (body' GhcTc)]
-> LMatch GhcTc (Located (body GhcTc))
-> ([Located (body' GhcTc)], LMatch GhcTc (Located (body' GhcTc)))
replaceLeavesMatch Type
res_ty) [LHsExpr GhcTc]
leaves' [LMatch GhcTc (LHsCmd GhcTc)]
SrcSpanLess (Located [LMatch GhcTc (LHsCmd GhcTc)])
matches
in_ty :: Type
in_ty = [Id] -> Type -> Type
envStackType [Id]
env_ids Type
stack_ty
CoreExpr
core_body <- HsExpr GhcTc -> DsM CoreExpr
dsExpr (XCase GhcTc
-> LHsExpr GhcTc
-> MatchGroup GhcTc (LHsExpr GhcTc)
-> HsExpr GhcTc
forall p.
XCase p -> LHsExpr p -> MatchGroup p (LHsExpr p) -> HsExpr p
HsCase XCase GhcTc
NoExt
noExt LHsExpr GhcTc
exp
(MG :: forall p body.
XMG p body
-> Located [LMatch p body] -> Origin -> MatchGroup p body
MG { mg_alts :: Located [LMatch GhcTc (LHsExpr GhcTc)]
mg_alts = SrcSpan
-> SrcSpanLess (Located [LMatch GhcTc (LHsExpr GhcTc)])
-> Located [LMatch GhcTc (LHsExpr GhcTc)]
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
l [LMatch GhcTc (LHsExpr GhcTc)]
SrcSpanLess (Located [LMatch GhcTc (LHsExpr GhcTc)])
matches'
, mg_ext :: XMG GhcTc (LHsExpr GhcTc)
mg_ext = [Type] -> Type -> MatchGroupTc
MatchGroupTc [Type]
arg_tys Type
sum_ty
, mg_origin :: Origin
mg_origin = Origin
origin }))
CoreExpr
core_matches <- [Id] -> Id -> CoreExpr -> DsM CoreExpr
matchEnvStack [Id]
env_ids Id
stack_id CoreExpr
core_body
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids Type
in_ty Type
sum_ty Type
res_ty CoreExpr
core_matches CoreExpr
core_choices,
CoreExpr -> DIdSet
exprFreeIdsDSet CoreExpr
core_body DIdSet -> VarSet -> DIdSet
forall a b. UniqDSet a -> UniqSet b -> UniqDSet a
`uniqDSetIntersectUniqSet` VarSet
local_vars)
dsCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stack_ty :: Type
stack_ty res_ty :: Type
res_ty (HsCmdLet _ lbinds :: LHsLocalBinds GhcTc
lbinds@(LHsLocalBinds GhcTc -> Located (SrcSpanLess (LHsLocalBinds GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ binds :: SrcSpanLess (LHsLocalBinds GhcTc)
binds) body :: LHsCmd GhcTc
body)
env_ids :: [Id]
env_ids = do
let
defined_vars :: VarSet
defined_vars = [Id] -> VarSet
mkVarSet (HsLocalBindsLR GhcTc GhcTc -> [IdP GhcTc]
forall (idL :: Pass) (idR :: Pass).
HsLocalBindsLR (GhcPass idL) (GhcPass idR) -> [IdP (GhcPass idL)]
collectLocalBinders SrcSpanLess (LHsLocalBinds GhcTc)
HsLocalBindsLR GhcTc GhcTc
binds)
local_vars' :: VarSet
local_vars' = VarSet
defined_vars VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
local_vars
(core_body :: CoreExpr
core_body, _free_vars :: DIdSet
_free_vars, env_ids' :: [Id]
env_ids')
<- DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmd DsCmdEnv
ids VarSet
local_vars' Type
stack_ty Type
res_ty LHsCmd GhcTc
body
Id
stack_id <- Type -> DsM Id
newSysLocalDs Type
stack_ty
CoreExpr
core_binds <- LHsLocalBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds LHsLocalBinds GhcTc
lbinds ([Id] -> Id -> CoreExpr
buildEnvStack [Id]
env_ids' Id
stack_id)
CoreExpr
core_map <- [Id] -> Id -> CoreExpr -> DsM CoreExpr
matchEnvStack [Id]
env_ids Id
stack_id CoreExpr
core_binds
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids
([Id] -> Type -> Type
envStackType [Id]
env_ids Type
stack_ty)
([Id] -> Type -> Type
envStackType [Id]
env_ids' Type
stack_ty)
Type
res_ty
CoreExpr
core_map
CoreExpr
core_body,
CoreExpr -> DIdSet
exprFreeIdsDSet CoreExpr
core_binds DIdSet -> VarSet -> DIdSet
forall a b. UniqDSet a -> UniqSet b -> UniqDSet a
`uniqDSetIntersectUniqSet` VarSet
local_vars)
dsCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stack_ty :: Type
stack_ty res_ty :: Type
res_ty do_block :: HsCmd GhcTc
do_block@(HsCmdDo stmts_ty :: XCmdDo GhcTc
stmts_ty
(Located [CmdLStmt GhcTc]
-> Located (SrcSpanLess (Located [CmdLStmt GhcTc]))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc stmts :: SrcSpanLess (Located [CmdLStmt GhcTc])
stmts))
env_ids :: [Id]
env_ids = do
SrcSpan -> DsM () -> DsM ()
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc (DsM () -> DsM ()) -> DsM () -> DsM ()
forall a b. (a -> b) -> a -> b
$
Type -> SDoc -> DsM ()
dsNoLevPoly Type
XCmdDo GhcTc
stmts_ty
(String -> SDoc
text "In the do-command:" SDoc -> SDoc -> SDoc
<+> HsCmd GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsCmd GhcTc
do_block)
(core_stmts :: CoreExpr
core_stmts, env_ids' :: DIdSet
env_ids') <- DsCmdEnv
-> VarSet
-> Type
-> [CmdLStmt GhcTc]
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmdDo DsCmdEnv
ids VarSet
local_vars Type
res_ty [CmdLStmt GhcTc]
SrcSpanLess (Located [CmdLStmt GhcTc])
stmts [Id]
env_ids
let env_ty :: Type
env_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids
CoreExpr
core_fst <- Type -> Type -> DsM CoreExpr
mkFstExpr Type
env_ty Type
stack_ty
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids
(Type -> Type -> Type
mkCorePairTy Type
env_ty Type
stack_ty)
Type
env_ty
Type
res_ty
CoreExpr
core_fst
CoreExpr
core_stmts,
DIdSet
env_ids')
dsCmd _ local_vars :: VarSet
local_vars _stack_ty :: Type
_stack_ty _res_ty :: Type
_res_ty (HsCmdArrForm _ op :: LHsExpr GhcTc
op _ _ args :: [LHsCmdTop GhcTc]
args) env_ids :: [Id]
env_ids = do
let env_ty :: Type
env_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids
CoreExpr
core_op <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
op
(core_args :: [CoreExpr]
core_args, fv_sets :: [DIdSet]
fv_sets) <- (LHsCmdTop GhcTc -> DsM (CoreExpr, DIdSet))
-> [LHsCmdTop GhcTc]
-> IOEnv (Env DsGblEnv DsLclEnv) ([CoreExpr], [DIdSet])
forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM (VarSet -> [Id] -> LHsCmdTop GhcTc -> DsM (CoreExpr, DIdSet)
dsTrimCmdArg VarSet
local_vars [Id]
env_ids) [LHsCmdTop GhcTc]
args
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps (CoreExpr -> CoreExpr -> CoreExpr
forall b. Expr b -> Expr b -> Expr b
App CoreExpr
core_op (Type -> CoreExpr
forall b. Type -> Expr b
Type Type
env_ty)) [CoreExpr]
core_args,
[DIdSet] -> DIdSet
unionDVarSets [DIdSet]
fv_sets)
dsCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stack_ty :: Type
stack_ty res_ty :: Type
res_ty (HsCmdWrap _ wrap :: HsWrapper
wrap cmd :: HsCmd GhcTc
cmd) env_ids :: [Id]
env_ids = do
(core_cmd :: CoreExpr
core_cmd, env_ids' :: DIdSet
env_ids') <- DsCmdEnv
-> VarSet
-> Type
-> Type
-> HsCmd GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmd DsCmdEnv
ids VarSet
local_vars Type
stack_ty Type
res_ty HsCmd GhcTc
cmd [Id]
env_ids
CoreExpr -> CoreExpr
core_wrap <- HsWrapper -> DsM (CoreExpr -> CoreExpr)
dsHsWrapper HsWrapper
wrap
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> CoreExpr
core_wrap CoreExpr
core_cmd, DIdSet
env_ids')
dsCmd _ _ _ _ _ c :: [Id]
c = String -> SDoc -> DsM (CoreExpr, DIdSet)
forall a. HasCallStack => String -> SDoc -> a
pprPanic "dsCmd" ([Id] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Id]
c)
dsTrimCmdArg
:: IdSet
-> [Id]
-> LHsCmdTop GhcTc
-> DsM (CoreExpr,
DIdSet)
dsTrimCmdArg :: VarSet -> [Id] -> LHsCmdTop GhcTc -> DsM (CoreExpr, DIdSet)
dsTrimCmdArg local_vars :: VarSet
local_vars env_ids :: [Id]
env_ids
(LHsCmdTop GhcTc -> Located (SrcSpanLess (LHsCmdTop GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (HsCmdTop
(CmdTopTc stack_ty cmd_ty ids) cmd )) = do
(meth_binds :: [CoreBind]
meth_binds, meth_ids :: DsCmdEnv
meth_ids) <- CmdSyntaxTable GhcTc -> DsM ([CoreBind], DsCmdEnv)
mkCmdEnv CmdSyntaxTable GhcTc
ids
(core_cmd :: CoreExpr
core_cmd, free_vars :: DIdSet
free_vars, env_ids' :: [Id]
env_ids')
<- DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmd DsCmdEnv
meth_ids VarSet
local_vars Type
stack_ty Type
cmd_ty LHsCmd GhcTc
cmd
Id
stack_id <- Type -> DsM Id
newSysLocalDs Type
stack_ty
CoreExpr
trim_code
<- [Id] -> Id -> CoreExpr -> DsM CoreExpr
matchEnvStack [Id]
env_ids Id
stack_id ([Id] -> Id -> CoreExpr
buildEnvStack [Id]
env_ids' Id
stack_id)
let
in_ty :: Type
in_ty = [Id] -> Type -> Type
envStackType [Id]
env_ids Type
stack_ty
in_ty' :: Type
in_ty' = [Id] -> Type -> Type
envStackType [Id]
env_ids' Type
stack_ty
arg_code :: CoreExpr
arg_code = if [Id]
env_ids' [Id] -> [Id] -> Bool
forall a. Eq a => a -> a -> Bool
== [Id]
env_ids then CoreExpr
core_cmd else
DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
meth_ids Type
in_ty Type
in_ty' Type
cmd_ty CoreExpr
trim_code CoreExpr
core_cmd
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return ([CoreBind] -> CoreExpr -> CoreExpr
forall b. [Bind b] -> Expr b -> Expr b
mkLets [CoreBind]
meth_binds CoreExpr
arg_code, DIdSet
free_vars)
dsTrimCmdArg _ _ _ = String -> DsM (CoreExpr, DIdSet)
forall a. String -> a
panic "dsTrimCmdArg"
dsfixCmd
:: DsCmdEnv
-> IdSet
-> Type
-> Type
-> LHsCmd GhcTc
-> DsM (CoreExpr,
DIdSet,
[Id])
dsfixCmd :: DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stk_ty :: Type
stk_ty cmd_ty :: Type
cmd_ty cmd :: LHsCmd GhcTc
cmd
= do { SrcSpan -> DsM () -> DsM ()
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs (LHsCmd GhcTc -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
getLoc LHsCmd GhcTc
cmd) (DsM () -> DsM ()) -> DsM () -> DsM ()
forall a b. (a -> b) -> a -> b
$ Type -> SDoc -> DsM ()
dsNoLevPoly Type
cmd_ty
(String -> SDoc
text "When desugaring the command:" SDoc -> SDoc -> SDoc
<+> LHsCmd GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsCmd GhcTc
cmd)
; ([Id] -> DsM (CoreExpr, DIdSet)) -> DsM (CoreExpr, DIdSet, [Id])
trimInput (DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsLCmd DsCmdEnv
ids VarSet
local_vars Type
stk_ty Type
cmd_ty LHsCmd GhcTc
cmd) }
trimInput
:: ([Id] -> DsM (CoreExpr, DIdSet))
-> DsM (CoreExpr,
DIdSet,
[Id])
trimInput :: ([Id] -> DsM (CoreExpr, DIdSet)) -> DsM (CoreExpr, DIdSet, [Id])
trimInput build_arrow :: [Id] -> DsM (CoreExpr, DIdSet)
build_arrow
= ((CoreExpr, DIdSet, [Id]) -> DsM (CoreExpr, DIdSet, [Id]))
-> DsM (CoreExpr, DIdSet, [Id])
forall a. (a -> DsM a) -> DsM a
fixDs (\ ~(_,_,env_ids :: [Id]
env_ids) -> do
(core_cmd :: CoreExpr
core_cmd, free_vars :: DIdSet
free_vars) <- [Id] -> DsM (CoreExpr, DIdSet)
build_arrow [Id]
env_ids
(CoreExpr, DIdSet, [Id]) -> DsM (CoreExpr, DIdSet, [Id])
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr
core_cmd, DIdSet
free_vars, DIdSet -> [Id]
dVarSetElems DIdSet
free_vars))
dsCmdDo :: DsCmdEnv
-> IdSet
-> Type
-> [CmdLStmt GhcTc]
-> [Id]
-> DsM (CoreExpr,
DIdSet)
dsCmdDo :: DsCmdEnv
-> VarSet
-> Type
-> [CmdLStmt GhcTc]
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmdDo _ _ _ [] _ = String -> DsM (CoreExpr, DIdSet)
forall a. String -> a
panic "dsCmdDo"
dsCmdDo ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars res_ty :: Type
res_ty [CmdLStmt GhcTc -> Located (SrcSpanLess (CmdLStmt GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc (LastStmt _ body _ _)] env_ids :: [Id]
env_ids = do
SrcSpan -> DsM () -> DsM ()
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc (DsM () -> DsM ()) -> DsM () -> DsM ()
forall a b. (a -> b) -> a -> b
$ Type -> SDoc -> DsM ()
dsNoLevPoly Type
res_ty
(String -> SDoc
text "In the command:" SDoc -> SDoc -> SDoc
<+> LHsCmd GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsCmd GhcTc
body)
(core_body :: CoreExpr
core_body, env_ids' :: DIdSet
env_ids') <- DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsLCmd DsCmdEnv
ids VarSet
local_vars Type
unitTy Type
res_ty LHsCmd GhcTc
body [Id]
env_ids
let env_ty :: Type
env_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids
Id
env_var <- Type -> DsM Id
newSysLocalDs Type
env_ty
let core_map :: CoreExpr
core_map = Id -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Id
env_var (CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
env_var) CoreExpr
mkCoreUnitExpr)
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids
Type
env_ty
(Type -> Type -> Type
mkCorePairTy Type
env_ty Type
unitTy)
Type
res_ty
CoreExpr
core_map
CoreExpr
core_body,
DIdSet
env_ids')
dsCmdDo ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars res_ty :: Type
res_ty (stmt :: CmdLStmt GhcTc
stmt:stmts :: [CmdLStmt GhcTc]
stmts) env_ids :: [Id]
env_ids = do
let bound_vars :: VarSet
bound_vars = [Id] -> VarSet
mkVarSet (CmdLStmt GhcTc -> [Id]
forall body. LStmt GhcTc body -> [Id]
collectLStmtBinders CmdLStmt GhcTc
stmt)
let local_vars' :: VarSet
local_vars' = VarSet
bound_vars VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
local_vars
(core_stmts :: CoreExpr
core_stmts, _, env_ids' :: [Id]
env_ids') <- ([Id] -> DsM (CoreExpr, DIdSet)) -> DsM (CoreExpr, DIdSet, [Id])
trimInput (DsCmdEnv
-> VarSet
-> Type
-> [CmdLStmt GhcTc]
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmdDo DsCmdEnv
ids VarSet
local_vars' Type
res_ty [CmdLStmt GhcTc]
stmts)
(core_stmt :: CoreExpr
core_stmt, fv_stmt :: DIdSet
fv_stmt) <- DsCmdEnv
-> VarSet
-> [Id]
-> CmdLStmt GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmdLStmt DsCmdEnv
ids VarSet
local_vars [Id]
env_ids' CmdLStmt GhcTc
stmt [Id]
env_ids
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_compose DsCmdEnv
ids
([Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids)
([Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids')
Type
res_ty
CoreExpr
core_stmt
CoreExpr
core_stmts,
DIdSet
fv_stmt)
dsCmdLStmt :: DsCmdEnv -> IdSet -> [Id] -> CmdLStmt GhcTc -> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmdLStmt :: DsCmdEnv
-> VarSet
-> [Id]
-> CmdLStmt GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmdLStmt ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars out_ids :: [Id]
out_ids cmd :: CmdLStmt GhcTc
cmd env_ids :: [Id]
env_ids
= DsCmdEnv
-> VarSet
-> [Id]
-> CmdStmt GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmdStmt DsCmdEnv
ids VarSet
local_vars [Id]
out_ids (CmdLStmt GhcTc -> SrcSpanLess (CmdLStmt GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc CmdLStmt GhcTc
cmd) [Id]
env_ids
dsCmdStmt
:: DsCmdEnv
-> IdSet
-> [Id]
-> CmdStmt GhcTc
-> [Id]
-> DsM (CoreExpr,
DIdSet)
dsCmdStmt :: DsCmdEnv
-> VarSet
-> [Id]
-> CmdStmt GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmdStmt ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars out_ids :: [Id]
out_ids (BodyStmt c_ty :: XBodyStmt GhcTc GhcTc (LHsCmd GhcTc)
c_ty cmd :: LHsCmd GhcTc
cmd _ _) env_ids :: [Id]
env_ids = do
(core_cmd :: CoreExpr
core_cmd, fv_cmd :: DIdSet
fv_cmd, env_ids1 :: [Id]
env_ids1) <- DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmd DsCmdEnv
ids VarSet
local_vars Type
unitTy Type
XBodyStmt GhcTc GhcTc (LHsCmd GhcTc)
c_ty LHsCmd GhcTc
cmd
CoreExpr
core_mux <- [Id] -> CoreExpr -> DsM CoreExpr
matchEnv [Id]
env_ids
(CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr
(CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr ([Id] -> CoreExpr
mkBigCoreVarTup [Id]
env_ids1) CoreExpr
mkCoreUnitExpr)
([Id] -> CoreExpr
mkBigCoreVarTup [Id]
out_ids))
let
in_ty :: Type
in_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids
in_ty1 :: Type
in_ty1 = Type -> Type -> Type
mkCorePairTy ([Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids1) Type
unitTy
out_ty :: Type
out_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
out_ids
before_c_ty :: Type
before_c_ty = Type -> Type -> Type
mkCorePairTy Type
in_ty1 Type
out_ty
after_c_ty :: Type
after_c_ty = Type -> Type -> Type
mkCorePairTy Type
XBodyStmt GhcTc GhcTc (LHsCmd GhcTc)
c_ty Type
out_ty
Type -> SDoc -> DsM ()
dsNoLevPoly Type
XBodyStmt GhcTc GhcTc (LHsCmd GhcTc)
c_ty SDoc
empty
CoreExpr
snd_fn <- Type -> Type -> DsM CoreExpr
mkSndExpr Type
XBodyStmt GhcTc GhcTc (LHsCmd GhcTc)
c_ty Type
out_ty
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids Type
in_ty Type
before_c_ty Type
out_ty CoreExpr
core_mux (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall a b. (a -> b) -> a -> b
$
DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_compose DsCmdEnv
ids Type
before_c_ty Type
after_c_ty Type
out_ty
(DsCmdEnv -> Type -> Type -> Type -> CoreExpr -> CoreExpr
do_first DsCmdEnv
ids Type
in_ty1 Type
XBodyStmt GhcTc GhcTc (LHsCmd GhcTc)
c_ty Type
out_ty CoreExpr
core_cmd) (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall a b. (a -> b) -> a -> b
$
DsCmdEnv -> Type -> Type -> CoreExpr -> CoreExpr
do_arr DsCmdEnv
ids Type
after_c_ty Type
out_ty CoreExpr
snd_fn,
DIdSet -> [Id] -> DIdSet
extendDVarSetList DIdSet
fv_cmd [Id]
out_ids)
dsCmdStmt ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars out_ids :: [Id]
out_ids (BindStmt _ pat :: LPat GhcTc
pat cmd :: LHsCmd GhcTc
cmd _ _) env_ids :: [Id]
env_ids = do
let pat_ty :: Type
pat_ty = LPat GhcTc -> Type
hsLPatType LPat GhcTc
pat
(core_cmd :: CoreExpr
core_cmd, fv_cmd :: DIdSet
fv_cmd, env_ids1 :: [Id]
env_ids1) <- DsCmdEnv
-> VarSet
-> Type
-> Type
-> LHsCmd GhcTc
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmd DsCmdEnv
ids VarSet
local_vars Type
unitTy Type
pat_ty LHsCmd GhcTc
cmd
let pat_vars :: VarSet
pat_vars = [Id] -> VarSet
mkVarSet (LPat GhcTc -> [Id]
collectPatBinders LPat GhcTc
pat)
let
env_ids2 :: [Id]
env_ids2 = (Id -> Bool) -> [Id] -> [Id]
forall a. (a -> Bool) -> [a] -> [a]
filterOut (Id -> VarSet -> Bool
`elemVarSet` VarSet
pat_vars) [Id]
out_ids
env_ty2 :: Type
env_ty2 = [Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids2
CoreExpr
core_mux <- [Id] -> CoreExpr -> DsM CoreExpr
matchEnv [Id]
env_ids
(CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr
(CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr ([Id] -> CoreExpr
mkBigCoreVarTup [Id]
env_ids1) CoreExpr
mkCoreUnitExpr)
([Id] -> CoreExpr
mkBigCoreVarTup [Id]
env_ids2))
Id
env_id <- Type -> DsM Id
newSysLocalDs Type
env_ty2
UniqSupply
uniqs <- TcRnIf DsGblEnv DsLclEnv UniqSupply
forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply
let
after_c_ty :: Type
after_c_ty = Type -> Type -> Type
mkCorePairTy Type
pat_ty Type
env_ty2
out_ty :: Type
out_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
out_ids
body_expr :: CoreExpr
body_expr = UniqSupply -> Id -> [Id] -> CoreExpr -> CoreExpr
coreCaseTuple UniqSupply
uniqs Id
env_id [Id]
env_ids2 ([Id] -> CoreExpr
mkBigCoreVarTup [Id]
out_ids)
CoreExpr
fail_expr <- HsMatchContext Id -> Type -> DsM CoreExpr
mkFailExpr (HsStmtContext Id -> HsMatchContext Id
forall id. HsStmtContext id -> HsMatchContext id
StmtCtxt HsStmtContext Id
forall id. HsStmtContext id
DoExpr) Type
out_ty
Id
pat_id <- LPat GhcTc -> DsM Id
selectSimpleMatchVarL LPat GhcTc
pat
CoreExpr
match_code
<- CoreExpr
-> HsMatchContext Name
-> LPat GhcTc
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimply (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
pat_id) (HsStmtContext Name -> HsMatchContext Name
forall id. HsStmtContext id -> HsMatchContext id
StmtCtxt HsStmtContext Name
forall id. HsStmtContext id
DoExpr) LPat GhcTc
pat CoreExpr
body_expr CoreExpr
fail_expr
Id
pair_id <- Type -> DsM Id
newSysLocalDs Type
after_c_ty
let
proj_expr :: CoreExpr
proj_expr = Id -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Id
pair_id (Id -> Id -> Id -> CoreExpr -> CoreExpr
coreCasePair Id
pair_id Id
pat_id Id
env_id CoreExpr
match_code)
let
in_ty :: Type
in_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids
in_ty1 :: Type
in_ty1 = Type -> Type -> Type
mkCorePairTy ([Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids1) Type
unitTy
in_ty2 :: Type
in_ty2 = [Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids2
before_c_ty :: Type
before_c_ty = Type -> Type -> Type
mkCorePairTy Type
in_ty1 Type
in_ty2
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids Type
in_ty Type
before_c_ty Type
out_ty CoreExpr
core_mux (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall a b. (a -> b) -> a -> b
$
DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_compose DsCmdEnv
ids Type
before_c_ty Type
after_c_ty Type
out_ty
(DsCmdEnv -> Type -> Type -> Type -> CoreExpr -> CoreExpr
do_first DsCmdEnv
ids Type
in_ty1 Type
pat_ty Type
in_ty2 CoreExpr
core_cmd) (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall a b. (a -> b) -> a -> b
$
DsCmdEnv -> Type -> Type -> CoreExpr -> CoreExpr
do_arr DsCmdEnv
ids Type
after_c_ty Type
out_ty CoreExpr
proj_expr,
DIdSet
fv_cmd DIdSet -> DIdSet -> DIdSet
`unionDVarSet` ([Id] -> DIdSet
mkDVarSet [Id]
out_ids
DIdSet -> VarSet -> DIdSet
forall a b. UniqDSet a -> UniqSet b -> UniqDSet a
`uniqDSetMinusUniqSet` VarSet
pat_vars))
dsCmdStmt ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars out_ids :: [Id]
out_ids (LetStmt _ binds :: LHsLocalBinds GhcTc
binds) env_ids :: [Id]
env_ids = do
CoreExpr
core_binds <- LHsLocalBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds LHsLocalBinds GhcTc
binds ([Id] -> CoreExpr
mkBigCoreVarTup [Id]
out_ids)
CoreExpr
core_map <- [Id] -> CoreExpr -> DsM CoreExpr
matchEnv [Id]
env_ids CoreExpr
core_binds
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv -> Type -> Type -> CoreExpr -> CoreExpr
do_arr DsCmdEnv
ids
([Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids)
([Id] -> Type
mkBigCoreVarTupTy [Id]
out_ids)
CoreExpr
core_map,
CoreExpr -> DIdSet
exprFreeIdsDSet CoreExpr
core_binds DIdSet -> VarSet -> DIdSet
forall a b. UniqDSet a -> UniqSet b -> UniqDSet a
`uniqDSetIntersectUniqSet` VarSet
local_vars)
dsCmdStmt ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars out_ids :: [Id]
out_ids
(RecStmt { recS_stmts :: forall idL idR body. StmtLR idL idR body -> [LStmtLR idL idR body]
recS_stmts = [CmdLStmt GhcTc]
stmts
, recS_later_ids :: forall idL idR body. StmtLR idL idR body -> [IdP idR]
recS_later_ids = [IdP GhcTc]
later_ids, recS_rec_ids :: forall idL idR body. StmtLR idL idR body -> [IdP idR]
recS_rec_ids = [IdP GhcTc]
rec_ids
, recS_ext :: forall idL idR body. StmtLR idL idR body -> XRecStmt idL idR body
recS_ext = RecStmtTc { recS_later_rets = later_rets
, recS_rec_rets = rec_rets } })
env_ids :: [Id]
env_ids = do
let
later_ids_set :: VarSet
later_ids_set = [Id] -> VarSet
mkVarSet [Id]
[IdP GhcTc]
later_ids
env2_ids :: [Id]
env2_ids = (Id -> Bool) -> [Id] -> [Id]
forall a. (a -> Bool) -> [a] -> [a]
filterOut (Id -> VarSet -> Bool
`elemVarSet` VarSet
later_ids_set) [Id]
out_ids
env2_id_set :: DIdSet
env2_id_set = [Id] -> DIdSet
mkDVarSet [Id]
env2_ids
env2_ty :: Type
env2_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
env2_ids
UniqSupply
uniqs <- TcRnIf DsGblEnv DsLclEnv UniqSupply
forall gbl lcl. TcRnIf gbl lcl UniqSupply
newUniqueSupply
Id
env2_id <- Type -> DsM Id
newSysLocalDs Type
env2_ty
let
later_ty :: Type
later_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
[IdP GhcTc]
later_ids
post_pair_ty :: Type
post_pair_ty = Type -> Type -> Type
mkCorePairTy Type
later_ty Type
env2_ty
post_loop_body :: CoreExpr
post_loop_body = UniqSupply -> Id -> [Id] -> CoreExpr -> CoreExpr
coreCaseTuple UniqSupply
uniqs Id
env2_id [Id]
env2_ids ([Id] -> CoreExpr
mkBigCoreVarTup [Id]
out_ids)
CoreExpr
post_loop_fn <- [Id] -> Id -> CoreExpr -> DsM CoreExpr
matchEnvStack [Id]
[IdP GhcTc]
later_ids Id
env2_id CoreExpr
post_loop_body
(core_loop :: CoreExpr
core_loop, env1_id_set :: DIdSet
env1_id_set, env1_ids :: [Id]
env1_ids)
<- DsCmdEnv
-> VarSet
-> [CmdLStmt GhcTc]
-> [Id]
-> [HsExpr GhcTc]
-> [Id]
-> [HsExpr GhcTc]
-> DsM (CoreExpr, DIdSet, [Id])
dsRecCmd DsCmdEnv
ids VarSet
local_vars [CmdLStmt GhcTc]
stmts [Id]
[IdP GhcTc]
later_ids [HsExpr GhcTc]
later_rets [Id]
[IdP GhcTc]
rec_ids [HsExpr GhcTc]
rec_rets
let
env1_ty :: Type
env1_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
env1_ids
pre_pair_ty :: Type
pre_pair_ty = Type -> Type -> Type
mkCorePairTy Type
env1_ty Type
env2_ty
pre_loop_body :: CoreExpr
pre_loop_body = CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr ([Id] -> CoreExpr
mkBigCoreVarTup [Id]
env1_ids)
([Id] -> CoreExpr
mkBigCoreVarTup [Id]
env2_ids)
CoreExpr
pre_loop_fn <- [Id] -> CoreExpr -> DsM CoreExpr
matchEnv [Id]
env_ids CoreExpr
pre_loop_body
let
env_ty :: Type
env_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids
out_ty :: Type
out_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
out_ids
core_body :: CoreExpr
core_body = DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids Type
env_ty Type
pre_pair_ty Type
out_ty
CoreExpr
pre_loop_fn
(DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_compose DsCmdEnv
ids Type
pre_pair_ty Type
post_pair_ty Type
out_ty
(DsCmdEnv -> Type -> Type -> Type -> CoreExpr -> CoreExpr
do_first DsCmdEnv
ids Type
env1_ty Type
later_ty Type
env2_ty
CoreExpr
core_loop)
(DsCmdEnv -> Type -> Type -> CoreExpr -> CoreExpr
do_arr DsCmdEnv
ids Type
post_pair_ty Type
out_ty
CoreExpr
post_loop_fn))
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr
core_body, DIdSet
env1_id_set DIdSet -> DIdSet -> DIdSet
`unionDVarSet` DIdSet
env2_id_set)
dsCmdStmt _ _ _ _ s :: [Id]
s = String -> SDoc -> DsM (CoreExpr, DIdSet)
forall a. HasCallStack => String -> SDoc -> a
pprPanic "dsCmdStmt" ([Id] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Id]
s)
dsRecCmd
:: DsCmdEnv
-> IdSet
-> [CmdLStmt GhcTc]
-> [Id]
-> [HsExpr GhcTc]
-> [Id]
-> [HsExpr GhcTc]
-> DsM (CoreExpr,
DIdSet,
[Id])
dsRecCmd :: DsCmdEnv
-> VarSet
-> [CmdLStmt GhcTc]
-> [Id]
-> [HsExpr GhcTc]
-> [Id]
-> [HsExpr GhcTc]
-> DsM (CoreExpr, DIdSet, [Id])
dsRecCmd ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars stmts :: [CmdLStmt GhcTc]
stmts later_ids :: [Id]
later_ids later_rets :: [HsExpr GhcTc]
later_rets rec_ids :: [Id]
rec_ids rec_rets :: [HsExpr GhcTc]
rec_rets = do
let
later_id_set :: VarSet
later_id_set = [Id] -> VarSet
mkVarSet [Id]
later_ids
rec_id_set :: VarSet
rec_id_set = [Id] -> VarSet
mkVarSet [Id]
rec_ids
local_vars' :: VarSet
local_vars' = VarSet
rec_id_set VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
later_id_set VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
local_vars
[CoreExpr]
core_later_rets <- (HsExpr GhcTc -> DsM CoreExpr)
-> [HsExpr GhcTc] -> IOEnv (Env DsGblEnv DsLclEnv) [CoreExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM HsExpr GhcTc -> DsM CoreExpr
dsExpr [HsExpr GhcTc]
later_rets
[CoreExpr]
core_rec_rets <- (HsExpr GhcTc -> DsM CoreExpr)
-> [HsExpr GhcTc] -> IOEnv (Env DsGblEnv DsLclEnv) [CoreExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM HsExpr GhcTc -> DsM CoreExpr
dsExpr [HsExpr GhcTc]
rec_rets
let
out_ids :: [Id]
out_ids = [CoreExpr] -> [Id]
exprsFreeIdsList ([CoreExpr]
core_later_rets [CoreExpr] -> [CoreExpr] -> [CoreExpr]
forall a. [a] -> [a] -> [a]
++ [CoreExpr]
core_rec_rets)
out_ty :: Type
out_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
out_ids
later_tuple :: CoreExpr
later_tuple = [CoreExpr] -> CoreExpr
mkBigCoreTup [CoreExpr]
core_later_rets
later_ty :: Type
later_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
later_ids
rec_tuple :: CoreExpr
rec_tuple = [CoreExpr] -> CoreExpr
mkBigCoreTup [CoreExpr]
core_rec_rets
rec_ty :: Type
rec_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
rec_ids
out_pair :: CoreExpr
out_pair = CoreExpr -> CoreExpr -> CoreExpr
mkCorePairExpr CoreExpr
later_tuple CoreExpr
rec_tuple
out_pair_ty :: Type
out_pair_ty = Type -> Type -> Type
mkCorePairTy Type
later_ty Type
rec_ty
CoreExpr
mk_pair_fn <- [Id] -> CoreExpr -> DsM CoreExpr
matchEnv [Id]
out_ids CoreExpr
out_pair
(core_stmts :: CoreExpr
core_stmts, fv_stmts :: DIdSet
fv_stmts, env_ids :: [Id]
env_ids) <- DsCmdEnv
-> VarSet
-> [Id]
-> [CmdLStmt GhcTc]
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmdStmts DsCmdEnv
ids VarSet
local_vars' [Id]
out_ids [CmdLStmt GhcTc]
stmts
Id
rec_id <- Type -> DsM Id
newSysLocalDs Type
rec_ty
let
env1_id_set :: DIdSet
env1_id_set = DIdSet
fv_stmts DIdSet -> VarSet -> DIdSet
forall a b. UniqDSet a -> UniqSet b -> UniqDSet a
`uniqDSetMinusUniqSet` VarSet
rec_id_set
env1_ids :: [Id]
env1_ids = DIdSet -> [Id]
dVarSetElems DIdSet
env1_id_set
env1_ty :: Type
env1_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
env1_ids
in_pair_ty :: Type
in_pair_ty = Type -> Type -> Type
mkCorePairTy Type
env1_ty Type
rec_ty
core_body :: CoreExpr
core_body = [CoreExpr] -> CoreExpr
mkBigCoreTup ((Id -> CoreExpr) -> [Id] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map Id -> CoreExpr
selectVar [Id]
env_ids)
where
selectVar :: Id -> CoreExpr
selectVar v :: Id
v
| Id
v Id -> VarSet -> Bool
`elemVarSet` VarSet
rec_id_set
= [Id] -> Id -> Id -> CoreExpr -> CoreExpr
mkTupleSelector [Id]
rec_ids Id
v Id
rec_id (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
rec_id)
| Bool
otherwise = Id -> CoreExpr
forall b. Id -> Expr b
Var Id
v
CoreExpr
squash_pair_fn <- [Id] -> Id -> CoreExpr -> DsM CoreExpr
matchEnvStack [Id]
env1_ids Id
rec_id CoreExpr
core_body
let
env_ty :: Type
env_ty = [Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids
core_loop :: CoreExpr
core_loop = DsCmdEnv -> Type -> Type -> Type -> CoreExpr -> CoreExpr
do_loop DsCmdEnv
ids Type
env1_ty Type
later_ty Type
rec_ty
(DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_premap DsCmdEnv
ids Type
in_pair_ty Type
env_ty Type
out_pair_ty
CoreExpr
squash_pair_fn
(DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_compose DsCmdEnv
ids Type
env_ty Type
out_ty Type
out_pair_ty
CoreExpr
core_stmts
(DsCmdEnv -> Type -> Type -> CoreExpr -> CoreExpr
do_arr DsCmdEnv
ids Type
out_ty Type
out_pair_ty CoreExpr
mk_pair_fn)))
(CoreExpr, DIdSet, [Id]) -> DsM (CoreExpr, DIdSet, [Id])
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr
core_loop, DIdSet
env1_id_set, [Id]
env1_ids)
dsfixCmdStmts
:: DsCmdEnv
-> IdSet
-> [Id]
-> [CmdLStmt GhcTc]
-> DsM (CoreExpr,
DIdSet,
[Id])
dsfixCmdStmts :: DsCmdEnv
-> VarSet
-> [Id]
-> [CmdLStmt GhcTc]
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmdStmts ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars out_ids :: [Id]
out_ids stmts :: [CmdLStmt GhcTc]
stmts
= ([Id] -> DsM (CoreExpr, DIdSet)) -> DsM (CoreExpr, DIdSet, [Id])
trimInput (DsCmdEnv
-> VarSet
-> [Id]
-> [CmdLStmt GhcTc]
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmdStmts DsCmdEnv
ids VarSet
local_vars [Id]
out_ids [CmdLStmt GhcTc]
stmts)
dsCmdStmts
:: DsCmdEnv
-> IdSet
-> [Id]
-> [CmdLStmt GhcTc]
-> [Id]
-> DsM (CoreExpr,
DIdSet)
dsCmdStmts :: DsCmdEnv
-> VarSet
-> [Id]
-> [CmdLStmt GhcTc]
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmdStmts ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars out_ids :: [Id]
out_ids [stmt :: CmdLStmt GhcTc
stmt] env_ids :: [Id]
env_ids
= DsCmdEnv
-> VarSet
-> [Id]
-> CmdLStmt GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmdLStmt DsCmdEnv
ids VarSet
local_vars [Id]
out_ids CmdLStmt GhcTc
stmt [Id]
env_ids
dsCmdStmts ids :: DsCmdEnv
ids local_vars :: VarSet
local_vars out_ids :: [Id]
out_ids (stmt :: CmdLStmt GhcTc
stmt:stmts :: [CmdLStmt GhcTc]
stmts) env_ids :: [Id]
env_ids = do
let bound_vars :: VarSet
bound_vars = [Id] -> VarSet
mkVarSet (CmdLStmt GhcTc -> [Id]
forall body. LStmt GhcTc body -> [Id]
collectLStmtBinders CmdLStmt GhcTc
stmt)
let local_vars' :: VarSet
local_vars' = VarSet
bound_vars VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
local_vars
(core_stmts :: CoreExpr
core_stmts, _fv_stmts :: DIdSet
_fv_stmts, env_ids' :: [Id]
env_ids') <- DsCmdEnv
-> VarSet
-> [Id]
-> [CmdLStmt GhcTc]
-> DsM (CoreExpr, DIdSet, [Id])
dsfixCmdStmts DsCmdEnv
ids VarSet
local_vars' [Id]
out_ids [CmdLStmt GhcTc]
stmts
(core_stmt :: CoreExpr
core_stmt, fv_stmt :: DIdSet
fv_stmt) <- DsCmdEnv
-> VarSet
-> [Id]
-> CmdLStmt GhcTc
-> [Id]
-> DsM (CoreExpr, DIdSet)
dsCmdLStmt DsCmdEnv
ids VarSet
local_vars [Id]
env_ids' CmdLStmt GhcTc
stmt [Id]
env_ids
(CoreExpr, DIdSet) -> DsM (CoreExpr, DIdSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsCmdEnv
-> Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr
do_compose DsCmdEnv
ids
([Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids)
([Id] -> Type
mkBigCoreVarTupTy [Id]
env_ids')
([Id] -> Type
mkBigCoreVarTupTy [Id]
out_ids)
CoreExpr
core_stmt
CoreExpr
core_stmts,
DIdSet
fv_stmt)
dsCmdStmts _ _ _ [] _ = String -> DsM (CoreExpr, DIdSet)
forall a. String -> a
panic "dsCmdStmts []"
matchSimplys :: [CoreExpr]
-> HsMatchContext Name
-> [LPat GhcTc]
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimplys :: [CoreExpr]
-> HsMatchContext Name
-> [LPat GhcTc]
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimplys [] _ctxt :: HsMatchContext Name
_ctxt [] result_expr :: CoreExpr
result_expr _fail_expr :: CoreExpr
_fail_expr = CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return CoreExpr
result_expr
matchSimplys (exp :: CoreExpr
exp:exps :: [CoreExpr]
exps) ctxt :: HsMatchContext Name
ctxt (pat :: LPat GhcTc
pat:pats :: [LPat GhcTc]
pats) result_expr :: CoreExpr
result_expr fail_expr :: CoreExpr
fail_expr = do
CoreExpr
match_code <- [CoreExpr]
-> HsMatchContext Name
-> [LPat GhcTc]
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimplys [CoreExpr]
exps HsMatchContext Name
ctxt [LPat GhcTc]
pats CoreExpr
result_expr CoreExpr
fail_expr
CoreExpr
-> HsMatchContext Name
-> LPat GhcTc
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimply CoreExpr
exp HsMatchContext Name
ctxt LPat GhcTc
pat CoreExpr
match_code CoreExpr
fail_expr
matchSimplys _ _ _ _ _ = String -> DsM CoreExpr
forall a. String -> a
panic "matchSimplys"
leavesMatch :: LMatch GhcTc (Located (body GhcTc))
-> [(Located (body GhcTc), IdSet)]
leavesMatch :: LMatch GhcTc (Located (body GhcTc))
-> [(Located (body GhcTc), VarSet)]
leavesMatch (LMatch GhcTc (Located (body GhcTc))
-> Located (SrcSpanLess (LMatch GhcTc (Located (body GhcTc))))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (Match { m_pats = pats
, m_grhss = GRHSs _ grhss (dL->L _ binds) }))
= let
defined_vars :: VarSet
defined_vars = [Id] -> VarSet
mkVarSet ([LPat GhcTc] -> [Id]
collectPatsBinders [LPat GhcTc]
pats)
VarSet -> VarSet -> VarSet
`unionVarSet`
[Id] -> VarSet
mkVarSet (HsLocalBindsLR GhcTc GhcTc -> [IdP GhcTc]
forall (idL :: Pass) (idR :: Pass).
HsLocalBindsLR (GhcPass idL) (GhcPass idR) -> [IdP (GhcPass idL)]
collectLocalBinders SrcSpanLess (LHsLocalBinds GhcTc)
HsLocalBindsLR GhcTc GhcTc
binds)
in
[(Located (body GhcTc)
body,
[Id] -> VarSet
mkVarSet ([LStmt GhcTc (LHsExpr GhcTc)] -> [Id]
forall body. [LStmt GhcTc body] -> [Id]
collectLStmtsBinders [LStmt GhcTc (LHsExpr GhcTc)]
stmts)
VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
defined_vars)
| (LGRHS GhcTc (Located (body GhcTc))
-> Located (SrcSpanLess (LGRHS GhcTc (Located (body GhcTc))))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (GRHS _ stmts body)) <- [LGRHS GhcTc (Located (body GhcTc))]
grhss]
leavesMatch _ = String -> [(Located (body GhcTc), VarSet)]
forall a. String -> a
panic "leavesMatch"
replaceLeavesMatch
:: Type
-> [Located (body' GhcTc)]
-> LMatch GhcTc (Located (body GhcTc))
-> ([Located (body' GhcTc)],
LMatch GhcTc (Located (body' GhcTc)))
replaceLeavesMatch :: Type
-> [Located (body' GhcTc)]
-> LMatch GhcTc (Located (body GhcTc))
-> ([Located (body' GhcTc)], LMatch GhcTc (Located (body' GhcTc)))
replaceLeavesMatch _res_ty :: Type
_res_ty leaves :: [Located (body' GhcTc)]
leaves
(LMatch GhcTc (Located (body GhcTc))
-> Located (SrcSpanLess (LMatch GhcTc (Located (body GhcTc))))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc
match :: SrcSpanLess (LMatch GhcTc (Located (body GhcTc)))
match@(Match { m_grhss = GRHSs x grhss binds }))
= let
(leaves' :: [Located (body' GhcTc)]
leaves', grhss' :: [LGRHS GhcTc (Located (body' GhcTc))]
grhss') = ([Located (body' GhcTc)]
-> LGRHS GhcTc (Located (body GhcTc))
-> ([Located (body' GhcTc)], LGRHS GhcTc (Located (body' GhcTc))))
-> [Located (body' GhcTc)]
-> [LGRHS GhcTc (Located (body GhcTc))]
-> ([Located (body' GhcTc)], [LGRHS GhcTc (Located (body' GhcTc))])
forall (t :: * -> *) a b c.
Traversable t =>
(a -> b -> (a, c)) -> a -> t b -> (a, t c)
mapAccumL [Located (body' GhcTc)]
-> LGRHS GhcTc (Located (body GhcTc))
-> ([Located (body' GhcTc)], LGRHS GhcTc (Located (body' GhcTc)))
forall (body' :: * -> *) (body :: * -> *).
[Located (body' GhcTc)]
-> LGRHS GhcTc (Located (body GhcTc))
-> ([Located (body' GhcTc)], LGRHS GhcTc (Located (body' GhcTc)))
replaceLeavesGRHS [Located (body' GhcTc)]
leaves [LGRHS GhcTc (Located (body GhcTc))]
grhss
in
([Located (body' GhcTc)]
leaves', SrcSpan
-> SrcSpanLess (LMatch GhcTc (Located (body' GhcTc)))
-> LMatch GhcTc (Located (body' GhcTc))
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
loc (SrcSpanLess (LMatch GhcTc (Located (body GhcTc)))
Match GhcTc (Located (body GhcTc))
match { m_ext :: XCMatch GhcTc (Located (body' GhcTc))
m_ext = XCMatch GhcTc (Located (body' GhcTc))
NoExt
noExt, m_grhss :: GRHSs GhcTc (Located (body' GhcTc))
m_grhss = XCGRHSs GhcTc (Located (body' GhcTc))
-> [LGRHS GhcTc (Located (body' GhcTc))]
-> LHsLocalBinds GhcTc
-> GRHSs GhcTc (Located (body' GhcTc))
forall p body.
XCGRHSs p body -> [LGRHS p body] -> LHsLocalBinds p -> GRHSs p body
GRHSs XCGRHSs GhcTc (Located (body' GhcTc))
XCGRHSs GhcTc (Located (body GhcTc))
x [LGRHS GhcTc (Located (body' GhcTc))]
grhss' LHsLocalBinds GhcTc
binds }))
replaceLeavesMatch _ _ _ = String
-> ([Located (body' GhcTc)], LMatch GhcTc (Located (body' GhcTc)))
forall a. String -> a
panic "replaceLeavesMatch"
replaceLeavesGRHS
:: [Located (body' GhcTc)]
-> LGRHS GhcTc (Located (body GhcTc))
-> ([Located (body' GhcTc)],
LGRHS GhcTc (Located (body' GhcTc)))
replaceLeavesGRHS :: [Located (body' GhcTc)]
-> LGRHS GhcTc (Located (body GhcTc))
-> ([Located (body' GhcTc)], LGRHS GhcTc (Located (body' GhcTc)))
replaceLeavesGRHS (leaf :: Located (body' GhcTc)
leaf:leaves :: [Located (body' GhcTc)]
leaves) (LGRHS GhcTc (Located (body GhcTc))
-> Located (SrcSpanLess (LGRHS GhcTc (Located (body GhcTc))))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc (GRHS x stmts _))
= ([Located (body' GhcTc)]
leaves, SrcSpan
-> SrcSpanLess (LGRHS GhcTc (Located (body' GhcTc)))
-> LGRHS GhcTc (Located (body' GhcTc))
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
loc (XCGRHS GhcTc (Located (body' GhcTc))
-> [LStmt GhcTc (LHsExpr GhcTc)]
-> Located (body' GhcTc)
-> GRHS GhcTc (Located (body' GhcTc))
forall p body.
XCGRHS p body -> [GuardLStmt p] -> body -> GRHS p body
GRHS XCGRHS GhcTc (Located (body' GhcTc))
XCGRHS GhcTc (Located (body GhcTc))
x [LStmt GhcTc (LHsExpr GhcTc)]
stmts Located (body' GhcTc)
leaf))
replaceLeavesGRHS [] _ = String
-> ([Located (body' GhcTc)], LGRHS GhcTc (Located (body' GhcTc)))
forall a. String -> a
panic "replaceLeavesGRHS []"
replaceLeavesGRHS _ _ = String
-> ([Located (body' GhcTc)], LGRHS GhcTc (Located (body' GhcTc)))
forall a. String -> a
panic "replaceLeavesGRHS"
foldb :: (a -> a -> a) -> [a] -> a
foldb :: (a -> a -> a) -> [a] -> a
foldb _ [] = String -> a
forall a. HasCallStack => String -> a
error "foldb of empty list"
foldb _ [x :: a
x] = a
x
foldb f :: a -> a -> a
f xs :: [a]
xs = (a -> a -> a) -> [a] -> a
forall a. (a -> a -> a) -> [a] -> a
foldb a -> a -> a
f ([a] -> [a]
fold_pairs [a]
xs)
where
fold_pairs :: [a] -> [a]
fold_pairs [] = []
fold_pairs [x :: a
x] = [a
x]
fold_pairs (x1 :: a
x1:x2 :: a
x2:xs :: [a]
xs) = a -> a -> a
f a
x1 a
x2a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a] -> [a]
fold_pairs [a]
xs
collectPatBinders :: LPat GhcTc -> [Id]
collectPatBinders :: LPat GhcTc -> [Id]
collectPatBinders pat :: LPat GhcTc
pat = LPat GhcTc -> [Id] -> [Id]
collectl LPat GhcTc
pat []
collectPatsBinders :: [LPat GhcTc] -> [Id]
collectPatsBinders :: [LPat GhcTc] -> [Id]
collectPatsBinders pats :: [LPat GhcTc]
pats = (LPat GhcTc -> [Id] -> [Id]) -> [Id] -> [LPat GhcTc] -> [Id]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr LPat GhcTc -> [Id] -> [Id]
collectl [] [LPat GhcTc]
pats
collectl :: LPat GhcTc -> [Id] -> [Id]
collectl :: LPat GhcTc -> [Id] -> [Id]
collectl (LPat GhcTc -> Located (SrcSpanLess (LPat GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ pat :: SrcSpanLess (LPat GhcTc)
pat) bndrs :: [Id]
bndrs
= LPat GhcTc -> [Id]
go SrcSpanLess (LPat GhcTc)
LPat GhcTc
pat
where
go :: LPat GhcTc -> [Id]
go (VarPat _ (Located (IdP GhcTc) -> Located (SrcSpanLess (Located Id))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ var :: SrcSpanLess (Located Id)
var)) = SrcSpanLess (Located Id)
Id
var Id -> [Id] -> [Id]
forall a. a -> [a] -> [a]
: [Id]
bndrs
go (WildPat _) = [Id]
bndrs
go (LazyPat _ pat :: LPat GhcTc
pat) = LPat GhcTc -> [Id] -> [Id]
collectl LPat GhcTc
pat [Id]
bndrs
go (BangPat _ pat :: LPat GhcTc
pat) = LPat GhcTc -> [Id] -> [Id]
collectl LPat GhcTc
pat [Id]
bndrs
go (AsPat _ (Located (IdP GhcTc) -> Located (SrcSpanLess (Located Id))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ a :: SrcSpanLess (Located Id)
a) pat :: LPat GhcTc
pat) = SrcSpanLess (Located Id)
Id
a Id -> [Id] -> [Id]
forall a. a -> [a] -> [a]
: LPat GhcTc -> [Id] -> [Id]
collectl LPat GhcTc
pat [Id]
bndrs
go (ParPat _ pat :: LPat GhcTc
pat) = LPat GhcTc -> [Id] -> [Id]
collectl LPat GhcTc
pat [Id]
bndrs
go (ListPat _ pats :: [LPat GhcTc]
pats) = (LPat GhcTc -> [Id] -> [Id]) -> [Id] -> [LPat GhcTc] -> [Id]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr LPat GhcTc -> [Id] -> [Id]
collectl [Id]
bndrs [LPat GhcTc]
pats
go (TuplePat _ pats :: [LPat GhcTc]
pats _) = (LPat GhcTc -> [Id] -> [Id]) -> [Id] -> [LPat GhcTc] -> [Id]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr LPat GhcTc -> [Id] -> [Id]
collectl [Id]
bndrs [LPat GhcTc]
pats
go (SumPat _ pat :: LPat GhcTc
pat _ _) = LPat GhcTc -> [Id] -> [Id]
collectl LPat GhcTc
pat [Id]
bndrs
go (ConPatIn _ ps :: HsConPatDetails GhcTc
ps) = (LPat GhcTc -> [Id] -> [Id]) -> [Id] -> [LPat GhcTc] -> [Id]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr LPat GhcTc -> [Id] -> [Id]
collectl [Id]
bndrs (HsConPatDetails GhcTc -> [LPat GhcTc]
forall p. HsConPatDetails p -> [LPat p]
hsConPatArgs HsConPatDetails GhcTc
ps)
go (ConPatOut {pat_args :: forall p. Pat p -> HsConPatDetails p
pat_args=HsConPatDetails GhcTc
ps, pat_binds :: forall p. Pat p -> TcEvBinds
pat_binds=TcEvBinds
ds}) =
TcEvBinds -> [Id]
collectEvBinders TcEvBinds
ds
[Id] -> [Id] -> [Id]
forall a. [a] -> [a] -> [a]
++ (LPat GhcTc -> [Id] -> [Id]) -> [Id] -> [LPat GhcTc] -> [Id]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr LPat GhcTc -> [Id] -> [Id]
collectl [Id]
bndrs (HsConPatDetails GhcTc -> [LPat GhcTc]
forall p. HsConPatDetails p -> [LPat p]
hsConPatArgs HsConPatDetails GhcTc
ps)
go (LitPat _ _) = [Id]
bndrs
go (NPat {}) = [Id]
bndrs
go (NPlusKPat _ (Located (IdP GhcTc) -> Located (SrcSpanLess (Located Id))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ n :: SrcSpanLess (Located Id)
n) _ _ _ _) = SrcSpanLess (Located Id)
Id
n Id -> [Id] -> [Id]
forall a. a -> [a] -> [a]
: [Id]
bndrs
go (SigPat _ pat :: LPat GhcTc
pat _) = LPat GhcTc -> [Id] -> [Id]
collectl LPat GhcTc
pat [Id]
bndrs
go (CoPat _ _ pat :: LPat GhcTc
pat _) = LPat GhcTc -> [Id] -> [Id]
collectl (SrcSpanLess (LPat GhcTc) -> LPat GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc SrcSpanLess (LPat GhcTc)
LPat GhcTc
pat) [Id]
bndrs
go (ViewPat _ _ pat :: LPat GhcTc
pat) = LPat GhcTc -> [Id] -> [Id]
collectl LPat GhcTc
pat [Id]
bndrs
go p :: LPat GhcTc
p@(SplicePat {}) = String -> SDoc -> [Id]
forall a. HasCallStack => String -> SDoc -> a
pprPanic "collectl/go" (LPat GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr LPat GhcTc
p)
go p :: LPat GhcTc
p@(XPat {}) = String -> SDoc -> [Id]
forall a. HasCallStack => String -> SDoc -> a
pprPanic "collectl/go" (LPat GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr LPat GhcTc
p)
collectEvBinders :: TcEvBinds -> [Id]
collectEvBinders :: TcEvBinds -> [Id]
collectEvBinders (EvBinds bs :: Bag EvBind
bs) = (EvBind -> [Id] -> [Id]) -> [Id] -> Bag EvBind -> [Id]
forall a r. (a -> r -> r) -> r -> Bag a -> r
foldrBag EvBind -> [Id] -> [Id]
add_ev_bndr [] Bag EvBind
bs
collectEvBinders (TcEvBinds {}) = String -> [Id]
forall a. String -> a
panic "ToDo: collectEvBinders"
add_ev_bndr :: EvBind -> [Id] -> [Id]
add_ev_bndr :: EvBind -> [Id] -> [Id]
add_ev_bndr (EvBind { eb_lhs :: EvBind -> Id
eb_lhs = Id
b }) bs :: [Id]
bs | Id -> Bool
isId Id
b = Id
bId -> [Id] -> [Id]
forall a. a -> [a] -> [a]
:[Id]
bs
| Bool
otherwise = [Id]
bs
collectLStmtsBinders :: [LStmt GhcTc body] -> [Id]
collectLStmtsBinders :: [LStmt GhcTc body] -> [Id]
collectLStmtsBinders = (LStmt GhcTc body -> [Id]) -> [LStmt GhcTc body] -> [Id]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LStmt GhcTc body -> [Id]
forall body. LStmt GhcTc body -> [Id]
collectLStmtBinders
collectLStmtBinders :: LStmt GhcTc body -> [Id]
collectLStmtBinders :: LStmt GhcTc body -> [Id]
collectLStmtBinders = Stmt GhcTc body -> [Id]
forall body. Stmt GhcTc body -> [Id]
collectStmtBinders (Stmt GhcTc body -> [Id])
-> (LStmt GhcTc body -> Stmt GhcTc body)
-> LStmt GhcTc body
-> [Id]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LStmt GhcTc body -> Stmt GhcTc body
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc
collectStmtBinders :: Stmt GhcTc body -> [Id]
collectStmtBinders :: Stmt GhcTc body -> [Id]
collectStmtBinders (RecStmt { recS_later_ids :: forall idL idR body. StmtLR idL idR body -> [IdP idR]
recS_later_ids = [IdP GhcTc]
later_ids }) = [Id]
[IdP GhcTc]
later_ids
collectStmtBinders stmt :: Stmt GhcTc body
stmt = Stmt GhcTc body -> [IdP GhcTc]
forall (idL :: Pass) (idR :: Pass) body.
StmtLR (GhcPass idL) (GhcPass idR) body -> [IdP (GhcPass idL)]
HsUtils.collectStmtBinders Stmt GhcTc body
stmt