Z Specification
Pre: Set Theory, Relations, Predicate Logic, Statements and Quantifiers
States
In Z, we think of the execution of a system as a sequence of states (trace):
where each state is a mapping from variables to values.
Z Specification
In a Z system spec, we describe:
- Types of data manipulated by the system (optional)
- Constants (elements that aren't changed by the system) and their types (optional)
- State space (set of possible states): system elements, their types, and invariants about relationships between system elements
- Operations and how these change the elements of the system
- System operations which change the system state or elements:
- System operations which report the system state:
- System operations which change the system state or elements:
We use Z schemas for 2-5. First, we present schemas, and then discuss types.
Schemas
A schema is a way of grouping together related information
- Above the middle line is a signature part, where we introduce elements and their types
- Below is the predicate part, which contains WFFs that are implicitly conjoined together
- These WFFs are called invariants, which are formulas stating what must be true in every state of execution
Schema Inclusion
We can include one schema in another schema by listing its name in the signature part of another schema. This adds the signature and predicates from the included schema.
is equivalent to writing:
Decorated Schemas
We wish to describe both the current and next values of a variable in the in the state.
We refer to the value of a variable in the next state by attaching a prime to the variable name, e.g
If we include schema like
Delta Schema Inclusion
A delta schema (
An operation that increments the values of both state variables:
Xi Schema Inclusion
A xi schema (
Used for outputs of some kind
Inputs and Outputs
?
as in means is an input!
as in means is an output
Operations
- Preconditions are conditions on the current state variables and inputs
- Postconditions are conditions on the next state variables and the outputs
For example, if a variable is to remain unchanged in an operation, then that must be included in the predicate part (
Note that this is not assignment, but rather an assertion of equality.
Types
Types of types:
- Built-in types:
, etc. - Generic types: types that exist without explicitly stating what the elements of the type are
Generic types are also called uninterpreted types, because they don't have a standard interpretation and we don't care
For example, or - Free (enumerated) types: a new type for which we describe its possible values
For example,
These values are assumed to be distinct - Compound Types
Compound Types
Values of the system can also be elements of a compound type
See Properties of Binary Relations#Uniqueness Properties
Name | Notation |
---|---|
Relation | |
Total function | |
Non-total function | |
Total surjective function | |
Non-total surjective function | |
Total injective function | |
Non-total injective function | |
Total bijective function | |
Non-total bijective function |
Remember that function ARE relations
Exception Handling
On top of the primary operations of a system, we also want to define exception handling operations for when preconditions are not met.
Exception handlers are always
Examples
- Don't think "how" to build a set, think declaratively: "what" should the state element satisfy after it has been constructed. You aren't writing the program to do the work, just the spec
- Set comprehension is usually not the right answer
Create a Z specification of the game of musical chairs:
The game involves a collection of chairs and players. There is always one less chair than players. In each round the players walk around while music plays and when the music goes off, the players must immediately sit on a chair. The person who doesn’t get a chair is eliminated. The winner is the person who gets a chair in the round where there are only two players left.
Src: Nimal Nissanke, Formal Specification, Springer, 1999
solution
Generic types:
Operations
dom(occupied') = ch'
means every chair has a person on it
Create a Z specification for the voting system for the Canadian federal House of Commons. Create operations for:
- Add candidates for ridings. The candidate must reside in the riding and be Canadian and at least 18 years of age.
- Register voters. Voters must be at least 18 years old, Canadian, and live in the riding. Ridings are determined by the voter’s postal code.
- Voting. A voter must be registered and can only vote for a candidate in the riding where he or she resides. A voter can make at most one vote.
- Determining the winner(s) for a riding.
Credit to Shahram Esmaeilsabzali for this example.
solution
Generic types:
Exceptions