effectful-core
Safe HaskellSafe-Inferred
LanguageGHC2021

Effectful.Internal.Env

Synopsis

The environment

data Env (es :: [Effect]) Source #

A strict (WHNF), thread local, mutable, extensible record indexed by types of kind Effect.

Warning: the environment is a mutable data structure and cannot be simultaneously used from multiple threads under any circumstances.

In order to pass it to a different thread, you need to perform a deep copy with the cloneEnv function.

Offers very good performance characteristics for most often performed operations:

  • Extending: O(n), where n is the size of the effect stack.
  • Shrinking: O(1).
  • Indexing via (:>): O(1)
  • Modification of a specific element: O(1).
  • Getting a tail: O(1).
  • Cloning: O(N), where N is the size of the Storage.

Constructors

Env 

Fields

data Ref Source #

Reference to the effect in Storage.

Constructors

Ref !Int !Version 

Instances

Instances details
Prim Ref Source # 
Instance details

Defined in Effectful.Internal.Env

data Storage Source #

A storage of effects.

Constructors

Storage 

Fields

StorageData

data StorageData Source #

Constructors

StorageData 

Fields

cloneStorage :: HasCallStack => IORef Storage -> IO (IORef Storage) Source #

Clone the storage to use it in a different thread.

Since: 2.7.0.0

replaceStorage :: Env es -> IORef Storage -> IO (Env es) Source #

Replace the storage of the environment.

Since: 2.7.0.0

backupStorageData :: HasCallStack => Env es -> IO StorageData Source #

Backup storage data of the environment.

It can be restored later with restoreStorageData.

Since: 2.7.0.0

restoreStorageData :: HasCallStack => StorageData -> Env es -> IO () Source #

Restore a copy of the StorageData.

The copy needs to be from the same Env as the target. It's consumed by this operation and must not be used afterwards.

Since: 2.5.0.0

Utils

data AnyRelinker Source #

Relinker in Storage.

data AnyEffect Source #

Effect in Storage.

Relinker

newtype Relinker :: (Effect -> Type) -> Effect -> Type where Source #

A function for relinking Env objects stored in the handlers and/or making a deep copy of the representation of the effect when cloning the environment.

Constructors

Relinker :: (HasCallStack => (forall es. Env es -> IO (Env es)) -> rep e -> IO (rep e)) -> Relinker rep e 

Dispatch

data Dispatch Source #

A type of dispatch. For more information consult the documentation in Effectful.Dispatch.Dynamic and Effectful.Dispatch.Static.

Constructors

Dynamic 
Static SideEffects 

data SideEffects Source #

Signifies whether core operations of a statically dispatched effect perform side effects. If an effect is marked as such, the runStaticRep family of functions will require the IOE effect to be in context via the MaybeIOE type family.

type family DispatchOf (e :: Effect) :: Dispatch Source #

Dispatch types of effects.

Instances

Instances details
type DispatchOf Fail Source # 
Instance details

Defined in Effectful.Internal.Monad

type DispatchOf IOE Source # 
Instance details

Defined in Effectful.Internal.Monad

type DispatchOf NonDet Source # 
Instance details

Defined in Effectful.Internal.Monad

type DispatchOf Prim Source # 
Instance details

Defined in Effectful.Internal.Monad

type DispatchOf (Error e) Source # 
Instance details

Defined in Effectful.Error.Static

type DispatchOf (Input i) Source # 
Instance details

Defined in Effectful.Input.Dynamic

type DispatchOf (Input i) Source # 
Instance details

Defined in Effectful.Input.Static

type DispatchOf (Input i) Source # 
Instance details

Defined in Effectful.Input.Static.Action

type DispatchOf (Error e) Source # 
Instance details

Defined in Effectful.Internal.Effect.Dynamic

type DispatchOf (Reader r) Source # 
Instance details

Defined in Effectful.Internal.Effect.Dynamic

type DispatchOf (State s) Source # 
Instance details

Defined in Effectful.Internal.Effect.Dynamic

type DispatchOf (Writer w) Source # 
Instance details

Defined in Effectful.Internal.Effect.Dynamic

type DispatchOf (Output o) Source # 
Instance details

Defined in Effectful.Output.Dynamic

type DispatchOf (Output o) Source # 
Instance details

Defined in Effectful.Output.Static.Action

type DispatchOf (Output o) Source # 
Instance details

Defined in Effectful.Output.Static.Local.Array

type DispatchOf (Output o) Source # 
Instance details

Defined in Effectful.Output.Static.Local.List

type DispatchOf (Output o) Source # 
Instance details

Defined in Effectful.Output.Static.Shared.Array

type DispatchOf (Output o) Source # 
Instance details

