• Tidak ada hasil yang ditemukan

Reasoning about Programs

N/A
N/A
Protected

Academic year: 2023

Membagikan "Reasoning about Programs"

Copied!
2
0
0

Teks penuh

(1)

The University of Waikato Department of Computer Science Dr. Robi Malik

COMP 340-07B

Reasoning about Programs

Assignment 5

In this assignment, you will complete the proof of the Euclidean Algorithm that was started in Assign- ment 4. The algorithm is given by the following Java program, and is claimed to compute the greatest common divider (GCD) for all pairs of non-negative integersxandy, unless both inputs are 0.

int gcd(int x, int y) {

if (y == 0) { return x;

} else {

return gcd(y, x % y);

} }

This assignment is to be done using the RISC ProofNavigator, which is installed in the Linux labs. Please download the theory file comp340-ass5.pnfrom the COMP 340-07B course home page,

http://cs.waikato.ac.nz/~robi/comp340-07b/

and use it for the following exercises. This file contains the needed propositions from the previous assignment, plus some new conjectures to be attacked now.

Exercise 1

(1+1+1+1+1+2+2 marks)

As a first step, some properties of the greatest common divider (GCD) of two non-negative integers need to be established. Given the axioms

(GCDdivides) ∀x: , y: (gcd(x, y)|x∧gcd(x, y)|y) (GCDmax) ∀d: , x: , y: (d|x∧d|y→gcd(x, y)≥d)

and using results about the “divides” relation from assignment 4, prove the following properties.

a) (GCDzero2) ∀x: (x6= 0→gcd(x,0) =x)

Hint.Remember that, instead of provingA=B, it sometimes is easier to prove A≤B∧B≤A.

b) (GCDself) ∀x: (x6= 0→gcd(x, x) =x)

c) (GCDleq) ∀v: , w: , x: , y: (∀d: (d|v∧d|w↔d|x∧d|y)→gcd(v, w)≤gcd(x, y)) d) (GCDeq) ∀v: , w: , x: , y: (∀d: (d|v∧d|w↔d|x∧d|y)→gcd(v, w) =gcd(x, y))

Hint.Prove this using (GCDleq) and use it for the following proofs.

e) (GCDcomm) ∀x: , y: gcd(x, y) = gcd(y, x) f) (GCDplus) ∀x: , y: gcd(x, y) = gcd(x+y, y)

Hint.Use (GCDeq), (DIVplus), and (DIVminus).

g) (GCDminus) ∀x: , y: (x≥y→gcd(x, y) =gcd(x−y, y))

(2)

Exercise 2

(1+2+2+1 marks)

As a second step, the result about subtraction needs to be lifted to the modulo operation. This becomes possible after some preparation, because modulo can be calculated by repeated subtraction. Using the axiom

(MOD) ∀x: , y: (y6= 0→xmody < y∧ ∃q: x=q·y+xmody) prove the following properties.

a) (MOD0) ∀x: (x6= 0→0 modx= 0) Hint.Use type axioms.

b) (MODless) ∀x: , y: (x < y→xmody=x) Hint.Perform case distinction and use type axioms.

c) (GCDminusn) ∀x: , y: , n: (x≥n·y→gcd(x, y) = gcd(x−n·y, y)) Hint.Use induction onn.

d) (GCDmod) ∀x: , y: (y6= 0→gcd(x, y) = gcd(xmody, y))

Exercise 3

(2+1+2 marks)

Finally, to prove thegcd algorithm correct, the function gcdimpl is defined by

(GCDIMPL) ∀x: , y: gcdimpl(x, y) =ify= 0 thenxelse gcdimpl(y, xmody)endif and proven to give the same result as the gcd function for all acceptable inputs. This needs to be done bystrong induction on the smaller of the two inputs. The smaller of the two inputs isy, except maybe for the first call.

Strong induction differs from regular induction in that the inductive hypothesis does not just consider one number n, but also all numbers less or equal to n. This kind of induction is not directly sup- ported by ProofNavigator, but can easily be emulated through normal induction. To do so, prove the formula (GCDimplaux1) using induction on y and case distinction on n = 0, and instantiate it to get (GCDimplaux2).

This proves the algorithm correct for all cases where the input satisfies x ≥ y. This result can be generalised using case distinction onx≥y, producing the final result (GCDimpl).

Therefore, to complete the verification of thegcd algorithm, prove the following formulas.

a) (GCDimplaux1) ∀n: , x: , y: (n≤y∧x≥n∧x6= 0→gcdimpl(x, n) = gcd(x, n)) b) (GCDimplaux2) ∀x: , y: (x≥y∧x6= 0→gcdimpl(x, y) = gcd(x, y))

c) (GCDimpl) ∀x: , y: (x6= 0∨y6= 0→gcdimpl(x, y) = gcd(x, y))

Submission

The RISC ProofNavigator will save your proofs in a directory called comp340-ass5. Please pack this directory into a .tar.gzarchive using the command

tar czf comp340-ass5.tar.gz comp340-ass5

and submit the archive through the assignment submission system at

http://byerley.cs.waikato.ac.nz/~tonym/submit/

before the due date. Please note that the assignment submission system works only from our computing laboratories on campus.

This assignment will be verified during the tutorial session on Wednesday 5 September 2007 from 12:00–

13:00 in Computing Laboratory 7 (R G.19). If you cannot attend at this time, please arrange an alter- native verification time with the lecturer before the due date.

Due date: Tuesday, 21st August 2007, 15:00

Referensi

Dokumen terkait

Vapor V, y* Condensor Distilat D y* L, x Feed F, xF Bottom B, xB Continuous flash distillation • Merupakan metode distilasi paling sederhana • Feed masuk dalam