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

Variable hoisting lets through subtle reference errors #68

Open
longouyang opened this issue Nov 5, 2014 · 0 comments
Open

Variable hoisting lets through subtle reference errors #68

longouyang opened this issue Nov 5, 2014 · 0 comments
Labels

Comments

@longouyang
Copy link
Member

In this code, A is defined before B, but doesn't cause a ReferenceError:

(define samples
 (mh-query 1000 10
    (define A (if B #t #f))
    (define B (flip))
    A
    B)
)
(hist samples "A")

And the resulting conditional distribution always has A = false, which is unintuitive.

The problem is that the JS we compile to looks like this:

(function () {
      var A = B ? true : false;
      var B = Math.random() > 0.5;
      return A
})()

JS does variable hoisting, which means that B always exists throughout the function body but it may not have a value. Indeed, at the first line, B's value is undefined, which is falsy in JS, and leads to A being false.

We might fix this issue by generating code with a ton of nested closures, e.g.,

(function () {
      var A = B ? true : false;
      (function() {
        var B = Math.random() > 0.5;
      })()
      return A
})()

But this would likely degrade performance

@longouyang longouyang added the bug label Nov 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant