Compiling to JavaScript with UHC

By Chris Done

Last week I took a crack at testing the JavaScript compilation backend supported by UHC. There don’t seem to be any instructions1, that I have been able to find, on working with the JS backend. But it’s more or less straight-forward to figure out.

As far as I understand quite a lot of what’s working now is thanks to Atze Dijkstra and Jurriën Stutterheim,2 so cheers to those!

Build and Install UHC

UPDATE 2012-04-23: The Git repo has moved to: git://

I used the UHC repository from Github:

$ mkdir uhcjs
$ cd uhcjs
$ git clone
$ cd uhc/EHC

GHC 7+ should work okay, I imagine.3

There is no INSTALL file and the README doesn’t mention installation, but the configure script is helpful.

$ ./configure

Have a look, see if it mentions that you need something installed.4 Personally, I had to install a few:

$ cabal install uuagc fgl EH-Util syb

Then I just ran make (-j runs n jobs and I have four cores):

$ make uhc -j 4
real	4m40.388s
user	6m19.628s
sys	0m12.053s

Takes about 5 minutes on a modern machine,5 so you probably want to run both make and make install (sans -j), which takes about 4 minutes:

$ make uhc -j 4 && sudo make install

And go get a cup of tea.

$ uhc --version
ehc-1.1.2, Revision exported

Now UHC is installed.

Get the JavaScript library

UPDATE 2012-02-16: Git repo below is currently not working due to rename from jscript → js, use git:// instead.

$ cd ../..
$ git clone git://

There is a Cabal file which does configure and build, but I couldn’t figure out how it was supposed to link up the JavaScript dependencies and generate a HTML file (that worked), but a direct call to uhc works.

First program

Go to the src directory:

$ cd uhc-jscript/uhc-jscript/src

Open up Main.hs and replace everything with something nice like:

import Language.UHC.JScript.Assorted
main = alert "Hello, Haskell!"

Then run:

$ uhc -tjs Main.hs

And open Main.html in your browser.

That’s about as far as I got. There’s a fairly substantial API already defined by Jurriën Stutterheim in src/, e.g. a HTML5 API and jQuery and AJAX. Enjoy hacking! It’s more complete than GHCJS, so it may be an interesting route to take for the Ludum Dare for making a browser-based game.

  1. Bar a short mention in the user manual.

  2. Check the paper on improving the UHC backend.

  3. I had to use GHC 7.0.4 as GHC 6.12.3 failed with a rather unsightly plethora of linker errors. So I enabled GHC 7.0.4:

    $ export PATH=/opt/ghc/7.0.4/bin/:$PATH
  4. For example:

      uuagc is available?     : yes
        version               : Attribute Grammar compil…
        command for uuagc     : /home/chris/.cabal/bin/uuagc
      ghc is available?       : yes
        version               : 6.12.3
        command for ghc       : /home/chris/Programs/bin/ghc
        command for ghc raw   : /home/chris/Programs/bin/ghc
          uulib is available? : yes
            version           : uulib-0.9.14
  5. Intel(R) Core(TM) i7 CPU M 620 (2.67GHz and 4096 KB cache size) x× 4.