fork download
  1. import Prelude hiding ((.), ($))
  2. import Control.Category ((.), Category)
  3.  
  4. class FunctionObject f where
  5. ($) :: f a b -> a -> b
  6.  
  7. infixr 0 $
  8.  
  9. instance FunctionObject (->) where
  10. f $ x = f x
  11.  
  12. data InvertibleFunction a b =
  13. InvertibleFunction (a -> b) (b -> a)
  14.  
  15. instance Category InvertibleFunction where
  16. (InvertibleFunction f f') . (InvertibleFunction g g') =
  17. InvertibleFunction (f . g) (g' . f')
  18.  
  19. instance FunctionObject InvertibleFunction where
  20. (InvertibleFunction f _) $ x = f $ x
  21.  
  22. inverse (InvertibleFunction f f') = InvertibleFunction f' f
  23.  
  24. add :: (Num n) => n -> InvertibleFunction n n
  25. add n = InvertibleFunction (+n) (subtract n)
  26.  
  27. main = do
  28. print $ add 2 $ 5
  29. print $ inverse (add 2) $ 5
  30.  
Success #stdin #stdout 0s 4596KB
stdin
Standard input is empty
stdout
7
3