17.04.2016 Views

sml

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

18.1 Signatures 145<br />

signature QUEUE WITH EMPTY =<br />

sig<br />

include QUEUE<br />

val is empty : ’a queue -> bool<br />

end<br />

As the notation suggests, the signature QUEUE is included into the body of<br />

the signature QUEUE WITH EMPTY, and an additional component is added.<br />

It is not strictly necessary to use include to define this signature. Indeed,<br />

we may define it directly using the following signature binding:<br />

signature QUEUE WITH EMPTY =<br />

sig<br />

type ’a queue<br />

exception Empty<br />

val empty : ’a queue<br />

val insert : ’a * ’a queue -> ’a queue<br />

val remove : ’a queue -> ’a * ’a queue<br />

val is empty : ’a queue -> bool<br />

end<br />

There is no semantic difference between the two definitions of QUEUE WITH EMPTY.<br />

Signature inclusion is a convenience that documents the “history” of how<br />

the more refined signature was created.<br />

Signature specialization is used to augment an existing signature with<br />

additional type definitions. For example, if we wish to refine the signature<br />

QUEUE to specify that the type constructor ’a queue must be defined as a<br />

pair of lists, we may proceed as follows:<br />

signature QUEUE AS LISTS =<br />

QUEUE where type ’a queue = ’a list * ’a list<br />

The where type clause “patches” the signature QUEUE by adding a definition<br />

for the type constructor ’a queue.<br />

The signature QUEUE AS LISTS may also be defined directly as follows:<br />

REVISED 11.02.11 DRAFT VERSION 1.2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!