de Morgan法律实施中的Haskell点(。)运算符

时间:2017-01-14 16:50:40

标签: haskell demorgans-law dot-operator

this question中,作者在Haskell中编写了de Morgan定律的实现。我理解notAandnotBnotAornotB的实现,但我很难理解notAorB的实现,即:

notAorB :: (Either a b -> c) -> (a -> c, b -> c)
notAorB f = (f . Left, f . Right)

有人可以解释(f . Left, f . Right)部分的工作原理吗?我已经看过之前使用的.运算符,但有三个参数,而不是两个。

提前谢谢。

1 个答案:

答案 0 :(得分:3)

回想一下.运算符的定义是(f . g) x = f (g x) f . g = \x -> f (g x)(从语法上讲,它是二进制运算符,只是Haskell的语法糖允许后一个定义被重述为前者。因此,您所拥有的定义可以改为

notAorB f = ((\x -> f (Left x)), (\y -> f (Right y)))

(这可以通过Lambdabot上的#haskell机械地完成,告诉他@unpl ‹expr›),或者更详细地

notAorB f = (lt, rt)
  where lt x = f (Left x)
        rt y = f (Right y)

与往常一样,尝试写下类型。如果(.) :: ∀ s t u. (t -> u) -> (s -> t) -> s -> uf :: Either a b -> cLeft :: ∀ p q. p -> Either p q,则f . Left(.) f Left :: a -> c等。