-
Notifications
You must be signed in to change notification settings - Fork 53
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
🔥 Pure & PureC #307
Conversation
There was a problem hiding this 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:
-
Every monad
m
could be an algebra forLift m
. -
The
Algebra
instance forIdentity
obsoletesPureC
. -
Lift Identity
is an equivalent effect toPure
.
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 |
There was a problem hiding this comment.
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!
-- | A carrier for pure effects, used to kick off a stack of effects with 'run'. | ||
-- | ||
-- @since 1.0.0.0 | ||
module Control.Carrier.Pure |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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'. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
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 forpure x
, but by contrast,Pure
doesn’t offer any operations at all—making it kind of a degenerate edge case. -
I love that the first sentence is now true in two ways: if
Lift
occurs, it’s the final effect; andLift
now occurs in all (ok, most, since Base instances #206) stacks!
As the Beatles sang,
|
There was a problem hiding this 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 {} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 #-} |
There was a problem hiding this comment.
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) | ||
|
||
|
There was a problem hiding this comment.
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.
-- | A carrier for pure effects, used to kick off a stack of effects with 'run'. | ||
-- | ||
-- @since 1.0.0.0 | ||
module Control.Carrier.Pure |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well-reasoned.
This PR:
PureC
.Pure
.