I'm trying to implement a data structure where if I had the use of infinity for numerical comparison purposes, it would simply things greatly. Note this isn't maxBound/minBound, because a value can be <= maxbound, but all values would be < infinity.
No hope?
Take a look at my RangedSets library, which does exactly this in a very general way. I defined a "Boundary" type so that a value of type "Boundary a" is always either above or below any given "a". Boundaries can be "AboveAll", "BelowAll", "Above x" and "Below x".
Try something like this. However, to get
Num
operations (like+
or-
) you will need to defineNum
instance forInfinitable a
type. Just like I've done it forOrd
class.There is a more principled approach based on an idea from non-standard analysis. Given a totally ordered ring R of characteristic zero, you can consider the Laurent ring R[inf,1/inf] with the natural lexicographic total ordering. For example, you have:
This way the Laurent ring R[inf,1/inf] is again a totally ordered Z-algebra, i.e. an instance of
Num
, with other niceties you possibly want, including +/-infinity, +/-infinitesimal, second-order infinitesimals, etc.. But note that it's not Archimedian and induction will no longer work, which is a sort of second-order arithmetic. For implementation take a look at this example. As in the comment in the code this construction should work for other algebras, such as the list monad. You can think of lists where two elements are "infinitely close" "second-order infinitely far away" etc. (which leads to a generalization of rose trees.)Well how about that! It turns out if you just type
1/0
it returnsInfinity
! On ghci:and then of course it runs forever, never finding a number bigger than infinity. (But see ephemient's comments below on the actual behavior of
[1..]
)