libatsopt

85 views
Skip to first unread message

gmhwxi

unread,
Oct 2, 2015, 10:49:48 PM10/2/15
to ats-lang-users

Please find some instructions on building libatsopt, that is, the
library version of patsopt:

https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt

For a bit of fun, here is a short program for building a compiler for ATS2
based on libatsopt:

https://github.com/githwxi/ATS-Postiats/blob/master/utils/libatsopt/TEST/patsopt.dats

Will Blair has already succeeded in build a JS version of libatsopt. This means we
can have a compiler for ATS2 that runs entirely in the browser. I actually tried such
a compiler built by him yesterday :)

gmhwxi

unread,
Oct 20, 2015, 2:57:54 PM10/20/15
to ats-lang-users

Following an example by Will Blair, I managed to use Emscripten
to compile libatsopt; I then generated a JS version of patsopt that
can be used to compile ATS2/prelude. So far I have been using node.js
to execute the JS version of patsopt. In the very near future, I shall be
able to generate another version that can be invoked inside a browser.

See:

https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS

Cheers!

H Zhang

unread,
Oct 21, 2015, 1:32:53 AM10/21/15
to ats-lang-users
So one will soon be able to write ATS and compile to JS and run it all in the browser? I think that is potentially really big for expanding ATS' audience. Compiling to a GC'ed VM means at a minimum one can start using ATS like a flavored ML. While there are many languages transpiling to js ATS may be able to really stand out on code efficiency? As I understand it ATS is able to compile recursive calls to loops. I think transpilers like Elm and SMLtoJs still don't have TCO yet.

Haitao

gmhwxi

unread,
Oct 21, 2015, 8:15:46 AM10/21/15
to ats-lang-users
Note that Emscripten itself is not yet compiled to JS.

To do "everything" in the browser, one can take the following route:

Use patsopt(JS) to compile ATS source into C and then use atscc2js(JS)
to compile the generated C code into JS, where patsopt(JS) and atscc2js(JS)
are Emscripten-generated JS.

By the way, it should not be so hard to compile ATS to languages like Elm and SML :)

H Zhang

unread,
Oct 21, 2015, 4:30:46 PM10/21/15
to ats-lang-users
It seems to me that an immediate application of this is to have a flycheck style syntax error highlighter either directly in the browser or in an Electron app. I had very good impression of Lean Prover's online tutorial. The source repo is at https://github.com/leanprover/tutorial Looks like they use the Ace editor. They use Ace's setAnnotation to do flycheck style highlighting. They also use a pre-generated dictionary for autocompletion. These all seem pretty readily achievable for ATS as well.


On Tuesday, October 20, 2015 at 11:57:54 AM UTC-7, gmhwxi wrote:

gmhwxi

unread,
Oct 24, 2015, 10:20:37 PM10/24/15
to ats-lang-users

Here is a very primitive version:

https://ats-lang.github.io/EXPERIMENT/test_libatsopt/test_libatsopt.html

I hope someone would be willing to step up to the challenge of using libatsopt(JS) to
build something interesting.

H Zhang

unread,
Oct 25, 2015, 8:07:34 PM10/25/15
to ats-lang-users
This is really cool! It runs very snappy. I was thinking that one way to fix the memory leak is to just reclaim all allocations after a run since the compiler runs in batch mode. Looks like you have solved the memory problem.

This looks like it could be used to create some interesting IDE features to aid the learning of ATS:

1) Correlate syntax errors back to the source code;
2) Correlate source code to highlight corresponding generated C code

Just off the top of my head.

gmhwxi

unread,
Oct 25, 2015, 10:41:08 PM10/25/15
to ats-lang-users

I have yet to address the memory issue. For now, each call to patsopt
leaks a few million bytes of memory. If one wants to call patsopt a large number
of times (e.g., 100 times), one may have to reload the page to start again.

I spent two days trying to figure out how to quit from a call to 'main' in Emscripten
generated JS code and then restart it. I gave up at the end. Now I will try to use a WebWorker
to run patsopt and then terminate the WebWorker after compilation finishes. This is essentially
what Will Blair did. Hopefully, this approach will work out.

H Zhang

unread,
Oct 26, 2015, 1:56:11 PM10/26/15
to ats-lang-users
So emscripten_force_exit didn't work? Or restart it was the problem? Web worker sounds like the right way to go. Another possibility is to use dlmalloc's mspace (arena allocator). You can create_mspace a block of memory, call mspace_malloc, mspace_free etc with the additional mspace pointer, and destroy_mspace at the end to release all the memory at once. From http://gee.cs.oswego.edu/pub/misc/malloc.h

/*
  mspace is an opaque type representing an independent
  region of space that supports mspace_malloc, etc.
*/
typedef void* mspace;

/*
  create_mspace creates and returns a new independent space with the
  given initial capacity, or, if 0, the default granularity size.  It
  returns null if there is no system memory available to create the
  space.  If argument locked is non-zero, the space uses a separate
  lock to control access. The capacity of the space will grow
  dynamically as needed to service mspace_malloc requests.  You can
  control the sizes of incremental increases of this space by
  compiling with a different DEFAULT_GRANULARITY or dynamically
  setting with mallopt(M_GRANULARITY, value).
*/
mspace create_mspace(size_t capacity, int locked);

/*
  destroy_mspace destroys the given space, and attempts to return all
  of its memory back to the system, returning the total number of
  bytes freed. After destruction, the results of access to all memory
  used by the space become undefined.
*/
size_t destroy_mspace(mspace msp);

gmhwxi

unread,
Oct 26, 2015, 4:35:48 PM10/26/15
to ats-lang-users

emscripten_force_exit works. After exiting from 'main', calling 'main' again causes
many timing-related issues. I did make it work after modifying Emscripten-generated
code, but I did not feel I should continue that way. It is not like my life being dependent
on it :)

gmhwxi

unread,
Oct 27, 2015, 2:38:42 PM10/27/15
to ats-lang-users

>>Web worker sounds like the right way to go

Just found out that the picture is less rosy.

Chromium uses a very limited call stack for each worker. Right now, compiling a program
insider a worker can often (but not always) cause the call stack being exhausted. Firefox
seems to fare a lot better in this regard. Anyway, I think what I have got so far is sufficient
for building some teaching stuff for ATS. Here is a webworker-based version:

http://ats-lang.github.io/EXPERIMENT/test_libatsopt/wktest_libatsopt.html

H Zhang

unread,
Oct 29, 2015, 10:42:44 PM10/29/15
to ats-lang-users
Well in that case the arena based allocator might be an easy alternative. Since you only need a single arena to reclaim the memory, you could just use a global variable for the arena pointer to be initialized at the top of the main and destroyed at the end. The malloc and free can be replaced with the macros that call mspace_malloc and mspace_free instead with the global mspace pointer threaded in. You don't have to change any existing code. I think Emscripten by default uses dlmalloc so hopefully compatibility isn't a problem. https://github.com/kripken/emscripten/blob/master/system/lib/dlmalloc.c

gmhwxi

unread,
Nov 6, 2015, 7:58:35 PM11/6/15
to ats-lang-users

I just noticed that running 2 pieces of Emscripten generated JS code could cause a
conflict between them. In my case, I wanted to run Patsopt and Atscc2js, but my attempt
failed for reasons unclear to me. Reality is always more fascinating than one's plan :)

Running Atscc2js inside a webworker is fine, though.
Reply all
Reply to author
Forward
0 new messages