Based on chapter 9 in The Way of Z.
Links to more Z lectures.
This page looks best when this and this X are
about the same size:
X. See these viewing tips.
Objects from discrete mathematics can model data structures.
Tuples (records)
Relations (tables, linked data structures)
Functions (lookup tables, trees and lists)
Sequences (lists, arrays)
Mathematics also provides precise meanings for operators.
TUPLES
Tuples can resemble C structures or Pascal records.
Tuples are instances of Cartesian product types.
First declare types for each component.
[NAME] |
![]() |
ID == ![]() |
![]() |
DEPT ::= admin | manufacturing | research |
Define the Cartesian product type EMPLOYEE.
EMPLOYEE == ID ![]() ![]() |
Declare tuples which are instances of the type.
![]() | ||
Frank, Aki: EMPLOYEE | ||
![]() | ||
![]() |
![]() |
![]() |
![]() | ||
Frank = (0019, frank, admin) | ||
Aki = (7408, aki, research) | ||
![]() |
RELATIONS
Relations are sets of tuples. They can resemble tables or databases.
ID | NAME | DEPT |
0019 | Frank | Admin |
0308 | Philip | Research |
7408 | Aki | Research |
... | ... | ... |
In Z this can be expressed
![]() | ||
Employee: ![]() | ||
![]() | ||
![]() |
![]() |
![]() |
![]() | ||
Employee = { | ||
![]() | ||
![]() | ||
![]() | ||
![]() | ||
![]() | ||
![]() |
PAIRS
Pairs are tuples with just two components.
(aki, 4117) |
The maplet arrow provides alternate syntax without parentheses.
aki ![]() |
The projection operators first and second extract the components of a pair.
first(aki,4117) = aki |
![]() |
second(aki, 4117) = 4117 |
BINARY RELATIONS (1)
Binary relations are sets of pairs.
![]() ![]() |
or
NAME ![]() |
Binary relations can model lookup tables.
NAME | PHONE |
Aki | 4019 |
Philip | 4107 |
Doug | 4107 |
Doug | 4136 |
Philip | 0113 |
Frank | 0110 |
Frank | 6190 |
... | ... |
BINARY RELATIONS (2)
In Z this can be expressed
![]() | ||
phone: NAME ![]() | ||
![]() | ||
![]() |
![]() |
![]() |
![]() | ||
phone = { | ||
![]() | ||
![]() ![]() | ||
![]() ![]() | ||
![]() ![]() | ||
![]() ![]() | ||
![]() ![]() | ||
![]() ![]() | ||
![]() ![]() | ||
![]() | ||
![]() | ||
![]() |
RELATIONAL CALCULUS (1)
The tool-kit provides many operators for binary relations.
Domain and range are the sets of first and second components, respectively.
dom phone = { ..., aki, philip, doug, frank, ... } |
![]() |
ran phone = { ..., 4019, 4107, 4136, 0113, ... } |
Relational image can model table lookup.
phone ![]() ![]() |
RELATIONAL CALCULUS (2)
Restriction operators can model database queries.
Domain restriction selects pairs based on their first component.
{ doug, philip } ![]() |
![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
Range restriction selects according to the second element.
phone ![]() |
![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() |
![]() |
RELATIONAL CALCULUS (3)
Overriding can model database updates.
phone ![]() ![]() ![]() |
![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() |
![]() |
RELATIONAL CALCULUS (4)
Inverse reverses domain and range by exchanging the components of each pair.
phone ~ = { |
![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() |
![]() |
RELATIONAL CALCULUS (5)
Composition merges two relations by combining pairs that share a matching component.
![]() | ||
dept: PHONE ![]() | ||
![]() | ||
![]() |
![]() |
![]() |
![]() | ||
dept = { | ||
![]() ![]() ![]() | ||
![]() ![]() ![]() | ||
![]() ![]() | ||
![]() |
Composing the phone and dept relations:
phone ![]() |
![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
![]() |
![]() |
FUNCTIONS
Functions are binary relations where each element in the domain appears just once. Each domain element is a unique key.
![]() | ||
phonef: NAME ![]() | ||
![]() | ||
![]() |
![]() |
![]() |
![]() | ||
phonef = { | ||
![]() | ||
![]() ![]() | ||
![]() ![]() | ||
![]() ![]() | ||
![]() ![]() | ||
![]() | ||
![]() | ||
![]() |
Function application is a special case of relational image. It associates a domain element with its unique range element.
phonef ![]() ![]() |
![]() |
phonef (doug) = 4107 |
![]() |
phonef doug = 4107 |
BINARY RELATIONS AND FUNCTIONS
X ![]() | Binary relations: many-to-many |
X ![]() | Partial functions: many-to-one |
Some function applications undefined | |
X ![]() | Total functions: |
All function applications defined | |
X ![]() | Partial injections: one-to-one |
Inverse is also a function | |
X ![]() | Total injections |
X ![]() | Bijections: cover entire range (onto) |
All have type (X
Y)
LINKED DATA STRUCTURES
Binary relations can model linked data structures such as lists, graphs and trees.
Each arc can be modelled by a pair of nodes.
For example a tree can be modelled by its child function.
![]() | ||
child: NODE ![]() | ||
![]() | ||
child = { a ![]() ![]() | ||
![]() ![]() ![]() | ||
![]() ![]() ![]() ![]() | ||
![]() |
``Pointers'' are not necessary.
The transitive closure operator builds the descendent relation from the child relation.
descendent = child + |
![]() |
descendent = { a ![]() ![]() ![]() ![]() |
SEQUENCES
Sequences can model arrays and lists.
DAYS ::= friday | monday | saturday | sunday |
![]() |
![]() | ||
weekday: seq DAYS | ||
![]() | ||
![]() |
![]() |
![]() |
![]() | ||
weekday = ![]() | ||
![]() ![]() | ||
![]() |
Sequence operators include head and
concatentation, .
head weekday = monday |
![]() |
week == ![]() ![]() ![]() ![]() ![]() ![]() |
Sequences are functions, and functions are sets.
weekday = { 1 ![]() ![]() |
![]() |
weekday 3 = wednesday |
![]() |
# week = 7 |
OPERATORS
Most operators are just functions with infix syntax. For example
2 + 3 = 5 |
is a function application that can also be written
(_ + _) (2,3) = 5 |
This function could be defined like any other.
![]() | ||
_ + _: ![]() ![]() ![]() ![]() ![]() | ||
![]() | ||
![]() |
![]() |
![]() |
![]() | ||
... | ||
![]() |
In Z the familiar plus sign ``+'' is actually the name of a set.
(_ + _) = { ..., (1,1) ![]() ![]() |
OPERATOR SYMBOLS AND SYNTAX
Operator symbols are defined systematically.
X ![]() | Domain restriction |
X ![]() | Domain anti-restriction |
R ![]() | Range restriction |
R ![]() | Range anti-restriction |
Pictorial symbols and infix syntax remind us of operand order and make expressions easy to parse by eye.
{ doug, philip } ![]() ![]() |
![]() |
![]() ![]() |
![]() ![]() |
![]() ![]() |
Compare to rres(dres(phone, (doug, philip)), upto(4000,4999)))
FORMAL DEFINITIONS
Z operators are defined by formulas.
![]() |
[X,Y] | ![]() | |
![]() |
![]() |
![]() | |
![]() | |||
![]() |
![]() |
![]() | |
![]() | |||
_ ![]() ![]() ![]() ![]() ![]() ![]() ![]() | |||
_ ![]() ![]() ![]() ![]() ![]() ![]() ![]() | |||
_ ~ : (X ![]() ![]() ![]() | |||
_ ![]() ![]() ![]() ![]() ![]() ![]() ![]() | |||
_ ![]() ![]() ![]() ![]() ![]() ![]() | |||
![]() | |||
![]() |
![]() |
![]() | |
![]() | |||
![]() ![]() ![]() | |||
![]() ![]() ![]() | |||
![]() | |||
S ![]() ![]() ![]() ![]() | |||
![]() | |||
S ![]() ![]() ![]() ![]() | |||
![]() | |||
R ![]() ![]() ![]() ![]() | |||
![]() | |||
R ![]() ![]() ![]() ![]() | |||
![]() | |||
R ~ = { x: X; y: Y | x R y ![]() ![]() ![]() | |||
![]() | |||
R ![]() ![]() ![]() ![]() ![]() ![]() | |||
![]() | |||
Q ![]() ![]() ![]() | |||
![]() | |||
![]() | |||
![]() |
Back to Z lectures.