Compiling ATS to Python?

179 views
Skip to first unread message

gmhwxi

unread,
Aug 6, 2014, 10:43:13 PM8/6/14
to ats-lan...@googlegroups.com
Hi,

I have been working on documenting the target language of ATS/Postiats.
It is a very small and standard subset of C. I have now got a functioning parser
for it.

Today, I wrote some code to *demonstrate* how one might generate Python code
from ATS source by making use of this parser:

https://github.com/githwxi/ATS-Postiats-contrib/tree/master/projects/MEDIUM/ATS-parse-emit/Python

As an example, here is some generated Python code

import sys
sys
.path.append('/home/hwxi/research/Postiats-contrib/git/projects/MEDIUM/ATS-parse-emit/Python/prelude')
from integer_cats import *

def fact(arg0):
 
#__patsflab_fact
  tmp1
= ATSLIB_056_prelude__gt_g0int_int__1__1(arg0, 0)
 
if(tmp1):
    tmp7
= atspre_g0int_sub_int(arg0, 1)
    tmp6
= fact(tmp7)
    tmpret0
= atspre_g0int_mul_int(arg0, tmp6)
 
else:
    tmpret0
= 1
 
#endif
 
return(tmpret0)

def ATSLIB_056_prelude__gt_g0int_int__1__1(arg0, arg1):
 
#__patsflab_gt_g0int_int
  tmp3__1
= atspre_g0int2int_int_int(arg1)
  tmpret2__1
= atspre_g0int_gt_int(arg0, tmp3__1)
 
return(tmpret2__1)

print "fact(100) =", fact(100)


Yes, the above Python code works!

At this point, there is still a lot of work to be done before all of this can actually be useful.

If someone is interested in making it work, I will be happy to offer a hand. Unfortunately,
I myself do not have time to go further along this path. At least, not now.

Cheers!


Raoul Duke

unread,
Aug 6, 2014, 10:50:52 PM8/6/14
to ats-lang-users
if anybody thinks this is cool (i certainly do) then also please
checkout http://shenlanguage.org :-)

Brandon Barker

unread,
Aug 6, 2014, 10:59:55 PM8/6/14
to ats-lang-users
This is exciting! Is the documentation embedded in the source of ATS-parse-emit?

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.
Visit this group at http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/798ce024-b9d2-4f43-add8-d0be6f1a696f%40googlegroups.com.

Hongwei Xi

unread,
Aug 6, 2014, 11:04:30 PM8/6/14
to ats-lan...@googlegroups.com
There is no documentation right now.

However, the code should be straightforward to follow.

And I will be happy to answer questions.

I myself do not write Python code. I am more interested in
generating Javascript code :)



Yannick Duchêne

unread,
Aug 7, 2014, 2:19:16 AM8/7/14
to ats-lan...@googlegroups.com


Le jeudi 7 août 2014 05:04:30 UTC+2, gmhwxi a écrit :
I myself do not write Python code. I am more interested in
generating Javascript code :)
 
The same for me!

Funny this thread comes now, as I was precisely thinking of trying to do something similar in the future. Will have a look at it very soon, 
 

Artyom Shalkhakov

unread,
Aug 7, 2014, 11:22:18 AM8/7/14
to ats-lan...@googlegroups.com
This is great! I think I'll try to write a compiler to IL bytecode. :)

Artyom Shalkhakov

unread,
Aug 7, 2014, 12:03:22 PM8/7/14
to ats-lan...@googlegroups.com
Hongwei,

Some questions:

1. what's the difference between ATSINSlab and ATSINSflab, and between ATSINSgoto and ATSINSfgoto?
2. what are pointer operations compiled to?
3. what is ATSINSmove_boxrec?
 
Cheers!


Greg Fitzgerald

unread,
Aug 7, 2014, 1:32:41 PM8/7/14
to ats-lan...@googlegroups.com
Neat! Instead of targeting Python, how about the asm.js subset of
JavaScript? For longish running benchmarks, performance should be on
par with the native-compiled version.

http://asmjs.org/spec/latest/

