Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add oneShot to Codensity instances #78

Open
Icelandjack opened this issue Feb 26, 2024 · 1 comment
Open

Add oneShot to Codensity instances #78

Icelandjack opened this issue Feb 26, 2024 · 1 comment

Comments

@Icelandjack
Copy link
Contributor

The IOSim s Monad has the same instances as Codensity (SimA s) except with oneShot. Could be added to the Codensity instances, giving the compiler better hints and allowing the io-sim library to derive via Codensity.

newtype IOSim s a = IOSim { unIOSim :: forall r. (a -> SimA s r) -> SimA s r }

instance Functor (IOSim s) where
    {-# INLINE fmap #-}
    fmap f = \d -> IOSim $ oneShot $ \k -> unIOSim d (k . f)

instance Applicative (IOSim s) where
    {-# INLINE pure #-}
    {-# INLINE (<*>) #-}
    {-# INLINE (*>) #-}
    pure = \x -> IOSim $ oneShot $ \k -> k x
    (<*>) = \df dx -> IOSim $ oneShot $ \k -> unIOSim df (\f -> unIOSim dx (\x -> k (f x)))
    (*>) = \dm dn -> IOSim $ oneShot $ \k -> unIOSim dm (\_ -> unIOSim dn k)

instance Monad (IOSim s) where
    {-# INLINE (>>=) #-}
    {-# INLINE (>>) #-}
    return = pure
    (>>=) = \dm f -> IOSim $ oneShot $ \k -> unIOSim dm (\m -> unIOSim (f m) k)
    (>>) = (*>)
@RyanGlScott
Copy link
Collaborator

I'm in favor. GHC includes a vendored-in copy of Codensity here, which provides an example of how to define its instances using oneShot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants