| ▲ | vq 6 hours ago | |||||||||||||
One slightly contrived example would be if you had a function that returned the point of a set closest to another given point. getClosest :: Set Point -> Point -> Point You could imagine getClosest build a quadtree internally and that tree wouldn't depend on the second argument. I say slightly contrived because I would probably prefer to make the tree explicit if this was important. Another example would be if you were wrapping a C-library but were exposing a pure interface. Say you had to create some object and lock a mutex for the first argument but the second was safe. If this was a function intended to be passed to higher-order functions then you might avoid a lot of unnecessary lock contention. You may be able to achieve something like this with optimisations of your explicit syntax, but argument order is relevant for this. I don't immediately see how it would be achieved without compiling a function for every permutation of the arguments. | ||||||||||||||
| ▲ | twic 6 hours ago | parent | next [-] | |||||||||||||
I think we need to see a few non-contrived examples, because i think in every case where you might take advantage of currying like this, you actually want to make it explicit, as you say. The flip side of your example is that people see a function signature like getClosest, and think it's fine to call it many times with a set and a point, and now you're building a fresh quadtree on each call. Making the staging explicit steers them away from this. | ||||||||||||||
| ||||||||||||||
| ▲ | emih 6 hours ago | parent | prev [-] | |||||||||||||
Those are nice examples, thanks. I was imagining you might achieve this optimization by inlining the function. So if you have
And call it like
Then the compiler might unfold the definition of getClosest and give you
Where it then notices the first part does not depend on p, and rewrite this to
Again, pretty contrived example. But maybe it could work. | ||||||||||||||
| ||||||||||||||