Skip to content

Commit

Permalink
fix: don't inline membership' (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
isovector authored Dec 22, 2022
1 parent 1b6ae7b commit c7e7b22
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/Polysemy/Internal/Union.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ data Union (r :: EffectRow) (mWoven :: Type -> Type) a where

instance Functor (Union r mWoven) where
fmap f (Union w t) = Union w $ f <$> t
{-# INLINE fmap #-}
{-# INLINABLE fmap #-}


data Weaving e mAfter resultType where
Expand Down Expand Up @@ -104,7 +104,7 @@ data Weaving e mAfter resultType where

instance Functor (Weaving e m) where
fmap f (Weaving e s d f' v) = Weaving e s d (f . f') v
{-# INLINE fmap #-}
{-# INLINABLE fmap #-}



Expand All @@ -121,7 +121,7 @@ weave s' d v' (Union w (Weaving e s nt f v)) =
(fmap Compose . d . fmap nt . getCompose)
(fmap f . getCompose)
(v <=< v' . getCompose)
{-# INLINE weave #-}
{-# INLINABLE weave #-}


hoist
Expand All @@ -130,7 +130,7 @@ hoist
-> Union r n a
hoist f' (Union w (Weaving e s nt f v)) =
Union w $ Weaving e s (f' . nt) f v
{-# INLINE hoist #-}
{-# INLINABLE hoist #-}

------------------------------------------------------------------------------
-- | A proof that @e@ is an element of @r@.
Expand Down Expand Up @@ -193,11 +193,9 @@ class Member (t :: Effect) (r :: EffectRow) where

instance {-# OVERLAPPING #-} Member t (t ': z) where
membership' = Here
{-# INLINE membership' #-}

instance Member t z => Member t (_1 ': z) where
membership' = There $ membership' @t @z
{-# INLINE membership' #-}

------------------------------------------------------------------------------
-- | A class for effect rows whose elements are inspectable.
Expand All @@ -211,27 +209,27 @@ class KnownRow r where

instance KnownRow '[] where
tryMembership' = Nothing
{-# INLINE tryMembership' #-}
{-# INLINABLE tryMembership' #-}

instance (Typeable e, KnownRow r) => KnownRow (e ': r) where
tryMembership' :: forall e'. Typeable e' => Maybe (ElemOf e' (e ': r))
tryMembership' = case eqT @e @e' of
Just Refl -> Just Here
_ -> There <$> tryMembership' @r @e'
{-# INLINE tryMembership' #-}
{-# INLINABLE tryMembership' #-}

------------------------------------------------------------------------------
-- | Given @'Member' e r@, extract a proof that @e@ is an element of @r@.
membership :: Member e r => ElemOf e r
membership = membership'
{-# INLINE membership #-}
{-# INLINABLE membership #-}

------------------------------------------------------------------------------
-- | Extracts a proof that @e@ is an element of @r@ if that
-- is indeed the case; otherwise returns @Nothing@.
tryMembership :: forall e r. (Typeable e, KnownRow r) => Maybe (ElemOf e r)
tryMembership = tryMembership' @r @e
{-# INLINE tryMembership #-}
{-# INLINABLE tryMembership #-}


------------------------------------------------------------------------------
Expand All @@ -241,7 +239,7 @@ tryMembership = tryMembership' @r @e
extendMembershipLeft :: forall l r e. SList l -> ElemOf e r -> ElemOf e (Append l r)
extendMembershipLeft SEnd pr = pr
extendMembershipLeft (SCons l) pr = There (extendMembershipLeft l pr)
{-# INLINE extendMembershipLeft #-}
{-# INLINABLE extendMembershipLeft #-}


------------------------------------------------------------------------------
Expand All @@ -250,7 +248,7 @@ extendMembershipLeft (SCons l) pr = There (extendMembershipLeft l pr)
extendMembershipRight :: forall l r e. ElemOf e l -> ElemOf e (Append l r)
extendMembershipRight Here = Here
extendMembershipRight (There e) = There (extendMembershipRight @_ @r e)
{-# INLINE extendMembershipRight #-}
{-# INLINABLE extendMembershipRight #-}


------------------------------------------------------------------------------
Expand All @@ -265,7 +263,7 @@ injectMembership :: forall right e left mid
injectMembership SEnd sm pr = extendMembershipLeft sm pr
injectMembership (SCons _) _ Here = Here
injectMembership (SCons sl) sm (There pr) = There (injectMembership @right sl sm pr)
{-# INLINE injectMembership #-}
{-# INLINABLE injectMembership #-}


------------------------------------------------------------------------------
Expand All @@ -276,14 +274,14 @@ decomp (Union p a) =
case p of
Here -> Right a
There pr -> Left $ Union pr a
{-# INLINE decomp #-}
{-# INLINABLE decomp #-}

------------------------------------------------------------------------------
-- | Retrieve the last effect in a 'Union'.
extract :: Union '[e] m a -> Weaving e m a
extract (Union Here a) = a
extract (Union (There _) _) = error "Unsafe use of UnsafeMkElemOf"
{-# INLINE extract #-}
{-# INLINABLE extract #-}


------------------------------------------------------------------------------
Expand All @@ -297,15 +295,15 @@ absurdU (Union _ _) = error "Unsafe use of UnsafeMkElemOf"
-- head.
weaken :: forall e r m a. Union r m a -> Union (e ': r) m a
weaken (Union pr a) = Union (There pr) a
{-# INLINE weaken #-}
{-# INLINABLE weaken #-}


------------------------------------------------------------------------------
-- | Weaken a 'Union' so it is capable of storing a number of new effects at
-- the head, specified as a singleton list proof.
weakenList :: SList l -> Union r m a -> Union (Append l r) m a
weakenList sl (Union pr e) = Union (extendMembershipLeft sl pr) e
{-# INLINE weakenList #-}
{-# INLINABLE weakenList #-}


------------------------------------------------------------------------------
Expand All @@ -317,7 +315,7 @@ weakenMid :: forall right m a left mid
-> Union (Append left right) m a
-> Union (Append left (Append mid right)) m a
weakenMid sl sm (Union pr e) = Union (injectMembership @right sl sm pr) e
{-# INLINE weakenMid #-}
{-# INLINABLE weakenMid #-}


------------------------------------------------------------------------------
Expand All @@ -329,7 +327,7 @@ inj e = injWeaving $ Weaving
(fmap Identity . runIdentity)
runIdentity
(Just . runIdentity)
{-# INLINE inj #-}
{-# INLINABLE inj #-}


------------------------------------------------------------------------------
Expand All @@ -343,13 +341,13 @@ injUsing pr e = Union pr $ Weaving
(fmap Identity . runIdentity)
runIdentity
(Just . runIdentity)
{-# INLINE injUsing #-}
{-# INLINABLE injUsing #-}

------------------------------------------------------------------------------
-- | Lift a @'Weaving' e@ into a 'Union' capable of holding it.
injWeaving :: forall e r m a. Member e r => Weaving e m a -> Union r m a
injWeaving = Union membership
{-# INLINE injWeaving #-}
{-# INLINABLE injWeaving #-}

------------------------------------------------------------------------------
-- | Attempt to take an @e@ effect out of a 'Union'.
Expand All @@ -359,7 +357,7 @@ prj :: forall e r m a
=> Union r m a
-> Maybe (Weaving e m a)
prj = prjUsing membership
{-# INLINE prj #-}
{-# INLINABLE prj #-}

------------------------------------------------------------------------------
-- | Attempt to take an @e@ effect out of a 'Union', given an explicit
Expand All @@ -370,7 +368,7 @@ prjUsing
-> Union r m a
-> Maybe (Weaving e m a)
prjUsing pr (Union sn a) = (\Refl -> a) <$> sameMember pr sn
{-# INLINE prjUsing #-}
{-# INLINABLE prjUsing #-}

------------------------------------------------------------------------------
-- | Like 'decomp', but allows for a more efficient
Expand All @@ -382,4 +380,4 @@ decompCoerce (Union p a) =
case p of
Here -> Right a
There pr -> Left (Union (There pr) a)
{-# INLINE decompCoerce #-}
{-# INLINABLE decompCoerce #-}

0 comments on commit c7e7b22

Please sign in to comment.