Escolar Documentos
Profissional Documentos
Cultura Documentos
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 1 / 30
Overview
1 Objective
2 The main contribution of the paper
3 How to prove the Correctness of program with loop
4 Translation
5 VIAP architecture
6 Proof strategies
7 Strategy 1: direct proof
8 Strategy 2: simple induction
9 A comparative study with other automatic verifiers
10 Partial correctness and total correctness
11 Conclusion and Future Work
12 References
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 2 / 30
Objectives
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 3 / 30
The main contribution of the paper
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 4 / 30
How to prove the Correctness of program with loop
First consider partial correctness
The loop may not terminate, but if it does, the post-condition will hold
{P} while B do S {Q}
Find an invariant Inv such that
P ) Inv
The invariant is initially true
{ Inv && B } S {Inv}
Each execution of the loop preserves the invariant
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 6 / 30
What Translation Gives
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 7 / 30
Translation Example
Sequence Statement
X =X +Y
Y =2⇤X
can be captured by the following two axioms:
X1 = X + Y ,
Y1 = 2 ⇤ (X + Y )
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 8 / 30
Translation of Loop
For a while loop like while C { B }, it generates the following two formulas:
Representing Loop
¬C (N), (1)
8n.n < N ! C (n) (2)
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 9 / 30
Example
Consider the following while loop
while X < M do { X = f ( X ) }
What does it output? No e↵ect on M, but for X, it depends on f:
M1 = M,
X M ! X1 = X ,
X < M ! X1 = X (N),
X (0) = X ,
8n.X (n + 1) = f (X (n)),
X (N) M,
8n.n < N ! X (n) < M
X1 = X (N)
N denotes the number of iterations that the loop runs until termination.
X(n) is the value of X after the nth iteration.
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 10 / 30
VIAP architecture
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 11 / 30
VIAP architecture
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 12 / 30
Proof strategies
Encoding of First order axioms in Z3. For example, to encode in Z3 the
following axiom that universally quantifies natural number variable n:
n < N2 ! r7 (n) Y
we use an integer type variable n and add the condition n 0:
n=Int(’n’)
Y=Int(’Y’)
N2=Const(’N2’,IntSort())
r7=Function(’r7’,IntSort(),IntSort())
ForAll([n], Implies(n>=0, Implies(n<N2,r7(n)>=Y)))
The second issue that we had with Z3 was that it has difficulty with
axioms of the form (2), which is crucial for our axiomatization of loops.
Our strategy is to add the following consequence of (2) to the axiom set:
N = 0 _ C (N 1). (3)
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 13 / 30
Proof strategies
Another issue is that many of the benchmark programs that we tried have
exponentiation. We found that Z3’s builtin module NLSat (nonlinear real
arithmetic) was too weak most of the time. Instead, we define our own
exponentiation function power (x, y ) on integers:
power=Function(’power’,IntSort(),IntSort()
,IntSort())
ForAll([n],Implies(n>=0, power(0,n)==0))
ForAll([n,m],Implies(power(m,n)==0,m==0))
ForAll([n],Implies(n>0, power(n,0)==1))
ForAll([n,m],Implies(power(n,m)==1,
Or(n==1,m==0)))
ForAll([n,m],Implies(And(n>0,m>=0),
power(n,m+1)==power(n,m)*n)))
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 14 / 30
Strategy 1: direct proof
To illustrate how our system works, consider the following program for
computing the integer square root of a given non-negative integer X
a =0; su =1; t =1;
while ( su <= X ) { a = a +1; t = t +2; su = su + t ;}
return a ;
To prove the correctness of the program we need to prove the following
post-conditions
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 15 / 30
After Translation To FOL Axioms
After Translation of Square Root program to First Order Logic Axioms,
We will have following set of Axioms
X1 = X ,
a1 = a3 (N), t1 = t3 (N), su1 = su3 (N),
a3 (n + 1) = a3 (n) + 1, t3 (n + 1) = t3 (n) + 2,
su3 (n + 1) = su3 (n) + t3 (n) + 2,
a3 (0) = 0, t3 (0) = 1, su3 (0) = 1,
(su3 (N) > X ),
(n < N) ! (su3 (n) X ),
where a denotes the input value of the program variable a, a3 (n) a
temporary function denoting the value of a after the nth iteration of the
while loop. Similar conventions apply to X , t, su and their subscripted
versions. The constant N is introduced to denote the number of iterations
of the while loop before it exits. The variable n ranges over natural
numbers and is universally quantified.
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 16 / 30
Find Close Form Solution and Eliminate Terms
Computes closed-form solutions for a3 (), t3 (), and su3 (), eliminates them,
and produces the following axioms
X1 = X ,
a1 = N,
t1 = 2N + 1,
su1 = N ⇤ N + 2 ⇤ N + 1,
(N ⇤ N + 2 ⇤ N + 1 > X ),
(n < N) ! (n ⇤ n + 2 ⇤ n + 1 X ).
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 17 / 30
Z3 query
_s . add ( X1 == X )
_s . add ( a1 == _N1 )
_s . add ( t1 == 2* _N1 + 1)
_s . add ( su1 == _N1 * _N1 + 2* _N1 + 1)
_s . add ( _N1 * _N1 + 2* _N1 + 1 > X )
_s . add ( ForAll ([ _n1 ] , Implies ( And (( _n1 < _N1 ) , _n1 >=0)
, _n1 * _n1 + 2* _n1 + 1 <= X )))
_s . add ( Or ( _N1 ==0 , _N1 * _N1 <= X ))
_s . add ( _N1 >=0)
_s . add (X >=0)
Now if we add the following sentence to the above query to prove post
condition a1 ⇤ a1 < X :
_s . add ( Not ( a1 * a1 <= X ))
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 18 / 30
Strategy 2: simple induction
To illustrate how our system works, consider the following program which
implements the well-known Cohns integer division algorithm-
Q =0; R = X ;
while ( R >= Y ) do {
A =1; B = Y ;
while ( R >= 2* B )
do { A = 2* A ; B = 2* B ;}
R = R-B; Q = Q+A; }
// return Q = X / Y ;
To prove the correctness of the program we need to prove the following
post-conditions
X = Y ⇤ Q1 + R1,
Y 1 > R1
R1 0
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 19 / 30
Find Close Form Solution and Eliminate Terms
Y1 = Y , X1 = X ,
A1 = A7 (N2 ), q1 = q7 (N2 ), r1 = r7 (N2 ), B1 = B7 (N2 ),
r7 (n) < 2 ⇥ 2N1 (n) ⇥ Y ,
n1 < N1 (n2 ) ! r7 (n2 ) 2 ⇥ 2n 1 ⇥ Y ,
A7 (n + 1) = 2N1 (n) ⇥ 1,
B7 (n + 1) = 2N1 (n) ⇥ Y ,
q7 (n + 1) = q7 (n) + 2N1 (n) ⇥ 1,
r7 (n + 1) = r7 (n) 2N1 (n) ⇥ Y ,
A7 (0) = A, B7 (0) = B, q7 (0) = 0, r7 (0) = X ,
r7 (N2 ) < Y ,
n < N2 ! r7 (n) Y
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 20 / 30
Simple Induction
As an illustration, consider proving the following postcondition
X = q1 Y + r1 (recall that q1 and r1 denote the output values of q and r ,
respectively):
X = q1 Y + r1 (4)
under the precondition :
X 0^Y >0
for the above Cohen’s division program. First VIAP converts the
postcondition into the following assertion using the equations about q1
and p1 in the set of axioms:
X = q7 (N2 )Y + r7 (N2 ).
Then it tries to prove it directly using Z3, but fails. So it tries to prove the
following more general one
8n.X = q7 (n)Y + r7 (n) (5)
by induction on n, which succeeds.
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 21 / 30
A comparative study with other automatic verifiers
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 22 / 30
Experimental Results(Table)
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 23 / 30
Partial correctness and total correctness
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 24 / 30
Partial correctness and total correctness
_s . add ( X1 == X2 ( _N1 ))
_s . add ( ForAll ([ _n1 ] , Implies ( _n1 >=0 ,
X2 ( _n1 +1)== If ( X2 ( _n1 )==0 ,1 , X2 ( _n1 )))))
_s . add ( X2 (0) == X )
_s . add (( X2 ( _N1 )==1))
_s . add ( ForAll ([ _n1 ] , Implies ( And (( _n1 < _N1 )
,_n1 >=0) ,
Not ( X2 ( _n1 )==1))))
_s . add ( _N1 >=0)
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 25 / 30
Conclusion and Future Work
The translation needs to be done only once for a program and the
translated axioms can be used to prove multiple post-conditions.
While not shown in this paper, properties about the program during
its execution can be expressed and proved as well. All one needs is a
label to mark the point of interest - see [Lin16].
For each loop in the program, the translated axioms include an
explicit representation of the exact number of iterations that this loop
executes before terminating. All properties about the loops are
expressed in terms of this representation.
Loop invariants are not required. Of course, they can be added to the
axioms if available. Our experiments show that for many programs,
VIAP can prove properties about them that would normally require
proper loop invariants. In fact, most of these programs are
benchmarks for testing systems that discover loop invariants.
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 26 / 30
Thank You
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 27 / 30
References I
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 29 / 30
References III
Pritom Rajkhowa and Fangzhen Lin (HKUST) VIAP September 20, 2017 30 / 30