1 -----------------------------------------------------------------------------
3 -- Module : Data.Function
4 -- Copyright : Nils Anders Danielsson 2006
5 -- License : BSD-style (see the LICENSE file in the distribution)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : portable
11 -- Simple combinators working solely on and with functions.
14 ( -- * "Prelude" re-exports
15 id, const, (.), flip, ($)
16 -- * Other combinators
24 -- | @(*) \`on\` f = \\x y -> f x * f y@.
26 -- Typical usage: @'Data.List.sortBy' ('compare' \`on\` 'fst')@.
28 -- Algebraic properties:
30 -- * @(*) \`on\` 'id' = (*)@ (if @(*) ∉ {⊥, 'const' ⊥}@)
32 -- * @((*) \`on\` f) \`on\` g = (*) \`on\` (f . g)@
34 -- * @'flip' on f . 'flip' on g = 'flip' on (g . f)@
36 -- Proofs (so that I don't have to edit the test-suite):
40 -- \x y -> id x * id y
43 -- = { If (*) /= _|_ or const _|_. }
48 -- ((*) `on` f) `on` g
50 -- \x y -> ((*) `on` f) (g x) (g y)
52 -- \x y -> (\x y -> f x * f y) (g x) (g y)
54 -- \x y -> f (g x) * f (g y)
56 -- \x y -> (f . g) x * (f . g) y
62 -- flip on f . flip on g
64 -- (\h (*) -> (*) `on` h) f . (\h (*) -> (*) `on` h) g
66 -- (\(*) -> (*) `on` f) . (\(*) -> (*) `on` g)
68 -- \(*) -> (*) `on` g `on` f
70 -- \(*) -> (*) `on` g . f
72 -- (\h (*) -> (*) `on` h) (g . f)
76 on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
77 (*) `on` f = \x y -> f x * f y