ATS REPL

157 views
Skip to first unread message

H Zhang

unread,
Jan 12, 2014, 12:26:30 AM1/12/14
to ats-lan...@googlegroups.com
I think it would be nice to have a way to do REPL in ATS, at least for the ML style subset of ATS. This may even be a good way to show how to do a project in ATS and I will be happy to contribute to such a project.

Now the first question is since ATS has a ML style subset would it make sense to actually support a true subset of ML? So one can write code in this intersection of ATS and ML and use the ML interpreter for REPL development and debugging and use ATS compiler to compile to native code for performance. Is there a reason why this can not be done? If doable this seems the low hanging fruit to get.

If we do want/need to do an interpreter for ATS (ML style) here are some thoughts:

As I understand it ATS depends on the C compilers to do a lot of the work so there may be some limitations on what can be done for the interpreter without herculean effort. ATS can already parse code into AST so that part should be reused to parse the input for the interpreter as well (although it may be important to generate better error messages for the interpreter). Parsed ATS expressions can be  evaluated if we can correctly dispatch function calls so we need a loader to find/load/register compiled functions and a runtime to manage associated symbol table and global data. The trick here is to ensure type safety even while type information may have been lost in the compiled native code, so the interpreter will need to access ATS's type analysis module to establish type correctness (so we will need access to the source/header files as well even for pre-compiled code). Alternatively we may use the compiler working in the background to establish type-correctness? On the other hand things like defining new data structures and functions are dependent on the C compilers and are not easy to support directly in the interpreter. Here I think the typical Prolog style of separating compilation (consult) and evaluation (query) may be a useful model. We can organize functions into different modules that get compiled into DLLs. If a source file is modified the associated DLL will be recompiled (maybe externally calling make but runtime may need to know the dependency so it can update type information) and reloaded.

I know these are rather immature thoughts but I hope this is somehow doable and I think with a good (enough) REPL ATS will be more accessible to programmers in general.

Thanks,

Haitao

H Zhang

unread,
Mar 18, 2014, 10:05:57 PM3/18/14
to ats-lan...@googlegroups.com
I use luajit (through the ZeroBrane IDE) to call the generated C functions from ATS and it feels pretty close to a REPL. The process is as follows:
1) Generate C files using patsopt -o prog.c -d prog.dats
2) Compile to object files with gcc -fPIC. Make sure that ATS include files are in the path and MALLOCFLAG is defined.
3) Create DLL with gcc -shared
4) In luajit console or script, write the appropriate C definitions, load the DLL and use the functions dynamically. The luajit C definitions can probably be automatically extracted from extern fun ... = "mac#" statements.

I hope this is helpful to others who find similar needs of an REPL. Luajit has the easiest CFFI I have seen of all dynamic languages. Since it has JIT it does not depend on the C compiler to generate calling code. Remember it is the luajit C FFI extension, not the regular Lua C API.

Haitao

H Zhang

unread,
Mar 18, 2014, 10:10:36 PM3/18/14
to ats-lan...@googlegroups.com

Oh I forgot to mention that one should #define ATS_DYNLOADFLAG 0
Since there is no main function for the DLL it appears this is how one can make the dynload initialization happy (actually turned off), otherwise the linker would complain about missing dynload functions.

Haitao

Brandon Barker

unread,
Mar 18, 2014, 10:11:32 PM3/18/14
to H Zhang, ats-lan...@googlegroups.com
Very cool! 

I hadn't heard of luajit before and might be doing some hobby lua work soon, so that's also nice to know about.

Brandon Barker
brandon...@gmail.com


--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/6c9f85b4-a1c0-4411-abe4-eea039e30824%40googlegroups.com.

gmhwxi

unread,
Mar 18, 2014, 10:17:44 PM3/18/14
to ats-lan...@googlegroups.com
This sounds like a pretty cool idea :)

I will teach a class based on ATS this summer. I will see if I can make it work.

Brandon Barker

unread,
Mar 18, 2014, 10:32:38 PM3/18/14
to gmhwxi, ats-lang-users
That's a lot of ATS teaching, in addition to your spring and fall semester courses. 

I've wondered, why do more (any) of your students not use/find this forum?

Brandon Barker
brandon...@gmail.com


--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.

gmhwxi

