Open dmcclean opened 10 years ago
Since I have absolutely no idea of the answer and it would be my first time ever reaching for these power tools, I'll defer on the question of whether {-# RULES ...#-}
sorcery is appropriate in this case or whether it is a horrible idea without complete confluence between what the by-the-book implementation would do and what the simplified-by-rule implementation would do. (Got this idea by reading here: http://hackage.haskell.org/package/base-4.7.0.0/docs/src/GHC-Real.html#^)
We could just add a special purpose square
combinator to start.
The boost interval library has a nice function
square :: Interval a -> Interval a
such thatsquare x
returns a tighter bound thanx * x
can (without observable sharing and a lot of crazy machinery) because it knows that the result must be positive.Apparently they also have an exponentiation operator for integer exponents that does the same for all even exponents. This could be tough for us though, because (I think maybe to avoid introducing a rank-2 type?) the
Prelude
(^)
function isn't part of theNum
typeclass it just has a fixed implementation in the prelude that delegates to(*)
. If the type was(^) :: (Num a) => (forall b.(Integral b) => a -> b -> a)
(or something like that, I may have too many parentheses or too few) instead of(^) :: (Num a, Integral b) => a -> b -> a
we would have a chance but as it stands I think the only option would be to offer this behavior under a new name.(There is a TODO comment in the current version about this, presumably written because the type issue makes the fix a murky design area. I am promoting it to an issue because at least the
square
case is pretty useful for my work.)