简体   繁体   English

违反适用的函子定律

[英]Applicative functor laws violation

Excersize from an online course.从在线课程中锻炼。

Suppose, that for a standard list Applicative functor the <*> operator is defined in a standard way, while pure is changed to假设,对于标准列表 Applicative functor, <*>运算符以标准方式定义,而pure更改为

pure x = [x,x]

What laws of Applicative typeclass will be violated?会违反 Applicative typeclass 的哪些规律?

  • Homomorphism: pure g <*> pure x ≡ pure (gx)同态: pure g <*> pure x ≡ pure (gx)
  • Identity: pure id <*> xs ≡ xs身份: pure id <*> xs ≡ xs
  • Interchange: fs <*> pure x ≡ pure ($ x) <*> fs交换: fs <*> pure x ≡ pure ($ x) <*> fs
  • Applicative-Functor: g <$> xs ≡ pure g <*> xs Applicative-Functor: g <$> xs ≡ pure g <*> xs
  • Composition: (.) <$> us <*> vs <*> xs ≡ us <*> (vs <*> xs)组成: (.) <$> us <*> vs <*> xs ≡ us <*> (vs <*> xs)

I created the following file:我创建了以下文件:

newtype MyList a = MyList {getMyList :: [a]}
  deriving Show

instance Functor MyList where
  fmap f (MyList xs) = MyList (map f xs)

instance Applicative MyList where
  pure x = MyList [x,x]
  MyList gs <*> MyList xs = MyList ([g x | g <- gs, x <- xs])

fs = MyList [\x -> 2*x, \x -> 3*x]
xs = MyList [1,2]
x = 1
g = (\x -> 2*x)
us = MyList [(\x -> 2*x)]
vs = MyList [(\x -> 3*x)]

Then I tried:然后我尝试:

Homomorphism: pure g <*> pure x ≡ pure (gx)同态: pure g <*> pure x ≡ pure (gx)

*Main> pure g <*> pure x :: MyList Integer
MyList {getMyList = [2,2,2,2]}
*Main> pure (g x) :: MyList Integer
MyList {getMyList = [2,2]}

Identity: pure id <*> xs ≡ xs身份: pure id <*> xs ≡ xs

*Main> pure id <*> xs :: MyList Integer
MyList {getMyList = [1,2,1,2]}
*Main> xs :: MyList Integer
MyList {getMyList = [1,2]}

Interchange: fs <*> pure x ≡ pure ($ x) <*> fs交换: fs <*> pure x ≡ pure ($ x) <*> fs

*Main> fs <*> pure x
[2,3]
*Main> pure ($ x) <*> fs
[2,3]

Applicative-Functor: g <$> xs ≡ pure g <*> xs Applicative-Functor: g <$> xs ≡ pure g <*> xs

*Main> g <$> xs
MyList {getMyList = [2,4]}
*Main> pure g <*> xs
MyList {getMyList = [2,4,2,4]}

Composition: (.) <$> us <*> vs <*> xs ≡ us <*> (vs <*> xs)组成: (.) <$> us <*> vs <*> xs ≡ us <*> (vs <*> xs)

*Main> (.) <$> us <*> vs <*> xs
MyList {getMyList = [6,12]}
*Main> us <*> (vs <*> xs)
MyList {getMyList = [6,12]}

Composition shouldn't be violated, because pure is not used here.不应违反组合,因为这里不使用pure

It seems that Homomorphism, Identity and Applicative-Functor don't work.似乎同态、恒等和 Applicative-Functor 不起作用。 But when I select them in the course, it says that the answer is wrong.但是当我在课程中选择它们时,它说答案是错误的。 So, who is the fool: me or the author of the course?那么,谁是傻瓜:我还是课程的作者?

As per @DavidFletcher's comment, using your code, I see different output for the interchange test:根据@DavidFletcher 的评论,使用您的代码,我看到交换测试的不同输出:

> fs <*> pure x
MyList {getMyList = [2,2,3,3]}
> pure ($ x) <*> fs
MyList {getMyList = [2,3,2,3]}

so you might want to double-check that one.所以你可能想仔细检查那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM