Created
October 15, 2015 16:51
-
-
Save LukaHorvat/2701f2e2231c36a068ae to your computer and use it in GitHub Desktop.
MapMonad.hs
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
import Data.Map (Map) | |
import qualified Data.Map as Map | |
instance (Ord k, Monoid k) => Applicative (Map k) where | |
pure = Map.singleton mempty | |
f <*> x = Map.fromList [(mappend fk xk, f Map.! fk $ x Map.! xk) | fk <- Map.keys f, xk <- Map.keys x] | |
instance (Ord k, Monoid k) => Monad (Map k) where | |
return = pure | |
m >>= f = Map.fromList $ do | |
mk <- Map.keys m | |
let childMap = f (m Map.! mk) | |
nk <- Map.keys childMap | |
return (mappend mk nk, childMap Map.! nk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment