如何创建具有数据类型值的映射或哈希表?

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

我觉得我以前问过这个问题并得到了答案,但我找不到那个问答。

ATS2 tutorial 中记录的哈希表和地图具有未记录的要求。最大的问题是,我无法从看到因未提供这些要求而导致的错误中找出这些要求。

例如这个字符串 -> 数据类型映射:

#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() = ()

构建失败并出现以下错误:

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

所以似乎 tostrptr_val<example> 未实现... ...在这一点上,在形成这个问题时,我发现 prelude/*/tostring 实现了这些东西。好的。这让我想到了

#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() = ()

仍然无法编译:

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

我能从中破译的是 /libats/DATS/funmap_avltree.dats 中的一个函数正在以我没有准备好的方式使用 example

您需要实现一个 fprint_val 打印实例 'example':

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