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
22 -- | @(*) \`on\` f = \\x y -> f x * f y@.
24 -- Typical usage: @'Data.List.sortBy' ('compare' \`on\` 'fst')@.
26 -- Algebraic properties:
28 -- * @(*) \`on\` 'id' = (*)@ (if @(*) ∉ {⊥, 'const' ⊥}@)
30 -- * @((*) \`on\` f) \`on\` g = (*) \`on\` (f . g)@
32 -- * @'flip' on f . 'flip' on g = 'flip' on (g . f)@
34 -- Proofs (so that I don't have to edit the test-suite):
38 -- \x y -> id x * id y
41 -- = { If (*) /= _|_ or const _|_. }
46 -- ((*) `on` f) `on` g
48 -- \x y -> ((*) `on` f) (g x) (g y)
50 -- \x y -> (\x y -> f x * f y) (g x) (g y)
52 -- \x y -> f (g x) * f (g y)
54 -- \x y -> (f . g) x * (f . g) y
60 -- flip on f . flip on g
62 -- (\h (*) -> (*) `on` h) f . (\h (*) -> (*) `on` h) g
64 -- (\(*) -> (*) `on` f) . (\(*) -> (*) `on` g)
66 -- \(*) -> (*) `on` g `on` f
68 -- \(*) -> (*) `on` g . f
70 -- (\h (*) -> (*) `on` h) (g . f)
74 on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
75 (*) `on` f = \x y -> f x * f y