I have a function in haskell on paper as an example:
function2 a b c = (a * b) + c
and I'm required to write the example in point free notation. I'm really bad at working with point free style as I find it really confusing with no proper guide through it so i gave it a try:
function2 a b c = (a * b) + c
function2 a b c = ((*) a b) + c #operator sectioning
function2 a b c = (+) ((*) a b)c #operator sectioning once more
#I'm stuck here now
I wasnt sure what should come up next as that was the limit i could think of for this example. Would appreciate some help on this.
--Second example:
function3 a b = a `div` (g b)
function3 a b = `div` a (g b) --operator sectioning
function3 a b = (`div` a) (g b) --parentheses
function3 a b = ((`div` a g).)b --B combinator
function3 a = ((`div` a g).) --eta conversion
function3 a = ((.)(`div` a g)) --operator sectioning
function3 a = ((.)flip(`div` g a))
function3 a = ((.)flip(`div` g).a) --B combinator
function3 = ((.)flip(`div` g)) --eta conversion (complete)
You can apply the B combinator (i.e. (f . g) x = f (g x)
) there:
function2 a b c = (a * b) + c
function2 a b c = ((*) a b) + c -- operator sectioning
function2 a b c = (+) ((*) a b) c -- operator sectioning once more
= (+) (((*) a) b) c -- explicit parentheses
= ((+) . ((*) a)) b c -- B combinator
= ((.) (+) ((*) a)) b c -- operator sectioning
= ((.) (+) . (*)) a b c -- B combinator
Indeed the types are the same:
> :t let function2 a b c = (a * b) + c in function2
let function2 a b c = (a * b) + c in function2
:: Num a => a -> a -> a -> a
> :t ((.) (+) . (*))
((.) (+) . (*)) :: Num b => b -> b -> b -> b
We work by extricating the arguments one by one in the correct order, to end up with
function2 a b c = (......) a b c
so that the eta-contraction can be applied to get rid of the explicit arguments,
function2 = (......)
Our tools in this, which we get to apply in both directions, are
S a b c = (a c) (b c) = (a <*> b) c
K a b = a = const a b
I a = a = id a
B a b c = a (b c) = (a . b) c
C a b c = a c b = flip a b c
W a b = a b b = join a b
U a = a a -- not in Haskell: `join id` has no type
There's also (f =<< g) x = f (g x) x = join (f . g) x
.
Some more, useful patterns that emerge when we work with pointfree for a while are:
((f .) .) g x y = f (g x y)
(((f .) .) .) g x y z = f (g x y z)
.....
((. g) . f) x y = f x (g y)
((. g) . f . h) x y = f (h x) (g y)
(update.) There's an error near the start in your second example, invalidating all the following steps after it:
function3 a b = a `div` (g b)
function3 a b = -- `div` a (g b) -- wrong syntax, you meant
div a (g b)
function3 a b = -- (`div` a) (g b) -- wrong; it is
(a `div`) (g b) --operator sectioning
function3 a b = ((a `div`) . g) b --B combinator
function3 a = (div a . g) --eta conversion; back with plain syntax
function3 a = (.) (div a) g --operator sectioning
function3 a = flip (.) g (div a) --definition of flip
function3 a = (flip (.) g . div) a --B combinator
function3 = (flip (.) g . div) --eta conversion
= (.) (flip (.) g) div --operator section
So yeah, some of the steps were in the right direction.
We can continue with:
function2 a b = (+) ((*) a b) [eta-reduction]
function2 a = (+) . (*) a [using a dot, to eliminate the b]
function2 = ((.) . (.)) (+) (*) ["blackbird" operator to pass two parameters]
Here the "blackbird operator" is a combination of three (.) :: (b -> c) -> (a -> b) -> a -> c
operators. It is functionally equivalent to:
((.) . (.)) :: (c -> d) -> (a -> b -> c) -> a -> b -> d
((.) . (.)) f g x y = f (g x y)
see here for a derivation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With