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
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@

- Removes `resetFresh`, as it was unsafe. Greater safety _and_ control over the generation of fresh values can be obtained by use of `runFresh`. ([#267](https://github.com/fused-effects/fused-effects/pull/267))

- Removes `PureC`; `Data.Functor.Identity.Identity` should be used instead. Note that `run` is still provided as a convenient synonym for `runIdentity`. ([#307](https://github.com/fused-effects/fused-effects/pull/307))

- Removes the `Pure` effect. It’s unlikely that this will require changes, as `Pure` had no operations, but `Lift Identity` should be used instead. ([#307](https://github.com/fused-effects/fused-effects/pull/307))

# v0.5.0.1

- Adds support for ghc 8.8.1.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ example3 = run . runReader "hello" . runState 0 $ do
put (length (list :: String))
```

`run` is itself actually an effect handler for the `Pure` effect, which has no operations and thus can only represent a final result value.
`run` is itself actually an effect handler for the `Lift Identity` effect, whose only operation is to lift a result value into a computation.

Alternatively, arbitrary `Monad`s can be embedded into effectful computations using the `Lift` effect. In this case, the underlying `Monad`ic computation can be extracted using `runM`. Here, we use the `MonadIO` instance for the `LiftC` carrier to lift `putStrLn` into the middle of our computation:

Expand Down
2 changes: 1 addition & 1 deletion benchmark/NonDet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module NonDet
( benchmark
) where

import Control.Algebra
import qualified Control.Carrier.NonDet.Church as NonDet.Church
import Control.Carrier.Pure
import Gauge hiding (benchmark)
import qualified NonDet.NQueens as NQueens

Expand Down
2 changes: 0 additions & 2 deletions fused-effects.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ library
Control.Carrier.Interpret
Control.Carrier.Lift
Control.Carrier.NonDet.Church
Control.Carrier.Pure
Control.Carrier.Reader
Control.Carrier.State.Lazy
Control.Carrier.State.Strict
Expand All @@ -77,7 +76,6 @@ library
Control.Effect.Fresh
Control.Effect.Lift
Control.Effect.NonDet
Control.Effect.Pure
Control.Effect.Reader
Control.Effect.State
Control.Effect.Sum
Expand Down
20 changes: 11 additions & 9 deletions src/Control/Algebra.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE ConstraintKinds, DeriveFunctor, EmptyCase, FlexibleInstances, FunctionalDependencies, RankNTypes, TypeOperators, UndecidableInstances #-}
{-# LANGUAGE ConstraintKinds, DeriveFunctor, FlexibleInstances, FunctionalDependencies, RankNTypes, TypeOperators, UndecidableInstances #-}

{- | The 'Algebra' class is the mechanism with which effects are interpreted.

Expand All @@ -8,25 +8,23 @@ An instance of the 'Algebra' class defines an interpretation of an effect signat
-}
module Control.Algebra
( Algebra(..)
, run
, Has
, send
, handleIdentity
, handleCoercible
-- * Re-exports
, (:+:) (..)
, run
, module Control.Effect.Class
) where

import Control.Carrier.Pure (PureC, run)
import Control.Effect.Catch.Internal
import Control.Effect.Choose.Internal
import Control.Effect.Class
import Control.Effect.Empty.Internal
import Control.Effect.Error.Internal
import Control.Effect.Lift.Internal
import Control.Effect.NonDet.Internal
import Control.Effect.Pure
import Control.Effect.Reader.Internal
import Control.Effect.State.Internal
import Control.Effect.Sum ((:+:)(..), Member(..), Members)
Expand Down Expand Up @@ -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 👍

{-# INLINE alg #-}

-- | Run an action exhausted of effects to produce its final result value.
--
-- @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.



-- | @m@ is a carrier for @sig@ containing @eff@.
Expand Down Expand Up @@ -103,8 +105,8 @@ handleCoercible = handleIdentity coerce
instance Algebra (Lift IO) IO where
alg = join . unLift

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’t hold for any Monad m that you could write an Algebra (Lift m) m instance—and indeed it would!


instance Algebra Choose NonEmpty where
alg (Choose m) = m True S.<> m False
Expand Down
62 changes: 0 additions & 62 deletions src/Control/Carrier/Pure.hs

This file was deleted.

3 changes: 2 additions & 1 deletion src/Control/Effect/Lift.hs
Original file line number Diff line number Diff line change
@@ -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’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!


Predefined carriers:

* "Control.Carrier.Lift"
* 'IO'
* 'Data.Functor.Identity.Identity'

@since 0.1.0.0
-}
Expand Down
27 changes: 0 additions & 27 deletions src/Control/Effect/Pure.hs

This file was deleted.

12 changes: 5 additions & 7 deletions test/Gen.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{-# LANGUAGE DataKinds, DeriveFunctor, DeriveGeneric, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, KindSignatures, LambdaCase, PatternSynonyms, RankNTypes, ScopedTypeVariables, StandaloneDeriving, TypeApplications, TypeOperators, UndecidableInstances, ViewPatterns #-}
{-# OPTIONS_GHC -Wno-identities #-}
module Gen
( module Control.Carrier.Pure
, Identity(..)
( module Data.Functor.Identity
-- * Polymorphic generation & instantiation
, m
, GenM
Expand Down Expand Up @@ -59,7 +58,6 @@ module Gen
) where

import Control.Applicative
import Control.Carrier.Pure
import Control.Monad.Trans.Class
import Control.Monad.Trans.Writer
import Data.Foldable (traverse_)
Expand Down Expand Up @@ -183,18 +181,18 @@ subtermM2 t1 t2 f = Comp1 (Hedgehog.subtermM2 (unComp1 t1) (unComp1 t2) (fmap un


-- | This captures the shape of the handler function passed to the "Monad" & "MonadFix" tests.
newtype Run f g m = Run (forall a . f (m a) -> PureC (g a))
newtype Run f g m = Run (forall a . f (m a) -> Identity (g a))

-- | Handlers with output state, but no input state (e.g. 'Control.Carrier.Error.Either.ErrorC').
runL :: (forall a . m a -> PureC (f a)) -> Run Identity f m
runL :: (forall a . m a -> Identity (f a)) -> Run Identity f m
runL run = Run (run . runIdentity)

-- | Handlers with input state, but no output state (e.g. 'Control.Carrier.Reader.ReaderC').
runR :: (forall a . f (m a) -> PureC a) -> Run f Identity m
runR :: (forall a . f (m a) -> Identity a) -> Run f Identity m
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.

Expand Down