|
I have finished evaluating your final exams. I will leave the exam papers with Margaret Jaffey in Ryerson 161A on Monday for you to pick up at your convenience. You may see your score on the grade record, along with a complete audit of your scores.
-
x,y := x,y,z
x,x := 1
The two restrictions: 1. the number of variables must be the same as the number of expressions; 2. no variable may be repeated on the left-hand side. Neither of these restrictions can be enforced by a CFG in a sensible way.
- A few of you forgot what a parse tree is, or didn't recognize that the commas are part of the notation.
- Only one or two people really nailed this one, and a few more seemed to have the idea, but didn't express it clearly. The parse-tree gimmicks that we used to enforce operator precedence were relatively benign, because they were local in the parse trees. By collapsing some number of adjacent nodes in the parse trees, we were able to produce the shapes of the abstract syntax trees. By contrast, the transformation from parse tree to T1 is inherently global. The transformation is similar to transposition of a matrix. A lot of you said totally bogus things about order of evaluation, which is not fixed by the shapes of the trees. Others made comments that had nothing directly to do with the comparison of tree shapes.
-
- The list representation is slightly more flexible than the array representation of functions. In the array notation, the range of indexes is fixed by the program. In the list representation it may vary dynamically at run time. If you said that they are equally flexible, you were pretty close.
- Functions with an infinite number of nonblank values cannot be represented by lists in the way I described. Functions with only a finite number of nonblank values may be represented by lists.
- Lists containing two different pairs with the same integer on the left, but different characters on the right, do not represent a uniquely determined function.
-
- Any example where i=j works. E.g., i=j=0, X[0]=0 initially. Then X[0]=1 after the assignment. But X[i]=X[0]=1 and
X[j]=X[0]=1 after the assignment, so
X[i] is not =X[j]+1.
- Array references allow different looking expressions to refer to the same l-values. Such pairs of expressions are called aliases. In our example, because i=j, X[i] and X[j] are aliases. When we substitute only for X[i], it gets out of synch with the equivalent, but different looking, expression
X[j].
Nobody solved the optional harder problem. With less time pressure, you can probable figure it out. The weird part is that there are now two exact copies of X[i] to substitute for, so the pre-assertion comes out X[X[i]]=X[X[X[i]]]. This pre-assertion is not always true, so you must find a particular starting state in which it is true, but in which the post-assertion X[i]=X[X[i]] is false after the assignment.
The simplest way to create a rule for reasoning correctly about array assignments is to treat them as assignments to the whole array. That is, treat
X[exp1]:=exp2 as if it were
X:=X[exp1 -> exp2]. This is annoying, because this replacement operator on arrays is not
part of the normal notation of programming languages, so we introduce a discrepancy between the notation in programs and the notation in assertions about programs. There are other ways to design correct rules without introducing new operators on arrays, but they use a rather subtle trick by which logical quantification, plus an assertion of equality, can simulate a programming-language assignment.
-
The key insight is that the body of a for loop has two parameters: the value of the index, and the starting state.
fun for_loop L U S x = if (L>U) then x
else for_loop (L+1) U S (S L x)
Nobody correctly fed the L parameter to S. When you neglect that, you make it impossible for the body S of the loop to use the index variable.
-
'a -> ('a -> 'b -> 'a) -> ('b list) -> 'a
-
val revstart = []
;
fun revred s x = x :: s
;
-
val idstart = []
;
fun idred s x = s @ [x]
;
Mike O'D.
|