# Obtaining the type of a dynamic constant

**URL:** <https://discourse.ats-lang.org/t/obtaining-the-type-of-a-dynamic-constant/404>\
**Category:** Google Group\
**Created:** [August 29, 2015, 6:38pm UTC](https://discourse.ats-lang.org/t/obtaining-the-type-of-a-dynamic-constant/404 "2015-08-29T18:38:06Z")\
**Posts on this page:** 2\
**Page:** 1

<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:** [August 29, 2015, 6:38pm UTC](https://discourse.ats-lang.org/t/obtaining-the-type-of-a-dynamic-constant/404/1 "2015-08-29T18:38:06Z")

</div>

In ATS, when a dynamic constant (usually a function)  
is declared, its type must be given as well. For instance:

extern  
fun{a:t@ype}  
list\_append{m,n:nat} (list(a, m), list(a, n)): list(a, m+n)

Sometimes, it can be very convenient if we have a way to  
refer to the type assigned to a dynamic constant. For instance,  
list\_append can be implemented as follows:

implement  
{a} // tmparg  
list\_append =  
append where  
{  
//  
fun append  
: $d2ctype(list\_append) =  
lam(xs, ys) =\>  
case+ xs of list\_nil() =\> ys | list\_cons(x, xs) =\> list\_cons(x, append(  
xs, ys))  
//  
} // end of [list\_append]

where the expression $d2ctype(list\_append) refers to the type assigned  
to list\_append.

This is now supported in ATS2-0.2.2 (just released).

Of course, list\_append can also be implemented as follows:

implement  
{a} // tmparg  
list\_append(xs, ys) =  
case+ xs of  
| list\_nil() =\> ys  
| list\_cons(x, xs) =\> list\_cons(x, list\_append(xs, ys))  
// end of [list\_append]

This implementation makes use of recursive template instantiation, which is  
not a preferred  
style as it can cause complications during the compilation of template  
instantiation.

---

<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:** [November 9, 2015, 11:40pm UTC](https://discourse.ats-lang.org/t/obtaining-the-type-of-a-dynamic-constant/404/2 "2015-11-09T23:40:20Z")

</div>

I just realized that list\_append should be implemented as follows:

implement  
{a} // tmparg  
list\_append =  
fix append  
: $d2ctype(list\_append) =\>  
lam(xs, ys) =\>  
case+ xs of list\_nil() =\> ys | list\_cons(x, xs) =\> list\_cons(x, append(  
xs, ys))  
// end of [list\_append]On Saturday, August 29, 2015 at 2:38:06 PM UTC-4, gmhwxi wrote:

 

> In ATS, when a dynamic constant (usually a function)  
> is declared, its type must be given as well. For instance:
> 
>  
> 
> extern  
> fun{a:t@ype}  
> list\_append{m,n:nat} (list(a, m), list(a, n)): list(a, m+n)
> 
>  
> 
> Sometimes, it can be very convenient if we have a way to  
> refer to the type assigned to a dynamic constant. For instance,  
> list\_append can be implemented as follows:
> 
>  
> 
> implement  
> {a} // tmparg  
> list\_append =  
> append where  
> {  
> //  
> fun append  
> : $d2ctype(list\_append) =  
> lam(xs, ys) =\>  
> case+ xs of list\_nil() =\> ys | list\_cons(x, xs) =\> list\_cons(x,  
> append(xs, ys))  
> //  
> } // end of [list\_append]
> 
>  
> 
> where the expression $d2ctype(list\_append) refers to the type assigned  
> to list\_append.
> 
>  
> 
> This is now supported in ATS2-0.2.2 (just released).
> 
>  
> 
> Of course, list\_append can also be implemented as follows:
> 
>  
> 
> implement  
> {a} // tmparg  
> list\_append(xs, ys) =  
> case+ xs of  
> | list\_nil() =\> ys  
> | list\_cons(x, xs) =\> list\_cons(x, list\_append(xs, ys))  
> // end of [list\_append]
> 
>  
> 
> This implementation makes use of recursive template instantiation, which  
> is not a preferred  
> style as it can cause complications during the compilation of template  
> instantiation.
