C++ Expression templates in ATS2

86 views
Skip to first unread message

Artyom Shalkhakov

unread,
Mar 14, 2015, 9:24:40 AM3/14/15
to ats-lan...@googlegroups.com
Here is my attempt to convert some C++ template metaprogramming approaches to ATS2, in particular, expression templates:

https://github.com/ashalkhakov/ATS-playground/blob/master/exptmp.dats

It seems to work (also, there is no dispatching happening at runtime, just as it doesn't in C++). However, I found that types must be distinct for using them to represent different nodes of the syntax tree. For instance, if Tid is [assume]d to be a double, the code compiles and runs, but produces undesired results.

Also, I'm wondering if C++ references can be mimicked in ATS2? Currently, the subtrees are embedded by value, instead of by reference. I'd like to avoid GC.

As a side-effect, looks like I'm finally starting to comprehend how C++ templates work by comparing to ATS. :)

gmhwxi

unread,
Mar 14, 2015, 11:05:54 AM3/14/15
to ats-lan...@googlegroups.com
Nice!

The example typechecks. But how to run it?

Artyom Shalkhakov

unread,
Mar 14, 2015, 11:16:06 AM3/14/15
to ats-lan...@googlegroups.com
On Saturday, March 14, 2015 at 9:05:54 PM UTC+6, gmhwxi wrote:
Nice!

The example typechecks. But how to run it?


I just do:

patscc exptmp.dats

And then:

./a.out

If you run it, it will print some results into console.

I didn't bother to create a proper makefile at this point.
 

gmhwxi

unread,
Mar 14, 2015, 3:35:22 PM3/14/15
to ats-lan...@googlegroups.com


On Saturday, March 14, 2015 at 9:24:40 AM UTC-4, Artyom Shalkhakov wrote:

gmhwxi

unread,
Mar 14, 2015, 9:42:44 PM3/14/15
to ats-lan...@googlegroups.com

It seems to work (also, there is no dispatching happening at runtime, just as it doesn't in C++). However, I found that types must be distinct for using them to represent different nodes of the syntax tree. For instance, if Tid is [assume]d to be a double, the code compiles and runs, but produces undesired results.

This is due to incorrect use of abstract types. See my code:

https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST0/exptmp.dats


Also, I'm wondering if C++ references can be mimicked in ATS2? Currently, the subtrees are embedded by value, instead of by reference. I'd like to avoid GC.

I don't quite understand the question. As it stands now, the code involves no memory allocation. When 'eval' is called, an expression value (a flat record) is passed
by reference.

 
On Saturday, March 14, 2015 at 9:24:40 AM UTC-4, Artyom Shalkhakov wrote:

Artyom Shalkhakov

unread,
Mar 14, 2015, 11:24:23 PM3/14/15
to ats-lan...@googlegroups.com
On Sunday, March 15, 2015 at 7:42:44 AM UTC+6, gmhwxi wrote:

It seems to work (also, there is no dispatching happening at runtime, just as it doesn't in C++). However, I found that types must be distinct for using them to represent different nodes of the syntax tree. For instance, if Tid is [assume]d to be a double, the code compiles and runs, but produces undesired results.

This is due to incorrect use of abstract types. See my code:

https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST0/exptmp.dats


Thanks! Much better this way. Oh I see, I forgot to define the size of the abstract type.
 
Also, I'm wondering if C++ references can be mimicked in ATS2? Currently, the subtrees are embedded by value, instead of by reference. I'd like to avoid GC.

I don't quite understand the question. As it stands now, the code involves no memory allocation. When 'eval' is called, an expression value (a flat record) is passed
by reference.

Taking Esub_(E1:t@ype, E2:t@ype) = @(E1, E2) as example, both E1 and E2 are flat.

The C++ version would use [const E1&] and [const E2&] as the types for values, instead of E1 and E2 (so, it would use C++ references aka non-null pointers).
 

gmhwxi

unread,
Mar 15, 2015, 12:19:40 AM3/15/15
to ats-lan...@googlegroups.com

See:

https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST0/exptmp_ref.dats

This one involves memory allocation. If an expression to be evaluated is known at compile-time,
then it can be constructed using static allocation.

Artyom Shalkhakov

unread,
Mar 15, 2015, 12:51:51 AM3/15/15
to ats-lan...@googlegroups.com
On Sunday, March 15, 2015 at 10:19:40 AM UTC+6, gmhwxi wrote:

See:

https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST0/exptmp_ref.dats

This one involves memory allocation. If an expression to be evaluated is known at compile-time,
then it can be constructed using static allocation.


Thank you. By "static allocation", do you mean something like an approach given in Chapter 8 of Introduction to Programming in ATS, "Constructing a Statically Allocated List"?
 

Hongwei Xi

unread,
Mar 15, 2015, 12:57:41 AM3/15/15
to ats-lan...@googlegroups.com
By "static allocation", do you mean something like an approach given in Chapter 8 of Introduction to Programming in ATS, "Constructing a Statically Allocated List"?

Yes.

--
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/067f50f4-2a20-43ce-af3e-a6ff00f496a1%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages