module Choice where
import Control.Applicative
import Control.Monad
import Theseus.Eff
import Theseus.Effect.Choice
import Theseus.Effect.Error
import Theseus.Effect.State
import Utils
testChoice :: SpecWith ()
testChoice = do
describe "Choice" do
it "runs all permutations" do
["ac", "ad", "bc", "bd"] === runEff $ unrestrict @Traversable $ runChoice do
x <- pure "a" <|> pure "b"
y <- pure "c" <|> pure "d"
pure $ x ++ y
it "skips empty branches" do
["ac", "bc", "bd"] === runEff $ unrestrict @Traversable $ runChoice do
x <- pure "a" <|> pure "b"
y <- pure "c" <|> pure "d"
let result = x ++ y
if result == "ad" then empty else pure result
it "doesn't blow up when nesting runChoices" do
["az", "ac", "az", "ad", "bz", "bc", "bz", "bd"] === runEff $ unrestrict @Traversable $ runChoice do
x <- pure "a" <|> pure "b"
yList <- runChoice do pure "z" <|> raise (pure "c" <|> pure "d")
y <- asum $ pure <$> yList
pure $ x ++ y