SML:使用 abstype 和使用签名来隐藏结构的实现有什么区别? [英] SML: What's the difference between using abstype and using a signature to hide the implementation of a structure?

查看:37
本文介绍了SML:使用 abstype 和使用签名来隐藏结构的实现有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去我在 SML 方面做过一些工作,但现在我开始接触更有趣的部分.

I've done a little work in SML in the past, but I'm now starting to get to the more interesting parts.

使用 abstype...with...end 构造,我可以做一些事情,但隐藏它们的实现细节.我还可以创建我想要制作的东西的签名,并使用 :> 运算符来创建一个遵守该签名的结构,以隐藏实现细节.

Using the abstype...with...end construct, I can make things but keep their implementation details hidden. I can also create a signature of the thing I want to make, and use the :> operator to make a structure adhering to that signature that keeps the implementation details hidden.

签名/结构不只是 abstype 的更通用版本吗?我可以用 abstypes 做什么,而我不能用签名/结构来做?我为什么要使用 abstype?

Aren't signatures/structures just a more general version of abstypes? What can I do with abstypes that I can't do with signatures/structures? Why would I ever want to use abstype?

预先感谢您的帮助!

举个例子:

signature SET = sig
    type set
    val empty: set
    val insert: int * set -> set
    val member: int * set -> bool
end

structure Set :> SET = struct
    type set = int list
    val empty = []
    fun insert(x, s) = x::s
    fun member(x, []) = false
      | member(x, h::t) = (x = h) orelse member(x, t)
end

看起来至少和

abstype AbsSet = absset of int list with
    val empty = absset([])
    fun insert(x, absset(s)) = absset(x::s)
    fun member(x, absset([])) = false
      | member(x, absset(h::t)) = (x = h) orelse member(x, absset(t))
end

推荐答案

我为什么要使用 abstype?

Why would I ever want to use abstype?

从最简单的开始,你不会.至少我想不出一个好的理由.

Starting with the easiest, you won't. Atleast I can't come up with one good reason.

签名/结构不只是 abstype 的更通用版本吗?

Aren't signatures/structures just a more general version of abstypes?

好吧,我想我们必须看看 SML 的历史.不透明 (... :> ...) 签名匹配不是 SML '90 的一部分,如有关模块 1.3.9.不透明签名匹配:>

Well, I guess we have to take a look at the history of SML. The opaque (... :> ...) signature matching was not part of SML '90 as explained on this smlnj document about modules 1.3.9. opaque signature matching :>

... 其目的是创建签名 SIG 的抽象"实例.由于各种原因,SML '90 忽略了此功能,但确实需要它.

... whose purpose was to create an "abstract" instance of the signature SIG. This feature was left out of SML '90 for various reasons, but the need for it was real.

我不知道不包括它的原因,但据我所知,McQueen 是 abstype 的更远",它是 SML '90 的一部分,由于某种原因没有在 SML '97 中删除(也许向后兼容?)

I have no idea about the reasoning for not including it, but as far as I know McQueen was the "farther" of the abstype which was part of SML '90 and for some reason wasn't removed in SML '97 (maybe backwards compatibility?)

然而,它们之间有根本的区别,abstype 是核心语言的一部分,其中模块/签名/函子是模块系统的一部分.

There is however a fundamentally difference between them, abstype is part of the core language where modules/signatures/functors are part of the module system.

我可以用 abstype 做什么而我不能用签名/结构来做?

What can I do with abstypes that I can't do with signatures/structures?

我想不出什么.但是,我很确定构建一些示例很容易,您可以使用不透明签名匹配而不能使用 abstypes.

I can't come up with anything. However I'm pretty sure it would be easy to construct some example that you can using opaque signature matching and can't do with abstypes.

更新

页面将abstype降级为派生形式实际上来自successor-ml wiki包含一个关于 abstype 是剩余物的非正式描述.

The page Degrade abstype to derived form from the successor-ml wiki actually contains a tiny informal description about abstype being a leftover.

与许多其他内容一样,它们也指 Defects in the Revised Definition of Standard ML 论文,其中包含有关一些次要"的详细信息abstype 定义中的错误/缺陷,尽管它们的链接已失效.标准ML的修订定义"是SML '97的定义.

As with many other, they also refer to the sections of the Defects in the Revised Definition of Standard ML paper which contains details about some "minor" errors/defects in the definition of abstype, although their link being dead. The "Revised Definition of Standard ML" is the definition of SML '97.

这篇关于SML:使用 abstype 和使用签名来隐藏结构的实现有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