Googling around, it looks like Firefox and Chrome have optimized for
asm.js since versions 22 and 28 respectively. And both browsers have
command-line shells (SpiderMonkey and V8) for easy benchmarking.

-Greg

Raoul Duke

unread,
Aug 7, 2014, 1:48:44 PM8/7/14
to ats-lang-users
hey, please! the web is dead! mobile first! and not that crappy html5
lies lies lies! sheesh. :-}

so i'd almost rather see another layer of abstraction. that's both a
horrible joke, and a serious thought. (it is also easy for me to say
as somebody who isn't working on this project.)

i.e.: use llvm bytecode! via Emscripten or whatever else! that way you
can get asm.js, and i can get code for mobile devices for games!

gmhwxi

unread,
Aug 7, 2014, 2:19:01 PM8/7/14
to ats-lan...@googlegroups.com

ATSINSlab/ATSINSgoto are used for pattern matching compilation.
ATSINSflab/ATSINSfgoto are used to support tail-call optimization.
Because Python does not support jumps, ATSINSgoto and ATSINSfgoto
need to be removed. I will try to do it.

For compiling code to Python, I have no plan to handle pointer operations.
Or one may go the Cython road.

ATSmove_boxrec(tmp, T): create an uninitialized record of the type T, and
assign it to tmp.

ATSmove_boxrec is always followed by a few assignments for field initialization;
they should probably grouped together and then translated into something like:

tmp = (v1, v2, ..., vn)

gmhwxi

unread,
Aug 7, 2014, 2:27:39 PM8/7/14
to ats-lan...@googlegroups.com
I think we can try all these approaches.

The target language of ATS is very small, and it should be
straightforward to translate it to llvm. Actually, it makes a lot
of sense to write a compiler for translating this target language
into object code since using gcc/clang is clearly an overkill.

Yannick Duchêne

unread,
Aug 7, 2014, 3:06:16 PM8/7/14
to ats-lan...@googlegroups.com


Le jeudi 7 août 2014 20:27:39 UTC+2, gmhwxi a écrit :
I think we can try all these approaches.

The target language of ATS is very small, and it should be
straightforward to translate it to llvm. Actually, it makes a lot
of sense to write a compiler for translating this target language
into object code since using gcc/clang is clearly an overkill.


This complaint makes some sense, but targeting C is a portability bonus. Also, on most software store, you can submit C programs not any misc language you use and even less binaries. And C is the most common denominator, covering both Apple's and Windows's and Ubuntu's own software stores (at least Apple and Windows favour C, to get run safety static check on the application… Ubuntu don't, but still requires something which can be compiled within a standard Ubuntu environment).

gmhwxi

unread,
Aug 7, 2014, 3:25:11 PM8/7/14
to ats-lan...@googlegroups.com
Yes. But we have already got C covered :)

The point here is to venture out, seeing if other popular languages can
be covered as well. Venturing out makes sense because the price is very
low. The heavy lifting has already been taken care of by ATS/Postiats.

gmhwxi

unread,
Aug 16, 2014, 7:51:22 PM8/16/14
to ats-lan...@googlegroups.com
Now I have added the support for pattern matching.

Here is an implementation of the list-length function in python
that is generated from some ATS source:


import sys
sys
.path.append('/home/hwxi/research/Postiats-contrib/git/projects/MEDIUM/ATS-parse-emit/Python/prelude/CATS')
from integer_cats import *

# (* ****** ****** *)

def nil(): return None
def cons(x, xs): return (x, xs)

def ATSCKptrisnil(xs): return (xs == None)
def ATSCKptriscons(xs): return (xs != None)

# (* ****** ****** *)

# extern
# fun
# listlen
#   : {a:t@ype} List0 (a) -> int = "ext#"
# //

# (* ****** ****** *)

# implement
# listlen{a}
#   (xs) = let
# //
# prval () = lemma_list_param (xs)
# //
# fun
# loop{i,j:nat} .<i>.
# (
#   xs: list (a, i), res: int(j)
# ) : int(i+j) = let
# in
# //
# case+ xs of
# | list_nil () => res | list_cons (_, xs) => loop (xs, res+1)
# //
# end // end of [loop]
# //
# in
#   loop (xs, 0)
# end // end of [intlistlen]

