[英]De Morgan's Laws in Haskell via the Curry-Howard Correspondence
我在Haskell實施了四個De Morgan定律中的三個:
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))
這不是一個好的解決方案,因為這是一個比德摩根定律更弱的法律。
我們知道De Morgan的定律是正確的,但我認為最后的定律不能用Haskell編碼是正確的嗎? 這對庫里 - 霍華德同構有什么看法? 如果每個證據都不能轉換成等效的計算機程序,那么它並不是真正的同構,對吧?
有一點讓我感到驚訝的是,你似乎沒有在任何地方使用定義或任何否定的屬性。
在閱讀了關於CHI的Haskell Wikibooks文章之后,這里有一個證據,假設你有一個排除中間的定律作為一個定理:
exc_middle :: Either a (a -> Void)
並且notAandB
de Morgan法律的證明將如下:
notAandB' :: Either a (a -> Void) -> ((a,b) -> Void) -> Either (a -> Void) (b -> Void)
notAandB' (Right notA) _ = Left notA
notAandB' (Left a) f = Right (\b -> f (a,b))
notAandB = notAandB' exc_middle
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.