为什么从根本上讲,遍历遍历定义在应用程序上?

发布于 2025-01-27 11:13:05 字数 1546 浏览 2 评论 0原文

最近,我一直在“将所有内容蒸馏到其基本面”中,而且我一直找不到明确的理论原因来定义可穿越的类型类型,只有实用性的,”在适用的山地上,许多数据类型都可以做到这一点,并有很多提示。

我知道有一个适用的“家庭”,如 https://duplode.github.io/posts/divisible-and-the-monoidal-quartet.html

我还知道,虽然可遍历遍历是适用的膜结构,但“半群”中的遍历1个类型术语描述了应用煤膜,而“分布式”中的分布式类型描述了函数代数。

此外,我知道可折叠,可折叠1和理论上的折叠家庭成员可以描述可以使用单体,半群和相应的单型家庭成员(例如岩浆(作为二进制树折叠)和每种折叠版本(用于折叠)的数据类型,每种杂物(用于折叠)(用于折叠为每个版本的无序版本)。

因此,由于可遍历是可折叠的子类,因此我认为它本质上是单的,同样,我认为traversable1本质上是半群,并且分布式本质上是共同体(如其在“分布式”软件包中的描述中提到的)。

这感觉就像是正确的轨道,但是应用和申请来自这里?是否有岩浆和可交换版本?类别中有一个非平凡性共素的类别的分布式家庭吗?

是“这些类型层是否存在,它们是什么?如果没有?

class FoldableMagma t => TraversableMagma t where
    traverseMagma :: ??? f => (a -> f b) -> (t a -> f (t b))
class FoldableCommute t => TraversableCommute t where
    traverseCommute :: ??? f => (a -> f b) -> (t a -> f (t b))
class Foldable t => ContraTraversable t where
    contraTraverse :: Divisible f => (b -> f a) -> (t a -> f (t b))
-- im really not sure on this last one
-- but it's how i'd expect an endofunctor over coalgebras to look
-- which seems potentially related to traversables?

本质上,我的问题 ”> https://hackage.haskage.haskage.org/package/package/data-functor-logistic

href =“ https://hackage.haskell.org/package/package/data-functor-logistic” rel =“ nofollow noreferrer 描述了偶性函数的分布式版本 - 是否可以在除外分(或决策)上等效地遍历?

I've been on a bit of a "distilling everything to its fundamentals" kick lately, and I've been unable to find clear theoretical reasons for how the Traversable typeclass is defined, only practical ones of "it's useful to be able to traverse over applicative coalgebras, and lots of datatypes can do it" and a whole lot of hints.

I'm aware that there's an applicative "family", as described by https://duplode.github.io/posts/divisible-and-the-monoidal-quartet.html.

I'm also aware that while Traversable traversals are applicative coalgebras, the Traversable1 typeclass from 'semigroupoids' describes apply coalgebras, and the Distributive typeclass from 'distributive' describes functor algebras.

Additionally, I'm aware that Foldable, Foldable1, and theoretical fold family members, describe datatypes that can be folded using monoids, semigroups, and corresponding monoid family members such as magmas (for folding as a binary tree) and commutative versions of each (for folding as unordered versions of each).

As such, as Traversable is a subclass of Foldable, I assume it's monoidal in nature, and similarly I assume Traversable1 is semigroupal in nature, and Distributive is comonoidal in nature (as mentioned in its description in the 'distributive' package).

This feels like the right track, but where do Applicative and Apply come from here? Are there magmatic and commutative versions? Would there be a distributive family in a category with non-trivial comonoids?

Essentially, my question is "do these typeclasses exist, and what are they? if not, why not?":

class FoldableMagma t => TraversableMagma t where
    traverseMagma :: ??? f => (a -> f b) -> (t a -> f (t b))
class FoldableCommute t => TraversableCommute t where
    traverseCommute :: ??? f => (a -> f b) -> (t a -> f (t b))
class Foldable t => ContraTraversable t where
    contraTraverse :: Divisible f => (b -> f a) -> (t a -> f (t b))
-- im really not sure on this last one
-- but it's how i'd expect an endofunctor over coalgebras to look
-- which seems potentially related to traversables?

Presumably less important bonus question: while attempting to research this, I came across the 'data-functor-logistic' package https://hackage.haskell.org/package/data-functor-logistic

This describes a version of Distributive over contravariant functors - is there an equivalent Traversable over Divisibles (or Decidables)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

檐上三寸雪 2025-02-03 11:13:05

我不知道有任何实现这些课程的库,但是我会尝试揭开这些课程所代表的内容。我是程序员,而不是类别理论家,因此请与一粒盐一起服用。

应用变体

applymagma

applymagma类具有与apply类完全相同的方法,但不需要遵循协会法。

class Functor f => ApplyMagma f where
    (<.>) :: f (a -> b) -> f a -> f b

如果应用类似于半群,则applymagma类似于岩浆。

applyCommute

applyCommute类将等同于申请类,但具有以下通勤定律:

f <
gt; x <.> y = flip f <
gt; y <.> x

如果apply apply> applycommute 类似于类似的交换性半群。

traversable1变体

traversable1magma

a traversable1magma可以看作是traversable1,提供了有关结构的更多信息。 折叠1类具有tononEmpty方法,而折叠1MAGMA类可以具有tobinarytree方法。

class (FoldableMagma t, Traversable1 t) => Traversable1Magma t where
    traverseMagma :: ApplyMagma f => (a -> f b) -> (t a -> f (t b))

traversable1 -commute

a traversable1commute可以看作是traverable1,而无需定义的订购元素。如果它不需要ORD A约束,则set来自容器可能是此类的一个实例。 traversable1commute可能是遍历1的超级类别。

class (FoldableCommute t, Functor t) => Traversable1Commute t where
    traverseCommute :: ApplyCommute f => (a -> f b) -> (t a -> f (t b))

请注意,这些是traversable1的变体,因为applymagma not applyCommute 具有等同于pure的函数。

Contratrableable

Contratraversable没有任何实例。要查看原因,请查看contratraverse函数的类型。

contraTraverse :: Divisible f => (b -> f a) -> (t a -> f (t b))

我们可以对以下内容进行专业化:

contraTraverse :: Monoid b => (b -> Op b a) -> (t a -> Op b (t b))

这是以下内容:

contraTraverse ~ Monoid b => (b -> a -> b) -> t a -> t b -> a

使用constconquer conquer 函数,这使我们能够创建任何类型的值,是不可能的。

I'm not aware of any library that implements those classes, but I'll try to unravel what those classes would represent. I am a programmer, not a category theorist, so take this with a grain of salt.

Applicative variants

ApplyMagma

The ApplyMagma class has exactly the same methods as the Apply class, but it doesn't need to follow the associativity law.

class Functor f => ApplyMagma f where
    (<.>) :: f (a -> b) -> f a -> f b

If Apply is analogous to semigroups, ApplyMagma is analogous to magmas.

ApplyCommute

The ApplyCommute class will be equivalent to the Apply class but with the following commutativity law:

f <
gt; x <.> y = flip f <
gt; y <.> x

If Apply is analogous to semigroups, ApplyCommute is analogous to commutative semigroups.

Traversable1 variants

Traversable1Magma

A Traversable1Magma can be seen as a Traversable1 with more information provided about the structure. While the Foldable1 class has a toNonEmpty method, The Foldable1Magma class could have a toBinaryTree method.

class (FoldableMagma t, Traversable1 t) => Traversable1Magma t where
    traverseMagma :: ApplyMagma f => (a -> f b) -> (t a -> f (t b))

Traversable1Commute

A Traversable1Commute can be seen as a Traversable1 without a defined ordering to the elements. If it didn't require an Ord a constraint, Set from containers could be an instance of this class. Traversable1Commute could be a superclass of Traversable1.

class (FoldableCommute t, Functor t) => Traversable1Commute t where
    traverseCommute :: ApplyCommute f => (a -> f b) -> (t a -> f (t b))

Note that these are variants of Traversable1 because neither ApplyMagma nor ApplyCommute have a function equivalent to pure.

ContraTraversable

ContraTraversable does not have any instances. To see why, look at the type of the contraTraverse function.

contraTraverse :: Divisible f => (b -> f a) -> (t a -> f (t b))

We can specialize this to the following:

contraTraverse :: Monoid b => (b -> Op b a) -> (t a -> Op b (t b))

Which is eqivalent to the following:

contraTraverse ~ Monoid b => (b -> a -> b) -> t a -> t b -> a

Using const and the conquer function from Divisible, this allows us to create a value of any type, which is impossible.

格子衫的從容 2025-02-03 11:13:05

自从问这个并收到以前的(出色的)答案以来,我已经了解了使用应用程序的另一个原因:代数数据类型!

如果没有任何约束,它只能描述无人居住的数据类型,例如:

data V1 a
instance VTraversable V1 where
    vtraverse _ = \case
-- uninhabited types can be pattern matched to create any result type

如果它是函子,它可以描述看起来像这样的代数数据类型:

data FTrav1 a = FTrav1 a
instance FTraversable FTrav1 where
    ftraverse strat (FTrav1 a) = FTrav1 <
gt; strat a

data FTrav2 a = FTrav2_1 a | FTrav2_2 (FTrav1 a)
instance FTraversable FTrav2 where
    ftraverse strat (FTrav2_1 a) = FTrav2_1 <
gt; strat a
    ftraverse strat (FTrav2_2 fa) = FTrav2_2 <
gt; ftraverse strat fa

本质上,它是任何具有任意的(可能是无限的,如果在Haskell中描述的)的数据类型单个ftraversable参数的构造函数(其中a身份a)。这就是说,任何可穿越的f a都是(f(f(),a),作者函数的同构。

引入应用程序启用额外的数据类型如下:

data ApplyTrav1 a = ApplyTrav1 a a
instance Traversable1 ApplyTrav1 where
    traverse1 strat (ApplyTrav1 a a) = ApplyTrav1 <
gt; strat a <*> strat a

data ApplyTrav2 a = ApplyTrav2_1 a (ApplyTrav1 a) | ApplyTrav2_2 (ApplyTrav1 a) a
instance Traversable1 ApplyTrav2 where
    traverse1 strat (ApplyTrav2_1 a fa) = ApplyTrav2_1 <
gt; strat a <*> traverse1 strat fa
    traverse1 strat (ApplyTrav2_2 fa a) = ApplyTrav2_2 <
gt; traverse1 strat fa <*> traverse1 strat a

现在,只要它的有限数量大于零!同构现在是(f(),nonepty a),它们的大小相等。

应用启用以下内容:

data ApplicTrav a = ApplicTrav0 | ApplicTrav a a
instance Traversable ApplicTrav where
    traverse _ ApplicTrav0 = pure ApplicTrav0
    traverse strat (ApplicTrav a a) = ApplicTrav <
gt; strat a <*> strat a

现在允许空构造函数!同构现在为(f(),[a])

假设的交换性应用程序将用于交换代数数据类型,如果它们是一件事情 - 也许,如果设置仅与交换性单体可折叠,它们将是相关的!但是据我所知,通勤数据类型并不是Haskell的核心部分,因此这种形式的遍历不会显示代数数据类型。

分布式相似,它用任意许多记录(潜在无限)的单个构造函数描述函子。

据我所知,逻辑与这种代数解释无关 - 由于读取器函数通常与Distribunives一起用于创建Getter函数的集合,因此Logistic旨在与OP Contravariant functor合作,以创建Setter函数的集合。

这向我表明,由于作者函数的特征是其与(a,r)是同构的,因此,由于作者函数的表征是其相反的((r,a))。

Since asking this and receiving the previous (excellent) answer, I have learnt another reason why Applicative is used: algebraic datatypes!

Without any constraint, it can only describe uninhabited datatypes, like this:

data V1 a
instance VTraversable V1 where
    vtraverse _ = \case
-- uninhabited types can be pattern matched to create any result type

If it were Functor, it could describe algebraic datatypes that look something like these:

data FTrav1 a = FTrav1 a
instance FTraversable FTrav1 where
    ftraverse strat (FTrav1 a) = FTrav1 <
gt; strat a

data FTrav2 a = FTrav2_1 a | FTrav2_2 (FTrav1 a)
instance FTraversable FTrav2 where
    ftraverse strat (FTrav2_1 a) = FTrav2_1 <
gt; strat a
    ftraverse strat (FTrav2_2 fa) = FTrav2_2 <
gt; ftraverse strat fa

Essentially, it's any datatype with an arbitrary (potentially infinite, if that were describable in Haskell) number of constructors of a single FTraversable argument (where a ~ Identity a). This is saying that any Traversable f a is isomorphic to (f (), a), the Writer functor.

Introducing Apply enables extra datatypes like the following:

data ApplyTrav1 a = ApplyTrav1 a a
instance Traversable1 ApplyTrav1 where
    traverse1 strat (ApplyTrav1 a a) = ApplyTrav1 <
gt; strat a <*> strat a

data ApplyTrav2 a = ApplyTrav2_1 a (ApplyTrav1 a) | ApplyTrav2_2 (ApplyTrav1 a) a
instance Traversable1 ApplyTrav2 where
    traverse1 strat (ApplyTrav2_1 a fa) = ApplyTrav2_1 <
gt; strat a <*> traverse1 strat fa
    traverse1 strat (ApplyTrav2_2 fa a) = ApplyTrav2_2 <
gt; traverse1 strat fa <*> traverse1 strat a

Now constructors can have arbitrarily many arguments, as long as it's a finite number greater than zero! The isomorphism is now to (f (), NonEmpty a), where they are of equal sizes.

Applicative enables the following:

data ApplicTrav a = ApplicTrav0 | ApplicTrav a a
instance Traversable ApplicTrav where
    traverse _ ApplicTrav0 = pure ApplicTrav0
    traverse strat (ApplicTrav a a) = ApplicTrav <
gt; strat a <*> strat a

Now empty constructors are allowed! The isomorphism is now to (f (), [a]).

A hypothetical commutative Applicative would be used for commutative algebraic datatypes, were they a thing - perhaps, if Set enforced that it were only foldable with commutative monoids, they would be relevant! But to my knowledge, commutative datatypes are not a core part of Haskell, and so this form of traversal will not show up for algebraic datatypes.

Distributive is similar, it describes functors with a single constructor of arbitrarily many records (potentially infinite).

Logistic is unrelated to this algebraic interpretation, to my knowledge - since the Reader functor is commonly used with Distributives to create a collection of getter functions, Logistic is designed to work with the Op contravariant functor to create a collection of setter functions.

This suggests to me that the equivalent for Traversable doesn't exist, due to the Writer functor that characterises Traversables being its own opposite ((r,a) is isomorphic to (a,r)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文