Defined in Effectful.Output.Static.Shared.List

type DispatchOf (Reader r) Source # 
Instance details

Defined in Effectful.Reader.Static

type DispatchOf (ReturnWith r) Source # 
Instance details

Defined in Effectful.ReturnWith.Dynamic

type DispatchOf (ReturnWith r) Source # 
Instance details

Defined in Effectful.ReturnWith.Static

type DispatchOf (State s) Source # 
Instance details

Defined in Effectful.State.Static.Local

type DispatchOf (State s) Source # 
Instance details

Defined in Effectful.State.Static.Shared

type DispatchOf (Writer w) Source # 
Instance details

Defined in Effectful.Writer.Static.Local

type DispatchOf (Writer w) Source # 
Instance details

Defined in Effectful.Writer.Static.Shared

type DispatchOf (Labeled label e) Source # 
Instance details

Defined in Effectful.Labeled

type DispatchOf (Labeled label e) = DispatchOf e
type DispatchOf (Provider e input f) Source # 
Instance details

Defined in Effectful.Provider

type DispatchOf (Provider e input f) = 'Dynamic
type DispatchOf (ProviderList providedEs input f) Source # 
Instance details

Defined in Effectful.Provider.List

type DispatchOf (ProviderList providedEs input f) = 'Dynamic

type family EffectRep (d :: Dispatch) :: Effect -> Type Source #

Internal representations of effects.

Instances

Instances details
type EffectRep 'Dynamic Source # 
Instance details

Defined in Effectful.Internal.Monad

type EffectRep ('Static sideEffects) Source # 
Instance details

Defined in Effectful.Internal.Monad

type EffectRep ('Static sideEffects) = StaticRep

Operations

emptyEnv :: HasCallStack => IO (Env '[]) Source #

Create an empty environment.

cloneEnv :: HasCallStack => Env es -> IO (Env es) Source #

Clone the environment to use it in a different thread.

sizeEnv :: Env es -> IO Int Source #

Get the current size of the environment.

tailEnv :: Env (e : es) -> IO (Env es) Source #

Access the tail of the environment.

Modification of the effect stack

consEnv Source #

Arguments

:: HasCallStack 
=> EffectRep (DispatchOf e) e

The representation of the effect.

-> Relinker (EffectRep (DispatchOf e)) e 
-> Env es 
-> IO (Env (e : es)) 

Extend the environment with a new data type.

unconsEnv :: HasCallStack => Env (e : es) -> IO () Source #

Shrink the environment by one data type.

The environment needs to come from consEnv, i.e. the intended usage is bracket (consEnv e f env) unconsEnv.

Note: after calling this function e from the input environment is no longer usable.

replaceEnv Source #

Arguments

:: forall e es. (HasCallStack, e :> es) 
=> EffectRep (DispatchOf e) e

The representation of the effect.

-> Relinker (EffectRep (DispatchOf e)) e 
-> Env es 
-> IO (Env es) 

Replace a specific effect in the stack with a new value.

Note: unlike in putEnv the value is not changed in place, so only the new environment will see it.

unreplaceEnv :: forall e es. (HasCallStack, e :> es) => Env es -> IO () Source #

Remove a reference to the replaced effect.

The environment needs to come from replaceEnv, i.e. the intended usage is bracket (replaceEnv e f env) unreplaceEnv.

Note: after calling this function the input environment is no longer usable.

subsumeEnv :: forall e es. e :> es => Env es -> IO (Env (e : es)) Source #

Reference an existing effect from the top of the stack.

injectEnv :: forall subEs es. Subset subEs es => Env es -> IO (Env subEs) Source #

Construct an environment containing a permutation (with possible duplicates) of a subset of effects from the input environment.

Data retrieval and update

getEnv Source #

Arguments

:: forall e es. (HasCallStack, e :> es) 
=> Env es

The environment.

-> IO (EffectRep (DispatchOf e) e) 

Extract a specific data type from the environment.

putEnv Source #

Arguments

:: forall e es. (HasCallStack, e :> es) 
=> Env es

The environment.

-> EffectRep (DispatchOf e) e 
-> IO () 

Replace the data type in the environment with a new value (in place).

stateEnv Source #

Arguments

:: forall e es a. (HasCallStack, e :> es) 
=> Env es

The environment.

-> (EffectRep (DispatchOf e) e -> (a, EffectRep (DispatchOf e) e)) 
-> IO a 

Modify the data type in the environment and return a value (in place).

modifyEnv Source #

Arguments

:: forall e es. (HasCallStack, e :> es) 
=> Env es

The environment.

-> (EffectRep (DispatchOf e) e -> EffectRep (DispatchOf e) e) 
-> IO () 

Modify the data type in the environment (in place).