\documentstyle{article}

\newcommand{\set}[2]{\bigl\{\, #1 \,\big|\, #2 \,\bigr\}}
\begin{document}

\section{Loop Invariants}\hfill 26 August 1993

\begin{enumerate}
\item Control Structures
  \begin{enumerate}
  \item Straight line
  \item Branch:
     {\tt case},
     {\tt if then else}.
  \item Loop: 
     {\tt for},
     {\tt repeat until},
     {\tt while do}.
  \end{enumerate}

\item Loops
  \begin{enumerate}
  \item \verb. ( r S u C ) <==> ( S ; w ~C d S ).
  \item \verb. ( w C d S ) <==> ( if C { r S u ~C }).  
  \item \verb. ( for i=I to F do S ) <==> ( i=I ; w i<=F do {S;i++}).
  \item So \verb.while. can simulate anything, a
    \verb.repeat. $+$ \verb.if. $=$ \verb.while., and \verb.for.
    is incable of inifite regress.
  \item Proof: transformation of flow charts:
  \item break statement, using flags, forcing the termination 
    condition, goto's and returns. {\em Example:} For each thing in a
    list, do something; how to break out on error doing something?
  \end{enumerate}

\item Subroutines and Goto's: How to they fit in? 
  \begin{enumerate}
  \item As straight-line code:
  \begin{quote} {\em The Golden Rule of Subroutines:}\/ Call a
   subroutine when passing from one level of detail to another.
  \end{quote}
  \item As loops: Recursion.
  \item Not at all, goto's and spaghetti code, they just don't fit in,
  and so are abandoned.
  \end{enumerate}

\item Loop Invariants
  \begin{enumerate}
  \item Format: \verb.{assert I} ; while C do { S; assert I };.
  \item Loop Invariant ${\cal I}$, true at assertion.
  \item Termination: advance towards ${\cal C}$ false.
  \item Goal: invariant $+$ termination $=$ goal.
  \item Initialization.
  \end{enumerate}

\samepage{
\item Invariant Example, max integer in array \verb,A[1..n],.
  \begin{enumerate}
  \item Invariant: $M=\max\set{A[j]}{1\le j\le i}$.
  \item Discussion: choice of invariant driven by the fact:
  \begin{quote} {\em There is no such thing as the maximum of a empty set.}
  \end{quote}
  \item Termination: $i=n$, advance $i$ by one each time through the loop.
  \item Goal, $M$ is maximum in array.
  \end{enumerate}
}

\item  Comment on Zeno's Paradox: Let ${\cal I}$ be Achilles' arrow
  is at distance $d$, then consider: \verb. d=0; while d<1 do d=d+(1-d)/2;.
  
\end{enumerate}

\section{Loop Invariants continued}\hfill 31 August 1993

\begin{enumerate}

\item Loop Invariants, another example: Find first negative number
  in an array.
  \begin{enumerate}
  \item Invariant: All numbers in $\set{A[j]}{1\le j< i}$ are positive.
  \item Termination: $i>n$ or $A[i]<0$, increase $i$ by one each 
  time through the loop.
  \item Goal $=$ invariant $+$ termination.
  \item Initialization: $i=1$.
  \item Notes:
    \begin{enumerate}
    \item Choice of invariant driven by the fact that the
    answer could be ``none.''
    \item Lack of short-circuited AND in Pascal.
    \item Sentinel bound: Let $A[n+1]=-1$.
    \end{enumerate}
  \end{enumerate}
  
\item Extended Example: greatest common divisor of two integers.
  \begin{enumerate}
  \item Divisibility: $\forall\, a,b\in{\bf Z},\;( a\,|\,b
    \iff \exists k\in{\bf Z}:ak=b).$
  \newcommand{\cd}{\mbox{cd}}
  \item Common Divisor: $\forall\, a,b\in{\bf Z},\;
    \cd(a,b) = \set{c\in{\bf Z}}{c\,|\,a,b}.$
  \item Greatest Common Divisor: $\forall\, a,b\in{\bf Z},\;
    \gcd(a,b) = \max\cd(a,b).$
    \begin{enumerate}
    \item $\gcd(2,4)=2,\; \gcd(a,ka)=a.$
    \item $\gcd(5,7)=1,\; \gcd(p_1,p_2)=1,$ for two distinct primes.
       In general, an $a,b$ such that $\gcd(a,b)=1$ are 
       called {\em relatively prime.}
    \item $\gcd(1547,560)=7.$
    \end{enumerate}
  \item Notes:
    \begin{enumerate}
    \item $\forall\,a\in{\bf Z},\; 1\,|\,a,$ hence $\cd(a,b)$ is 
       never empty.
    \item $\forall\,a\in{\bf Z},\; a\,|\,0,$ hence $\cd(0,0)={\bf Z}.$
       We shall {\em define}\/ $\gcd(0,0)=0.$
    \item $\forall\,c\in\cd(a,b),\; c\,|\,\gcd(a,b).$
    \end{enumerate}
  \item Euclidean Algorithm: \begin{quote}{\em
    Keep replacing the larger of $a, b$ by their difference, until
    the answer becomes obvious.}\end{quote}
  \item Key lemma: $\forall a, b\in{\bf Z},\; \gcd(a-b,b)=\gcd(a,b).$
    {\em Proof:}\/ Show $\cd(a,b)\subseteq\cd(a-b,b)$ and then
    that $cd(a-b,b)\subseteq\cd(a,b)$.
  \item Invariant: Let $C$ be the gcd we seek. We keep integers $a,b$
    according to the invariant: $C=\gcd(a,b)$ and $a\ge b\ge 0.$
  \item Termination: $b=0$, we advance by decreasing the sum
    $a+b$ each time through the loop.
  \item Goal $=$ invariant $+$ termination: $C=(a,0)=a$.
  \item Initialization: Make invariant true by setting
    $a$ and $b$ to the input values, perhaps
    sign corrected and swaped.
  \end{enumerate}

\end{enumerate}


\section{Text files and strings}\hfill 2 September 1993

\begin{enumerate}

\item Files, general files, streams, text files.
\item Assignment compatibility of files.
\item Text Files: {\em reference page 233--235, Oh! Pascal!}
  \begin{enumerate}
  \item A text file is a sequence of characters including
  special end-of-line and end-of-file characters.
  \item File always ends with eoln eof sequence.
  \item Each open file has a cursor over the next character to read.
  \item Boolean valued function eoln is true if cursor over an eoln;
    eof is ture if cursor over an eof.
  \item What character is at the cursor position of an eoln or eof is
    implementation dependent.
    \begin{enumerate}
    \item In Standard Pascal, eoln is a blank and it is illegal to read
    and eof.
    \item In Turbo Pascal, eoln is the two character sequence crlf
     ( chr(13) chr(10)) and eof is a control-Z (chr(26)).
    \end{enumerate}
  \item Read will not read past an eoln, readln must be called;
    write will not write an eoln, writeln must be called.
  \item Example:
  \begin{verbatim}
      while not eof do begin
         while not eoln do begin
            read(Ch) ; write(ch) ; end ;
         readln ; writeln ; end ;
  \end{verbatim}
  \end{enumerate}

\item Strings. \verb.var s : string [MAXLENGTH]; .: {\em reference page
106, 301--303, Oh! Pascal!}
  \begin{enumerate}
  \item A string is ``almost'' an array of characters.
  \item Strings have maximum lengths and current lengths.
  \item Maximum lenghts do not affect type.
  \item \verb.length(s) : integer. is the string's current length.
  \item \verb.s[i] : char. is the current actual $i$-th character
  of the string, for $1\le i\le $ \verb. length(s).
  \item Other SYSTEM functions on strings are:
    \begin{enumerate}
    \item \verb?Concat(S1,...)->string?.
    \item \verb.Copy(S,Indx,Cnt)->string..
    \item \verb.Insert, Delete, Pos..
    \end{enumerate}
  \end{enumerate}

\item Reading words:
  Case study of the program on pages 241--242 of {\em Oh! Pascal!}.
  \begin{enumerate}
  \item Invariant: Exist \verb.count. completed words to the left of the
  cursor; and \verb.InAWord. is true iff cursor was just over
  a currently incomplete word.
  \item Termination: advance one character towards the end of file.
  \item Goal: assuming eof is after a eoln, count words because we
  completed the last word.
  \item Initialization: no words and we were not over a completed word.
  \item Each time through the loop:
  \begin{enumerate}
  \item Case non-blank: We check that InAWord is true.
  \item Case blank: We check that InAWord false, and if word just ended
  that count is incremented. By the invariant, word just ended if InAWord was
  true last time. That is, whenever InAWord goes from True to
  false.
  \end{enumerate}
  \item Note filter style of the cascade of the two conditionals.
  \end{enumerate}

\item Notes:
  \begin{enumerate}
    \item Students didn't believe all Pascal's had strings.
    \item Some students didn't remember arrays.
    \item Needed to introduce ASCII better.
    \item Some students didn't believe that a text file in Pascal
    needed an eoln before an eof. If not, readln is useless.
  \end{enumerate}
  I should have done it this way:
  \begin{enumerate}
  \item Characters and Ascii.
  \item Arrays and strings.
  \item Text files w/ specific advice for Ultrix, VMS and Turbo.
 \end{enumerate}

\item Streams are not supported in PASCAL!
\end{enumerate}

\section{Pointers}\hfill 7 September 1993

\begin{enumerate}

\item Memory model.  address $\rightarrow$ \framebox{cell}.
\item Cell contains an integer of fixed maximum size, or one symbol
   from a fixed finite set.
\item Address identifies in some way the cell, and are sequenced.
   Can think of the addresses as being integers.
\item Byte: often a cell is a byte.
     Everything is built up from bytes, and certain composites
    of consecutive bytes can be used just as easily as bytes.
\begin{verbatim}
        8 oz   = 1 cup      2 cups  = 1 pint
        8 bits = 1 byte     2 bytes = 1 word
\end{verbatim}
    Also, quart is a longword; a half-gallon is a quad-word.
\item What is a byte? byte $=$ char $=$ ASCII.
\item What is a word? Integers, positive and negative, however:
\begin{verbatim}
         MAXINT + 1 -> (-MAXINT)-1
         2 * MAXINT -> -2
\end{verbatim}
\item What is a quad word? Floating point computations.
\item What is an address?
   A variety of approaches which Pascal trys to make uniform.
\begin{enumerate}
   \item Linear memory: an address is a big integer. {\em Ex.}\/
   MC68000, it is a long-word.
   \item Segmented memory: an address is a pair of integers which
   are not independent. {\em Ex.}\/ Intel 8086, a pair of words
   which are added according to $a*16+b$.
   \item Trade-offs: speed and size in favor of Intel approach,
   ease of programming in favor of 68000 approach.
   \item Pages and Virtual: no fixed correlation between address
   and cells. Hardware negotiates invisibly to programmer.
   {\em Advantages:}\/ Large, sparse memory space, use of fast
   expensive memory for some of the memory space.
\end{enumerate}
\item How to compilers use addresses?
\item Address of variables: {\em Address as a static entity.}
\begin{enumerate}
  \item Maintains a memory map for ``dataland''.
  \item Assigns variables to a fixed address and reserves enough
   consecutive bytes for the variable.
  \item This is why {\em define before use} and {\em type} are
   required.
  \item Name of variable is turned into a use of its address at
   compile time.
\end{enumerate}
\item Arrays and records, {\em Address as objects to calculate with.}
\begin{enumerate}
  \item Arrays: uses the fact that memory addresses can be treated
  as integers. Assigns a {\em base address} and enough bytes
  for the entire array. Inserts code to calculate address of array
  element from base, index, and element size.
  \item Non-zero based and multidimensional arrays.
  \item Records: offset needed because it could be an array of
  records.
\end{enumerate}
\item VAR parameters and {\em Indirect Addressing}. {\em Addresses as things you can store.}
\item[(*)] Recursion and stack based local var.

\item The Heap: a memory space available to the user to dynamically
   create variables while the program is running.
   \begin{enumerate}
   \item Getting memory, memory management. (Assume infinite, no
reuse.)
   \item Returning to the user a {\em pointer} to the memory.
   \item A Pointer: A variable which stores an address.
   \item The type of a pointer includes the type of what it
      points to. This is to make it harder to make mistakes.
   \item There is the pointer, and what the pointer points to.
      There is also the address of where the pointer is stored.
      This last is hidden, the first is simply the pointer's name.
      The second is indirect addressing and uses the dereferencing
      arrow \verb.^..
  \end{enumerate}
\end{enumerate}


\section{Pointers}\hfill 9 September 1993

\begin{enumerate}
\item New, Dispose and Nil.
\item Syntax: \verb?var a : record a: integer ; end ;?
\item Syntax: \verb?var p : ^ integer ;?
\item Syntax: \verb?p^ := 1 ;?
\item Assignment compatibility of pointers. Based on the
  strict compatibility of what they point to.
\item Note: if ``pointer to'' introduced a new type, then
  it would be impossible to have a record include a pointer
  to itself.
\end{enumerate}

\section{Linked Lists}\hfill 14 September 1993

\begin{enumerate}
\item
  A linked lists, the data structure.
\item Adding to ehad, traversing and printing.
\item General insertion and dummy headers.
\item Deletion.
\item Variants: last pointers, circular lists,
  doubly linked lists.
\item DOS file layout and the File Allocation Table.
\end{enumerate}

\section{Boolean Logic}\hfill 16 September 1993

\begin{enumerate}

\item Boolean Algebra
  \begin{enumerate}
  \item Boolean variables receiving boolean valued expressions.
  \item Operations AND, OR and NOT.
  \item Absorptive Laws:
    \begin{enumerate}
    \item $A \wedge ( A \vee B ) = A$.
    \item $A \vee ( A \wedge B ) = B$.
    \item {\em Proof: }\/ Truth table method of proof.
    \end{enumerate}
  \item DeMorgan's Theorem:
    \begin{enumerate}
    \item $\neg ( A \vee B ) = \neg A  \wedge  \neg B$.
    \item $\neg ( A \wedge B ) = \neg A \vee \neg B$.
    \item {\em Proof:}\/ Venn Diagram: a short version of Truth Table.
    Four points and circles enclosing points true for sentence.
    \end{enumerate}
  \item Distributive Laws:
    \begin{enumerate}
    \item $A \vee ( B \wedge C ) = (A \vee B ) \wedge ( A \vee C ).$
    \item $ A \wedge ( B \vee C ) = ( A\wedge B)\vee(A\wedge C).$
    \item {\em Proof:}\/ Venn Diagram: 
    three circles enclosing eight points.
    \end{enumerate}
  \end{enumerate}

\item Examples:
  \begin{quote} Numbers which are odd and positive are exactly
  those which are neither even nor negative.
  \end{quote}
  \begin{enumerate}
  \item $O(n) \wedge n>0 = \neg( E(n) \vee n \le 0 ) =
    \neg( (E(n) \vee n=0)\vee n < 0 ) = \neg( E(n)\vee n<0).$
  \item The complement of the set $1, 3, 5,\ldots$ is the union
   of evens, $0,2,4,\ldots$ and negatives, $-1, -2, -3, \ldots$.
  \end{enumerate}
  \begin{quote} However, the even and positive numbers do not
  include all which are neither odd nor negative, because zero
  should not be not included.\end{quote}

\item Stacks, Sedgewick's example ... didn't work.

\end{enumerate}

\section{Personal Summary}

Pointers: best perhaps to keep to a cell and pointer model, where
the cell is an array and can have certain addressing calculations.
That is, it is important to see the cell as cut out of a linear
space, but it is easier to draw as a cell.

Begin with a variable $i$. It has a name, a place in memory, and
this place has an address. The name and the address are one. They
are written beside a cell representing the memory location.
Using a variable depends on whether it appears at the left or
right of an assignment. Its use could be a retrieve at the
location or a store to the location. The value of a variable
is implicitely the retrieve from the location.

An array is a bunch of places starting at a location. The name
could be synonymous with the address of the first cell.
The data is placed into the cell in order so that arithmetic
gives you direct access.
Records and arrays of records are similar. 

Indirect addressing is when you store the location in a
variable. The variable is called a pointer.

\end{document}
