Search code examples
ats

How do you create a map or hashtable with a datatype value?


I feel like I've asked this very question and gotten an answer before, but I'm not able to find that Q&A.

Hashtables and maps as documented in the ATS2 tutorial have undocumented requirements. The big problem is that I can't see figure out those requirements from seeing the errors that result from not providing them.

So for example this string -> datatype map:

#include "share/atspre_staload.hats"
#include "share/atspre_staload_libats_ML.hats"

datatype example = example of (string)

// commented: this doesn't change the error output
//fn fprint_example(out: FILEref, w: example): void = fprint!(out, "(not implemented)")
//overload fprint with fprint_example

local
        typedef key = string
        and itm = example
        staload "libats/ML/SATS/funmap.sats"
in
        #include "libats/ML/HATS/myfunmap.hats"
end

val test = myfunmap_nil()

implement main0() = ()

fails to build with these errors:

patscc -DATS_MEMALLOC_GCBDW -o test test.dats -latslib -lgc
test_dats.c:20972:54: warning: implicit declaration of function 'S2Ecst' is invalid in C99 [-Wimplicit-function-declaration]
ATSINSmove(tmp859__1, PMVtmpltcstmat[0](tostrptr_val<S2Ecst(example)>)(arg1)) ;
                                                     ^
test_dats.c:20972:61: error: use of undeclared identifier 'example'
ATSINSmove(tmp859__1, PMVtmpltcstmat[0](tostrptr_val<S2Ecst(example)>)(arg1)) ;
                                                            ^
test_dats.c:20972:41: error: use of undeclared identifier 'tostrptr_val'
ATSINSmove(tmp859__1, PMVtmpltcstmat[0](tostrptr_val<S2Ecst(example)>)(arg1)) ;
                                        ^
test_dats.c:20972:70: error: expected expression
ATSINSmove(tmp859__1, PMVtmpltcstmat[0](tostrptr_val<S2Ecst(example)>)(arg1)) ;
                                                                     ^
test_dats.c:20972:23: error: use of undeclared identifier 'PMVtmpltcstmat'
ATSINSmove(tmp859__1, PMVtmpltcstmat[0](tostrptr_val<S2Ecst(example)>)(arg1)) ;
                      ^
1 warning and 4 errors generated.
make: *** [test] Error 1

So it seems that a tostrptr_val<example> is unimplemented... ... and at this point, of forming this question, I found prelude/*/tostring where this stuff is implemented. OK. That brought me to

#include "share/atspre_staload.hats"
#include "share/atspre_staload_libats_ML.hats"

datatype example = example of (string)

fn tostrptr_example(ex: example):<!wrt> Strptr1 = $UNSAFE.castvwtp0{Strptr1}("not implemented")
fn tostring_example(ex: example):<> string = "not implemented"
fn fprint_example(out: FILEref, ex: example): void = fprint!(out, tostring_example(ex))
implement tostrptr_val<example> = tostrptr_example
implement tostring_val<example> = tostring_example
implement fprint_val<example> = fprint_example

local
        typedef key = string
        and itm = example
        staload "libats/ML/SATS/funmap.sats"
in
        #include "libats/ML/HATS/myfunmap.hats"
end

val test = myfunmap_nil()

implement main0() = ()

which still doesn't compile:

patscc -DATS_MEMALLOC_GCBDW -o test test.dats -latslib -lgc
test_dats.c:20765:31: warning: implicit declaration of function 'fprint_example_5' is invalid in C99 [-Wimplicit-function-declaration]
ATSINSmove_void(tmpret822__1, fprint_example_5(env0, arg1)) ;
                              ^
1 warning generated.
Undefined symbols for architecture x86_64:
  "_fprint_example_5", referenced from:
      _ATSLIB_056_libats_056_funmap_avltree__funmap_foreach__fwork__123__1 in test_dats-bd83ab.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1

All I can decipher from this is that a single function in /libats/DATS/funmap_avltree.dats is using example in a manner I haven't prepared for.


Solution

  • You need to implement a fprint_val instanace for printing 'example':

    implement fprint_val<example>(out, x) = ...