<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Brian Carroll</title>
    <description>The latest articles on Forem by Brian Carroll (@briancarroll).</description>
    <link>https://forem.com/briancarroll</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F86412%2F83fa878a-10b7-4d91-9999-33b748b05b31.jpeg</url>
      <title>Forem: Brian Carroll</title>
      <link>https://forem.com/briancarroll</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/briancarroll"/>
    <language>en</language>
    <item>
      <title>Porting Elm to WebAssembly</title>
      <dc:creator>Brian Carroll</dc:creator>
      <pubDate>Tue, 28 Sep 2021 19:28:58 +0000</pubDate>
      <link>https://forem.com/briancarroll/porting-elm-to-webassembly-2lp4</link>
      <guid>https://forem.com/briancarroll/porting-elm-to-webassembly-2lp4</guid>
      <description>&lt;p&gt;For a few years now, on and off, I've been working on an unofficial port of the Elm language to WebAssembly. It's not production ready but I'm at the stage now where I have some good working demos, and things are taking shape.&lt;/p&gt;

&lt;p&gt;For the past year or so I've been mainly working on robustness, doing a &lt;em&gt;lot&lt;/em&gt; of debugging, which also led to some rewriting and architecture changes. I rewrote a large part of the GC, fixed lots of edge cases in the language implementation, and made the Wasm/JavaScript interop a lot more efficient.&lt;/p&gt;

&lt;p&gt;After all that I've managed to reach my goal of being able to run Richard Feldman's &lt;a href="https://github.com/rtfeldman/elm-spa-example"&gt;Elm SPA Example&lt;/a&gt; in my system! 😃 Here's a working implementation &lt;a href="https://brian-carroll.github.io/elm_c_wasm/elm-spa-example-wasm/"&gt;compiled to WebAssembly&lt;/a&gt;. And for comparison, you can also check out the same code &lt;a href="https://brian-carroll.github.io/elm_c_wasm/elm-spa-example-js/"&gt;compiled to JavaScript&lt;/a&gt;. (Unfortunately the publicly available APIs don't seem to be returning very much data at the moment but there's not much I can do about that!)&lt;/p&gt;

&lt;h2&gt;
  
  
  Robustness
&lt;/h2&gt;

&lt;p&gt;My early attempts to get the SPA example running failed pretty badly. There were just too many compiler and core library bugs to be able to disentangle everything. I realised I needed to be patient and work on robustness. So I started by writing lots of unit tests for the low-level C code. You can &lt;a href="https://brian-carroll.github.io/elm_c_wasm/unit-tests/?argv=-av"&gt;run the tests in the browser&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And there was a specific part of the GC that was always throwing up hard-to-find bugs and was too complicated and hard to understand. It's the system that tracks references from the stack to the heap. I decided to just throw it out and keep trying different approaches until I found something that just felt stable and obvious and robust. I ended up rewriting it 4 times. The end result is something that's much more straightforward and a lot less scary, and I haven't had any bugs there since.&lt;/p&gt;

&lt;p&gt;Once all that handwritten C code was solid, I needed to make sure the C generated from Elm was working properly. I found the &lt;a href="https://github.com/elm/core/tree/master/tests/tests/Test"&gt;source&lt;/a&gt; for the core library's unit tests and decided to port them into my project and add some of my own tests. You can &lt;a href="https://brian-carroll.github.io/elm_c_wasm/language-test"&gt;run the tests in WebAssembly&lt;/a&gt; in your browser too. (Funnily enough, one of the biggest challenges was getting the &lt;a href="https://github.com/elm-explorations/test"&gt;Elm Test&lt;/a&gt; framework itself to run! The framework is more complex than the tests themselves. I still need to come back to the fuzzer tests!)&lt;/p&gt;

&lt;p&gt;Then finally, with a bit more debugging, the SPA example came together. That has a lot of code in it and I figure if I can get it to run, I can get most things to run.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance
&lt;/h2&gt;

&lt;p&gt;I haven't really focused on performance yet, but I did a quick analysis using Lighthouse from Chrome devtools. Already, without having optimised the JS/Wasm interface for each kernel module, it looks like the SPA example has similar performance to the official compiler's JS output with &lt;code&gt;--optimize&lt;/code&gt; and &lt;code&gt;uglify-js&lt;/code&gt;. That's a great place to be! There is a lot of encoding and decoding to optimise away to get more performance, which suggests the app code is running a lot faster in WebAssembly. And VirtualDom should get a lot faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  Still canned demos only!
&lt;/h2&gt;

