I am a Haskell novice. I am trying to create a mini-language within Haskell, and would like if possible to have a higher-order function called opp
(short for "opposite") that converts a number of familiar functions into their obvious opposites. For example, opp succ
would be the function pred
, opp head
would be last
, and so on. I don't have some general definition of what it means to convert a function into its opposite: I just want to pick a few key examples and declare what their opposites. So I want a highly polymorphic function that is hardly ever defined.
The difficulty seems to be that I want to recognise the functions by their names rather than by their essences (so to speak). One manifestation of that difficulty is that if I write
opp succ = pred
then Haskell treats succ
as a variable and therefore gives me a constant function that always takes the value pred
. What I really want is to say something more like, "If you ever see the string opp succ
then think of it as another name for pred
." But after searching around for quite a while, I can't find out how to do that (if it's possible at all).
To summarize, I would like to define a function
opp :: (a -> b) -> (a -> b)
by saying things like
opp succ = pred
opp pred = succ
opp head = last
opp last = head
and adding to this list whenever I feel like it. Obviously I can't do it like that, but is there some non-horrible way of achieving the same effect?