Skip to content

Make function composition stack safe #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

safareli
Copy link
Contributor

TODO

  • add more tests
  • add benchmarks
  • add comments to code

@safareli
Copy link
Contributor Author

I got this idea just today and couldn't hold to build and open PR 🗡

It's sort of composition of work done in Eff http://github.com/purescript/purescript-eff/pull/31 and the post I wrote a while ago about Stack safe Function composition.

/cc @natefaubion @paf31

It's worth noting that whenever it's known that composition is happening for function type, compiler is doing optimization so this FFI function will not be called at all, that's why I use Semigroupoid constraint in test to force use of the FFI function.

@paf31
Copy link
Contributor

paf31 commented Dec 14, 2017

Sorry, but in my opinion this is totally unsuitable for Prelude. As I said on the eff PR, I think it's interesting, and a worthwhile addition in the form of a separate library, but it's just not the right sort of approach for the core libraries.

@hdgarrood
Copy link
Contributor

I'm with @paf31 on this, I think, especially since this only seems to have an effect in rare cases (I don't remember the last time I wrote a function with a Semigroupoid constraint). This approach seems risky to me from a maintenance perspective too; I imagine it might be quite easy to accidentally break this in the future if this code path is only rarely being hit.

@safareli
Copy link
Contributor Author

Fair points 🙌

@safareli safareli closed this Dec 14, 2017
@safareli safareli deleted the fast-fn branch December 14, 2017 18:59
@matthewleon
Copy link
Contributor

matthewleon commented Feb 13, 2018

@safareli did you end up doing anything with this in terms of making a separate lib? I think there could be some really handy uses.

@paf31
Copy link
Contributor

paf31 commented Feb 13, 2018

I don't think this will work for Builder.

@safareli
Copy link
Contributor Author

Nope, how you think this could be used as separate lib?

@matthewleon
Copy link
Contributor

matthewleon commented Feb 13, 2018 via email

@matthewleon
Copy link
Contributor

I don't think this will work for Builder.

Builder as it exists now, no... But if someone wrote a separate stack safe Builder lib which used this under the hood, shouldn't that work out?

@safareli
Copy link
Contributor Author

Did you get the stack overflow by using Builder?

@matthewleon
Copy link
Contributor

Yes, if you compose enough Builders you will get a stack overflow. The Category instance is just newtype derived from Function.

@matthewleon
Copy link
Contributor

(or rather, the Semigroupoid instance)

@safareli
Copy link
Contributor Author

Sure, it's less likely to happen, so was curious if you had practical case where it happened.
I will try to create a lib then

@matthewleon
Copy link
Contributor

Don't worry too much about it, as I have another solution for my situation. I just think this particular solution is quite interesting, and probably could be applied in other situations, too.

@safareli
Copy link
Contributor Author

I have published the lib here https://github.com/safareli/purescript-stacksafe-function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants