Skip to content

Instantly share code, notes, and snippets.

@startling
Created July 5, 2013 05:54
Show Gist options
  • Save startling/5932234 to your computer and use it in GitHub Desktop.
Save startling/5932234 to your computer and use it in GitHub Desktop.
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