|
9
DESCRIPTORS
9.1 Descriptor Types
9.2 Descriptor Layout
9.3 Descriptor
Creation
9.4
Selecting Descriptor Elements
9.5 Summary
Descriptors are used to describe concrete or
abstract real world entities such as persons, cars, insurance
policies, weather conditions, and so on. These kinds of entities
are often described in terms of attributes or properties, like
size, age, or color. Therefore a descriptor is a composite data
structure with named elements that are used to characterize the
attributes and properties of an entity. This chapter explains the
concepts of descriptors and how to use them.
9.1
Descriptor Types
Each descriptor is of a user-defined type.
Descriptors of the same type have the same structure.
User-defined types are simply introduced by so-called descriptor-type-specifications.
For example, the user may introduce the concept of an employee by
specifying:
type Employee;
From now on, Employee is known as the
name of a new descriptor-type.
9.2
Descriptor Layout
A descriptor is a data structure consisting of
a fixed number of elements. Each element has a name, a type and a
single value. The name of an element should be unique within the
descriptor. The types of the elements in a descriptor may be
different. The descriptor type is associated with the descriptor.
The layout of the descriptor is described by a so-called descriptor
constructor. For example, the descriptor constructor for an Employee
may be:
Employee:[ Name= PersonsName; Hiredate= Date; Salary= Amount ]
A descriptor constructor starts with the name
of the descriptor type, followed by a colon, followed by a list
of elements within square brackets and separated by semicolons.
In our example, an Employee-descriptor has three elements:
Name, Hiredate, and Salary.
9.3
Descriptor Creation
A new descriptor is created by the evaluation
of the descriptor-constructor. A descriptor-constructor is a
special kind of expression. Its evaluation results in the
creation of a corresponding descriptor and in the assignment of
the specified values to its elements.
This means that in our example, after the
creation of the Employee-descriptor, the PersonsName
will be assigned to the Name element, that Date
will be assigned to the Hiredate and that Amount
will be assigned to the Salary element. Furthermore, Name
will get the type of PersonsName, Hiredate will get
the type of Date, and Salary will get the type of Amount.
Often, a descriptor-constructor is embedded in
a function-definition. Such a function is called a constructor
function, as in:
NewEmployee (Name= text, Date= text, Salary= integer) -> Employee;
NewEmployee (PersonsName, Date, Amount) =
Employee:[ Name=PersonsName; Hiredate=Date; Salary=Amount ];
Each call of NewEmployee will now result
in the creation of a new Employee-descriptor, as will be
shown in the following dialogue:
John = NewEmployee ( "John Smith", "3-7-90", 10000); John?
Employee:[ Name = "John Smith";
Hiredate = "3-7-90";
Salary = 10000];
In this dialogue, the user first introduces a
new employee, John Smith, by calling the NewEmployee
constructor function. The name John is assigned to the
resulting descriptor.
The following step from the user is to ask for John.
The system returns a textual representation of the Employee
descriptor associated with John.
The dialogue session may be continued in the
following way:
Mary = NewEmployee ( "Mary Ford", "9-4-91", 15000); Mary?
Employee:[ Name = "Mary Ford";
Hiredate = "9-4-91";
Salary = 15000];
In our example we used in the
descriptor-constructor element definitions such as Name =
PersonsName. In many cases, however, it is more convenient to
use a single name as a short-hand notation for an element. Such a
single name must refer to an existing name. If such a name is
used then the element's name, type and value are copied from the
referred name. So, our example can be rewritten, without any
change in functionality, as:
NewEmployee (Name= text, Date= text, Salary= integer) -> Employee;
NewEmployee (Name, Hiredate, Salary) = Employee:[ Name; Hiredate; Salary ];
The elements Name, Hiredate and Salary
are receiving their names, types and values from the
corresponding parameters.
In practical applications, both the Name
and the Hiredate are itself again descriptors,
representing composite data structures. For example, Name
may be composed of First Name, Middle Name, and Last Name. We
will show later on, how descriptors of a given type can be used
as elements of other descriptors. Also arrays and lists may be
used as elements of descriptors. On the other hand, descriptors
may also be used as elements of arrays and lists.
Exercise:
- 9.1 Make a constructor function for
Name which is composed of First Name, Middle Name, and
Last Name.
9.4
Selecting Descriptor Elements
An element of a descriptor is selected by
placing a period between the name of the descriptor and the name
of the element. For example, to select the Salary of John, we may
write:
Instead of John, we may also write an
expression which represents John, or in a more general case, an
expression which represents an Employee. We say that the
element name is qualified by the descriptor expression. Suppose
we want to ask for the Salary of any Employee, by means of a
function:
ShowSalary (Employee) -> integer;
ShowSalary (Empl) = Empl.Salary;
We can now ask for the salaries of John and
Mary:
ShowSalary (John)?
10000 ShowSalary (Mary)?
15000
If we want to change the salary of an Employee
we define:
UpdateSalary (Employee, Increase = integer) -> nothing;
UpdateSalary (Empl, Inc) = [Empl.Salary:= Empl.Salary + Inc];
In the following dialogue the given definitions
are used:
UpdateSalary (Mary, 2000); ShowSalary (Mary)?
17000 UpdateSalary (John, -1000); ShowSalary (John)?
9000
May be you have noticed that in this chapter
three types of definitions were shown:
- constructors, such as NewEmployee,
which are creating new descriptors.
- accessors, such as ShowSalary,
which are retrieving data from descriptor-elements
without changing them.
- mutators, such as UpdateSalary,
which are changing values of descriptor-elements.
Later on, we will see more applications of
these different kinds of definitions.
Exercises:
- 9.2 Define an accessor function for
Name of Exercise 9.1 to retrieve the Last Name.
- 9.3 Define a mutator function for
changing the First Name of Exercise 9.1.
9.5 Summary
- Descriptors may be used to describe real
world entities. The elements of a descriptor are used to
describe attributes or properties. Each element has a
name, a type and a single value. The name of an element
should be unique within the descriptor. The types of the
elements in a descriptor may be different.
- Constructor definitions are
creating new descriptors.
- Accessor definitions are retrieving
data from descriptor-elements without changing them.
- Mutator definitions are changing
values of descriptor-elements.
 |
|
Part 1: Language
Description |
|
Chapter 9: Descriptors |
|