在 Haskell 中,是否存在无穷大 :: Num a =>一个?

发布于 2024-08-23 16:23:10 字数 137 浏览 7 评论 0原文

我正在尝试实现一种数据结构,如果我使用无穷大来进行数值比较,事情就会变得非常简单。请注意,这不是 maxBound/minBound,因为值可以 <= maxbound,但所有值都将 <= maxBound。无穷大。

没有希望了吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

自由范儿 2024-08-30 16:23:10

嗯,那又如何呢!事实证明,如果您只输入 1/0,它就会返回 Infinity!在 ghci 上:

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

当然它会永远运行,永远不会找到大于无穷大的数字。 (但请参阅下面关于 [1..] 实际行为的 ephemient 评论)

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

是你 2024-08-30 16:23:10
infinity = read "Infinity"
infinity = read "Infinity"
顾冷 2024-08-30 16:23:10

也许您想要 Maybe 类型?

data Infinite a = Infinite | Only a

然后为 Num a => 编写一个 Num 实例无限 a,具有您需要的数字规则。

Maybe you want a Maybe type?

data Infinite a = Infinite | Only a

then write a Num instance for Num a => Infinite a, with the numeric rules you need.

尛丟丟 2024-08-30 16:23:10

尝试这样的事情。但是,要获得 Num 操作(例如 +-),您需要为 定义 Num 实例>无限a类型。就像我为 Ord 类所做的那样。

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)

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)
你的他你的她 2024-08-30 16:23:10
λ: let infinity = (read "Infinity")::Double
λ: infinity > 1e100
True
λ: -infinity < -1e100
True
λ: let infinity = (read "Infinity")::Double
λ: infinity > 1e100
True
λ: -infinity < -1e100
True
初见你 2024-08-30 16:23:10

看看我的 RangedSets 库,它以非常通用的方式执行此操作。我定义了一个“边界”类型,以便“边界 a”类型的值始终高于或低于任何给定的“a”。边界可以是“AboveAll”、“BelowAll”、“Above x”和“Below x”。

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

清君侧 2024-08-30 16:23:10

您可以使用ieee754软件包 它提供了 <代码>IEEE类型类。该类型类有一个infinity 成员。该类型类针对 FloatDoubleCFloatCDouble 实现。

因此,您可以安装 ieee-754 软件包,然后通过以下方式获得正无穷大(或负无穷大):

ghci> import Numeric.IEEE
ghci> infinity :: Double
Infinity
ghci> -infinity :: Double
-Infinity

对于 Float 、Double 等.它实现为[源]

<前><代码>无穷大 ​​= 1/0

You can work with the ieee754 package which offers an IEEE typeclass. This typeclass has an infinity member. The typeclass is implemented for Float, Double, CFloat and CDouble.

You thus install the ieee-754 package, and then obtain positive infinity (or negative infinity) with:

ghci> import Numeric.IEEE
ghci> infinity :: Double
Infinity
ghci> -infinity :: Double
-Infinity

For Floats, Doubles, etc. it is implemented as [src]:

    infinity = 1/0
昇り龍 2024-08-30 16:23:10

如果您的用例是您的边界条件有时需要检查,但有时不需要,您可以这样解决:

type Bound a = Maybe a

withinBounds :: (Num a, Ord a) => Bound a -> Bound a -> a -> Bool
withinBounds lo hi v = maybe True (<=v) lo && maybe True (v<=) hi

If your use case is that you have boundary conditions that sometimes need to be checked, but sometimes not, you can solve it like this:

type Bound a = Maybe a

withinBounds :: (Num a, Ord a) => Bound a -> Bound a -> a -> Bool
withinBounds lo hi v = maybe True (<=v) lo && maybe True (v<=) hi
伤痕我心 2024-08-30 16:23:10

有一种基于非标准分析想法的更有原则的方法。给定特征为零的全序环 R,您可以考虑具有自然词典全序的洛朗环 R[inf,1/inf]。例如,您有:

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

这样,洛朗环 R[inf,1/inf] 又是一个全序 Z 代数,即 Num 的实例,以及您可能想要的其他细节,包括 + /-无穷大、+/-无穷小、二阶无穷小等。但请注意,这不是阿基米德,归纳法将不再起作用,这是一种二阶算术。如需实现,请查看此示例 。正如代码中的注释所示,这种构造应该适用于其他代数,例如列表单子。您可以想到两个元素“无限接近”“二阶无限远”等的列表(这导致了玫瑰树的泛化。)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文