Finding the Missing 'Reduce' Typeclass from Finger Tree Article

ぐ巨炮叔叔 提交于 2019-12-22 04:24:31

问题


Yesterday's Wikibender that started with this stackoverflow qestion on Comonads ended up at MarkCC's article on Finger Trees.

In the article he makes extensive use of the Reduce type class. He writes about this typeclass as if it is a very common and frequently use library, but I cannot find it on hackage, nor can I find enough documentation to really understand the code.

Can someone help me understand what the Reduce typeclass is doing, how the (-<) and (>-) operators work, and what should tell me about the code in the article (copied below)?


Code Listing from Finger Trees Done Right (I hope):

Listing 1: instance declaration for Node

instance Reduce Node where
  reducer (-<) (Node2 a b) z = a -< (b -< z)
  reducer (-<) (Node3 a b c) z = a -< (b -< (c -< z))
  reducer (>-) (Node2 b a) = (z >- b) >- a
  reducer (>-) (Node3 c b a) = ((z >- c) >- b) >- a

Listing 2: instance declaration for FingerTree

instance Reduce FingerTree where
  reducer (-<) Empty zero = zero
  reducer (-<) (Single x) zero = x -< zero
  reducer (-<) Deep left mid right zero = left -<' (mid -<'' (right -<' zero))
    where (-<') = reducer (-<)
          (-<'') = reducer (reducer (-<))
  reducel (>-) zero Empty = zero
  reducel (>-) zero (Single x) = zero >- x
  reducel (>-) zero (Deep left mid right) = ((zero >-' left) >-'' mid) >-' right
    where (>-') = reducel (>-)
          (>-'') = reducel (reducel (>-))

listing 3: data types

data Node s = Node2 s s | Node3 s s s

data FingerTree a = Empty
  |  Single a
  | Deep (Digit a) (FingerTree (Node a)) (Digit a)

data Digit a = [ a ]

回答1:


Given that reduce is a common alternate name for a "fold" function, I'd guess that it's something similar to the Foldable type class. The instance definitions seem to make sense as such, as well.

The Foldable class can be defined using just foldr, which has the type signature foldr :: (Foldable t) => (a -> b -> b) -> b -> t a -> b, whereas the reducer in that code appears to be reducer :: (Reduce t) => (a -> b -> b) -> t a -> b -> b. Other than a different argument order, it should work the same.

Note that the operators are just arguments to the function--you could replace them all with f or another similarly generic identifier. Using an operator as the name of a binary function argument is... a slightly unusual choice, but it does emphasize some aspects of the structure of the fold, I guess.




回答2:


It's defined in the paper linked in the article: Finger Trees: A Simple General-purpose Data Structure.

class Reduce f where
  reducer :: (a -> b -> b) -> (f a -> b -> b)
  reducel :: (b -> a -> b) -> (b -> f a -> b)


来源:https://stackoverflow.com/questions/8450448/finding-the-missing-reduce-typeclass-from-finger-tree-article

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!