SSCCE for the entire Elm syntax

For some reason, I was curious to see how a Short, Self Contained, Correct Example of the entire Elm syntax would look like.

I started with the counter example and added things as they came to my mind making sure to elm-format after each change. This is the result.

Did I missed any piece of syntax?

What would you add? What would you take away? What would be your take on a minimal example of the entire syntax?

How would you change the comments so that is easier to understand. (I tried to keep the comments to a minimum).

13 Likes

Nice! Seems useful to me. What about record extension?

What is your goal for this, btw?

1 Like

Cool!

  • Should there be an example of destructuring in let in? let (Foo foo) = bar
  • Should there be a toplevel definition without a type annotation?
  • And a let in with a type definition? Maybe also one where a “type variable is re-used from the parent function type annotation” (I think that’s a thing).
  • Maybe a multi-line type annotation?
  • Is there any formatting we want to add, that elm-format does not output? Such as single-line if.
3 Likes

Good Point. Maybe a phantom type too.

It’s just playing. No serious goals here. I sometime get questions in my mind that I would like answered. I guess I do am interested in how low can one go. Can all these things be expressed in less code? :slight_smile:

1 Like

I have produced a new version integrating the suggestions that were easier to think through.

  • Is there any formatting we want to add, that elm-format does not output? Such as single-line if.

I love the uniformity of elm-format and I would like the example to stay formatted. But you have a good point here. I know that Evan publishes code that has unconventional formatting ( like this section ) and single line ifs that fit into the line width limit are one of the things I wish elm-format would allow.

2 Likes

There are some weirder bits of Elm syntax:

  • WebGL literals (technically this could inflate the example considerably since Elm subsumes most, but not all GLSL syntax).
  • The syntax used internally like effect managers / kernel code (not sure there would be much value in this)
  • There are various phantom type/extensible record tricks. Some of these were first popularised by elm-css.
1 Like

I would rather stay away from the private API of the runtime as this is not available for regular users.

1 Like

Cool! Wish this existed years ago already! This is the relevant AST if you want to make sure you’ve covered everything: https://github.com/elm/compiler/blob/master/compiler/src/AST/Canonical.hs

1 Like

This will be great for stress-testing elm-in-elm, thanks @pdamoc!

2 Likes

Should << be there as well?

I have extended the example to cover extensible records, phantom types and the WebGL shaders language.

For phantom types and the shader language I just left the code as dead code.

I have also moved the code into a github repository.

I like the idea.

From a quick glance, it seems that at least the following are missing:

  • multiline strings (with """)
  • tuples and unit type pattern matching
  • exposing (..) import
  • characters (Char with ')
  • records fields accessors without the syntax suggar (.field record)

You could also show that record fields accessors work on expressions, for example:

(named "myname").name

Also have a look there:

There are other things like multiple imports using the same alias.

2 Likes

I have updated the example to cover the things you pointed at.

I just added dead code to exemplify some of the things.

LATER EDIT: I also added an example for Debug.log usage and for multiple declarations for _.

2 Likes

Seems like a cool resource to point people to if they want to get a pretty thorough overview of Elm syntax! A couple potential additions:

  • An example of partial function application
  • An example of using an operator as a function, either in prefix form like
    three =
        (+) 1 2
    
    or perhaps as an argument to a higher-order function, e.g.
    sums =
        List.map2 (+) [ 1, 2, 3 ] [ 10, 20, 30 ]
    
1 Like

I have updated the example with your suggestions.

It is very funny how I missed partial application as this is something I do all the time. Using infix operators in higher order functions is less frequent in my code but still… I use it from time to time. The use-case for infix operator as prefix function that I have in my code is actually partial application of the operator.

4 Likes

This is great! I’ve seen a lot of people new to Elm ask for this sort of document, thank you for making it.

keep in mind that you’ll be missing glsl and the internal apis like infix

1 Like

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