打字员科代表Haskell生态系统中的标准和基本抽象:
class Contravariant f where
contramap :: (a -> b) -> f b -> f a
class Contravariant f => Divisible f where
conquer :: f a
divide :: (a -> (b, c)) -> f b -> f c -> f a
class Divisible f => Decidable f where
lose :: (a -> Void) -> f a
choose :: (a -> Either b c) -> f b -> f c -> f a然而,要理解这些类型分类背后的概念并不那么容易。我认为,如果你能看到一些反例,这将有助于更好地理解这些类型的人。因此,本着https://stackoverflow.com/questions/7220436/good-examples-of-not-a-functor-functor-applicative-monad的精神,我正在寻找满足以下要求的数据类型的对比示例:
Contravariant的类型构造函数Contravariant,而不是Divisible。Divisible,但不是Decidable。Decidable发布于 2019-05-14 11:59:17
(部分答覆)
非反差
newtype F a = K (Bool -> a)不是逆变(然而,它是一个协变函子)。
反变,但不可除
newtype F a = F { runF :: a -> Void }是反变体,但它不能是Divisible,因为否则
runF (conquer :: F ()) () :: Void关于“可分但不可分”的注记
我没有一个合理的可分的例子,这是不可分的。我们可以看到,这样的反例必须是这样的,因为它违反了法律,而不仅仅是类型签名。事实上,如果Divisible F坚持,
instance Decidable F where
lose _ = conquer
choose _ _ _ = conquer满足方法的类型签名。
在库中,当Const m是单半群时,我们发现m是可除的。
instance Monoid m => Divisible (Const m) where
divide _ (Const a) (Const b) = Const (mappend a b)
conquer = Const mempty也许这不可能是合法的Decidable?(我不确定,它似乎符合Decidable定律,但库中没有Decidable (Const m)实例。)
判断力
摘自图书馆:
newtype Predicate a = Predicate (a -> Bool)
instance Divisible Predicate where
divide f (Predicate g) (Predicate h) = Predicate $ \a -> case f a of
(b, c) -> g b && h c
conquer = Predicate $ const True
instance Decidable Predicate where
lose f = Predicate $ \a -> absurd (f a)
choose f (Predicate g) (Predicate h) = Predicate $ either g h . fhttps://stackoverflow.com/questions/56129585
复制相似问题