【问题标题】:withtype fails with mlton and succeeds with smlnjwithtype 用 mlton 失败,用 smlnj 成功
【发布时间】:2012-10-11 15:48:33
【问题描述】:

我正在编写一个应用程序,我想在数据类型声明的 withtype 子句中定义许多类型。下面的代码 sn -p 演示一下:

datatype ta = A
withtype tb = int
     and tc = tb

mlton 无法编译此代码,尽管 smlnj 成功。

$ mlton -stop o test.sml
Error: test.sml 3.15.
  Undefined type tb.
compilation aborted: parseAndElaborate reported errors

我正在使用 mlton-20100608 和 smlnj-110.71。

这是 mlton 中的错误吗?

如果没有这种声明,我不知道该怎么做:一组相互递归的数据类型和类型。

这个想法来自 Andrew Appel 在他的书 Modern Compiler Implementation in ML,第 98 页,文件 absyn.sml 中为 Tiger 语言提出的抽象语法树类型

【问题讨论】:

    标签: types recursion sml smlnj


    【解决方案1】:

    根据 mlton 社区的 Andreas Rossberg 的说法,这不是 mlton 的错误,而是 SML/NJ 与标准的(已知)偏差。根据规范,withtype 后面的类型缩写不是相互递归的,只有前面的数据类型。该示例被重写为

    datatype ta = A
    type tb = int
    and tc = tb
    

    也就是说,MLton 正确地标记了这一点。

    总是可以在右侧扩展其他类型的构造函数,因此没有真正的限制。不过,这样做可能会很麻烦。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-02
      • 2014-02-16
      • 2015-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多