• No results found

Our signatures will declare finitely many sorts, constants and operations, so we make a restriction.

Definition (Finite Signatures) A signature is finite if: (i) there are finitely many sorts, i.e.,S is finite; and

(ii) there are finitely many constants and operations, i.e., Σw,s 6=∅for at most finitely many

w,s.

Henceforth we make the assumption:

Assumption We will assume that each signature Σ is finite. Now let us look at lots of examples of finite signatures.

4.2

Examples of Signatures

We will illustrate the general idea of signature by giving signatures for some of the algebras of Booleans and numbers we met in Chapter 3, and for some new data. For particular algebras, a number of choices of notation for the sorts, constants and operations in a signature are always available. Sometimes we write operators in prefix form, sometimes in infix, postfix or mixfix. Our examples will be displayed rather than presented as tuples.

4.2.1

Examples of Signatures for Basic Data Types

Booleans From among the many algebras of Booleans, here is a signature for a standard set of Boolean operations:

signature Booleans sorts Bool

constants true,false : Bool

operations and : Bool ×Bool →Bool not : Bool Bool

endsig

The operations are prefix notations. Notice that we have shortened the declaration of constants by making a list rather than the two declarations, i.e., true : Bool and false : Bool.

Naturals We met a signature ΣPeano Arithmetic for the Peano Arithmetic in Section 4.1.1;

it used prefix notation and was based on the algebra in Section 3.3. If we wanted to use the standard infix notation for successor, addition and multiplication then we can choose the following signature Naturals:

signature Naturals sorts nat

constants 0 : →nat

operations +1 : nat nat + : nat ×nat nat . : nat ×nat →nat endsig

Integers In Section 3.4.1, we introduced an algebra of integers using a prefix notation for addition, subtraction and multiplication. Here is a signature Integers:

signature Integers sorts int

constants zero,one : →int

operations add : int ×int int minus : int int times : int×int →int endsig

Next, we give a signature IntegersInfix for the integers that uses the standard infix notation for addition, subtraction and multiplication:

signature IntegersInfix sorts int

constants 0,1 : →int

operations + : int ×int int

− : int →int

. : int ×int int endsig

Reals In Section 3.5, we introduced an algebra of real numbers with lots of basic operations. Here is a signature Reals containing some infix and some prefix notations.

4.2. EXAMPLES OF SIGNATURES 91 signature Reals sorts real Bool constants 0 : →real 1 : real π : real true : →bool false : bool

operations + : real ×real →real

− : real real

. : real ×real real −1 : real real

: real real

| |: real real sin : real real cos : real →real tan : real real exp : real real log : real →real

=: real ×real Bool <: real ×real Bool and : Bool×Bool →Bool not : Bool Bool

endsig

Notice that some of these names are for operations that are partial when applied to all real numbers. The functions of division −1, square root, tangent tan, and logarithm log are not

defined on all real numbers. However, this is not visible in the notation here. Should it be? We will not introduce a special notation for partiality. Usually our operations will be total.

Perhaps we should refine the signature and introduce special names for sorts for the domains of partial operations. Sometimes this seems easy, as for the sorts of non-zero real numbers, e.g.,

−1 :real

6

=0 →real

and positive real numbers, e.g.,

:real

≥0 →real.

Of course, we will need to add operations that link these new sorts to the sort real. But it can also seem clumsy, as in the case of tan.

4.2.2

Signature for Subsets

Suppose we want to calculate with subsets of a given set. We shall have constants empty :subset

to represent the set with no elements, and

universe :→subset

to represent the set which has all the elements of that which we are calculating over. We shall combine subsets with the usual set manipulation operations:

union :subset×subset subset intersection :subset×subset subset complement :subset →subset

signature Subsets sorts subset

constants empty : →subset universe : subset

operations union : subset×subset →subset intersection : subset×subset subset complement : subset subset

endsig

4.2.3

Signature for Strings

Suppose we have a signature Basic Strings with Length for calculating with strings. We shall want to be able to create a string from some alphabet. If we allow strings of zero length, then we shall want a constant

empty :string

for the empty string. We can build a string by prefixing letters from the alphabet using an operation

prefix :alphabet ×string string.

To measure the length of a string, we shall use the natural numbers, and an operation length :string nat.

signature Basic Strings with Length sorts alphabet,string,nat

constants empty : string zero: nat

operations prefix : alphabet ×string string length : string →nat

succ: nat nat endsig

4.2. EXAMPLES OF SIGNATURES 93

4.2.4

Storage Media

Thinking abstractly, a storage medium is simply something that stores data. We imagine that it must have a store, and means of putting data into store and of taking data from store. These “means” are operations on the store. Storage media are specified by different stores equipped with different input-output operations.

What would be a simple interface for abstract storage media? The following signature Storage models such an interface:

signature Storage sorts store

address data

constants empty : →store

operations in : data×address×store store out : address ×store data

endsig

For example, data structures store and access data in different ways: there are the record, the array, the stack, the list, the queue, and many more. Some of these, such as the stack, do not have addresses.

4.2.5

Machines

Thinking abstractly, a machine is simply a device that processes input and output. It has a memory and a program; indeed it may be programmable. For simplicity, we imagine that it must have

(i) a state that combines memory and commands, and

(ii) a means of reading input data into state and of writing the output data from the state. The input of data changes the state of the machine. These ideas can be expressed by two operations on the state. What would be a simple interface for abstract machines? An interface to a machine is modelled by this signature Machine:

signature Machine sorts state

input output

constants initial : state

operations next : state×input →state write : state×input output endsig