Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add New Guide: Compiling Haskell to WebAssembly #36849

Closed
wants to merge 2 commits into from
Closed

Conversation

tusharad
Copy link

Description

This PR introduces a new guide titled Compiling Haskell to WebAssembly under the WebAssembly section. The guide provides step-by-step instructions on how to set up the environment, compile Haskell code into WebAssembly, and integrate it into a web application. Additionally, it covers compiling a Haskell Cabal project for WebAssembly and handling data types such as strings.

Motivation

This guide aims to broaden the range of languages covered in the WebAssembly section on MDN, providing valuable resources for Haskell developers interested in WebAssembly.

Additional details

Related issues and pull requests

Question for Reviewers:

Before proceeding further, I'd like to confirm whether new content of this nature is allowed in the MDN documentation. If there are any specific guidelines or prerequisites for contributing new topics, please let me know, and I'll adjust accordingly.

Looking forward to your feedback!

@tusharad tusharad requested review from a team as code owners November 18, 2024 17:43
@tusharad tusharad requested review from chrisdavidmills and removed request for a team November 18, 2024 17:43
@github-actions github-actions bot added Content:wasm WebAssembly docs size/m [PR only] 51-500 LoC changed labels Nov 18, 2024

{{WebAssemblySidebar}}

# Compiling Haskell to WebAssembly: A Step-by-Step Guide
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[markdownlint] reported by reviewdog 🐶
MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "Compiling Haskell to WebAssemb..."]

@Josh-Cena
Copy link
Member

As much as I love Haskell, I don't think it would be maintainable as I doubt others on the team know Haskell and in particular compiling it to Wasm. Is it possible to make this a blog post and link to it in the "see also" instead?


### Installing the Custom GHC for WebAssembly

To compile Haskell to WebAssembly, you need a custom GHC version that supports the `wasm32-wasi` target. Detailed installation instructions can be found in the [GHC WebAssembly meta repository](https://gitlab.haskell.org/ghc/ghc-wasm-meta). Here’s a quick setup guide:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
To compile Haskell to WebAssembly, you need a custom GHC version that supports the `wasm32-wasi` target. Detailed installation instructions can be found in the [GHC WebAssembly meta repository](https://gitlab.haskell.org/ghc/ghc-wasm-meta). Heres a quick setup guide:
To compile Haskell to WebAssembly, you need a custom GHC version that supports the `wasm32-wasi` target. Detailed installation instructions can be found in the [GHC WebAssembly meta repository](https://gitlab.haskell.org/ghc/ghc-wasm-meta). Here's a quick setup guide:


To compile Haskell to WebAssembly, you need a custom GHC version that supports the `wasm32-wasi` target. Detailed installation instructions can be found in the [GHC WebAssembly meta repository](https://gitlab.haskell.org/ghc/ghc-wasm-meta). Here’s a quick setup guide:

1. **(Optional): Choose a GHC flavor.**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
1. **(Optional): Choose a GHC flavor.**
1. **(Optional): Choose a GHC flavor.**


1. **(Optional): Choose a GHC flavor.**
This determines the specific GHC version. For stability, we'll use GHC 9.8.3:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change

wasm32-wasi-cabal --version
```


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change


### Writing the Haskell Code

Let’s start with a simple Haskell function that takes an integer and adds 10:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
Lets start with a simple Haskell function that takes an integer and adds 10:
Let's start with a simple Haskell function that takes an integer and adds 10:

Comment on lines +223 to +224
inst.exports.freeBuffer(ptr);
inst.exports.freeBuffer(resultPtr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
inst.exports.freeBuffer(ptr);
inst.exports.freeBuffer(resultPtr);
inst.exports.freeBuffer(ptr);
inst.exports.freeBuffer(resultPtr);

Comment on lines +226 to +227
return result;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
return result;
}
return result;
}

Comment on lines +229 to +231
wasi.initialize(inst);
inst.exports.hs_init(0, 0);
console.log(callWithString(inst.exports.hs_sayHello, "John")); // Outputs: Hello, John
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
wasi.initialize(inst);
inst.exports.hs_init(0, 0);
console.log(callWithString(inst.exports.hs_sayHello, "John")); // Outputs: Hello, John
wasi.initialize(inst);
inst.exports.hs_init(0, 0);
console.log(callWithString(inst.exports.hs_sayHello, "John")); // Outputs: Hello, John

Comment on lines +237 to +238
Congratulations! You’ve successfully compiled Haskell to WebAssembly and integrated it into a web app. This guide covered the basics, but there’s much more to explore, including advanced data types and optimizations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
Congratulations! Youve successfully compiled Haskell to WebAssembly and integrated it into a web app. This guide covered the basics, but theres much more to explore, including advanced data types and optimizations.
Congratulations! You've successfully compiled Haskell to WebAssembly and integrated it into a web app. This guide covered the basics, but there's much more to explore, including advanced data types and optimizations.


- [GHC WebAssembly Meta Repository](https://gitlab.haskell.org/ghc/ghc-wasm-meta)
- [Ormolu live](https://github.com/tweag/ormolu/tree/master/ormolu-live)
- [Sql2er](https://github.com/tusharad/sql2er)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
- [Sql2er](https://github.com/tusharad/sql2er)
- [Sql2er](https://github.com/tusharad/sql2er)

@tusharad
Copy link
Author

As much as I love Haskell, I don't think it would be maintainable as I doubt others on the team know Haskell and in particular compiling it to Wasm. Is it possible to make this a blog post and link to it in the "see also" instead?

Sure @Josh-Cena, no problem! Closing the PR.

@tusharad tusharad closed this Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:wasm WebAssembly docs size/m [PR only] 51-500 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants