Functor, Foldable and Traversable for Expr

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Functor, Foldable and Traversable for Expr

Sebastian Graf
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

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: Functor, Foldable and Traversable for Expr

Sebastian Graf
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 <[hidden email]>:
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

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: Functor, Foldable and Traversable for Expr

Ben Gamari-3
Sebastian Graf <[hidden email]> writes:

> 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).
>
Agreed, I think there is enough subtlety here that it's best to be
explicit about which things in particular you want to traverse.

Cheers,

- Ben

_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

signature.asc (497 bytes) Download Attachment