Vous êtes sur la page 1sur 38

Nazir Ahmad Zafar

M. Sc. Mathematics:
Quaid-i-Azam University, Islamabad, 1991
M. Phil Mathematics:
Quaid-i-Azam University, Islamabad, 1993,
M. Sc. Nuclear Engineering:
Quaid-i-Azam University, Islamabad,1994,
PhD. Computer Science:
Kyushu University, Japan, 2004.
Dr. Nazir A. Zafar

Formal Methods

Formal Methods

Dr. Nazir A. Zafar

Formal Methods

Lecture No. 1
Introduction

Dr. Nazir A. Zafar

Formal Methods

Introduction to Formal Approaches


Approaches based on mathematical tools for
specifying properties of software systems
These techniques usually drawn from areas of
discrete mathematics, such as
Logic,
Set theory,
Graph theory etc.
Formal Methods have rigorous tool support
Formal methods increase quality of software

Dr. Nazir A. Zafar

Formal Methods

Introduction to Formal Approaches

Presently more than 90 tools/languages


Z, VDM, Petri-nets, B, RAISE, CCS, CSP, ..
Notation Coverage, Description v.s. Proof
Sets, Relations, Mapping etc.
Useful tools and a first step to abstraction
Modeling example:
Reference models
Basis for rigorous discussion & analysis
Community over Internet: http://archive.
comlab.ox.ac.uk/formal-methods.html
Dr. Nazir A. Zafar

Formal Methods

Introduction to Formal Approaches

Existing methods offer large amounts of text,


pictures, and diagrams, but these are often
imprecise and ambiguous.
Important information is hidden among irrelevant
details.
Design flaws are discovered too late, making
them expensive or impossible to correct.
One way to improve the quality of software is
changing the software design
An alternative is using Formal Approaches at all
stages of software development.

Dr. Nazir A. Zafar

Formal Methods

Introduction to Formal Approaches

Formal methods can be used to produce precise,


unambiguous specification at every level of
software development
Information is structured and presented at an
appropriate level of abstraction.
Some approaches can be used to subsequent
processes i.e., testing, and maintenance.
Use of formal methods may be argued as
expensive but experience applying Formal
methods shows, it actually reduces costs.

Dr. Nazir A. Zafar

Formal Methods

Introduction to Formal Approaches

Formal methods can be applied in any area of


computer science and engineering
Example are:
Software engineering, information security,
Data bases e.g. use of the relational calculus
in database theory
Formal methods can be used for the description
of state of a statement and properties defining
relationship between change of states
It includes a theory of refinement that allows
mathematics to be used at every stage of
program development
Dr. Nazir A. Zafar

Formal Methods

Software Development using Formal Methods


Validation: Are we building the right system?
Verification: Are we building the system right?

Dr. Nazir A. Zafar

Formal Methods

An Introduction to Z Notations
Abstract data types and sequential programs
Describes state space of a system, operations over it
Operation are relations over pair of state spaces
Advancing to object oriented-ness
Z is based upon set theory and mathematical logic.
Set theory used includes set operators, set
comprehensions, cartesian products, power sets.
Mathematical logic is a first order predicate
calculus.
Together, they make up a mathematical language that
is easy to learn and to apply.
However, this language is only one aspect of Z.
Dr. Nazir A. Zafar

Formal Methods

An Introduction to Z Notations
Another aspect is the way in which the
mathematics can be structured.
Mathematical objects and their properties can be
collected together in schemas which are
patterns of declaration and constraint.
The schema language can be used to describe
the state of a system, and the ways in which that
state may change.
It is also used to describe system properties, and
to reason about possible refinements
Dr. Nazir A. Zafar

Formal Methods

An Introduction to Z Notations
A characteristic feature of Z is: types
Every object in mathematical language has a
unique type, represented as a maximal set in the
current specification.
This provides a useful link to programming
Notion of types means, in an algorithm, type of
object can be checked in specification
Several type-checking tools exist e.g. Z/eves
A third aspect is, we use natural languages to
relate the mathematics to objects in real world
A well-written specification should be perfectly
obvious to the reader
Dr. Nazir A. Zafar

Formal Methods

An Introduction to Z Notations
A fourth aspect is refinement.
We may develop a system by constructing a
model of a design, using simple mathematical
data types to identify the desired behavior.
We may then refine this description by
constructing another model which respects the
design decisions made, and yet is closer to
implementation.
This process of refinement can be continued
until executable code is produced.
Dr. Nazir A. Zafar

Formal Methods

An Introduction to Z Notations
Z is not intended for the description of nonfunctional
properties,
such
as
usability,
performance, size, and reliability.
Neither it is intended for the description of timed or
concurrent behavior.
However, there are other formal methods that are
well suited for these purposes.
We may use these methods combining with Z to
relate state and state-change information as a
complement

Dr. Nazir A. Zafar

Formal Methods

An Example : Designing A Birthday Book


For a moment, we choose a simple system, e.g., a
birthday book
It is a system which records peoples birthdays, and
is able to issue a reminder when the birthday comes
We require names and dates. At this stage do not
care how names and dates will be implemented.
[NAME], [DATE]

OR [NAME, DATE]

Given

known is the set of names with birthdays recorded


birthday is a function which, when applied to certain
name, gives the birthday of associated one.

Dr. Nazir A. Zafar

Formal Methods

Requirements
Formal Specification
1. Define a book
2. Possible operations

add a new person in the birthday book


Deleting an existing person from the book
Finding date of a person whose name is given
Issuing a reminder when birthday of a particular
person comes

Dr. Nazir A. Zafar

Formal Methods

Defining A Birthday Book

[NAME, DATE ]

