Polymorphic datatype

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

great, I will use number.hats !
thanks

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

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

Note that the following files are available for use:

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

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

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 !

Now I remember :slight_smile:

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

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.