代码之家  ›  专栏  ›  技术社区  ›  Julian Fondren

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

ats
  •  1
  • Julian Fondren  · 技术社区  · 7 年前

    我觉得我已经问过这个问题,并得到了一个答案,但我找不到那个问题。

    哈希表和映射如 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> 未实现… …在这一点上,在形成这个问题的过程中,我发现了序曲/*/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 在某种程度上我还没有准备好。

    1 回复  |  直到 7 年前
        1
  •  0
  •   cs320 bucas    7 年前

    要打印“example”,您需要实现一个fprint“val instanace”:

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