Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SML: What's the difference between using abstype and using a signature to hide the implementation of a structure?

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

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.

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?

Thanks in advance for the help!

As an example:

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

seems at least as powerful as

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
like image 316
Alan Avatar asked Sep 04 '11 01:09

Alan


1 Answers

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.


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

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 :>

... 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.

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?)

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.


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

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.


UPDATE

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

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.

like image 193
Jesper.Reenberg Avatar answered Sep 29 '22 12:09

Jesper.Reenberg