zippers: mapping over last breadcrumb
Asked Answered
C

1

6

I've bumped into an issue using zippers and lens. Consider following example:

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
import Control.Lens
import Control.Zipper

data A = AA { _aa :: A }
       | AB { _ab :: B }
       deriving (Show)

data B = B deriving (Show)

makeLenses ''A
makeLenses ''B

main :: IO ()
main = do
    let a = AA $ AB $ B

        z :: Top :>> A
        z = zipper a

        zAA :: Maybe (Top :>> A :>> A)
        zAA = z & within aa

        zAB :: Maybe (Top :>> A :>> B)
        zAB = z & within (aa . ab)
    return ()

As you can see, I can move from Top :>> A either to Top :>> A :>> A and Top :>> A :>> B.

Having ab lens, how can I move from Top :>> A :>> A (zAA) to Top :>> A :>> B (zAB), without using upward - just mapping with lens over last breadcrumb?

Crosspollinate answered 8/1, 2015 at 16:48 Comment(0)
S
2

Basically, you can't.

To change the type of the current focus you'd need to move upwards. The type you'll be 'sealing up with' has already been committed to inside the zipper. It contains the second half of the traversal or lens that got you to this point already. You've opened the lens or traversal already and 'upwards' seals off the changes by writing them back into the surrounding context.

Strabismus answered 20/2, 2015 at 4:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.