Haskell to JavaScript translation test

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]

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

Useful links

HTML Last modified: Thu Oct 10 14:22:20 CEST 2013
TH