200 likes | 331 Views
Discrete Structures Lecture 3 Assignment. Review Problem. (1.8) Leibniz: X = Y E[z:=X] = E[z:=Y] Suppose: X = a + b; Y = 4•w; E = 2•x + z 2 Complete the following inference rule: a + b = 4•w 2•x + (a + b) 2 = ?.
E N D
Discrete Structures Lecture 3 Assignment
Review Problem (1.8) Leibniz: X = Y E[z:=X] = E[z:=Y] Suppose: X = a + b; Y = 4•w; E = 2•x + z2 Complete the following inference rule: a + b = 4•w 2•x + (a + b)2 = ?
Proofs using Leibniz Leibniz (1.5) allows us to “substitute for equals” in an expression without changing the value of that expression. E[z:=X] = <X=Y> E[z:=Y]
Example ProofMary has twice as many apples as John. Mary throws half of her apples away, because they are rotten, and John eats one of his. Mary still has twice as many apples as John. How many apples did Mary and John have initially? (0.1) m=2•jandm/2=2•(j-1) (1.9) 2•x/2 = x m/2 = 2•(j-1) = < m=2•j, by (0.1) > (2•j)/2 = 2•(j-1) = <unnecessary parentheses> 2•j/2 = 2•(j-1) = < (1.9), with x:= j > j = 2•(j-1) = <Arithmetic> j = 2j - 2 j = 2 eventually will find j=2 and m=4
Proof Pitfalls Do not get sloppy about the proof format. Be careful not to: • Omit the hint. • Omit the = symbol in the left-hand column. • Indent the expressions being manipulated instead of the hints. • Fail to align the beginnings of the expressions or beginnings of the hints.
The Assignment Statement Execution of the assignment statement (1.10)x:= E evaluates the expression E and stores the result in variable x. state: (z,3), (m,11), (s,35) s := z + m state: (z,3), (m,11), (s,14)
Pre- and Post- Conditions We can judge the effect of assignment statements by thinking about pre-conditions and post-conditions. Precondition: Condition that holds before the execution of the statement. Postcondition: Condition that holds after the execution of the statement.
Hoare Triples Hoare triple: (1.11){P} S {Q} A Hoare triple is valid iff execution of the statement S in any state in which the precondition holds results in a state in which the postcondition holds. (Note: iff stands for "if and only if" X iff Y means: X is true if Y is true and Y is true if X is true.)
Examples of Hoare Triples Valid Hoare triples: {x=0} x:=x+1 {x>0} {x+4>11} x:=x+1 {x>0} Hoare triple which is not valid: {x=-6} x:=x+1 {x>0}
Assignment Statement Definition (1.12){R[x:=E]} x:=E {R} x := 5 {x=5} {(x=5)[x:=5]} x := 5 {x=5} {(5=5)} x := 5 {x=5} { T } x := 5 {x=5}
Assignment Statement Definition (1.12){R[x:=E]} x:=E {R} x := 5 {x!=5} {(x!=5)[x:=5]} x := 5 {x!=5} {(5!=5)} x := 5 {x!=5} { F } x := 5 {x!=5}
Assignment Statement Definition (1.12){R[x:=E]} x:=E {R} x := x+1 {x < 0} {(x<0)[x := x+1]} x := x+1 {x < 0} {(x+1)<0} x := x+1 {x < 0} {x < -1} x := x+1 {x < 0}
Assignment Statement Definition (1.12){R[x:=E]} x:=E {R} x := x•x {x4 = 10} {(x4=10)[x:=x•x]} x := x•x {x4 = 10} {(x•x)4=10}x := x•x {x4 = 10} {x8 = 10} {x= 1.231144413} x:=x•x {x4 = 10}
Assignment Statement Summary (1.12){R[x:=E]} x:=E {R} {T} x := 5 {x = 5} {F} x := 5 {x != 5} {x <-1} x := x+1 {x < 0} {x = 1.231144413} {x8 = 10} x:=x•x {x4 = 10}
Multiple Assignment {R[x,y:=E,F]} x,y:= E,F {R} { ? } x,y := x-y,x+y{x+y = C} to get precondition: replace x with x - y and y with x + y in R => (x - y + x + y = C) => (x + x = C) => (2x = C) {2x=C} x,y := x-y, x+y{x+y=C}
Sequential Assignment {(R[y:=F])[x:=E]} x:=E; y:=F {R} {?} x:=x-y; y:=x+y{x+y=C} replace y with x+y in R (x + y = C)[y := x + y] => (x + x + y = C) => (2x + y = C) now have: {?} x:=x-y; {2x+y=C}y:=x+y{x+y=C}
Sequential Assignment continued {?} x:=x-y; {2x+y=C}y:=x+y{x+y=C} {?} x:=x-y; {2x+y=C} replace x with x-y in new R (2x+y= C)[x := x - y] => (2(x-y) + y = C) => (2x-2y + y = C) => (2x- y = C) {2x-y=C} x:=x-y; y:=x+y{x+y=C}
Multiple Assignment Practice {R[x,y:=E,F]} x,y:= E,F {R} { ? } a,b := b+2, a-3 {a•b> 10}
Sequential Assignment Practice {(R[y:=F])[x:=E]} x:=E; y:=F {R} {?} a:=b+2; b:=a-3 {a•b> 10}