Question about numbers example of elm-lang.org

On https://elm-lang.org/examples/numbers
there is an example to generate a diece (number)

The update part looks like this:

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Roll ->
      ( model
      , Random.generate NewFace (Random.int 1 6)
      )

    NewFace newFace ->
      ( Model newFace
      , Cmd.none
      )

Am i correct to assume that when i click on the Roll button,
it first goes to Roll of the above case-statement, producing a (Model, Cmd Msg);
and then, since there is a Msg, this get’s passed again to the same update function,
but now it goes to Newface newFace on the above case-statement.

If this the case, how does this stop then? (because the Cmd.none is there?)

Eg: changing the above example to

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Roll ->
      ( model
      , Random.generate NewFace (Random.int 1 6)
      )

    NewFace newFace ->
      ( model
      , Random.generate NewFace (Random.int 1 6)
      )

seems to result in an endless loop.

Yes, everything you said is correct. It stops because of the Cmd.none.

Commands can create new messages, usually because they want to send some value back to your code.

Random.generate creates a new message containing a new random number.

But Cmd.none doesn’t create a new message. It’s just the dummy command you use when you don’t want to actually do any “real” command. In a loosely-typed language you’d probably use null or something instead of having a specific “empty command” like this.

Thank you,
this also means that elm allows endless loops that go nowhere as is also the case in (most?) programming languages.

That’s right. Disallowing infinite loops would require the compiler to first detect or predict that it would happen. This is a famous thing in computer science called the Halting Problem . It’s literally impossible to predict all cases where this could happen, if your language allows complex enough programs. So almost every language tolerates the possibility of infinite loops. I can’t remember exactly what features result in this. I’m sure wikipedia will tell you.

I think there is one very obscure language that prevents it. Can’t remember what it’s called. But there’s a reason it’s obscure!

1 Like

From the top of my head:

The configuration language Dhall makes sure you can’t loop indefinitely, because it doesn’t allow recursion and hasn’t got any other looping mechanisms.

There are some examples of theorem proving capable languages like Coq, Agda or Idris that require you to proove termination by default. There is some interesting talks/videos about termination checking in Idris, if you’re interested in that.

Back to elm: there is some termination checking in elm: if you write a value that is obviously non-terminating, you’ll get an error, for example with:

recursiveValue = 1 + recursiveValue

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.