3
votes

I am learning Elm, by going through "Programming Elm" by Fairbank (https://pragprog.com/book/jfelm/programming-elm), and going through some of the same growing pains as the person who posted what does the elm signature mean in "Program Never Model Msg"? -- the reply there by @dogbert states that

Program is a type parameterized by three type variables

What does that mean exactly? Does it mean that Program is a function and that the types of its parameters are the three type variables?

Thus

main: Program () {something: string} msg

says Program accepts only empty flags, a model that has the type given, and a msg?

(By the way, what then is it saying the type of mainis in this context? That is, it's a function of type Program?)

The nice thing about the book "Programming with Elm" is that it gets you up and going quickly, but the not so great thing is that it introduces some things -- like the above -- without going into much detail about what, exactly, they mean. I suppose I should really start with reading through the tutorial on the elm site, which is my next destination. But any help here would be appreciated.

1

1 Answers

9
votes

Program is a type. That means we can have values of type Program, like

main: Program () {something: String} msg
main =
    Browser.sandbox { init = init, update = update, view = view }

In Elm, main is not a function. Rather, it typically provides several functions for the runtime to call:

  • First, the runtime calls init, to pass in any flags and initialise the model.
  • Then it calls view, passing the model as an argument, to generate HTML to display.
  • Whenever your program needs to react to something (eg an event like a button click), the runtime calls update with the current model and a message indicating what happened. update returns a new model, and the runtime updates its state accordingly, then calls view to update the DOM.

Not all Elm programs require all of these, and there may be additional ones (like subscriptions), but the gist is that rather than having one main function that will be called when your program runs (as in many other languages), Elm programs rely on the Elm runtime to manage their lifecycle.

Now some more on the parameters (called type variables) to the Program type. It takes a bit of time to get used to the way Elm, as a purely functional language, handles types.

Maybe the List a type is a more relatable example. Elm's lists can only contain elements of the same type. This type is specified as a parameter a to List, so the compiler can verify that we won't put anything else in there.

So you can have a value of type List String or List Int:

messages : List String
messages =
    ["Hello!", "Goodbye"]

primes : List Int
primes =
    [2, 3, 5, 7]

The same way, Program is parametrised to specify the kind of flags, model and messages it accepts. This way, the Elm compiler can verify that the view, update etc. given in your main function behave the way they should with regards to their argument and return types, and the Elm runtime can make sure it's passing your program the correct arguments (eg flags).

Read more on type variables here.