【问题标题】:Data Structure Differentiation, Intuition Building数据结构差异化,直觉构建
【发布时间】:2011-08-20 15:58:23
【问题描述】:

根据this paper 区分适用于数据结构。

根据this answer:

微分,数据类型D的导数(记为D')是具有单个“洞”的D-结构的类型,即不包含任何数据的有区别的位置。这惊人地满足了与微积分中的微分相同的规则。

规则是:

 1 = 0
 X′ = 1
 (F + G)′ = F' + G′
 (F • G)′ = F • G′ + F′ • G
 (F ◦ G)′ = (F′ ◦ G) • G′

引用的论文对我来说有点太复杂了,无法获得直觉。 这在实践中意味着什么?一个具体的例子会很棒。

【问题讨论】:

  • 在实践中,这意味着您可以计算或构造数据类型的导数。 +、• 和 ◦ 是可以用来以更一般的方式描述数据类型的结构。孔可以用于拉链,通过这种方式可以自动找到数据结构中的孔,使其对泛型有用。
  • 我仅使用形式规则在此基础上建立了直觉。 “求和类型中的一个洞”或“产品类型中的一个洞”应该是什么很清楚,所以你会得到数学家所说的微分。然后,你会自动知道你可以做计算导数时所做的所有事情:链式法则、微分逆、偏导数、微分方程(指数?指数函子有没有用处?)、定点方程的微分解(这就是你计算拉链类型的方法)!
  • @Alexandre:e^X = 1 + X + X^2/2 + X^3/3! + ...。你如何划分一个类型? :)
  • @KennyTM: exp 被定义为F'(X) = F(X) 的解(作为方程的“通用”解,无论是初始解还是终结解,就像列表是L(X) = 1 + A L(X) 的解)。我不知道满足它的函子(如果有的话),也不知道它们有什么用处。
  • @Alexandre:如果存在这种类型,它就不是“多项式类型”。 (假设 F(X) ≠ 0)

标签: data-structures haskell algebraic-data-types


【解决方案1】:

我的解释是,T 的导数(拉链)是所有实例的类型,类似于 T 的“形状”,但恰好有 1 个元素被“孔”替换。

例如,一个列表是

List t = 1     []
       + t     [a]
       + t^2   [a,b]
       + t^3   [a,b,c]
       + t^4   [a,b,c,d]
       + ...   [a,b,c,d,...]

如果我们用一个洞(表示为@)替换那些'a'、'b'、'c'等中的任何一个,我们将得到

List' t = 0      empty list doesn't have hole
        + 1      [@]
        + 2*t    [@,b]     or [a,@]
        + 3*t^2  [@,b,c]   or [a,@,c]   or [a,b,@]
        + 4*t^3  [@,b,c,d] or [a,@,c,d] or [a,b,@,d] or [a,b,c,@]
        + ...

另一个例子,二叉树是

data Tree t = TEmpty | TNode t (Tree t) (Tree t)
-- Tree t = 1 + t (Tree t)^2

所以添加一个洞会生成类型:

{-

Tree' t = 0                    empty tree doesn't have hole
        + (Tree X)^2           the root is a hole, followed by 2 normal trees
        + t*(Tree' t)*(Tree t) the left tree has a hole, the right is normal
        + t*(Tree t)*(Tree' t) the left tree is normal, the right has a hole

          @    or      x     or     x    
         / \          / \          / \   
        a   b       @?   b        a   @?
       /\   /\     / \   /\      /\   /\ 
      c  d e  f   @? @? e  f    c  d @? @?
-}

data Tree' t = THit (Tree t) (Tree t)
             | TLeft t (Tree' t) (Tree t)
             | TRight t (Tree t) (Tree' t)

说明链式法则的第三个例子是玫瑰树(可变参数树):

data Rose t = RNode t [Rose t]
-- R t = t*List(R t)

导数是R' t = List(R t) + t * List'(R t) * R' t,意思是

{-

R' t = List (R t)        the root is a hole
     + t                 we have a normal root node,
       * List' (R t)       and a list that has a hole,
       * R' t              and we put a holed rose tree at the list's hole

        x
        |
       [a,b,c,...,p,@?,r,...]
                    |
                   [@?,...]

-}

data Rose' t = RHit [Rose t] | RChild t (List' (Rose t)) (Rose' t)

注意data List' t = LHit [t] | LTail t (List' t)。

(这些可能与拉链是“方向”列表的传统类型不同,但它们是同构的。)


导数是一种记录如何对结构中的位置进行编码的系统方法,例如我们可以搜索:(不是很优化)

locateL :: (t -> Bool) -> [t] -> Maybe (t, List' t)
locateL _ [] = Nothing
locateL f (x:xs) | f x       = Just (x, LHit xs)
                 | otherwise = do
                                  (el, ctx) <- locateL f xs
                                  return (el, LTail x ctx)

locateR :: (t -> Bool) -> Rose t -> Maybe (t, Rose' t)
locateR f (RNode a child)
      | f a       = Just (a, RHit child)
      | otherwise = do 
                      (whichChild, listCtx) <- locateL (isJust . locateR f) child
                      (el, ctx) <- locateR f whichChild
                      return (el, RChild a listCtx ctx)

并使用上下文信息进行变异(插入漏洞):

updateL :: t -> List' t -> [t]
updateL x (LHit xs) = x:xs
updateL x (LTail a ctx) = a : updateL x ctx

updateR :: t -> Rose' t -> Rose t
updateR x (RHit child) = RNode x child
updateR x (RChild a listCtx ctx) = RNode a (updateL (updateR x ctx) listCtx)

【讨论】:

    【解决方案2】:

    X 中的 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。

    递归数据类型通常被定义为多项式的固定点。

    data Tree = Leaf | Node Tree Tree
    

    实际上是在说 Tree = 1 + Tree*Tree。多项式的微分告诉您立即子树的上下文:叶子中没有子树;在一个节点中,它要么是左边的洞,右边的树,要么是左边的树,右边的洞。

    data Tree' = NodeLeft () Tree | NodeRight Tree ()
    

    这是多项式微分并呈现为一种类型。因此,树中子树的上下文是这些树步骤的列表。

    type TreeCtxt = [Tree']
    type TreeZipper = (Tree, TreeCtxt)
    

    例如,这里是一个函数(未尝试过的代码),它在树中搜索通过给定测试子树的子树。

    search :: (Tree -> Bool) -> Tree -> [TreeZipper]
    search p t = go (t, []) where
      go :: TreeZipper -> [TreeZipper]
      go z = here z ++ below z
      here :: TreeZipper -> [TreeZipper]
      here z@(t, _) | p t       = [z]
                    | otherwise = []
      below (Leaf,     _)  = []
      below (Node l r, cs) = go (l, NodeLeft () r : cs) ++ go (r, NodeRight l () : cs)
    

    “下面”的作用是生成与给定树相关的 Tree' 的居民。

    数据类型的区分是制作像“搜索”这样的程序的好方法通用。

    【讨论】:

    • 太棒了。非常直观的解释。我喜欢作者自己回答有关论文的问题。我将寻找在我的工作中应用数据类型导数的地方。
    猜你喜欢
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多