This page contains some examples of Haskell code runnng in your web browser!
The Haskell code has been converted to
JavaScript with an automatic translator. The translator handles the
Haskell 98 language, including key features like laziness,
higher order functions, type classes and overloading,
data types, pattern matching and list comprehensions.
The examples shown below are defined in Test0.hs.
The Prelude and other library modules are also part of the
translated code. Lots of unused library functions (dead code)
have been eliminated using slicing, reducing the size of the
JavaScript code from ~2MB to ~0.3MB.
The reason this translator was created was
to make it possible to run applications based on
GF (such as the
Minibar) offline, not just through communication with a server that runs GF.
To test how well the JavaScript
translation of the PGF library works and how well browers deal with the
size and complexity of the code, there are two variants of the Minibar:
Minibar Offline 1 and
Minibar Offline 2. They work best in
the Opera web browser, but at least
Minibar Offline 1 works also in browers based on WebKit (Safari, Chrome),
while browsers based on Gecko (Firefox) fail, probably because of a hardcoded
call stack depth limit.
Below are some simpler tests of the translator that works in most browers,
even on Android, iOS and Blackberry!
Laziness
First some examples that test the use of laziness to build
potentially infinite data structures. Use the
more buttons to
advance the lazy computations further.
numbers = 0:[n+1|n<-numbers]
primes = sieve [2..]
sieve (p:ns) = p:sieve [n|n<-ns,n `mod` p/=0]
- numbers = ...
- primes = ...
- primes3 = ...
Interactive tests of particular functions
fac :: Int->Int
fac 0 = 1
fac n = n * fac(n-1)
fib :: Integer->Integer
fib n = if n<2
then 1
else fib(n-1)+fib(n-2)
Arbitrary JavaScript
Here you can enter arbitrary JavaScript expressions. You can use the
Haskell functions that have been translated to JavaScript.
Example:
PreludeList_map.v(Test0_fac)(Test0_numbers)
Related work
- There is work in progress on a JavaScript backend
for the Timber compiler
by Johan Nordlander.
- YCR2JS, a Converter of Yhc Core to Javascript.
- There is work in progress on
A JavaScript backend for UHC.
- ghcjs:
a javascript backend for GHC.
- Not-yet-published Erlang to JavaScript compiler by Witold Baryluk.
- SMLtoJs:
Standard ML to JavaScript compiler and
SMLtoJs Prompt,
an entire Standard ML compiler hosted in a Web browser.
- Lambdascript,
a prototype language for strongly typed, lazy, purely
functional client side web scripting. It provides a basic subset of
Haskell's functionality, compiled into Javascript.
- JSHC, a
Haskell compiler implemented in JavaScript. It currently supports a
subset of Haskell 2010 and has a user interface based
on Ymacs
(Emacs-like editor in JavaScript) that lets users write Haskell program
and run them in a web page.
- haste,
a Haskell to Javascript compiler based on GHC. It is easy to install
haste-compiler
from Hackage.
- Fay is a subset of Haskell that
compiles to JavaScript.
- Elm is Haskell-like language
for functional reactive programming that compiles to JavaScript. You can
try Elm programming directly in your
browser.
Useful links