This document discusses key concepts in the formal specification language Z, including:
- Sets and types, where elements of a set must be of the same type
- Declarations introduce variables and specify the set they belong to
- Expressions describe variable values using names, literals, and operators
- Predicates constrain variables and include equations, inequalities, and membership statements. Laws describe operator properties.
2. Topics in this lecture
Sets and types,
declarations,
variables,
expressions,
operators,
predicates,
equations and
laws.
3. Sets
Displaying sets
The obvious way to describe a set is to list or enumerate all of its members or elements. This is
called a set display. In Z we follow the ordinary mathematical convention and write sets with
braces, separating elements by commas. Here is a display of the set of lamps in a traffic light:
{red, yellow, green]
4. Naming sets
To write a program that simulates a dice game, we need a set that contains
the numbers of spots found on the faces of dice: {1,2, 3,4, 5,6). Z does
provide notation for a range of consecutive numbers; we can abbreviate this 1
.. 6 (without braces) pronounced one up to six
5. Types
In Z we can only form sets from objects that are similar in some way. We say
that elements of the same set must have the same type; sets in Z are typed
{2,4, red, yellow, 6} [TYPE ERROR! Elements have different types.]
6. Type and set
Types and sets are very closely related. Every type has a carrier set that
contains all of the objects of that type. For example, the carrier set for the
integer type Z is the set with every integer in it: {..., —2, — 1,0, 1, 2,...}. We
usually say that the type is its carrier set . Every type is a set, but not all sets
are types.
Natural numbers belong to the type integer, Z, because every natural
numberis also an integer.
Types are very important in Z, even though only one type is built in: the type
integer, appropriately named Z
8. Declaration cont…
Each name introduced in a declaration names or denotes a single element in
the set that appears to the right of the colon. This element is sometimes
called the name's value. This value may be unknown or undetermined, so the
names introduced in declarations are called variables. In the preceding
declarations, i, d1,d2 and signal are variables
9. Constraining variables
In Z we can constraints the value the variable it can take. These are
axiomatic definitions that include a paragraph with contraints. Predicates are
the constraints
13. . A definition where the types are explicitly spelled out in this way is said to
be normalized.
. A signature is a declaration that names the type, as we must have in a
normalized definition: e : EVEN is a declaration, but e : Z is a signature.
14. Defining new types
Two methods for defining types.
FREE TYPE definition.
BASIC TYPE definition.
19. Expressions and operators
Expressions describe the values that variables might have.
Expressions enable us to describe values in terms of names and literals we
have already defined. Expressions are formulas where names and literal
values appear together with operators. Expressions are sometimes called
terms
20. Arithmetic expression
The Z mathematical tool-kit defines the usual arithmetic operators addition,
subtraction, and multiplication +, —, and *. The tool-kit doesn't provide any
way to represent fractions — it doesn't define real or rational numbers — so
ordinary division is not available. However, the tool-kit does provide integer
division div and remainder or modulus mod
12 div 5=2
12 mod 5=2
22. Expression and type
Every expression has a type: the type of the value it denotes.
Some operators are generic; they can work with different types as long as
types are combined correctly
Some operators take operands of one type and denote values of a different
type. For example, the size (or cardinality) operator # counts the elements of
a set. Its operand is a set, but its value is a number:
23. Erroneous expressions
Expressions must have the correct appearance or syntax. In Z, as in
traditional mathematics, most binary operators have infix syntax: They
appear between their operands, as in 5 -f 3OTODD U EVEN.Many unary
operators in Z have pre/u syntax: They appear before their operands, as in —
x or #DICE. Using a prefix operator as if it were postfix is an example of a
syntax error.
DICE# is an error.
24. Predicates , equations and law
Three kinds of predicates: equations such as size = 2048, inequalities such as
size > 640, and membership predicates such as e € EVEN.
25. Equations-
An equation is a predicate where two expressions are joined by an equal sign:
e1 = e2. means that e1 and e2 both have the same value. Equations are
perhaps the most common predicates
Size=2048
26. Law
They are also used to describe the operators themselves. Predicates used in
this way are called laws