Skip to content
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

Tail call optimization fails on multidimensional iteration #408

Open
pimlu opened this issue Jul 22, 2017 · 0 comments
Open

Tail call optimization fails on multidimensional iteration #408

pimlu opened this issue Jul 22, 2017 · 0 comments

Comments

@pimlu
Copy link

pimlu commented Jul 22, 2017

I get some weird SO behavior on the following program, which I think Haste tries to avoid by policy to my understanding. (I tried simplifying the code in a few ways, but it wouldn't crash anymore.)

import Haste.Foreign
import Haste.Prim
import Data.Function

data Point = Point Double Double
sqdist (Point x y) = x^2 + y^2

calcpi n = fdiv (400*count) $ n^2
  where fdiv = (/) `on` fromIntegral
        count = length $ filter ((<=1) . sqdist) points
        points = [Point (fdiv j l) $ fdiv i l | i<-[0..l], j<-[0,100..l]]
        l = n-1

main = export (toJSStr "calcpi") calcpi

Note the use of [0,100..l] in the second loop - this is to crash faster, because the inner loop is TCO'd but the outer loop results in a few function calls each iteration. However, this only happens the second time it's evaluated:

stack trace on second call

If I expand the inner loop to [0..l], it will hang indefinitely if I call it with n=5000 first (it probably eventually returns); I would have to call it with lower values of n to successfully blow the stack.

I'm using the latest haste binary release (0.5.5.1). Built with -O2 --opt-all --pretty-print.

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

No branches or pull requests

1 participant