[Haskell-beginners] does this function exist already? (a -> b -> c) -> (d -> b) -> (( a -> d -> c ))

Silent Leaf silent.leaf0 at gmail.com
Fri Apr 8 21:42:37 UTC 2016


[Note, total newbie to mailing lists in general, and beginner in haskell as
one might have surmised ^^ hopefully I'm doing the right thing here with
this mail...]

All, or virtually all in the title, but I'll develop. I wrote some useful
function (which i rather awkwardly called "point2", for "(.) operator
applied at the level of the second argument of the first function"). Here
is its def:

point2 :: (a -> b -> c) -> (d -> b) -> (( a -> d -> c ))
-- the double parentheses, totally optional and syntactically pointless,
are just there to imply that the function is seen by we users as
'outputting' another function, even though said outputted function can be
applied right away, of course, outputting then possibly c, or (d -> c) if
partial application.
-- I named the variables in the pattern like their respective types, for
'clarity'
point2 f g a d = f a (g d)
-- i originally wrote it in point free style using flip, from Data.Function
(well, my own rebuilt wheel actually); so, alternate definition using flip
== (\f y x -> f x y)
point2 f g = flip (flip f . g)
-- proof by decomposition: (or is it "composition", as we don't actually
decompose?)
-- f :: a -> b -> c
-- flip f :: b -> a -> c
-- flip f . g :: d -> a -> c
-- flip (flip f . g) == f `point2` g :: a -> d -> c

anyways, it's pretty useful if you wanna combine f and g but g is meant to
output the *second* argument of f, not the first, that is:
(f a) . g == (f `point2` g) a
in the first expression, "a" must necessarily be known and given to combine
f and g in point-free style, whereas in the second one, "a" can be omitted,
hence then we could write "foo = f `point2` g", which is way closer to the
point-free style, way simpler to understand too in my opinion once you got
the picture.

If you got all I said above, my question is then to know if this point2
function already exists officially, coz I don't really wanna reinvent the
wheel, plus I wonder how they called it ^^ I'm not really satisfied of
"point2" as variable name. I'd love (.2) but it's not compatible with
Haskell. ^^

Also, same question for the following function (does it already exists?),
again a sibling of (.), here, the purpose being to write h(a, b) = f (g(a,
b))  in point-free style:
after2 :: (c -> d) -> (a -> b -> c) -> (( a -> b -> d ))
after2 f g a b = f (g a b)
-- its name implies an infix use, for example: h = f `after2` g
basically, if you know about Data.Function(on), it's a bit (one of) its
opposite: `on` applies g to both arguments of f independently, before
giving both results to the 2-ary function f, ie
(f `on` g) a b == f (g a) (g b)

I'm not entirely sure, but I think we could write:
f `after2` g == curry (f . (uncurry g))
-- since:
uncurry g :: (a,b) -> c
f . uncurry g :: (a,b) -> d
curry (f . uncurry g) :: a -> b -> d

un/curry functions' defs, if needed:
curry :: ((a,b) -> c) -> a -> b -> c
curry f a b = f (a,b)
-- curry f :: a -> b -> c when f :: (a,b) -> c
uncurry :: (a -> b -> c) -> (a,b) -> c
uncurry g (a,b) = g a b
-- uncurry g :: (a,b) -> c when g :: a -> b -> c
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160408/5386cdca/attachment.html>


More information about the Beginners mailing list