-
-
Save kevinmeredith/f7066ae80a08e757e085b7f4fc8be4fa to your computer and use it in GitHub Desktop.
Monad Arrow Notes
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
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