Formal Semantics Problem Set 4 Solutions: GCD Algorithm and Loop Invariants, Assignments of Computer Science

The solutions to problem set 4 of the cpsc 430: formal semantics course, focusing on the gcd algorithm and loop invariants. It includes the proof of total correctness for a given program using the rules of inference and the strong progress property.

Typology: Assignments

Pre 2010

Uploaded on 11/08/2009

koofers-user-1cz
koofers-user-1cz 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPSC 430 : Formal Semantics
Paul Hudak
Date: October 18, 2007
Problem Set 4 Solution
Problem 3.1
1. y=x2
2. y=zx=w
3. (aby=x×b)(a<by=x×a)
4. true
5. false
Problem 3.2
Consider the program:
newvar x:=a in
newvar y:=b in
(while y > 0 do
newvar t:=x rem y in
(x:=y; y:=t);
c:=x)
The proof of total correctness is:
pf3

Partial preview of the text

Download Formal Semantics Problem Set 4 Solutions: GCD Algorithm and Loop Invariants and more Assignments Computer Science in PDF only on Docsity!

CPSC 430 : Formal Semantics Paul Hudak Date: October 18, 2007

Problem Set 4 Solution

Problem 3.

  1. y = x^2
  2. y = z ∧ x = w
  3. (a ≥ b ∧ y = x × b) ∨ (a < b ∧ y = x × a)
  4. true
  5. false

Problem 3.

Consider the program:

newvar x:=a in newvar y:=b in (while y > 0 do newvar t:=x rem y in (x:=y; y:=t); c:=x)

The proof of total correctness is:

  1. (a ≥ 0 ) ∧ (b ≥ 0 ) ⇒ (a ≥ 0 ) ∧ (b ≥ 0 ) ∧ (gcd(a, b) = gcd(a, b))
  2. [(a ≥ 0 ) ∧ (b ≥ 0 )] x := a; y := b [(x ≥ 0 ) ∧ (y ≥ 0 ) ∧ (gcd(x, y) = gcd(a, b))] (RAS 2 , 1)
  3. (x ≥ 0 ) ∧ (y ≥ 0 ) ∧ (gcd(x, y) = gcd(a, b)) ∧ (y > 0 ) ∧ (y = m) ⇒ (y ≥ 0 ) ∧ (x rem y ≥ 0 ) ∧ (gcd(y, x rem y) = gcd(a, b)) ∧ (x rem y < m)
  4. [(x ≥ 0 ) ∧ (y ≥ 0 ) ∧ (gcd(x, y) = gcd(a, b)) ∧ (y > 0 ) ∧ (y = m)] t := x rem y; x := y; y := t [(x ≥ 0 ) ∧ (y ≥ 0 ) ∧ (gcd(x, y) = gcd(a, b)) ∧ (y < m)] (RAS 3 , 3)
  5. [(x ≥ 0 ) ∧ (y ≥ 0 ) ∧ (gcd(x, y) = gcd(a, b)) ∧ (y > 0 ) ∧ (y = m)] newvar t:=x rem y in (x:=y; y:=t) [(x ≥ 0 ) ∧ (y ≥ 0 ) ∧ (gcd(x, y) = gcd(a, b)) ∧ (y < m)] (DC, 4)
  6. (x ≥ 0 ) ∧ (y ≥ 0 ) ∧ (gcd(x, y) = gcd(a, b)) ∧ (y > 0 ) ⇒ (y ≥ 0 )
  7. [(x ≥ 0 ) ∧ (y ≥ 0 ) ∧ (gcd(x, y) = gcd(a, b))] while y>0 do newvar t:=x rem y in (x:=y; y:=t) [(x ≥ 0 ) ∧ (y ≥ 0 ) ∧ (gcd(x, y) = gcd(a, b)) ∧ ¬(y > 0 )] (W HT, 5, 6)
  8. (x ≥ 0 ) ∧ (y ≥ 0 ) ∧ (gcd(x, y) = gcd(a, b)) ∧ ¬(y > 0 ) ⇒ (x = gcd(a, b))
  9. [(x ≥ 0 ) ∧ (y ≥ 0 ) ∧ (gcd(x, y) = gcd(a, b))] while y > 0 do newvar t:=x rem y in (x:=y; y:=t) [x = gcd(a, b)] (WC, 7, 8)
  10. [x = gcd(a, b)] c:=x [c = gcd(a, b)] (AS)
  11. [(a ≥ 0 ) ∧ (b ≥ 0 )] x:=a; y:=b; while y > 0 do newvar t:=x rem y in (x:=y; y:=t); c:=x [c = gcd(a, b)] (MSQ 3 , 2, 9, 10)
  12. [(a ≥ 0 ) ∧ (b ≥ 0 )] x:=a; newvar y:=b; in (while y > 0 do newvar t:=x rem y in (x:=y; y:=t); c:=x) [c = gcd(a, b)] (DC, 11)
  13. [(a ≥ 0 ) ∧ (b ≥ 0 )] newvar x:=a in newvar y:=b; in (while y > 0 do newvar t:=x rem y in (x:=y; y:=t); c:=x) [c = gcd(a, b)] (DC, 12)

Problem 3.

  1. [x > 0 ∧ y = y 0 + (x 0 − x) × (x 0 + x − 1 ) ÷ 2 ∧ x = cnt] (x := x - 1; y := y + x) [x ≥ 0 ∧ y = y 0 + (x 0 − x) × (x 0 + x − 1 ) ÷ 2 ∧ x + 1 = cnt] (RAS 2 )
  2. x ≥ 0 ∧ y = y 0 + (x 0 − x) × (x 0 + x − 1 ) ÷ 2 ∧ x 6 = 0 ∧ x = cnt ⇒ x > 0 ∧ y = y 0 + (x 0 − x) × (x 0 + x − 1 ) ÷ 2 ∧ x = cnt
  3. x ≥ 0 ∧ y = y 0 + (x 0 − x) × (x 0 + x − 1 ) ÷ 2 ∧ x + 1 = cnt ⇒ x ≥ 0 ∧ y = y 0 + (x 0 − x) × (x 0 + x − 1 ) ÷ 2 ∧ x < cnt]