[jhc] The Wonders of generic applicative traversal routines

Roman Cheplyaka roma at ro-che.info
Tue May 13 11:28:23 UTC 2014


* John Meacham <john at repetae.net> [2014-05-13 03:21:51-0700]
> Yeah, I have used those before, and still use Data.Data generics in jhc in
> some places but always found them unwieldy. As you note in your page, .exact
> structural children and logical children of a type may be different and
> I found them brittle when I modified a type, I ended up not being able to
> use standard types like [] and Maybe as much because I occasionally wanted
> to do more interesting things with traversal.

To be 100% clear, the approach proposed on that page doesn't have this
limitation and doesn't conflate structural and logical children.
(Sorry if I'm stating the obvious; I am not sure what you're referring to by
"those".)

> This hybrid explicit dictionary approach that applies the traversal to
> components in parallel seems to be a sweet spot. I attempted something like
> it before using typeclasses, but it didn't work because I needed to modify
> the traversal functions en route sometimes based on dynamic info so anything
> hardcoded at the type level, even with clever newtype deriving, I found
> lacking.

It's great that it works for you!

I guess you're dealing with an intermediate representation of Haskell code.
My motivation behind traverse-with-class is dealing with full Haskell AST as
defined in haskell-src-exts. An traversal dictionary for that would be enormous,
and most of the components would be the same in any particular traversal (but
you don't know in advance which ones, of course).

So traverse-with-class helped me to manage that complexity.

I also had to deal with modifying the traversal; e.g. here's how I propagate
scope information in haskell-names:

https://github.com/haskell-suite/haskell-names/blob/master/src/Language/Haskell/Names/Open/Base.hs#L54
https://github.com/haskell-suite/haskell-names/blob/master/src/Language/Haskell/Names/Open/Instances.hs

It's a small edsl, so it may look weird at first, but it actually worked out
pretty nicely.

Roman
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://www.haskell.org/pipermail/jhc/attachments/20140513/2a58d6b6/attachment.sig>


More information about the jhc mailing list