# Runtime polymorphism for unboxed types?

**URL:** <https://discourse.ats-lang.org/t/runtime-polymorphism-for-unboxed-types/540>\
**Category:** Google Group\
**Created:** [February 20, 2015, 2:57pm UTC](https://discourse.ats-lang.org/t/runtime-polymorphism-for-unboxed-types/540 "2015-02-20T14:57:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shea\_Levy](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Shea\_Levy](https://discourse.ats-lang.org/u/Shea_Levy)\
**Post date:** [February 20, 2015, 2:57pm UTC](https://discourse.ats-lang.org/t/runtime-polymorphism-for-unboxed-types/540/1 "2015-02-20T14:57:59Z")

</div>

Hi all,

I know ATS2 supports function templates, and polymorphic functions for boxed types. Is there any support for runtime polymorphism (with vtables or standalone structs of functions or similar) available in the language?

Related question: Is there support for _value_ templates?

~Shea

---

<div class="post-metadata">

**Author:** ![Artyom\_Shalkhakov](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Artyom\_Shalkhakov](https://discourse.ats-lang.org/u/Artyom_Shalkhakov)\
**Post date:** [February 21, 2015, 2:46am UTC](https://discourse.ats-lang.org/t/runtime-polymorphism-for-unboxed-types/540/2 "2015-02-21T02:46:10Z")

</div>

> I added an example of loop unrolling here:
> 
> [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats)

This is ingenious. 🙂

> I do have a plan to implement the following feature
> 
> fun vec\_add{i:int}(…) =  
> sif i == 0  
> then …  
> else …
> 
> vec\_add{10}(…) can then be expanded at compile-time.

That would be awesome. It would drastically simplify e.g. the  
implementation of generic vectors in little math libraries for use in  
graphics.\> On Friday, February 20, 2015 at 10:29:50 AM UTC-5, Artyom Shalkhakov wrote:

> > On Friday, February 20, 2015 at 8:58:01 PM UTC+6, Shea Levy wrote:
> > 
> > > Hi all,
> > > 
> > > I know ATS2 supports function templates, and polymorphic functions for  
> > > boxed types. Is there any support for runtime polymorphism (with vtables or  
> > > standalone structs of functions or similar) available in the language?
> > 
> > HX once implemented Smalltalk-style objects in an ATS prototype, but I  
> > think he abandoned the plan to implement it a long time ago.
> > 
> > > Related question: Is there support for _value_ templates?
> > 
> > Are you asking about something like:
> > 
> > fun{i:pos}  
> > vec\_add(x: &(@[float][i]), y: &(@[float][i]), o: &(@[float][i])): void =  
> > // do an element-wise addition, putting results into [o]  
> > // i is known at compile time, so we can get it at runtime  
> > // using something like constant\<int,i\>, similarly to sizeof
> > 
> > I’d like to know the answer as well.
> > 
> > > ~Shea

---

<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:** [March 7, 2015, 1:41am UTC](https://discourse.ats-lang.org/t/runtime-polymorphism-for-unboxed-types/540/3 "2015-03-07T01:41:55Z")

</div>

I find the example intriguing and I am trying to understand how the  
unrolling is actually carried out. Is this paper  
[http://www.cs.bu.edu/~hwxi/academic/papers/icfp05.pdf](http://www.cs.bu.edu/~hwxi/academic/papers/icfp05.pdf) still a good  
reference on how this part of ATS works? The erasure function defined in  
fig 6 seems to simply remove the proof terms. Could you talk about how the  
program transformation is achieved or give a reference paper?

Thanks,  
HaitaoOn Friday, February 20, 2015 at 12:42:15 PM UTC-8, gmhwxi wrote:

> I added an example of loop unrolling here:
> 
> [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats)
> 
> I do have a plan to implement the following feature
> 
> fun vec\_add{i:int}(…) =  
> sif i == 0  
> then …  
> else …
> 
> vec\_add{10}(…) can then be expanded at compile-time.
> 
> On Friday, February 20, 2015 at 10:29:50 AM UTC-5, Artyom Shalkhakov wrote:
> 
> > On Friday, February 20, 2015 at 8:58:01 PM UTC+6, Shea Levy wrote:
> > 
> > > Hi all,
> > > 
> > > I know ATS2 supports function templates, and polymorphic functions for  
> > > boxed types. Is there any support for runtime polymorphism (with vtables or  
> > > standalone structs of functions or similar) available in the language?
> > 
> > HX once implemented Smalltalk-style objects in an ATS prototype, but I  
> > think he abandoned the plan to implement it a long time ago.
> > 
> > > Related question: Is there support for _value_ templates?
> > 
> > Are you asking about something like:
> > 
> > fun{i:pos}  
> > vec\_add(x: &(@[float][i]), y: &(@[float][i]), o: &(@[float][i])): void =  
> > // do an element-wise addition, putting results into [o]  
> > // i is known at compile time, so we can get it at runtime  
> > // using something like constant\<int,i\>, similarly to sizeof
> > 
> > I’d like to know the answer as well.
> > 
> > > ~Shea

---

<div class="post-metadata">

**Author:** ![Artyom\_Shalkhakov](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Artyom\_Shalkhakov](https://discourse.ats-lang.org/u/Artyom_Shalkhakov)\
**Post date:** [March 6, 2015, 3:01am UTC](https://discourse.ats-lang.org/t/runtime-polymorphism-for-unboxed-types/540/4 "2015-03-06T03:01:34Z")

</div>

> I tried to implement what is mentioned below. Unfortunately, my plan did  
> not pan out  
> well. The simple reason is that the condition following each ‘sif’ is  
> static but the static  
> information is essentially all erased at the stage where ‘sif’ needs to be  
> eliminated.  
> I could try to eliminate ‘sif’ at the point where such information is  
> still available but this  
> means templates making use of ‘sif’ cannot be handled (as template  
> instantiation happens  
> at a later stage).
> 
> For now, I think we simply need to implement a version of array\_foreach  
> that supports  
> loop unrolling. For instance, see:
> 
> [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/TYPEVAL/dotprod.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/TYPEVAL/dotprod.dats)

Could you post the resulting C code for dotprod.dats? I’d really like to  
take a look, even if it’s very long.\> On Monday, March 2, 2015 at 12:22:36 PM UTC-5, Shea Levy wrote:

> > Cool! If we had that feature, would we be able to parameterize function  
> > templates by any sort, and have full use of scase/sif in the  
> > implementations? That would be very useful, I’m currently adapting your  
> > example to implement type lists (to use where in C++ I’d use variadic  
> > templates), but having a proper type list datasort would be much nicer than  
> > passing around proofs that a given list is proper 🙂
> > 
> > ~Shea
> > 
> > On Feb 20, 2015, at 3:42 PM, gmhwxi [gmh...@gmail.com](mailto:gmh...@gmail.com) wrote:
> > 
> > I added an example of loop unrolling here:
> > 
> > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats)
> > 
> > I do have a plan to implement the following feature
> > 
> > fun vec\_add{i:int}(…) =  
> > sif i == 0  
> > then …  
> > else …
> > 
> > vec\_add{10}(…) can then be expanded at compile-time.
> > 
> > On Friday, February 20, 2015 at 10:29:50 AM UTC-5, Artyom Shalkhakov wrote:
> > 
> > > On Friday, February 20, 2015 at 8:58:01 PM UTC+6, Shea Levy wrote:
> > > 
> > > > Hi all,
> > > > 
> > > > I know ATS2 supports function templates, and polymorphic functions for  
> > > > boxed types. Is there any support for runtime polymorphism (with vtables or  
> > > > standalone structs of functions or similar) available in the language?
> > > 
> > > HX once implemented Smalltalk-style objects in an ATS prototype, but I  
> > > think he abandoned the plan to implement it a long time ago.
> > > 
> > > > Related question: Is there support for _value_ templates?
> > > 
> > > Are you asking about something like:
> > > 
> > > fun{i:pos}  
> > > vec\_add(x: &(@[float][i]), y: &(@[float][i]), o: &(@[float][i])): void =  
> > > // do an element-wise addition, putting results into [o]  
> > > // i is known at compile time, so we can get it at runtime  
> > > // using something like constant\<int,i\>, similarly to sizeof
> > > 
> > > I’d like to know the answer as well.
> > > 
> > > > ~Shea
> > 
> > –  
> > 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...@googlegroups.com](mailto:ats-lang...@googlegroups.com).  
> > To post to this group, send email to [ats-l...@googlegroups.com](mailto:ats-l...@googlegroups.com).  
> > Visit this group at [http://groups.google.com/group/ats-lang-users](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/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/ats-lang-users/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .

---

<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:** [March 6, 2015, 12:27am UTC](https://discourse.ats-lang.org/t/runtime-polymorphism-for-unboxed-types/540/5 "2015-03-06T00:27:25Z")

</div>

I tried to implement what is mentioned below. Unfortunately, my plan did  
not pan out  
well. The simple reason is that the condition following each ‘sif’ is  
static but the static  
information is essentially all erased at the stage where ‘sif’ needs to be  
eliminated.  
I could try to eliminate ‘sif’ at the point where such information is still  
available but this  
means templates making use of ‘sif’ cannot be handled (as template  
instantiation happens  
at a later stage).

For now, I think we simply need to implement a version of array\_foreach  
that supports  
loop unrolling. For instance, see:

[https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/TYPEVAL/dotprod.datsOn](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/TYPEVAL/dotprod.datsOn) Monday, March 2, 2015 at 12:22:36 PM UTC-5, Shea Levy wrote:

> Cool! If we had that feature, would we be able to parameterize function  
> templates by any sort, and have full use of scase/sif in the  
> implementations? That would be very useful, I’m currently adapting your  
> example to implement type lists (to use where in C++ I’d use variadic  
> templates), but having a proper type list datasort would be much nicer than  
> passing around proofs that a given list is proper 🙂
> 
> ~Shea
> 
> On Feb 20, 2015, at 3:42 PM, gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> I added an example of loop unrolling here:
> 
> [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats)
> 
> I do have a plan to implement the following feature
> 
> fun vec\_add{i:int}(…) =  
> sif i == 0  
> then …  
> else …
> 
> vec\_add{10}(…) can then be expanded at compile-time.
> 
> On Friday, February 20, 2015 at 10:29:50 AM UTC-5, Artyom Shalkhakov wrote:
> 
> > On Friday, February 20, 2015 at 8:58:01 PM UTC+6, Shea Levy wrote:
> > 
> > > Hi all,
> > > 
> > > I know ATS2 supports function templates, and polymorphic functions for  
> > > boxed types. Is there any support for runtime polymorphism (with vtables or  
> > > standalone structs of functions or similar) available in the language?
> > 
> > HX once implemented Smalltalk-style objects in an ATS prototype, but I  
> > think he abandoned the plan to implement it a long time ago.
> > 
> > > Related question: Is there support for _value_ templates?
> > 
> > Are you asking about something like:
> > 
> > fun{i:pos}  
> > vec\_add(x: &(@[float][i]), y: &(@[float][i]), o: &(@[float][i])): void =  
> > // do an element-wise addition, putting results into [o]  
> > // i is known at compile time, so we can get it at runtime  
> > // using something like constant\<int,i\>, similarly to sizeof
> > 
> > I’d like to know the answer as well.
> > 
> > > ~Shea
> 
> –  
> 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...@googlegroups.com](mailto:ats-lang...@googlegroups.com) \<javascript:\>.  
> To post to this group, send email to [ats-l...@googlegroups.com](mailto:ats-l...@googlegroups.com)  
> \<javascript:\>.  
> Visit this group at [http://groups.google.com/group/ats-lang-users](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/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com)  
> [https://groups.google.com/d/msgid/ats-lang-users/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .

---

<div class="post-metadata">

**Author:** ![Shea\_Levy](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Shea\_Levy](https://discourse.ats-lang.org/u/Shea_Levy)\
**Post date:** [March 2, 2015, 5:22pm UTC](https://discourse.ats-lang.org/t/runtime-polymorphism-for-unboxed-types/540/6 "2015-03-02T17:22:33Z")

</div>

Cool! If we had that feature, would we be able to parameterize function templates by any sort, and have full use of scase/sif in the implementations? That would be very useful, I’m currently adapting your example to implement type lists (to use where in C++ I’d use variadic templates), but having a proper type list datasort would be much nicer than passing around proofs that a given list is proper 🙂

~Shea\> On Feb 20, 2015, at 3:42 PM, gmhwxi [gmh...@gmail.com](mailto:gmh...@gmail.com) wrote:

> I added an example of loop unrolling here:
> 
> [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats) [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats)
> 
> I do have a plan to implement the following feature
> 
> fun vec\_add{i:int}(…) =  
> sif i == 0  
> then …  
> else …
> 
> vec\_add{10}(…) can then be expanded at compile-time.
> 
> On Friday, February 20, 2015 at 10:29:50 AM UTC-5, Artyom Shalkhakov wrote:  
> On Friday, February 20, 2015 at 8:58:01 PM UTC+6, Shea Levy wrote:  
> Hi all,
> 
> I know ATS2 supports function templates, and polymorphic functions for boxed types. Is there any support for runtime polymorphism (with vtables or standalone structs of functions or similar) available in the language?
> 
> HX once implemented Smalltalk-style objects in an ATS prototype, but I think he abandoned the plan to implement it a long time ago.
> 
> Related question: Is there support for _value_ templates?
> 
> Are you asking about something like:
> 
> fun{i:pos}  
> vec\_add(x: &(@[float][i]), y: &(@[float][i]), o: &(@[float][i])): void =  
> // do an element-wise addition, putting results into [o]  
> // i is known at compile time, so we can get it at runtime  
> // using something like constant\<int,i\>, similarly to sizeof
> 
> I’d like to know the answer as well.
> 
> ~Shea
> 
> –  
> 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](mailto:ats-lang-user...@googlegroups.com) [mailto:ats-lang-user...@googlegroups.com](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com) [mailto:ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [http://groups.google.com/group/ats-lang-users](http://groups.google.com/group/ats-lang-users) [http://groups.google.com/group/ats-lang-users](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/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com) [https://groups.google.com/d/msgid/ats-lang-users/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com?utm_medium=email&utm_source=footer).

---

<div class="post-metadata">

**Author:** ![Artyom\_Shalkhakov](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Artyom\_Shalkhakov](https://discourse.ats-lang.org/u/Artyom_Shalkhakov)\
**Post date:** [February 20, 2015, 3:29pm UTC](https://discourse.ats-lang.org/t/runtime-polymorphism-for-unboxed-types/540/7 "2015-02-20T15:29:49Z")

</div>

> Hi all,
> 
> I know ATS2 supports function templates, and polymorphic functions for  
> boxed types. Is there any support for runtime polymorphism (with vtables or  
> standalone structs of functions or similar) available in the language?

HX once implemented Smalltalk-style objects in an ATS prototype, but I  
think he abandoned the plan to implement it a long time ago.

> Related question: Is there support for _value_ templates?

Are you asking about something like:

fun{i:pos}  
vec\_add(x: &(@[float][i]), y: &(@[float][i]), o: &(@[float][i])): void =  
// do an element-wise addition, putting results into [o]  
// i is known at compile time, so we can get it at runtime  
// using something like constant\<int,i\>, similarly to sizeof

I’d like to know the answer as well.

---

<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:** [March 7, 2015, 2:24am UTC](https://discourse.ats-lang.org/t/runtime-polymorphism-for-unboxed-types/540/8 "2015-03-07T02:24:45Z")

</div>

The template system of ATS2 is largely undocumented.

Unlike dependent types and linear types in ATS, I have done very little  
theoretical study on templates in ATS. I just don’t feel that I have time  
and energy for it ☹

This example is actually quite straightforward.

Say you want to evaluate tally\<S(S(Z))\>(… | 2)

The compiler essentially generates three functions:

tally\_0 for tally  
tally\_1 for tally\<S(Z)\>  
tally\_2 for tally\<S(S(Z))\>

It goes like this:

First, the compiler introduces tally\_2 for tally\<S(S(Z))\> using the  
template tally\<S(t)\>;  
when generating the body of tally\_2, the compiler introduces tally\_1 for  
tally\<S(Z)\> (as t  
is mapped to S(Z) this time); when generating the body of tally\_1; the  
compiler introduces  
tally\_0 (as t is mapped to Z this time).On Friday, March 6, 2015 at 8:41:55 PM UTC-5, H Zhang wrote:

> I find the example intriguing and I am trying to understand how the  
> unrolling is actually carried out. Is this paper  
> [http://www.cs.bu.edu/~hwxi/academic/papers/icfp05.pdf](http://www.cs.bu.edu/~hwxi/academic/papers/icfp05.pdf) still a good  
> reference on how this part of ATS works? The erasure function defined in  
> fig 6 seems to simply remove the proof terms. Could you talk about how the  
> program transformation is achieved or give a reference paper?
> 
> Thanks,  
> Haitao
> 
> On Friday, February 20, 2015 at 12:42:15 PM UTC-8, gmhwxi wrote:
> 
> > I added an example of loop unrolling here:
> > 
> > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats)
> > 
> > I do have a plan to implement the following feature
> > 
> > fun vec\_add{i:int}(…) =  
> > sif i == 0  
> > then …  
> > else …
> > 
> > vec\_add{10}(…) can then be expanded at compile-time.
> > 
> > On Friday, February 20, 2015 at 10:29:50 AM UTC-5, Artyom Shalkhakov wrote:
> > 
> > > On Friday, February 20, 2015 at 8:58:01 PM UTC+6, Shea Levy wrote:
> > > 
> > > > Hi all,
> > > > 
> > > > I know ATS2 supports function templates, and polymorphic functions for  
> > > > boxed types. Is there any support for runtime polymorphism (with vtables or  
> > > > standalone structs of functions or similar) available in the language?
> > > 
> > > HX once implemented Smalltalk-style objects in an ATS prototype, but I  
> > > think he abandoned the plan to implement it a long time ago.
> > > 
> > > > Related question: Is there support for _value_ templates?
> > > 
> > > Are you asking about something like:
> > > 
> > > fun{i:pos}  
> > > vec\_add(x: &(@[float][i]), y: &(@[float][i]), o: &(@[float][i])): void =  
> > > // do an element-wise addition, putting results into [o]  
> > > // i is known at compile time, so we can get it at runtime  
> > > // using something like constant\<int,i\>, similarly to sizeof
> > > 
> > > I’d like to know the answer as well.
> > > 
> > > > ~Shea

---

<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:** [March 6, 2015, 3:07am UTC](https://discourse.ats-lang.org/t/runtime-polymorphism-for-unboxed-types/540/9 "2015-03-06T03:07:32Z")

</div>

See the attached file.

If you do:

patscc -O2 -S dotprod\_dats.c

you can see that gcc can actually does a very thorough job:

dotprod3(…) is already evaluated to 14 at compile-time.On Thu, Mar 5, 2015 at 10:01 PM, Artyom Shalkhakov \< [artyom.s...@gmail.com](mailto:artyom.s...@gmail.com)\> wrote:

> On Friday, March 6, 2015 at 6:27:26 AM UTC+6, gmhwxi wrote:
> 
> > I tried to implement what is mentioned below. Unfortunately, my plan did  
> > not pan out  
> > well. The simple reason is that the condition following each ‘sif’ is  
> > static but the static  
> > information is essentially all erased at the stage where ‘sif’ needs to  
> > be eliminated.  
> > I could try to eliminate ‘sif’ at the point where such information is  
> > still available but this  
> > means templates making use of ‘sif’ cannot be handled (as template  
> > instantiation happens  
> > at a later stage).
> > 
> > For now, I think we simply need to implement a version of array\_foreach  
> > that supports  
> > loop unrolling. For instance, see:
> > 
> > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/)  
> > EXAMPLE/TYPEVAL/dotprod.dats
> 
> Could you post the resulting C code for dotprod.dats? I’d really like to  
> take a look, even if it’s very long.
> 
> > On Monday, March 2, 2015 at 12:22:36 PM UTC-5, Shea Levy wrote:
> > 
> > > Cool! If we had that feature, would we be able to parameterize function  
> > > templates by any sort, and have full use of scase/sif in the  
> > > implementations? That would be very useful, I’m currently adapting your  
> > > example to implement type lists (to use where in C++ I’d use variadic  
> > > templates), but having a proper type list datasort would be much nicer than  
> > > passing around proofs that a given list is proper 🙂
> > > 
> > > ~Shea
> > > 
> > > On Feb 20, 2015, at 3:42 PM, gmhwxi [gmh...@gmail.com](mailto:gmh...@gmail.com) wrote:
> > > 
> > > I added an example of loop unrolling here:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/)  
> > > EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats
> > > 
> > > I do have a plan to implement the following feature
> > > 
> > > fun vec\_add{i:int}(…) =  
> > > sif i == 0  
> > > then …  
> > > else …
> > > 
> > > vec\_add{10}(…) can then be expanded at compile-time.
> > > 
> > > On Friday, February 20, 2015 at 10:29:50 AM UTC-5, Artyom Shalkhakov wrote:
> > > 
> > > > On Friday, February 20, 2015 at 8:58:01 PM UTC+6, Shea Levy wrote:
> > > > 
> > > > > Hi all,
> > > > > 
> > > > > I know ATS2 supports function templates, and polymorphic functions for  
> > > > > boxed types. Is there any support for runtime polymorphism (with vtables or  
> > > > > standalone structs of functions or similar) available in the language?
> > > > 
> > > > HX once implemented Smalltalk-style objects in an ATS prototype, but I  
> > > > think he abandoned the plan to implement it a long time ago.
> > > > 
> > > > > Related question: Is there support for _value_ templates?
> > > > 
> > > > Are you asking about something like:
> > > > 
> > > > fun{i:pos}  
> > > > vec\_add(x: &(@[float][i]), y: &(@[float][i]), o: &(@[float][i])): void =  
> > > > // do an element-wise addition, putting results into [o]  
> > > > // i is known at compile time, so we can get it at runtime  
> > > > // using something like constant\<int,i\>, similarly to sizeof
> > > > 
> > > > I’d like to know the answer as well.
> > > > 
> > > > > ~Shea
> > > 
> > > –  
> > > 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...@googlegroups.com](mailto:ats-lang...@googlegroups.com).  
> > > To post to this group, send email to [ats-l...@googlegroups.com](mailto:ats-l...@googlegroups.com).  
> > > Visit this group at [http://groups.google.com/group/ats-lang-users](http://groups.google.com/group/ats-lang-users).  
> > > To view this discussion on the web visit [https://groups.google](https://groups.google).  
> > > com/d/msgid/ats-lang-users/4e92a56c-bb36-4901-aa58-  
> > > 205fcc67f373%[40googlegroups.com](http://40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/ats-lang-users/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/4e92a56c-bb36-4901-aa58-205fcc67f373%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .
> > > 
> > > –  
> > > 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](mailto:ats-lang-user...@googlegroups.com).  
> > > To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> > > Visit this group at [http://groups.google.com/group/ats-lang-users](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/7c7a0e69-5bb1-4090-bdd3-57f97adb2070%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/7c7a0e69-5bb1-4090-bdd3-57f97adb2070%40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/ats-lang-users/7c7a0e69-5bb1-4090-bdd3-57f97adb2070%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/7c7a0e69-5bb1-4090-bdd3-57f97adb2070%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .

[dotprod\_dats.c](https://us1.discourse-cdn.com/flex019/uploads/ats_lang/original/1X/8d8e890d02ca4588435fc74520dd6edcf8fdfdca.c) (174 KB)

---

<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:** [February 20, 2015, 8:42pm UTC](https://discourse.ats-lang.org/t/runtime-polymorphism-for-unboxed-types/540/10 "2015-02-20T20:42:15Z")

</div>

I added an example of loop unrolling here:

> <https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-02-20.dats>

I do have a plan to implement the following feature

fun vec\_add{i:int}(…) =  
sif i == 0  
then …  
else …

vec\_add{10}(…) can then be expanded at compile-time.On Friday, February 20, 2015 at 10:29:50 AM UTC-5, Artyom Shalkhakov wrote:

> On Friday, February 20, 2015 at 8:58:01 PM UTC+6, Shea Levy wrote:
> 
> > Hi all,
> > 
> > I know ATS2 supports function templates, and polymorphic functions for  
> > boxed types. Is there any support for runtime polymorphism (with vtables or  
> > standalone structs of functions or similar) available in the language?
> 
> HX once implemented Smalltalk-style objects in an ATS prototype, but I  
> think he abandoned the plan to implement it a long time ago.
> 
> > Related question: Is there support for _value_ templates?
> 
> Are you asking about something like:
> 
> fun{i:pos}  
> vec\_add(x: &(@[float][i]), y: &(@[float][i]), o: &(@[float][i])): void =  
> // do an element-wise addition, putting results into [o]  
> // i is known at compile time, so we can get it at runtime  
> // using something like constant\<int,i\>, similarly to sizeof
> 
> I’d like to know the answer as well.
> 
> > ~Shea
