Functor, Foldable and Traversable for Expr
Sebastian Graf
sgraf1337 at gmail.com
Mon Jun 18 14:28:57 UTC 2018
OK, so just deriving the instances doesn't yield the expected behavior,
because the `Var` case explicitly mentions `Id`s instead of the type
parameter `b`.
Even if that would be changed, it's not easy to pin down over which parts
of the syntax tree we should 'map'.
Should we include binding sites of local variables? I'm inclined to say No,
but only because I have my concrete use case in mind.
It's probably best to have non-derived, non-typeclass functions
`foldMapVars :: Monoid m => (Id -> m) -> Expr b -> m`, or a variant where
the mapping function also gets supplied a value of `data NameSite = Lam |
Let | VarRef` (for lack of a better name).
Am Mo., 18. Juni 2018 um 14:43 Uhr schrieb Sebastian Graf <
sgraf1337 at gmail.com>:
> Hi everyone,
>
> I'm repeatedly wondering why there are no `Functor`, `Foldable` and
> `Traversable` instances for `Expr`.
>
> Is this just by lack of motive?
> I could help there: I was looking for a function that would tell me if an
> expression mentions `makeStatic`. After spending some minutes searching in
> the code base, I decided to roll my own thing in `CoreUtils`.
> I really couldn't think about a good name, so I settled for
> `anyReferenceMatching :: (b -> Bool) -> Expr b -> Bool` and realized that I
> could generalize the function to `foldMapExpr :: Monoid m => (b -> m) ->
> Expr b -> m`.
>
> Occasionally this need pops up and I really want to avoid writing my own
> traversals over the syntax tree. So, would anyone object to a patch
> implementing these instances?
>
> Thanks
> Sebastian
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20180618/d4f92aa3/attachment.html>
More information about the ghc-devs
mailing list