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

Invalid recursive values should be a compiler error, not a runtime exception #873

Closed
evancz opened this Issue Jan 5, 2015 · 6 comments

Comments

Projects
None yet
5 participants
@evancz
Member

evancz commented Jan 5, 2015

Right now, Elm will not yell at you for writing invalid recursive values like ones = 1 :: ones where a value is defined directly in terms of itself. In a strict language like Elm, this means you'd spend an infinite amount of time building the entire structure!

Current Behavior

Right now, invalid code like this leads to a runtime exception. In some ways that is better than infinite looping forever, and these exceptions happen very reliably. It is not conditional on anything, you just need to evaluate the code. So it's not awful, but it is also something that beginners run into a decent amount.

Solution

Only permit recursion when there is at least one lambda between the definition and the usage. So x = x is not allowed, but fact n = if n <= 0 then 1 else n * fact (n - 1) is allowed because the definition implicitly introduces a lambda.

Timeline

This bug is kind of tricky to resolve, but it is not crazy. From a raw time efficiency standpoint, it is probably best to batch with work on Dead Code Elimination. I find it super annoying when people say "but Elm can have runtime exceptions" as if a compiler bug with a clear resolution is the same as runtime exceptions in JavaScript, so I'd like to do it sooner than DCE if possible.

Other Info

  • Check out https://github.com/elm-lang/core/issues/305 which shows how the order of definitions can be important in generated code. Probably should be done together.
  • Xavier has some very useful analysis here about how it works in OCaml.

@mgold mgold referenced this issue Jun 2, 2015

Closed

List of known runtime errors #913

1 of 7 tasks complete

@kmarekspartz kmarekspartz referenced this issue Aug 26, 2015

Closed

WIP: revamping code generation #1024

6 of 7 tasks complete

@elm elm locked and limited conversation to collaborators Aug 31, 2016

@evancz evancz changed the title from Properly deal with recursively defined values to Invalid recursive values should be a compiler error Aug 31, 2016

@evancz evancz added bug meta labels Aug 31, 2016

@evancz evancz changed the title from Invalid recursive values should be a compiler error to Invalid recursive values should be a compiler error, not a runtime exception Aug 31, 2016

@evancz

This comment has been minimized.

Member

evancz commented Oct 11, 2016

This should be fixed in 0.18

@evancz evancz closed this Oct 11, 2016

@evancz

This comment has been minimized.

Member

evancz commented Nov 1, 2016

Okay, writing something like x = x + 1 will now result in this message:

mutation

The link at the end points here.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.