C++ Expression templates in ATS2

Here is my attempt to convert some C++ template metaprogramming approaches
to ATS2, in particular, expression templates:

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. :slight_smile:

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.On Saturday, March 14, 2015 at 11:24:23 PM UTC-4, Artyom Shalkhakov wrote:

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).

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

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. :slight_smile:

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:

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. :slight_smile:

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.On Sun, Mar 15, 2015 at 12:51 AM, Artyom Shalkhakov < artyom.s...@gmail.com> wrote:

On Sunday, March 15, 2015 at 10:19:40 AM UTC+6, gmhwxi wrote:

See:

GitHub - githwxi/ATS-Postiats-test: This repository is primarily for testing ATS2 on various platforms
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”?

On Saturday, March 14, 2015 at 11:24:23 PM UTC-4, Artyom Shalkhakov wrote:

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:

GitHub - githwxi/ATS-Postiats-test: This repository is primarily for testing ATS2 on various platforms
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).

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

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. :slight_smile:


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
https://groups.google.com/d/msgid/ats-lang-users/067f50f4-2a20-43ce-af3e-a6ff00f496a1%40googlegroups.com?utm_medium=email&utm_source=footer
.

Nice!

The example typechecks. But how to run it?On Saturday, March 14, 2015 at 9:24:40 AM UTC-4, Artyom Shalkhakov wrote:

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. :slight_smile:

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.> On Saturday, March 14, 2015 at 9:24:40 AM UTC-4, Artyom Shalkhakov wrote:

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. :slight_smile:

I like this example. I tidied it up:

https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST0/exptmp.datsOn Saturday, March 14, 2015 at 9:24:40 AM UTC-4, Artyom Shalkhakov wrote:

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. :slight_smile:

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).

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”?> On Saturday, March 14, 2015 at 11:24:23 PM UTC-4, Artyom Shalkhakov wrote:

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).

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

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. :slight_smile: