Created
August 21, 2013 21:17
-
-
Save Tarrasch/6300402 to your computer and use it in GitHub Desktop.
Amb in haskell
This file contains hidden or 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
| {-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
| module Amb where | |
| import Control.Monad | |
| newtype Amb a = Amb { unAmb :: [a] } | |
| deriving(Monad) | |
| safeHead :: [a] -> Maybe a | |
| safeHead (x:_) = Just x | |
| safeHead _ = Nothing | |
| executeAmbigiously :: Amb a -> Maybe a | |
| executeAmbigiously = safeHead . unAmb | |
| amb = Amb | |
| ex1 :: Amb (Int, Int) | |
| ex1 = do | |
| x <- amb [1, 2, 3] | |
| y <- amb [4, 5, 6] | |
| unless (x*y == 8) $ amb [] | |
| return (x, y) | |
| main = print $ executeAmbigiously ex1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment