MyNixOS website logo
Description

An easy to use, performant extensible effects library.

An easy to use, performant extensible effects library with seamless integration with the existing Haskell ecosystem. . This is the "batteries-included" variant. See the effectful-core package if you need a more limited dependency footprint or want to browse documentation of core modules.

effectful

Build Status Hackage Dependencies Stackage LTS Stackage Nightly

An easy to use, fast extensible effects library with seamless integration with the existing Haskell ecosystem.

Main features:

  1. Very fast (benchmarks).

  2. Easy to use API (comparable with usage of the MonadUnliftIO class).

  3. Correct semantics in presence of runtime exceptions (no more discarded state updates).

  4. Seamless integration with the existing ecosystem (exceptions, monad-control, unliftio-core, resourcet etc.).

  5. Support for thread local and shared state (e.g. StateT provides a thread local state, while MVar holds a shared state, both approaches have their merits).

  6. Support for statically (implementation determined at compile time) and dynamically (implementation determined at run time) dispatched effects.

Motivation

Do we really need yet another library for handling effects? There's freer-simple, fused-effects, polysemy, eff and probably a few more.

It needs to be noted that of all of them only the work-in-progress eff library is a promising proposition because of reasonable performance characteristics (see the talk Effects for Less for more information) and potential for good interoperability with the existing ecosystem.

The second point is arguably the most important, because it allows focusing on things that matter instead of reinventing all kinds of wheels, hence being a necessary condition for broader adoption of the library.

Unfortunately, the development of eff has stalled due to a fewsubtleissues related to its use of delimited continuations underneath.

What about mtl?

It's true that its "effects as classes" approach is widely known and used often.

However:

  • mtl style effects are slow.

  • The majority of popular monad transformers (except ReaderT) used for effect implementations are rife with subtle issues.

These are problematic enough that the ReaderT design pattern was invented. Its fundamentals are solid, but it's not an effect system.

A solution? Use the ReaderT pattern as a base and build around it to make an extensible effects library! This is where effectful comes in. The Eff monad it uses is essentially a ReaderT over IO on steroids, allowing us to extend its environment with data types representing effects.

This concept is quite simple, so:

  • It's reasonably easy to understand what is going on under the hood.

  • The Eff monad being a reader allows for seamless interoperability with ubiquitous classes such as MonadBaseControl and MonadUnliftIO and solves issues of monad transformers mentioned above.

What is more, the Eff monad is concrete, so GHC has many possibilities for optimization, which results in a very fast code at a default optimization level. There is no need to explicitly mark functions with INLINE pragmas or enable additional optimization passes, it just works.

Any downsides?

As always, there's no free lunch. The Eff monad doesn't support effect handlers that require the ability to suspend or capture the rest of the computation and resume it later (potentially multiple times). This prevents effectful from providing (in particular):

  • A NonDet effect handler that executes multiple Alternative branches and collects their results.

  • A Coroutine effect.

It needs to be noted however that such NonDet effect handler in existing libraries is broken and none of the ones with support for higher order effects provide the Coroutine effect, so arguably it's not a big loss.

If you need such capability in your application, there are well established libraries such as conduit or list-t that can be used with effectful without any hassle.

Summary

effectful is an extensible effects library that aims to be the replacement for:

  • The bare ReaderT pattern by being essentially its enriched version.

  • Monad transformer stacks typically encountered in the wild (i.e. consisting of a dozen of newtype'd ExceptT, ReaderT, StateT and WriterT transformers and their derivatives) by providing equivalent effects with improved semantics, performance, usability and making it easy to reuse them for your own effects.

It doesn't try to make monad transformers obsolete, so you're free to use it with ConduitT, ContT, ListT etc. when necessary.

Package structure

The library is split among several packages:

  • The effectful-core package contains the core of the library along with basic effects. It aims for a small dependency footprint and provides building blocks for more advanced effects.

  • The effectful-plugin package provides an optional GHC plugin for improving disambiguation of effects (see here for more information).

  • The effectful-th package provides utilities for generating bits of effect-related boilerplate via Template Haskell.

  • The effectful package re-exports public modules of effectful-core and additionally provides most features of the unliftio package divided into appropriate effects.

Examples

For the examples see the Introduction sections of Effectful.Dispatch.Dynamic and Effectful.Dispatch.Static (when in doubt, start with dynamic dispatch).

Acknowledgements

To all contributors of existing effect libraries - thank you for putting the time and effort to explore the space. In particular, conversations in issue trackers of cleff, eff, freer-simple, fused-effects and polysemy repositories were invaluable in helping me discover and understand challenges in the space.

Resources

Resources that inspired the rise of this library and had a lot of impact on its design.

Talks:

Blog posts:


Icons made by Freepik from www.flaticon.com.
Metadata

Version

2.3.1.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows