LARA

Lecture 06

Q&A session for homeworks and quiz.

Otherwise, you can try to solve this problem as an exercise:

Exercise on Arrays

For the next code fragment, do the following

  1. transform the loop into guarded command language
  2. construct a formula F(x, x') for the loop body
  3. write down the loop invariant and show it is inductive with respect to the given pre- and postcondition.

\begin{equation*}
P: n> 0\wedge \forall i. (0 \le i\le n-1) \to (a[i] = a_0[i]) 
\end{equation*}

\begin{equation*}
Q: \forall i. (0 \le i\le n-2) \to (a[i] = a_0[i+1]) \wedge \forall i. (i = n-1) \to (a[i] = a_0[0])
\end{equation*}

e = a[0];
i = 0;
while(i <= n-2) {
  a[i] = a[i+1];
  i = i + 1;
}
a[n-1] = e