freer-simple/tests/Tests/NonDetEff.hs

23 lines
599 B
Haskell
Raw Normal View History

2015-09-13 08:00:16 +03:00
{-# LANGUAGE FlexibleContexts #-}
module Tests.NonDetEff where
import Control.Applicative
import Control.Monad
import Control.Monad.Freer
ifte :: Member NonDetEff r
=> Eff r a -> (a -> Eff r b) -> Eff r b -> Eff r b
ifte t th el = msplit t >>= maybe el (\(a,m) -> th a <|> (m >>= th))
2015-09-13 08:00:16 +03:00
generatePrimes :: Member NonDetEff r => [Int] -> Eff r Int
generatePrimes xs = do
n <- gen
ifte (do d <- gen
guard $ d < n && n `mod` d == 0)
(const mzero)
(return n)
where gen = msum (fmap return xs)
2015-09-13 08:00:16 +03:00
testIfte :: [Int] -> [Int]
testIfte = run . makeChoiceA . generatePrimes