Haskell で 4 つの De Morgan の法則のうち 3 つを実装しました。
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
ただし、最後の法律 (住民が 2 人いる) を実装することは可能ではないと思います。
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))
私の見方では、2つの可能な解決策があります。
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 でエンコードできないと仮定するのは正しいですか? これはカリー・ハワード同型について何を言っているのですか? すべての証明を同等のコンピューター プログラムに変換できない場合、それは実際には同型ではありませんよね?