在Haskell中,Representable有什么用? [英] What is Representable used for in Haskell?

查看:85
本文介绍了在Haskell中,Representable有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解 Representable 在Haskell中代表.定义

Haskell类型类别中具有代表性的endofunctors对读者monad是同构的,因此可以免费继承大量属性.

对我来说还不够清楚.我想看一个真实的例子,以了解如何使用tabulateindex方法.

所以我的问题是:

  • 可代表对象的用途是什么?
  • 能否请您澄清一下定义并提供示例?

解决方案

Representable是类似容器的函子,具有与另一种类型的特殊关系",该另一种类型充当Representable的索引.在Haskell定义中,此索引类型由关联的类型族type Rep f :: *

给出 对于索引的每个值和Representable每个值,我们可以调用index :: f a -> Rep f -> a函数以获取相应的元素. tabulate :: (Rep f -> a) -> f a构造一个容器,其中每个元素都从其自己的索引派生.

现在,这是一个无法表示的函子的示例:典型的Haskell列表类型[].可能会天真的认为它可以用 Natural ,但是问题在于列表可以为空,或者没有足够的元素来达到给定的索引.

data Stream a = Stream a (Stream a) 那样的总是无限类型是 Representable,并且由Natural进行索引,因为传递给Natural始终会有一个值>.

类似地,同构对data Pair a = Pair a a的索引类型为Bool:索引告诉我们选择哪个组件.

如果得到依赖,则固定大小的向量Representable,并通过有限自然,其边界取决于向量的大小.它们没有被无界的Natural索引,因为那样我们就可以进行越界访问!


读取为Representable定义的各种实例是有启发性的,但是似乎我们必须深入到源代码,因为相关的类型在Haddock中不可见.一些有趣的花絮:

  • Identity函子通过单元类型()进行索引,这是有道理的,因为Identity可以说只有一个"slot",所以我们不需要提供任何信息. /p>

  • ((->) e)类型的某种类型的功能"由源类型本身建立索引.而index就是id.这就是对阅读器monad同构"的含义,因为Reader e monad只是((->) e)的新类型.

  • Composition (嵌套)两个可表示的函子再次为Representable,并由原始索引的 pair 进行索引!这是有道理的:首先我们必须知道如何索引到外部函子,然后再索引到内部函子.

  • 两个Representable函子的Product (对)通过原始索引的总和(Either)进行索引. Either的分支告诉我们要在产品的哪个部分进行索引.

  • 一个明显的遗漏(因为它通常不是真的):没有(Representable f, Representable g) => Representable (Sum f g)实例.

I am looking to understand what does Representable stand for in Haskell. The definition

Representable endofunctors over the category of Haskell types are isomorphic to the reader monad and so inherit a very large number of properties for free.

is not clear enough to me. I would like to take a look at a real example, to understand how can I use tabulate and index methods.

So, my question is:

  • What is Representable used for?
  • Can you clarify the definition and provide an example, please?

解决方案

Representables are containter-like functors that have a "special relationship" with another type that serves as an index into the Representable. In the Haskell definition, this index type is given by the associated type family type Rep f :: *

For every value of the index and for every value of the Representable, we can call the index :: f a -> Rep f -> a function to get the corresponding element. And tabulate :: (Rep f -> a) -> f a constructs a container where each element is derived from its own index.

Now, here is an example of a functor that is NOT representable: the typical Haskell list type []. One might naively think that it can be indexed by something like a Natural, but the problem is that lists can be empty, or not have enough elements to reach a given index.

An always-infinite type like data Stream a = Stream a (Stream a) is Representable and it's indexed by Natural, because there will always be a value for any given Natural that we pass to index.

Similarly, the homogeneous pair data Pair a = Pair a a is indexed by the type Bool: the index tells us which of the components to select.

If we get dependent-ish, fixed-size vectors are Representable and are indexed by finite naturals bounded by the size of the vector. They aren't indexed by unbounded Naturals because then we could have an out-of-bounds access!


Reading the various instances defined for Representable is instructive, but it seems we have to get down to the source code because the associated types are not visible in the Haddocks. Some interesting tidbits:

  • The Identity functor is indexed by the unit type (), this makes sense because Identity only has one "slot" so to speak, so we don't need to provide any information.

  • The "functions from some type" type ((->) e) is indexed by the source type itself. And index is simply id. This is what's meant by "isomorphic to the reader monad", because the Reader e monad is just a newtype over ((->) e).

  • The Composition (nesting) of two representable functors is again Representable, and it is indexed by the pair of the original indexes! This makes sense: first we must know how to index into the outer functor, and then into the inner one.

  • The Product (pairing) of two Representable functors is indexed by the sum (Either) of the original indexes. The branch of the Either tells us in which part of the product to index.

  • A notable omission (because it is not true in general): there is no (Representable f, Representable g) => Representable (Sum f g) instance.

这篇关于在Haskell中,Representable有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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