Fwd: [Haskell-cafe] Data.Tree.Zipper in the standard libraries

Iavor Diatchki iavor.diatchki at gmail.com
Sat Jun 7 14:05:56 EDT 2008

Hi Ian,
Thanks for discussing the actual Zipper!

On Wed, Jun 4, 2008 at 2:36 PM, Ian Lynagh <igloo at earth.li> wrote:
> On Wed, Jun 04, 2008 at 11:11:58PM +0200, apfelmus wrote:
>> >    Data.Tree.Zipper> toTree (Loc (Node 'a' []) [Node 'b' []] [Node 'c'
>> >    []] [])
>> >    Node {rootLabel = 'a', subForest = []}
>> >This feels a little scary to me.
> I should expand on that: I wonder if toTree should return
>    Maybe (Tree a)
> which returns Nothing if either sibling is non-empty.

The idea is that "toTree" computes the tree that contains the current
location.  It makes no calims about this being the only tree in the
forest.  I think it is useful to have such a function.  The function
"toForest" computes the entire forest.  You can always get the "Maybe"
version by examining the result of "toForest", but if you think that
it would be useful, we could also add another function to do it
(perhaps it belongs to Tree: forestToTree?)

> It would actually simplify the datastructure; rather than
> we could have just
>    data TreeLoc a  = Loc
>      { tree    :: Tree a       -- ^ The currently selected tree.
>      , parents :: [(Forest a, a, Forest a)]
>          -- ^ The contexts of the parents for this location.
>      } deriving (Read,Show,Eq)
> where the Forests are now children of the parent nodes, rather than
> their siblings. The cost is that you now can't represent forests nicely.

Yeah, this is nice. I think that it would be good to support working
with forests though.

While we are on questions of API, currently the insertion functions of
the Zipper make the inserted tree be the current tree.  It is also
easy to have functions that insert to the left or to the right but
don't change the current tree.  What do you think?


More information about the Libraries mailing list