我最近一直在尝试使用Monoid和分销剂,我认为我发现了一些有趣的东西(在答案中描述了) - 这些已经是已知的结构吗? (我一直无法在线找到对它们的任何参考,但我不认为我错过了一些原因,它们会毫无意义
)给任何不是我的人?
引导我这里的问题是:
- 如果您将产品交换为comonoids中的总和会发生什么?
- 如果您将产品交换为共同函数的总和(共同涂抹剂,煤层,代码可见,编码)会发生什么? (仅在前两个回答)
I've been experimenting with monoids and Distributives lately, and I think I've found something of interest (described in my answer) - are these already known structures? (I've been unable to find any reference to them online, and I don't think I've missed some reason they would be nonsensical)
If not previously known, do they seem useful, or interesting to anyone who isn't me?
The questions that lead me here are:
- What happens if you swap products for sums in comonoids?
- What happens if you swap products for sums in comonoidal functors (coapplicatives, coalternatives, codivisibles, codecidables)? (Only somewhat answered for the first two)
发布评论
评论(2)
以下是这些结构背后的定律,因为它们是最新的实验产物。
首先,Cocartesian comagma,以及身份:
这些是具有固有分支能力的数据类型 - 成为合适的comonoid,此分支不应改变其值(由于功能的整洁Typeclass实例),但这会导致较小的有趣的类型用于此处描述的目的。
以下是函数的实例,与OP的单次征用实例相对应:
不幸的是,除非合法分裂,否则这不会是关联的 - 但是我认为它的非社交形式仍然可以使用吗?
也可以定义一个可展开的类型类似于单型的可折叠类型,半群的可折叠1及其理论上的进一步家庭成员:
下一:我认为是“ cocartesian cocatesian colternative functor”:
用于代数数据类型每个构造函数中的一个值(然后可以观察并匹配模式)。
可匹配的描述每个构造函数中具有一个值的函子(因此,无人居住的值会导致无人居住的函子)。
我认为可匹配的严格较弱比与函子而不是应用程序遍历的可遍历的类型类似,但尚未证明这一点 - 这与所有适用的分发剂相对应。 (每个构造函数中完全具有一个参数值的代数数据类型既可以匹配又可以遍历。)
对我来说,Motalables似乎很有趣,是我无法找到任何参考的扩展应用家庭的一部分,但它们确实会进入他们自己的“分布式”库(遍布双重)的分布函数。
正常的发行剂是
读取器r
的同构,并且是著名的(我是著名的,至少是吗?这似乎是众所周知)等于代表函数,或者在Hask中的右伴随。它们是针对代数数据类型的解释,它们是具有一个构造函数的代数数据类型。但是,这些可以扩展到基于函数的分布之外!
这些镜像可遍历〜
(l(),[a])
,traversable1〜(l(),nonepty a)
和备用的函数 - 函数 - 可转换〜(l(),a)
。(感兴趣:对于代数数据类型,每个可穿越的家庭成员具有与分布式等效物具有构造函数一样多的记录,反之亦然)
(有趣的是:就像共同应用在Hask中是微不足道的一样,我希望可以解释这一点 - 我希望这可以解释作为启用许多分布式和comatchables的记录,可以使许多可遍布的构造函数?)
匹配物也像定义通用实例的应用一样,除了它具有具有独特应用程序实例的应用程序的产品,但实际上是具有多种匹配物的总和一个独特的实例!
Cocartesian共同应用充当替代性的等效物-Cocartesian共同涂抹可以解释为能够在“ Unzip”操作中选择哪一方,而Cocartesian Coapplicative描述了一个完全不habf的函数,如v1中的v1。
总而言之:
这里有4种类型的单体家族(Monoid,Comonoid,Cocartesian Monoid,Cocartesian comonoid),其中第一个和最后一个在Hask中是不个性的。如果您包括这些以及(),也许有6个?
这里有6个应用家庭的成员(应用,替代性,可分割,可决定性,可匹配,有偏见),加上琐碎的共同应用且可梳理 - 可能会进一步填写至16名成员!或36包括上述
可匹配的函子启用较弱的分布式版本 - 如果始终存在分布式数据,则有时仅存在匹配分发中的数据,或者可能永远不会在可匹配的分布
中出现。
(这些适用的=从'semialign''软件包中对齐,但是在Ziplist的类型类实例中看到的与应用程序和拉链之间的关系相对应的法律较少?)
:一个更对称的列表实例
编辑 类似于单质型的类型类似的方法。
但是,进行展开的有用实例将是价值变化的,并且已经为这些价值变化选择了左/右偏置。例如:
因此,不对称实际上是该过程固有的,而所需的一切就是要保持左或右信号的终点,以表明列表状(线性,有限)的端点。
The following is fairly light on the laws behind these structures, as they are a recent product of experimentation.
First, the cocartesian comagma, plus identity:
These are datatypes with an inherent ability to branch - to be a proper comonoid, this branching should not change its value (due to a neat typeclass instance for functions), but that leads to a far less interesting typeclass for the purposes described here.
Here's that instance for functions, corresponding to the monoid-requiring Divisible instance for Op:
This will not be associative unless Split is lawful, unfortunately - but I think its non-associative form could still be of use?
It is also possible to define an Unfoldable typeclass, similar to Foldable for monoids, Foldable1 for semigroups, and their theoretical further family members:
Next: what I think is a "cocartesian coalternative functor":
For algebraic datatypes, Match describes functors with at most one value in each constructor (which can then be observed and pattern-matched over).
Matchable describes functors with exactly one value in each constructor (so an uninhabited value leads to an uninhabited functor).
I believe Matchable is strictly weaker than a Traversable typeclass that traverses with Functors instead of Applicatives, but have not proven this - this would correspond to all Distributives being Applicative. (All algebraic datatypes with exactly one parameter value in each constructor are both Matchable and Traversable.)
Matchables seem interesting to me, being a part of the extended applicative family that I've been unable to find any reference to, but they really come into their own with Distributive functors from the 'distributive' library (the dual of Traversables).
Normal Distributives are isomorphic to
Reader r
for some r, and are famously (I presume famously, at least? It seems well known) equivalent to Representable functors, or right adjoints in Hask. Interpreted for algebraic datatypes, they are the algebraic datatypes with exactly one constructor.These can be extended beyond the Functor-based Distribute, though!
These mirror Traversable ~
(l (), [a])
, Traversable1 ~(l (), NonEmpty a)
, and the much-rarer functor-traversable ~(l (), a)
.(Of interest: for algebraic datatypes, the each Traversable family member has as many records as the Distributive equivalent has constructors, and vice versa)
(Of interest: just as Coapplicatives are trivial in Hask, so are Comatchables - I expect this can be interpreted as Coapplicatives enabling the many records of Distributive, and Comatchables enabling the many constructors of Traversable?)
Matchables also act like Applicatives for defining generic instances, except that while it's products of Applicatives that have a unique Applicative instance, it's actually sums of Matchables that have a unique instance!
Cocartesian Coapplicatives act as the Alternative equivalent - Cocartesian Coapply can be interpreted as being able to choose which side to take in an 'unzip' operation, and Cocartesian Coapplicative describes a totally uninhabited functor like V1 in generics.
In summary:
There are 4 types of monoid family here (monoid, comonoid, cocartesian monoid, cocartesian comonoid), of which the first and last are non-trivial in Hask. Maybe there are 6 if you include These as well as (,) and Either?
There are 6 members of the applicative family here (applicative, alternative, divisible, decidable, matchable, biasable), plus the trivial coapplicative and comatchable - this could presumably be filled out further to a total of 16 members! Or 36 including These as above
Matchable functors enable weaker versions of Distributive - where Distributive's data is always present, the data in a Match-Distributive is only sometimes present, or potentially never present in a Matchable-Distributive
(These-wise applicative = Align from the 'semialign' package, but with fewer laws? corresponding to the relationship between applicatives and zips, seen in ZipList's typeclass instances?)
Edit: A more symmetric Unfoldable instance for lists
It is possible to leave the choice to bias left or right until after-the-fact, using a similar method to the Monofoldable typeclass.
However, useful instances of Split for unfolding will be value-changing, and will have already chosen a left/right bias for these value changes. For example:
As such, the asymmetry is practically inherent to the process, and all that is needed is to be consistent about whether Left or Right signal an end-point for listlike (linear, finite) unfolds.
在这个答案中(理想情况下,我应该在这个问题第一周年之前写过一段时间,但可惜),我将主要坚持协变量的单型函数,以保持范围可管理,尽管有机会考虑一些范围问题的其他方面和您的自我答复。
我的另一种社论选择是在使用“共同”命名事物时行使约束。这不仅是因为当您注意到的是16个假设的单体函数类的基线时,很容易在前缀汤中迷失,而且还因为对于每个人来说,通常都有一个以上可见的候选人。 - “绰号。例如,请考虑
应用
(投入书籍单函数签名):一个人可能希望采用其直接的“共同应用”其直接 oplax monoidal 通过从哈斯克切换到hask op (从而绕过方法的箭头, )在将
(,)
保留为张量产品时:这些组合者对每个
fuctrotor
都有合法的实现,并使用unzip = fmap = fmap fst&& fmap snd
和trivialu = const()
。值得注意的是,这是分配文档当它注意到:除了
unzip
的微不足道外,称其为共同应用的另一个合理异议是,应用程序的彻底双重化还应替换(,)
(带有的产品)
(Hask Op 中的产品)。这样做会导致您的可匹配的
类,我将在此处显示在此处显示,该演示文稿是从 Conor McBride :(值得注意的是,从协变量转换为contravariant functors时会出现类似的问题。在替换
在
funct> functor
contravariant应用>应用
的签名中,会导致可划分
,如果张量产品也以兼容的方式对双重化,我们最终以decidiDable添加此外 和单二重奏博客文章,
decidable
镜像应用程序
比difsible
。)>可划分 看起来只有符合一个值的函数可以是
决定性
,这只有在我们需要orwell
作为同构时才保持。特别是,正如McBride的亮点一样,可以制作任何comoNAD(或实际上,任何具有extract
的作用的函子)可以成为decisive
:orwellw
>将提取
的结果用作默认值,以编辑在此实现中,
callastify
使用用于设置的谓词基于
基于的cosemigroup(换句话说,是您split
的合法实例)。orwellw
将此cosemigroup提升到非空的列表(假设为了保持一致性,p d
是true
)。(顺便说一句,
redact
也想到了如何通过适当的应用程序
实例实现非空列表的填充zip,如证明)
决定性
/可匹配的
,traverable
和分布
,我相信它们归结为所有持有一个值的函数(左HASK内functors或您的ftraversable
)是comOnads,因此decisive
。就这些而言,您提出的不太强大的分布最终将只是ftraversable
(在内部推动左侧伴随,而不是在外部拉出右旁边),现在我看不出如何它可能会推广到决定性
。现在,让我们看一下
替代
。一本书单函数的演示可能是:由于我们需要在一段时间内需要它,因此值得强调的是,我们可以通过举起(< |>) https://stackoverflow.com/a/56200814/2751851"> Trivial
基于基于:
直接的oplax oplax对应
替代恰好是某种东西熟悉:
这实际上等同于 mapMaybe 基于
过滤
class ,详细介绍, <<<< em>每个替代的单子都可以过滤吗? 。特别是,使用redact
以前说明的谓词Cosemigroup举起,直接导致filter
。不过,我们还没有对张量进行双重张紧。这样做会导致您的
偏差
(和有偏见
,但是我放弃了身份以使其居住的类型):举起Trivial
(,)
- 基于基于的共核心给我们:bias
至少提供了形状分类器(无论sibing
是否给出left
或右
取决于其参数的函数形状)以及对组件之间的相关选择。我之所以说“至少”,是因为将身份丢弃偏见
仅使用关联法,并说法律不排除f
shape的更改或重新排列,为只要它们是势力,并保留左
/右
分类。如果
偏见
如此对法律的看法是一个问题,则有一种合理的方法可以将其拧紧。为了提出它,让我们回到应用
和替代
暂时。虽然在单型函子公式中替代
在原则上独立于应用程序
,但有时还有一些其他可能的法律被调用以连接这两个类并更好地描述的含义替代
(有关此的评论,请参见 “替代”类型类及其与其他类型类的关系 )。这些定律之一是(&lt;*&gt;)
(&lt; |&gt;)
的正确分发性:我们可以将其转换为我们在此处使用的词汇...
...并使它变得无点,为了更轻松的操作:
现在,如果
bias
是双重的,alt
(替代
sans sans Identity)和>决定性
是应用
是双重的。采用该法律(和/或类似的左发行法)将禁止
)从更改形状。这是因为,按照
偏见
(并且,按扩展,决定性
的身份定律,orwell
无法更改形状,这特别是:应用分配法,然后才会导致
:
偏见
使形状未触及。因此,分发性定律可以确保有偏见
是一个形状分类器,并在fmap fst
和fmap snd
之间进行选择。所有这些都非常整洁地突出了替代
/alt
和bias
之间的对应关系,还可以在应用程序和
决定性
。In this answer (which I ideally should have written some time before the first anniversary of the question, but alas), I will mostly stick to covariant monoidal functors in order to keep the scope manageable, though there will be opportunity to consider some of the other aspects of the question and of your self-answer.
Another editorial choice of mine will be exercising restraint in using "co-" to name things. That's not just because it's easy to get lost in a soup of prefixes when, as you note, there is a baseline of sixteen hypothetical monoidal functor classes, but also because for each of them there generally is more than one plausible candidate for the "co-" moniker. For instance, consider
Applicative
(cast into a by-the-book monoidal functor signatures):One might want to adopt as "coapplicative" its straightforward oplax monoidal counterpart, obtained by switching from Hask to Haskop (and thus turning around the arrows of the methods) while keeping
(,)
as the tensor product:These combinators have a lawful implementation for every
Functor
, withunzip = fmap fst &&& fmap snd
andtrivialU = const ()
. Notably, this is the coapplicative alluded to by the distributive documentation when it notes that:Besides the triviality of
Unzip
, another plausible objection to calling it coapplicative is that a thorough dualisation of applicative should also replace(,)
(the product in Hask) withEither
(the product in Haskop). Doing so leads to yourMatchable
class, which I'll display here in a presentation borrowed from a post by Conor McBride :(It is worth noting that similar questions arise when switching from covariant to contravariant functors. While replacing
Functor
withContravariant
in the signatures ofApplicative
leads toDivisible
, if the tensor products are also dualised in a compatible way we end up withDecidable
instead. Furthermore, as argued in my Divisible and the monoidal quartet blog post,Decidable
mirrorsApplicative
more closely thanDivisible
.)While at first it might look like only functors holding exactly one value can be
Decisive
, that only holds if we requireorwell
to be an isomorphism. In particular, as McBride highlights, any comonad (or really, any functor with something to play the role ofextract
) can be madeDecisive
:orwellW
uses the result ofextract
as a default value, in order to redact one of theEither
cases. For demonstrative purposes, here is how it might be deployed in implementing a filter-resembling operation which, instead of merely removing rejected, replaces them with a default:In this implementation,
classify
uses the predicate to set up anEither
-based cosemigroup (in other words, a lawful instance of yourSplit
).orwellW
lifts this cosemigroup to non-empty lists (assuming that, for the sake of consistency,p d
isTrue
).(
redact
, by the way, also brings to mind how a padding zip can be implemented through a suitableApplicative
instance for non-empty lists, as demonstrated in this answer, incidentally also written by McBride, though at the moment I'm not sure about how deep the relationship really is.)On the connections between
Decisive
/Matchable
,Traversable
andDistributive
, I believe they boil down to all functors holding exactly one value (the left adjoint Hask endofunctors, or yourFTraversable
) being comonads, and thusDecisive
. As far as these are concerned, the less powerful distributive you propose would ultimately be justFTraversable
(pushing a left adjoint inside, rather than pulling a right adjoint outside), and right now I don't see how it might generalise toDecisive
.Now let's look at
Alternative
. A by-the-book monoidal functor presentation might be:Since we'll need it in a little while, it's worth emphasising we can recover
(<|>)
by lifting the trivialEither
-based monoid:The straightforward oplax counterpart to
Alternative
happens to be something familiar:This is actually equivalent to the familiar,
mapMaybe
-basedFilterable
class, as covered in detail by Is every Alternative Monad Filterable?. In particular, using the predicate cosemigroup lifting illustrated before withredact
leads directly tofilter
.We haven't dualised the tensors yet, though. Doing so leads to your
Bias
(andBiasable
, but I'm giving up on the identity in order to have inhabited types):Lifting the trivial
(,)
-based comonoid gives us:Bias
offers, at least, a classifier of shapes (whetherbiasing
gives outLeft
orRight
depends on the functorial shape of its argument), plus an associated choice between pair components. I say "at least" because dropping the identity leavesBias
with just an associativity law, and said law doesn't rule out changes or rearrangements of thef
-shape, as long as they are idempotent and preserve theLeft
/Right
classification.If
Bias
being so light on laws is deemed a problem, there is one plausible way of tightening it up somewhat. In order to present it, let's go back toApplicative
andAlternative
for a moment. While in the monoidal functor formulationAlternative
is in principle independent fromApplicative
, there are a few other possible laws that are sometimes invoked to connect the two classes and better delineate the meaning ofAlternative
(for commentary on that, see Antal Spector-Zabusky's answer to Confused by the meaning of the 'Alternative' type class and its relationship to other type classes). One of these laws is right distributivity of(<*>)
over(<|>)
:We can translate that to the vocabulary we're using here...
... and make it pointfree, for the sake of easier manipulation:
Now, if
Bias
is dual toAlt
(Alternative
sans identity) andDecisive
is dual toApplicative
, we should be able to dualise the distributivity law for a functor which is bothDecisive
andBias
:Adopting this law (and/or the analogous left distributivity law) would forbid
biasing
(and, by extension,biased
) from changing the shape. That's because, by the identity laws ofDecisive
,orwell
can't change shapes, which means, in particular:Applying the distributive law then leads to:
Which is only possible if
biasing
leaves the shape untouched. The distributivity law, therefore, can ensurebiased
is a shape classifier with bundled with a choice betweenfmap fst
andfmap snd
. That it all works out so neatly highlights the correspondence not just betweenAlternative
/Alt
andBias
, but also betweenApplicative
andDecisive
.