A friendly effect system for Haskell
Go to file
Alexis King e5ef0fec4a Relax bounds on template-haskell to support GHC 8.10
This requires no code changes, so this simply tracks a relaxed bound via
revision in version 1.2.1.1.
2020-05-18 17:54:19 -05:00
bench Update stack.yml to LTS-12 2018-09-01 13:46:19 -05:00
examples/src Better support GHC 8.8 2019-10-04 19:12:37 -05:00
src Better support GHC 8.8 2019-10-04 19:12:37 -05:00
tests Generalize makeEffect to work with more complicated GADTs 2018-11-15 10:25:45 -06:00
.gitignore Reformat project to make the style a little more idiomatic 2017-12-06 11:49:45 -08:00
.travis.yml Better support GHC 8.8 2019-10-04 19:12:37 -05:00
CHANGELOG.md 1.2.1.1 2019-10-04 19:12:37 -05:00
CODE_OF_CONDUCT.md Reintroduce freer’s code of conduct 2017-12-05 14:48:00 -08:00
LICENSE Update copyright notices 2017-12-06 11:50:25 -08:00
package.yaml Relax bounds on template-haskell to support GHC 8.10 2020-05-18 17:54:19 -05:00
README.md Restructure and simplify README 2018-11-16 09:58:16 -06:00
Setup.hs initial commit 2015-09-12 00:38:18 -05:00
stack.ghc-8.4.yaml Better support GHC 8.8 2019-10-04 19:12:37 -05:00
stack.ghc-8.8.yaml Better support GHC 8.8 2019-10-04 19:12:37 -05:00
stack.yaml Better support GHC 8.8 2019-10-04 19:12:37 -05:00
stack.yaml.lock Better support GHC 8.8 2019-10-04 19:12:37 -05:00

freer-simple — a friendly effect system for Haskell Build Status

The freer-simple library is an implementation of an extensible effect system for Haskell, a general-purpose way of tracking effects at the type level and handling them in different ways. The concept of an “effect” is very general: it encompasses the things most people consider side-effects, like generating random values, interacting with the file system, and mutating state, but it also includes things like access to an immutable global environment and exception handling.

The key features of freer-simple are:

  • An efficient effect system for Haskell as a library.

  • Implementations for several common Haskell monads as effects, including Reader, Writer, State, Error, and others.

  • A combinator language for defining your own effects, designed to make simple, common use cases easy to read and write.

For more details, see the package documentation on Hackage.

Code example

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}

import qualified Prelude
import qualified System.Exit

import Prelude hiding (putStrLn, getLine)

import Control.Monad.Freer
import Control.Monad.Freer.TH
import Control.Monad.Freer.Error
import Control.Monad.Freer.State
import Control.Monad.Freer.Writer

--------------------------------------------------------------------------------
                               -- Effect Model --
--------------------------------------------------------------------------------
data Console r where
  PutStrLn    :: String -> Console ()
  GetLine     :: Console String
  ExitSuccess :: Console ()
makeEffect ''Console

--------------------------------------------------------------------------------
                          -- Effectful Interpreter --
--------------------------------------------------------------------------------
runConsole :: Eff '[Console, IO] a -> IO a
runConsole = runM . interpretM (\case
  PutStrLn msg -> Prelude.putStrLn msg
  GetLine -> Prelude.getLine
  ExitSuccess -> System.Exit.exitSuccess)

--------------------------------------------------------------------------------
                             -- Pure Interpreter --
--------------------------------------------------------------------------------
runConsolePure :: [String] -> Eff '[Console] w -> [String]
runConsolePure inputs req = snd . fst $
    run (runWriter (runState inputs (runError (reinterpret3 go req))))
  where
    go :: Console v -> Eff '[Error (), State [String], Writer [String]] v
    go (PutStrLn msg) = tell [msg]
    go GetLine = get >>= \case
      [] -> error "not enough lines"
      (x:xs) -> put xs >> pure x
    go ExitSuccess = throwError ()

Acknowledgements

The freer-simple package began as a fork of freer-effects by Ixperta Solutions, which in turn is a fork of freer by Allele Dev. All implementations are based on the paper and reference implementation by Oleg Kiselyov.