2

如何在从函子的结果派生类型的结构中使用的签名中引用类型。下面是一个使用 poly 解释器的例子:

> signature Res = sig type f end;
signature Res = sig type f end
> functor F (A: sig type t end) : Res = struct datatype f = None | Some end;
functor F (A: sig type t end): Res
> structure S = struct local structure A = F(struct type t = int end) in type t = A.f list end end;
structure S: sig type t = A.f list end

首先,我不明白为什么 Af 在结构本地时会出现在结果签名中。其次,如何创建与此结构 S 匹配的签名?

像这样的东西不起作用:

signature SSig = sig type t = F(struct type t = int end).t list end

此外,如果类型 f 是 int 而不是数据类型,S 最终会以某种方式意识到 f 是 int 而不是被签名隐藏。即使使用不透明签名不显示 int,这似乎也不是合理的行为。

> functor F (A: sig type t end) : Res = struct type f = int end;
functor F (A: sig type t end): Res
> structure S = struct local structure A = F(struct type t = int end) in type t = A.f list end end;
structure S: sig type t = int list end
> functor F(A: sig type t end):> Res = struct type f = int end;
functor F (A: sig type t end): Res
> structure S = struct local structure A = F(struct type t = int end) in type t = A.f list end end;
structure S: sig type t = A.f list end
4

2 回答 2

2

我对你的第一个问题没有真正的答案,只是一个猜测,所以我不会对此发表评论。安德烈亚斯·罗斯伯格可能会澄清那里的事情:)

关于你的第二个问题。我不明白你为什么要在签名中实例化一个仿函数。也许你想要这个?

signature Res =
  sig
    type f
  end

signature SSig =
  sig
    structure R : Res
    type t = R.f list
  end

然后,任何实现SSig的人都可以自由地将调用的结果分配给FR结构。

关于你的最后一点。除非您不透明地实现签名,否则不会隐藏类型。

于 2016-09-27T12:22:41.797 回答
1

[...] 为什么 Af 在结构本地时会出现在结果签名中。

这似乎是 Poly/ML 的产物。莫斯科 ML似乎没有泄露名称:

Moscow ML version 2.10
Enter `quit();' to quit.
- signature Res = sig type f end;
> signature Res = /\f.{type f = f}
- functor F (A: sig type t end) : Res =
  struct
    datatype f = None | Some
  end;
> functor F : !t.{type t = t}->?=f.{type f = f}
- structure S =
  struct
    local structure A = F(struct type t = int end)
    in type t = A.f list
    end
  end;
> New type names: =f
  structure S : {type t = f list}

如何在从函子的结果派生类型的结构中使用的签名中引用类型?

(评论)问题是我不想在 SSig 中使类型不透明,但我也不想在签名中包含 R,因为我不希望消费者可以访问它。我可以使用不透明类型 f = Rf 做类似的事情,但我必须将其包含在签名中,并且再次使签名变得混乱。

Drup 最近关于如何在设计模块时决定是在类型级别还是模块级别进行参数化的回答讨论了单态化模块输入类型的缺点。

当所有 R 包含的都是类型 t 时,“在 SSig 中不透明”和“在 SSig 的签名中不包括 R”不是等效的吗?也许 Res 包含的东西比类型 t 多,在这种情况下你可以提供structure R : sig type t end。或者也许 Ionuț 答案的这种变体更可取:

signature Res =
sig
  type t (* rather than structure R *)
  type f = t list
end

functor F (A : sig type t end) : Res =
struct
  type t = A.t
  type f = t list
end

structure S = F(struct type t = int end)

我不确定如何避免type f = t list.

于 2016-09-27T14:29:28.077 回答