How do you find and rewrite expressions that refer to the same bound name? For example, in the expression

let xs = ...
in ...map f xs...map g xs...

both the expression map f xs and the expression map g xs refer to the same bound name, namely xs. Are there any standard compiler analyses that would let us identify this situation and rewrite the two map expressions to e.g.

let xs = ...
    e = unzip (map (f *** g) xs)
in ...fst e...snd e...

I've been thinking about the problem in terms of a tree traversal. For example given the AST:

data Ast = Map (a -> b) -> Ast -> Ast
         | Var String
         | ...

we could try to write a tree traversal to detect this case, but that seems difficult since two Map nodes that refer to the same Var might appear at widely different places in the tree. This analysis seems easier to do if you inverted all the references in the AST, making it a graph, but I wanted to see if there are any alternatives to that approach.

Edit
Report