MasterSlides - Z Specifications - Formal Methods in SE
MasterSlides - Z Specifications - Formal Methods in SE
Introduction to Z Specification
Outline
• Z language
• Specifications of Z language
• Z language
• Z language Schemas review
• Z language example
• Schema
• Schema theory
• Type of schemas
– State based schema
– Operation based schema
• Z Example: A Birthday Book
Formal Methods
Logic
true, false logical constants
P not P
Z Specification
PQ conjunction: “P and Q”
PQ disjunction: “P or Q”
PQ implication: “If P then Q”
PQ equivalence: : “If P then Q and if Q then P”
t=r term t is equal to term r
tr term t is not equal to term r, that is: (t=r)
Predicates:
" x:T.P for all x of type T, P holds
x: T.P there exists an x of type T, for which P holds
Z Specification
Relations:
XY the set of ordered pairs of X’s and Y’s
XY the set of relations from X to Y; = =P(XY)
xRy x is related to y by R; (x,y) R
dom R the domain of relation R; = ={x:X | (x:X . x R y) .
x}
ran R the range of a relation R; = ={y:Y | (x:X . x R y) . y}
SR the relation R is domain restricted to S
RS the relation R is range restricted to S
R~ the inverse of R
Z Specification
– Functions:
– XY the set of partial functions from
X to Y
= ={f:XY | (x:X | x dom f.
(1y:Y.x f y))}
– X Y the set of total functions from X
to Y
= ={f: X Y| dom f = X.f}
– fx or f(x) function f is applied to x
Z Specification
<x1,…xn>^<y1,…yn> concatenation
= = <x1,…xn,y1,…yn>
head S ==S1
last S = = S #S
tail S^S1 ==S
frontS^<x> = = S where <x>= =last S
rev S the sequence S in reverse order
Z Specification
Schemas:
Class Schema:
Class Name[generic parameter]
type definitions
constant definitions
state schema
initial state schema
operation schemas
history invariant
Z Specification
State Schema
items: seq T
#items max
declaration predicate
Z Specification
Operational schemas
Operation name
declarations
predicates
Z Specification
Schema decorations:
22
Z Specification
Schemas:
Class Schema:
Class Name[generic parameter]
inherited classes
type definitions
constant definitions
state schema
initial state schema
operation schemas
history invariant
Z Specification
State Schema
items: seq T
#items max
declaration predicate
Example
We do not for the moment specify what exact type the book
variable would be. The book may be identified by an
alphanumeric string, such as by the book title; alphabetic, such as
by author’s name; numeric, such as by ISBN, etc. It does not
matter for the moment.
abook? onshelf
location’ = location {abook? aspot?}
Proof:
onshelf’ = dom location’ [by invariant after]
= dom (location {abook? aspot?} [Spec of
AddtoLibrary]
= dom location dom{abook? aspot?} [fact about dom]
= dom location {abook?} [fact about functions]
= onshelf {abook?} [invariant before}
A Library System
abook? onshelf
aspot! = location(abook?)
A Library System
The new symbol Ξ indicates that the operation
FindLocation has induced no change of state. It is in
this sense mathematically equivalent to:
• What is Schema???
– A representation of a plan or theory in the
form of an outline or model.
– a diagram, plan, or scheme. Synonyms:
outline, framework, model.
– an underlying organizational pattern or
structure; conceptual framework:
– A schema provides the basis by which
someone relates to the events he or she
experiences.
Model-Based Specification
• Z — like VDM, its main competitor — is a model-based
specification framework.
• The idea is to construct an abstract model of the system
we
This model is:
– high level;
– idealised;
– does not detail with implementation specifics.
• What does the model consist of?
– description of system state space;
– description of system operations.
• System state-space is the set of all states that the
system could be in.
• The state of a system describes the value of each
variable (and memory location).
Continue..
• The most fundamental operation we use is the assignment
statement, ‘:=’ . . . Such statements change the state of a
system.
• In Z, we represent the state space of a system as a collection
of functions, sets, relations, sequences, etc., together
with a collection of invariant properties on these objects.
• These invariant properties describe regularities between state
changes.
• How about operations? What level of abstraction we deal with
them? Lowest level would be assignment statement level.
• We start with more abstract descriptions.
• Operations are usually defined in terms of pre- and post-
conditions.
• Operations define acceptable state transitions.
State Space Schemas
• Here is an example state-space schema,
representing part of a system that records details
about the phone numbers of staff. (Assume that
NAME is a set of names, and PHONE is a set of
phone numbers.)
Continue..
– The declarations part of this schema introduces two
variables: known and tel.
– The value of known will be a subset of NAME, i.e., a set
of names.
– This variable will be used to represent all the names that
we know about — those that we can give a phone
number for.
– The value of tel will be a partial function from NAME to
PHONE, i.e., it will associate names with phone numbers.
– The declarations part is separated from the predicate part
by the horizontal line.
– The predicate part contains the following invariant:
– The domain of tel is always equal to the set known.
Summary
40
Continue..
– The declarations part of this schema introduces two
variables: known and tel.
– The value of known will be a subset of NAME, i.e., a set
of names.
– This variable will be used to represent all the names that
we know about — those that we can give a phone
number for.
– The value of tel will be a partial function from NAME to
PHONE, i.e., it will associate names with phone numbers.
– The declarations part is separated from the predicate part
by the horizontal line.
– The predicate part contains the following invariant:
– The domain of tel is always equal to the set known.
Operation Schemas
• In specifying a system operation, we must consider:
–the objects that are accessed by the operation,
and of these:
∗ the objects that are known to remain
unchanged by the operation (cf. value
parameters);
∗ the objects that may be altered by the
operation (cf. variable parameter);
– the pre-conditions of the operation, i.e., the things
that must be true for the operation to succeed;
– the post-conditions — the things that will be true
after the operation, if the pre-condition was
satisfied before the operation.
Continue…
• Return to the telephone book example, and
consider the ‘lookup’ operation:
– We put a name in, and get a phone number out.
– this operation accesses the PhoneBook
schema;
– it does not change it;
– it takes a single ‘input’ — a name for which we
want to find a phone number;
– it produces a single output — a phone number.
– it has the pre-condition that
the name is known to the database.
– Here is a Z schema specifying
the lookup operation:
Schema
47
Z Example: A Birthday
Book
Description
A system that records people’s birthdays and is able to
issue a reminder when the day comes around
Spivey - Z Reference Manual, chapter one (pages 1-10)
48
States
Initial State Schema
49
Operations
Specify an operation to add a new birthday entry
Consider: convention, inputs, and pre/post- condition
50
Operations (cont’d)
Specify an operation to find (retrieve) a person’s birthd
Consider: convention, inputs, outputs, and pre/post-
51
Operations (cont’d)
Specify an operation to remind you who’s birthday is it
Consider: convention, inputs, outputs, and pre/post-
Remind Operation
52
Considering All Scenarios
An implementation of the Birthday Book specification
will work as long as there are no mistakes in the
input, i.e., if
the pre-conditions have been met.
56
Summary