import Control.Category ((.), Category)
class FunctionObject f where
($) :: f a b -> a -> b
infixr 0 $
instance FunctionObject (->) where
f $ x = f x
data InvertibleFunction a b =
InvertibleFunction (a -> b) (b -> a)
instance Category InvertibleFunction where
(InvertibleFunction f f') . (InvertibleFunction g g') =
InvertibleFunction (f . g) (g' . f')
instance FunctionObject InvertibleFunction where
(InvertibleFunction f _) $ x = f $ x
inverse (InvertibleFunction f f') = InvertibleFunction f' f
add
:: (Num n
) => n
-> InvertibleFunction n n
add n
= InvertibleFunction
(+n
) (subtract n
)
main = do
print $ inverse
(add
2) $ 5
aW1wb3J0IFByZWx1ZGUgaGlkaW5nICgoLiksICgkKSkKaW1wb3J0IENvbnRyb2wuQ2F0ZWdvcnkgKCguKSwgQ2F0ZWdvcnkpCgpjbGFzcyBGdW5jdGlvbk9iamVjdCBmIHdoZXJlCiAgKCQpIDo6IGYgYSBiIC0+IGEgLT4gYgoKaW5maXhyIDAgJAoKaW5zdGFuY2UgRnVuY3Rpb25PYmplY3QgKC0+KSB3aGVyZQogIGYgJCB4ID0gZiB4CgpkYXRhIEludmVydGlibGVGdW5jdGlvbiBhIGIgPSAKICAgSW52ZXJ0aWJsZUZ1bmN0aW9uIChhIC0+IGIpIChiIC0+IGEpCgppbnN0YW5jZSBDYXRlZ29yeSBJbnZlcnRpYmxlRnVuY3Rpb24gd2hlcmUKICAoSW52ZXJ0aWJsZUZ1bmN0aW9uIGYgZicpIC4gKEludmVydGlibGVGdW5jdGlvbiBnIGcnKSA9CiAgICBJbnZlcnRpYmxlRnVuY3Rpb24gKGYgLiBnKSAoZycgLiBmJykKICAgIAppbnN0YW5jZSBGdW5jdGlvbk9iamVjdCBJbnZlcnRpYmxlRnVuY3Rpb24gd2hlcmUKICAoSW52ZXJ0aWJsZUZ1bmN0aW9uIGYgXykgJCB4ID0gZiAkIHgKCmludmVyc2UgKEludmVydGlibGVGdW5jdGlvbiBmIGYnKSA9IEludmVydGlibGVGdW5jdGlvbiBmJyBmCgphZGQgOjogKE51bSBuKSA9PiBuIC0+IEludmVydGlibGVGdW5jdGlvbiBuIG4KYWRkIG4gPSBJbnZlcnRpYmxlRnVuY3Rpb24gKCtuKSAoc3VidHJhY3QgbikKCm1haW4gPSBkbwogIHByaW50ICQgYWRkIDIgJCA1CiAgcHJpbnQgJCBpbnZlcnNlIChhZGQgMikgJCA1Cg==