Last active
October 5, 2019 01:33
-
-
Save incertia/efed412622fcad0031e5cd8752f573dd to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE LambdaCase #-} | |
{-# LANGUAGE PolyKinds #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE TemplateHaskell #-} | |
{-# LANGUAGE TypeApplications #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE TypeOperators #-} | |
--{-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-} | |
import Control.Monad (void) | |
import Data.Function ((&)) | |
import Polysemy | |
import Polysemy.Embed | |
import Polysemy.Error | |
import Polysemy.IO | |
data MyError = ErrorA | |
| ErrorB | |
| ErrorUnknown String | |
deriving (Show, Eq) | |
data MyProgram m a where | |
MyPrint :: String -> MyProgram m () | |
makeSem ''MyProgram | |
interpretMyProgram :: Member (Embed IO) r => Sem (MyProgram ': r) a -> Sem r a | |
interpretMyProgram = interpret $ \case | |
MyPrint msg -> embed $ putStrLn msg | |
prog :: (Members '[MyProgram, Error MyError] r) => Sem r () | |
prog = do | |
myPrint "yolo" | |
throw ErrorA | |
myPrint "swag" | |
realprog :: (Members '[MyProgram, Error MyError] r) => Sem r () | |
realprog = catch @MyError prog $ \e -> myPrint ("got error: " ++ show e) | |
main :: IO () | |
main = void $ realprog & interpretMyProgram | |
& runError @MyError -- this type application is important for type inference to work less we | |
-- separate this entire block into a function with signature: | |
-- IO (Either MyError ()) | |
& runM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment