Technical Reasoning/Example of Program Validation

Pseudo-code
For a final example, we will look at a computer program written in pseudo-code.

"Pseudo-code" is a term meaning that the code is not written in an actual programming language. Rather, pseudo-code is like a formal blueprint which could be easily written in any programming language.

Because pseudo-code is not an official or uniformly defined language, but rather something in-between a computer language and a natural human language, there is no one version of pseudo-code.

Therefore everyone has their own system of pseudo-code, and I will describe the one that I find useful for this course.

Assignment and Comparison Equality
Let us begin to contrast two very different kinds of equal signs in pseudo-code.
 * , the assignment operator
 * , comparison operator

The choice of these symbols, and the operation that each expresses, is consistent with the OCaml syntax for these concepts.

Both of them have a flavor that is similar to what we mean by "equals", but they each do vastly different things.

The assignment operator takes a variable and assigns to it a value.

So for instance,  will assign to the variable   the value.

The assignment operator requires that the only thing to the left of  is a variable, and the only thing to the right of it is some expression which evaluates to a value. Therefore it is acceptable to have, for example,

s := 1+1

because the expression  evaluates to the value. can then be assigned to the variable.

Now contrast this with the comparison operator. This performs a "test" for equality of two objects.

As a test, it returns a value which may be  or.

For example,

1 = 1

returns

True

and

1 = 0

returns

False

For comparison, the left-hand side and the right-hand side must each be an expression which evaluates to some value. Therefore

1+1 == 2

returns

True

Lists
The program that we will use below assumes that there is a list of numbers, stored in the variable.

The list may be  or it may be. Let us not worry about how the list gets there.

Although the program will assume that some list is stored in the variable, it will not make any assumption about which list is there. The program will only assume that  is filled with numbers that can be added together.

A list essentially represents a collection of data, bundled together. A single list may contain the heights of a bunch of people, or the temperatures for a location over some sequence of days, or anything else.

By putting related data together in a list, is often a signal that somehow the data is related to each other. If the list contains the heights of various people, then by computing the average over the list, one is able to produce the average height for this set of people.

We will not spend much time in this course interpreting the data or values in computer programs. I mention the example of heights only to give a very brief indication of why one might want to bundle data together in the first place.

Structuring and Destructuring
Since lists represent a bundle of information, then it is important to know how one may place data into the bundle and also remove data out of the bundle. One can think of the former as "giving structure" to disparate data, and the later as "removing from structure" or "destructuring" the data in a structure.

Suppose for instance that we want to create a list and place the value 1 in it. One way to do that would be to create an empty list, and then add the value 1 into it. In the code below, I show how one could do this.

Note that the symbol  indicates the start of a "comment". A comment is text inside the program which does not execute, and is only there to help the reader to understand the program.

lst := []       # Create an empty list and place it in the variable lst. lst.append(1)   # Append the value 1 to lst.

Another more elegant way to do this, is to directly write the data into a list and do the assignment all in one line, like so:

lst := [1]

Now once a list contains values, we can retrieve them by way of indexing.

The very first element of a list is located at "index" zero. For this reason, we say that computer lists are "zero indexed". (There are some programming languages, like R, which do not use zero for the first index of a list. But they are quite rare.)

To access the index  of the list   one writes. That is to say, one writes the list, then immediately adjacent to it (with no space) an "open square-bracket", then a number for a position in the list, followed by a close square-bracket.

To demonstrate the use of this, the following program will evaluate to, because the "zeroth" element of the list is  , and the "oneth" element of the list is.

lst := [2,3,5,7] lst[1] = 3

For-loops
The code snippet

1. for element e in lst: 2.  s := s+e 3.  s := s+1

is a somewhat random example of a for-loop.

Let us assume, for the current conversation, that the list,, is equal to. That is to say,  evaluates to.

Note: Throughout the rest of this course, I will write something like  to tell you what is in a given variable. When I write this, I am essentially telling you that the code expression evaluates to.

The for-loop is initiated by line 1 in the snippet, which is called the "head" of the for-loop.

Lines 2 and 3 are what we call the "body" of the for-loop. These lines execute repeatedly, in a loop.

What this for-loop does, by a high-level description, is that it takes each element of the list and adds it into the variable. Then (for no reason other than to show some more lines of code in this demonstration) it also adds 1 to whatever is currently in.

If you assume that  before the for-loop initiates, then when the for-loop terminates,.

At a lower-level description, when a computer runs this code, it maintains an index variable which we cannot see. Let us call the index variable  just so that you and I can talk about it; but of course, there isn't actually a variable   in this program.

First Iteration, :

When the computer reads the for-loop declaration on line 1, it creates the variable  and initializes its value to. Therefore, by the time the computer first reaches line 2,.

At the same time, it assigns to  the value. Therefore, after line 1 is finished,  for our example.

After it does this on line 1, it moves on to line 2. Here, whatever value was previously stored in the variable, the computer now uses to evaluate. Finally, still on line 2, the computer uses this value to store it into the variable, as is instructed by the assignment operator.

In our example, since  before this line is reached, then it evaluates. (Chaining the equations together is just my shorthand way of telling you that all three of these are equal.) Then due to the assignment operator, this is assigned to. Therefore after line 2 executes,.

Now line 2 is complete so it moves to line 3, where it takes the content of, adds  , and assigns this new value into. Before line 3 executes,. After line 3 executes,.

Now line 3 is complete, which marks the end of the body of the for-loop. Whenever reaching the end of a for-loop, we return to the head of the loop, which is line 1 here.

Second Iteration, :

We increment the index and reassign the variable. Therefore, after line 1 finishes executing again,  and.

We now proceed again to line 2, after which.

After line 3 finishes,.

Third Iteration, :

Again we return to the head at line 1, after which  and. We make another "pass through the body of the for-loop", after which.

Fourth Iteration, :

Again we return to the head, after which  and. We make anther pass through the body, after which.

End:

Again we return to the head, after which. However, at this point the computer realizes that the list only has four elements and therefore there is no value at.

Therefore the program ends the for-loop at this point, and moves on to the next part of the program if there is one. (In the code snippet for this example, there is no rest of the program for the computer to move on to. In the example below, there will be.)

The Program
Finally we have reviewed all of the syntax and components needed to analyze the following program.

This program will assume that there is a variable,, holding a list of numbers. Intuitively, this program should sum all the elements of the list.

1. let lst be a given list of integers 2. let s := 0 initially 3. for element e in lst: 4.  s := s+e 5. return s

Proof of Validity
When we write a computer program, it is often important to prove the validity of the program.

Not every programmer will write a proof of the correctness of every one of her programs. But it is especially important when a computer system controls something very sensitive and important, like hospital systems or financial records.

The program above is valid.

Before the execution of the for-loop,  holds the partial sum of the list   before index. This is simply because the sum of no elements defaults to.

Suppose that, before iteration number i of the for-loop,  holds the partial sum of   before index. Then after iteration i of the loop,  holds the partial sum of the list up to and including index i.

By the assumption,  holds the value , before the ith iteration. (If  we understand this to mean that  .)

After line 4 executes,, which shows that   then holds the partial sum of the list up to and including index.

Because the last iteration occurs at the final index of the list, then when the for-loop terminates,  holds the sum of all the elements in the list.

Since the program then returns, therefore the program returns the sum over.

Admittedly this proof seems very verbose and is probably no clearer than the pseudo-code itself!

However, as programs become more complicated, proofs of validity allow us to summarize and abstract some of the behavior of programs while still arguing for their validity. So as always, this is merely a "toy" example to get us started. As we progress in complexity, the reader should see the value added by performing these proofs.