unread,
Mar 18, 2014, 11:20:32 PM3/18/14
to ats-lan...@googlegroups.com, gmhwxi

I am trying to use the teaching opportunity to flush out as many bugs as possible :)

The students are probably more comfortable with Piazza.

H Zhang

unread,
Mar 19, 2014, 1:04:37 AM3/19/14
to ats-lan...@googlegroups.com
Then you definitely want to try ZeroBrane if you haven't used it before. Very small IDE yet very versatile and powerful. Shows how embedding a good language makes a powerful editor/IDE a breeze. It has very nice pedagogical demos (written in Lua) that could be used to demo ATS as well. Luajit is now ZeroBrane's default interpreter so you don't even have to separately build luajit (which is straightforward to do anyway).

I would be very interested in combining this with the ATS compiler to make exploring the ATS compiler easier. If you want to start a project on this I would be happy to contribute.

Haitao

H Zhang

unread,
Mar 19, 2014, 9:09:14 PM3/19/14
to ats-lan...@googlegroups.com
Here is an interesting paper that discussed combining Lua with ML in writing an optimizing compiler for C--: http://www.cs.tufts.edu/~nr/pubs/embedj.pdf
They went as far as writing the Lua interpreter themselves in ML in order to avoid the GC clash problems. I think luajit and ATS would pair together a lot better for the task.
Following is a quote of the part on advantages of having embedded Lua for the compiler. In case the significance is lost on anyone they are changing internal variables/parameters of the compiler using Lua assignments at the command line.

There are significant advantages to having this code in Lua.
• It is easy to change the configuration of the optimizer, the compiler, or even the target
machine by putting a simple phrase on the command line. For example, simply
writing qc-- backend=Backend.mips allows us to cross-compile for our MIPS
machine. Cross-compilation is better than compiling natively because our MIPS machine
is very slow.
• The Lua code is invaluable for debugging. For example, we can turn off a pass of
the compiler by a command-line assignment such as backend.expand=nil, and we
can make the compiler emit “diagnostic assembly language” by using the assignment
backend.asm=Asm.cmm.
• The “driver” code in Lua runs not only the C-- compiler but also preprocessors,
front ends, the assembler, and the linker. It can therefore be used to compile a test
case from start to finish, then do another test without restarting the compiler. The
advantage is speed. For example, to test calling conventions, we compile a suite of
several hundred test cases. Using the Lua driver, these cases compile three to five
times faster than using a Perl script in which the compiler is started separately for
each case.

And later on:

As shown by highlighting, almost every stage in the Lua record is actually implemented in
the host language, Objective Caml—but the stages are composed using Lua. The advantage
is that by putting an appropriate Lua assignment on the compiler’s command line, we can
omit or change any stage, including the assembly-language emitter.

Enjoy,
Haitao

Raoul Duke

unread,
Mar 19, 2014, 9:10:55 PM3/19/14
to ats-lang-users
please god choose a language that at least has /optional/ static
typing. yeah, yeah, lua is great... except for all the ways it is an
embodiment of pure evil! :-}
Message has been deleted

gmhwxi

unread,
Mar 20, 2014, 1:24:52 AM3/20/14
to ats-lan...@googlegroups.com
I installed ZeroBrane today and played with it a bit.

I have to say that zbstudio is probably not something I am looking for right now.

What I am really interested in is an IDE for ATS that can support various forms
of meta-programming. Supporting run-time debugging for ATS is a secondary issue
to me.

Maybe I will give CINT a try next.

Brandon Barker

unread,
Jun 2, 2015, 8:32:13 AM6/2/15
to ats-lan...@googlegroups.com
While CINT is still probably what would be desired for this, I did see a new (to me) C interpreter today called PicoC: http://www.osnews.com/comments/28602

Also a few other fringe C compilers mentioned in the thread.

gmhwxi

unread,
Jun 2, 2015, 12:46:00 PM6/2/15
to ats-lan...@googlegroups.com
Well, in principle, what is needed for writing an interpreter for ATS is a C interpreter
instead of a C compiler.

An interpreter has a state that allows top-level values obtained during evaluation to be
recorded for subsequent use.

With a C compiler like PicoC, you could turn ATS into a scripting language. Actually, I
did this with TinyCC. It was for fun and probably for fun only.
Reply all
Reply to author
Forward
0 new messages