fork download
  1. import Prelude hiding ((.))
  2. import Control.Category ((.), (<<<), (>>>), Category)
  3.  
  4. data InvertibleFunction a b = InvertibleFunction (a -> b) (b -> a)
  5.  
  6. instance Category InvertibleFunction where
  7. (InvertibleFunction b_c c_b) . (InvertibleFunction a_b b_a) = InvertibleFunction (b_c . a_b) (b_a . c_b)
  8.  
  9. inv (InvertibleFunction x y) = InvertibleFunction y x
  10.  
  11. add :: (Num n) => n -> InvertibleFunction n n
  12. add x = InvertibleFunction (+x) (\y -> y - x)
  13.  
  14. class KindaApplicative f where
  15. (<$>) :: f a b -> a -> b
  16.  
  17. instance KindaApplicative InvertibleFunction where
  18. (InvertibleFunction f _) <$> x = f x
  19.  
  20. main = print $ ((inv (add 2)) <$> 5)
  21.  
Success #stdin #stdout 0s 4552KB
stdin
Standard input is empty
stdout
3