Skip to content

Instantly share code, notes, and snippets.

@twopoint718
Created August 22, 2019 00:40
Show Gist options
  • Save twopoint718/be9f18cb55686c136cadd900f3899475 to your computer and use it in GitHub Desktop.
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.
{-# 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