In Haskell, is there infinity :: Num a => a?

2020-02-10 01:13发布

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?

8条回答
家丑人穷心不美
2楼-- · 2020-02-10 01:35

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".

查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-10 01:36

Try something like this. However, to get Num operations (like + or -) you will need to define Num instance for Infinitable a type. Just like I've done it for Ord class.

data Infinitable a = Regular a | NegativeInfinity | PositiveInfinity deriving (Eq, Show)

instance Ord a => Ord (Infinitable a) where
    compare NegativeInfinity NegativeInfinity = EQ
    compare PositiveInfinity PositiveInfinity = EQ
    compare NegativeInfinity _ = LT
    compare PositiveInfinity _ = GT
    compare _ PositiveInfinity = LT
    compare _ NegativeInfinity = GT
    compare (Regular x) (Regular y) = compare x y    

main =
    let five = Regular 5
        pinf = PositiveInfinity::Infinitable Integer
        ninf = NegativeInfinity::Infinitable Integer
        results = [(pinf > five), (ninf < pinf), (five > ninf)]
    in
        do putStrLn (show results)
查看更多
Lonely孤独者°
4楼-- · 2020-02-10 01:38

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:

for all x>0 in R,
.. -inf < -x < -d < -d^2 < .. < 0 < .. < d^2 < d < x < inf < inf^2 < .. 
where d = 1/inf.

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.)

查看更多
地球回转人心会变
5楼-- · 2020-02-10 01:40

Well how about that! It turns out if you just type 1/0 it returns Infinity! On ghci:

Prelude> 1/0
Infinity
Prelude> :t 1/0
1/0 :: (Fractional t) => t
Prelude> let inf=1/0
Prelude> filter (>=inf) [1..]

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..])

查看更多
我只想做你的唯一
6楼-- · 2020-02-10 01:40
infinity = read "Infinity"
查看更多
我欲成王,谁敢阻挡
7楼-- · 2020-02-10 01:45
λ: let infinity = (read "Infinity")::Double
λ: infinity > 1e100
True
λ: -infinity < -1e100
True
查看更多
登录 后发表回答