Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an mtl compatibility layer (instead of) or (in addition to) reimplementing lens operations #1

Open
liskin opened this issue Feb 6, 2017 · 1 comment

Comments

@liskin
Copy link

liskin commented Feb 6, 2017

Something like this, perhaps:

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}

import Control.Monad.Freer (Eff, Member) 
import Control.Monad.Freer.State (State, get, put)
import Control.Monad.State (MonadState)
import qualified Control.Monad.State (MonadState(get, put))

newtype StateEff s effs a = StateEff { runStateEff :: Eff effs a }
  deriving (Functor, Applicative, Monad)

instance (Member (State s) effs) => MonadState s (StateEff s effs) where
    get = StateEff get
    {-# INLINE get #-}
    put = StateEff . put                             
    {-# INLINE put #-}

wrapState :: Member (State s) effs => (forall m. MonadState s m => m a) -> Eff effs a
wrapState m = runStateEff m
{-# INLINE wrapState #-}
@liskin
Copy link
Author

liskin commented Feb 10, 2017

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

import Control.Monad.Freer (Eff, Member)
import Control.Monad.Freer.State (State, get, put)
import Control.Monad.State (MonadState)
import qualified Control.Monad.State (MonadState(get, put))
import Data.Coerce (coerce)

newtype StateEff s effs a = StateEff { runStateEff :: Eff effs a }
  deriving (Functor, Applicative, Monad)

instance (Member (State s) effs) => MonadState s (StateEff s effs) where
    get = coerce (get :: Eff effs s)
    {-# INLINE get #-}
    put = coerce (put :: s -> Eff effs ())
    {-# INLINE put #-}

wrapState :: forall s effs a. Member (State s) effs => (forall m. MonadState s m => m a) -> Eff effs a
wrapState m = coerce (m :: StateEff s effs a)
{-# INLINE wrapState #-}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant