Skip to content

Instantly share code, notes, and snippets.

@Tarrasch
Created August 21, 2013 21:17
Show Gist options
  • Select an option

  • Save Tarrasch/6300402 to your computer and use it in GitHub Desktop.

Select an option

Save Tarrasch/6300402 to your computer and use it in GitHub Desktop.
Amb in haskell
{-# 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