Skip to content

Instantly share code, notes, and snippets.

@naoto-ogawa
Last active February 17, 2019 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoto-ogawa/d81b69807a66a2acfe90f5c7a7af8b36 to your computer and use it in GitHub Desktop.
Save naoto-ogawa/d81b69807a66a2acfe90f5c7a7af8b36 to your computer and use it in GitHub Desktop.
extensible_sample.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
import Control.Monad.Reader
import Control.Monad.Reader.Class
import Data.Extensible
import Data.Extensible.Effect
import Data.Extensible.Effect.Default
-- ===================== mtl compatible style
-- ex_reader :: Reader Int Int -- > NG
ex_reader :: (MonadReader Int m) => m Int -- > OK
ex_reader = do
x <- ask
return $ x * 2
run_reader x = runReader ex_reader x -- normal
run_ex_reader x = leaveEff $ runReaderDef ex_reader x -- extensible
-- extensible 1
ex_reader_ext :: ((Associate "foo" (ReaderEff Int)) xs ) => Eff xs Int
ex_reader_ext = do
x <- askEff #foo
return $ x * 2
run_ex_reader_ext x = leaveEff $ runReaderEff @ "foo" ex_reader_ext x -- TypeApplication
-- extensible 2
ex_reader_ext' :: Eff '["foo" >: ReaderEff Int] Int -- DataKinds, TypeOperators
ex_reader_ext' = do
x <- askEff #foo
return $ x * 2
run_ex_reader_ext' x = leaveEff $ runReaderEff ex_reader_ext' x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment