【发布时间】:2016-08-29 05:44:00
【问题描述】:
我在 Haskell 中实现了四个德摩根定律中的三个:
notAandNotB :: (a -> c, b -> c) -> Either a b -> c
notAandNotB (f, g) (Left x) = f x
notAandNotB (f, g) (Right y) = g y
notAorB :: (Either a b -> c) -> (a -> c, b -> c)
notAorB f = (f . Left, f . Right)
notAorNotB :: Either (a -> c) (b -> c) -> (a, b) -> c
notAorNotB (Left f) (x, y) = f x
notAorNotB (Right g) (x, y) = g y
但是,我不认为有可能实施最后一条法律(有两个居民):
notAandBLeft :: ((a, b) -> c) -> Either (a -> c) (b -> c)
notAandBLeft f = Left (\a -> f (a, ?))
notAandBRight :: ((a, b) -> c) -> Either (a -> c) (b -> c)
notAandBRight f = Right (\b -> f (?, b))
在我看来,有两种可能的解决方案:
- 使用
undefined代替?。这不是一个好的解决方案,因为它是作弊。 -
使用单态类型或有界多态类型来编码默认值。
notAandBLeft :: Monoid b => ((a, b) -> c) -> Either (a -> c) (b -> c) notAandBLeft f = Left (\a -> f (a, mempty)) notAandBRight :: Monoid a => ((a, b) -> c) -> Either (a -> c) (b -> c) notAandBRight f = Right (\b -> f (mempty, b))这不是一个好的解决方案,因为它比德摩根定律更弱。
我们知道德摩根定律是正确的,但我认为最后一条定律不能用 Haskell 编码是否正确?这对 Curry-Howard 同构有什么看法?如果不能将每个证明都转换为等效的计算机程序,这并不是真正的同构,对吧?
【问题讨论】:
-
我倾向于怀疑您错误地应用了同构。
-
我认为我没有错误地应用同构。你能详细说明一下吗?
-
请注意,这些公式纯粹是命题(周围没有量词)。由于命题直觉逻辑是可判定的,因此您可以使用定理证明器(例如 Djinn)来解决这些问题。如果证明者失败了,那么它肯定是一个非直觉的重言式。
-
想一想这个“定律”在计算上可能意味着什么。它说,“只要一个连词是假的,你就可以找出哪个连词是假的”。例如,我们知道
(a, a -> c) -> c,所以 de Morgan 告诉我们,我们可以决定任何命题a,解决停机问题,随便你说。即使 Haskell 的多态性不是参数化的,那也是一个相当大的问题。但是对于参数多态性,这个“定律”必须在不知道命题是什么的情况下决定任意命题。
标签: haskell functional-programming demorgans-law curry-howard