[Haskell-cafe] Functors and the Visitor Pattern

Johan Tibell johan.tibell at gmail.com
Thu Jun 4 03:55:11 EDT 2009


On Thu, Jun 4, 2009 at 9:13 AM, wren ng thornton <wren at freegeek.org> wrote:

>  The Visitor pattern isn't a functor, it's a collection of things. The type
> being visited is the functor[1], the set of methods on that type for
> accepting a visitor is a catamorphism[2], and the visitor itself is an
> algebra for the functor[3].
>
>
> [1] Or rather, the coproduct of all related classes that can chain a
> visitor forms a type, and that type is a functor.
>
>
> [2] For the recursive Visitor pattern I use most often, that is. For the
> non-recursive version it's usually fmap. This is the part where the pattern
> gets a bit shaky because there are actually many different patterns all
> called "Visitor". The main points of interest are whether it's recursive or
> not, and whether it applies the visitor to itself, to its children, or both.
>
> non-recursive + itself == ($)
>
> non-recursive + children == fmap (under open-recursion interpretation of
> the type, aka all nodes are elements)
>
> recursive + children == fmap (under closed-recursion interpretation, aka
> only fringe nodes are elements)
>
> recursive + both == cata (usually, though it depends how you aggregate)
>
> recursive + itself == This is actually a variant of the Iterator pattern


Could you be so kind to give an example for each?

Cheers,

Johan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090604/e0d6257b/attachment.html


More information about the Haskell-Cafe mailing list