mirror of
https://github.com/lexi-lambda/freer-simple.git
synced 2024-12-26 23:55:57 +03:00
a7f7be7973
The problem was the definition of `ifte`. It reported `el` if `th` was `const mzero`.
23 lines
599 B
Haskell
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
|