0

Give a proof that the following is correct.

{n != 0}    
if n<0 then 
n= -n 
{n>0} 

The following inference rule should help

{B and P} S {Q}, (not B) and P=>Q 
---------------------------------
{P}if B then S{Q} 

I have been looking all over the web for a clear explanation or at least an example to follow along but I don't quite understand it, I found some sites that maybe some help below but there aren't any examples.

Page 148-160

Any help is much appreciated, I would like to see this problem done so I can do others and I am very stuck and the book doesn't show any examples.

These links may also be of some help. Thanks, 10 Points!

http://en.wikipedia.org/wiki/Hoare_logic#Conditional_rule

wadie
  • 496
  • 1
  • 10
  • 24

1 Answers1

0

If you read carefully the book you will see that fule rule for selection statement looks like:

{B and P} S1 {Q}, {(not B) and P} S2 {Q}
--------------------------------------
{P} if B then S1 else S2 {Q}

You can find quite obvious fact in the book that

first logical statement above the line represents the then clause; the second represents the else clause.

So if you have only then section the rule changes to

{B and P} S {Q}, {not B and P} {Q}
--------------------------------------
{P} if B then S {Q}

It's very logic rule. You will reach sequence S if and only if B is true. So you can add it to the precondition statement.

In your case the inference rule looks like:

{n < 0 and n != 0} n = -n {n > 0}, {n >= 0 and n != 0} {n > 0}
--------------------------------------------------------------
{n != 0} if n < 0 then n = -n {n > 0}

If we prove logical statements above the line we will prove statement below the line.

Statement

{n < 0 and n != 0} n = -n {n > 0}

can be proved by assignment axiom which can be found on page 150 in the book.

n = -n {n > 0}

We have to substitute n with n = -n in the postcondition. So we have

{-n > 0}

which equals to

{n < 0}

which in turn satisfies precondition.

Statement

{n >= 0 and n != 0} {n > 0}

obviously is correct too. Well, we've ensured that both statements above the line are correct so statement below the line correct as well.