开发者

Data Structure Differentiation, Intuition Building

开发者 https://www.devze.com 2023-03-04 03:29 出处:网络
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

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
 开发者_JAVA技巧(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.


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.

data Tree = Leaf | Node Tree Tree

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.

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

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.

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

Here, for example, is a function (untried code) which searches a tree for subtrees passing a given test subtree.

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)

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.


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

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

if we replace any of those 'a', 'b', 'c' etc by a hole (represented as @), we'll get

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,@]
        + ...

Another example, a binary tree is

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

so adding a hole generates the type:

{-

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)

A third example which illustrates the chain rule is the rose tree (variadic tree):

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

the derivative says R' t = List(R t) + t * List'(R t) * R' t, which means

{-

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)

Note 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)

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)

and mutate (plug in the hole) using the context info:

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消