Docsity

formal method exercise., Exercises of Computer Science

Toggle sidebar
bg1
Next: Strengthening the specification Up: An Introduction to Z Previous: What is a
The birthday book
The best way to see how these ideas work out is to look at a small example. For a first
example, it is important to choose something simple, and I have chosen a system so
simple that it is usually implemented with a notebook and pencil rather than a
computer. It is a system which records people's birthdays, and is able to issue a
reminder when the day comes round. The first thing to describe is the state space of
the system, and we do this with a schema:
Like most schemas, this consists of a part above the central dividing line, in which
some variables are declared, and a part below the line which gives a relationship
between the values of the variables. In this case we are describing the state space of a
system, and the two variables represent important observations which we can make of
the state:
ļ‚·known is the set of names with birthdays recorded.
ļ‚·birthday is a function which, when applied to certain names, gives the
birthdays associated with them.
The part of the schema below the line gives a relationship which is true in every state
of the system and is maintained by every operation on it: in this case, it says that the
set known is the same as the domain of the function birthday -- the set of names to
which it can be validly applied. This relationship is an invariant of the system.
In this example, the invariant allows the the value of the variable known to be derived
from the value of birthday: known is a derived component of the state, and it would
be possible to specify the system without mentioning known at all. However, giving
names to important concepts helps to make specifications more readable; because we
pf3
pf4
pf5

Partial preview of the text

Download formal method exercise. and more Exercises Computer Science in PDF only on Docsity!

Next: Strengthening the specification Up: An Introduction to Z Previous: What is a

The birthday book

The best way to see how these ideas work out is to look at a small example. For a first example, it is important to choose something simple, and I have chosen a system so simple that it is usually implemented with a notebook and pencil rather than a computer. It is a system which records people's birthdays, and is able to issue a reminder when the day comes round. The first thing to describe is the state space of the system, and we do this with a schema: Like most schemas, this consists of a part above the central dividing line, in which some variables are declared, and a part below the line which gives a relationship between the values of the variables. In this case we are describing the state space of a system, and the two variables represent important observations which we can make of the state: ļ‚· known is the set of names with birthdays recorded. ļ‚· birthday is a function which, when applied to certain names, gives the birthdays associated with them. The part of the schema below the line gives a relationship which is true in every state of the system and is maintained by every operation on it: in this case, it says that the set known is the same as the domain of the function birthday -- the set of names to which it can be validly applied. This relationship is an invariant of the system. In this example, the invariant allows the the value of the variable known to be derived from the value of birthday : known is a derived component of the state, and it would be possible to specify the system without mentioning known at all. However, giving names to important concepts helps to make specifications more readable; because we

are describing an abstract view of the state space of the birthday book, we can do this without making a commitment to represent known explicitly in an implementation. One possible state of the system is the following: Here there are three names known to the system, and the birthday function associates a date with each of them. Notice that in this description of the state space of the system, we have not been forced to place a limit on the number of birthdays recorded in the birthday book, nor to say that the entries will be stored in a particular order. We have also avoided making a premature decision about the format of names and dates. On the other hand, we have concisely captured the information that each person can have only one birthday, because the variable birthday is a function, and that two people can share the same birthday as in our example. So much for the state space; we can now start on some operations on the system. The first of these is to add a new birthday, and we describe it with a schema: The declaration alerts us to the fact that the schema is describing a state change : it introduces four variables known , birthday , and. The first two are observations of the state before the change, and the last two are observations of the state after the change. Each pair of variables is implicitly constrained to satisfy the invariant, so it must hold both before and after the operation. Next come the declarations of the two inputs to the operation. By convention, the names of inputs end in a question mark. The part of the schema below the line first of all gives a pre-condition for the success of the operation: the name to be added must not already be one of those known to the

Two new notations are illustrated by this schema. One is the declaration , which indicates an operation in which the state does not change: the values and of the observations after the operation are equal to their values known and birthday beforehand. Including above the line is the same as including above the line and the two equations: below it. The other notation is the use of a name ending in an exclamation mark for an output: the FindBirthday operation takes a name as input and yields the corresponding birthday as output. The pre-condition for success of the operation is that is one of the names known to the system; if this is so, the output is the value of the birthday function at argument. The most useful operation on the system is the one to find which people have birthdays on a given date. The operation has one input , and one output, , which is a set of names: there may be zero, one, or more people with birthdays on a particular day, to whom birthday cards should be sent. Again the convention is used to indicate that the state does not change. This time there is no pre-condition. The output is specified to be equal to the set of all values n drawn from the set known such that the value of the birthday function at n is. In general, y is a member of the set exactly if y is a member of S and the condition , obtained by replacing x with y , is satisfied: So, in our case,

A name m is in the output set exactly if it is known to the system and the birthday recorded for it is. To finish the specification, we must say what state the system is in when it is first started. This is the initial state of the system, and it also is specified by a schema: This schema describes a birthday book in which the set known is empty: in consequence, the function birthday is empty too. What have we achieved in this specification? We have described in the same mathematical framework both the state space of our birthday-book system and the operations which can be performed on it. The data objects which appear in the system were described in terms of mathematical data types such as sets and functions. The description of the state space included an invariant relationship between the parts of the state -- information which would not be part of a program implementing the system, but which is vital to understanding it. The effects of the operations are described in terms of the relationship which must hold between the input and the output, rather than by giving a recipe to be followed. This is particularly striking in the case of the Remind operation, where we simply documented the conditions under which a name should appear in the output. An implementation would probably have to examine the known names one at a time, printing the ones with today's date as it found them, but this complexity has been avoided in the specification. The implementor is free to use this technique, or any other one, as he or she chooses. Mathematical specifications have the three virtues of being concise, precise and unambiguous. They are concise because mathematical notation is capable of expressing complex facts about information systems in a short space. Practical experience shows that a mathematical specification of a system is often much shorter than an equivalent informal specification. Hayes [ 3 ] reports that a formal specification for a module in the CICS system is comparable in length with the less informative English-language manual entry for the same module. Mathematical specifications are precise because they allow requirements to be documented accurately. The desired function of a system is described in a way that does not unduly constrain either the