def listlen(arg0):
  tmpret0
= None
  funlab_py
= None
  tmplab_py
= None
 
#__patsflab_listlen
  tmpret0
= loop_1(arg0, 0)
 
return tmpret0

def loop_1(arg0, arg1):
  apy0
= None
  apy1
= None
  tmpret1
= None
  tmp3
= None
  tmp4
= None
  funlab_py
= None
  tmplab_py
= None
  mbranch_1
= None
 
def __atstmplab0():
   
nonlocal arg0, arg1
   
nonlocal apy0, apy1, tmpret1, tmp3, tmp4
   
nonlocal funlab_py, tmplab_py
   
nonlocal mbranch_1
    tmplab_py
= 0
   
if (ATSCKptriscons(arg0)): tmplab_py = 4 ; return#__atstmplab3
    __atstmplab1
()
   
return
 
def __atstmplab1():
   
nonlocal arg0, arg1
   
nonlocal apy0, apy1, tmpret1, tmp3, tmp4
   
nonlocal funlab_py, tmplab_py
   
nonlocal mbranch_1
    tmplab_py
= 0
    tmpret1
= arg1
   
return
 
def __atstmplab2():
   
nonlocal arg0, arg1
   
nonlocal apy0, apy1, tmpret1, tmp3, tmp4
   
nonlocal funlab_py, tmplab_py
   
nonlocal mbranch_1
    tmplab_py
= 0
    __atstmplab3
()
   
return
 
def __atstmplab3():
   
nonlocal arg0, arg1
   
nonlocal apy0, apy1, tmpret1, tmp3, tmp4
   
nonlocal funlab_py, tmplab_py
   
nonlocal mbranch_1
    tmplab_py
= 0
    tmp3
= arg0[1]
    tmp4
= atspre2py_add_int1_int1(arg1, 1)
   
#tailcal_beg
    apy0
= tmp3
    apy1
= tmp4
    arg0
= apy0
    arg1
= apy1
    funlab_py
= 1 #__patsflab_loop_1
   
#tailcal_end
   
return
  mbranch_1
= { 1: __atstmplab0, 2: __atstmplab1, 3: __atstmplab2, 4: __atstmplab3 }
 
while (1):
    funlab_py
= 0
   
#__patsflab_loop_1
   
#ATScaseof_beg
    tmplab_py
= 1
   
while (1):
      mbranch_1
.get(tmplab_py)()
     
if (tmplab_py == 0): break
   
#ATScaseof_end
   
if (funlab_py == 0): break
 
return tmpret1

# (* ****** ****** *)

xs
= cons(1, cons(2, cons(3, nil())))

print("listlen(", xs, ") =", listlen(xs))

# (* ****** ****** *)

Artyom Shalkhakov

unread,
Aug 17, 2014, 2:37:52 AM8/17/14
to ats-lan...@googlegroups.com
Hongwei,

This is very nice!

Is it possible for atsopt to emit #line pragmas (instead of comments) so that paremit would parse them?

I think that propagating such information is very important for debugging/tracing purposes.


</s
...

Hongwei Xi

unread,
Aug 17, 2014, 3:42:13 AM8/17/14
to ats-lan...@googlegroups.com
If you add the flag --gline:

patsopt --gline -o listlen_dats.c -d listlen.dats

then you will get line information on the source code in the output.

I have just added code for parsing line pragmas.



--
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.
Visit this group at http://groups.google.com/group/ats-lang-users.

Artyom Shalkhakov

unread,
Aug 17, 2014, 6:02:08 AM8/17/14
to ats-lan...@googlegroups.com
On Sunday, August 17, 2014 1:42:13 PM UTC+6, gmhwxi wrote:
If you add the flag --gline:

patsopt --gline -o listlen_dats.c -d listlen.dats

then you will get line information on the source code in the output.

I have just added code for parsing line pragmas.


Great! Thanks!

Reply all
Reply to author
Forward
0 new messages