Formal Method in SE
Formal Method in SE
Z Notation Basics
Purpose:
• Set Theory: Z utilizes set theory to define data and system variables. Sets
represent collections of elements, which can be manipulated using
operations like union, intersection, and subtraction.
o Example: A set of students in a class can be represented as
Students = {Ali, Sara, Ahmed}.
• Predicate Logic: Predicate logic is used to define constraints and
relationships between variables in a system. Logical operators like AND
(∧), OR (∨), and NOT (¬) are used to express conditions.
o Example: A condition that all students must have an ID can be
written as: ∀s ∈ Students • s.ID ≠ null
Importance in Software Engineering
1. High Reliability: Ensures that the system functions correctly under all
specified conditions.
2. Formal Verification: Allows for mathematical proof of system
properties.
3. Clear Communication: Provides a precise and unambiguous way to
convey system requirements and behavior.
4. Error Detection: Helps in identifying logical inconsistencies or missing
requirements early in development.
Components of Z Notation
Schemas
Schemas are the building blocks of Z specifications. They are used to define:
Student ==
[name: ℤ;
age: ℤ
•
age ≥ 18]
• Explanation:
o name and age are state variables of type ℤ (integer).
o age ≥ 18 is a predicate ensuring the age constraint.
State Variables
Predicates
Predicates define rules or constraints on state variables. They ensure the system
adheres to specified conditions.
Precise Specifications
Logical Consistency
System Correctness
Purpose of STS:
1. States:
States represent the system’s conditions at a particular moment. Each state
shows the system’s situation based on its current data or settings.
o Example 1: In a login system, states can include "Logged Out,"
"Logging In," and "Logged In."
o Example 2: In a traffic control system, states might be "Red Light,"
"Yellow Light," and "Green Light."
States are like snapshots of the system. They show what the system is
doing or its status at a specific point in time.
2. Transitions:
Transitions are the "actions" or "events" that cause the system to move from
one state to another. These transitions are triggered by specific inputs,
events, or conditions.
o Example: In a login system:
▪ If a user enters the correct password, the system transitions from
"Logged Out" to "Logged In."
▪ If the password is incorrect, the system remains in the "Logged
Out" state.
Representation of STS
Graphical Representation:
In many cases, STS is represented graphically using state diagrams.
• States are shown as circles, and transitions are arrows connecting these
circles.
• Labels on the arrows indicate the event or condition causing the
transition.
For example, in a login system diagram, an arrow labeled “Correct
Password” would connect the "Logged Out" state to the "Logged In"
state.
Real-World Examples of STS
1. Login Systems:
o States: "Logged Out," "Logging In," and "Logged In."
o Transitions:
▪ User enters the correct password → Transition to "Logged
In."
▪ User enters an incorrect password → Stay in "Logged Out."
This STS ensures that only valid credentials allow access to the system,
enhancing security.
2. Banking Systems:
o States: "Active Account," "Inactive Account," "Overdrawn."
o Transitions:
▪ A withdrawal reduces the account balance.
▪ If the balance falls below zero, the state transitions to
"Overdrawn."
▪ Deposits transition the account back to "Active."
For example:
• In a bank account system, the machine state could include the current
account balance, account status (active or inactive), and recent
transactions.
• In a traffic light system, the machine state might be whether the light is
"Red," "Yellow," or "Green."
The machine state evolves through specific actions, inputs, or events. Each
action causes the system to transition from one state to another. This evolution
follows defined rules or operations that ensure the system behaves predictably.
For instance:
The evolution of states reflects how the system reacts to various inputs over
time, ensuring the system operates correctly.
State Change Operations
Operations are the key actions that cause a system to move from one state to
another. Each operation defines:
Operations ensure that state changes are controlled and logical. For example:
Examples of Operations
These operations define how and when the machine state changes.
Operation Schema: Modeling State Transitions in Z
Z
Deposit ==
∆State
depositAmount: ℕ
balance' = balance + depositAmount
Explanation:
This schema precisely defines how the system’s state (balance) evolves during a
deposit operation.
State Transition Diagram
Key Benefits
Z notation is widely used in systems where precision and reliability are critical.
Here are some examples:
1. Safety-Critical Systems
In industries like aviation and healthcare, Z helps model systems where errors
can have catastrophic consequences.
2. Banking Systems
In traffic control, Z helps design state transitions for managing vehicles safely
and efficiently.