如何在 Standard-ML 中的数据类型中指定类型约束?

How to specify type constraints in a datatype in Standard-ML?

考虑标准 ML 中的这种数据类型:

datatype 't options = Null
                   | Some of 't

我还有其他数据类型:

datatype option = Apple | Orange | Melon

我想指定 datatype options 应该只接受 datatype option 作为其输入类型 't。如何做到这一点?

SML 中没有这样的功能,也没有作为我听说过的任何 SML 方言的扩展。

您可以将更专业的版本定义为类型别名:

type option_options = option options

如果需要,您可以在模块的签名中使该类型抽象化,但这会隐藏构造函数。

这个怎么样,自制选项:

strobel@suse132-intel:~> sml
Standard ML of New Jersey v110.79 [built: Tue Dec 22 21:53:32 2015]
- datatype fruits = Apple | Orange | Melon;
datatype fruits = Apple | Melon | Orange
- datatype fruitopt = Nofruit | Fruit of fruits;
datatype fruitopt = Fruit of fruits | Nofruit
-