At ZuriHac 2024, Jane Street was giving out shirts with the following prompt on the back:
I was interested in doing something with GHC's WebAssembly (wasm) backend, so I decided to solve the problem above using a wasm-driven simulation in the browser.
Here it is in action:
All of the logic for this simulation is written in Haskell and compiled to wasm, including:
- Concurrent execution of each simulation
- Mutex protecting the HTML canvas rendering context
- Randomness using the random package
No libraries were used for interacting with the DOM, just raw JavaScript FFI through the wasm backend.
The ghc-wasm-meta README is a good resource for setting up a wasm cross compilation toolchain. The GHC team does supply official distributions of wasm cross compilers for Linux, but not other platforms (yet). On other platforms (e.g. macOS), you will need to build a cross compiler from source.
Once you have a toolchain set up, you can simply run the ./build.sh
file in
this repository and serve the dist
directory on a local file server.
- Finley McIlwaine (author)
- Andreas Klebinger (@AndreasPK)
- Zubin Duggal (@wz1000)
- ZuriHac 2024