# Polymorphic datatype

**URL:** <https://discourse.ats-lang.org/t/polymorphic-datatype/366>\
**Category:** Google Group\
**Created:** [June 4, 2013, 2:41pm UTC](https://discourse.ats-lang.org/t/polymorphic-datatype/366 "2013-06-04T14:41:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [June 4, 2013, 2:41pm UTC](https://discourse.ats-lang.org/t/polymorphic-datatype/366/1 "2013-06-04T14:41:24Z")

</div>

> unfortunately, I have problem when using separate files for this code
> 
> > snippet.
> 
> If I use three files get\_value.sats, get\_value.dats and main.dats, I
> 
> > cannot make it working…
> 
> — get\_value.sats —
> 
> datatype color = Black | Red
> 
> datatype tree (a:t@ype) =
> 
> Empty(a) of ()
> 
> | Leaf(a) of (color, tree a, a, tree a)
> 
> fun{a:t@ype} add\_x(w: a, x: a): a
> 
> fun{a:t@ype} get\_value(t: tree a, x: a) : a
> 
> — get\_value.dats —
> 
> staload “get\_value.sats”
> 
> implement add\_x(w,x) = w + x
> 
> implement add\_x(w,x) = w + x
> 
> implement{a} get\_value(t, x) = case+ t of
> 
> | Leaf(\_, \_, v0, \_) =\> add\_x(v0, x)
> 
> | \_ =\> x
> 
> — main.dats —
> 
> staload “get\_value.sats”
> 
> dynload “get\_value.dats” // I put dynamic here so add\_x defined in
> 
> > get\_value.dats can be seen
> 
> implement main(argc, argv) = () where {
> 
> val () = print(get\_value(Leaf(Black, Empty, 10.1f, Empty),
> 
> > 3.14159f)) // for floats
> 
> val () = print\_newline()
> 
> val () = print(get\_value(Leaf(Black, Empty, 10, Empty), 31)) // for
> 
> > integers
> 
> val () = print\_newline()
> 
> }
> 
> I compile with : @ atscc -o test get\_value.sats get\_value.dats main.dats
> 
> and the error is : main.dats: 489(line=24, offs=18) – 505(line=24,
> 
> > offs=34): error(ccomp): the template definition for [get\_value] is  
> > unavailable at [get\_value$1696\_ats\_float\_type].
> 
> You need to add a line:  
> staload “get\_value.sats”  
> staload \_(_anon_) = “get\_value.dats” // **add this one**  
> dynload “get\_value.dats” // I put dynamic here so add\_x defined in  
> get\_value.dats can be seen

I added the line but still have an error :  
get\_value.dats: 163(line=5, offs=26) – 168(line=5, offs=31): error(ccomp):  
the template definition for [add\_x] is unavailable at  
[add\_x$1695\_ats\_float\_type].

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [June 4, 2013, 6:03pm UTC](https://discourse.ats-lang.org/t/polymorphic-datatype/366/2 "2013-06-04T18:03:27Z")

</div>

great, I will use number.hats !  
thanks

---

<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:** [June 5, 2013, 12:50pm UTC](https://discourse.ats-lang.org/t/polymorphic-datatype/366/3 "2013-06-05T12:50:59Z")

</div>

Say you rename number.hats as number2.hats. Feel free to modify it as you  
wish.

---

<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:** [June 4, 2013, 3:23pm UTC](https://discourse.ats-lang.org/t/polymorphic-datatype/366/4 "2013-06-04T15:23:38Z")

</div>

Right, this is how we do it in ATS. It should be much easier to do this in  
ATS2.

---

<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:** [June 4, 2013, 3:35pm UTC](https://discourse.ats-lang.org/t/polymorphic-datatype/366/5 "2013-06-04T15:35:54Z")

</div>

Note that the following files are available for use:

$ATSHOME/prelude/SATS/number.sats  
$ATSHOME/prelude/HATS/number.hats

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [June 5, 2013, 8:55am UTC](https://discourse.ats-lang.org/t/polymorphic-datatype/366/6 "2013-06-05T08:55:23Z")

</div>

mm I have problems using number.hats with my OS which is FreeBSD.

the gcc compiler complains about lacking of complex functions missing  
(undefined reference to `ccos’, …)

I suspect a problem with c99 complex extensions implementation in gcc. I  
have posted a message in the FreeBSD forum.

Is that anyone using freeBSD with ATS here ?

Maybe it is possible to disable the complex feature in ATS in order to use  
number.hats anyway ?

thanks

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [June 4, 2013, 3:02pm UTC](https://discourse.ats-lang.org/t/polymorphic-datatype/366/7 "2013-06-04T15:02:09Z")

</div>

ok,  
So I would make a separate file :

— numeric\_gen.hats —  
implement add\_x(w,x) = w + x  
implement add\_x(w,x) = w + x

and include it from main.dats with :

#include “numeric\_gen.hats”

thanks a lot !

---

<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:** [June 4, 2013, 2:50pm UTC](https://discourse.ats-lang.org/t/polymorphic-datatype/366/8 "2013-06-04T14:50:57Z")

</div>

Now I remember 🙂

The following two lines needs to be moved to main.dats:

implement add\_x(w,x) = w + x

implement add\_x(w,x) = w + x

---

<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:** [June 4, 2013, 2:48pm UTC](https://discourse.ats-lang.org/t/polymorphic-datatype/366/9 "2013-06-04T14:48:09Z")

</div>

> > dynload “get\_value.dats” // I put dynamic here so add\_x defined in  
> > get\_value.dats can be seen

First, dynload is for doing some kind dynamic initialization of values.

You need

staload \_ = “get\_value.dats”

to make the template definitions available to the compiler.
