freer-simple/tests/Tests/NonDetEff.hs
ElvishJerricco a7f7be7973 Fixed nonDetEffTests
The problem was the definition of `ifte`. It reported `el` if `th` was
`const mzero`.
2016-03-03 10:29:13 -05:00

23 lines
599 B
Haskell

{-# 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))
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)
testIfte :: [Int] -> [Int]
testIfte = run . makeChoiceA . generatePrimes