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鈥檒l 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
Contributor

@robrix robrix commented Oct 27, 2019

This PR:

  • 馃敟s PureC.
  • 馃敟s Pure.

Copy link
Contributor 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鈥檝e 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
Contributor 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鈥檛 hold for any Monad m that you could write an Algebra (Lift m) m instance鈥攁nd 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
Contributor 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鈥檚 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鈥檚 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
Contributor 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鈥檛 know how I鈥檇 never realized this before, but yes, Lift Identity is an equivalent terminal effect for pure code. sendM (Identity x) isn鈥檛 the most readable synonym for pure x, but by contrast, Pure doesn鈥檛 offer any operations at all鈥攎aking it kind of a degenerate edge case.

  2. I love that the first sentence is now true in two ways: if Lift occurs, it鈥檚 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
Contributor 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鈥攊t 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
Contributor 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鈥檓 honestly not sure.

No, we shouldn鈥檛. 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鈥檚 some underlying thing going on here that鈥檚 intensely beautiful but that I don鈥檛 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.

None yet

2 participants