一个没有成为应用的函子的具体类型例子? [英] Concrete Type Example of a Functor that Fails to be an Applicative?

查看:142
本文介绍了一个没有成为应用的函子的具体类型例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不适用的函子


一个类型构造函数,它是一个Functor而不是一个Applicative。一个简单的例子是一对:

但是,如何定义 Applicative 实例,而不对 r 施加额外限制。特别是,如何定义 pure :: a - >是没有办法的。 (r,a)为任意 r


这里,纯粹无法一次为所有类型定义;然而,对于任何具体类型 T ,可以使((,)T)

问题:是否存在一个具体仿函数(即,没有涉及的类型变量)的例子,它是一个仿函数而不是应用程序?

解决方案

我没有50个评论,所以我会尽力回答:然而,对于任何具体类型T,可以使((,)T)成为一个应用。


$ b

...


数学中有一个定理,至少2个元素可以被制成一个monoid。因此,对于任何具体类型T,它原则上可以成为Monoid的成员,然后原则上可以适用。这个推理有什么问题?


那么这个无人居住类型的元组呢? (,)Void



它是 Functor ,对吗?



您可以为它导出 Applicative 吗? 纯粹如何实现?


From functors that are not applicatives:

A type constructor which is a Functor but not an Applicative. A simple example is a pair:

instance Functor ((,) r) where
    fmap f (x,y) = (x, f y)

But there is no way how to define its Applicative instance without imposing additional restrictions on r. In particular, there is no way how to define pure :: a -> (r, a) for an arbitrary r.

Here, pure fails to be definable for all types at once; however, for any concrete type T, one can make ((,) T) an applicative.

Question: Is there an example of a concrete functor (i.e., no type variables involved) that is a functor but not an applicative?

解决方案

I don't have 50 reputation to comment here, so I'll try to do it as an answer:

however, for any concrete type T, one can make ((,) T) an applicative.

...

There's a theorem in mathematics that any collection with at least 2 elements can be made into a monoid. So for any concrete type T, it could in principle be made a member of Monoid, and then could in principle be made Applicative. What's wrong with this reasoning?

What about the tuple from the uninhabited type? (,) Void

It is a Functor,right?

Could you derive Applicative for it? How would pure be implemented?

这篇关于一个没有成为应用的函子的具体类型例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