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.Writer.Static.Shared

Support for access to a write only value of a particular type.

The value is shared between multiple threads. If you want each thread to manage its own version of the value, use Effectful.Writer.Static.Local.

Warning: Writer's state will be accumulated via left-associated uses of <>, which makes it unsuitable for use with types for which such pattern is inefficient. This applies, in particular, to the standard list type, which makes the Writer effect pretty niche.

If you just want to accumulate values, use Effectful.Output.Static.Shared.Array or Effectful.Output.Static.Shared.List.

Note: while the Writer from the transformers package includes additional operations pass and censor, they don't cooperate with runtime exceptions very well, so they're deliberately omitted here.

  • 1 type
  • 5 values

Effect

1 declaration
datadata Writer w (a :: Type -> Type) b
#

Provide access to a strict (WHNF), shared, write only value of type w.

Instances2DispatchOf, StaticRep

Handlers

Operations

valuelisten
  1. :: (HasCallStack, Writer w :> es, Monoid w)
  2. => Eff es a
  3. -> Eff es (a, w)
#

Execute an action and append its output to the overall output of the Writer.

Note: the output of tell executed from threads spawned within the nested action is accounted for only if it completes before listen merges the output, which happens as soon as the action finishes. In particular, the output of threads that outlive the scope of listen will be lost:

Example1 expression
:{  runEff . execWriter @String $ do    lock <- liftIO newEmptyMVar    done <- liftIO newEmptyMVar    tell "1"    _ <- listen @String $ do      tell "2"      withEffToIO (ConcUnlift Ephemeral $ Limited 1) $ \unlift -> do        _ <- forkIO $ do          takeMVar lock          unlift $ tell "3"          putMVar done ()        pure ()    liftIO $ putMVar lock ()    liftIO $ takeMVar done    tell "4":}"124"

Note: if an exception is received while the action is executed, the partial output of the action will still be appended to the overall output of the Writer:

Example1 expression
:{  runEff . execWriter @String $ do    tell "Hi"    handle (\(_::ErrorCall) -> pure ((), "")) $ do      tell " there"      listen $ do        tell "!"        error "oops":}"Hi there!"