You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ATS2中创建值为自定义数据类型的Map/Hashtable?

Fixing Undefined Symbol Errors with Custom Types in ATS2 Funmaps

Let's break down the issues you're facing and fix them step by step:

Root Causes

  1. Missing Required Type Implementations: ATS2's funmap (specifically the avltree-backed implementation) expects custom item types to provide standard serialization/printing functions, even if you don't explicitly use them in your code. The first error occurred because your example type lacked tostrptr_val, tostring_val, and fprint_val implementations.
  2. Incorrect Template Implementation Syntax: Your second error (undefined fprint_example_5) comes from using angle brackets <> instead of curly braces {} when implementing template functions like tostrptr_val. ATS requires curly braces to bind type parameters to template implementations, so your code wasn't properly linking the functions to the example type.

Corrected Code

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

datatype example = example of (string)

// Implement required conversion/print functions for the example type
fn tostrptr_example(ex: example):<!wrt> Strptr1 = 
  let val example(s) = ex in strptr$create(s) end // Properly convert to Strptr1

fn tostring_example(ex: example):<> string = 
  let val example(s) = ex in s end // Return the underlying string

fn fprint_example(out: FILEref, ex: example): void = 
  fprint!(out, tostring_example(ex))

// Correctly bind implementations to the example type using curly braces
implement tostrptr_val{example} = tostrptr_example
implement tostring_val{example} = tostring_example
implement fprint_val{example} = fprint_example

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

val test = myfunmap_nil()

implement main0() = ()

Key Fixes Explained

  • Template Implementation Syntax: Switched from <example> to {example} in the implement statements. This tells ATS to associate your custom functions with the example type for the template functions.
  • Proper Strptr1 Conversion: Replaced the unsafe cast with strptr$create to correctly convert the underlying string of example to a Strptr1 (this avoids undefined behavior from unsafe casts).
  • Explicit Value Extraction: Used pattern matching (val example(s) = ex) to safely access the string inside the example datatype, making the code more robust and readable.

Why This Works

The funmap implementation relies on these standard functions for internal operations (like debugging output or memory management helpers). By providing correct, syntactically valid implementations, you satisfy the implicit requirements of the library code, resolving both the compilation and linking errors.

内容的提问来源于stack exchange,提问作者Julian Fondren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:53:32