Professional Documents
Culture Documents
Engineering(SE)
Ikhlaq Ahmed
MS (Software Engineering), NUST
Software Engineer , NADRA HQ
Ahmed.ikhlak@gmail.com
Cell#: 0308-5059325
Marks Distributions
Quizzes(Surprised) : Marks : 10
Why we need FM ?
8
2N Paths
Coverage Levels
Statement Coverage : Write test cases so
that all the statements of a source program
should be executed at least once.
P Q PQ
T T T
T F F
F T T
F F T
Bi-conditional if and only if
P Q PQ
T T T
T F F
F F T
F T F
P Q means P Q Q P
A compound proposition that is always true,
irrespective of the truth values of the comprising
propositions, is called a tautology.
pp
The propositions p and q are called logically
equivalent if p q is tautology.
It is written as ,
pq
For example: (p q) p q
Some useful equivalences
p or true true p or false p
Some useful equivalences
p or true true p or false p
p and true p p and false false
Some useful equivalences
p or true true p or false p
p and true p p and false false
p or p p p and p p
Some useful equivalences
p or true true p or false p
p and true p p and false false
p or p p p and p p
not not p p
Some useful equivalences
p or true true p or false p
p and true p p and false false
p or p p p and p p
not not p p
associativity of
distributivity of , , and
34
Logic problem for the day
AH
We have seen that (X S)
Therefore
(A A H)
A H AH A (A H)
Proof Using Truth Table
A H AH
T T T
F T T
T F F
F F T
Proof Using Truth Table
A AH A (A H)
T T T
F T F
T F F
F T F
Proof Using Truth Table
A H AH A (A H)
T T T T
F T T F
T F F F
F F T F
Proof using equivalences
A (A H)
A (not A or H)
(A and (not A or H)) or
(not A and not (not A or H))
Proof using equivalences
A and (not A or H)
(A and not A) or (A and H)
false or (A and H)
A and H
Proof using equivalences
Hence
A (not A or H)
(A and H) or false
A and H
Rules of Inference and Logical
Deduction
Introduction
Elimination
Rules of Inference and Logical
Deduction
Introduction
p q
______________
p and q
Rules of Inference and Logical
Deduction
Introduction
p q
___________________ ___________________
p or q p or q
Rules of Inference and Logical
Deduction
Elimination
p and q p and q
___________________ ___________________
p q
Rules of Inference and Logical
Deduction
Introduction
q
______________
pq
Rules of Inference and Logical
Deduction
Elimination
p pq
______________________
q
Rules of Inference and Logical
Deduction
Elimination
p not p false
____________________ _______________
false p
Does the Superman Exist?
((W and A) E)
and ((not A) I)
and ((not W) M)
and (not E)
and (X not (I or M))
not X
1. Assume
((W and A) E)
and ((not A) I)
and ((not W) M)
and (not E)
and (X not (I or M))
2. Assume X
7. X not (I or M)
8. not (I or M)
Now application of elimination on 2 and 7
derives another simple proposition
2. Assume X
7. X not (I or M)
8. not (I or M)
ABC
Associativity of Equivalence
ABC
can be evaluated as
(A B) C
or
A (B C)
Even and odd numbers
m+n is even m is even n is even
m+n is even (m is even n is even)
ABC
A B C AB (A B) C
F F F T F
F F T T T
F T F F T
F T T F F
T F F F T
T F T F F
T T F T F
T T T T T
Properties of equivalence
constant true
true p p
true = (p p)
(true p) = p
Properties of equivalence
Symmetry
(p q) = (q p)
Properties of equivalence
ppqprq
pppqqr
true p true r
pr
Properties of equivalence
ppqprq
pppqqr
true p true r
pr
A (A G)
(A A) G
true G
G
A is a knight A
B is a knight B
A says I am the same type as B AB
A is a knight A
B is a knight B
A says I am the same type as B AB
AAB
B
Island of knights and knaves
Suppose A is the proposition person A is
a knight and suppose A makes a
statement S. Then A is true is the same as
S is true. That is:
AS
Negation
p p false
p (p false)
(p p) false
Negation
p p false
p (p false)
(p p) false
p p q p r q
p p p q q r
true p false r
pr
There are two natives A and B. A says, B is
a knight is the same as I am a knave.
What can you determine about A and B?
There are two natives A and B. A says, B is
a knight is the same as I am a knave.
What can you determine about A and B?
As statement is: BA
There are two natives A and B. A says, B is
a knight is the same as I am a knave.
What can you determine about A and B?
As statement is: BA
So, we have: ABA
AAB
false B
B
A?
Golden Rule
pqpqpq
Implication
p q p p q
p q q p q
If I am a knight, B is a knight
AB
If I am a knight, B is a knight
AB
AAB
AAAB
AB
Three of the inhabitants A, B, and C were
standing together in a garden. A stranger
passed by and asked A, Are you a knight or
a knave? A answered but the stranger could
not understand. The stranger then asked B,
What did A say?. B replied, A said that he
is a knave. At this point, the third C, said,
Dont believe B; he is lying!
So, we have:
(B A A) (C B)
Bs statement is: A A
Cs statement is: B
So, we have:
(B A A) (C B)
B (C B)
( B C) ( B B)
( B C) B
BC
A says, either I am a knave or B is a knight
A says, either I am a knave or B is a knight
AAB
A (A false ) B
A (A B false B)
AABB
A says, either I am a knave or B is a knight
AAB
A (A false ) B
A (A B false B)
AABB
AB
Rule of Sequential Composition
wp(S1;S2,Q) wp(S1, wp(S2,Q))
Rule of Sequential Composition
wp(S1;S2,Q) wp(S1, wp(S2,Q))
2. WP(S, True)
Universal set
(m = i or m = j) and (m j and m j)
ELSE PART
(i > j)
m = j;
(m = i or m = j) and (m j and m j)
ELSE PART
(i > j)
m = j;
(m = i or m = j) and (m j and m j)
(i>j) and (j = i or j = j) and (j i and j j)
ELSE PART
(i > j)
m = j;
(m = i or m = j) and (m j and m j)
(i>j) and (j = i or j = j) and (j i and j j)
(i>j) and (true) and (j i and true)
ELSE PART
(i > j)
m = j;
(m = i or m = j) and (m j and m j)
(i>j) and (j = i or j = j) and (j i and j j)
(i>j) and (true) and (j i and true)
(i>j) and (j i)
ELSE PART
(i > j)
m = j;
(m = i or m = j) and (m j and m j)
(i>j) and (j = i or j = j) and (j i and j j)
(i>j) and (true) and (j i and true)
(i>j) and (j i)
(i>j)
IF PART
(i <= j)
m = i;
IF PART
(i <= j)
m = i;
(m = i or m = j) and (m i and m i)
IF PART
(i <= j)
m = i;
(m = i or m = j) and (m i and m i)
(i j) and (i = i or i = j) and (i i and i j)
IF PART
(i <= j)
m = i;
(m = i or m = j) and (m i and m i)
(i j) and (i = i or i = j) and (i i and i j)
(i j) and (true) and (true and i j)
IF PART
(i <= j)
m = i;
(m = i or m = j) and (m i and m i)
(i j) and (i = i or i = j) and (i i and i j)
(i j) and (true) and (true and i j)
(i j) and (i j)
IF PART
(i <= j)
m = i;
(m = i or m = j) and (m i and m i)
(i j) and (i = i or i = j) and (i i and i j)
(i j) and (true) and (true and i j)
(i j) and (i j)
(i j)
Design by Contract
Design by Contract and the language that implements the Design by
Contract principles (called Eiffel) was developed in Santa Barbara
by Bertrand Meyer (he was a UCSB professor at the time, now he is
at ETH)
Bertrand Meyer won the 2006 ACM Software System Award for the
Eiffel!
Award citation: For designing and developing the Eiffel
programming language, method and environment, embodying
the Design by Contract approach to software development and
other features that facilitate the construction of reliable,
extendible and efficient software.
The company which supports the Eiffel language is located in Santa
Barbara:
Eiffel Software (http://www.eiffel.com)
The material in the following slides is mostly from the following
paper:
Applying Design by Contract, B. Meyer, IEEE Computer, pp.
40-51, October 1992.
Dependability and Object-
Orientation
An important aspect of object oriented design is reuse
For reusable components correctness is crucial since an error in
a module can effect every other module that uses it
Main goal of object oriented design and programming is to improve
the quality of software
The most important quality of software is its dependability
Availability(deliver services when requested)
Reliability(deliver services specified)
Safety(operate without catastrophic failure)
Security(defend itself against intrusion)
Design by contract presents a set of principles to produce
dependable and robust object oriented software
Basic design by contract principles can be used in any object
oriented programming language
What is a Contract?
There are two parties:
Client which requests a service
Supplier which supplies the service
Benefit of the client is the obligation of the supplier, and vice versa.
What is a Contract?
As an example lets think about the
contract between a tenant and a landlord
Party Obligations Benefits
This results in redundant checks (for example, both caller and callee may check the
same condition)
A lot of checks makes the software more complex and harder to maintain
procedure_name(argument declarations) is
-- Header comment
require
Precondition
do
Procedure body
ensure
Postcondition
end
Design by Contract in Eiffel
An example:
put_child(new_child: NODE) is
-- Add new to the children of current node
require
new_child /= Void
do
... Insertion algorithm ...
ensure
new_child.parent = Current;
child_count = old child_count + 1
end -- put_child
For example, a class invariant for a binary tree could be (in Eiffel notation)
invariant
left /= Void implies (left.parent = Current)
right /=Void implies (right.parent = Current)
Design by Contract and
Inheritance
Inheritance enables declaration of
subclasses which can redeclare some of
the methods of the parent class, or provide
an implementation for the abstract
methods of the parent class
Inheritance: Preconditions
If the precondition of the
ClassB.someMethod is stronger than the Client ClassA
precondition of the ClassA.someMethod,
then this is not fair to the Client someMethod()
someMethod()
Inheritance: Postconditions
If the postcondition of the
ClassB.someMethod is weaker than the Client ClassA
postcondition of the ClassA.someMethod,
then this is not fair to the Client someMethod()
someMethod()
In ClassA: In ClassB which is derived from ClassA:
invariant invariant
classInvariant newClassInvariant
someMethod() is someMethod() is
require require
Precondition newPrecondition
do do
Procedure body Procedure body
ensure ensure
Postcondition newPostcondition
end end
Client ClassA
The precondition of ClassB.aMethod is defined as:
someMethod()
newPrecondition or Precondition
return o != null;
return o != null;
}
Some additional rules about preconditions
Software
Module Test
Design
148
VDM for Requirements
Analysis
F
i
n
a
l
P
r
o
d
u
c
t
R
e
q
u
i
r
e
m
e
nt
s
Software
Module Test
Design
149
VDM for Analysis & Design
F
i
n
a
l
P
r
o
d
u
c
t
R
e
q
u
i
r
e
m
e
nt
s
Software
Module Test
Design
150
VDM for the Full Life-cycle
Model
F
i
n
a
l
P
r
o
d
u
c
t
R
e
q
u
i
r
e
m
e
nt
s
Software
Module Test
Design
Code
Unit Test
Generation
151
Case Study - Incubator
The temperature needs to be carefully controlled and monitored in order to provide
the correct conditions for a particular biological experiment to be undertaken.
The temperature of the incubator increments or decrements and each time a change
of one degree has been achieved, the software is informed of the change, which it
records.
Safety requirements dictate that the temperature of the incubator must never be
allowed to rise above 10 C, nor fall below -10 C.
UML Specifications
IncubatorMonitor
temp: integer
increment()
decrement()
getTemp(): int
Specifying the State in VDM-SL
State refers to the permanent data that must be stored by the system, and which can
be accessed by means of operations.
The state is specified by declaring variables, in a very similar manner to the way that
this is done in a programming language
state IncubatorMonitor of
temp : Z
end
increment()
wr temp: Z
pre true
values
MAX: Z =10
MIN: Z = -10
is read is defined as