A friendly effect system for Haskell
Go to file
Tomas Janousek ed556f85ad Fix randomly failing "Multiple readers work" test
Comparing floating points using (==) is a bad idea. I don't see how this
could possible have ever worked. :-/
2017-01-28 12:57:31 +01:00
bench CPP around differences in 7.8 vs 7.10 2016-04-21 11:05:08 +10:00
examples/src fix: GHC 7.8 and 7.10 compatibility 2016-11-25 02:21:34 -07:00
src Expose data constructors of all included effects 2017-01-28 12:37:50 +01:00
tests Fix randomly failing "Multiple readers work" test 2017-01-28 12:57:31 +01:00
.gitignore prevent stack.yaml from being checked in 2016-04-16 21:33:09 -05:00
changelog.md Expose data constructors of all included effects 2017-01-28 12:37:50 +01:00
freer.cabal README/changelog/cabal cleanup 2017-01-28 12:11:58 +01:00
LICENSE fix: deadname issue, update copyright 2016-11-14 19:43:47 -07:00
README.md README/changelog/cabal cleanup 2017-01-28 12:11:58 +01:00
Setup.hs initial commit 2015-09-12 00:38:18 -05:00

Freer: Extensible Effects with Freer Monads

Freer is an implementation of "Freer Monads, More Extensible Effects". Much of the implementation is a repackaging and cleaning up of the reference materials provided here.

Features

The key features of Freer are:

  • An efficient effect system for Haskell as a library
  • Implementations for several common Haskell monad instances:
    • Reader
    • Writer
    • State
    • StateRW: State in terms of Reader/Writer
    • Trace
    • Exception
  • Core components for defining your own Effects

Example: Teletype DSL

Here's what using Freer looks like:

{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
module Teletype where

import Control.Monad.Freer
import Control.Monad.Freer.Internal
import System.Exit hiding (ExitSuccess)

--------------------------------------------------------------------------------
                          -- Effect Model --
--------------------------------------------------------------------------------
data Teletype s where
  PutStrLn    :: String -> Teletype ()
  GetLine     :: Teletype String
  ExitSuccess :: Teletype ()

putStrLn' :: Member Teletype r => String -> Eff r ()
putStrLn' = send . PutStrLn

getLine'  :: Member Teletype r => Eff r String
getLine' = send GetLine

exitSuccess' :: Member Teletype r => Eff r ()
exitSuccess' = send ExitSuccess

--------------------------------------------------------------------------------
                     -- Effectful Interpreter --
--------------------------------------------------------------------------------
runTeletype :: Eff '[Teletype] w -> IO w
runTeletype (Val x) = return x
runTeletype (E u q) = case extract u of
              (PutStrLn msg) -> putStrLn msg  >> runTeletype (qApp q ())
              GetLine        -> getLine      >>= \s -> runTeletype (qApp q s)
              ExitSuccess    -> exitSuccess

--------------------------------------------------------------------------------
                        -- Pure Interpreter --
--------------------------------------------------------------------------------
runTeletypePure :: [String] -> Eff '[Teletype] w -> [String]
runTeletypePure inputs req =
  reverse . snd $ run (handleRelayS (inputs, []) (\s _ -> pure s) go req)
  where
    go :: ([String], [String])
       -> Teletype v
       -> (([String], [String]) -> Arr '[] v ([String], [String]))
       -> Eff '[] ([String], [String])
    go (is, os) (PutStrLn msg) q = q (is, msg : os) ()
    go (i:is, os) GetLine q = q (is, os) i
    go ([], _) GetLine _ = error "Not enough lines"
    go (_, os) ExitSuccess _ = pure ([], os)

Contributing

Contributions are welcome! Documentation, examples, code, and feedback - they all help.

Developer Setup

The easiest way to start contributing is to install stack. stack can install GHC/Haskell for you, and automates common developer tasks.

The key commands are:

  • stack setup : install GHC
  • stack build
  • stack clean
  • stack haddock : builds documentation
  • stack test
  • stack bench
  • stack ghci : start a REPL instance

Licensing

This project is distrubted under a BSD3 license. See the included LICENSE file for more details.

Acknowledgements

This package would not be possible without the paper and the reference implementation. In particular:

There will be deviations from the source.