我在 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 同构有什么看法?如果每个证明都不能转换成等效的计算机程序,那它就不是真正的同构,对吧?