Skip to content

Instantly share code, notes, and snippets.

@kevinmeredith
Created November 1, 2017 13:01
Show Gist options
  • Save kevinmeredith/f7066ae80a08e757e085b7f4fc8be4fa to your computer and use it in GitHub Desktop.
Save kevinmeredith/f7066ae80a08e757e085b7f4fc8be4fa to your computer and use it in GitHub Desktop.
Monad Arrow Notes
import Control.Arrow
import Control.Monad
-- Prelude Control.Monad Control.Arrow> :t (|||)
-- (|||) :: ArrowChoice a => a b d -> a c d -> a (Either b c) d
-- Prelude Control.Monad Control.Arrow> :k ArrowChoice
-- ArrowChoice :: (* -> * -> *) -> Constraint
-- Prelude Control.Monad Control.Arrow> :k Kleisli
-- Kleisli :: (* -> *) -> * -> * -> *
-- provide a function to be run on 'left' and 'right', loosely speaking
example :: Kleisli IO Int String ->
Kleisli IO () String ->
Kleisli IO (Either Int ()) String
example x y = x ||| y
run :: (Either Int ()) ->
Kleisli IO (Either Int ()) String ->
IO String
run input k = (runKleisli k) input
-- always returns "a!" when Kleisli + IO are run
a :: Kleisli IO Int String
a = Kleisli $ const $ return "a!"
-- always returns "b!" when Kleisli + IO are run
b :: Kleisli IO () String
b = Kleisli $ const $ return "b!"
-- will output "b!"
leftResult :: IO String
leftResult = run (Right ()) (example a b)
-- will output "a!"
rightResult :: IO String
rightResult = run (Left 666) (example a b)
-- from REPL
-- >rightResult
-- "a!"
-- >leftResult
-- "b!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment