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
25 -- | @'fix' f@ is the least fixed point of the function @f@,
26 -- i.e. the least defined @x@ such that @f x = x@.
28 fix f = let x = f x in x
30 -- | @(*) \`on\` f = \\x y -> f x * f y@.
32 -- Typical usage: @'Data.List.sortBy' ('compare' \`on\` 'fst')@.
34 -- Algebraic properties:
36 -- * @(*) \`on\` 'id' = (*)@ (if @(*) ∉ {⊥, 'const' ⊥}@)
38 -- * @((*) \`on\` f) \`on\` g = (*) \`on\` (f . g)@
40 -- * @'flip' on f . 'flip' on g = 'flip' on (g . f)@
42 -- Proofs (so that I don't have to edit the test-suite):
46 -- \x y -> id x * id y
49 -- = { If (*) /= _|_ or const _|_. }
54 -- ((*) `on` f) `on` g
56 -- \x y -> ((*) `on` f) (g x) (g y)
58 -- \x y -> (\x y -> f x * f y) (g x) (g y)
60 -- \x y -> f (g x) * f (g y)
62 -- \x y -> (f . g) x * (f . g) y
68 -- flip on f . flip on g
70 -- (\h (*) -> (*) `on` h) f . (\h (*) -> (*) `on` h) g
72 -- (\(*) -> (*) `on` f) . (\(*) -> (*) `on` g)
74 -- \(*) -> (*) `on` g `on` f
76 -- \(*) -> (*) `on` g . f
78 -- (\h (*) -> (*) `on` h) (g . f)
82 on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
83 (*) `on` f = \x y -> f x * f y