数据结构差异化,直觉构建
根据本文,微分适用于数据结构。
根据这个答案:
微分,数据类型 D 的导数(给出为 D')是具有单个“洞”的 D 结构类型,即不包含任何数据的可区分位置。令人惊讶的是,这满足了与微积分微分相同的规则。
规则是:
1 = 0
X′ = 1
(F + G)′ = F' + G′
(F • G)′ = F • G′ + F′ • G
(F ◦ G)′ = (F′ ◦ G) • G′
参考论文有点太复杂,我无法获得直觉。 这在实践中意味着什么?一个具体的例子就太好了。
According to this paper differentiation works on data structures.
According to this answer:
Differentiation, the derivative of a data type D (given as D') is the type of D-structures with a single “hole”, that is, a distinguished location not containing any data. That amazingly satisfy the same rules as for differentiation in calculus.
The rules are:
1 = 0
X′ = 1
(F + G)′ = F' + G′
(F • G)′ = F • G′ + F′ • G
(F ◦ G)′ = (F′ ◦ G) • G′
The referenced paper is a bit too complex for me to get an intuition.
What does this this mean in practice? A concrete example would be fantastic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
X in an X 的单孔上下文是什么?没有选择:它是 (-),由单位类型表示。
X*X 中的 X 的单孔上下文是什么?它类似于 (-,x2) 或 (x1,-),因此可以用 X+X(或 2*X,如果您愿意的话)表示。
X*X*X 中的 X 的单孔上下文是什么?它类似于 (-,x2,x3) 或 (x1,-,x3) 或 (x1,x2,-),由 X*X + X*X + X*X 表示,或 (3*X^2,如果你喜欢)。
更一般地,具有孔的F*G是具有孔的F和完整的G,或者完整的F和具有孔的G。
递归数据类型通常定义为多项式的不动点。
实际上是在说 Tree = 1 + Tree*Tree。对多项式进行微分可以告诉您直接子树的上下文:叶子中没有子树;在节点中,它要么是左边的洞,右边的树,或者是左边的树,右边的洞。
这就是多项式微分并呈现为一种类型。因此,树中子树的上下文是这些树步骤的列表。
例如,这里是一个函数(未尝试的代码),它在树中搜索通过给定测试子树的子树。
“下面”的作用是生成与给定树相关的树的居民。
区分数据类型是使“搜索”等程序变得通用的好方法。
What's a one hole context for an X in an X? There's no choice: it's (-), representable by the unit type.
What's a one hole context for an X in an X*X? It's something like (-,x2) or (x1,-), so it's representable by X+X (or 2*X, if you like).
What's a one hole context for an X in an X*X*X? It's something like (-,x2,x3) or (x1,-,x3) or (x1,x2,-), representable by X*X + X*X + X*X, or (3*X^2, if you like).
More generally, an F*G with a hole is either an F with a hole and a G intact, or an F intact and a G with a hole.
Recursive datatypes are often defined as fixpoints of polynomials.
is really saying Tree = 1 + Tree*Tree. Differentiating the polynomial tells you the contexts for immediate subtrees: no subtrees in a Leaf; in a Node, it's either hole on the left, tree on the right, or tree on the left, hole on the right.
That's the polynomial differentiated and rendered as a type. A context for a subtree in a tree is thus a list of those Tree' steps.
Here, for example, is a function (untried code) which searches a tree for subtrees passing a given test subtree.
The role of "below" is to generate the inhabitants of Tree' relevant to the given Tree.
Differentiation of datatypes is a good way make programs like "search" generic.
我的解释是,T 的导数(拉链)是类似于 T 的“形状”的所有实例的类型,但恰好有 1 个元素被“洞”替换。
例如,列表是
如果我们用一个洞(表示为
@
)替换其中的任何'a','b','c'等,我们将 另一个例子,二叉树是
这样添加一个洞生成类型:
说明链式规则的第三个例子是玫瑰树(可变树):
导数说
R' t = 列表(R t) + t * List'(R t) * R' t
,这意味着注意
data List' t = LHit [t] | LTail t(列表)
。(这些可能与传统类型不同,其中拉链是“方向”列表,但它们是同构的。)
导数是记录如何对结构中的位置进行编码的系统方法,例如我们可以使用以下命令进行搜索:(没有完全优化)
并使用上下文信息进行变异(插入漏洞):
My interpretation is that, the derivative (zipper) of T is the type of all instances that resembles the "shape" of T, but with exactly 1 element replaced by a "hole".
For instance, a list is
if we replace any of those 'a', 'b', 'c' etc by a hole (represented as
@
), we'll getAnother example, a binary tree is
so adding a hole generates the type:
A third example which illustrates the chain rule is the rose tree (variadic tree):
the derivative says
R' t = List(R t) + t * List'(R t) * R' t
, which meansNote that
data List' t = LHit [t] | LTail t (List' t)
.(These may be different from the conventional types where a zipper is a list of "directions", but they are isomorphic.)
The derivative is a systematic way to record how to encode a location in a structure, e.g. we can search with: (not quite optimized)
and mutate (plug in the hole) using the context info: