I had a thought to generalise ($)
like Control.Category
generalises (.)
, and I've done so with the code at the end of this post (also ideone).
In this code I've created a class called FunctionObject
. This class has a function ($)
with the following signature:
($) :: f a b -> a -> b
Naturally I make (->)
an instance of this class so $
continues to work with ordinary functions.
But this allows you to make special functions that, for example, know their own inverse, as the example below shows.
I've concluded there's one of three possibilities:
- I'm the first to think of it.
- Someone else has already done it and I'm reinventing the wheel.
- It's a bad idea.
Option 1 seems unlikely, and my searches on hayoo didn't reveal option 2, so I suspect option 3 is most likely, but if someone could explain why that is it would be good.
import Prelude hiding ((.), ($))
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 $ add 2 $ 5 -- 7
print $ inverse (add 2) $ 5 -- 3
$
applies morphisms to values. The concept of a value seems trivial, but actually, general categories need to have no such notion. Morphisms are values (arrow-values... whatever), but objects (types) needn't actually contain any elements.However, in many categories, there is a special object, the terminal object. In Hask, this is the
()
type. You'll notice that functions() -> a
are basically equivalent toa
values themselves. Categories in which this works are called well-pointed. So really, the fundamental thing you need for something like$
to make sense isThen you can define the application operator by
The obvious instance for
WellPointed
is of course Hask itself:The other well-known category,
Kleisli
, is not an instance ofWellPointed
as I wrote it (it allowspoint
, but notunpoint
). But there are plenty of categories which would make for a goodWellPointed
instance, if they could properly be implemented in Haskell at all. Basically, all the categories of mathematical functions with particular properties (LinK, Grp, {{•}, Top}...). The reason these aren't directly expressible as aCategory
is that they can't have any Haskell type as an object; newer category libraries like categories or constrained-categories do allow this. For instance, I have implemented this:As you see, the class interface is actually a bit different from what I wrote above. There isn't one universally accepted way of implementing such stuff in Haskell yet... in
constrained-categories
, the$
operator actually works more like what Cirdec described.There are two abstractions used for things like this in Haskell, one usings
Arrow
s and the otherApplicative
s. Both can be broken down into smaller parts than those used inbase
.If you go in the
Arrow
direction and break down the capabilities ofArrow
s into component pieces, you'd have a separate class for those arrows that are able to lift arbitrary functions into the arrow.This would be the opposite of
ArrowArr
, arrows where any arbitrary arrow can be dropped to a function.If you just split
arr
off ofArrow
you are left with arrow like categories that can construct and deconstruct tuples.If you go in the
Applicative
direction this is aCopointed
"Applicative
withoutpure
" (which goes by the nameApply
).When you go this way you typically drop the
Category
for functions and instead have a type constructorC a
representing values (including function values) constructed according to a certain set of rules.