Created
July 5, 2013 05:54
-
-
Save startling/5932234 to your computer and use it in GitHub Desktop.
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
module Control.Applicative.Recipes where | |
-- base | |
import Control.Applicative | |
import Data.Foldable | |
import Data.Traversable | |
import Data.Monoid | |
-- containers | |
import Data.Set | |
data Recipe k v m a = Recipe | |
{ ingredients :: Set k | |
, cook :: (k -> m v) -> m a | |
} | |
instance Functor m => Functor (Recipe k v m) where | |
fmap f (Recipe ks g) = Recipe ks (fmap f . g) | |
instance (Applicative m, Ord k) => Applicative (Recipe k v m) where | |
pure v = Recipe Data.Set.empty (const $ pure v) | |
Recipe ks f <*> Recipe ls g = Recipe (ks <> ls) (\x -> f x <*> g x) | |
using :: k -> Recipe k v m v | |
using k = Recipe (singleton k) ($ k) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment