Textual substitution, Equality, and Assignment

advertisement
Textual substitution, Equality, and Assignment
Syntax of Simple Expressions:
(defined recursively)(syntax refers to rules for composition, semantics refers to meaning)
 a constant is an expression
 a variable is an expression
 if E is an expression, then (E) is an expression
 if circle is a unary prefix operator and E is an expression then circle E is an expression, with
operand E. (e.g. – is a unary prefix operator so –5 is an expression)
 if * is a binary infix operator and D and E are expressions, then D*E is too with operands D and E.
(e.g. + *(times) are )
Parentheses are used to group items together. Precedence is assigned to operators in order to reduce
the necessity for parentheses. A list of precedence is on the inside front cover of the book.
Defn: a STATE is a list of variables together with a set of values for these variables. (x,5), (y,6) is the
state in which x is 5 and y is 6.
Evaluation of an Expression, E, in a state, S, is accomplished by replacing all variables in E by their
values in S and then computing the value E. So if E is x-y+2 and S is as above then E is 1.
Textual Substitution Let E and R be expressions and let x be a variable. E [x:=R] or ExR denotes the
expression that is the same as E except that all occurrences of x have been replaced by "( R)".
Do stuff on page 8.
Look at z+y[z,y:=5,6] is z+6
(z+y)[z,y :=5,6] is 5+6.
Look at (z+y)[z,y:= y*y,w] is y*y+w NOT w*w+w because simultaneous substitution is uh…
simultaneous.
NOTE:
 If you have simultaneous substitution the x's must be distinct.
 This rule is for textual substitution of variables not expressions.
Textual substitution is left associative:
E[x:=R][y:=Q] is (E[x:=R])[y:=Q]. Therefore in general E[x:=R][y:=Q] = E[x,y:=R,Q].
Do (x+2y) [x,y := y,x] and (x+2y)[x:= y][y:=x]
Hidden Variables must be taken account of.
First Rule of Textual Substitution: is an inference rule, which is a syntactic mechanism for deriving
THEOREMS or truths. (A Theorem is an expression that is true in all states) An inference rule
consists of a list of expressions, called premises or hypotheses or antecedents and an expression
called the conclusion or consequence. The premises are separated from the conclusion by a horizontal
line with the premises above and the conclusion below. The inference rule claims that if the premises
are theorems the so is the conclusion.
E
The inference rule of substitution is stated : (1.1) ---------------------------. This simply states that if
E[v := F]
E is an expression, and v is a list of variables and F a corresponding list of expressions, then if E is a
theorem, then so is E with all occurrences of v replaced by F.
Consider the following example: E: x+y = y+x. v is x,y and F is z,2 then the substitution principle
allows us to say that x+y= y+x yields z+2 = 2+z.
Textual Substitution and Equality Consider the expression X=Y. If we take a particular state S and
evaluate X and Y in the state S and in that state X=Y then the expression X=Y has a value of true in
the state S. It has the value false if this is not true. HOWEVER it is inconvenient to define equality in
this manner. For one thing to determine if two expressions are equal they must be evaluated in every
possible state. e.g. we know that x=y equals y=x regardless of the values of x and y. What we need is
a set of laws that allow us to transform one expression into another expression while preserving
equality. In that case we can call this a "definition" of equality.
(1.2)
(1.3)
(1.4)
(1.5)
Reflexivity: x = x
Symmetry: (x=y) = (y=x)
Transitivity: X=Y, Y=Z
---------------X=Z
Leibnitz Two expressions are equal in all states iff replacing one with the other in any
expression E does not change the value of E in any state.
X=Y
---------------------------E[z:=X] = E[z:=Y]
We need the z in the textual substitution because substitution is not defined for expressions only
variables.
Leibniz rule and function Evaluation
if g.z : E (g of z is given by E) then g.X = E[z:=X]. do some simple functions like on page 13.
we can use this idea to rephrase Leibniz as
(1.8)
X=Y
----------------g.X = g.Y
This give us a way of reasoning using Leibniz (substituting equals for equals gives equals).
Go through stuff in Section 1.5
Assignment Statement: x := E (x becomes E or x gets E)
(1.10) x := E
Hoare Triple {P} S {Q} where P is a precondition, Q is a postcondition and S is a statement.
Example: {x=0} x := x+1 {x > 0} is VALID iff execution of x := x+1 in any state in which x=0 results
in a state in which x>0. Look at the example on the top of page 18.
Valid Hoare triples for the assignment statement x := E. If R is a post condition then R[x:=E] is a
suitable pre-condition. Hence we are defining the precondition based on the (assignment) statement
and the post-condition. [which is basically the way that we program in that we start out with what we
want and go from there working backwards to what we have.]
(1.12) Definition of Assignment {R[x:=E]} x:=E {R}.
This looks backwards doesn’t it? but consider suppose we want to use the assignment statement
x:=x+1 and we want a post condition of x>4. Then R is x>4 so the pre-condition is x>4[x:= x+1] or as
we just learned x+1 > 4 simplified to x > 3.
Multiple assignment statements (x,y := P, Q) means do x:=P and y:=Q simultaneously. Look at the
example on page 20.
Download