{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
module GHC.HsToCore.Match.Constructor ( matchConFamily, matchPatSyn ) where
#include "HsVersions.h"
import GHC.Prelude
import {-# SOURCE #-} GHC.HsToCore.Match ( match )
import GHC.Hs
import GHC.HsToCore.Binds
import GHC.Core.ConLike
import GHC.Types.Basic ( Origin(..) )
import GHC.Tc.Utils.TcType
import GHC.Core.Multiplicity
import GHC.HsToCore.Monad
import GHC.HsToCore.Utils
import GHC.Core ( CoreExpr )
import GHC.Core.Make ( mkCoreLets )
import GHC.Utils.Misc
import GHC.Types.Id
import GHC.Types.Name.Env
import GHC.Types.FieldLabel ( flSelector )
import GHC.Types.SrcLoc
import GHC.Utils.Outputable
import Control.Monad(liftM)
import Data.List (groupBy)
import Data.List.NonEmpty (NonEmpty(..))
matchConFamily :: NonEmpty Id
-> Type
-> NonEmpty (NonEmpty EquationInfo)
-> DsM (MatchResult CoreExpr)
matchConFamily :: NonEmpty Id
-> Type
-> NonEmpty (NonEmpty EquationInfo)
-> DsM (MatchResult CoreExpr)
matchConFamily (Id
var :| [Id]
vars) Type
ty NonEmpty (NonEmpty EquationInfo)
groups
= do let mult :: Type
mult = Id -> Type
idMult Id
var
NonEmpty (CaseAlt DataCon)
alts <- (NonEmpty EquationInfo
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt DataCon))
-> NonEmpty (NonEmpty EquationInfo)
-> IOEnv (Env DsGblEnv DsLclEnv) (NonEmpty (CaseAlt DataCon))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ((CaseAlt ConLike -> CaseAlt DataCon)
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt ConLike)
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt DataCon)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CaseAlt ConLike -> CaseAlt DataCon
toRealAlt (IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt ConLike)
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt DataCon))
-> (NonEmpty EquationInfo
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt ConLike))
-> NonEmpty EquationInfo
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt DataCon)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Id]
-> Type
-> Type
-> NonEmpty EquationInfo
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt ConLike)
matchOneConLike [Id]
vars Type
ty Type
mult) NonEmpty (NonEmpty EquationInfo)
groups
MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> Type -> NonEmpty (CaseAlt DataCon) -> MatchResult CoreExpr
mkCoAlgCaseMatchResult Id
var Type
ty NonEmpty (CaseAlt DataCon)
alts)
where
toRealAlt :: CaseAlt ConLike -> CaseAlt DataCon
toRealAlt CaseAlt ConLike
alt = case CaseAlt ConLike -> ConLike
forall a. CaseAlt a -> a
alt_pat CaseAlt ConLike
alt of
RealDataCon DataCon
dcon -> CaseAlt ConLike
alt{ alt_pat :: DataCon
alt_pat = DataCon
dcon }
ConLike
_ -> String -> CaseAlt DataCon
forall a. String -> a
panic String
"matchConFamily: not RealDataCon"
matchPatSyn :: NonEmpty Id
-> Type
-> NonEmpty EquationInfo
-> DsM (MatchResult CoreExpr)
matchPatSyn :: NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchPatSyn (Id
var :| [Id]
vars) Type
ty NonEmpty EquationInfo
eqns
= do let mult :: Type
mult = Id -> Type
idMult Id
var
CaseAlt PatSyn
alt <- (CaseAlt ConLike -> CaseAlt PatSyn)
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt ConLike)
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt PatSyn)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CaseAlt ConLike -> CaseAlt PatSyn
toSynAlt (IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt ConLike)
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt PatSyn))
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt ConLike)
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt PatSyn)
forall a b. (a -> b) -> a -> b
$ [Id]
-> Type
-> Type
-> NonEmpty EquationInfo
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt ConLike)
matchOneConLike [Id]
vars Type
ty Type
mult NonEmpty EquationInfo
eqns
MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> Type -> CaseAlt PatSyn -> MatchResult CoreExpr
mkCoSynCaseMatchResult Id
var Type
ty CaseAlt PatSyn
alt)
where
toSynAlt :: CaseAlt ConLike -> CaseAlt PatSyn
toSynAlt CaseAlt ConLike
alt = case CaseAlt ConLike -> ConLike
forall a. CaseAlt a -> a
alt_pat CaseAlt ConLike
alt of
PatSynCon PatSyn
psyn -> CaseAlt ConLike
alt{ alt_pat :: PatSyn
alt_pat = PatSyn
psyn }
ConLike
_ -> String -> CaseAlt PatSyn
forall a. String -> a
panic String
"matchPatSyn: not PatSynCon"
type ConArgPats = HsConDetails (LPat GhcTc) (HsRecFields GhcTc (LPat GhcTc))
matchOneConLike :: [Id]
-> Type
-> Mult
-> NonEmpty EquationInfo
-> DsM (CaseAlt ConLike)
matchOneConLike :: [Id]
-> Type
-> Type
-> NonEmpty EquationInfo
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt ConLike)
matchOneConLike [Id]
vars Type
ty Type
mult (EquationInfo
eqn1 :| [EquationInfo]
eqns)
= do { let inst_tys :: [Type]
inst_tys = ASSERT( all tcIsTcTyVar ex_tvs )
ASSERT( tvs1 `equalLength` ex_tvs )
[Type]
arg_tys [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Id] -> [Type]
mkTyVarTys [Id]
tvs1
val_arg_tys :: [Scaled Type]
val_arg_tys = ConLike -> [Type] -> [Scaled Type]
conLikeInstOrigArgTys ConLike
ConLikeP GhcTc
con1 [Type]
inst_tys
match_group :: [Id]
-> [(ConArgPats, EquationInfo)] -> DsM (MatchResult CoreExpr)
match_group :: [Id] -> [(ConArgPats, EquationInfo)] -> DsM (MatchResult CoreExpr)
match_group [Id]
arg_vars [(ConArgPats, EquationInfo)]
arg_eqn_prs
= ASSERT( notNull arg_eqn_prs )
do { ([CoreExpr -> CoreExpr]
wraps, [EquationInfo]
eqns') <- ([(CoreExpr -> CoreExpr, EquationInfo)]
-> ([CoreExpr -> CoreExpr], [EquationInfo]))
-> IOEnv
(Env DsGblEnv DsLclEnv) [(CoreExpr -> CoreExpr, EquationInfo)]
-> IOEnv
(Env DsGblEnv DsLclEnv) ([CoreExpr -> CoreExpr], [EquationInfo])
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [(CoreExpr -> CoreExpr, EquationInfo)]
-> ([CoreExpr -> CoreExpr], [EquationInfo])
forall a b. [(a, b)] -> ([a], [b])
unzip (((HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)
-> IOEnv
(Env DsGblEnv DsLclEnv) (CoreExpr -> CoreExpr, EquationInfo))
-> [(HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)]
-> IOEnv
(Env DsGblEnv DsLclEnv) [(CoreExpr -> CoreExpr, EquationInfo)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)
-> IOEnv
(Env DsGblEnv DsLclEnv) (CoreExpr -> CoreExpr, EquationInfo)
shift [(HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)]
[(ConArgPats, EquationInfo)]
arg_eqn_prs)
; let group_arg_vars :: [Id]
group_arg_vars = [Id] -> [(ConArgPats, EquationInfo)] -> [Id]
select_arg_vars [Id]
arg_vars [(ConArgPats, EquationInfo)]
arg_eqn_prs
; MatchResult CoreExpr
match_result <- [Id] -> Type -> [EquationInfo] -> DsM (MatchResult CoreExpr)
match ([Id]
group_arg_vars [Id] -> [Id] -> [Id]
forall a. [a] -> [a] -> [a]
++ [Id]
vars) Type
ty [EquationInfo]
eqns'
; MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return (MatchResult CoreExpr -> DsM (MatchResult CoreExpr))
-> MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$ ((CoreExpr -> CoreExpr)
-> (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr)
-> [CoreExpr -> CoreExpr] -> CoreExpr -> CoreExpr
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 (CoreExpr -> CoreExpr)
-> (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) [CoreExpr -> CoreExpr]
wraps (CoreExpr -> CoreExpr)
-> MatchResult CoreExpr -> MatchResult CoreExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MatchResult CoreExpr
match_result
}
shift :: (HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)
-> IOEnv
(Env DsGblEnv DsLclEnv) (CoreExpr -> CoreExpr, EquationInfo)
shift (HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc)))
_, eqn :: EquationInfo
eqn@(EqnInfo
{ eqn_pats :: EquationInfo -> [Pat GhcTc]
eqn_pats = ConPat
{ pat_args :: forall p. Pat p -> HsConPatDetails p
pat_args = ConArgPats
args
, pat_con_ext :: forall p. Pat p -> XConPat p
pat_con_ext = ConPatTc
{ cpt_tvs :: ConPatTc -> [Id]
cpt_tvs = [Id]
tvs
, cpt_dicts :: ConPatTc -> [Id]
cpt_dicts = [Id]
ds
, cpt_binds :: ConPatTc -> TcEvBinds
cpt_binds = TcEvBinds
bind
}
} : [Pat GhcTc]
pats
}))
= do [CoreBind]
ds_bind <- TcEvBinds -> DsM [CoreBind]
dsTcEvBinds TcEvBinds
bind
(CoreExpr -> CoreExpr, EquationInfo)
-> IOEnv
(Env DsGblEnv DsLclEnv) (CoreExpr -> CoreExpr, EquationInfo)
forall (m :: * -> *) a. Monad m => a -> m a
return ( [(Id, Id)] -> CoreExpr -> CoreExpr
wrapBinds ([Id]
tvs [Id] -> [Id] -> [(Id, Id)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Id]
tvs1)
(CoreExpr -> CoreExpr)
-> (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Id, Id)] -> CoreExpr -> CoreExpr
wrapBinds ([Id]
ds [Id] -> [Id] -> [(Id, Id)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Id]
dicts1)
(CoreExpr -> CoreExpr)
-> (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CoreBind] -> CoreExpr -> CoreExpr
mkCoreLets [CoreBind]
ds_bind
, EquationInfo
eqn { eqn_orig :: Origin
eqn_orig = Origin
Generated
, eqn_pats :: [Pat GhcTc]
eqn_pats = [Scaled Type] -> ConArgPats -> [Pat GhcTc]
conArgPats [Scaled Type]
val_arg_tys ConArgPats
args [Pat GhcTc] -> [Pat GhcTc] -> [Pat GhcTc]
forall a. [a] -> [a] -> [a]
++ [Pat GhcTc]
pats }
)
shift (HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc)))
_, (EqnInfo { eqn_pats :: EquationInfo -> [Pat GhcTc]
eqn_pats = [Pat GhcTc]
ps })) = String
-> SDoc
-> IOEnv
(Env DsGblEnv DsLclEnv) (CoreExpr -> CoreExpr, EquationInfo)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"matchOneCon/shift" ([Pat GhcTc] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Pat GhcTc]
ps)
; let scaled_arg_tys :: [Scaled Type]
scaled_arg_tys = (Scaled Type -> Scaled Type) -> [Scaled Type] -> [Scaled Type]
forall a b. (a -> b) -> [a] -> [b]
map (Type -> Scaled Type -> Scaled Type
forall a. Type -> Scaled a -> Scaled a
scaleScaled Type
mult) [Scaled Type]
val_arg_tys
; [Id]
arg_vars <- [Scaled Type] -> ConArgPats -> DsM [Id]
selectConMatchVars [Scaled Type]
scaled_arg_tys ConArgPats
args1
; let groups :: [[(ConArgPats, EquationInfo)]]
groups :: [[(ConArgPats, EquationInfo)]]
groups = ((HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)
-> (HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)
-> Bool)
-> [(HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)]
-> [[(HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)
-> (HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)
-> Bool
forall a. (ConArgPats, a) -> (ConArgPats, a) -> Bool
compatible_pats [ (Pat GhcTc -> ConArgPats
forall p. Pat p -> HsConPatDetails p
pat_args (EquationInfo -> Pat GhcTc
firstPat EquationInfo
eqn), EquationInfo
eqn)
| EquationInfo
eqn <- EquationInfo
eqn1EquationInfo -> [EquationInfo] -> [EquationInfo]
forall a. a -> [a] -> [a]
:[EquationInfo]
eqns ]
; [MatchResult CoreExpr]
match_results <- ([(HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)]
-> DsM (MatchResult CoreExpr))
-> [[(HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)]]
-> IOEnv (Env DsGblEnv DsLclEnv) [MatchResult CoreExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ([Id] -> [(ConArgPats, EquationInfo)] -> DsM (MatchResult CoreExpr)
match_group [Id]
arg_vars) [[(HsConDetails
(Located (Pat GhcTc)) (HsRecFields GhcTc (Located (Pat GhcTc))),
EquationInfo)]]
[[(ConArgPats, EquationInfo)]]
groups
; CaseAlt ConLike -> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt ConLike)
forall (m :: * -> *) a. Monad m => a -> m a
return (CaseAlt ConLike
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt ConLike))
-> CaseAlt ConLike
-> IOEnv (Env DsGblEnv DsLclEnv) (CaseAlt ConLike)
forall a b. (a -> b) -> a -> b
$ MkCaseAlt :: forall a.
a -> [Id] -> HsWrapper -> MatchResult CoreExpr -> CaseAlt a
MkCaseAlt{ alt_pat :: ConLike
alt_pat = ConLike
ConLikeP GhcTc
con1,
alt_bndrs :: [Id]
alt_bndrs = [Id]
tvs1 [Id] -> [Id] -> [Id]
forall a. [a] -> [a] -> [a]
++ [Id]
dicts1 [Id] -> [Id] -> [Id]
forall a. [a] -> [a] -> [a]
++ [Id]
arg_vars,
alt_wrapper :: HsWrapper
alt_wrapper = HsWrapper
wrapper1,
alt_result :: MatchResult CoreExpr
alt_result = (MatchResult CoreExpr
-> MatchResult CoreExpr -> MatchResult CoreExpr)
-> [MatchResult CoreExpr] -> MatchResult CoreExpr
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 MatchResult CoreExpr
-> MatchResult CoreExpr -> MatchResult CoreExpr
combineMatchResults [MatchResult CoreExpr]
match_results } }
where
ConPat { pat_con :: forall p. Pat p -> Located (ConLikeP p)
pat_con = L SrcSpan
_ ConLikeP GhcTc
con1
, pat_args :: forall p. Pat p -> HsConPatDetails p
pat_args = ConArgPats
args1
, pat_con_ext :: forall p. Pat p -> XConPat p
pat_con_ext = ConPatTc
{ cpt_arg_tys :: ConPatTc -> [Type]
cpt_arg_tys = [Type]
arg_tys
, cpt_wrap :: ConPatTc -> HsWrapper
cpt_wrap = HsWrapper
wrapper1
, cpt_tvs :: ConPatTc -> [Id]
cpt_tvs = [Id]
tvs1
, cpt_dicts :: ConPatTc -> [Id]
cpt_dicts = [Id]
dicts1
}
} = EquationInfo -> Pat GhcTc
firstPat EquationInfo
eqn1
fields1 :: [Name]
fields1 = (FieldLbl Name -> Name) -> [FieldLbl Name] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map FieldLbl Name -> Name
forall a. FieldLbl a -> a
flSelector (ConLike -> [FieldLbl Name]
conLikeFieldLabels ConLike
ConLikeP GhcTc
con1)
ex_tvs :: [Id]
ex_tvs = ConLike -> [Id]
conLikeExTyCoVars ConLike
ConLikeP GhcTc
con1
select_arg_vars :: [Id] -> [(ConArgPats, EquationInfo)] -> [Id]
select_arg_vars :: [Id] -> [(ConArgPats, EquationInfo)] -> [Id]
select_arg_vars [Id]
arg_vars ((ConArgPats
arg_pats, EquationInfo
_) : [(ConArgPats, EquationInfo)]
_)
| RecCon HsRecFields GhcTc (LPat GhcTc)
flds <- ConArgPats
arg_pats
, let rpats :: [LHsRecField GhcTc (Located (Pat GhcTc))]
rpats = HsRecFields GhcTc (Located (Pat GhcTc))
-> [LHsRecField GhcTc (Located (Pat GhcTc))]
forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds HsRecFields GhcTc (Located (Pat GhcTc))
HsRecFields GhcTc (LPat GhcTc)
flds
, Bool -> Bool
not ([LHsRecField GhcTc (Located (Pat GhcTc))] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LHsRecField GhcTc (Located (Pat GhcTc))]
rpats)
= ASSERT2( fields1 `equalLength` arg_vars,
ppr con1 $$ ppr fields1 $$ ppr arg_vars )
(LHsRecField GhcTc (Located (Pat GhcTc)) -> Id)
-> [LHsRecField GhcTc (Located (Pat GhcTc))] -> [Id]
forall a b. (a -> b) -> [a] -> [b]
map LHsRecField GhcTc (Located (Pat GhcTc)) -> Id
lookup_fld [LHsRecField GhcTc (Located (Pat GhcTc))]
rpats
| Bool
otherwise
= [Id]
arg_vars
where
fld_var_env :: NameEnv Id
fld_var_env = [(Name, Id)] -> NameEnv Id
forall a. [(Name, a)] -> NameEnv a
mkNameEnv ([(Name, Id)] -> NameEnv Id) -> [(Name, Id)] -> NameEnv Id
forall a b. (a -> b) -> a -> b
$ String -> [Name] -> [Id] -> [(Name, Id)]
forall a b. String -> [a] -> [b] -> [(a, b)]
zipEqual String
"get_arg_vars" [Name]
fields1 [Id]
arg_vars
lookup_fld :: LHsRecField GhcTc (Located (Pat GhcTc)) -> Id
lookup_fld (L SrcSpan
_ HsRecField GhcTc (Located (Pat GhcTc))
rpat) = NameEnv Id -> Name -> Id
forall a. NameEnv a -> Name -> a
lookupNameEnv_NF NameEnv Id
fld_var_env
(Id -> Name
idName (GenLocated SrcSpan Id -> Id
forall l e. GenLocated l e -> e
unLoc (HsRecField GhcTc (Located (Pat GhcTc)) -> GenLocated SrcSpan Id
forall arg. HsRecField GhcTc arg -> GenLocated SrcSpan Id
hsRecFieldId HsRecField GhcTc (Located (Pat GhcTc))
rpat)))
select_arg_vars [Id]
_ [] = String -> [Id]
forall a. String -> a
panic String
"matchOneCon/select_arg_vars []"
compatible_pats :: (ConArgPats,a) -> (ConArgPats,a) -> Bool
compatible_pats :: forall a. (ConArgPats, a) -> (ConArgPats, a) -> Bool
compatible_pats (RecCon HsRecFields GhcTc (LPat GhcTc)
flds1, a
_) (RecCon HsRecFields GhcTc (LPat GhcTc)
flds2, a
_) = HsRecFields GhcTc (LPat GhcTc)
-> HsRecFields GhcTc (LPat GhcTc) -> Bool
same_fields HsRecFields GhcTc (LPat GhcTc)
flds1 HsRecFields GhcTc (LPat GhcTc)
flds2
compatible_pats (RecCon HsRecFields GhcTc (LPat GhcTc)
flds1, a
_) (ConArgPats, a)
_ = [LHsRecField GhcTc (Located (Pat GhcTc))] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (HsRecFields GhcTc (Located (Pat GhcTc))
-> [LHsRecField GhcTc (Located (Pat GhcTc))]
forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds HsRecFields GhcTc (Located (Pat GhcTc))
HsRecFields GhcTc (LPat GhcTc)
flds1)
compatible_pats (ConArgPats, a)
_ (RecCon HsRecFields GhcTc (LPat GhcTc)
flds2, a
_) = [LHsRecField GhcTc (Located (Pat GhcTc))] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (HsRecFields GhcTc (Located (Pat GhcTc))
-> [LHsRecField GhcTc (Located (Pat GhcTc))]
forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds HsRecFields GhcTc (Located (Pat GhcTc))
HsRecFields GhcTc (LPat GhcTc)
flds2)
compatible_pats (ConArgPats, a)
_ (ConArgPats, a)
_ = Bool
True
same_fields :: HsRecFields GhcTc (LPat GhcTc) -> HsRecFields GhcTc (LPat GhcTc)
-> Bool
same_fields :: HsRecFields GhcTc (LPat GhcTc)
-> HsRecFields GhcTc (LPat GhcTc) -> Bool
same_fields HsRecFields GhcTc (LPat GhcTc)
flds1 HsRecFields GhcTc (LPat GhcTc)
flds2
= (LHsRecField GhcTc (Located (Pat GhcTc))
-> LHsRecField GhcTc (Located (Pat GhcTc)) -> Bool)
-> [LHsRecField GhcTc (Located (Pat GhcTc))]
-> [LHsRecField GhcTc (Located (Pat GhcTc))]
-> Bool
forall a b. (a -> b -> Bool) -> [a] -> [b] -> Bool
all2 (\(L SrcSpan
_ HsRecField GhcTc (Located (Pat GhcTc))
f1) (L SrcSpan
_ HsRecField GhcTc (Located (Pat GhcTc))
f2)
-> GenLocated SrcSpan Id -> Id
forall l e. GenLocated l e -> e
unLoc (HsRecField GhcTc (Located (Pat GhcTc)) -> GenLocated SrcSpan Id
forall arg. HsRecField GhcTc arg -> GenLocated SrcSpan Id
hsRecFieldId HsRecField GhcTc (Located (Pat GhcTc))
f1) Id -> Id -> Bool
forall a. Eq a => a -> a -> Bool
== GenLocated SrcSpan Id -> Id
forall l e. GenLocated l e -> e
unLoc (HsRecField GhcTc (Located (Pat GhcTc)) -> GenLocated SrcSpan Id
forall arg. HsRecField GhcTc arg -> GenLocated SrcSpan Id
hsRecFieldId HsRecField GhcTc (Located (Pat GhcTc))
f2))
(HsRecFields GhcTc (Located (Pat GhcTc))
-> [LHsRecField GhcTc (Located (Pat GhcTc))]
forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds HsRecFields GhcTc (Located (Pat GhcTc))
HsRecFields GhcTc (LPat GhcTc)
flds1) (HsRecFields GhcTc (Located (Pat GhcTc))
-> [LHsRecField GhcTc (Located (Pat GhcTc))]
forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds HsRecFields GhcTc (Located (Pat GhcTc))
HsRecFields GhcTc (LPat GhcTc)
flds2)
selectConMatchVars :: [Scaled Type] -> ConArgPats -> DsM [Id]
selectConMatchVars :: [Scaled Type] -> ConArgPats -> DsM [Id]
selectConMatchVars [Scaled Type]
arg_tys ConArgPats
con = case ConArgPats
con of
(RecCon {}) -> [Scaled Type] -> DsM [Id]
newSysLocalsDsNoLP [Scaled Type]
arg_tys
(PrefixCon [LPat GhcTc]
ps) -> [(Type, Pat GhcTc)] -> DsM [Id]
selectMatchVars ([Scaled Type] -> [Located (Pat GhcTc)] -> [(Type, Pat GhcTc)]
forall {a} {l} {b}. [Scaled a] -> [GenLocated l b] -> [(Type, b)]
zipMults [Scaled Type]
arg_tys [Located (Pat GhcTc)]
[LPat GhcTc]
ps)
(InfixCon LPat GhcTc
p1 LPat GhcTc
p2) -> [(Type, Pat GhcTc)] -> DsM [Id]
selectMatchVars ([Scaled Type] -> [Located (Pat GhcTc)] -> [(Type, Pat GhcTc)]
forall {a} {l} {b}. [Scaled a] -> [GenLocated l b] -> [(Type, b)]
zipMults [Scaled Type]
arg_tys [Located (Pat GhcTc)
LPat GhcTc
p1, Located (Pat GhcTc)
LPat GhcTc
p2])
where
zipMults :: [Scaled a] -> [GenLocated l b] -> [(Type, b)]
zipMults = String
-> (Scaled a -> GenLocated l b -> (Type, b))
-> [Scaled a]
-> [GenLocated l b]
-> [(Type, b)]
forall a b c. String -> (a -> b -> c) -> [a] -> [b] -> [c]
zipWithEqual String
"selectConMatchVar" (\Scaled a
a GenLocated l b
b -> (Scaled a -> Type
forall a. Scaled a -> Type
scaledMult Scaled a
a, GenLocated l b -> b
forall l e. GenLocated l e -> e
unLoc GenLocated l b
b))
conArgPats :: [Scaled Type]
-> ConArgPats
-> [Pat GhcTc]
conArgPats :: [Scaled Type] -> ConArgPats -> [Pat GhcTc]
conArgPats [Scaled Type]
_arg_tys (PrefixCon [LPat GhcTc]
ps) = (Located (Pat GhcTc) -> Pat GhcTc)
-> [Located (Pat GhcTc)] -> [Pat GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map Located (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc [Located (Pat GhcTc)]
[LPat GhcTc]
ps
conArgPats [Scaled Type]
_arg_tys (InfixCon LPat GhcTc
p1 LPat GhcTc
p2) = [Located (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc Located (Pat GhcTc)
LPat GhcTc
p1, Located (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc Located (Pat GhcTc)
LPat GhcTc
p2]
conArgPats [Scaled Type]
arg_tys (RecCon (HsRecFields { rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField GhcTc (LPat GhcTc)]
rpats }))
| [LHsRecField GhcTc (Located (Pat GhcTc))] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LHsRecField GhcTc (Located (Pat GhcTc))]
[LHsRecField GhcTc (LPat GhcTc)]
rpats = (Type -> Pat GhcTc) -> [Type] -> [Pat GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map Type -> Pat GhcTc
forall p. XWildPat p -> Pat p
WildPat ((Scaled Type -> Type) -> [Scaled Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Scaled Type -> Type
forall a. Scaled a -> a
scaledThing [Scaled Type]
arg_tys)
| Bool
otherwise = (LHsRecField GhcTc (Located (Pat GhcTc)) -> Pat GhcTc)
-> [LHsRecField GhcTc (Located (Pat GhcTc))] -> [Pat GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map (Located (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc (Located (Pat GhcTc) -> Pat GhcTc)
-> (LHsRecField GhcTc (Located (Pat GhcTc)) -> Located (Pat GhcTc))
-> LHsRecField GhcTc (Located (Pat GhcTc))
-> Pat GhcTc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecField GhcTc (Located (Pat GhcTc)) -> Located (Pat GhcTc)
forall id arg. HsRecField' id arg -> arg
hsRecFieldArg (HsRecField GhcTc (Located (Pat GhcTc)) -> Located (Pat GhcTc))
-> (LHsRecField GhcTc (Located (Pat GhcTc))
-> HsRecField GhcTc (Located (Pat GhcTc)))
-> LHsRecField GhcTc (Located (Pat GhcTc))
-> Located (Pat GhcTc)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsRecField GhcTc (Located (Pat GhcTc))
-> HsRecField GhcTc (Located (Pat GhcTc))
forall l e. GenLocated l e -> e
unLoc) [LHsRecField GhcTc (Located (Pat GhcTc))]
[LHsRecField GhcTc (LPat GhcTc)]
rpats