# Libatsopt

**URL:** <https://discourse.ats-lang.org/t/libatsopt/470>\
**Category:** Google Group\
**Created:** [October 3, 2015, 2:49am UTC](https://discourse.ats-lang.org/t/libatsopt/470 "2015-10-03T02:49:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [October 3, 2015, 2:49am UTC](https://discourse.ats-lang.org/t/libatsopt/470/1 "2015-10-03T02:49:48Z")

</div>

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

> **[githwxi/ATS-Postiats](https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt)**
>
> ATS2: Unleashing the Potentials of Types and Templates - githwxi/ATS-Postiats

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 🙂

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [October 27, 2015, 6:38pm UTC](https://discourse.ats-lang.org/t/libatsopt/470/2 "2015-10-27T18:38:42Z")

</div>

> > 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.htmlOn](http://ats-lang.github.io/EXPERIMENT/test_libatsopt/wktest_libatsopt.htmlOn) Monday, October 26, 2015 at 1:56:11 PM UTC-4, H Zhang wrote:

> 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](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);
> 
> On Sunday, October 25, 2015 at 7:41:08 PM UTC-7, gmhwxi wrote:
> 
> > 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.
> > 
> > On Sunday, October 25, 2015 at 8:07:34 PM UTC-4, H Zhang wrote:
> > 
> > > 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.
> > > 
> > > On Saturday, October 24, 2015 at 7:20:37 PM UTC-7, gmhwxi wrote:
> > > 
> > > > Here is a very primitive version:
> > > > 
> > > > [test\_libatsopt](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.
> > > > 
> > > > On Wednesday, October 21, 2015 at 4:30:46 PM UTC-4, H Zhang wrote:
> > > > 
> > > > > 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 [GitHub - leanprover/tutorial: Lean Tutorials](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:
> > > > > 
> > > > > > 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](https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS)
> > > > > > 
> > > > > > Cheers!
> > > > > > 
> > > > > > On Friday, October 2, 2015 at 10:49:48 PM UTC-4, gmhwxi wrote:
> > > > > > 
> > > > > > > Please find some instructions on building libatsopt, that is, the  
> > > > > > > library version of patsopt:
> > > > > > > 
> > > > > > > [https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt](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](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 🙂

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [October 25, 2015, 2:20am UTC](https://discourse.ats-lang.org/t/libatsopt/470/3 "2015-10-25T02:20:36Z")

</div>

Here is a very primitive version:

[https://ats-lang.github.io/EXPERIMENT/test\_libatsopt/test\_libatsopt.html](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.On Wednesday, October 21, 2015 at 4:30:46 PM UTC-4, H Zhang wrote:

> 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 [GitHub - leanprover/tutorial: Lean Tutorials](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:
> 
> > 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](https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS)
> > 
> > Cheers!
> > 
> > On Friday, October 2, 2015 at 10:49:48 PM UTC-4, gmhwxi wrote:
> > 
> > > Please find some instructions on building libatsopt, that is, the  
> > > library version of patsopt:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt](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](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 🙂

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [October 26, 2015, 2:41am UTC](https://discourse.ats-lang.org/t/libatsopt/470/4 "2015-10-26T02:41:07Z")

</div>

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.On Sunday, October 25, 2015 at 8:07:34 PM UTC-4, H Zhang wrote:

> 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.
> 
> On Saturday, October 24, 2015 at 7:20:37 PM UTC-7, gmhwxi wrote:
> 
> > Here is a very primitive version:
> > 
> > [test\_libatsopt](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.
> > 
> > On Wednesday, October 21, 2015 at 4:30:46 PM UTC-4, H Zhang wrote:
> > 
> > > 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 [GitHub - leanprover/tutorial: Lean Tutorials](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:
> > > 
> > > > 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](https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS)
> > > > 
> > > > Cheers!
> > > > 
> > > > On Friday, October 2, 2015 at 10:49:48 PM UTC-4, gmhwxi wrote:
> > > > 
> > > > > Please find some instructions on building libatsopt, that is, the  
> > > > > library version of patsopt:
> > > > > 
> > > > > [https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt](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](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 🙂

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [October 20, 2015, 6:57pm UTC](https://discourse.ats-lang.org/t/libatsopt/470/5 "2015-10-20T18:57:54Z")

</div>

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>
>
> //github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS

Cheers!On Friday, October 2, 2015 at 10:49:48 PM UTC-4, gmhwxi wrote:

> Please find some instructions on building libatsopt, that is, the  
> library version of patsopt:
> 
> [https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt](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](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 🙂

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [October 26, 2015, 8:35pm UTC](https://discourse.ats-lang.org/t/libatsopt/470/6 "2015-10-26T20:35:48Z")

</div>

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 :)On Monday, October 26, 2015 at 1:56:11 PM UTC-4, H Zhang wrote:

> 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](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);
> 
> On Sunday, October 25, 2015 at 7:41:08 PM UTC-7, gmhwxi wrote:
> 
> > 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.
> > 
> > On Sunday, October 25, 2015 at 8:07:34 PM UTC-4, H Zhang wrote:
> > 
> > > 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.
> > > 
> > > On Saturday, October 24, 2015 at 7:20:37 PM UTC-7, gmhwxi wrote:
> > > 
> > > > Here is a very primitive version:
> > > > 
> > > > [test\_libatsopt](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.
> > > > 
> > > > On Wednesday, October 21, 2015 at 4:30:46 PM UTC-4, H Zhang wrote:
> > > > 
> > > > > 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 [GitHub - leanprover/tutorial: Lean Tutorials](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:
> > > > > 
> > > > > > 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](https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS)
> > > > > > 
> > > > > > Cheers!
> > > > > > 
> > > > > > On Friday, October 2, 2015 at 10:49:48 PM UTC-4, gmhwxi wrote:
> > > > > > 
> > > > > > > Please find some instructions on building libatsopt, that is, the  
> > > > > > > library version of patsopt:
> > > > > > > 
> > > > > > > [https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt](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](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 🙂

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [November 7, 2015, 12:58am UTC](https://discourse.ats-lang.org/t/libatsopt/470/7 "2015-11-07T00:58:34Z")

</div>

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.On Thursday, October 29, 2015 at 10:42:44 PM UTC-4, H Zhang wrote:

> 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](https://github.com/kripken/emscripten/blob/master/system/lib/dlmalloc.c)
> 
> On Tuesday, October 27, 2015 at 11:38:42 AM UTC-7, gmhwxi wrote:
> 
> > > > 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:
> > 
> > [wktest\_libatsopt](http://ats-lang.github.io/EXPERIMENT/test_libatsopt/wktest_libatsopt.html)
> > 
> > On Monday, October 26, 2015 at 1:56:11 PM UTC-4, H Zhang wrote:
> > 
> > > 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](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);
> > > 
> > > On Sunday, October 25, 2015 at 7:41:08 PM UTC-7, gmhwxi wrote:
> > > 
> > > > 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.
> > > > 
> > > > On Sunday, October 25, 2015 at 8:07:34 PM UTC-4, H Zhang wrote:
> > > > 
> > > > > 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.
> > > > > 
> > > > > On Saturday, October 24, 2015 at 7:20:37 PM UTC-7, gmhwxi wrote:
> > > > > 
> > > > > > Here is a very primitive version:
> > > > > > 
> > > > > > [test\_libatsopt](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.
> > > > > > 
> > > > > > On Wednesday, October 21, 2015 at 4:30:46 PM UTC-4, H Zhang wrote:
> > > > > > 
> > > > > > > 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  
> > > > > > > [GitHub - leanprover/tutorial: Lean Tutorials](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:
> > > > > > > 
> > > > > > > > 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](https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS)
> > > > > > > > 
> > > > > > > > Cheers!
> > > > > > > > 
> > > > > > > > On Friday, October 2, 2015 at 10:49:48 PM UTC-4, gmhwxi wrote:
> > > > > > > > 
> > > > > > > > > Please find some instructions on building libatsopt, that is, the  
> > > > > > > > > library version of patsopt:
> > > > > > > > > 
> > > > > > > > > [https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt](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](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 🙂

---

<div class="post-metadata">

**Author:** ![H\_Zhang](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@H\_Zhang](https://discourse.ats-lang.org/u/H_Zhang)\
**Post date:** [October 21, 2015, 5:32am UTC](https://discourse.ats-lang.org/t/libatsopt/470/8 "2015-10-21T05:32:52Z")

</div>

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.

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

> 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](https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS)
> 
> Cheers!
> 
> On Friday, October 2, 2015 at 10:49:48 PM UTC-4, gmhwxi wrote:
> 
> > Please find some instructions on building libatsopt, that is, the  
> > library version of patsopt:
> > 
> > [https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt](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](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 🙂

---

<div class="post-metadata">

**Author:** ![H\_Zhang](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@H\_Zhang](https://discourse.ats-lang.org/u/H_Zhang)\
**Post date:** [October 26, 2015, 5:56pm UTC](https://discourse.ats-lang.org/t/libatsopt/470/9 "2015-10-26T17:56:10Z")

</div>

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](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);On Sunday, October 25, 2015 at 7:41:08 PM UTC-7, gmhwxi wrote:

> 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.
> 
> On Sunday, October 25, 2015 at 8:07:34 PM UTC-4, H Zhang wrote:
> 
> > 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.
> > 
> > On Saturday, October 24, 2015 at 7:20:37 PM UTC-7, gmhwxi wrote:
> > 
> > > Here is a very primitive version:
> > > 
> > > [test\_libatsopt](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.
> > > 
> > > On Wednesday, October 21, 2015 at 4:30:46 PM UTC-4, H Zhang wrote:
> > > 
> > > > 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 [GitHub - leanprover/tutorial: Lean Tutorials](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:
> > > > 
> > > > > 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](https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS)
> > > > > 
> > > > > Cheers!
> > > > > 
> > > > > On Friday, October 2, 2015 at 10:49:48 PM UTC-4, gmhwxi wrote:
> > > > > 
> > > > > > Please find some instructions on building libatsopt, that is, the  
> > > > > > library version of patsopt:
> > > > > > 
> > > > > > [https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt](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](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 🙂

---

<div class="post-metadata">

**Author:** ![H\_Zhang](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@H\_Zhang](https://discourse.ats-lang.org/u/H_Zhang)\
**Post date:** [October 30, 2015, 2:42am UTC](https://discourse.ats-lang.org/t/libatsopt/470/10 "2015-10-30T02:42:44Z")

</div>

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.cOn](https://github.com/kripken/emscripten/blob/master/system/lib/dlmalloc.cOn) Tuesday, October 27, 2015 at 11:38:42 AM UTC-7, gmhwxi wrote:

> > > 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:
> 
> [wktest\_libatsopt](http://ats-lang.github.io/EXPERIMENT/test_libatsopt/wktest_libatsopt.html)
> 
> On Monday, October 26, 2015 at 1:56:11 PM UTC-4, H Zhang wrote:
> 
> > 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](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);
> > 
> > On Sunday, October 25, 2015 at 7:41:08 PM UTC-7, gmhwxi wrote:
> > 
> > > 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.
> > > 
> > > On Sunday, October 25, 2015 at 8:07:34 PM UTC-4, H Zhang wrote:
> > > 
> > > > 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.
> > > > 
> > > > On Saturday, October 24, 2015 at 7:20:37 PM UTC-7, gmhwxi wrote:
> > > > 
> > > > > Here is a very primitive version:
> > > > > 
> > > > > [test\_libatsopt](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.
> > > > > 
> > > > > On Wednesday, October 21, 2015 at 4:30:46 PM UTC-4, H Zhang wrote:
> > > > > 
> > > > > > 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  
> > > > > > [GitHub - leanprover/tutorial: Lean Tutorials](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:
> > > > > > 
> > > > > > > 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](https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS)
> > > > > > > 
> > > > > > > Cheers!
> > > > > > > 
> > > > > > > On Friday, October 2, 2015 at 10:49:48 PM UTC-4, gmhwxi wrote:
> > > > > > > 
> > > > > > > > Please find some instructions on building libatsopt, that is, the  
> > > > > > > > library version of patsopt:
> > > > > > > > 
> > > > > > > > [https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt](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](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 🙂

---

<div class="post-metadata">

**Author:** ![H\_Zhang](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@H\_Zhang](https://discourse.ats-lang.org/u/H_Zhang)\
**Post date:** [October 21, 2015, 8:30pm UTC](https://discourse.ats-lang.org/t/libatsopt/470/11 "2015-10-21T20:30:46Z")

</div>

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 [GitHub - leanprover/tutorial: Lean Tutorials](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:

> 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](https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS)
> 
> Cheers!
> 
> On Friday, October 2, 2015 at 10:49:48 PM UTC-4, gmhwxi wrote:
> 
> > Please find some instructions on building libatsopt, that is, the  
> > library version of patsopt:
> > 
> > [https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt](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](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 🙂

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [October 21, 2015, 12:15pm UTC](https://discourse.ats-lang.org/t/libatsopt/470/12 "2015-10-21T12:15:46Z")

</div>

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 :)On Wednesday, October 21, 2015 at 1:32:53 AM UTC-4, H Zhang wrote:

> 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
> 
> On Tuesday, October 20, 2015 at 11:57:54 AM UTC-7, gmhwxi wrote:
> 
> > 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](https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS)
> > 
> > Cheers!
> > 
> > On Friday, October 2, 2015 at 10:49:48 PM UTC-4, gmhwxi wrote:
> > 
> > > Please find some instructions on building libatsopt, that is, the  
> > > library version of patsopt:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt](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](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 🙂

---

<div class="post-metadata">

**Author:** ![H\_Zhang](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@H\_Zhang](https://discourse.ats-lang.org/u/H_Zhang)\
**Post date:** [October 26, 2015, 12:07am UTC](https://discourse.ats-lang.org/t/libatsopt/470/13 "2015-10-26T00:07:33Z")

</div>

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.On Saturday, October 24, 2015 at 7:20:37 PM UTC-7, gmhwxi wrote:

> Here is a very primitive version:
> 
> [test\_libatsopt](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.
> 
> On Wednesday, October 21, 2015 at 4:30:46 PM UTC-4, H Zhang wrote:
> 
> > 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 [GitHub - leanprover/tutorial: Lean Tutorials](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:
> > 
> > > 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](https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt/JS)
> > > 
> > > Cheers!
> > > 
> > > On Friday, October 2, 2015 at 10:49:48 PM UTC-4, gmhwxi wrote:
> > > 
> > > > Please find some instructions on building libatsopt, that is, the  
> > > > library version of patsopt:
> > > > 
> > > > [https://github.com/githwxi/ATS-Postiats/tree/master/utils/libatsopt](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](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 🙂