BirthdayBook
known : P NAME
birthday : NAME DATE
known = dom birthday

Dr. Nazir A. Zafar

Formal Methods

Changing State
Decoration

BirthdayBook
known : P NAME
birthday : NAME DATE
known = dom birthday

Dr. Nazir A. Zafar

Formal Methods

Recording Birthday of a New Comer

AddBirthday
BirthdayBook
BirthdayBook

name? : NAME
date? : DATE
name? known
birthday = birthday U {name? date?}
Dr. Nazir A. Zafar

Formal Methods

Recording Birthday of a New Comer

AddBirthday
BirthdayBook

name? : NAME
date? : DATE
name? known
birthday = birthday U {name? date?}

Dr. Nazir A. Zafar

Formal Methods

Recording Birthday of a New Comer

AddBirthday

known : P NAME
birthday : NAME DATE
Known : P NAME
Birthday : NAME DATE
name? : NAME
date? : DATE
known = dom birthday
Known = dom birthday
name? known
birthday = birthday U {name? date?}
Dr. Nazir A. Zafar

Formal Methods

Finding Birthday of a Person

FindBirthday
known : P NAME
birthday : NAME DATE
name? : NAME
date! : DATE
name? known
date! = birthday (name?)
Dr. Nazir A. Zafar

Formal Methods

Issuing a Reminder

Remind

known : P NAME
birthday : NAME DATE
today? : DATE
cards! : P NAME
cards! = {n : known | birthday (n) = today?}
Note: today? Is (Input variable) and
cards! (output variable)
Dr. Nazir A. Zafar

Formal Methods

Chapter No. 2
Propositional Logic

Dr. Nazir A. Zafar

Formal Methods

Propositional Logic
Definition: Propositional logic deals with the
statement of alleged facts which must be either
true or false, but not both.
Example 2.1: The following statements
propositions:
A tomato is a fruit.
An orange is a fruit.
Oranges are not the only fruit.

Dr. Nazir A. Zafar

are

Formal Methods

Logical Connectives
In logical language, propositions may be
connected in various ways.
Table shows five propositional connectives, in
descending order of operator precedence
Symbol

Name

Pronunciation

negation
conjunction
disjunction
implication
equivalence

not
and
or
implies
if and only if

Dr. Nazir A. Zafar

Formal Methods

Logical Connectives and compound propositions


p q r p r is equivalent to version (((p)
q) r) (p r)
Example 2.2
1. (jaffa cakes are biscuits)
2. your cat is rich your dog is good looking
3. economy has recovered minister is lying
4. Jim is thirty-something Jim is under forty
5. Jim is thirty-something Jim is under forty
Dr. Nazir A. Zafar

Formal Methods

Conjunction
The conjunction p q is true only if p and q both
are true otherwise false
The conjunction follows the commutative
property i.e. p q = q p

Dr. Nazir A. Zafar

pq

t
t
f
f

t
f
t
f

t
f
f
f
Formal Methods

Disjunctions
The disjunction p q is false if both p and q are
false otherwise true
The disjunction follows the commutative
property as well, i.e., p q = q p

Dr. Nazir A. Zafar

pq

t
t
f
f

t
f
t
f

t
t
t
f
Formal Methods

Implications
The p is antecedent and q is consequent
The antecedent is stronger than consequent.
Commutative property does not hold, i.e.,
(p q) (q p)

p
t
t
f
f
Dr. Nazir A. Zafar

q
t
f
t
f

pq
t
f
t
t
Formal Methods

Example 2.7: Prove (p q r) (p (q r))


p
t
t
t
t
f
f
f
f

q
t
t
f
f
t
t
f
f

Dr. Nazir A. Zafar

r
t
f
t
f
t
f
t
f

(p q r)
t t
t
f
f
t
f
t
f
t
f
t
f
t
f
t

(p (q r))
t
t
tt
t
f
ft
t
t
tt
t
t
tt
t
t
tt
t
t
ft
t
t
tt
t
t
tt
Formal Methods

Equivalence
The equivalence p q means p q & q p
Commutative property does hold, i.e.,
(p q) = (q p)

p
t
t
f
f
Dr. Nazir A. Zafar

q
t
f
t
f

pq
t
f
f
t
Formal Methods

Negation
p is true if and only if p is false. The truth table is
simple
It is to be noted that
(p p) = false
(p p) = true

Dr. Nazir A. Zafar

t
Formal Methods

de Morgans Laws
1. (p q) = p q)
p

t
t
f
f

t
f
t
f

Dr. Nazir A. Zafar

p q (p q) p
t
f
f
f

f
t
t
t

f
f
t
t

p q

f
t
f
t

f
t
t
t
Formal Methods

de Morgans Laws
2. (p q) = p q)

t
t
f
f

t
f
t
f

Dr. Nazir A. Zafar

p q (p q) p
t
t
t
f

f
f
f
t

f
f
t
t

p q

f
t
f
t

f
f
f
t
Formal Methods

Tautologies and Contradictions


Tautology :
Propositions which evaluate to t in every
combination of their propositional variables are
known as tautologies
Contradiction :
If, on the other hand, propositions evaluate to f
in every combination, then they are known as
contradictions.
Note:
Of course, negation of a contradiction is a
tautology, and vice versa.
Dr. Nazir A. Zafar

Formal Methods

Examples 2.12
Examples of Tautologies:
1. p p
2. p p
3. p (q p)
Examples of Contradiction
1. p p)
2. p p
3. (p (q p))
Dr. Nazir A. Zafar

Formal Methods

Examples 2.13
Prove that following are tautologies
pqpq
(p q) p q
(p q) p q

Dr. Nazir A. Zafar

Formal Methods

Vous aimerez peut-être aussi