4 Lecture 4 Specification Language in Formal Methods
4 Lecture 4 Specification Language in Formal Methods
• The specification says that a dictionary d’ is the result of adding a key k and a value v to
some other dictionary d if and only if d’ is equal to d ∪ {(key, v)} if k is not already used,
and otherwise d’ is just like d except that the old pair with k is removed and the new pair
{k, v} added.
• Predicate logic has less support for modularity
• large specifications in predicate logic usually are very hard to understand
Writing formal specifications
More readable
Modular
Formal Modeling Methods
• Formal methods are mathematics based languages, techniques and tools that can be
applied at any part of the program lifecycle.
• Formal methods make use of refinement techniques at any stage to ensure the correctness,
completeness and consistency of specification.
• The representation used in formal methods is
called a formal specification language
• A formal specification language can be used to
model the most complex systems using relatively
simple mathematical entities, such as sets,
relations and functions.
• Types of Formal Specification Styles
Formal Modeling Methods
1. Model Based Languages : Specify system behavior by the construction of a mathematical model with an
underlying state (data) and a collection of operations on that state using relations, sets, sequences and
functions.
• The most widely used notations for developing model based languages are Vienna Development Method
(VDM) , Zed (Z) and B.
2. Algebraic Specification: These languages uses methods derived from abstract algebra to specify behavior of
information system.
• Algebraic approach was originally designed for the definition of abstract data types and interface.
• The most widely used notations for developing algebraic specification languages are LARCH, ASL and OBJ.
3. Process Oriented Specification is used to describe concurrent system.
• The most widely used process oriented language is Communicating Sequential Processes (CSP).
Model based specification
• Model-based specification is an approach to formal specification where the system
specification is expressed as a system state model.
• The state model is constructed using well-understood mathematical entities such as sets
and functions.
• System operations are specified by defining how they affect the state of the system model.
• The most widely used notations for developing model-based specifications are Z, CSP, B,
Petri Net and VDM.
Sequential Concurrent
Z-method
• Z formalism goal was to develop a specification formalism that achieves precision,
conciseness, and modularity without imposing unnecessary constraint.
• Z is based on first-order predicate logic and set theory. Its built-in theory contains
booleans, integers, reals, relations, functions, sets, bags (sets with duplicates), and
sequences and the standard operations on them.
• Schemas encapsulate specifications of initial states, constants, types, functions, and
operations.
• Schemas can be composed using Z’s schema calculus.
• A refinement calculus allows the stepwise refinement of specifications into code.
• Z decomposes specifications into manageably sized modules, called schemas:
Model- Based Languages
Z-method…
• Schemas are divided into 3 parts:
A State
A collection of state variables and their values
There are also some operations that can change its state
Model- Based Languages
• One possible state of the system has three people in the set
known, with their birthdays recorded by the function birthday:
known = { John; Mike; Susan }
birthday = { John 25-Mar,
Mike 20-Dec,
Susan 20-Dec }
Model- Based Languages
Z-method… • The declaration BirthdayBook alerts us to the fact that the
schema is describing a state change:
• 4 variables The first two are observations of the state
before the change, and the last two are observations of
the state after the change.
• Each pair of variables is implicitly constrained to satisfy
the invariant, so it must hold both before and after the
operation.
• Next come the declarations of the two inputs to the
operation.
VDM : It is a collection of techniques for the modeling, specification and design of computer-
based system.
• A VDM model has a specific role in application areas, such as semantics of programming
language, databases and construction of compilers.
• When using VDM, an abstract model traditionally contains the following components.
• Semantic domains: These types describe the objects to be operated on
• Invariants: Invariants are the boolean functions that define a set of condition on the
objects that is described by semantic domains.
• Syntactic domains: Types that define a "language" in which to express commands for
manipulating the objects defined by the semantic domains.
• Well-formedness conditions: These are the functions that define when the commands,
which is defined by the syntactic domains have a well-defined effect.
• Semantic functions: These functions provide the effect of commands on the objects
defined by the semantic domains.
Model- Based Languages
Model- Based Languages
B-method
• The main concept follows in B- method is to initiate
with an abstract model of the system under
development, which roughly corresponds to the
modules in many programming languages.
• A development process creates a number of proof
obligations, which guarantee the correctness.
• An abstract MACHINE of a given name contains
information given as follows.
Algebraic Specification
1. OBJ
• The OBJ languages are based on algebra,
first-order logic, temporal logic and set
theory etc…
• Formal manipulation of the specification
based classes, objects, inheritance and
etc…
2. LARCH: Used to model language
dependent and language independent
specifications.
Process Oriented
Communicating Sequential Process(CSP)
• Usually used to model deadlock, live lock, and determinism.
• Csp is used for the formal analysis of safety-critical systems, where deadlock and live lock
could have serious consequences.
• It is formal language for describing patterns of interaction in concurrent systems.
• CSP, each process was assigned an explicit name, and the source or destination of a
message was defined by specifying the name of the intended sending or receiving process.
• Syntax.
• CSP has two basic processes: SKIP and STOP. SKIP is used to represent successful
termination while STOP represents a deadlocked process which can no longer engage in
any events.
• The syntax of CSP defines the “legal” ways in which processes and events may be
combined. Let e be an event, and X be a set of events, then the basic syntax of CSP can
be defined as
Process Oriented
Semantics
• channel c has a set of ‘basic’ events as its type T(c), the channel CardSlot has the type
T(CardSlot) = {cardI, cardO}.
• In Csp it is standard to write
Formal Verification ATM CSP Example
Process Termination
• Enrich the ATM example by considering a ‘cancel’ button for Canceling a Session
by Interrupt.
• Any time after inserting the bank card and before retrieving the cash, the
costumer has a choice to cancel the session. Upon cancellation, the ATM returns
the bank card and is ready for a fresh session.
Formal Verification ATM CSP Example
• Services
Algebraic specification
Specifications in Software
• Specifications can be formal or informal
Formal specs are usually expressed mathematically
Informal specs are usually expressed in natural language
• Most specification languages include explicit logical expressions, so it is very easy to apply
logic coverage criteria
• Programmers often include preconditions for their methods
• The preconditions are often expressed in comments in method headers
SDLC
Logic and Testing
Test Plan and Test case
Test Flow
Test Size
Test Depth
Logic and Testing
Other Testing
Unit Testing
Control Flow Testing
Data Flow Testing
Domain Testing
43
SDLC Revision
44
Testing Concept
Test Flow
• There are two Test Flows
1. Top down : a test flow that starts from a general level down to the specific detail.
– Example an inventory system that start from a inventory module down to sub module
called product module.
2. Bottom Up : Starts at specific and move to a general level.
– Example an inventory system that start from a product module up to Inventory module
called
45
Testing Concept
Test Size: Based on the test coverage we have 4 types of test sizes
1. Unit Testing refers to testing specific module independently. Testing customer module, and
then testing product module alone.
2. Integration Testing refers to a test that joins individual modules. Example for an inventory
system testing the product and customer module to test out the sales invoice module.
3. System Testing : This is a test that starts to studies the system environment of the software.
Example for an inventory system will be to test if the bar code reader at the POS can read the
barcode label on the product.
4. Acceptance Testing: is a quality assurance (QA) process that determines to what degree an
application meets end users' approval. Depending on the organization, acceptance testing
might take the form of beta testing, application testing, field testing or end-user testing.
46
Testing Concept
2. White box testing also called code-based testing, glass box testing, open box testing, clear
box testing, and transparent box testing.
– The software application’s internal coding, design, and structure are examined in white box testing to
verify data flow from input to output.
– White box testing is leveraged to improve design, usability, and application security. 47
Testing Concept
3. Gray box testing is a software testing method to test the software application with partial
knowledge of the internal working structure. It is a combination of black box and white box
testing because it involves access to internal coding to design test cases as white box testing
and testing practices are done at functionality level as black box testing.
Logic Tests are intended to choose some subset of the total number of truth assignments to
the expressions in specification or code.
Predicate Coverage (PC) : For each p in P, Test Rule (TR) contains two requirements: when p
evaluates to true, and when p evaluates to false.
50
Logic and Testing
51
Logic and Testing
print(“highway”)
else
52
Logic and Testing
Consider a safety critical system with a valve that might be either open or
closed, and that has several modes, two of which are “Standby” and
“Operational.”
Suppose that there are two constraints:
1. The valve must be open in “Operational” and closed in all other modes.
2. The mode cannot be both “Standby” and “Operational” at the same time.
These constraints lead to the following clause definitions:
a = “The valve is closed”
b = “The system status is Operational”
1. ¬a ↔ b
c = “The system status is Standby”
The two constraints can now be formalized as: 2. ¬(b ∧ c)
Develop truth assignment table for this
logic 53
Logic and Testing
Clause Coverage (CC) : For each c in C, TR contains two requirements: when c evaluates to
true, and when c evaluates to false.
55
Logic and Testing
57
How to cover the executions
58
How to cover the executions
1, Execute every statement at least once
for A=2,B=0,X=3
IF (A>1) & & (B=0) THEN X=X/A
END
IF (A=2) | | (X>1) THEN X=X+1
END
60
How to cover the executions
4, Multiple Condition Coverage
Test all combinations of all conditions in each test.
–A>1,B=0 IF (A>1) & & (B=0) THEN X=X/A
–A>1,B≠0 END
–A1,B=0 IF (A=2) | | (X>1) THEN X=X+1
–A1,B≠0 END
–A=2,X>1
–A=2,X1 **Further optimization: not all combinations, but given
–A≠2,X>1 conditions C and D,
–A≠2,X1 For C /\ D, check (C, D), (C, D), (C, D).
For C \/ D, check (C, D), (C, D), (C, D).
61
Example Applications
Given the following business specification for an ATM application
Actors
Customer
Bank Server
Business Functions
Card Reader checks ATM card
PIN Entry Screen [Customer enters the PIN]
Account Options Screen [Customer gets main screen]
Exceptions Conditions
Unreadable Card Screen
PIN Error Screen or No PIN Screen
Account Locked Screen
Develop the possible predicates and clauses from the above business description.
Design exhaustive [predicate and clause] test coverages
62