Sml 指签名中涉及函子结果的类型

Sml 指签名中涉及函子结果的类型,sml,polyml,Sml,Polyml,如何在从函子结果派生类型的结构中使用的签名中引用类型。以下是使用多边形解释器的示例: > 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 = struc

如何在从函子结果派生类型的结构中使用的签名中引用类型。以下是使用多边形解释器的示例:

> 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
首先,我不明白为什么A.f出现在结构的局部签名中。第二,如何创建与此结构匹配的签名

像这样的事情是行不通的:

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

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

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

signature Res =
  sig
    type f
  end

signature SSig =
  sig
    structure R : Res
    type t = R.f list
  end
然后,实现
SSig
的人可以自由地将调用
F
的结果分配给
R
子结构

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

[…]为什么A.f在结构的局部签名中出现

这似乎是Poly/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中的t类型不透明,但我也不想在签名中包含R,因为我不想让消费者访问它。我可以用不透明类型f=R.f做类似的事情,但我必须在签名中包含它,这会使签名变得混乱

Drup on最近的回答讨论了模块输入类型单态化的缺点

当R包含的都是t类型时,“在SSig中使t不透明”和“在SSig的签名中不包含R”不是等价的吗?也许Res比type t包含更多的内容,在这种情况下,您可以提供
结构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)

我不确定如何避免重复
类型f=t列表

我担心我可能不得不这样做。问题是我不想在SSig中使t类型不透明,但我也不想在签名中包含R,因为我不希望消费者能够访问它。我可以用不透明类型f=R.f做类似的事情,但我必须在签名中包含它,这会使签名变得混乱。最后一点,我很困惑,因为Res没有给出f的类型定义。但我想,既然F不使用不透明签名,SML可以提取出用于S的签名。不复制
类型F
是关键。在我的真实代码中,
类型f
是应用于字符串结构的RedBlackTree函子的结果。这将生成类型为t的结果StringMap结构。我唯一的选择似乎是创建一个在结构中设置为StringMap.t的
类型sm
,或者在签名中添加一个签名结构RedBlackTrees。但是,我不想这两个都出现在签名中。我已经到了困惑的地步,为什么你指的是附在结构上的类型,而不是签名。我只想说REDBLACKTREERES.t.@eatonphil
REDBLACKTREERES.t
的问题是,在SML中,函子是生成的,这意味着使用同一函子创建的两个不同结构的类型不会被视为相等的。这就是为什么类型必须来自结构而不是签名。@eatonphil为什么要公开
类型f
?该签名的客户端应该如何使用它?@eatonphil:另外,为引用“真实世界的标准ML代码”而竖起大拇指