gpt4 book ai didi

haskell - 最小完整定义 Ord 是如何选择的?

转载 作者:行者123 更新时间:2023-12-04 18:11:18 25 4
gpt4 key购买 nike

Data.Ord 包括这些方法:

compare :: a -> a -> Ordering

(<) :: a -> a -> Bool

(<=) :: a -> a -> Bool

(>) :: a -> a -> Bool

(>=) :: a -> a -> Bool

max :: a -> a -> a

min :: a -> a -> a
最小的完整定义是 compare | (<=) .
我了解如何从这两种方法中的任何一种中确定其他方法。
我不明白为什么 (>) (例如)也不能用作最小的完整定义。 not (a > b)相当于 a <= b .
是对 (>) 的决定吗?作为一个最小的完整定义被排除在外,还是我遗漏了什么?

最佳答案

我认为 the source对此有一点启发:

class  (Eq a) => Ord a  where
compare :: a -> a -> Ordering
(<), (<=), (>), (>=) :: a -> a -> Bool
max, min :: a -> a -> a

compare x y = if x == y then EQ
else if x <= y then LT
else GT

x < y = case compare x y of { LT -> True; _ -> False }
x <= y = case compare x y of { GT -> False; _ -> True }
x > y = case compare x y of { GT -> True; _ -> False }
x >= y = case compare x y of { LT -> False; _ -> True }
如您所见,所有比较都是根据 compare 实现的。和 compare 本身是根据 <= 实现的.选择有点随意(尽管正如 Willem van Onsem 所写,它可能源于数学传统),但这确实排除了允许使用额外的运算符作为最小定义。 compare 只能有一个默认定义.

关于haskell - 最小完整定义 Ord 是如何选择的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71211495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com