When I have some function of type like
f :: (Ord a) => a -> a -> Bool f a b = a > b
I should like make function which wrap this function with not.
e.g. make function like this
g :: (Ord a) => a -> a -> Bool g a b = not $ f a b
I can make combinator like
n f = (\a -> \b -> not $ f a b)
But I don’t know how.
*Main> let n f = (\a -> \b -> not $ f a b) n :: (t -> t1 -> Bool) -> t -> t1 -> Bool Main> :t n f n f :: (Ord t) => t -> t -> Bool *Main> let g = n f g :: () -> () -> Bool
What am I doing wrong?
And bonus question how I can do this for function with more and lest parameters e.g.
t -> Bool t -> t1 -> Bool t -> t1 -> t2 -> Bool t -> t1 -> t2 -> t3 -> Bool
Unless you want to go hacking around with typeclasses, which is better left for thought experiments and proof of concept, you just don’t generalize to multiple arguments. Don’t try.
As for your main question, this is most elegantly solved with Conal Elliott’s semantic editor combinators. A semantic editor combinator is a function with a type like:
Where
F(x)is some expression involvingx. There are also ‘contravariant’ editor combinators which take a(b -> a)instead. Intuitively, an editor combinator selects a part of some larger value to operate on. The one you need is calledresult:Look at the type of the expression you’re trying to operate on:
The result (codomain) of this type is
a -> Bool, and the result of that type isBool, and that’s what you’re trying to applynotto. So to applynotto the result of the result of a functionf, you write:This beautifully generalizes. Here are a few more combinators:
So if you have a value
xof type:And you want to apply
notto the Bool, you just spell out the path to get there:Oh, and if you’ve gotten to Functors yet, you’ll notice that
fmapis an editor combinator. In fact, the above can be spelled:But I think it’s clearer to use the expanded names.
Enjoy.