Verified Commit e4b8aa5d authored by Justin Andresen's avatar Justin Andresen
Browse files

Add example Cabal project for fused-effects

parent 27f5acc9
### Files and directories generated by Cabal ###
dist
dist-newstyle
.ghc.environment.*
Copyright (c) 2020, Justin Andresen <jan.dresen.95@gmail.com>, Kiel University
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the copyright holder nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import Distribution.Simple
main = defaultMain
-- Cabal configuration file for this package.
packages: mapro-fused-effects-test.cabal
-- Configure this package to compile only if there are no compile time
-- warnings in production mode. To disable fatal warnings during development
-- pass `--ghc-option=-Wwarn` to `cabal`.
package mapro-fused-effects-test
ghc-options: -Werror
cabal-version: 2.4
-- Initial package description 'mapro-fused-effects-test.cabal' generated
-- by 'cabal init'. For further documentation, see
-- http://haskell.org/cabal/users-guide/
name: mapro-fused-effects-test
version: 0.1.0.0
synopsis: Cabal test project for the fused-effects library.
-- description:
-- bug-reports:
license: BSD-3-Clause
license-file: LICENSE
author: Justin Andresen
maintainer: jan.dresen.95@gmail.com
copyright: (c) 2020, Justin Andresen, Kiel University
-- category:
library
-- exposed-modules:
-- other-extensions:
build-depends: base ^>=4.12.0.0
, fused-effects
hs-source-dirs: src
default-language: Haskell2010
other-modules: MyEffects
, MyHandlers
, MyProgs
{-# LANGUAGE DeriveFunctor, DeriveGeneric, FlexibleInstances
, GeneralizedNewtypeDeriving, MultiParamTypeClasses
, TypeOperators, UndecidableInstances #-}
{-# LANGUAGE GADTs, StandaloneDeriving #-}
module MyEffects where
import Control.Algebra
import GHC.Generics ( Generic1 )
-------------------------------------------------------------------------------
-- State Effect --
-------------------------------------------------------------------------------
-- Unlike in Polysemy and freer-effects, the effect data type contains
-- a continutation.
data MyState s m k where
MyGet :: (s -> m k) -> MyState s m k
MyPut :: s -> m k -> MyState s m k
deriving (Functor, Generic1)
instance HFunctor (MyState s)
instance Effect (MyState s)
-- Unlike Polysemy and freer-effects, there is no Template Haskell integration
-- to generate the effect operations from the data type.
-- Thus, we have to write the wrappers manually.
myGet :: Has (MyState s) sig m => m s
myGet = send (MyGet pure)
myPut :: Has (MyState s) sig m => s -> m ()
myPut s = send (MyPut s (pure ()))
-------------------------------------------------------------------------------
-- Because no @Generic1@ instances can be derived from types with constructors
-- with existential type variables, we have to use messages of type @String@
-- instead of @Show a => a@. We can work around this limitation by invoking
-- @show@ in the wrapper function instead.
data MyLog m k where
MyLog :: String -> m k -> MyLog m k
deriving (Functor, Generic1)
instance HFunctor MyLog
instance Effect MyLog
myLog :: (Has MyLog sig m, Show a) => a -> m ()
myLog msg = send (MyLog (show msg) (pure ()))
{-# LANGUAGE DeriveFunctor, DeriveGeneric, FlexibleInstances
, GeneralizedNewtypeDeriving, MultiParamTypeClasses
, TypeOperators, UndecidableInstances #-}
{-# LANGUAGE FlexibleContexts, RankNTypes #-}
module MyHandlers where
import Control.Algebra
import Control.Monad.IO.Class
import Control.Carrier.Interpret
import Control.Carrier.State.Strict
import Control.Monad ( liftM
, ap
)
import MyEffects
-------------------------------------------------------------------------------
-- State Effect --
-------------------------------------------------------------------------------
-- In contrast to Polysemy and freer-simple we can define stateful handlers
-- in fused-effects without using internal functions of the library.
-- In a first approach I've used a custom carry type. This is more efficient
-- than the version below but requires a lot of boilerplate code.
{-
newtype MyStateC s m a = MyStateC { runMyStateC :: s -> m (s, a) }
instance Monad m => Functor (MyStateC s m) where
fmap = liftM
instance Monad m => Applicative (MyStateC s m) where
pure = return
(<*>) = ap
instance Monad m => Monad (MyStateC s m) where
return x = MyStateC $ \s -> return (s, x)
mx >>= f = MyStateC $ \s -> do
(s', x) <- runMyState s mx
runMyState s' (f x)
instance Algebra sig m => Algebra (MyState s :+: sig) (MyStateC s m) where
alg (L (MyGet k )) = MyStateC $ \s -> runMyState s (k s)
alg (L (MyPut s' k)) = MyStateC $ \_ -> runMyState s' k
alg (R other ) = undefined -- TODO implement me
runMyState :: s -> MyStateC s m a -> m (s, a)
runMyState = flip runMyStateC
-}
-- Later I've discovered 'runInterpretState' which is less efficient than
-- the version with a custom carrier type but much shorter. In fact, the
-- obscure type signature is much longer than the actual code. In consequence
-- this definition is still more verbose than the equivalent definitions in
-- Polysemy and freer-simple.
runMyState
:: Monad m
=> s
-> ( forall t
. Reifies t (Handler (MyState s) (StateC s m))
=> InterpretC t (MyState s) (StateC s m) a
)
-> m (s, a)
runMyState = runInterpretState $ \s op -> case op of
MyGet k -> runState s (k s)
MyPut s' k -> runState s' k
-------------------------------------------------------------------------------
-- In fused effects there is apparently no difference between
-- the interpretation and interception of an effect.
-- See also <https://github.com/fused-effects/fused-effects/issues/220>.
logMyState
:: (Has (MyState s) sig m, Has MyLog sig m, Show s)
=> ( forall t
. Reifies t (Handler (MyState s) m)
=> InterpretC t (MyState s) m a
)
-> m a
logMyState = runInterpret $ \op -> case op of
MyGet k -> myGet >>= k
MyPut s' k -> myPut s' >> myLog s' >> k
-------------------------------------------------------------------------------
printMyLog
:: MonadIO m
=> (forall t . Reifies t (Handler MyLog m) => InterpretC t MyLog m a)
-> m a
printMyLog = runInterpret $ \op -> case op of
MyLog msg k -> liftIO (putStrLn ("Info: " ++ msg)) >> k
{-# LANGUAGE DeriveFunctor, DeriveGeneric, FlexibleInstances
, GeneralizedNewtypeDeriving, MultiParamTypeClasses
, TypeOperators, UndecidableInstances #-}
{-# LANGUAGE TypeApplications #-}
module MyProgs where
import Control.Algebra
import Control.Carrier.Lift
import MyEffects
import MyHandlers
-- Just like with freer-simple, GHC is also not able to infer the
-- type of 'myGet' in this case.
myProg :: Has (MyState Int) sig m => m Bool
myProg = do
n <- myGet @Int
myPut (2 * n)
m <- myGet
myPut (m - n)
n' <- myGet
return (n' == n)
myInitialState :: Int
myInitialState = 42
-- This function cannot be written like in the examples for the
-- other libraries due to the higher-rank type in 'runMyState'.
runMyProg :: (Int, Bool)
runMyProg = run (runMyState myInitialState myProg)
logMyProg :: IO (Int, Bool)
logMyProg =
runM (printMyLog (runMyState myInitialState (logMyState @Int myProg)))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment