This general applicative pattern for numbers is packed up in the applicative-numbers package [1].
In addition to Ralf's paper, there's a discussion in section 10 of *Denotational design with type class morphisms* [2] and an application in sections 2 & 4 of *Beautiful differentiation* [3].
[1]: http://hackage.haskell.org/package/applicative-numbers
[2]: http://conal.net/papers/type-class-morphisms/
[3]: http://conal.net/papers/beautiful-differentiation/
-- Conal
On 3/19/12 12:57 PM, sdiyazg@sjtu.edu.cn wrote:You should take a look at Ralf Hinze's _The Lifting Lemma_:
By arithmetic I mean the everyday arithmetic operations used in engineering.
In signal processing for example, we write a lot of expressions like f(t)=g(t)+h(t)+g'(t) or f(t)=g(t)*h(t).
I feel it would be very natural to have in haskell something like
http://www.cs.ox.ac.uk/ralf.hinze/WG2.8/26/slides/ralf.pdf
The fact that you can lift arithmetic to work on functions comes from the fact that for every type T, the type (T->) is a monad and therefore is an applicative functor. The output type of the function doesn't matter, except inasmuch as the arithmetic operations themselves care.
This pattern has been observed repeatedly, even long before Haskell was around. But one reason it's not too common in production Haskell code is that it's all too easy to make a mistake when programming (e.g., you don't mean to be adding functions but you accidentally forget some argument), and if you're using this trick implicitly by providing a Num instance, then you can get arcane/unexpected/unhelpful error messages during type checking.
But then you do get some fun line noise :)
twiceTheSumOf = (+) + (+)
squareTheSumOf = (+) * (+)
cubeTheSumOf = (+) * (+) * (+)
-- N.B., the names only make sense if all arguments
-- are numeric literals. Don't look at the types.
addThreeThings = (+) . (+)
addFourThings = (+) . (+) . (+)
addFiveThings = (+) . (+) . (+) . (+)
--
Live well,
~wren
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe