Created
August 22, 2019 00:40
-
-
Save twopoint718/be9f18cb55686c136cadd900f3899475 to your computer and use it in GitHub Desktop.
Playing around with no-parameter typeclasses. These can pull values out of the "App" environment so that the `run` function can use them.
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 MultiParamTypeClasses, MonoLocalBinds #-} | |
import Control.Monad.Reader | |
class HasFoo where | |
foo :: App String | |
class HasBar where | |
bar :: String | |
instance HasFoo where | |
foo = asks _foo | |
instance HasBar where | |
bar = "BAR" | |
data Ctx = Ctx | |
{ _foo :: String | |
, _bar :: String | |
} | |
type App a = Reader Ctx a | |
run :: (HasFoo, HasBar) => String -> App String | |
run s = do | |
f <- foo | |
return $ f ++ s ++ bar | |
m' :: String | |
m' = runReader (run "|") ctx | |
where ctx = Ctx "CTX:FOO" "CTX:BAR" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment