CSC195, Class 31: Assignment, Continued Overview: * Problems from last class * Assigning to arrays * Assigning multiple values * Other issues Notes: * Extra credit for convo * Have a great break! * Questions on exam? On "the first problem" (readLine) Three cases: The line contains fewer than max characters 0 The line contains more than max characters 1 EOF -1 What to do for too many characters? Stop reading. Leave the file cursor where it is. On "the second problem" (rdLn) What if it's twenty characters long? 20abcdeabcdeabcdeabcde STRINGS CANNOT BEGIN WITH DIGITS Daren recommends that you use scanf("%d", &size); ---------------------------------------- Review: * What's wp, the weakest precondition xxx? wp is a function from statement and postcondtion that returns A set of states such that your postcondition will hold and the statement will behave correctly We often represent the set of states as a predicate * What is substitution? "Substitute e for x in E" Replace all unbound instances of x in E with e, taking care not to bind variables in e New notation E[x->e] * How do we use these two things to define simple assignment? We can assign a variable to another variable when x is in wp("x := y", P) NO x is either an integer, a Boolean, or arrays wp("x := y", P) = P[x->y] wp("x := e", P) = P[x->e] wp("x := y+1", x=y+1) (x=y+1)[x->y+1] x[x->y+1]=(y+1)[x->y+1] y+1 = y+1 number(y) wp("x := x+1", x > 5) (x>5)[x->x+1] (x+1)>5 x > 4 wp("x := x+1", x = x+1) (x=x+1)[x->x+1] x+1=x+1+1 x+1=x+2 false Moral: If there is no state that satisfies the postcondition, you're not going to have any states in the precondition wp("x := a[i]", x=a[i]) i is a valid index of a wp("x := a[x]", x=a[x]) Can it happen? Yes Does it usually happen? Who knows? Can it not happen? Yes Precondition: x is in valid indices, a[x] is in valid indices, and a[x] = a[a[x]] We now know something about assigning to simple variables. Cool variant assignment in GOL v1,v2,...,vn := e1,e2,...,en Meaning (informal): Evaluate e1 ... en; Assign "en masse" to v1 ... vn Why would we ever want a statement like this? Gain benefits from simultaneity Weakest precondition might change every time if you're assigning separately Saves work might make array substitution easier Makes swapping-like operations easier x,y := y,x wp("v1,...,vn := e1,...,en", P) = E[v1...vn->e1....en] "Hack for multiple substitution": Add a temporary for each variable; assign ei to tempi (in order); assign tempi to vi (in order) Better solution: Go through recursively. Base cases are now vi[v1...vn->e1...en] => ei vj[v1...vn->e1...en] => vj, j not in 1 .. n ----- Assigning to array elements wp("a[i] := e", P) PROBLEM: a[i] may not appear explicitly in P, but it may appear implicitly. e.g., "a[5] = 2", a[x+3] > 1" The Gries "hack" What does "a[i] := e" mean? Have the ith term of a refer to e. Do we have notation for that? (a,i:e) - The array a, with e in the ith position a[i] := e is the same as a := (a,i:e) wp("a := (a,i:e)", P) = P[a->(a,i:e)] wp("a[i] := e", P) = P[a->(a,i:e)]