&lt;p&gt;So overall I think the project is in a pretty good place! But it's not ready for general use. Currently it's only set up to run on the "canned" demo apps in my repo, which all have their own build scripts with minor variations. And there's no solution for package management, so you can't have two apps with different versions of Kernel code.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;The system breaks down into a few different areas:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compiler&lt;/strong&gt;: I chose to use C as an intermediate language. My forked Elm compiler generates C, then I use Emscripten/Clang to go from C to WebAssembly &amp;amp; JS. (I can also compile C to native code, which is a much better debugging experience.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kernel code&lt;/strong&gt;: Elm's runtime and its main data structures are implemented in handwritten JavaScript, so most of that had to be ported to C. (Again, having both the compiled code and kernel code in C is very helpful).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Garbage Collector&lt;/strong&gt;: The Elm language expects its target platform to automatically manage memory for it. Browsers don't implement garbage collection for WebAssembly so I built a mark/sweep garbage collector in C. My measurements estimate it only adds 7kB of Wasm to the bundle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Elm/JS interop&lt;/strong&gt;: This is actually the toughest part of the project! Let's get into it a bit more below, because it isn't obvious.&lt;/p&gt;

&lt;h2&gt;
  
  
  Targeting two languages
&lt;/h2&gt;

&lt;p&gt;The single most important thing to know about targeting WebAssembly for browsers is: &lt;strong&gt;WebAssembly doesn't have access to any of the Web APIs yet&lt;/strong&gt;. That means that if you want to do anything useful, you need to produce both WebAssembly &lt;em&gt;and&lt;/em&gt; JavaScript. This one crucial fact drives a lot of the system design.&lt;/p&gt;

&lt;p&gt;"Web API" here means things like &lt;code&gt;document.createElement&lt;/code&gt;, &lt;code&gt;XMLHttpRequest&lt;/code&gt; and so on. They are the interfaces between user code and the browser's internal functionality, often with an underlying implementation written in C++.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is obviously a major drawback and the WebAssembly project has several proposals to work towards better host integration. One of the key issues is how to manage reference lifetimes - if a Wasm module is holding a reference to a DOM node, then it can't be garbage-collected. And if it is just a number in Wasm then it can be copied, which makes it hard to keep track of the copies. These issues are addressed in the &lt;a href="https://github.com/WebAssembly/gc/blob/master/proposals/gc/Overview.md"&gt;GC proposal&lt;/a&gt;, which has been at "stage 1" since I first looked at it in 2018.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So our app gets compiled to a mix of WebAssembly and JS, and they need to talk to each other. That in turn means we need to encode and decode values between WebAssembly and JavaScript representations of the same values. For plain numbers, that's easy. For more complex structures it means serialising and deserialising to a binary format.&lt;/p&gt;

&lt;p&gt;This turns out to be a huge deal!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sure, WebAssembly "makes things fast"... but lots of serialising and deserialising "makes things slow"!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In practice, what I've found is that the performance of the system depends almost entirely on how you design this interface between WebAssembly and JavaScript. In my initial versions, there was a lot of unnecessary converting back and forth, and the WebAssembly+JS versions of apps were much slower than the plain JS versions. That traffic was reduced a lot when I ported the Platform and Scheduler Kernel code to C.&lt;/p&gt;

&lt;p&gt;We know the kernel code has to be split between C and JS, but &lt;em&gt;exactly&lt;/em&gt; where do you draw that line? It's fast for Elm code to call into Kernel C code, but slow to to call into Kernel JS code. So for performance, you want most of it to be in C. But on the other hand, the more Kernel code we decide to leave in JavaScript, the less work we have to do to port it.&lt;/p&gt;

&lt;p&gt;Obviously when the kernel libraries were designed, there was no slow "barrier" in between Elm code and Kernel code, or between two different parts of the Kernel code. I wonder if that constraint might have resulted in slightly different designs for some libraries? In practice, I want existing Elm apps to run in my system without modification, so I need all ported core libraries to at least retain the same APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Targeting two memory managers
&lt;/h2&gt;

&lt;p&gt;The two target languages are running in two isolated memory management zones. WebAssembly doesn't have access to the browser's main garbage collector.&lt;/p&gt;

&lt;p&gt;Unfortunately there are cases where WebAssembly code may want to hold a long-lived reference to some JS value, and vice versa. We need to make sure we don't end up with stale references to values after they've been collected.&lt;/p&gt;

&lt;p&gt;For example if we pass a value from external JS code through a port, it will appear in Elm as a &lt;code&gt;Json.Decode.Value&lt;/code&gt;. The Elm app could decide to store it in the model and keep it forever. But this is a general JavaScript value that could be unserialisable! We have to keep a long-lived reference to it in Wasm somehow. To do that, we push it into a dedicated JS array, and just pass the array index to Wasm. So the Wasm representation of &lt;code&gt;Json.Decode.Value&lt;/code&gt; is just an array index that tells it where to find the value in that array. When our Wasm Garbage Collector does a collection, it also calls out to JavaScript to remove any references that are no longer needed.&lt;/p&gt;

&lt;p&gt;Going the other direction is harder and we have to find workarounds. Most references from JS code to Wasm values are user-defined callbacks, sending a message back to the app from an effect module. Wasm functions themselves live at fixed addresses and don't get moved around by the Garbage Collector. But those functions may contain partially-applied arguments or closed-over values, which could get moved around in a major GC, so they're not safe. The current solution is to synchronously deserialise those values to JavaScript, and avoid having a JS-to-Wasm reference altogether. The values are serialised back to Wasm whenever the function is called.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next?
&lt;/h2&gt;

&lt;p&gt;Probably the most important practical issues are usability and scalability. I'd like to make the build system general enough and usable enough for people to try out the system on their own apps. And, related to that, I'd like to come up with a more general and scalable way to deal with packages so that all apps don't have to use the same package versions! Maybe we can get some real apps running.&lt;/p&gt;

&lt;p&gt;There's also lots of performance ideas I'd like to try out&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up a benchmark for some more focused performance work (perhaps &lt;a href="https://krausest.github.io/js-framework-benchmark/2021/table_chrome_93.0.4577.63.html"&gt;this one&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Port more kernel modules to C/Wasm. It looks like this could be one of the key performance drivers but there's a &lt;em&gt;lot&lt;/em&gt; of code to port.&lt;/li&gt;
&lt;li&gt;Finish building a VirtualDom implementation in C using cache-friendly "data-oriented design" techniques and an arena allocator&lt;/li&gt;
&lt;li&gt;Remove the Emscripten layer and just use clang. Emscripten was handy to get going but it bloats code size a lot.&lt;/li&gt;
&lt;li&gt;Implement some optimisations that should make function calls faster&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>elm</category>
      <category>webassembly</category>
    </item>
    <item>
      <title>Elm in Wasm: Custom types and extensible Records</title>
      <dc:creator>Brian Carroll</dc:creator>
      <pubDate>Mon, 02 Aug 2021 15:41:40 +0000</pubDate>
      <link>https://forem.com/briancarroll/elm-in-wasm-custom-types-and-extensible-records-15m0</link>
      <guid>https://forem.com/briancarroll/elm-in-wasm-custom-types-and-extensible-records-15m0</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/briancarroll/elm-in-wasm-built-in-typeclasses-2kcb"&gt;last post&lt;/a&gt; we discussed Elm's built-in types: Int, Float, Char, String, List, and tuples. This time let's look at user-defined "custom" types and extensible Records!&lt;/p&gt;

&lt;p&gt;This is part of a series about porting the Elm language to WebAssembly. It's a project I am doing as a hobbyist, not an official project by the core team.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Custom types
&lt;/h2&gt;

&lt;p&gt;Elm &lt;a href="https://guide.elm-lang.org/types/custom_types.html" rel="noopener noreferrer"&gt;Custom types&lt;/a&gt; are collections that the Elm programmer can define. They can have different variants, each with a "constructor" to identify it. And each constructor can contain some collection of values.&lt;/p&gt;

&lt;p&gt;Let's take a simple example where one constructor takes no parameters and the other takes two. We'll then look at how to implement this as a byte-level data structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;MyCustomType&lt;/span&gt;
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Ctor0&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Ctor1&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="kt"&gt;Char&lt;/span&gt;

&lt;span class="n"&gt;myCtor1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Ctor1&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="sc"&gt;'x'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each value of this type will need&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A value header with some runtime metadata (which helps with GC and some built-in operators like &lt;code&gt;==&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Some way of identifying the associated constructor&lt;/li&gt;
&lt;li&gt;The contained values, if any&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;An implementation is shown in the diagram below. (Each small box represents a 32-bit value.)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo2bmlgdklx9zm436f1uk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo2bmlgdklx9zm436f1uk.png" alt="Custom type structures"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ctor&lt;/code&gt; field identifies the constructor variant of the type the value is. It value needs to be unique &lt;em&gt;within&lt;/em&gt; a given type so that we can implement pattern matching. It doesn't need to be unique within the &lt;em&gt;program&lt;/em&gt; because the Elm compiler ensures that we can never compare or pattern-match values of different types. (Though with 32 bits, we get around 4 billion constructors. So it's easy to make them globally unique, and it can be handy for debugging).&lt;/p&gt;

&lt;p&gt;Variants that take parameters will have an associated constructor function, generated by the compiler.&lt;/p&gt;

&lt;p&gt;Variants that take no parameters are static constants in the program, without a constructor function. There only needs to be one instance of the value per program. For example the list &lt;code&gt;[Ctor0, Ctor0, Ctor0]&lt;/code&gt; would just contain three pointers to the same memory address where &lt;code&gt;Ctor0&lt;/code&gt; is located.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Unit
&lt;/h2&gt;

&lt;p&gt;The Unit type is just like a custom type with a single constructor. It has its own special symbol &lt;code&gt;()&lt;/code&gt; in Elm source code but it's otherwise equivalent to the definition below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Bool
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Bool&lt;/code&gt; follows the same structure as custom types. It has two constructors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;
    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;True&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;True&lt;/code&gt; and &lt;code&gt;False&lt;/code&gt; are constructors without any parameters, so they can be global constant values, defined once per program at a fixed memory location.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An alternative way to implement &lt;code&gt;Bool&lt;/code&gt; would be to just use the integers 1 and 0, and that would make a lot of code more efficient. However a complication arises when we want to create something like a &lt;code&gt;List Bool&lt;/code&gt;. Normally a &lt;code&gt;Cons&lt;/code&gt; cell in a &lt;code&gt;List&lt;/code&gt; contains a pointer to its &lt;code&gt;head&lt;/code&gt; value. But now we're saying that &lt;code&gt;True&lt;/code&gt; is not a pointer, it's just a literal number! That means the &lt;code&gt;head&lt;/code&gt; of a &lt;code&gt;Cons&lt;/code&gt; cell &lt;em&gt;might&lt;/em&gt; be a pointer or it &lt;em&gt;might&lt;/em&gt; be just an integer, it depends! We need infrastructure in the compiler and runtime to resolve that ambiguity. Almost all mature language runtimes do this, but my project is not quite that mature yet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Extensible Records
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://elm-lang.org/docs/records" rel="noopener noreferrer"&gt;Records&lt;/a&gt; are one of the most interesting parts of Elm's type system. In the following code, each function takes an extensible record type, which allows us to pass it a value of either type &lt;code&gt;Rec1&lt;/code&gt; or &lt;code&gt;Rec2&lt;/code&gt;. But values are either definitely of type &lt;code&gt;Rec1&lt;/code&gt;, or definitely of type &lt;code&gt;Rec2&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Rec1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myField&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Rec2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;myField&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;otherField&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;sumMyField&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;myField&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;sumMyField&lt;/span&gt; &lt;span class="n"&gt;recList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;myField&lt;/span&gt; &lt;span class="n"&gt;recList&lt;/span&gt;   &lt;span class="c1"&gt;-- accessor .myField is an Elm function&lt;/span&gt;

&lt;span class="n"&gt;incrementMyField&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;myField&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
&lt;span class="n"&gt;incrementMyField&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;myField&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;myField&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;  &lt;span class="c1"&gt;-- record update expression&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The basic operators that work on extensible record types are accessor functions and update expressions. In both cases we need to &lt;em&gt;find&lt;/em&gt; the relevant field in a particular record before we can do anything with it. So there needs to be some mechanism to look up the position of a field within a record.&lt;/p&gt;

&lt;h3&gt;
  
  
  Field IDs as integers
&lt;/h3&gt;

&lt;p&gt;In Elm source code, fields are human-readable labels for parameters of a Record. But they can be represented in more efficient ways. For example the 0.19 compiler in &lt;code&gt;--optimize&lt;/code&gt; mode converts field names to unique shortened names in the generated JavaScript.&lt;/p&gt;

&lt;p&gt;For a lower-level target like Wasm, we can transform field names to integer IDs rather than shortened names.&lt;/p&gt;

&lt;p&gt;There are two ways to do this&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Copy and modify the compiler code for generating short names and generate integers intead&lt;/li&gt;
&lt;li&gt;If we're targeting an intermediate language, let it generate integer values for us. For example my C implementation emits an &lt;code&gt;enum&lt;/code&gt; like this:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;FIELD_init&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;FIELD_subscriptions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;FIELD_update&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;FIELD_view&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All C code refers to Record fields using the &lt;code&gt;enum&lt;/code&gt; members, which the C compiler later transforms to numbers. This approach helps with readability of the C code when debugging the compiler.&lt;/p&gt;

&lt;h3&gt;
  
  
  Record implementation
&lt;/h3&gt;

&lt;p&gt;Let's see how we can represent records, using the following value as an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;ExampleRecordType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;field123&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
    &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;field456&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Char&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;myRecord&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ExampleRecordType&lt;/span&gt;
&lt;span class="n"&gt;myRecord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;field123&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
    &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;field456&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'x'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be represented by the collection of low-level structures below. For illustration, we assume the compiler has converted the field name &lt;code&gt;field123&lt;/code&gt; to the integer 123, and &lt;code&gt;field456&lt;/code&gt; to 456. Integers and pointers are 32 bits and so take up 1 size unit, Floats are 64 bits and take up 2 size units.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuxez6ve4qb6r8gytj0uu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuxez6ve4qb6r8gytj0uu.png" alt="Record data structures"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;FieldGroup&lt;/code&gt; data structure is an array of integers with a size. It is a static piece of metadata about the record type &lt;code&gt;ExampleRecordType&lt;/code&gt;. My &lt;a href="https://github.com/brian-carroll/elm-compiler" rel="noopener noreferrer"&gt;fork&lt;/a&gt; of the Elm compiler generates one instance for each record type, and populates it with the relevant integer field IDs. All records of the same type point to a single shared &lt;code&gt;FieldGroup&lt;/code&gt;. The &lt;code&gt;FieldGroup&lt;/code&gt; does not need a &lt;code&gt;header&lt;/code&gt; field since it can never be confused with any other type. It only be accessed through a &lt;code&gt;Record&lt;/code&gt; and is not garbage-collected since it's static data.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Record&lt;/code&gt; itself is a collection of pointers, referencing its &lt;code&gt;FieldGroup&lt;/code&gt; and its parameter values. The value pointers are arranged in the same order as the field IDs in the &lt;code&gt;FieldGroup&lt;/code&gt;, so that accessor functions and update expressions can easily find the value corresponding to a particular field ID.&lt;/p&gt;

&lt;h3&gt;
  
  
  Field access
&lt;/h3&gt;

&lt;p&gt;To implement an expression like &lt;code&gt;myRecord.field123&lt;/code&gt;, the algorithm is as follows&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Look at the &lt;code&gt;fieldgroup&lt;/code&gt; property of &lt;code&gt;myRecord&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Follow the pointer to  the &lt;code&gt;FieldGroup&lt;/code&gt; instance&lt;/li&gt;
&lt;li&gt;Search for the field ID &lt;code&gt;123&lt;/code&gt;, finding it at position &lt;code&gt;0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Look up the value at position &lt;code&gt;0&lt;/code&gt; in &lt;code&gt;myRecord&lt;/code&gt; itself&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Accessor functions
&lt;/h3&gt;

&lt;p&gt;An Elm accessor function is a special function that looks up a particular field name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;field123&lt;/span&gt; &lt;span class="n"&gt;myRecord&lt;/span&gt; &lt;span class="c1"&gt;-- 42&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;.field123&lt;/code&gt; is a function that will access &lt;code&gt;field123&lt;/code&gt; of any record that contains it.&lt;/p&gt;

&lt;p&gt;The simplest way to implement this is to define a kernel function that can access any Record field by ID, taking the field ID as the first parameter. Then we can just use partial application to specialise it to a specific field in the generated code.&lt;/p&gt;

&lt;p&gt;If you're interested in more details, check out the &lt;a href="https://github.com/brian-carroll/elm_c_wasm/blob/master/src/kernel/core/utils.c" rel="noopener noreferrer"&gt;full source&lt;/a&gt;, and perhaps read my previous post on &lt;a href="https://dev.to/briancarroll/elm-functions-in-webassembly-50ak"&gt;Elm functions in Wasm&lt;/a&gt; to see how partial application is implemented.&lt;/p&gt;

&lt;h3&gt;
  
  
  Update expressions
&lt;/h3&gt;

&lt;p&gt;Elm update expressions look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;updatedRecord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;originalRecord&lt;/span&gt;
          &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;updatedField1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newValue1&lt;/span&gt;
          &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updatedField2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newValue2&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Elm 0.19 this is implemented by &lt;a href="https://github.com/elm/core/blob/1.0.0/src/Elm/Kernel/Utils.js#L151-L166" rel="noopener noreferrer"&gt;a JavaScript function&lt;/a&gt; that clones the old record, and then updates each of the selected fields in the new record.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;_Utils_update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldRecord&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;updatedFields&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;newRecord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;oldRecord&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;newRecord&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;oldRecord&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;updatedFields&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;newRecord&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;updatedFields&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newRecord&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can do something similar in C as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;Utils_update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n_updates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r_new&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;clone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n_updates&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;field_pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fieldgroup_search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r_new&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;fieldgroup&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="n"&gt;r_new&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;field_pos&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;r_new&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I've left out the details of &lt;code&gt;clone&lt;/code&gt; and &lt;code&gt;fieldgroup_search&lt;/code&gt; but they pretty much do what you'd expect. Feel free to take a look at the &lt;a href="https://github.com/brian-carroll/elm_c_wasm/blob/master/src/kernel/core/utils.c" rel="noopener noreferrer"&gt;full source code&lt;/a&gt;, which includes &lt;a href="https://github.com/brian-carroll/elm_c_wasm/blob/master/src/kernel/core/utils_test.c" rel="noopener noreferrer"&gt;tests&lt;/a&gt; that mimic generated code from the compiler.&lt;/p&gt;

&lt;h3&gt;
  
  
  Records in similar languages
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://dev.realworldocaml.org/runtime-memory-layout.html" rel="noopener noreferrer"&gt;OCaml has records&lt;/a&gt;, but not extensible record types. That means a given field always refers to the same position in a record type, so there's no need to search for it at runtime. All field names can safely be transformed into position offsets at compile time.&lt;/p&gt;

&lt;p&gt;Haskell has extensible records, and the original paper on them is &lt;a href="http://web.archive.org/web/20160322051608/http://research.microsoft.com/en-us/um/people/simonpj/Haskell/records.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;. The focus is very much on trying to make the record system backwards-compatible with Haskell's pre-existing types, which were all positional rather than named. Unfortunately this means that most of their design decisions were driven by a constraint that Elm just doesn't have, so I didn't find it directly useful.&lt;/p&gt;

&lt;p&gt;However the &lt;code&gt;FieldGroup&lt;/code&gt; concept is very much inspired by the &lt;a href="https://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/HeapObjects#InfoTables" rel="noopener noreferrer"&gt;InfoTable&lt;/a&gt; that is generated for every type in a Haskell program.&lt;/p&gt;

</description>
      <category>elm</category>
      <category>webassembly</category>
    </item>
    <item>
      <title>Elm in Wasm: Built-in typeclasses</title>
      <dc:creator>Brian Carroll</dc:creator>
      <pubDate>Mon, 02 Aug 2021 11:51:39 +0000</pubDate>
      <link>https://forem.com/briancarroll/elm-in-wasm-built-in-typeclasses-2kcb</link>
      <guid>https://forem.com/briancarroll/elm-in-wasm-built-in-typeclasses-2kcb</guid>
      <description>&lt;p&gt;In my &lt;a href="https://dev.to/briancarroll/elm-functions-in-webassembly-50ak"&gt;last post&lt;/a&gt;, I proposed some ideas for how Elm's first-class functions could work in WebAssembly.&lt;/p&gt;

&lt;p&gt;This time, let's start looking at some of the other value types in Elm. What do the most fundamental value types look like?&lt;/p&gt;

&lt;p&gt;This is part of a series about porting the Elm language to WebAssembly. It's a project I am doing as a hobbyist, not an official project by the core team.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Comparables, Appendables and Numbers
&lt;/h2&gt;

&lt;p&gt;Let's start with the fundamentals: &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Char&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;List&lt;/code&gt; and &lt;code&gt;Tuple&lt;/code&gt;. It's fairly straightforward to design binary representations for these, but there are also some subtleties!&lt;/p&gt;

&lt;p&gt;The trickiest aspect of these types in Elm is that they are all members of &lt;a href="https://guide.elm-lang.org/types/reading_types.html#constrained-type-variables" rel="noopener noreferrer"&gt;constrained type variables&lt;/a&gt;. This is the mechanism that allows some functions like &lt;code&gt;++&lt;/code&gt;, &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;&amp;gt;&lt;/code&gt;, to work on &lt;em&gt;more than one, but not all&lt;/em&gt; value types.&lt;/p&gt;

&lt;p&gt;The table below lists the four constrained type variables, and which functions from the core libraries use them.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Type variable&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Core library functions&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;appendable&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;++&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;number&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;^&lt;/code&gt;, &lt;code&gt;negate&lt;/code&gt;, &lt;code&gt;abs&lt;/code&gt;, &lt;code&gt;clamp&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;comparable&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;compare&lt;/code&gt;, &lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;=&lt;/code&gt;, &lt;code&gt;&amp;gt;=&lt;/code&gt;, &lt;code&gt;max&lt;/code&gt;, &lt;code&gt;min&lt;/code&gt;, &lt;code&gt;Dict.*&lt;/code&gt;, &lt;code&gt;Set.*&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;compappend&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;(Internal compiler use only)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Here's a breakdown of which types belong to which type variables&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;number&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;comparable&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;appendable&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;compappend&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Float&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Char&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;String&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;List a&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✓*&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;✓*&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;(a, b)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✓*&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;(a, b, c)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✓*&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;* Lists and Tuples are only comparable only if their contents are comparable&lt;/p&gt;

&lt;p&gt;Low-level functions that operate on these type variables need to be able to look at an Elm value and decide which concrete type it is. For example the &lt;code&gt;compare&lt;/code&gt; function (which is the basis for  &lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;=&lt;/code&gt;, and &lt;code&gt;&amp;gt;=&lt;/code&gt;) can accept five different types, and needs to run different low-level code for each.&lt;/p&gt;

&lt;p&gt;There's no syntax to do that in Elm code - it's deliberately restricted to Kernel code. Let's look at the JavaScript implementation, and then think about how a WebAssembly version might work. We'll focus on &lt;code&gt;comparable&lt;/code&gt;, since it covers the most types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparable values in JavaScript
&lt;/h2&gt;

&lt;p&gt;Well Elm is open source, so we can just take a peek at the &lt;a href="https://github.com/elm/core/blob/master/src/Elm/Kernel/Utils.js#L87-L120" rel="noopener noreferrer"&gt;Kernel code for &lt;code&gt;compare&lt;/code&gt;&lt;/a&gt; to see how it's done. For the purposes of this article, we only care about how it tells the difference between different Elm types, so I've commented out everything else below.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;_Utils_cmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// x and y will always have the same Elm type in a compiled program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Elm Int, Float or String. Compare using `===` and `&amp;lt;`&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Elm Char. Take x.valueOf() and y.valueOf(), then compare.&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Elm Tuples ('#2' or '#3'). Recursively compare contents.&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;//  ... Elm List (the only remaining comparable type). Recursively compare elements.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Elm's &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt; values correspond directly to JavaScript primitives and can be identified using JavaScript's &lt;code&gt;typeof&lt;/code&gt; operator. This is not something we'll have available in WebAssembly, so we'll have to find another way to get the same kind of information.&lt;/p&gt;

&lt;p&gt;The other Elm types are all represented as different object types. &lt;code&gt;Char&lt;/code&gt; values are represented as &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Distinction_between_string_primitives_and_String_objects" rel="noopener noreferrer"&gt;String objects&lt;/a&gt; and can be identified using the &lt;code&gt;instanceof&lt;/code&gt; operator. Again, &lt;code&gt;instanceof&lt;/code&gt; is not available in WebAssembly, and we need something else.&lt;/p&gt;

&lt;p&gt;In the next part of the function we get a clue that when Elm values are represented as JS objects, they normally have a &lt;code&gt;$&lt;/code&gt; property. This is set to different values for different types. It's &lt;code&gt;#2&lt;/code&gt; or &lt;code&gt;#3&lt;/code&gt; for Tuples, &lt;code&gt;[]&lt;/code&gt; or &lt;code&gt;::&lt;/code&gt; for Lists, and can take on various other values for custom types and records. In &lt;code&gt;--optimize&lt;/code&gt; mode it becomes a number.&lt;/p&gt;

&lt;p&gt;Now this is something we &lt;em&gt;can&lt;/em&gt; do in WebAssembly. The &lt;code&gt;$&lt;/code&gt; property is just an extra piece of data that's bundled along with the value itself. We can add a "header" of extra bytes in front of the runtime representation of every value to carry the type information we need.&lt;/p&gt;

&lt;h2&gt;
  
  
  Value Headers
&lt;/h2&gt;

&lt;p&gt;Many languages add a "header" to their value representations to carry metadata that's only for the runtime, not for the application developer. We can use that technique to distinguish the different types. All Elm types can be covered with only 11 tags, which only requires 4 bits.&lt;/p&gt;

&lt;p&gt;It's also helpful to add a &lt;code&gt;size&lt;/code&gt; parameter to the header, indicating the size in memory of the value in a way that is independent of its type. This is useful for memory operations like cloning and garbage collection, as well as for testing equality of strings, custom type values, and records.&lt;/p&gt;

&lt;p&gt;In my &lt;a href="https://github.com/brian-carroll/elm_c_wasm/blob/master/src/kernel/types.h" rel="noopener noreferrer"&gt;project&lt;/a&gt; I've chosen the following bit assignments for the header. They add up to 32 bits in total, which is convenient for memory layout.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Bits&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Tag&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Elm value type. See enum definition above&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Size&lt;/td&gt;
&lt;td&gt;28&lt;/td&gt;
&lt;td&gt;Payload size in 32-bit words. Maximum is 2&lt;sup&gt;28&lt;/sup&gt;-1 units = (2&lt;sup&gt;28&lt;/sup&gt;-1) * 4 bytes = 1GB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following C code represents the header.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Tag_Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Tag_Float&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Tag_Char&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Tag_String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Tag_List&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Tag_Tuple2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Tag_Tuple3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Tag_Custom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Tag_Record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Tag_Closure&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;u32&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// payload size in integers (28 bits =&amp;gt; &amp;lt;1GB)&lt;/span&gt;
  &lt;span class="n"&gt;Tag&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// runtime type tag (4 bits)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Comparable values in WebAssembly
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwnjn1xh968x3od9jg9ad.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwnjn1xh968x3od9jg9ad.png" alt="value-representations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using these representations, we can distinguish between any of the values that are members of &lt;code&gt;comparable&lt;/code&gt;, &lt;code&gt;appendable&lt;/code&gt;, or &lt;code&gt;number&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, to add two Elm &lt;code&gt;number&lt;/code&gt;values, the algorithm would be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If tag is &lt;code&gt;Float&lt;/code&gt; (1)

&lt;ul&gt;
&lt;li&gt;Do floating-point addition&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;else

&lt;ul&gt;
&lt;li&gt;Do integer addition&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;We need this information because in WebAssembly, integer and floating-point addition are different &lt;a href="https://webassembly.github.io/spec/core/syntax/instructions.html#numeric-instructions" rel="noopener noreferrer"&gt;instructions&lt;/a&gt;. We're not allowed to be ambiguous about it like in JavaScript.&lt;/p&gt;

&lt;p&gt;Functions operating on &lt;code&gt;appendable&lt;/code&gt; values can use similar techniques to distinguish String (7) from List (0 or 1) and execute different code branches for each.&lt;/p&gt;

&lt;h3&gt;
  
  
  Structural sharing
&lt;/h3&gt;

&lt;p&gt;To have efficient immutable data structures, it's important that we do as much structural sharing as possible. The above implementations of List and Tuple allow for that by using pointers. For example when we copy a List, we'll just do a "shallow" copy, without recursively following pointers. The pointer is copied literally, so we get a second pointer to the same value.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;I've outlined some possible byte-level representations for the most basic Elm data types. We haven't discussed Custom types or Records yet. That's for the next post!&lt;/p&gt;

&lt;p&gt;We discussed some of the challenges presented by Elm's "constrained type variables" &lt;code&gt;comparable&lt;/code&gt;,  &lt;code&gt;appendable&lt;/code&gt;, and &lt;code&gt;number&lt;/code&gt; needing some type information at runtime. We came up with a way of dealing with this using "boxed" values with headers.&lt;/p&gt;

&lt;p&gt;If you like you can check out some of my GitHub repos&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;a href="https://github.com/brian-carroll/elm-compiler" rel="noopener noreferrer"&gt;fork of the Elm compiler&lt;/a&gt; that generates Wasm (from my Elm AST test data, not from real apps!)&lt;/li&gt;
&lt;li&gt;Some of the &lt;a href="https://github.com/brian-carroll/elm_c_wasm" rel="noopener noreferrer"&gt;Elm kernel libraries in C&lt;/a&gt;, compiled to Wasm.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Next Post
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://dev.to/briancarroll/elm-in-wasm-custom-types-and-extensible-records-15m0"&gt;Custom types and extensible Records&lt;/a&gt;&lt;/p&gt;

</description>
      <category>elm</category>
      <category>webassembly</category>
    </item>
    <item>
      <title>WebAssembly compiler update</title>
      <dc:creator>Brian Carroll</dc:creator>
      <pubDate>Sat, 02 Jan 2021 15:31:17 +0000</pubDate>
      <link>https://forem.com/briancarroll/webassembly-compiler-update-1i2g</link>
      <guid>https://forem.com/briancarroll/webassembly-compiler-update-1i2g</guid>
      <description>&lt;p&gt;&lt;em&gt;This is a repost of a &lt;a href="https://discourse.elm-lang.org/t/webassembly-compiler-update/5866"&gt;Discourse post&lt;/a&gt; from June 2020&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I've posted a &lt;a href="https://discourse.elm-lang.org/t/first-class-functions-in-webassembly/1577"&gt;few&lt;/a&gt; &lt;a href="https://discourse.elm-lang.org/t/elm-core-libs-in-webassembly/4443"&gt;times&lt;/a&gt; &lt;a href="https://discourse.elm-lang.org/t/basic-demo-of-elm-in-webassembly/4627"&gt;before&lt;/a&gt; about my project to compile Elm to WebAssembly.&lt;/p&gt;

&lt;p&gt;The project consists of two GitHub repos, one for the &lt;a href="https://github.com/brian-carroll/elm-compiler"&gt;compiler&lt;/a&gt; and one for the &lt;a href="https://github.com/brian-carroll/elm_c_wasm"&gt;core libraries&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;None of this is official. I'm not part of the core team and, as far as I know, they have no plans to move to WebAssembly any time soon. This is a hobby project driven by my own curiosity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary of previous posts
&lt;/h3&gt;

&lt;p&gt;In &lt;a href="https://discourse.elm-lang.org/t/elm-core-libs-in-webassembly/4443"&gt;this post&lt;/a&gt;, I described the custom Garbage Collector, and the parts of the core libraries I'd ported to Wasm.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://discourse.elm-lang.org/t/basic-demo-of-elm-in-webassembly/4627"&gt;my last post&lt;/a&gt; I described the system architecture. The Elm runtime remains in JavaScript because WebAssembly doesn't have Web APIs yet. The Wasm app talks to the runtime through a JS wrapper.&lt;/p&gt;

&lt;p&gt;I also showed a demo of a very basic working app. It was "hand compiled" as I didn't have a working compiler yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  Latest news
&lt;/h3&gt;

&lt;p&gt;My &lt;a href="https://brian-carroll.github.io/elm_c_wasm/todo-mvc/"&gt;latest demo&lt;/a&gt; is actually fully compiled code!&lt;/p&gt;

&lt;p&gt;It's a WebAssembly port of Evan's &lt;em&gt;TodoMVC&lt;/em&gt; example from a few years ago (here's his &lt;a href="https://github.com/evancz/elm-todomvc"&gt;original repo&lt;/a&gt;)&lt;/p&gt;

&lt;h3&gt;
  
  
  Compiler changes
&lt;/h3&gt;

&lt;p&gt;The forked compiler accepts &lt;code&gt;--output elm.c&lt;/code&gt; as a command-line option as well as &lt;code&gt;--output elm.js&lt;/code&gt; and &lt;code&gt;--output elm.html&lt;/code&gt;. Once I have the C file, I use &lt;a href="https://emscripten.org/"&gt;Emscripten&lt;/a&gt; to further compile it to WebAssembly. There are a few build steps that I coordinate using GNU &lt;code&gt;make&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I ran into a few challenges with type information. The compiler has several different stages. I only worked on the last stage, code generation, to limit the scope. But all type information has been dropped from the AST by then, and that created some challenges.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Currently it's unsafe to use a Float parameter in an app-level &lt;code&gt;Msg&lt;/code&gt; type. I have no way to tell Int from Float when passing messages from the JS runtime to the Wasm app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;Time&lt;/code&gt; module doesn't work because it uses Int for timestamps. Realistic values require at least 42 bits but I'm using 32 bits. Some low level details work out nicely that way, because Wasm pointers are 32 bits. And the &lt;code&gt;Json&lt;/code&gt; and &lt;code&gt;Bitwise&lt;/code&gt; libraries rrequire 32-bit integers as well.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I need to be able to distinguish custom types from tuples and lists. I'm using runtime type detection, but I'd prefer not to.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More detail here: &lt;a href="https://github.com/brian-carroll/elm-compiler#architecture-challenges"&gt;https://github.com/brian-carroll/elm-compiler#architecture-challenges&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Development Status
&lt;/h3&gt;

&lt;p&gt;So is that it? Is it all working? Can I use it in production right now? Is it really fast? OMG!&lt;/p&gt;

&lt;p&gt;Nope! Sorry!&lt;/p&gt;

&lt;p&gt;I'm still working through lots of implementation issues. For example I have not yet managed to get Richard Feldman's &lt;a href="https://github.com/rtfeldman/elm-spa-example"&gt;elm-spa-example&lt;/a&gt; working. It's a great test-case because it's complex enough that if I have any bugs, it's bound to show them up!&lt;/p&gt;

&lt;p&gt;I haven't done any performance work yet. Before I can focus on that, I need to debug it and sort out some issues with the architecture (see "current focus" below).&lt;/p&gt;

&lt;h3&gt;
  
  
  Current focus
&lt;/h3&gt;

&lt;p&gt;A lot of the work I'm currently doing is on the JS/Wasm interface. Since I have the runtime in JS and the app in Wasm, the interface between the two is a major focus.&lt;/p&gt;

&lt;p&gt;Two of the topics I'm thinking about:&lt;/p&gt;

&lt;p&gt;Some of the objects passed from the JS runtime to the app are unserialisable. For example, DOM events are not serialisable because they contain cyclical references. It's all to do with how the Json library is implemented. I have something that works &lt;em&gt;most&lt;/em&gt; of the time! But I'm working on something more reliable.&lt;/p&gt;

&lt;p&gt;Currently the app's &lt;code&gt;Model&lt;/code&gt; is stored in JS but the &lt;code&gt;update&lt;/code&gt; function is in Wasm. That means the model has to get passed from JS to Wasm and back again on every update cycle, getting serialised and deserialised along the way. The only reason it works this way is that it was quicker to get up and running, because I didn't need to change anything in the JS runtime.&lt;/p&gt;

&lt;h3&gt;
  
  
  String encoding
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://github.com/elm/projects#explore-webassembly"&gt;original post&lt;/a&gt; suggesting this project specifically mentions string encoding, and UTF-8 in particular. And there was some discussion of this in my &lt;a href="https://discourse.elm-lang.org/t/elm-core-libs-in-webassembly/4443/7"&gt;last post&lt;/a&gt;. I suggested that UTF-16 might have advantages, due to better compatibility with JS and most of the browser APIs.&lt;/p&gt;

&lt;p&gt;I did some &lt;a href="https://github.com/brian-carroll/elm_c_wasm/tree/ebd9a466a8c30a140caa74cdd5f62b1afc6f7221/demos/2020-04-string-encoding"&gt;benchmarking&lt;/a&gt; on both encodings, to get an idea of the performance implications.&lt;/p&gt;

&lt;p&gt;There's not much performance difference. Based on the results, I initially wanted to go with UTF-8. But then I realised that every time I pick an app to test the compiler on, I would also have to migrate its Elm code to using a new String library as well. Otherwise things like URL parsing might break, and who knows what else? It just makes things too complicated. So I'm sticking with UTF-16 for this project. UTF-8 is a separate project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Asynchronous initialisation
&lt;/h3&gt;

&lt;p&gt;WebAssembly modules are normally compiled &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming"&gt;asynchronously&lt;/a&gt; once loaded into the browser. We have to wait until the compilation is finished before we can call &lt;code&gt;Elm.Main.init&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I created a new function &lt;code&gt;Elm.onReady&lt;/code&gt; to help with this. You just put your app's normal setup code in a callback, and &lt;code&gt;Elm.onReady&lt;/code&gt; will execute it at the right time.&lt;/p&gt;

&lt;p&gt;For my WebAssembly version of the TodoMVC example, it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;Elm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onReady&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;storedState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;elm-todo-save&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;startingState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;storedState&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;storedState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Elm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;startingState&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;elm-todo-save&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;We can now compile some Elm apps to WebAssembly, including the &lt;a href="https://brian-carroll.github.io/elm_c_wasm/todo-mvc/"&gt;TodoMVC demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are some architecture issues to work out, there's no performance work done yet, and there's lots of kernel code unwritten.&lt;/p&gt;

&lt;p&gt;Wasm &lt;em&gt;enables&lt;/em&gt; UTF-8 but it's a separate project&lt;/p&gt;

&lt;p&gt;There are some changes in the setup API due to async compilation&lt;/p&gt;

</description>
      <category>elm</category>
      <category>webassembly</category>
      <category>compiler</category>
    </item>
    <item>
      <title>Elm functions in WebAssembly</title>
      <dc:creator>Brian Carroll</dc:creator>
      <pubDate>Sat, 28 Jul 2018 20:15:18 +0000</pubDate>
      <link>https://forem.com/briancarroll/elm-functions-in-webassembly-50ak</link>
      <guid>https://forem.com/briancarroll/elm-functions-in-webassembly-50ak</guid>
      <description>&lt;p&gt;I’ve been pretty fascinated for the past few months with trying to understand how the &lt;a href="http://elm-lang.org/"&gt;Elm&lt;/a&gt; compiler might be able to target &lt;a href="https://webassembly.org/"&gt;WebAssembly&lt;/a&gt; in the future. What are the major differences from generating JavaScript? What are the hard parts, what approaches would make sense?&lt;/p&gt;

&lt;p&gt;I think one of the most interesting questions is: how do you implement first-class functions in WebAssembly? JavaScript has them built in, but WebAssembly doesn’t. Treating functions as values is a pretty high level of abstraction, and WebAssembly is a very low-level language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Contents&lt;/strong&gt;&lt;br&gt;
Elm and WebAssembly&lt;br&gt;
Elm’s first-class functions&lt;br&gt;
Key WebAssembly concepts&lt;br&gt;
Representing closures as bytes&lt;br&gt;
Function application&lt;br&gt;
Lexical closure&lt;br&gt;
Code generation&lt;br&gt;
Summary&lt;br&gt;
What’s next?&lt;br&gt;
References&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h2&gt;
  
  
  Elm and WebAssembly
&lt;/h2&gt;

&lt;p&gt;Before we get started, I just want to mention that from what I’ve heard from the core team, there is a general expectation that Elm will compile to WebAssembly some day, but currently no concrete plan. WebAssembly is still an MVP and won’t really be ready for Elm until it has garbage collection, and probably also access to the DOM and other Web APIs. The &lt;a href="https://github.com/WebAssembly/gc/blob/master/proposals/gc/Overview.md"&gt;GC extension&lt;/a&gt; is still in &lt;a href="https://github.com/WebAssembly/design/issues/1079"&gt;"feature proposal"&lt;/a&gt; stage so it'll be quite a while before it's available.&lt;/p&gt;

&lt;p&gt;But... it will get released at some point, and WebAssembly is one of the suggested &lt;a href="https://github.com/elm/projects#explore-webassembly"&gt;research projects&lt;/a&gt; for community members, and well, it's just really interesting! So let’s have a think about what Elm in WebAssembly could look like!&lt;/p&gt;

&lt;p&gt;Now... how do you go about implementing first-class functions in a low-level language like WebAssembly? WebAssembly is all just low-level machine instructions, and machine instructions aren’t something you can "pass around"! And what about partial function application? And isn’t there something about "closing over" values from outside the function scope?&lt;/p&gt;

&lt;p&gt;Let’s break this down.&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h2&gt;
  
  
  Elm’s first-class functions
&lt;/h2&gt;

&lt;p&gt;Let's start by looking at some example Elm code, then list all the features of Elm functions that we’ll need to implement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="kt"&gt;ElmFunctionsDemo&lt;/span&gt; &lt;span class="k"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;outerFunc&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;outerFunc&lt;/span&gt; &lt;span class="n"&gt;closedOver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt;
        &lt;span class="n"&gt;innerFunc&lt;/span&gt; &lt;span class="n"&gt;arg1&lt;/span&gt; &lt;span class="n"&gt;arg2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
            &lt;span class="n"&gt;closedOver&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;arg1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;arg2&lt;/span&gt;
    &lt;span class="k"&gt;in&lt;/span&gt;
        &lt;span class="n"&gt;innerFunc&lt;/span&gt;

&lt;span class="n"&gt;myClosure&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;myClosure&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;outerFunc&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="n"&gt;curried&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;curried&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;myClosure&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="n"&gt;higherOrder&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;higherOrder&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

&lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;higherOrder&lt;/span&gt; &lt;span class="n"&gt;curried&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case you're wondering, the &lt;code&gt;answer&lt;/code&gt; is 1+2+3=6. This is definitely not the simplest way to write this calculation, but it does illustrate all the most important features of Elm functions!&lt;/p&gt;

&lt;h3&gt;
  
  
  Three key features
&lt;/h3&gt;

&lt;p&gt;Firstly, Elm functions are first-class, meaning they are &lt;em&gt;values&lt;/em&gt; that can be returned from other functions (like &lt;code&gt;outerFunc&lt;/code&gt;) and passed into other functions (like &lt;code&gt;higherOrder&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Secondly, they support &lt;em&gt;lexical closure&lt;/em&gt;. &lt;code&gt;innerFunc&lt;/code&gt; "captures" a value from it's parent's scope, called &lt;code&gt;closedOver&lt;/code&gt;. This means that &lt;code&gt;myClosure&lt;/code&gt; "remembers" the value of &lt;code&gt;closedOver&lt;/code&gt; that it was created with, which in this case is &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, Elm functions support &lt;em&gt;partial application&lt;/em&gt;. &lt;code&gt;myClosure&lt;/code&gt; is a function that takes two arguments, but the body of &lt;code&gt;curried&lt;/code&gt;, we only apply one argument to it. As a result, we get a new function that is waiting for one more argument before it can actually run. This new function "remembers" the value that was partially applied, as well as the closed-over value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clues in the code
&lt;/h3&gt;

&lt;p&gt;Note that we now have several Elm functions that will all will end up executing the &lt;em&gt;same line of code&lt;/em&gt; when they actually get executed! That's this expression:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;closedOver + arg1 + arg2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If somebody calls &lt;code&gt;curried&lt;/code&gt; with one more argument, this is the expression that will calculate the return value. Same thing if somebody calls &lt;code&gt;myClosure&lt;/code&gt; with two arguments.&lt;/p&gt;

&lt;p&gt;This gives us a clue how to start implementing this. All of the function values we’re passing around will need to have a &lt;em&gt;reference&lt;/em&gt; to the same WebAssembly function, which evaluates the body expression.&lt;/p&gt;

&lt;p&gt;In WebAssembly, we can’t pass functions around, only data. But maybe we can create a data structure that &lt;em&gt;represents&lt;/em&gt; an Elm function value, keeping track of the curried arguments and closed-over values. When we finally have all the arguments and we’re ready to evaluate the body expression, we can execute a WebAssembly function to produce a return value.&lt;/p&gt;

&lt;p&gt;There are still lots of details missing at this stage. In order to fill in the gaps, we’re going to need a bit of background knowledge on some of WebAssembly’s language features.&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h2&gt;
  
  
  Key WebAssembly concepts
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Linear memory
&lt;/h3&gt;

&lt;p&gt;WebAssembly modules have access to a block of "linear memory" that they can use to store and load data. It’s a linear array of bytes, indexed by a 32-bit integer. WebAssembly has built-in instructions to store and load integers and floats, but anything more complex has to be built up from raw bytes.&lt;/p&gt;

&lt;p&gt;The fact that everything is built up from raw bytes means that WebAssembly can be a compile target for lots of different languages. Different data structures will make sense for different languages, but they’re all just bytes in the end. It’s up to each compiler and runtime to define how those bytes are manipulated.&lt;/p&gt;
&lt;h3&gt;
  
  
  Tables
&lt;/h3&gt;

&lt;p&gt;WebAssembly has a feature called "tables" which it uses to implement "indirect calls". Indirect calls are a feature of almost every high-level language, but what are they?&lt;/p&gt;

&lt;p&gt;When a machine executes a function call, it obviously needs some reference to know which function to invoke. In a &lt;em&gt;direct call&lt;/em&gt;, that function reference is simply hardcoded, so it invokes the same function every time. In an &lt;em&gt;indirect call&lt;/em&gt;, however, the function reference is provided by a runtime value instead. This is a very handy thing to be able to do, because it means the caller doesn’t need to know in advance the full list of functions it might have to call. Because of this, most languages have some version of this. C and C++ have function pointers, Java has class-based polymorphism, and Elm has first-class functions.&lt;/p&gt;

&lt;p&gt;A WebAssembly &lt;em&gt;table&lt;/em&gt; is an array of functions, each indexed by a 32-bit integer. There’s a special &lt;code&gt;call_indirect&lt;/code&gt; instruction that takes the index of the function to be called, with a list of arguments, and executes it. The program statically declares which functions are &lt;em&gt;elements&lt;/em&gt; of the table, and &lt;code&gt;call_indirect&lt;/code&gt; only works on those functions. (Incidentally, there’s also a &lt;code&gt;call&lt;/code&gt; instruction for direct calls, but we won’t be focusing on that too much for now.)&lt;/p&gt;

&lt;p&gt;By the way, WebAssembly has this design for safety reasons. If functions were stored in linear memory, it would be possible for code to inspect or corrupt other code, which is not good for web security. But with an indexed function table, that’s impossible. The only instruction that can even access the table is &lt;code&gt;call_indirect&lt;/code&gt;, which is safe.&lt;/p&gt;

&lt;p&gt;If you’re interested in some further reading, I recommend Mozilla’s article on &lt;a href="https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format"&gt;Understanding the Text Format&lt;/a&gt;, and the design document on &lt;a href="https://github.com/WebAssembly/design/blob/master/Semantics.md"&gt;WebAssembly Semantics&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But for now, we already have enough knowledge to discuss how to implement first-class functions.&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h2&gt;
  
  
  Representing closures as bytes
&lt;/h2&gt;

&lt;p&gt;As mentioned earlier, to represent an Elm function in WebAssembly we’ll need a function and a data structure. We’ll use the term "closure" to refer to the data structure, and "evaluator function" to refer to the WebAssembly function that will evaluate the body expression and produce a return value.&lt;/p&gt;

&lt;p&gt;One way of representing a closure in binary is the following, where each box represents an integer (4 bytes).&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;fn_index&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;arity&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;mem_ptr0&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;mem_ptr1&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;mem_ptr2&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;...&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;fn_index&lt;/code&gt;&lt;/strong&gt; is an integer index into the function table where the evaluator function for this closure can be found. At runtime, once all of the arguments have been applied to the closure, we can invoke the &lt;code&gt;call_indirect&lt;/code&gt; instruction to look up the table, call the evaluator function, and return a result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;arity&lt;/code&gt;&lt;/strong&gt; is the &lt;em&gt;remaining&lt;/em&gt; number of parameters to be applied to the closure. Every time we apply another argument, we insert a pointer to that argument, and decrement the arity. When it reaches zero, we’re ready to call the evaluator function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;mem_ptr*&lt;/code&gt;&lt;/strong&gt; are pointers representing the addresses in linear memory of the arguments and closed-over values. They all start off "empty" (zero), and are filled in reverse order as arguments are applied. So if the closure has an arity of 2, then &lt;code&gt;mem_ptr0&lt;/code&gt; and &lt;code&gt;mem_ptr1&lt;/code&gt; will be "empty". When we apply the next argument, the &lt;code&gt;mem_ptr1&lt;/code&gt; will be filled with the address of the argument value, and &lt;code&gt;arity&lt;/code&gt; will be decremented from 2 to 1, with &lt;code&gt;mem_ptr0&lt;/code&gt; still being empty.&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h2&gt;
  
  
  Function application
&lt;/h2&gt;

&lt;p&gt;We’ve already mentioned some of the things that need to happen when a closure is applied to some arguments, but here's the algorithm in full:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make a new copy of the closure&lt;/li&gt;
&lt;li&gt;For each applied argument

&lt;ul&gt;
&lt;li&gt;Let &lt;code&gt;a&lt;/code&gt; be the remaining arity of the closure&lt;/li&gt;
&lt;li&gt;Write the address of the argument into the &lt;code&gt;mem_ptr&lt;/code&gt; at position &lt;code&gt;a-1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Decrement the arity &lt;code&gt;a&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If remaining arity is greater than 0

&lt;ul&gt;
&lt;li&gt;return the new closure&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;else

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;call_indirect&lt;/code&gt; to execute the function referenced by &lt;code&gt;func_index&lt;/code&gt;, passing the closure as its argument&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's work through an example, applying two arguments to a closure of arity 2.&lt;/p&gt;

&lt;p&gt;Here's what the data structure looks like before we apply any arguments. All of the pointers are set to zero (the &lt;code&gt;null&lt;/code&gt; pointer).&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;fn_index&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;arity&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;mem_ptr0&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;mem_ptr1&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;null&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;null&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Before applying the closure, we need to create a new copy of it, so that the old closure is still available for other code to use. All Elm values are immutable, and the closure is no exception.&lt;/p&gt;

&lt;p&gt;Now let's apply an argument, &lt;code&gt;arg0&lt;/code&gt;. Our algorithm says that for arity &lt;code&gt;2&lt;/code&gt;, we should put the argument address into the &lt;code&gt;mem_ptr&lt;/code&gt; at position &lt;code&gt;2-1=1&lt;/code&gt;. In other words, &lt;code&gt;mem_ptr1&lt;/code&gt;. Let's see what that looks like.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;fn_index&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;arity&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;mem_ptr0&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;mem_ptr1&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;null&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;arg0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Notice that we're filling the argument pointers in reverse. This is just an efficiency trick. If we filled them in ascending order, we'd need to know how many had already been applied so that we could skip over them and put the next argument in the next free position. That information would have to be stored as an extra field in the closure, taking up extra space.&lt;/p&gt;

&lt;p&gt;But if we fill the arguments in reverse, we only need to know the current arity. If the current arity is 2 then the first two positions are free, regardless of whether this is a simple two-parameter function, or a five-parameter function that has already had 3 other arguments applied.&lt;/p&gt;

&lt;p&gt;Let's apply one more argument, &lt;code&gt;arg1&lt;/code&gt;. As before, we'll put the address of the argument into the highest available &lt;code&gt;mem_ptr&lt;/code&gt;, which is &lt;code&gt;mem_ptr0&lt;/code&gt;, and decrement the arity.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;fn_index&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;arity&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;mem_ptr0&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;mem_ptr1&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;arg1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;arg0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Having applied all of the arguments we've got, we check the remaining arity. If it's non-zero, this must be a partial application, and we can just return the closure. But if it’s zero, that means all arguments have been applied. In that case, it's time to call the evaluator function, and return the value it gives us.&lt;/p&gt;

&lt;p&gt;Note that the evaluator function takes the closure structure as its only argument. It contains all of the necessary data, because that’s exactly what it was designed for!&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h2&gt;
  
  
  Lexical closure
&lt;/h2&gt;

&lt;p&gt;Let’s look again at our example of closing over values from an outer scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;outerFunc&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;outerFunc&lt;/span&gt; &lt;span class="n"&gt;closedOver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt;
        &lt;span class="n"&gt;innerFunc&lt;/span&gt; &lt;span class="n"&gt;arg1&lt;/span&gt; &lt;span class="n"&gt;arg2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
            &lt;span class="n"&gt;closedOver&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;arg1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;arg2&lt;/span&gt;
    &lt;span class="k"&gt;in&lt;/span&gt;
        &lt;span class="n"&gt;innerFunc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To help us think about how to generate WebAssembly for &lt;code&gt;innerFunc&lt;/code&gt;, let’s first refactor the source code to the equivalent version below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;outerFunc&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;outerFunc&lt;/span&gt; &lt;span class="n"&gt;closedOver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt;
        &lt;span class="c1"&gt;-- Replace inner function definition with partial application&lt;/span&gt;
        &lt;span class="n"&gt;innerFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
            &lt;span class="n"&gt;transformedInnerFunc&lt;/span&gt; &lt;span class="n"&gt;closedOver&lt;/span&gt;
    &lt;span class="k"&gt;in&lt;/span&gt;
        &lt;span class="n"&gt;innerFunc&lt;/span&gt;


&lt;span class="c1"&gt;-- Move definition to top level, inserting a new first argument&lt;/span&gt;
&lt;span class="n"&gt;transformedInnerFunc&lt;/span&gt; &lt;span class="n"&gt;closedOver&lt;/span&gt; &lt;span class="n"&gt;arg1&lt;/span&gt; &lt;span class="n"&gt;arg2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;closedOver&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;arg1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;arg2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we’ve moved the definition of the inner function to the top level, and inserted &lt;code&gt;closedOver&lt;/code&gt; as a new first argument, instead of actually closing over it. This doesn’t make any difference to anyone who calls &lt;code&gt;outerFunc&lt;/code&gt; - it still creates an &lt;code&gt;innerFunc&lt;/code&gt; that remembers the value of &lt;code&gt;closedOver&lt;/code&gt; it was created with.&lt;/p&gt;

&lt;p&gt;The big win here is that we no longer have nested function definitions. Instead, they’re all defined at top level. This is useful because we need to put all of our evaluator functions into one global WebAssembly function table. Remember, the table is WebAssembly’s way of supporting indirect function calls. So we’ll need the compiler to do this transformation on all nested function definitions.&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h2&gt;
  
  
  Code generation
&lt;/h2&gt;

&lt;p&gt;We’re now ready to look at the steps the compiler needs to take to generate code for an Elm function.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Generate the body expression, keeping track of all of the &lt;em&gt;local names&lt;/em&gt; referenced in the body (we can ignore top-level names).&lt;/li&gt;
&lt;li&gt; From the set of local names, remove the argument names and any names defined &lt;code&gt;let&lt;/code&gt; subexpressions. Only the closed-over names will remain.&lt;/li&gt;
&lt;li&gt; Prepend the list of the closed-over names to the list of function arguments, to get the argument list for the evaluator function.&lt;/li&gt;
&lt;li&gt; Generate the evaluator function&lt;/li&gt;
&lt;li&gt; Declare the evaluator function as an element of the function table&lt;/li&gt;
&lt;li&gt; Insert code into the parent scope that does the following

&lt;ul&gt;
&lt;li&gt;Create a new closure structure in memory&lt;/li&gt;
&lt;li&gt;Partially apply the closed-over values from the parent scope&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;At the top of the post, we started by noting that one of the interesting challenges in compiling Elm to WebAssembly is how to implement first-class functions.&lt;/p&gt;

&lt;p&gt;Elm functions have a lot of advanced features that are not directly available in WebAssembly. They behave like values, they can be partially applied, and they can capture values from outer scopes.&lt;/p&gt;

&lt;p&gt;Although WebAssembly doesn’t have these features natively, it does provide the foundations to build them. WebAssembly supports indirect function calls using a function table, allowing us to pass around &lt;em&gt;references&lt;/em&gt; to WebAssembly functions in the form of a table index.&lt;/p&gt;

&lt;p&gt;We can represent an Elm function using a WebAssembly function and a data structure. We saw what the byte level representation of the data structure could look like. The data structure is what gets passed around the program, keeping track of partially-applied arguments and closed-over values. It also contains the table index of the evaluator function, which is what will eventually produce a return value.&lt;/p&gt;

&lt;p&gt;We discussed a way to implement lexical closure. It involves automatically transforming Elm code, flattening nested function definitions so that they can be inserted into the WebAssembly function table. This transformation turns lexical closure into partial function application.&lt;/p&gt;

&lt;p&gt;Finally we outlined some of the steps the compiler’s code generator needs to take, and looked at the runtime algorithm for function application.&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h2&gt;
  
  
  What’s next?
&lt;/h2&gt;

&lt;p&gt;I’m working on a prototype code generator to prove out these ideas. I’m making reasonable progress, and there don’t appear to be any major blockers, but it needs some more work to get it working. I’ll probably share something more if/when I get that far!&lt;/p&gt;

&lt;p&gt;I’ve also got some ideas for more blog posts around the topic of Elm in WebAssembly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Byte-level representations of the other Elm data structures (Extensible records, union types, numbers, comparables, appendables...)&lt;/li&gt;
&lt;li&gt;Code generation architecture (WebAssembly AST, Is it reasonable to generate Wasm from Haskell? What about Rust?)&lt;/li&gt;
&lt;li&gt;The Elm runtime in WebAssembly (Platform, Scheduler, Task, Process, Effect Managers...)&lt;/li&gt;
&lt;li&gt;DOM, HTTP, and ports. Differences between Wasm MVP and post-MVP.&lt;/li&gt;
&lt;li&gt;Strings and Unicode&lt;/li&gt;
&lt;li&gt;Tail-Call Elimination with trampolines&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Let me know in the comments if you’d like to see any of these!&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/HeapObjects#FunctionClosures"&gt;Haskell's implementation of closures&lt;/a&gt;&lt;br&gt;
&lt;a href="https://en.wikipedia.org/wiki/Closure_(computer_programming)#Implementation_and_theory"&gt;Wikipedia Closure article&lt;/a&gt;&lt;br&gt;
&lt;a href="https://en.wikipedia.org/wiki/Lambda_lifting"&gt;Wikipedia Lambda Lifting article&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>elm</category>
      <category>webassembly</category>
      <category>compiler</category>
    </item>
  </channel>
</rss>
