[Haskell-cafe] Re: Zippers

Ryan Ingram ryani.spam at gmail.com
Wed Mar 4 21:00:15 EST 2009

On Wed, Mar 4, 2009 at 12:53 PM, Cristiano Paris <frodo at theshire.org> wrote:
> I'd (and indeed I did) write 'update' as:
> update z x = z { this = this z >> Just x }
> exploiting the '>>' operator's logic. How would this differ from the
> corresponding 'update' in the original Huet's FP? Maybe I don't get
> how my update would impact performances. In both cases the update
> functions leave context unchanged, don't they? If my update function
> have to replicate the context since functional values are immutable,
> doesn't Huet's do the same?
> Thank you for any further comments.

The problem is that your zipper structure is too open, you don't have
any specification of the "zipper laws".

For example, I'd expect that for any zipper z, one of the following holds:

a) moveUp z = Nothing
b) moveUp z >>= moveLeft = Just z
c) moveUp z >>= moveRight = Just z

Here is the problem with your "update":

tree = Fork (Leaf 1) (Leaf 2)
ztree = initZ tree

test = fromJust $ do
    z1 <- moveLeft ztree
    let z2 = update z1 3
    z3 <- moveUp z2
    z4 <- moveLeft z3
    this z4

I'd expect "test" to equal 3, but I believe with your code that it
still equals 1.  As apfelmus said, update needs to completely
re-construct the zipper structure with the tied knot, which defeats
the purpose of using a zipper in the first place.

  -- ryan

More information about the Haskell-Cafe mailing list