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

🔥 Pure & PureC #307

Merged
merged 15 commits into from
Oct 28, 2019
Merged

🔥 Pure & PureC #307

merged 15 commits into from
Oct 28, 2019

Conversation

robrix
Copy link
Collaborator

@robrix robrix commented Oct 27, 2019

This PR:

  • 🔥s PureC.
  • 🔥s Pure.

Copy link
Collaborator Author

@robrix robrix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is the result of three observations by @patrickt:

  1. Every monad m could be an algebra for Lift m.

  2. The Algebra instance for Identity obsoletes PureC.

  3. Lift Identity is an equivalent effect to Pure.

I’ve been working on this thing for 14 months straight and @patrickt still manages to show me new things about it 💖

instance Algebra Pure Identity where
alg v = case v of {}
instance Algebra (Lift Identity) Identity where
alg = join . unLift
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I wrote the Algebra instance for IO, above, @patrickt asked if it wouldn’t hold for any Monad m that you could write an Algebra (Lift m) m instance—and indeed it would!

Comment on lines -3 to -6
-- | A carrier for pure effects, used to kick off a stack of effects with 'run'.
--
-- @since 1.0.0.0
module Control.Carrier.Pure
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I added the Algebra instance for Identity, @patrickt asked if we should eliminate PureC since it’s now redundant. At the time I felt like it would be better to keep our own definition around and define our own instances, but upon reflection this only makes interoperation with other libraries harder since it’s probable that our users are using run instead of runIdentity as the terminus of pure effect stacks. By eliminating PureC, and redefining run as a synonym for runIdentity, we not only reduce our surface area, but also operate seamlessly with anything else defining instances for Identity.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well-reasoned.

@@ -1,11 +1,12 @@
{- | Provides a mechanism to kick off the evaluation of an effect stack that takes place in a monadic context.

'Lift' effects are always the last effect in a given effect stack. These stacks are invoked with 'Control.Effect.Lift.runM'. The 'Control.Effect.Pure.Pure' effect is equivalent to @Lift Identity@.
'Lift' effects are always the last effect in a given effect stack. These stacks are invoked with 'Control.Carrier.Lift.runM' or 'Control.Algebra.run'.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I noticed the final sentence (added by @patrickt in Document the effects and their carriers. #251) yesterday, and it blew my mind. I don’t know how I’d never realized this before, but yes, Lift Identity is an equivalent terminal effect for pure code. sendM (Identity x) isn’t the most readable synonym for pure x, but by contrast, Pure doesn’t offer any operations at all—making it kind of a degenerate edge case.

  2. I love that the first sentence is now true in two ways: if Lift occurs, it’s the final effect; and Lift now occurs in all (ok, most, since Base instances #206) stacks!

@robrix robrix marked this pull request as ready for review October 27, 2019 12:24
@robrix robrix requested a review from patrickt October 27, 2019 12:24
@robrix robrix mentioned this pull request Oct 27, 2019
5 tasks
@robrix
Copy link
Collaborator Author

robrix commented Oct 27, 2019

As the Beatles sang,

All you need is Lift, Lift
Lift is all you need 🎵

@robrix robrix mentioned this pull request Oct 27, 2019
4 tasks
Copy link
Collaborator

@patrickt patrickt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are generous with your praise, especially given the fact that I never put any of these assertions together—it was you who realized that there exists a potential for a reduction in library surface area like this. Bravo!

@@ -55,9 +53,13 @@ class (Effect sig, Monad m) => Algebra sig m | m -> sig where
-- | Construct a value in the carrier for an effect signature (typically a sum of a handled effect and any remaining effects).
alg :: sig m a -> m a

instance Algebra Pure PureC where
alg v = case v of {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can ditch the EmptyCase extension in this file now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good call 👍

-- @since 1.0.0.0
run :: Identity a -> a
run = runIdentity
{-# INLINE run #-}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we deprecate this? I’m honestly not sure.

No, we shouldn’t. This is good, as per your comment below.

runR run = Run (fmap Identity . run)

-- | Handlers with curried input state (e.g. 'Control.Carrier.Reader.ReaderC', 'Control.Carrier.State.Strict.StateC').
runC :: (forall a . s -> m a -> PureC (f a)) -> Run ((,) s) f m
runC :: (forall a . s -> m a -> Identity (f a)) -> Run ((,) s) f m
runC run = Run (uncurry run)


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s some underlying thing going on here that’s intensely beautiful but that I don’t have the vocabulary to describe.

Comment on lines -3 to -6
-- | A carrier for pure effects, used to kick off a stack of effects with 'run'.
--
-- @since 1.0.0.0
module Control.Carrier.Pure
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well-reasoned.

@robrix robrix merged commit 2bd1fd5 into master Oct 28, 2019
@robrix robrix deleted the lift-identity-subsumes-pure branch October 28, 2019 01:00
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

Successfully merging this pull request may close these issues.

2 participants