module - 如何避免结构和签名中的 SML 数据类型重复?

标签 module sml algebraic-data-types

给定签名A数据类型为 t , 说

signature A = sig
    datatype t = T of int | S of string
end

是否可以提供没有 t 的实现(结构)?重复?例如,在下面的签名中,t 的定义重复。它适用于小型数据类型,但对于较大的数据类型有点笨拙。
structure AImpl : A = struct
    datatype t = T of int | S of string
end

我的意图只是提供一个接口(interface),以便人们可以了解所有的声明。但我不希望每个实现都重复数据类型定义。

尽管签名和结构似乎都可以包含来自另一个结构的数据类型,但是通过单独检查签名将无法知道数据类型声明。例如:
structure AData = struct
    datatype t = T of int | S of string
end

signature A = sig
    datatype t = datatype AData.t
end

structure a : A = struct
    open AData
end

当然,如果我把两个 ADataA在同一个.sig文件。

最佳答案

不,这是不可能的,因为签名匹配规则在 sml 中是如何工作的。

当您在签名中引入一些非抽象类型时,每个结构都带有
此签名必须提供相同的类型绑定(bind)。原因是签名
在 sml 中主要用于隐藏结构细节。

例如,您可以使用抽象类型向结构用户隐藏数据类型详细信息:

signature A = 
sig
  type t
end


structure AImpl :> A = 
struct
   datatype t = T of int | S of string
end

或者您可以只公开一种类型的构造函数:
signature A = 
sig
  type t
  val T : int -> t
end


structure AImpl :> A = 
struct
   datatype t = T of int | S of string
end

val test = AImpl.T 12;

关于module - 如何避免结构和签名中的 SML 数据类型重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26800360/

相关文章:

module - 我的私有(private)代码在哪里公开?

arrays - 将大值传递给模块

types - 有没有办法约束仿函数的参数签名,以便参数可以为结构提供未指定的相等类型?

syntax - SML 语法 :  `val rec` and `fun` compared to each other

algorithm - 计算列表列表中的元素数量

c# - 结构元组的性能

python - 为什么导入的函数 "as"另一个名称保留其原始__name__?

list - 从列表中获取元素的机制

c - haskell中的递归数据类型

haskell - 在未装箱的向量中