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
|
2016-03-03 18:10:00 +03:00
|
|
|
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)
|
2016-03-03 18:10:00 +03:00
|
|
|
where gen = msum (fmap return xs)
|
2015-09-13 08:00:16 +03:00
|
|
|
|
|
|
|
testIfte :: [Int] -> [Int]
|
|
|
|
testIfte = run . makeChoiceA . generatePrimes
|