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

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

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

Comments

@evancz
Copy link
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 mentioned this issue Jun 2, 2015
7 tasks
@elm elm locked and limited conversation to collaborators Aug 31, 2016
@evancz evancz changed the title Properly deal with recursively defined values Invalid recursive values should be a compiler error Aug 31, 2016
@evancz evancz changed the title Invalid recursive values should be a compiler error Invalid recursive values should be a compiler error, not a runtime exception Aug 31, 2016
@evancz
Copy link
Member Author

evancz commented Oct 11, 2016

This should be fixed in 0.18

@evancz evancz closed this as completed Oct 11, 2016
@evancz
Copy link
Member Author

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.

rtfeldman pushed a commit to rtfeldman/elm-compiler that referenced this issue Nov 19, 2018
This will detect any values defined in terms of themselves without any
lambda in the way to delay evaluation. It cannot catch things like “x =
(\_ -> x) ()” but at that point you are trying to solve the halting
problem, so that is out of scope.

Big thanks to the curious person who inadvertently helped me realize
how to do this!
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants