Skip to content

Instantly share code, notes, and snippets.

@jvranish
Created March 9, 2013 15:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvranish/5124513 to your computer and use it in GitHub Desktop.
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!
{-
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