不是函子(或不可遍历)的可折叠示例?
可折叠
实例可能是某种容器,因此也可能是一个函子。事实上,这说
Foldable
类型也是一个容器(尽管该类在技术上不需要Functor
,有趣的Foldable
都是Functor
s)。
那么是否有一个 Foldable
的例子,它本质上不是一个 Functor
或 Traversable
? (也许 Haskell wiki 页面错过了:-))
A Foldable
instance is likely to be some sort of container, and so is likely to be a Functor
as well. Indeed, this says
A
Foldable
type is also a container (although the class does not technically requireFunctor
, interestingFoldable
s are allFunctor
s).
So is there an example of a Foldable
which is not naturally a Functor
or a Traversable
? (which perhaps the Haskell wiki page missed :-) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个完全参数化的示例:
Weird
不是Functor
,因为a
出现在负数位置。Here's a fully parametric example:
Weird
is not aFunctor
becausea
occurs in a negative position.这是一个简单的示例:
Data.Set.Set
。 亲自看看。如果您检查为
Set
定义的专用fold
和map
函数的类型,其原因应该很明显:因为数据结构依赖于二叉搜索树内部,元素需要
Ord
约束。Functor
实例必须允许任何元素类型,所以这是不可行的,唉。另一方面,折叠总是破坏树来产生汇总值,因此不需要对折叠的中间结果进行排序。即使折叠实际上正在构建一个新的
Set
,满足Ord
约束的责任在于传递给折叠的累积函数,而不是折叠本身。这同样可能适用于任何不完全参数化的容器类型。考虑到 Data.Set 的实用性,我认为这使得您引用的有关“有趣的”Foldable 的评论似乎有点可疑!
Here's an easy example:
Data.Set.Set
. See for yourself.The reason for this should be apparent if you examine the types of the specialized
fold
andmap
functions defined forSet
:Because the data structure relies on a binary search tree internally, an
Ord
constraint is needed for elements.Functor
instances must allow any element type, so that's not viable, alas.Folding, on the other hand, always destroys the tree to produce the summary value, so there's no need to sort the intermediate results of the fold. Even if the fold is actually building a new
Set
, the responsibility for satisfying theOrd
constraint lies on the accumulation function passed to the fold, not the fold itself.The same will probably apply to any container type that's not fully parametric. And given the utility of
Data.Set
, this makes the remark you quoted about "interesting"Foldable
s seem a bit suspect, I think!阅读美丽的折叠
将其包装成
Functor
我意识到任何
Foldable
都可以通过使用简单的智能构造函数:(这只是 存储 comonad 数据类型。)
现在我们可以 。
这样,我们就可以将
Data.Set.Set
和 Sjoerd Visscher 的Weird
都设为函子 (但是,由于该结构不会记住其值,因此如果我们在fmap
中使用的函数很复杂,则重复折叠它可能会非常低效。)更新:这也提供了一个函子结构的示例,可折叠但不可遍历。为了使
Store
可遍历,我们需要使(->) r
可遍历。因此,我们需要实现Let's take
Either b
forf
。那么我们需要实现显然,没有这样的功能(你可以用 Djinn 来验证) 。所以我们都无法实现
sequenceA
。Reading Beautiful folding
I realized that any
Foldable
can be made aFunctor
by wrapping it intowith a simple smart contructor:
(This is just a variant of the Store comonad data type.)
Now we can define
This way, we can make both
Data.Set.Set
and Sjoerd Visscher'sWeird
a functor. (However, since the structure doesn't memoize its values, repeatedly folding over it could be very inefficient, if the function that we used infmap
is complex.)Update: This also provides an example of a structure that is a functor, foldable but not traversable. To make
Store
traversable, we would need to make(->) r
traversable. So we'd need to implementLet's take
Either b
forf
. Then we'd need to implementClearly, there is no such function (you can verify with Djinn). So we can neither realize
sequenceA
.