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

Named IIFE (immediately invoked function expression) optimized away #803

Open
JsonFreeman opened this issue Mar 29, 2017 · 2 comments
Open

Comments

@JsonFreeman
Copy link

(function factorial(n) {
    if (!n) {
        return 1;
    }
    return n * factorial(n-1);
})(3);

When this is compiled, the factorial function is optimized away because it is invoked immediately. I believe it is because of the condition here:

https://github.com/probmods/webppl/blob/dev/src/transforms/optimize.js#L85

I think it is good to check if the callee has a name, because then it might be used for recursion.

@null-a
Copy link
Member

null-a commented Mar 29, 2017

I wonder if (at least part of) the problem here is the fact that webppl doesn't bind the name of named function expressions in their body. e.g. ((function f() { return typeof f; })()); evaluates to 'function' in JS and 'undefined' in webppl?

(It's news to me that JS named function expressions do this.)

@JsonFreeman
Copy link
Author

I think named function expressions generally get misunderstood. Its often sufficient in practice to assign an anonymous function expression to a named variable and use that for recursion and I think this is the most common practice in webppl. It really comes down to the issue of assigning a name to an intermediate expression.

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

2 participants