Created
March 9, 2013 15:30
-
-
Save jvranish/5124513 to your computer and use it in GitHub Desktop.
A comparison of LogicT and Stream (from Olegs FBackTrack.hs) Stream has better termination criteria!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
To install dependencies: | |
cabal install logict | |
cabal install stream-monad | |
-} | |
import Control.Monad.Logic | |
import Control.Monad.Stream | |
choose :: (MonadPlus m) => [a] -> m a | |
choose [] = mzero | |
choose (x:xs) = return x `mplus` choose xs | |
odds :: (MonadPlus m) => m Int | |
odds = choose [1,3..] | |
two :: (Monad m) => m Int | |
two = return 2 | |
filterEvens :: (MonadPlus m) => m Int -> m Int | |
filterEvens m = do | |
x <- m | |
if even x then return x else mzero | |
test1 :: MonadLogic m => m Int | |
test1 = filterEvens (odds `interleave` two) | |
test2 :: MonadLogic m => m Int | |
test2 = filterEvens odds `interleave` filterEvens two | |
-- Spits out a 2, and then loops | |
test1_LogicT :: [Int] | |
test1_LogicT = observeAll $ test1 | |
-- loops | |
test2_LogicT :: [Int] | |
test2_LogicT = observeAll $ test2 | |
-- spits out a 2 and then loops | |
test2_Stream :: [Int] | |
test2_Stream = runStream $ test2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment