gcanti / fp-ts

Functional programming in TypeScript
https://gcanti.github.io/fp-ts/
MIT License
10.85k stars 503 forks source link

Stack safety #1194

Open safareli opened 4 years ago

safareli commented 4 years ago

Couple years ago I was playing with stack safety in JS. but now I'm sort of playing with typescript and looks like the IO, free monad and free applicative all are nonstacksafe.

I have written optimized stack safe implementations for all of them in past years:

I can port them to TS and open PR's if this would be valuable for people

giogonzo commented 4 years ago

As answered also in slack:

Stack-safety hasn't been one of the major objectives for fp-ts core up to now, but at the same time having safer defaults would help IMO -> I still see value on making more constructs in fp-ts core stack-safe, as long as the complexity/maintenance burden is not too much

safareli commented 4 years ago

Great, then my question would be where should I start implementing this structures? I guess IO here and FreeAp and FreeMonad in fp-ts-contrib?

Implementations are linked in the issue so if they seam to be fine I can start adding types and preparing PRs.

giogonzo commented 4 years ago

well I would let @gcanti speak his mind first 😄

safareli commented 4 years ago

It should be possible to change the Effect into Effect<ENV,A> like this (haven't tested tho, but something like that should do the trick). Then it could be used to implement this structures in performant and stack safe way:

If this works out, all this would be at the cost of little complex implementation of the base synchronous ReaderT+IO, without braking API and with all the performance + stacksafety benefits. Tho I haven't done much TS and not sure how hard would it be to actually change representations like that but I think it shouldn't be too hard either.

of course for non basic cases people can use matechs-effect & waveguide.

shinzui commented 4 years ago

@safareli Do you mind sharing your Free Applicative port if you have it?

safareli commented 4 years ago

@shinzui I haven't made port in TS. If there is interest I can make one and open PR for fp-ts.

shinzui commented 4 years ago

@safareli I just started using fp-ts, and a free applicative was one of the first things I needed. It looks like fp-ts used to have it, but it got removed at some point.