问题描述
我认为我在针对Control的黑客文章中发现了一个缺陷.适用 .作为适用函子定律的描述,它说:
I think I found a flaw in the hackage article for Control.Applicative
.As a description of the applicative functor laws, it says:
class Functor f => Applicative f where
带有应用程序的函子,可为嵌入纯表达式( pure
),并进行序列计算并组合其结果(< *>
).
A functor with application, providing operations to embed pure expressions (pure
), and sequence computations and combine their results (<*>
).
最小的完整定义必须包括满足以下法律的这些功能的实现:
A minimal complete definition must include implementations of these functions satisfying the following laws:
身份
identity
pure id <*> v = v
组成
composition
pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
同态
homomorphism
pure f <*> pure x = pure (f x)
交换
interchange
u <*> pure y = pure ($ y) <*> u
(请注意,这并没有说明fmap),它指出这些法律决定了 Functor
实例:
(note that this does not say anything about fmap) and it states that these laws determine the Functor
instance:
fmap f x = pure f <*> x
我认为起初这显然是错误的.也就是说,我猜想必须存在一个满足以下两个条件的类型构造器 t
:
I thought at first this was obviously wrong. That is, I guessed there must exist a type constructor t
satisfying the following two conditions:
-
t
是满足上述规则的Applicative
的实例,并且 -
instance(Functor t)
有两种不同的实现方式(即,有两个不同的函数fmap1,fmap2 ::(a-> b)->(t a-> t b)
,满足函子法则.
t
is an instance ofApplicative
fulfilling the rules above, and- There are two different implementations of
instance (Functor t)
(i.e. there are two distinct functionsfmap1, fmap2 :: (a->b) -> (t a->t b)
, satisfying the functor laws).
如果(且仅当)以上内容正确,则必须将以上语句 重写为
If (and only if) the above is correct, the above statement must be rewritten to
fmap f x = pure f <*> x
作为这些法律的结果,这满足了 Functor
法律.
As a consequence of these laws, this satisfies the Functor
laws.
这是正确的,无论我的猜测是否正确.
我的问题是 :我的猜测正确吗?有符合条件的 t
吗?
下面是对我自己回答这个问题时的想法的解释.
What follows is the explanation of what I thought during trying to answer this question by myself.
如果我们只是对实际的Haskell编程不感兴趣的数学家,我们可以轻松地肯定地回答这个问题.实际上,
If we were mere mathematicians uninterested in actual Haskell programming, we could easily answer this question affirmatively. In fact,
t = Identity
newtype Identity a = Identity {runIdentity :: a}
满足上述要求1和2(实际上,几乎所有操作都可以完成).实际上, Identity
只是 Functor
和 Applicative
的一个实例,如.(这满足 fmap f =(纯f< *>)
.)要定义 instance(Functor f)
的另一个实现",请为每种 a
类型采用两个函数
meets the requirements 1 and 2 above (in fact, almost anything will do). Indeed, Identity
is trivially an instance of Functor
and Applicative
, as defined in Data.Functor.Identity
. (This satisfies fmap f = (pure f <*>)
.)To define another "implementation" of instance (Functor f)
, Take, for each type a
, two functions
transf_a, tinv_a :: a -> a
如此
tinv_a . transf_a = id
(这很容易进行理论上的设置).现在定义
(This is, set-theoretically, easy). Now define
instance (Functor Identity) where
fmap (f :: a->b) = Identity . transf_b . f . tinv_a . runIdentity
这符合 Functor
法则,并且与琐碎的实现是不同的实现
This satisfies the Functor
laws and is a different implementation from the trivial one if there are
x :: a
f :: a -> b
如此
f x /= transf_b $ f $ tinv_a x
但是我们是否可以在Haskell中做到这一点一点也不明显.是这样的:
But it is not at all obvious whether we can do it in Haskell. Is something like:
class (Isom a) where
transf, tinv :: a -> a
instance (Isom a) where
transf = id
tinv = id
specialized instance (Isom Bool) where
transf = not
tinv = not
可能在Haskell吗?
possible in Haskell?
我忘了写非常重要的东西.我将 Control.Applicative
识别为GHC基本软件包的一部分,所以我也对我的问题的答案是否随任何GHC语言扩展(例如 FlexibleInstances
或 OverlappingInstances
,我还不了解.
I forgot to write something very important. I recognize Control.Applicative
as part of GHC base package, so I am also interested in whether the answer to my question changes with any GHC language extensions, such as FlexibleInstances
or OverlappingInstances
, which I don't understand yet.
推荐答案
Haskell中的任何类型最多可以具有一个 Functor
实例,因此您的猜测是不正确的:对于 no 输入 t
(无论它是否是 Applicative
的实例),是否可以存在 instance(Functor t)
的两种不同实现.请参阅: http://article.gmane.org/gmane.comp.lang.haskell.libraries/15384
Any type in Haskell can have at most one instance of Functor
, so your guess is not correct: For no type t
(whether or not it's an instance of Applicative
) can there exist two different implementations of instance (Functor t)
. See: http://article.gmane.org/gmane.comp.lang.haskell.libraries/15384
这篇关于Haskell:hackage Control中适用仿函数定律的描述有缺陷.适用文章?:它说由Applicative确定Functor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!