HORIZON HASKELLDocslts/ghc-9.14.x45dd5372026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 45dd537 · 2026-10-11

Moduleeffectful-core-2.7.1.2GHC2021

Effectful.Internal.Monad

The Eff monad.

This module is intended for internal use only, and may change without warning in subsequent releases.

  • 13 types
  • 1 class
  • 34 values

The Eff monad

2 declarations
newtypenewtype Eff (es :: [Effect]) a
#

The Eff monad provides the implementation of a computation that performs an arbitrary set of effects. In Eff es a, es is a type-level list that contains all the effects that the computation may perform. For example, a computation that produces an Integer by consuming a String from the global environment and acting upon a single mutable value of type Bool would have the following type:

(Reader String :> es, State Bool :> es) => Eff es Integer

Abstracting over the list of effects with (:>):

  • Allows the computation to be used in functions that may perform other effects.

  • Allows the effects to be handled in any order.

Instances23MonadError, MonadReader, MonadState, MonadWriter, MonadBase, MonadBaseControl, …
  • (Show e, Error e :> es, MonadError e (Eff es)) => MonadError e (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Effect.Dynamic · orphan

    Instance included for compatibility with existing code.

  • (Reader r :> es, MonadReader r (Eff es)) => MonadReader r (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Effect.Dynamic · orphan

    Instance included for compatibility with existing code.

  • (State s :> es, MonadState s (Eff es)) => MonadState s (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Effect.Dynamic · orphan

    Instance included for compatibility with existing code.

  • (Monoid w, Writer w :> es, MonadWriter w (Eff es)) => MonadWriter w (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Effect.Dynamic · orphan

    Instance included for compatibility with existing code.

    Warning: pass is not implemented due to ambiguous semantics in presence of runtime exceptions, so calling it (also indirectly via censor, which is defined in terms of pass) results in a runtime error.

  • IOE :> es => MonadBase IO (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad

    Instance included for compatibility with existing code.

    Usage of liftIO is preferrable as it's a standard.

  • IOE :> es => MonadBaseControl IO (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad

    Instance included for compatibility with existing code.

    Usage of withEffToIO is preferrable as it allows specifying the UnliftStrategy on a case-by-case basis and has better error reporting.

    Note: the unlifting strategy for liftBaseWith is taken from the IOE context (see unliftStrategy).

  • Monad (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • Functor (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • MonadFix (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • Fail :> es => MonadFail (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • Applicative (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • NonDet :> es => Alternative (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • NonDet :> es => MonadPlus (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • IOE :> es => MonadIO (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • MonadCatch (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad

    Available without any effect requirements.

    This is the one instance of the three that would arguably benefit from requiring IOE (or a more specialized effect), as catching imprecise exceptions makes it possible to write non-deterministic pure functions with runPureEff. Unfortunately it can't, because MonadCatch is a superclass of MonadMask, which needs to be available unconditionally (see the note there).

    For the full discussion see issue #76.

  • MonadMask (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad

    Available without any effect requirements.

    This makes it possible to use cleanup functions such as bracket or finally anywhere, e.g. to restore a state on error:

    transactionally :: forall s es a. State s :> es => Eff es a -> Eff es a
    transactionally = bracketOnError (get @s) (put @s) . const
    

    Requiring IOE would make functions like the above impossible to write and force IOE to show up in application code that otherwise only needs more restricted effects, which would be a significant usability regression. On the other hand, requiring a more specialized effect would be annoying, since functions making use of MonadMask are ubiquitous.

  • MonadThrow (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad

    Available without any effect requirements.

    Gating it behind an effect (such as IOE or a more specialized effect) would accomplish nothing, since any Haskell expression is free to throw an exception with throw at any point.

  • Prim :> es => PrimMonad (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • IOE :> es => MonadUnliftIO (Eff es)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad

    Instance included for compatibility with existing code.

    Usage of withEffToIO is preferrable as it allows specifying the UnliftStrategy on a case-by-case basis and has better error reporting.

    Note: the unlifting strategy for withRunInIO is taken from the IOE context (see unliftStrategy).

  • Semigroup a => Semigroup (Eff es a)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • Monoid a => Monoid (Eff es a)Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • type PrimState (Eff es) = PrimStateEffDefined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • type StM (Eff es) a = aDefined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
valuerunPureEff :: HasCallStack => Eff '[] a -> a
#

Run a pure Eff computation.

For running computations with side effects see runEff.

Note: the computation runs in a separate thread as a workaround for #380.

Access to the internal representation

valueunsafeEff :: (Env es -> IO a) -> Eff es a
#

Access the underlying IO monad along with the environment.

This function is unsafe because it can be used to introduce arbitrary IO actions into pure Eff computations.

valueunsafeEff_ :: IO a -> Eff es a
#

Access the underlying IO monad.

This function is unsafe because it can be used to introduce arbitrary IO actions into pure Eff computations.

NonDet

1 declaration

Fail

1 declaration

IO

2 declarations
datadata IOE (a :: Type -> Type) b
#

Run arbitrary IO computations via MonadIO or MonadUnliftIO.

Note: it is not recommended to use this effect in application code as it is too liberal. Ideally, this is only used in handlers of more fine-grained effects.

Instances2DispatchOf, StaticRep

Prim

3 declarations
datadata Prim (a :: Type -> Type) b
#

Provide the ability to perform primitive state-transformer actions.

Instances2DispatchOf, StaticRep

Lifting

5 declarations
valueraise :: Eff es a -> Eff (e ': es) a
#

Lift an Eff computation into an effect stack with one more effect.

valueraiseWith
  1. :: HasCallStack
  2. => UnliftStrategy
  3. -> ((forall r. Eff (e ': es) r -> Eff es r) -> Eff es a)

    Continuation with the unlifting function in scope.

  4. -> Eff (e ': es) a
#

Lift an Eff computation into an effect stack with one more effect and create an unlifting function with the given strategy.

valuesubsume :: e :> es => Eff (e ': es) a -> Eff es a
#

Eliminate a duplicate effect from the top of the effect stack.

valueinject :: Subset subEs es => Eff subEs a -> Eff es a
#

Allow for running an effect stack subEs within es as long as subEs is a permutation (with possible duplicates) of a subset of es.

Generalizes raise and subsume.

Example3 expressions
data E1 :: Effectdata E2 :: Effectdata E3 :: Effect

It makes it possible to rearrange the effect stack however you like:

Example1 expression
:{  shuffle :: Eff (E3 : E1 : E2 : es) a -> Eff (E1 : E2 : E3 : es) a  shuffle = inject:}

It can also turn a monomorphic effect stack into a polymorphic one:

Example1 expression
:{  toPoly :: (E1 :> es, E2 :> es, E3 :> es) => Eff [E1, E2, E3] a -> Eff es a  toPoly = inject:}

Moreover, it allows for hiding specific effects from downstream:

Example1 expression
:{  onlyE1 :: Eff (E1 : es) a -> Eff (E1 : E2 : E3 : es) a  onlyE1 = inject:}
Example1 expression
:{  onlyE2 :: Eff (E2 : es) a -> Eff (E1 : E2 : E3 : es) a  onlyE2 = inject:}
Example1 expression
:{  onlyE3 :: Eff (E3 : es) a -> Eff (E1 : E2 : E3 : es) a  onlyE3 = inject:}

However, it's not possible to inject a computation into an incompatible effect stack:

Example1 expression
:{  coerceEs :: Eff es1 a -> Eff es2 a  coerceEs = inject:}......Couldn't match type ‘es1’ with ‘es2’...
classclass KnownPrefix es => Subset (subEs :: [Effect]) (es :: [Effect]) where
#

Provide evidence that subEs is a subset of es.

Instances3Subset
  • (KnownPrefix es, IsUnknownSuffixOf subEs es) => Subset subEs esDefined in effectful-core-2.7.1.2 · Effectful.Internal.Effect
  • KnownPrefix es => Subset '[] esDefined in effectful-core-2.7.1.2 · Effectful.Internal.Effect
  • (e :> es, Subset subEs es) => Subset (e ': subEs) esDefined in effectful-core-2.7.1.2 · Effectful.Internal.Effect

Unlifting

9 declarations
datadata UnliftStrategy
#

The strategy to use when unlifting Eff computations via withEffToIO or the localUnlift family.

Constructors

  • SeqUnlift

    The sequential strategy is the fastest and a default setting for IOE. Any attempt of calling the unlifting function in threads distinct from its creator will result in a runtime error.

  • SeqForkUnlift

    Like SeqUnlift, but all unlifted actions will be executed in a cloned environment.

    The main consequence is that thread local state is forked at the point of creation of the unlifting function and its modifications in unlifted actions will not affect the main thread of execution (and vice versa):

    Example3 expressions
    import Effectfulimport Effectful.State.Dynamic:{ action :: (IOE :> es, State Int :> es) => Eff es () action = do   modify @Int (+1)   withEffToIO SeqForkUnlift $ \unlift -> unlift $ modify @Int (+2)   modify @Int (+4):}
    Example1 expression
    runEff . execStateLocal @Int 0 $ action5
    Example1 expression
    runEff . execStateShared @Int 0 $ action7

    Because of this it's possible to safely use the unlifting function outside of the scope of effects it captures, e.g. by creating an IO action that executes effectful operations and running it later:

    Example1 expression
    :{  delayed :: UnliftStrategy -> IO (IO String)  delayed strategy = runEff . evalStateLocal "Hey" $ do    r <- withEffToIO strategy $ \unlift -> pure $ unlift get    modify (++ "!!!")    pure r:}

    This doesn't work with the SeqUnlift strategy because when the returned action runs, State is no longer in scope:

    Example1 expression
    join $ delayed SeqUnlift*** Exception: version (...) /= storageVersion (0)...

    However, it does with the SeqForkUnlift strategy:

    Example1 expression
    join $ delayed SeqForkUnlift"Hey"
  • ConcUnlift !Persistence !Limit

    The concurrent strategy makes it possible for the unlifting function to be called in threads distinct from its creator. See Persistence and Limit settings for more information.

Instances5Eq, Ord, Show, Generic, Rep
datadata Persistence
#

Persistence setting for the ConcUnlift strategy.

Different functions require different persistence strategies. Examples:

  • Lifting pooledMapConcurrentlyN from the unliftio library requires the Ephemeral strategy as we don't want jobs to share environment changes made by previous jobs run in the same worker thread.

  • Lifting forkIOWithUnmask requires the Persistent strategy, otherwise the unmasking function would start with a fresh environment each time it's called.

Both cases come down to what happens when the unlifting function is called more than once in the same thread. If a thread calls it only once, the Persistence setting makes no observable difference.

Example 1

Consider a thread that modifies thread local state, then inspects it with a second call to the unlifting function:

Example4 expressions
import Control.Concurrentimport Control.Monadimport Effectfulimport Effectful.State.Dynamic
Example1 expression
:{  modifyThenGet :: UnliftStrategy -> IO Int  modifyThenGet strategy = runEff . evalStateLocal @Int 0 $ do    withEffToIO strategy $ \unlift -> do      result <- newEmptyMVar      void . forkIO $ do        unlift $ modify @Int (+1)        putMVar result =<< unlift (get @Int)      takeMVar result:}

With the Persistent strategy the unlifting function keeps the environment between the calls, so the second call sees the modification from the first one:

Example1 expression
modifyThenGet $ ConcUnlift Persistent (Limited 1)1

On the other hand, with Ephemeral each call to the unlifting function starts with a fresh copy of the environment, so the modification is silently lost:

Example1 expression
modifyThenGet $ ConcUnlift Ephemeral (Limited 2)0

This also showcases the limit meaning different things for the two settings: for the Persistent strategy it limits the number of threads the unlifting can happen in, for Ephemeral it limits the number of calls to the unlifting function.

Example 2

Consider a situation where a single worker thread runs multiple independent jobs:

Example1 expression
:{  twoJobs :: UnliftStrategy -> IO [Int]  twoJobs strategy = runEff . evalStateLocal @Int 0 $ do    withEffToIO strategy $ \unlift -> do      result <- newEmptyMVar      void . forkIO $ do        let job = unlift $ modify @Int (+1) >> get @Int        putMVar result =<< sequence [job, job]      takeMVar result:}

With Ephemeral both jobs start from the environment as it was when the unlifting function was created:

Example1 expression
twoJobs $ ConcUnlift Ephemeral Unlimited[1,1]

With Persistent the second job inherits changes made by the first one, even though the user would most likely expect them to be independent:

Example1 expression
twoJobs $ ConcUnlift Persistent Unlimited[1,2]

Constructors

  • Ephemeral

    Don't persist the environment between calls to the unlifting function in threads distinct from its creator.

  • Persistent

    Persist the environment between calls to the unlifting function within a particular thread.

Instances5Eq, Ord, Show, Generic, Rep
datadata Limit
#

Limit setting for the ConcUnlift strategy.

Constructors

  • Limited !Int

    Behavior dependent on the Persistence setting.

    For Ephemeral, it limits the amount of uses of the unlifting function in threads distinct from its creator to N. The unlifting function will create N copies of the environment when called N times and K+1 copies when called K < N times.

    For Persistent, it limits the amount of threads, distinct from the creator of the unlifting function, it can be called in to N. The amount of calls to the unlifting function within a particular threads is unlimited. The unlifting function will create N copies of the environment when called in N threads and K+1 copies when called in K < N threads.

  • Unlimited

    Unlimited use of the unlifting function.

Instances5Eq, Ord, Show, Generic, Rep
valuewithSeqEffToIO
  1. :: (HasCallStack, IOE :> es)
  2. => ((forall r. Eff es r -> IO r) -> IO a)

    Continuation with the unlifting function in scope.

  3. -> Eff es a
#

Create an unlifting function with the SeqUnlift strategy. For the general version see withEffToIO.

Note: usage of this function is preferrable to withRunInIO because of explicit unlifting strategy and better error reporting.

valuewithEffToIO
  1. :: (HasCallStack, IOE :> es)
  2. => UnliftStrategy
  3. -> ((forall r. Eff es r -> IO r) -> IO a)

    Continuation with the unlifting function in scope.

  4. -> Eff es a
#

Create an unlifting function with the given strategy.

Note: usage of this function is preferrable to withRunInIO because of explicit unlifting strategy and better error reporting.

valuereallyUnsafeLiftMapIO :: (IO a -> IO b) -> Eff es a -> Eff es b
#

Utility for lifting IO computations of type

IO a -> IO b

to

Eff es a -> Eff es b

This function is really unsafe because:

  • It can be used to introduce arbitrary IO actions into pure Eff computations.

  • The IO computation must run its argument in a way that's perceived as sequential to the outside observer, e.g. in the same thread or in a worker thread that finishes before the argument is run again.

Warning: if you disregard the second point, you will experience weird bugs, data races or internal consistency check failures.

When in doubt, use unsafeLiftMapIO, especially since this version saves only a simple safety check per call of reallyUnsafeLiftMapIO f.

valuereallyUnsafeUnliftIO :: ((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
#

Create an unlifting function.

This function is really unsafe because:

  • It can be used to introduce arbitrary IO actions into pure Eff computations.

  • Unlifted Eff computations must be run in a way that's perceived as sequential to the outside observer, e.g. in the same thread as the caller of reallyUnsafeUnliftIO or in a worker thread that finishes before another unlifted computation is run.

Warning: if you disregard the second point, you will experience weird bugs, data races or internal consistency check failures.

When in doubt, use unsafeSeqUnliftIO, especially since this version saves only a simple safety check per call of the unlifting function.

Low-level unlifts

valueseqForkUnliftsIO
  1. :: HasCallStack
  2. => Env es
  3. -> Env localEs
  4. -> ((forall r. Eff es r -> IO r) -> (forall r. Eff localEs r -> IO r) -> IO a)

    Continuation with the unlifting functions in scope.

  5. -> IO a
#

Create two unlifting functions with the SeqForkUnlift strategy.

The unlifting functions will share the effect storage (unlike with two separate calls to seqForkUnliftIO).

Warning: both environments must have the same underlying storage.

valueconcUnliftsIO
  1. :: HasCallStack
  2. => Env es
  3. -> Env localEs

    The environment.

  4. -> Persistence
  5. -> Limit
  6. -> ((forall r. Eff es r -> IO r) -> (forall r. Eff localEs r -> IO r) -> IO a)

    Continuation with the unlifting functions in scope.

  7. -> IO a
#

Create unlifting functions with the ConcUnlift strategy.

In the Persistent variant the unlifting functions will share the effect storage in each thread (unlike with two separate calls to concUnliftIO).

Warning: both environments must have the same underlying storage.

Dispatch

0 declarations

Dynamic dispatch

newtypenewtype LocalEnv (localEs :: [Effect])
#

Opaque representation of the Eff environment at the point of calling the send function, i.e. right before the control is passed to the effect handler.

Note: functions that consume it perform runtime checks to ensure that it's used within the scope of the effect handler it belongs to.

datadata Handler (a :: Effect) where
#

An internal representation of dynamically dispatched effects, i.e. the effect handler bundled with its environment.

Constructors

Static dispatch

data familydata family StaticRep (e :: Effect)
#

Internal representations of statically dispatched effects.

Instances17StaticRep, …
  • data StaticRep IOEDefined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • data StaticRep Prim
    • Prim
    Defined in effectful-core-2.7.1.2 · Effectful.Internal.Monad
  • data StaticRep (Error e)
    • Error ErrorId
    Defined in effectful-core-2.7.1.2 · Effectful.Error.Static
  • data StaticRep (Input i)
    • Input i
    Defined in effectful-core-2.7.1.2 · Effectful.Input.Static
  • data StaticRep (Input i)
    • Input :: !Env inputEs -> !InputImpl i inputEs -> R:StaticRepInput i
    Defined in effectful-core-2.7.1.2 · Effectful.Input.Static.Action
  • data StaticRep (Labeled label e)Defined in effectful-core-2.7.1.2 · Effectful.Labeled
  • data StaticRep (Output o)
    • Output :: !Env actionEs -> !OutputImpl o actionEs -> R:StaticRepOutput o
    Defined in effectful-core-2.7.1.2 · Effectful.Output.Static.Action
  • data StaticRep (Output o)Defined in effectful-core-2.7.1.2 · Effectful.Output.Static.Local.Array
  • data StaticRep (Output o)
    • Output [o]
    Defined in effectful-core-2.7.1.2 · Effectful.Output.Static.Local.List
  • data StaticRep (Output o)
    • Output (MVar (OutputData o))
    Defined in effectful-core-2.7.1.2 · Effectful.Output.Static.Shared.Array
  • data StaticRep (Output o)Defined in effectful-core-2.7.1.2 · Effectful.Output.Static.Shared.List
  • data StaticRep (Reader r)
    • Reader r
    Defined in effectful-core-2.7.1.2 · Effectful.Reader.Static
  • data StaticRep (ReturnWith r)
    • ReturnWith ReturnWithId
    Defined in effectful-core-2.7.1.2 · Effectful.ReturnWith.Static
  • data StaticRep (State s)
    • State s
    Defined in effectful-core-2.7.1.2 · Effectful.State.Static.Local
  • data StaticRep (State s)Defined in effectful-core-2.7.1.2 · Effectful.State.Static.Shared
  • data StaticRep (Writer w)
    • Writer w
    Defined in effectful-core-2.7.1.2 · Effectful.Writer.Static.Local
  • data StaticRep (Writer w)Defined in effectful-core-2.7.1.2 · Effectful.Writer.Static.Shared