Created
August 15, 2022 15:48
-
-
Save viviag/6d7d70b30be779e4618b9130efba3e87 to your computer and use it in GitHub Desktop.
Playing around Hom(X,_) in Hask
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
module Main where | |
import Control.Monad | |
-- Directly composing morphisms as if we were not in End_{Hask}. | |
regular :: (->) Int Int | |
regular = (+) 1 . (+) 1 . (+) 1 . (+) 1 | |
-- Actually the same, but explicitly using action of Hom-functor on arrows. | |
functorial :: (->) Int Int | |
functorial = (+) 1 <$> (+) 1 <$> (+) 1 <$> (+) 1 | |
-- A bit of a sudden, but where is a Monad instance for Hom-functor. It operates X as a context of reader passing through. | |
-- Since it's just an arrow, nothing prevents from accumulating this value. | |
-- But we cannot derive essentially new way of composing functions like `f >=> f >=> f` or so. | |
monadic :: (->) Int Int | |
monadic = (+) 1 >>= ((+) >=> (+) >=> (+)) | |
-- And if instances of Monad and Functor are defined it's possible to define Applicative. | |
applicative :: (->) Int Int | |
applicative = (+) <*> (+) 1 <$> (+) 1 | |
main :: IO () | |
main = do | |
print $ regular 1 | |
print $ functorial 1 | |
print $ monadic 1 | |
print $ applicative 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment