-
Notifications
You must be signed in to change notification settings - Fork 52
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
馃敟 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鈥檝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 |
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鈥檛 hold for any Monad
m that you could write an Algebra (Lift m) m
instance鈥攁nd 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鈥檚 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
.
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鈥檛 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 forpure x
, but by contrast,Pure
doesn鈥檛 offer any operations at all鈥攎aking it kind of a degenerate edge case. -
I love that the first sentence is now true in two ways: if
Lift
occurs, it鈥檚 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鈥攊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 {} |
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鈥檓 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) | ||
|
||
|
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鈥檚 some underlying thing going on here that鈥檚 intensely beautiful but that I don鈥檛 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
.