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

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Modulemonoidal-synchronisation-0.1.0.8Haskell2010

Data.Monoid.Synchronisation

  • 3 types
  • 4 values
newtypenewtype FirstToFinish (m :: Type -> Type) a
#

First-to-finish synchronisation. Like Alt it is a monoid under <|>. First-to-finish is the additive monoid of near-semiring for which multiplication is given by LastToFinish.

-- | Return first available value.
--
readFirstMVar :: [TMVar a] -> STM a
readFirstMVar = runFirstToFinish
              . foldMap (FirstToFinish . readTMVar)
Instances14Generic1, Monad, Functor, Applicative, Foldable, Traversable, …
newtypenewtype LastToFinish (m :: Type -> Type) a
#

Last-to-finish synchronisation. It is the multiplicative semigroup of the near-semiring for which addition is given by FirstToFinish.

This is similar to Ap (see LastToFinishM) in the sense that it will wait for all monadic effects, but unlike Ap it will not combine all results using a monoid instance, but rather it will return the last one. Also unlike Ap it does not have a monoidal unit.

-- | Read all 'TMVar's and return the one that was filled last.
--
readAllTMVars :: Monoid a => NonEmpty (TMVar a) -> STM a
readAllTMVars = runLastToFinish
              . foldMap1 (LastToFinish . readTMVar)
                -- from 'semigroupoids' package or use 'foldr1' and 'fmap'
                -- from 'base'

Constructors

Instances12Generic1, Monad, Functor, Applicative, Foldable, Traversable, …
newtypenewtype LastToFinishM (m :: Type -> Type) a
#

Last-to-finish synchronisation. Like Ap it is a monoid under <*>. The advantage over LastToFinish is that it has a Monoid instance, but a must be a Monoid as well. LastToFinishM and FirstToFinish form a unitial near-ring when m ~ STM.

-- | Read all 'TMVar's and combine the result using 'Monoid' instance.
--
readAllTMVars :: Monoid a => [TMVar a] -> STM a
readAllTMVars = runLastToFinish
              . foldMap (LastToFinish . readTMVar)
Instances13Generic1, Monad, Functor, Applicative, Foldable, Traversable, …