为什么守卫需要 Idris 中的 Ord 类型类?

Why does guard need the Ord typeclass in Idris?

在另一个问题 () 中,我试图理解为什么 Prelude.Applicative.guard 需要一个 Ord 类型类。

守卫已定义like this:

guard : Alternative f => Bool -> f ()
guard a = if a then pure () else empty

查看Alternative接口docs(我实际上还不明白它在代码中是如何定义的,但我在学习 Idris 方面还不是很远),我不知道也不明白它如何需要 Ord

guard 单独不需要 Ord 约束,而是 (<) 在你之前的问题中。我在那里给出了区分 List (Ord b)Ord b => List b 的答案。

要了解为什么 guard 抱怨缺少约束,请查看 monad comprehensions 是如何脱糖的。

[y | y <- xs, y < x] 变为 do {y <- xs; guard (y < x); pure y}.