Looking for Examples using WASM Backend

As the title suggests, I’m looking for repos that showcase GHC’s WASM capabilities that I can clone and play with. I have looked at Glasgow Haskell Compiler / ghc-wasm-meta · GitLab which is a good start, but it’s short on end to end demos.

Particularly I am looking for things which highlight:

  1. Running the wasm program from within a host program, either in Javascript or Rust
  2. Compiling pure top-level functions (i.e. something other than a self contained main :: IO ()) which is used as FFI in the host program.
  3. Anything which exhibits how Integer/Natural are dealt with in GHC 9+, and how you get around this if you’re using wasm32.

I don’t really care if it’s run in the browser or using e.g. node, either are welcome.

My actual use case (in case it matters) is that I want to be able to execute the same pure function in both Haskell and ultimately inside a rust program or in the browser. The function has type something like [Integer] -> [Integer] and only depends on base and containers (i.e. nothing eccentric).

thanks! :pray:

5 Likes

Ormolu Live (https://github.com/tweag/ormolu/tree/master/ormolu-live) and Fourmolu Live (https://github.com/fourmolu/fourmolu/tree/main/web) are examples of exposing a Text -> Text (somewhat simplified) function from WASM and using it in a Purescript/Javascript app.

3 Likes

My Brassica compiles a library to WASM. You can find details in BUILDING.md and the last paragraph of ARCHITECTURE.md, and the code under gui/brassica-interop-wasm and gui/brassica-web.

4 Likes

wasm aside, this is a really interesting project!

1 Like

I will, incidentally, note that I used Ormolu and Formolu as references when making Brassica. So those are probably the best places to start.

Proof of concept of compiling Haskell to Wasm to run on Fastly’s compute engine:

4 Likes

I have made a couple of simple demo Web apps that are deployed to GitHub pages. They are meant to be starting points of making games using Yampa library (though the first of the following examples does not really need Yampa). The game step function is called from JavaScript, which performs the logical step which in turn calls wrapped JS Canvas functions to perform drawing via FFI.

Here is one of the examples: GitHub - AntanasKal/yampa-wasm-example: Example of using Yampa and GHC Wasm backend to create a simple web app.
Here it is deployed to GitHub pages: Yampa Wasm example
This example contains a Haskell function reverseCharArray that takes an input array, manipulates it and returns (playing according to the linear memory model of Wasm by doing some manual memory management) and it can be called from JS.

4 Likes

This is not really the the use case that you described, but there is also a work-in-progress fork of Space Invaders game from Yampa Arcade paper ported to Wasm. It was done to check if it is viable to make at least simple Wasm games in Haskell: GitHub - AntanasKal/SpaceInvaders: A Haskell game written in Yampa, ported to Wasm
Here it is deployed to GitHub pages: SPACE INVADERS

5 Likes