{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Eff.Choose ( Choose (..)
, choose
, makeChoice
, mzero'
, mplus'
) where
import Control.Eff.Internal
import Data.OpenUnion
#if __GLASGOW_HASKELL__ > 708
import Control.Applicative
#endif
import Control.Monad
import Control.Monad.Base
import Control.Monad.Trans.Control
newtype Choose a = Choose [a]
instance ( MonadBase m m
, SetMember Lift (Lift m) r
, MonadBaseControl m (Eff r)
) => MonadBaseControl m (Eff (Choose ': r)) where
type StM (Eff (Choose ': r)) a = StM (Eff r) [a]
liftBaseWith f = raise $ liftBaseWith $ \runInBase ->
f (runInBase . makeChoice)
restoreM x = do lst <- raise (restoreM x)
choose lst
choose :: Member Choose r => [a] -> Eff r a
choose lst = send $ Choose lst
mzero' :: Member Choose r => Eff r a
mzero' = choose []
mplus' :: Member Choose r => Eff r a -> Eff r a -> Eff r a
mplus' m1 m2 = join $ choose [m1,m2]
#if __GLASGOW_HASKELL__ > 708
instance Member Choose r => Alternative (Eff r) where
empty = mzero'
(<|>) = mplus'
instance Member Choose r => MonadPlus (Eff r) where
mzero = empty
mplus = (<|>)
#endif
makeChoice :: forall a r. Eff (Choose ': r) a -> Eff r [a]
makeChoice = handle_relay
(return . (:[]))
(\(Choose lst) k -> handle lst k)
where
handle :: [t] -> (t -> Eff r [a]) -> Eff r [a]
handle [] _ = return []
handle [x] k = k x
handle lst k = fmap concat $ mapM k lst