Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion effectful-core/effectful-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ library
Effectful.Fail
Effectful.Internal.Effect
Effectful.Internal.Env
Effectful.Internal.MTL
Effectful.Internal.Monad
Effectful.Internal.Unlift
Effectful.Internal.Utils
Expand Down
1 change: 0 additions & 1 deletion effectful-core/src/Effectful.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ import Control.Monad.IO.Unlift

import Effectful.Internal.Effect
import Effectful.Internal.Env
import Effectful.Internal.MTL ()
import Effectful.Internal.Monad

-- $intro
Expand Down
2 changes: 1 addition & 1 deletion effectful-core/src/Effectful/Error/Dynamic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import GHC.Stack (withFrozenCallStack)
import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.Error.Static qualified as E
import Effectful.Internal.MTL (Error(..))
import Effectful.Internal.Monad (Error(..))

-- | Handle errors of type @e@ (via "Effectful.Error.Static").
runError
Expand Down
90 changes: 0 additions & 90 deletions effectful-core/src/Effectful/Internal/MTL.hs

This file was deleted.

96 changes: 96 additions & 0 deletions effectful-core/src/Effectful/Internal/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ module Effectful.Internal.Monad
, PrimStateEff
, runPrim

-- * Error
, Error(..)

-- * Reader
, Reader(..)

-- * State
, State(..)

-- * Writer
, Writer(..)

-- * Lifting
, raise
, raiseWith
Expand Down Expand Up @@ -83,11 +95,15 @@ import Control.Exception qualified as E
import Control.Monad
import Control.Monad.Base
import Control.Monad.Catch qualified as C
import Control.Monad.Except qualified as MTL
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.IO.Unlift
import Control.Monad.Primitive
import Control.Monad.Reader qualified as MTL
import Control.Monad.State qualified as MTL
import Control.Monad.Trans.Control
import Control.Monad.Writer qualified as MTL
import Data.Kind (Constraint)
import GHC.Exts (oneShot)
import GHC.IO (IO(..))
Expand Down Expand Up @@ -472,6 +488,86 @@ instance Prim :> es => PrimMonad (Eff es) where
type PrimState (Eff es) = PrimStateEff
primitive = unsafeEff_ . IO . unsafeCoerce

----------------------------------------
-- Error

-- | Provide the ability to handle errors of type @e@.
data Error e :: Effect where
-- | @since 2.4.0.0
ThrowErrorWith :: (e -> String) -> e -> Error e m a
CatchError :: m a -> (CallStack -> e -> m a) -> Error e m a

type instance DispatchOf (Error e) = Dynamic

-- | Instance included for compatibility with existing code.
instance
( Show e
, Error e :> es
, MTL.MonadError e (Eff es)
) => MTL.MonadError e (Eff es) where
throwError = send . ThrowErrorWith show
catchError action = send . CatchError action . const

----------------------------------------
-- Reader

data Reader r :: Effect where
Ask :: Reader r m r
Local :: (r -> r) -> m a -> Reader r m a

type instance DispatchOf (Reader r) = Dynamic

-- | Instance included for compatibility with existing code.
instance
( Reader r :> es
, MTL.MonadReader r (Eff es)
) => MTL.MonadReader r (Eff es) where
ask = send Ask
local f = send . Local f
reader f = f <$> send Ask

----------------------------------------
-- State

-- | Provide access to a mutable value of type @s@.
data State s :: Effect where
Get :: State s m s
Put :: s -> State s m ()
State :: (s -> (a, s)) -> State s m a
StateM :: (s -> m (a, s)) -> State s m a

type instance DispatchOf (State s) = Dynamic

-- | Instance included for compatibility with existing code.
instance
( State s :> es
, MTL.MonadState s (Eff es)
) => MTL.MonadState s (Eff es) where
get = send Get
put = send . Put
state = send . State

----------------------------------------
-- Writer

-- | Provide access to a write only value of type @w@.
data Writer w :: Effect where
Tell :: w -> Writer w m ()
Listen :: m a -> Writer w m (a, w)

type instance DispatchOf (Writer w) = Dynamic

-- | Instance included for compatibility with existing code.
instance
( Monoid w
, Writer w :> es
, MTL.MonadWriter w (Eff es)
) => MTL.MonadWriter w (Eff es) where
writer (a, w) = a <$ send (Tell w)
tell = send . Tell
listen = send . Listen
pass = error "pass is not implemented due to ambiguous semantics in presence of runtime exceptions"

----------------------------------------
-- Lifting

Expand Down
2 changes: 1 addition & 1 deletion effectful-core/src/Effectful/Reader/Dynamic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Effectful.Reader.Dynamic

import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.Internal.MTL (Reader(..))
import Effectful.Internal.Monad (Reader(..))

-- | Run the 'Reader' effect with the given initial environment.
runReader
Expand Down
2 changes: 1 addition & 1 deletion effectful-core/src/Effectful/State/Dynamic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Effectful.State.Dynamic

import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.Internal.MTL (State(..))
import Effectful.Internal.Monad (State(..))
import Effectful.State.Static.Local qualified as L
import Effectful.State.Static.Shared qualified as S

Expand Down
2 changes: 1 addition & 1 deletion effectful-core/src/Effectful/Writer/Dynamic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Effectful.Writer.Dynamic

import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.Internal.MTL (Writer(..))
import Effectful.Internal.Monad (Writer(..))
import Effectful.Writer.Static.Local qualified as L
import Effectful.Writer.Static.Shared qualified as S

Expand Down
Loading