Last active
April 15, 2017 19:10
-
-
Save luqui/08a88265a68a0c88fdfe76a59752f1f8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-- In an alternate universe of Haskell where this worked... | |
data (Num a) => Complex a = a :+ a | |
instance Functor Complex where | |
fmap f (x :+ y) = f x :+ f y | |
instance Monad Complex where | |
return x = x :+ 0 | |
join ((a :+ b) :+ (c :+ d)) = (a - d) :+ (b + c) | |
-- Associativity | |
join . fmap join $ ((a :+ b) :+ (c :+ d)) :+ ((e :+ f) :+ (g :+ h)) | |
= join $ join ((a :+ b) :+ (c :+ d)) :+ join ((e :+ f) :+ (g :+ h)) | |
= join $ ((a - d) :+ (b + c)) :+ ((e - h) :+ (f + g)) | |
= (a - d - f - g) :+ (b + c + e - h) | |
= (a - g - d - f) :+ (b - h + c + e) | |
= join $ ((a - g) :+ (b - h)) :+ ((c + e) :+ (d + f)) | |
= join $ ((a :+ b) - (g :+ h)) :+ ((c :+ d) + (e :+ f)) | |
= join . join $ ((a :+ b) :+ (c :+ d)) :+ ((e :+ f) :+ (g :+ h)) | |
-- Right unit | |
join . fmap return $ a :+ b | |
= join $ return a :+ return b | |
= join $ (a :+ 0) :+ (b :+ 0) | |
= (a - 0) :+ (0 + b) | |
= id $ a :+ b | |
-- Left unit | |
join . return $ a :+ b | |
= join $ (a :+ b) :+ (0 :+ 0) | |
= (a - 0) :+ (b + 0) | |
= id $ a :+ b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment