Difference between revisions of "Num instance for functions"
Jump to navigation
Jump to search
(Category:FAQ, misspelling reflected the change precisely, but it was not the intended one) |
(Refurbish very patchy article to contain the essentials) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | + | It is possible to write a <hask>Num</hask> instance for functions in Haskell: |
|
− | to add functions nicely, say for |
||
− | <haskell>f, g :: Num a => b -> a</haskell> |
||
− | you would define |
||
− | <haskell>(f+g) x = f x + g x</haskell> |
||
+ | <haskell> |
||
− | With an according definition of <hask>fromInteger</hask> |
||
+ | instance Num b => Num (a -> b) where |
||
− | <haskell>fromInteger = const</haskell> |
||
+ | negate = fmap negate |
||
− | numeric literals would also denote constant functions. This allows |
||
− | + | (+) = liftA2 (+) |
|
+ | (*) = liftA2 (*) |
||
+ | fromInteger = pure . fromInteger |
||
+ | abs = fmap abs |
||
+ | signum = fmap signum |
||
+ | </haskell> |
||
+ | This allows writing expressions of the form |
||
− | Even nicer, the mathematically established notation of omitting the |
||
− | multiplication dot |
||
− | <haskell>2(x+y) :: Integer</haskell> |
||
− | will now be parsed by a Haskell compiler to the most obvious meaning |
||
− | <haskell>2 :: Integer</haskell> |
||
− | ! :-) |
||
+ | <haskell> |
||
− | == Note == |
||
+ | > (sin^2 + cos^2) 123.4 |
||
+ | 1.0 |
||
+ | </haskell> |
||
+ | where functions can be added like <hask>Num</hask>bers, leaving their arguments open for later. |
||
− | This article is in category Proposals in order to show people that this idea was already proposed, |
||
− | but that one should think twice implementing it. |
||
− | There should be a category Counterproposals. |
||
+ | The usefulness of this instance is debatable, and it is not currently part of [[Language and library specification|the language report]] or defined in any popular library. |
||
== See also == |
== See also == |
||
+ | * The [[applicative-numbers]] package, which generates numeric class instances for arbitrary [[applicative functor]]s (including functions). |
||
* http://www.haskell.org/pipermail/haskell-cafe/2006-November/019374.html |
* http://www.haskell.org/pipermail/haskell-cafe/2006-November/019374.html |
||
* http://www.haskell.org/pipermail/haskell-cafe/2006-October/019105.html |
* http://www.haskell.org/pipermail/haskell-cafe/2006-October/019105.html |
||
* http://www.haskell.org/pipermail/haskell-cafe/2001-February/001531.html |
* http://www.haskell.org/pipermail/haskell-cafe/2001-February/001531.html |
||
+ | * http://augustss.blogspot.com/2009/02/regression-they-say-that-as-you-get.html |
||
− | |||
[[Category:Humor]] |
[[Category:Humor]] |
||
⚫ | |||
[[Category:FAQ]] |
[[Category:FAQ]] |
||
⚫ |
Latest revision as of 01:24, 28 December 2014
It is possible to write a Num
instance for functions in Haskell:
instance Num b => Num (a -> b) where
negate = fmap negate
(+) = liftA2 (+)
(*) = liftA2 (*)
fromInteger = pure . fromInteger
abs = fmap abs
signum = fmap signum
This allows writing expressions of the form
> (sin^2 + cos^2) 123.4
1.0
where functions can be added like Num
bers, leaving their arguments open for later.
The usefulness of this instance is debatable, and it is not currently part of the language report or defined in any popular library.
See also
- The applicative-numbers package, which generates numeric class instances for arbitrary applicative functors (including functions).
- http://www.haskell.org/pipermail/haskell-cafe/2006-November/019374.html
- http://www.haskell.org/pipermail/haskell-cafe/2006-October/019105.html
- http://www.haskell.org/pipermail/haskell-cafe/2001-February/001531.html
- http://augustss.blogspot.com/2009/02/regression-they-say-that-as-you-get.html