\documentstyle[makeidx]{article}
\makeindex
%\setlength{\oddsidemargin}{.5in}
%\setlength{\evensidemargin}{.5in}
%\setlength{\textwidth}{5.8in}
\begin{document}
%\setlength{\parindent}{0pt}
%\newcounter{bean}
%\begin{list}{\arabic{bean}.}{\usecounter{bean}\setlength{\leftmargin}{0pt}\setlength{\rightmargin}{0pt}\setlength{\listparindent}{20pt}\setlength{\parsep}{5pt}}
%\item[]

\begin{verbatim}#|

 Copyright (C) 1994 by Computational Logic, Inc.  All Rights Reserved.

 This script is hereby placed in the public domain, and therefore unlimited
 editing and redistribution is permitted.

 NO WARRANTY

 Computational Logic, Inc. PROVIDES ABSOLUTELY NO WARRANTY.  THE EVENT SCRIPT
 IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF MERCHANTABILITY AND
 FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND
 PERFORMANCE OF THE SCRIPT IS WITH YOU.  SHOULD THE SCRIPT PROVE DEFECTIVE, YOU
 ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.

 IN NO EVENT WILL Computational Logic, Inc. BE LIABLE TO YOU FOR ANY DAMAGES,
 ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL
 DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THIS SCRIPT (INCLUDING BUT
 NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES
 SUSTAINED BY THIRD PARTIES), EVEN IF YOU HAVE ADVISED US OF THE POSSIBILITY OF
 SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.

|#

; An NQTHM Formalization of a Small Machine

; by J Strother Moore II
; May 30, 1991

; This file serves as a good introduction to the Nqthm approach to
; language semantics.  We have carried out this approach on much larger
; examples than presented here.  It is, for example, that used at all
; levels of the CLI short stack (hardware description language, machine
; language, assembly language, high-level language).  The semantics of those
; levels are so large and complicated that it is difficult to see the
; basic ideas.  Those ideas are highlighted here simply by dealing with
; a trivial language.

; This is a list of events to be processed by NQTHM starting from the
; GROUND-ZERO state.  In it I develop
; (a) an operational semantics for a simple programming language
; (b) a program that implements multiplication by repeated addition
; (c) a proof of the correctness of the multiplication program
;     directly from the "operational" semantics
; (d) a program that does exponentiation and uses the multiplier
; (e) a proof of the correctness of the exponentiation program
; (f) the most general correctness theorem about the multiplier
; (g) the definition and correctness of the "McCarthy" functional
;     semantics of the multiplier
; (h) a proof of the correctness of the multiplier by the inductive
;     assertion method.
; (i) May 13, 1992.  a proof of the general theorem that our
;     standard form of a correctness result for a subroutine 
;     implies our standard form of a termination result.  This
;     part of the file is not part of the tutorial because the
;     proof is pretty messy.

; The programming language is not particularly elegant.  Its only
; redeeming features are that its semantics is easily written down and
; it lets me illustrate the points I'm trying to make.  This is by no
; means a complete or exemplary "library" for dealing with programs in
; this language; I have in fact kept the facts to a bare minimum.

; We start by defining our "small machine."

\end{verbatim}
\noindent{\sc Event}:   Start with the initial {\bf nqthm} theory.

\addvspace{18pt}

\begin{verbatim}; States are represented by the following shell objects:

\end{verbatim}
\noindent{\sc Event}:   Add the shell {\it{st\/}}\index{st}, with recognizer function symbol {\it{stp\/}} and 5 accessors: {\it{pc\/}}, with type restriction (none-of) and default value zero; {\it{stk\/}}, with type restriction (none-of) and default value zero; {\it{mem\/}}, with type restriction (none-of) and default value zero; {\it{haltedp\/}}, with type restriction (none-of) and default value zero; {\it{defs\/}}, with type restriction (none-of) and default value zero.

\addvspace{18pt}

\begin{verbatim}; Utility Functions

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{add1-pc}$\;\;$
{\rm{add1-pc}}\,({\it{pc\/}})\index{add1-pc} $=$ {\rm{cons}}\,({\rm{car}}\,({\it{pc\/}}), $1\;+$ {\rm{cdr}}\,({\it{pc\/}}))
\end{tabbing}

\begin{tabbing}{\sc Definition}:\index{get} \\  
\=\+{\rm{get}}\,({\it{n\/}}, {\it{lst\/}})\index{get} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\it{n\/}} $\simeq {\tt{0}}$$\;\;${\bf then }{\rm{car}}\,({\it{lst\/}}) \\ 
{\bf else }{\rm{get}}\,({\it{n\/}} $-\;1$, {\rm{cdr}}\,({\it{lst\/}}))\index{get}$\;${\bf  endif}\-\-
\end{tabbing}

\begin{tabbing}{\sc Definition}:\index{put} \\  
\=\+{\rm{put}}\,({\it{n\/}}, {\it{v\/}}, {\it{lst\/}})\index{put} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\it{n\/}} $\simeq {\tt{0}}$$\;\;${\bf then }{\rm{cons}}\,({\it{v\/}}, {\rm{cdr}}\,({\it{lst\/}})) \\ 
{\bf else }{\rm{cons}}\,({\rm{car}}\,({\it{lst\/}}), {\rm{put}}\,({\it{n\/}} $-\;1$, {\it{v\/}}, {\rm{cdr}}\,({\it{lst\/}}))\index{put})$\;${\bf  endif}\-\-
\end{tabbing}

\begin{tabbing}{\sc Definition}:\index{fetch} \\  
{\rm{fetch}}\,({\it{pc\/}}, {\it{defs\/}})\index{fetch} $=$ {\rm{get}}\,({\rm{cdr}}\,({\it{pc\/}}), {\rm{cdr}}\,({\rm{assoc}}\,({\rm{car}}\,({\it{pc\/}}), {\it{defs\/}})))\index{get}
\end{tabbing}

\begin{verbatim}; The Semantics of Individual Instructions

; Move Instructions

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{move} \\  
\=\+{\rm{move}}\,({\it{addr1\/}}, {\it{addr2\/}}, {\it{s\/}})\index{move} \\ 
$=$$\;\;\;\;${\rm{st}}\,(\=\+{\rm{add1-pc}}\,({\rm{pc}}\,({\it{s\/}})\index{pc})\index{add1-pc}, \\ 
{\rm{stk}}\,({\it{s\/}})\index{stk}, \\ 
{\rm{put}}\,({\it{addr1\/}}, {\rm{get}}\,({\it{addr2\/}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{get}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{put}, \\ 
{\bf{f}}, \\ 
{\rm{defs}}\,({\it{s\/}})\index{defs})\-\index{st}\-
\end{tabbing}

\begin{tabbing}{\sc Definition}:\index{movi} \\  
\=\+{\rm{movi}}\,({\it{addr\/}}, {\it{val\/}}, {\it{s\/}})\index{movi} \\ 
$=$$\;\;\;\;${\rm{st}}\,({\rm{add1-pc}}\,({\rm{pc}}\,({\it{s\/}})\index{pc})\index{add1-pc}, {\rm{stk}}\,({\it{s\/}})\index{stk}, {\rm{put}}\,({\it{addr\/}}, {\it{val\/}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{put}, {\bf{f}}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{st}\-
\end{tabbing}

\begin{verbatim}; Arithmetic Instructions

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{add} \\  
\=\+{\rm{add}}\,({\it{addr1\/}}, {\it{addr2\/}}, {\it{s\/}})\index{add} \\ 
$=$$\;\;\;\;${\rm{st}}\,(\=\+{\rm{add1-pc}}\,({\rm{pc}}\,({\it{s\/}})\index{pc})\index{add1-pc}, \\ 
{\rm{stk}}\,({\it{s\/}})\index{stk}, \\ 
{\rm{put}}\,({\it{addr1\/}}, {\rm{get}}\,({\it{addr1\/}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{get} $+$ {\rm{get}}\,({\it{addr2\/}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{get}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{put}, \\ 
{\bf{f}}, \\ 
{\rm{defs}}\,({\it{s\/}})\index{defs})\-\index{st}\-
\end{tabbing}

\begin{tabbing}{\sc Definition}:\index{subi} \\  
\=\+{\rm{subi}}\,({\it{addr\/}}, {\it{val\/}}, {\it{s\/}})\index{subi} \\ 
$=$$\;\;\;\;${\rm{st}}\,(\=\+{\rm{add1-pc}}\,({\rm{pc}}\,({\it{s\/}})\index{pc})\index{add1-pc}, \\ 
{\rm{stk}}\,({\it{s\/}})\index{stk}, \\ 
{\rm{put}}\,({\it{addr\/}}, {\rm{get}}\,({\it{addr\/}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{get} $-$ {\it{val\/}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{put}, \\ 
{\bf{f}}, \\ 
{\rm{defs}}\,({\it{s\/}})\index{defs})\-\index{st}\-
\end{tabbing}

\begin{verbatim}; Jump Instructions

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{jumpz} \\  
\=\+{\rm{jumpz}}\,({\it{addr\/}}, {\it{pc\/}}, {\it{s\/}})\index{jumpz} \\ 
$=$$\;\;\;\;${\rm{st}}\,(\=\+\=\+{\bf if }{\rm{get}}\,({\it{addr\/}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{get} $\simeq {\tt{0}}$$\;\;${\bf then }{\rm{cons}}\,({\rm{car}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}), {\it{pc\/}}) \\ 
{\bf else }{\rm{add1-pc}}\,({\rm{pc}}\,({\it{s\/}})\index{pc})\index{add1-pc}$\;${\bf  endif}\-, \\ 
{\rm{stk}}\,({\it{s\/}})\index{stk}, \\ 
{\rm{mem}}\,({\it{s\/}})\index{mem}, \\ 
{\bf{f}}, \\ 
{\rm{defs}}\,({\it{s\/}})\index{defs})\-\index{st}\-
\end{tabbing}

\begin{tabbing}{\sc Definition}:\index{jump} \\  
{\rm{jump}}\,({\it{pc\/}}, {\it{s\/}})\index{jump} $=$ {\rm{st}}\,({\rm{cons}}\,({\rm{car}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}), {\it{pc\/}}), {\rm{stk}}\,({\it{s\/}})\index{stk}, {\rm{mem}}\,({\it{s\/}})\index{mem}, {\bf{f}}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{st}
\end{tabbing}

\begin{verbatim}; Subroutine Call and Return

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{call} \\  
\=\+{\rm{call}}\,({\it{subr\/}}, {\it{s\/}})\index{call} \\ 
$=$$\;\;\;\;${\rm{st}}\,({\rm{cons}}\,({\it{subr\/}}, {\tt{0}}), {\rm{cons}}\,({\rm{add1-pc}}\,({\rm{pc}}\,({\it{s\/}})\index{pc})\index{add1-pc}, {\rm{stk}}\,({\it{s\/}})\index{stk}), {\rm{mem}}\,({\it{s\/}})\index{mem}, {\bf{f}}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{st}\-
\end{tabbing}

\begin{tabbing}{\sc Definition}:\index{ret} \\  
\=\+{\rm{ret}}\,({\it{s\/}})\index{ret} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\rm{stk}}\,({\it{s\/}})\index{stk} $\simeq\;${{\bf{nil}}}${}$$\;\;${\bf then }{\rm{st}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{stk}}\,({\it{s\/}})\index{stk}, {\rm{mem}}\,({\it{s\/}})\index{mem}, {\bf{t}}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{st} \\ 
{\bf else }{\rm{st}}\,({\rm{car}}\,({\rm{stk}}\,({\it{s\/}})\index{stk}), {\rm{cdr}}\,({\rm{stk}}\,({\it{s\/}})\index{stk}), {\rm{mem}}\,({\it{s\/}})\index{mem}, {\bf{f}}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{st}$\;${\bf  endif}\-\-
\end{tabbing}

\begin{verbatim}; One can imagine adding new instructions.

; The Interpreter

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{execute} \\  
\=\+{\rm{execute}}\,({\it{ins\/}}, {\it{s\/}})\index{execute} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\rm{car}}\,({\it{ins\/}}) $=$ {\tt{'}}{\tt{move}}$\;\;${\bf then }{\rm{move}}\,({\rm{cadr}}\,({\it{ins\/}}), {\rm{caddr}}\,({\it{ins\/}}), {\it{s\/}})\index{move} \\ 
{\bf elseif }{\rm{car}}\,({\it{ins\/}}) $=$ {\tt{'}}{\tt{movi}}$\;\;${\bf then }{\rm{movi}}\,({\rm{cadr}}\,({\it{ins\/}}), {\rm{caddr}}\,({\it{ins\/}}), {\it{s\/}})\index{movi} \\ 
{\bf elseif }{\rm{car}}\,({\it{ins\/}}) $=$ {\tt{'}}{\tt{add}}$\;\;${\bf then }{\rm{add}}\,({\rm{cadr}}\,({\it{ins\/}}), {\rm{caddr}}\,({\it{ins\/}}), {\it{s\/}})\index{add} \\ 
{\bf elseif }{\rm{car}}\,({\it{ins\/}}) $=$ {\tt{'}}{\tt{subi}}$\;\;${\bf then }{\rm{subi}}\,({\rm{cadr}}\,({\it{ins\/}}), {\rm{caddr}}\,({\it{ins\/}}), {\it{s\/}})\index{subi} \\ 
{\bf elseif }{\rm{car}}\,({\it{ins\/}}) $=$ {\tt{'}}{\tt{jumpz}}$\;\;${\bf then }{\rm{jumpz}}\,({\rm{cadr}}\,({\it{ins\/}}), {\rm{caddr}}\,({\it{ins\/}}), {\it{s\/}})\index{jumpz} \\ 
{\bf elseif }{\rm{car}}\,({\it{ins\/}}) $=$ {\tt{'}}{\tt{jump}}$\;\;${\bf then }{\rm{jump}}\,({\rm{cadr}}\,({\it{ins\/}}), {\it{s\/}})\index{jump} \\ 
{\bf elseif }{\rm{car}}\,({\it{ins\/}}) $=$ {\tt{'}}{\tt{call}}$\;\;${\bf then }{\rm{call}}\,({\rm{cadr}}\,({\it{ins\/}}), {\it{s\/}})\index{call} \\ 
{\bf elseif }{\rm{car}}\,({\it{ins\/}}) $=$ {\tt{'}}{\tt{ret}}$\;\;${\bf then }{\rm{ret}}\,({\it{s\/}})\index{ret} \\ 
{\bf else }{\it{s\/}}$\;${\bf  endif}\-\-
\end{tabbing}

\begin{tabbing}{\sc Definition}:\index{step} \\  
\=\+{\rm{step}}\,({\it{s\/}})\index{step} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\rm{haltedp}}\,({\it{s\/}})\index{haltedp}$\;\;${\bf then }{\it{s\/}} \\ 
{\bf else }{\rm{execute}}\,({\rm{fetch}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch}, {\it{s\/}})\index{execute}$\;${\bf  endif}\-\-
\end{tabbing}

\begin{tabbing}{\sc Definition}:\index{sm} \\  
\=\+{\rm{sm}}\,({\it{s\/}}, {\it{n\/}})\index{sm} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\it{n\/}} $\simeq {\tt{0}}$$\;\;${\bf then }{\it{s\/}} \\ 
{\bf else }{\rm{sm}}\,({\rm{step}}\,({\it{s\/}})\index{step}, {\it{n\/}} $-\;1$)\index{sm}$\;${\bf  endif}\-\-
\end{tabbing}

\begin{verbatim}; This concludes our formal definition of the intepreter.

; We next prove a small collection of lemmas that tightly control the
; expansion of the the interpreter.  The idea is that we don't want sm
; or step to expand unless we know what the current instruction is and
; have enough time on the clock to execute it.  So we will prove
; certain rewrite rules that manipulate step and sm and then disable
; those functions so that only the rules are available.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  step-opener \\ \index{step-opener}
\=\+({\rm{haltedp}}\,({\it{s\/}})\index{haltedp} $\rightarrow$ ({\rm{step}}\,({\it{s\/}})\index{step} $=$ {\it{s\/}})) \\ 
$\wedge$$\;\;\;\;$(\=\+{\rm{listp}}\,({\rm{fetch}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch}) \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{step}}\,({\it{s\/}})\index{step} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\rm{haltedp}}\,({\it{s\/}})\index{haltedp}$\;\;${\bf then }{\it{s\/}} \\ 
{\bf else }{\rm{execute}}\,({\rm{fetch}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch}, {\it{s\/}})\index{execute}$\;${\bf  endif}\-)\-)\-\-
\end{tabbing}


\noindent{\sc Event}:   Disable step.

\addvspace{18pt}

\begin{tabbing}{\sc Theorem}:  sm-plus \\ \index{sm-plus}
{\rm{sm}}\,({\it{s\/}}, {\it{i\/}} $+$ {\it{j\/}})\index{sm} $=$ {\rm{sm}}\,({\rm{sm}}\,({\it{s\/}}, {\it{i\/}})\index{sm}, {\it{j\/}})\index{sm}
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  sm-add1 \\ \index{sm-add1}
{\rm{sm}}\,({\it{s\/}}, $1\;+$ {\it{i\/}})\index{sm} $=$ {\rm{sm}}\,({\rm{step}}\,({\it{s\/}})\index{step}, {\it{i\/}})\index{sm}
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  sm-0 \\ \index{sm-0}
{\rm{sm}}\,({\it{s\/}}, {\tt{0}})\index{sm} $=$ {\it{s\/}}
\end{tabbing}


\noindent{\sc Event}:   Disable sm.

\addvspace{18pt}

\begin{verbatim}; Now we move to our first example program.  We will define a program
; that multiplies two naturals by successive addition.  We will then
; prove it correct.

; The program we have in mind is:

; (times (movi 2 0)
;        (jumpz 0 5)
;        (add 2 1)
;        (subi 0 1)
;        (jump 1)
;        (ret))

; Observe that the program multiplies the contents of reg 0 by the
; contents of reg 1 and leaves the result in reg 2.  At the end, reg 0
; is 0 and reg 1 is unchanged.  If we start at a (call times) this
; program requires 2+4i+2 instructions, where i is the initial
; contents of reg 0.  To keep the proof incredibly simple, we will
; prove the program correct only for the 5 register version of our
; machine!  (Why 5?  Why not 3?  Because eventually we will use times
; in another program that uses 5 registers.  In general we should
; prove it for an arbitrarily large memory -- and we will -- but that
; just complicates the statement without contributing to the example.)

; We start by defining the constant that is this program:

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{times-program} \\  
\=\+{\sc{times-program}}\index{times-program} \\ 
$=$$\;\;\;\;${\tt{'}}{\tt{(}}\=\+{\tt{times}} \\ 
{\tt{(}}{\tt{movi}}{\tt{ }}{\tt{2}}{\tt{ }}{\tt{0}}{\tt{)}} \\ 
{\tt{(}}{\tt{jumpz}}{\tt{ }}{\tt{0}}{\tt{ }}{\tt{5}}{\tt{)}} \\ 
{\tt{(}}{\tt{add}}{\tt{ }}{\tt{2}}{\tt{ }}{\tt{1}}{\tt{)}} \\ 
{\tt{(}}{\tt{subi}}{\tt{ }}{\tt{0}}{\tt{ }}{\tt{1}}{\tt{)}} \\ 
{\tt{(}}{\tt{jump}}{\tt{ }}{\tt{1}}{\tt{)}} \\ 
{\tt{(}}{\tt{ret}}{\tt{)}}{\tt{)}}\-\-
\end{tabbing}    \begin{verbatim}; 5  Return

; and a function that multiplies the "same way."

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{times-fn} \\  
\=\+{\rm{times-fn}}\,({\it{i\/}}, {\it{j\/}}, {\it{ans\/}})\index{times-fn} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\it{i\/}} $\simeq {\tt{0}}$$\;\;${\bf then }{\it{ans\/}} \\ 
{\bf else }{\rm{times-fn}}\,({\it{i\/}} $-\;1$, {\it{j\/}}, {\it{ans\/}} $+$ {\it{j\/}})\index{times-fn}$\;${\bf  endif}\-\-
\end{tabbing}

\begin{verbatim}; In some sense, the following mathematical fact completely captures
; the correctness of the program:

\end{verbatim}\begin{tabbing}{\sc Theorem}:  times-fn-is-times \\ \index{times-fn-is-times}
({\it{ans\/}} $\in\;${\bf{N}}${}$) $\rightarrow$ ({\rm{times-fn}}\,({\it{i\/}}, {\it{j\/}}, {\it{ans\/}})\index{times-fn} $=$ (({\it{i\/}} $*$ {\it{j\/}}) $+$ {\it{ans\/}}))
\end{tabbing}

\begin{verbatim}; at least if one also understands

\end{verbatim}\begin{tabbing}{\sc Theorem}:  plus-right-id \\ \index{plus-right-id}
({\it{x\/}} $+$ {\tt{0}}) $=$ {\rm{fix}}\,({\it{x\/}})
\end{tabbing}


\begin{verbatim}; The real problem is proving that the program has this semantics.
; First, how much time does the program need?  It takes one tick to do
; the CALL, one for the MOVI at pc 0, then 4 ticks for each iteration
; of the loop at pc 1, and then 2 more ticks to get out of the loop
; and do the RET.  So:

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{times-clock}$\;\;$
{\rm{times-clock}}\,({\it{i\/}})\index{times-clock} $=$ ({\tt{2}} $+$ ({\it{i\/}} $*$ {\tt{4}}) $+$ {\tt{2}})
\end{tabbing}

\begin{verbatim}; We could have written (plus 4 (times i 4)) but by using this
; algebraically odd expression we make sm-plus, above, immediately
; applicable.

; We next address ourselves to the loop from pc 1 through 4.  Consider
; an arbitary arrival at pc 1 and suppose you have (times i 4) ticks.
; The following theorem tells us what you get:

\end{verbatim}\begin{tabbing}{\sc Theorem}:  times-correct-lemma \\ \index{times-correct-lemma}
\=\+(({\it{i\/}} $\in\;${\bf{N}}${}$) $\wedge$ ({\rm{assoc}}\,({\tt{'}}{\tt{times}}, {\it{defs\/}}) $=$ {\sc{times-program}}\index{times-program})) \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{sm}}\,({\rm{st}}\,({\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{1}}{\tt{)}}, {\it{stk1\/}}, {\rm{list}}\,({\it{i\/}}, {\it{j\/}}, {\it{ans\/}}, {\it{r3\/}}, {\it{r4\/}}), {\bf{f}}, {\it{defs\/}})\index{st}, {\it{i\/}} $*$ {\tt{4}})\index{sm} \\ 
$=$$\;\;\;\;${\rm{st}}\,(\=\+{\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{1}}{\tt{)}}, \\ 
{\it{stk1\/}}, \\ 
{\rm{list}}\,({\tt{0}}, {\it{j\/}}, {\rm{times-fn}}\,({\it{i\/}}, {\it{j\/}}, {\it{ans\/}})\index{times-fn}, {\it{r3\/}}, {\it{r4\/}}), \\ 
{\bf{f}}, \\ 
{\it{defs\/}})\-\index{st})\-\-
\end{tabbing}

\begin{verbatim}; It is then trivial to construct the entire correctness proof:

\end{verbatim}\begin{tabbing}{\sc Theorem}:  times-correct \\ \index{times-correct}
\=\+(\=\+({\rm{fetch}}\,({\it{pc\/}}, {\it{defs\/}})\index{fetch} $=$ {\tt{'}}{\tt{(}}{\tt{call}}{\tt{ }}{\tt{times}}{\tt{)}}) \\ 
$\wedge$$\;\;\;\;$({\rm{assoc}}\,({\tt{'}}{\tt{times}}, {\it{defs\/}}) $=$ {\sc{times-program}}\index{times-program}) \\ 
$\wedge$$\;\;\;\;$({\it{i\/}} $\in\;${\bf{N}}${}$))\- \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{sm}}\,({\rm{st}}\,({\it{pc\/}}, {\it{stk\/}}, {\rm{list}}\,({\it{i\/}}, {\it{j\/}}, {\it{r2\/}}, {\it{r3\/}}, {\it{r4\/}}), {\bf{f}}, {\it{defs\/}})\index{st}, {\rm{times-clock}}\,({\it{i\/}})\index{times-clock})\index{sm} \\ 
$=$$\;\;\;\;${\rm{st}}\,({\rm{add1-pc}}\,({\it{pc\/}})\index{add1-pc}, {\it{stk\/}}, {\rm{list}}\,({\tt{0}}, {\it{j\/}}, {\it{i\/}} $*$ {\it{j\/}}, {\it{r3\/}}, {\it{r4\/}}), {\bf{f}}, {\it{defs\/}})\index{st})\-\-
\end{tabbing}
                
\begin{verbatim}; We disable the clock function so that subsequent programs can
; use it without its expansion messing up their algebraic
; form.

\end{verbatim}
\noindent{\sc Event}:   Disable times-clock.

\addvspace{18pt}

\begin{verbatim}; It is worth noting that this file has been rather carefully crafted
; to make the above proof go through with a minimum of fuss.  In
; general, we will have to prove lots of lemmas about the utility
; functions GET and PUT to handle arbitrarily sized memories.  And we
; have to prove lots of lemmas about arithmetic to explain the data
; handling in our programs.  To some extent those arithmetic facts get
; in the way of our desired treatment of the clock in our proofs,
; e.g., if the theorem prover knows the usual facts about PLUS and
; TIMES then (PLUS 2 (TIMES I 4) 2) would become (PLUS 4 (TIMES 4 I))
; and we'd then have to take special care to force sm to open the way
; we want in this proof.  One avenue that has been used to avoid this
; problem is to define the clock functions with special arithmetic
; primitives, e.g., CLK-PLUS and CLK-TIMES (which are in fact just the
; familiar functions) but which we then disable and isolate from the
; free-wheeling arithmetic simplifications.

; We now consider the role of subroutine call and return in this
; language.  To illustrate it we'll implement exponentiation, which
; will CALL our TIMES program.  The proof of the correctness of the
; exponentiation program will rely on the correctness of TIMES, not on
; re-analysis of the code for TIMES.

; The mathematical function we wish to implement is:

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{exp} \\  
\=\+{\rm{exp}}\,({\it{i\/}}, {\it{j\/}})\index{exp} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\it{j\/}} $\simeq {\tt{0}}$$\;\;${\bf then }{\tt{1}} \\ 
{\bf else }{\rm{exp}}\,({\it{i\/}}, {\it{j\/}} $-\;1$)\index{exp} $*$ {\it{i\/}}$\;${\bf  endif}\-\-
\end{tabbing}

\begin{verbatim}; The program we have in mind is:

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{exp-program} \\  
\=\+{\sc{exp-program}}\index{exp-program} \\ 
$=$$\;\;\;\;${\tt{'}}{\tt{(}}\=\+{\tt{exp}} \\ 
{\tt{(}}{\tt{move}}{\tt{ }}{\tt{3}}{\tt{ }}{\tt{0}}{\tt{)}} \\ 
{\tt{(}}{\tt{move}}{\tt{ }}{\tt{4}}{\tt{ }}{\tt{1}}{\tt{)}} \\ 
{\tt{(}}{\tt{movi}}{\tt{ }}{\tt{1}}{\tt{ }}{\tt{1}}{\tt{)}} \\ 
{\tt{(}}{\tt{jumpz}}{\tt{ }}{\tt{4}}{\tt{ }}{\tt{9}}{\tt{)}} \\ 
{\tt{(}}{\tt{move}}{\tt{ }}{\tt{0}}{\tt{ }}{\tt{3}}{\tt{)}} \\ 
{\tt{(}}{\tt{call}}{\tt{ }}{\tt{times}}{\tt{)}} \\ 
{\tt{(}}{\tt{move}}{\tt{ }}{\tt{1}}{\tt{ }}{\tt{2}}{\tt{)}} \\ 
{\tt{(}}{\tt{subi}}{\tt{ }}{\tt{4}}{\tt{ }}{\tt{1}}{\tt{)}} \\ 
{\tt{(}}{\tt{jump}}{\tt{ }}{\tt{3}}{\tt{)}} \\ 
{\tt{(}}{\tt{ret}}{\tt{)}}{\tt{)}}\-\-
\end{tabbing}      \begin{verbatim}; 9  return

; A recursive description of the loop (pc 3 through 8) in this
; algorithm is:

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{exp-fn} \\  
\=\+{\rm{exp-fn}}\,({\it{r0\/}}, {\it{r1\/}}, {\it{r2\/}}, {\it{r3\/}}, {\it{r4\/}})\index{exp-fn} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\it{r4\/}} $\simeq {\tt{0}}$$\;\;${\bf then }{\it{r1\/}} \\ 
{\bf else }{\rm{exp-fn}}\,({\tt{0}}, {\it{r3\/}} $*$ {\it{r1\/}}, {\it{r3\/}} $*$ {\it{r1\/}}, {\it{r3\/}}, {\it{r4\/}} $-\;1$)\index{exp-fn}$\;${\bf  endif}\-\-
\end{tabbing}

\begin{verbatim}; Pretty weird.

; We need a little more arithmetic than we have, namely
; associativity and right identity for times:

\end{verbatim}\begin{tabbing}{\sc Theorem}:  associativity-of-times \\ \index{associativity-of-times}
(({\it{i\/}} $*$ {\it{j\/}}) $*$ {\it{k\/}}) $=$ ({\it{i\/}} $*$ ({\it{j\/}} $*$ {\it{k\/}}))
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  times-right-id \\ \index{times-right-id}
({\it{i\/}} $*$ {\tt{1}}) $=$ {\rm{fix}}\,({\it{i\/}})
\end{tabbing}

\begin{verbatim}; So now the system can prove that the weird exp-fn is just exp (in a
; generalized sense that accomodates the initial value of r1).

\end{verbatim}\begin{tabbing}{\sc Theorem}:  exp-fn-is-exp \\ \index{exp-fn-is-exp}
({\it{r1\/}} $\in\;${\bf{N}}${}$) $\rightarrow$ ({\rm{exp-fn}}\,({\it{r0\/}}, {\it{r1\/}}, {\it{r2\/}}, {\it{r3\/}}, {\it{r4\/}})\index{exp-fn} $=$ ({\rm{exp}}\,({\it{r3\/}}, {\it{r4\/}})\index{exp} $*$ {\it{r1\/}}))
\end{tabbing}

\begin{verbatim}; Here is the clock function for exp.  Again we use an algebraically
; odd form simply to gain instant access to the desired sm-plus
; decomposition.  The "4" gets us past the CALL and the first 3
; initialization instructions; the times expression takes us around
; the exp loop j times, and the final "2" gets us out through the RET.
; Note that as we go around the loop we make explicit reference to
; TIMES-CLOCK to explain the CALL of TIMES.

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{exp-clock} \\  
{\rm{exp-clock}}\,({\it{i\/}}, {\it{j\/}})\index{exp-clock} $=$ ({\tt{4}} $+$ ({\it{j\/}} $*$ ({\tt{2}} $+$ {\rm{times-clock}}\,({\it{i\/}})\index{times-clock} $+$ {\tt{3}})) $+$ {\tt{2}})
\end{tabbing}

\begin{verbatim}; Now we prove the "loop invariant" for the EXP program.  We simply
; tell the system to induct according to exp-fn.  We could "trick" it
; into doing that by using exp-fn in place of the (times (exp r3 r4)
; r1) expressions, but that is devious and doesn't always work.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  exp-correct-lemma \\ \index{exp-correct-lemma}
\=\+(\=\+({\it{r3\/}} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$({\it{r4\/}} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$({\rm{assoc}}\,({\tt{'}}{\tt{exp}}, {\it{defs\/}}) $=$ {\sc{exp-program}}\index{exp-program}) \\ 
$\wedge$$\;\;\;\;$({\rm{assoc}}\,({\tt{'}}{\tt{times}}, {\it{defs\/}}) $=$ {\sc{times-program}}\index{times-program}))\- \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{sm}}\,(\=\+{\rm{st}}\,({\tt{'}}{\tt{(}}{\tt{exp}}{\tt{ .\ }}{\tt{3}}{\tt{)}}, {\it{stk\/}}, {\rm{list}}\,({\it{r0\/}}, {\it{r1\/}}, {\it{r2\/}}, {\it{r3\/}}, {\it{r4\/}}), {\bf{f}}, {\it{defs\/}})\index{st}, \\ 
{\it{r4\/}} $*$ ({\tt{2}} $+$ {\rm{times-clock}}\,({\it{r3\/}})\index{times-clock} $+$ {\tt{3}}))\-\index{sm} \\ 
$=$$\;\;\;\;${\rm{st}}\,(\=\+{\tt{'}}{\tt{(}}{\tt{exp}}{\tt{ .\ }}{\tt{3}}{\tt{)}}, \\ 
{\it{stk\/}}, \\ 
\=\+{\bf if }{\it{r4\/}} $\simeq {\tt{0}}$$\;\;${\bf then }{\rm{list}}\,({\it{r0\/}}, {\it{r1\/}}, {\it{r2\/}}, {\it{r3\/}}, {\it{r4\/}}) \\ 
{\bf else }{\rm{list}}\,(\=\+{\tt{0}}, \\ 
{\rm{exp}}\,({\it{r3\/}}, {\it{r4\/}})\index{exp} $*$ {\it{r1\/}}, \\ 
{\rm{exp}}\,({\it{r3\/}}, {\it{r4\/}})\index{exp} $*$ {\it{r1\/}}, \\ 
{\it{r3\/}}, \\ 
{\tt{0}})\-$\;${\bf  endif}\-, \\ 
{\bf{f}}, \\ 
{\it{defs\/}})\-\index{st})\-\-
\end{tabbing}

\begin{verbatim}; The theorem prover is now set up to prove that exp is correct
; without further assistance.  (But you must not underestimate how
; clever this assistance has been to make this possible!)

\end{verbatim}\begin{tabbing}{\sc Theorem}:  exp-correct \\ \index{exp-correct}
\=\+(\=\+({\it{i\/}} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$({\it{j\/}} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$({\rm{fetch}}\,({\it{pc\/}}, {\it{defs\/}})\index{fetch} $=$ {\tt{'}}{\tt{(}}{\tt{call}}{\tt{ }}{\tt{exp}}{\tt{)}}) \\ 
$\wedge$$\;\;\;\;$({\rm{assoc}}\,({\tt{'}}{\tt{exp}}, {\it{defs\/}}) $=$ {\sc{exp-program}}\index{exp-program}) \\ 
$\wedge$$\;\;\;\;$({\rm{assoc}}\,({\tt{'}}{\tt{times}}, {\it{defs\/}}) $=$ {\sc{times-program}}\index{times-program}))\- \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{sm}}\,({\rm{st}}\,({\it{pc\/}}, {\it{stk\/}}, {\rm{list}}\,({\it{i\/}}, {\it{j\/}}, {\it{r2\/}}, {\it{r3\/}}, {\it{r4\/}}), {\bf{f}}, {\it{defs\/}})\index{st}, {\rm{exp-clock}}\,({\it{i\/}}, {\it{j\/}})\index{exp-clock})\index{sm} \\ 
$=$$\;\;\;\;${\rm{st}}\,(\=\+{\rm{add1-pc}}\,({\it{pc\/}})\index{add1-pc}, \\ 
{\it{stk\/}}, \\ 
\=\+{\bf if }{\it{j\/}} $\simeq {\tt{0}}$$\;\;${\bf then }{\rm{list}}\,({\it{i\/}}, {\rm{exp}}\,({\it{i\/}}, {\it{j\/}})\index{exp}, {\it{r2\/}}, {\it{i\/}}, {\tt{0}}) \\ 
{\bf else }{\rm{list}}\,({\tt{0}}, {\rm{exp}}\,({\it{i\/}}, {\it{j\/}})\index{exp}, {\rm{exp}}\,({\it{i\/}}, {\it{j\/}})\index{exp}, {\it{i\/}}, {\tt{0}})$\;${\bf  endif}\-, \\ 
{\bf{f}}, \\ 
{\it{defs\/}})\-\index{st})\-\-
\end{tabbing}

\begin{verbatim}; Ok, enough of this.  Presumably the point has been made:  correctness
; proofs can be "stacked."

; Recall that we have been dealing with an unnecessarily restricted
; view of the machine, namely that it only have 5 memory locations.
; Before leaving this approach and pursuing some others, let us
; quickly prove the most general form of the correctness result for
; TIMES.

; We start with the basic normalization rules for get and put.

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{length} \\  
\=\+{\rm{length}}\,({\it{lst\/}})\index{length} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\it{lst\/}} $\simeq\;${{\bf{nil}}}${}$$\;\;${\bf then }{\tt{0}} \\ 
{\bf else }$1\;+$ {\rm{length}}\,({\rm{cdr}}\,({\it{lst\/}}))\index{length}$\;${\bf  endif}\-\-
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  put-put-0 \\ \index{put-put-0}
\=\+(({\it{addr\/}} $<$ {\rm{length}}\,({\it{mem\/}})\index{length}) $\wedge$ ({\rm{get}}\,({\it{addr\/}}, {\it{mem\/}})\index{get} $=$ {\it{val\/}})) \\ 
$\rightarrow$$\;\;\;\;$({\rm{put}}\,({\it{addr\/}}, {\it{val\/}}, {\it{mem\/}})\index{put} $=$ {\it{mem\/}})\-
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  put-put-1 \\ \index{put-put-1}
{\rm{put}}\,({\it{addr\/}}, {\it{v2\/}}, {\rm{put}}\,({\it{addr\/}}, {\it{v1\/}}, {\it{mem\/}})\index{put})\index{put} $=$ {\rm{put}}\,({\it{addr\/}}, {\it{v2\/}}, {\it{mem\/}})\index{put}
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  put-put-2 \\ \index{put-put-2}
\=\+(({\it{addr1\/}} $\in\;${\bf{N}}${}$) $\wedge$ ({\it{addr2\/}} $\in\;${\bf{N}}${}$) $\wedge$ ({\it{addr1\/}} $\not=$ {\it{addr2\/}})) \\ 
$\rightarrow$$\;\;\;\;$({\rm{put}}\,({\it{addr2\/}}, {\it{v2\/}}, {\rm{put}}\,({\it{addr1\/}}, {\it{v1\/}}, {\it{mem\/}})\index{put})\index{put} $=$ {\rm{put}}\,({\it{addr1\/}}, {\it{v1\/}}, {\rm{put}}\,({\it{addr2\/}}, {\it{v2\/}}, {\it{mem\/}})\index{put})\index{put})\-
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  get-put \\ \index{get-put}
\=\+(({\it{addr1\/}} $\in\;${\bf{N}}${}$) $\wedge$ ({\it{addr2\/}} $\in\;${\bf{N}}${}$)) \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{get}}\,({\it{addr1\/}}, {\rm{put}}\,({\it{addr2\/}}, {\it{val\/}}, {\it{mem\/}})\index{put})\index{get} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\it{addr1\/}} $=$ {\it{addr2\/}}$\;\;${\bf then }{\it{val\/}} \\ 
{\bf else }{\rm{get}}\,({\it{addr1\/}}, {\it{mem\/}})\index{get}$\;${\bf  endif}\-)\-\-
\end{tabbing}


\begin{tabbing}{\sc Theorem}:  length-put \\ \index{length-put}
({\it{addr\/}} $<$ {\rm{length}}\,({\it{mem\/}})\index{length}) $\rightarrow$ ({\rm{length}}\,({\rm{put}}\,({\it{addr\/}}, {\it{val\/}}, {\it{mem\/}})\index{put})\index{length} $=$ {\rm{length}}\,({\it{mem\/}})\index{length})
\end{tabbing}


\noindent{\sc Event}:   Disable get.

\addvspace{18pt}

\noindent{\sc Event}:   Disable put.

\addvspace{18pt}

\begin{verbatim}; And a few basic arithmetic facts.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  difference-1 \\ \index{difference-1}
({\it{x\/}} $-$ {\tt{1}}) $=$ ({\it{x\/}} $-\;1$)
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  difference-elim \\ \index{difference-elim}
(({\it{i\/}} $\in\;${\bf{N}}${}$) $\wedge$ ({\it{i\/}} $\not<$ {\it{j\/}})) $\rightarrow$ (({\it{j\/}} $+$ ({\it{i\/}} $-$ {\it{j\/}})) $=$ {\it{i\/}})
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  associativity-of-plus \\ \index{associativity-of-plus}
(({\it{i\/}} $+$ {\it{j\/}}) $+$ {\it{k\/}}) $=$ ({\it{i\/}} $+$ ({\it{j\/}} $+$ {\it{k\/}}))
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  commutativity-of-plus \\ \index{commutativity-of-plus}
({\it{i\/}} $+$ {\it{j\/}}) $=$ ({\it{j\/}} $+$ {\it{i\/}})
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  commutativity2-of-plus \\ \index{commutativity2-of-plus}
({\it{i\/}} $+$ ({\it{k\/}} $+$ {\it{j\/}})) $=$ ({\it{k\/}} $+$ ({\it{i\/}} $+$ {\it{j\/}}))
\end{tabbing}

\begin{verbatim}; Ok, now we get specific to the TIMES program.  The following function
; "is" loop in the TIMES program vis-a-vis its effect on a completely
; arbitrary memory mem.  If a program is run entirely for its effect on
; memory (as opposed to the subroutine stack or the haltedp flag, then
; this program "is" the McCarthy-esque functional analogue of the loop.

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{times-mem-fn-loop} \\  
\=\+{\rm{times-mem-fn-loop}}\,({\it{mem\/}})\index{times-mem-fn-loop} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\rm{get}}\,({\tt{0}}, {\it{mem\/}})\index{get} $\simeq {\tt{0}}$$\;\;${\bf then }{\it{mem\/}} \\ 
{\bf else }{\rm{times-mem-fn-loop}}\,({\rm{put}}\,(\=\+{\tt{0}}, \\ 
{\rm{get}}\,({\tt{0}}, {\it{mem\/}})\index{get} $-\;1$, \\ 
{\rm{put}}\,({\tt{2}}, {\rm{get}}\,({\tt{2}}, {\it{mem\/}})\index{get} $+$ {\rm{get}}\,({\tt{1}}, {\it{mem\/}})\index{get}, {\it{mem\/}})\index{put})\-\index{put})\index{times-mem-fn-loop}$\;${\bf  endif}\-\-
\end{tabbing}

\begin{tabbing}{\sc Definition}:\index{times-mem-fn} \\  
{\rm{times-mem-fn}}\,({\it{mem\/}})\index{times-mem-fn} $=$ {\rm{times-mem-fn-loop}}\,({\rm{put}}\,({\tt{2}}, {\tt{0}}, {\it{mem\/}})\index{put})\index{times-mem-fn-loop}
\end{tabbing}

\begin{verbatim}; In proving this functional analogue correct we essentially carry
; our McCarthy's functional semantics approach.  The theorem below
; establishes that times-mem-fn-loop just does two puts into mem:  it
; 0's r0 and it puts (r0*r1)+r2 into location r2:

\end{verbatim}\begin{tabbing}{\sc Theorem}:  times-mem-fn-loop-is-times \\ \index{times-mem-fn-loop-is-times}
\=\+(({\rm{get}}\,({\tt{0}}, {\it{mem\/}})\index{get} $\in\;${\bf{N}}${}$) $\wedge$ ({\rm{get}}\,({\tt{2}}, {\it{mem\/}})\index{get} $\in\;${\bf{N}}${}$) $\wedge$ ({\tt{2}} $<$ {\rm{length}}\,({\it{mem\/}})\index{length})) \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{times-mem-fn-loop}}\,({\it{mem\/}})\index{times-mem-fn-loop} \\ 
$=$$\;\;\;\;${\rm{put}}\,(\=\+{\tt{0}}, \\ 
{\tt{0}}, \\ 
{\rm{put}}\,({\tt{2}}, ({\rm{get}}\,({\tt{0}}, {\it{mem\/}})\index{get} $*$ {\rm{get}}\,({\tt{1}}, {\it{mem\/}})\index{get}) $+$ {\rm{get}}\,({\tt{2}}, {\it{mem\/}})\index{get}, {\it{mem\/}})\index{put})\-\index{put})\-\-
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  times-mem-fn-is-correct \\ \index{times-mem-fn-is-correct}
\=\+(({\rm{get}}\,({\tt{0}}, {\it{mem\/}})\index{get} $\in\;${\bf{N}}${}$) $\wedge$ ({\tt{2}} $<$ {\rm{length}}\,({\it{mem\/}})\index{length})) \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{times-mem-fn}}\,({\it{mem\/}})\index{times-mem-fn} \\ 
$=$$\;\;\;\;${\rm{put}}\,({\tt{0}}, {\tt{0}}, {\rm{put}}\,({\tt{2}}, {\rm{get}}\,({\tt{0}}, {\it{mem\/}})\index{get} $*$ {\rm{get}}\,({\tt{1}}, {\it{mem\/}})\index{get}, {\it{mem\/}})\index{put})\index{put})\-\-
\end{tabbing}

\begin{verbatim}; Our aim, in the revisited times-correct theorem, is to establish that
; executing a CALL of TIMES has the following effect on an almost arbitrary
; state s: 

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{times-step} \\  
\=\+{\rm{times-step}}\,({\it{s\/}})\index{times-step} \\ 
$=$$\;\;\;\;${\rm{st}}\,(\=\+{\rm{add1-pc}}\,({\rm{pc}}\,({\it{s\/}})\index{pc})\index{add1-pc}, \\ 
{\rm{stk}}\,({\it{s\/}})\index{stk}, \\ 
{\rm{put}}\,({\tt{0}}, {\tt{0}}, {\rm{put}}\,({\tt{2}}, {\rm{get}}\,({\tt{0}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{get} $*$ {\rm{get}}\,({\tt{1}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{get}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{put})\index{put}, \\ 
{\bf{f}}, \\ 
{\rm{defs}}\,({\it{s\/}})\index{defs})\-\index{st}\-
\end{tabbing}

\begin{verbatim}; The proof proceeds, as we have seen twice before, first by an
; inductive analysis of the loop itself.  Note that we induct
; according to times-mem-fn-loop.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  times-correct-lemma-revisited \\ \index{times-correct-lemma-revisited}
\=\+(({\rm{get}}\,({\tt{0}}, {\it{mem\/}})\index{get} $\in\;${\bf{N}}${}$) $\wedge$ ({\rm{assoc}}\,({\tt{'}}{\tt{times}}, {\it{defs\/}}) $=$ {\sc{times-program}}\index{times-program})) \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{sm}}\,({\rm{st}}\,({\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{1}}{\tt{)}}, {\it{stk1\/}}, {\it{mem\/}}, {\bf{f}}, {\it{defs\/}})\index{st}, {\rm{get}}\,({\tt{0}}, {\it{mem\/}})\index{get} $*$ {\tt{4}})\index{sm} \\ 
$=$$\;\;\;\;${\rm{st}}\,({\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{1}}{\tt{)}}, {\it{stk1\/}}, {\rm{times-mem-fn-loop}}\,({\it{mem\/}})\index{times-mem-fn-loop}, {\bf{f}}, {\it{defs\/}})\index{st})\-\-
\end{tabbing}

\begin{verbatim}; Unfortunately, the above lemma is not quite applicable in our use below
; because the mem that occurs in the state in the lhs of the conclusion is
; not going to be syntactically identical to the mem that occurs in the
; (times (get 0 mem) 4) in the clock.  The reason is that the clock mem is
; the original mem while the state mem is the one produced by moving a 0
; into r2.  Of course, they have the same r0 value.  So, having proved
; the inductive fact we need, we now "generalize" it.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  times-correct-lemma-revisited-and-generalized \\ \index{times-correct-lemma-revisited-a!nd-generalized}
\=\+(\=\+({\it{r0\/}} $=$ {\rm{get}}\,({\tt{0}}, {\it{mem\/}})\index{get}) \\ 
$\wedge$$\;\;\;\;$({\rm{get}}\,({\tt{0}}, {\it{mem\/}})\index{get} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$({\rm{assoc}}\,({\tt{'}}{\tt{times}}, {\it{defs\/}}) $=$ {\sc{times-program}}\index{times-program}))\- \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{sm}}\,({\rm{st}}\,({\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{1}}{\tt{)}}, {\it{stk1\/}}, {\it{mem\/}}, {\bf{f}}, {\it{defs\/}})\index{st}, {\it{r0\/}} $*$ {\tt{4}})\index{sm} \\ 
$=$$\;\;\;\;${\rm{st}}\,({\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{1}}{\tt{)}}, {\it{stk1\/}}, {\rm{times-mem-fn-loop}}\,({\it{mem\/}})\index{times-mem-fn-loop}, {\bf{f}}, {\it{defs\/}})\index{st})\-\-
\end{tabbing}

\begin{verbatim}; And now we can prove the most general form of the correctness of our
; TIMES program.  It tells us that if you are interested in (sm s n),
; where the pc points to a CALL of TIMES, the definition of 'TIMES is
; ours, memory is at least 3 long, r0 is numeric, the halt flag is
; off, and there are at least (times-clock r0) ticks on the clock,
; then you can just take a times-step and decrease the clock by
; (times-clock r0).  What more could you want?

\end{verbatim}\begin{tabbing}{\sc Theorem}:  times-correct-revisited \\ \index{times-correct-revisited}
\=\+(\=\+({\rm{fetch}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch} $=$ {\tt{'}}{\tt{(}}{\tt{call}}{\tt{ }}{\tt{times}}{\tt{)}}) \\ 
$\wedge$$\;\;\;\;$({\rm{assoc}}\,({\tt{'}}{\tt{times}}, {\rm{defs}}\,({\it{s\/}})\index{defs}) $=$ {\sc{times-program}}\index{times-program}) \\ 
$\wedge$$\;\;\;\;$({\tt{2}} $<$ {\rm{length}}\,({\rm{mem}}\,({\it{s\/}})\index{mem})\index{length}) \\ 
$\wedge$$\;\;\;\;$({\it{r0\/}} $=$ {\rm{get}}\,({\tt{0}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{get}) \\ 
$\wedge$$\;\;\;\;$({\it{r0\/}} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$({\it{n\/}} $\not<$ {\rm{times-clock}}\,({\it{r0\/}})\index{times-clock}) \\ 
$\wedge$$\;\;\;\;$($\neg$ {\rm{haltedp}}\,({\it{s\/}})\index{haltedp}))\- \\ 
$\rightarrow$$\;\;\;\;$({\rm{sm}}\,({\it{s\/}}, {\it{n\/}})\index{sm} $=$ {\rm{sm}}\,({\rm{times-step}}\,({\it{s\/}})\index{times-step}, {\it{n\/}} $-$ {\rm{times-clock}}\,({\it{r0\/}})\index{times-clock})\index{sm})\-
\end{tabbing}



\begin{verbatim}; The Inductive Assertion Approach

; First, we simply prove the hand-generated verification
; conditions from an informal annotation of our TIMES
; program.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  verification-conditions-for-times \\ \index{verification-conditions-for-time!s}
\=\+(\=\+(({\it{i0\/}} $\in\;${\bf{N}}${}$) $\wedge$ ({\it{i1\/}} $\in\;${\bf{N}}${}$)) \\ 
$\rightarrow$$\;\;\;\;$(({\tt{0}} $\in\;${\bf{N}}${}$) $\wedge$ (({\it{i0\/}} $*$ {\it{i1\/}}) $=$ ({\tt{0}} $+$ ({\it{i0\/}} $*$ {\it{i1\/}})))))\- \\ 
$\wedge$$\;\;\;\;$(\=\+(({\it{r2\/}} $\in\;${\bf{N}}${}$) $\wedge$ (({\it{i0\/}} $*$ {\it{i1\/}}) $=$ ({\it{r2\/}} $+$ ({\it{r0\/}} $*$ {\it{r1\/}}))) $\wedge$ ({\it{r0\/}} $\not\simeq {\tt{0}}$)) \\ 
$\rightarrow$$\;\;\;\;$(\=\+(({\it{r2\/}} $+$ {\it{r1\/}}) $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$(({\it{i0\/}} $*$ {\it{i1\/}}) $=$ (({\it{r2\/}} $+$ {\it{r1\/}}) $+$ (({\it{r0\/}} $-\;1$) $*$ {\it{r1\/}}))))\-)\- \\ 
$\wedge$$\;\;\;\;$(\=\+(({\it{r2\/}} $\in\;${\bf{N}}${}$) $\wedge$ (({\it{i0\/}} $*$ {\it{i1\/}}) $=$ ({\it{r2\/}} $+$ ({\it{r0\/}} $*$ {\it{r1\/}}))) $\wedge$ ({\it{r0\/}} $\simeq {\tt{0}}$)) \\ 
$\rightarrow$$\;\;\;\;$({\it{r2\/}} $=$ ({\it{i0\/}} $*$ {\it{i1\/}})))\-\-
\end{tabbing}

\begin{verbatim}; Now we develop the analogue of the inductive assertion
; method formally.

; Introduce p as an arbitrary invariant under stepping.  The
; everywhere true predicate witnesses this constraint.

\end{verbatim}\begin{tabbing}{\sc Conservative Axiom}:  p-step \\ \index{p-step}
{\rm{p}}\,({\it{s\/}})\index{p} $\rightarrow$ {\rm{p}}\,({\rm{step}}\,({\it{s\/}})\index{step})\index{p}
\end{tabbing}\noindent Simultaneously, we introduce the new function symbol {\it p\/}.

\begin{verbatim}; Observe that such a p is invariant under arbitrary length runs of the
; machine.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  p-invariant \\ \index{p-invariant}
{\rm{p}}\,({\it{s0\/}})\index{p} $\rightarrow$ {\rm{p}}\,({\rm{sm}}\,({\it{s0\/}}, {\it{n\/}})\index{sm})\index{p}
\end{tabbing}

\begin{verbatim}; That's it.  It is really deep isn't it?

; Now we'll define a p that suits our specification for TIMES.  We call
; it timesp.

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{r0}$\;\;$
{\rm{r0}}\,({\it{s\/}})\index{r0} $=$ {\rm{get}}\,({\tt{0}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{get}
\end{tabbing}
\begin{tabbing}{\sc Definition}:\index{r1}$\;\;$
{\rm{r1}}\,({\it{s\/}})\index{r1} $=$ {\rm{get}}\,({\tt{1}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{get}
\end{tabbing}
\begin{tabbing}{\sc Definition}:\index{r2}$\;\;$
{\rm{r2}}\,({\it{s\/}})\index{r2} $=$ {\rm{get}}\,({\tt{2}}, {\rm{mem}}\,({\it{s\/}})\index{mem})\index{get}
\end{tabbing}

\begin{tabbing}{\sc Definition}:\index{timesp} \\  
\=\+{\rm{timesp}}\,({\it{i0\/}}, {\it{i1\/}}, {\it{s\/}})\index{timesp} \\ 
$=$$\;\;\;\;$(\=\+({\it{i0\/}} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$({\it{i1\/}} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;${\rm{stp}}\,({\it{s\/}})\index{stp} \\ 
$\wedge$$\;\;\;\;$({\rm{stk}}\,({\it{s\/}})\index{stk} $\simeq\;${{\bf{nil}}}${}$) \\ 
$\wedge$$\;\;\;\;$({\rm{assoc}}\,({\tt{'}}{\tt{times}}, {\rm{defs}}\,({\it{s\/}})\index{defs}) $=$ {\sc{times-program}}\index{times-program}) \\ 
$\wedge$$\;\;\;\;$({\it{i1\/}} $=$ {\rm{r1}}\,({\it{s\/}})\index{r1}) \\ 
$\wedge$$\;\;\;\;$\=\+{\bf if }{\rm{pc}}\,({\it{s\/}})\index{pc} $=$ {\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{0}}{\tt{)}}$\;\;${\bf then }{\it{i0\/}} $=$ {\rm{r0}}\,({\it{s\/}})\index{r0} \\ 
{\bf elseif }{\rm{pc}}\,({\it{s\/}})\index{pc} $=$ {\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{1}}{\tt{)}} \\ 
{\bf then }\=\+({\rm{r2}}\,({\it{s\/}})\index{r2} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$(({\it{i0\/}} $*$ {\it{i1\/}}) $=$ ({\rm{r2}}\,({\it{s\/}})\index{r2} $+$ ({\rm{r0}}\,({\it{s\/}})\index{r0} $*$ {\rm{r1}}\,({\it{s\/}})\index{r1})))\- \\ 
{\bf elseif }{\rm{pc}}\,({\it{s\/}})\index{pc} $=$ {\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{2}}{\tt{)}} \\ 
{\bf then }\=\+({\rm{r0}}\,({\it{s\/}})\index{r0} $\not\simeq {\tt{0}}$) \\ 
$\wedge$$\;\;\;\;$({\rm{r2}}\,({\it{s\/}})\index{r2} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$(({\it{i0\/}} $*$ {\it{i1\/}}) $=$ ({\rm{r2}}\,({\it{s\/}})\index{r2} $+$ ({\rm{r0}}\,({\it{s\/}})\index{r0} $*$ {\rm{r1}}\,({\it{s\/}})\index{r1})))\- \\ 
{\bf elseif }{\rm{pc}}\,({\it{s\/}})\index{pc} $=$ {\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{3}}{\tt{)}} \\ 
{\bf then }\=\+({\rm{r0}}\,({\it{s\/}})\index{r0} $\not\simeq {\tt{0}}$) \\ 
$\wedge$$\;\;\;\;$({\rm{r2}}\,({\it{s\/}})\index{r2} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$(\=\+({\it{i1\/}} $+$ ({\it{i0\/}} $*$ {\it{i1\/}})) \\ 
$=$$\;\;\;\;$({\rm{r2}}\,({\it{s\/}})\index{r2} $+$ ({\rm{r0}}\,({\it{s\/}})\index{r0} $*$ {\rm{r1}}\,({\it{s\/}})\index{r1})))\-\- \\ 
{\bf elseif }{\rm{pc}}\,({\it{s\/}})\index{pc} $=$ {\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{4}}{\tt{)}} \\ 
{\bf then }\=\+({\rm{r2}}\,({\it{s\/}})\index{r2} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$(({\it{i0\/}} $*$ {\it{i1\/}}) $=$ ({\rm{r2}}\,({\it{s\/}})\index{r2} $+$ ({\rm{r0}}\,({\it{s\/}})\index{r0} $*$ {\rm{r1}}\,({\it{s\/}})\index{r1})))\- \\ 
{\bf elseif }{\rm{pc}}\,({\it{s\/}})\index{pc} $=$ {\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{5}}{\tt{)}}$\;\;${\bf then }\=\+{\rm{r2}}\,({\it{s\/}})\index{r2} \\ 
$=$$\;\;\;\;$({\it{i0\/}} $*$ {\it{i1\/}})\- \\ 
{\bf else }{\bf{f}}$\;${\bf  endif}\-)\-\-
\end{tabbing}

\begin{verbatim}; Since timesp is preserved by step:

\end{verbatim}\begin{tabbing}{\sc Theorem}:  timesp-step \\ \index{timesp-step}
{\rm{timesp}}\,({\it{i0\/}}, {\it{i1\/}}, {\it{s\/}})\index{timesp} $\rightarrow$ {\rm{timesp}}\,({\it{i0\/}}, {\it{i1\/}}, {\rm{step}}\,({\it{s\/}})\index{step})\index{timesp}
\end{tabbing}

\begin{verbatim}; we can immediately conclude by functional instantiation that
; it is preserved under arbitrary runs of the machine:

\end{verbatim}\begin{tabbing}{\sc Theorem}:  timesp-invariant \\ \index{timesp-invariant}
{\rm{timesp}}\,({\it{i0\/}}, {\it{i1\/}}, {\it{s0\/}})\index{timesp} $\rightarrow$ {\rm{timesp}}\,({\it{i0\/}}, {\it{i1\/}}, {\rm{sm}}\,({\it{s0\/}}, {\it{n\/}})\index{sm})\index{timesp}
\end{tabbing}
  
\begin{verbatim}; By additionally assuming that the initial and final pcs
; are at 0 and 5 respectively in TIMES, we derive the
; desired theorem.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  times-correct-revisited-again \\ \index{times-correct-revisited-again}
\=\+(\=\+{\rm{stp}}\,({\it{s0\/}})\index{stp} \\ 
$\wedge$$\;\;\;\;$({\rm{stk}}\,({\it{s0\/}})\index{stk} $\simeq\;${{\bf{nil}}}${}$) \\ 
$\wedge$$\;\;\;\;$({\rm{assoc}}\,({\tt{'}}{\tt{times}}, {\rm{defs}}\,({\it{s0\/}})\index{defs}) $=$ {\sc{times-program}}\index{times-program}) \\ 
$\wedge$$\;\;\;\;$({\it{i0\/}} $=$ {\rm{get}}\,({\tt{0}}, {\rm{mem}}\,({\it{s0\/}})\index{mem})\index{get}) \\ 
$\wedge$$\;\;\;\;$({\it{i1\/}} $=$ {\rm{get}}\,({\tt{1}}, {\rm{mem}}\,({\it{s0\/}})\index{mem})\index{get}) \\ 
$\wedge$$\;\;\;\;$({\it{i0\/}} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$({\it{i1\/}} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$({\rm{pc}}\,({\it{s0\/}})\index{pc} $=$ {\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{0}}{\tt{)}}) \\ 
$\wedge$$\;\;\;\;$({\rm{pc}}\,({\rm{sm}}\,({\it{s0\/}}, {\it{n\/}})\index{sm})\index{pc} $=$ {\tt{'}}{\tt{(}}{\tt{times}}{\tt{ .\ }}{\tt{5}}{\tt{)}}))\- \\ 
$\rightarrow$$\;\;\;\;$({\rm{get}}\,({\tt{2}}, {\rm{mem}}\,({\rm{sm}}\,({\it{s0\/}}, {\it{n\/}})\index{sm})\index{mem})\index{get} $=$ ({\it{i0\/}} $*$ {\it{i1\/}}))\-
\end{tabbing}


\begin{verbatim}; The following events are not at all easy to follow and should not be
; considered part of the tutorial.  They are included in this file to
; justify the sentence, in the second edition of the Handbook, that
; our standard form of correctness theorem for a subroutine implies
; the standard form of the termination theorem for that subroutine.
; In particular, we lead the system the proof of the following
; theorem.  Suppose s is a state poised to execute a CALL of some
; subroutine fn (and the halt flag of s is F).  Suppose that some
; non-zero number of steps, n, later the stack is the same as it is in
; s.  Intuitively, this means that the subroutine was called and
; eventually returned.  Then if the subroutine is called as the
; top-level program the halt flag is eventually set.  That is to say,
; let s' be obtained from s by setting the pc to (fn . 0), the first
; instruction in fn, and let the stack be nil, i.e., this is the
; top-level, main program.  Then by running s' n steps we obtain a
; state with the halt flag set.  That is the theorem
; standard-correctness-implies-termination, below.

; It is a fairly difficult theorem for two reasons.  First, it
; considers running fn in two different states: as part of a
; continuing computation and as the top-level main program.  We
; therefore have to develop lemmas that let us modify the state, e.g.,
; change the stack, without damaging some aspects of the computation.
; Second, the hypothesis that the stack eventually (at tick n) is the
; same as before the CALL means that a balanced RET was executed.  But
; it does not mean the balancing RET was executed at tick n.  For all
; we know, the CALL returned immediately and during the remaining
; ticks we possibly called other routines or even returned from the
; caller and eventually re-entered!  But we can convert that
; hypothesis into one that says for some k<n the balancing RET was
; executed on the kth tick and if we considered the top-level
; computation at that tick, we'll see that it sets the halt flag.  The
; remaining ticks at the top-level computation just leave the halt
; flag on.

; This proof took several days to construct and I found it frustrating
; in its complexity.  Perhaps someone can simplify it.  That said,
; here are the events with which I proved it.

; Because the tutorial has left the data base in a state designed to
; prove things of individual programs, there is a fair amount of
; enabling and disabling to get access to the guts of the machine.

; First we prove that once the machine halts, it stays halted.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  step-preserves-haltedp \\ \index{step-preserves-haltedp}
($\neg$ {\rm{haltedp}}\,({\rm{step}}\,({\it{s\/}})\index{step})\index{haltedp}) $\rightarrow$ ($\neg$ {\rm{haltedp}}\,({\it{s\/}})\index{haltedp})
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  sm-preserves-haltedp \\ \index{sm-preserves-haltedp}
($\neg$ {\rm{haltedp}}\,({\rm{sm}}\,({\it{s\/}}, {\it{n\/}})\index{sm})\index{haltedp}) $\rightarrow$ ($\neg$ {\rm{haltedp}}\,({\it{s\/}})\index{haltedp})
\end{tabbing}

\begin{verbatim}; And that only RET sets the halt flag, i.e., if it becomes halted,
; then the current pc points to a RET.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  only-ret-sets-haltedp \\ \index{only-ret-sets-haltedp}
\=\+(($\neg$ {\rm{haltedp}}\,({\it{s\/}})\index{haltedp}) $\wedge$ ({\rm{haltedp}}\,({\rm{step}}\,({\it{s\/}})\index{step})\index{haltedp} $\wedge$ ({\it{defs\/}} $=$ {\rm{defs}}\,({\it{s\/}})\index{defs}))) \\ 
$\rightarrow$$\;\;\;\;$({\rm{car}}\,({\rm{get}}\,({\rm{cdr}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}), {\rm{cdr}}\,({\rm{assoc}}\,({\rm{car}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}), {\it{defs\/}})))\index{get}) $=$ {\tt{'}}{\tt{ret}})\-
\end{tabbing}

\begin{verbatim}; This function finds the k<n at which the balancing RET is
; executed.  Imagine that s is the state immediately after the
; CALL and that d is the depth of the stack in that state.
; Then we count ticks until we are poised to execute a RET from
; a state with stack depth d.

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{k} \\  
\=\+{\rm{k}}\,({\it{s\/}}, {\it{d\/}}, {\it{n\/}})\index{k} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\it{n\/}} $\simeq {\tt{0}}$$\;\;${\bf then }{\tt{0}} \\ 
{\bf elseif }\=\+({\rm{length}}\,({\rm{stk}}\,({\it{s\/}})\index{stk})\index{length} $=$ {\it{d\/}}) \\ 
$\wedge$$\;\;\;\;$({\rm{car}}\,({\rm{fetch}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch}) $=$ {\tt{'}}{\tt{ret}})\-$\;\;${\bf then }{\tt{0}} \\ 
{\bf else }$1\;+$ {\rm{k}}\,({\rm{step}}\,({\it{s\/}})\index{step}, {\it{d\/}}, {\it{n\/}} $-\;1$)\index{k}$\;${\bf  endif}\-\-
\end{tabbing}

\begin{verbatim}; Because we'll keep step disabled, we'll need the following to
; analyze what it does to the stack depth.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  length-stk-step \\ \index{length-stk-step}
\=\+{\rm{length}}\,({\rm{stk}}\,({\rm{step}}\,({\it{s\/}})\index{step})\index{stk})\index{length} \\ 
$=$$\;\;\;\;$\=\+{\bf if }{\rm{haltedp}}\,({\it{s\/}})\index{haltedp}$\;\;${\bf then }{\rm{length}}\,({\rm{stk}}\,({\it{s\/}})\index{stk})\index{length} \\ 
{\bf elseif }{\rm{car}}\,({\rm{fetch}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch}) $=$ {\tt{'}}{\tt{ret}}$\;\;${\bf then }{\rm{length}}\,({\rm{stk}}\,({\it{s\/}})\index{stk})\index{length} $-\;1$ \\ 
{\bf elseif }{\rm{car}}\,({\rm{fetch}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch}) $=$ {\tt{'}}{\tt{call}}$\;\;${\bf then }$1\;+$ {\rm{length}}\,({\rm{stk}}\,({\it{s\/}})\index{stk})\index{length} \\ 
{\bf else }{\rm{length}}\,({\rm{stk}}\,({\it{s\/}})\index{stk})\index{length}$\;${\bf  endif}\-\-
\end{tabbing}

\begin{verbatim}; The following theorem establishes that if, within n, the
; stack depth falls below d then the computed k is less than n.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  exists-terminating-ret \\ \index{exists-terminating-ret}
\=\+(({\it{d\/}} $\in\;${\bf{N}}${}$) $\wedge$ ({\rm{length}}\,({\rm{stk}}\,({\it{s0\/}})\index{stk})\index{length} $\not<$ {\it{d\/}}) $\wedge$ ({\rm{length}}\,({\rm{stk}}\,({\rm{sm}}\,({\it{s0\/}}, {\it{n\/}})\index{sm})\index{stk})\index{length} $<$ {\it{d\/}})) \\ 
$\rightarrow$$\;\;\;\;$({\rm{k}}\,({\it{s0\/}}, {\it{d\/}}, {\it{n\/}})\index{k} $<$ {\it{n\/}})\-
\end{tabbing}

\begin{verbatim}; We now want to prove that if the computed k is less than n, then
; various things are true of the state at tick k.  We need the
; obvious fact that the defs field never changes.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  defs-step \\ \index{defs-step}
{\rm{defs}}\,({\rm{step}}\,({\it{s\/}})\index{step})\index{defs} $=$ {\rm{defs}}\,({\it{s\/}})\index{defs}
\end{tabbing}

\begin{verbatim}; So here are some important properties of our k (when it is less than
; n), namely, that the stack depth of the kth state is d and that it
; is poised to execute a RET.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  properties-of-k \\ \index{properties-of-k}
\=\+({\rm{k}}\,({\it{s0\/}}, {\it{d\/}}, {\it{n\/}})\index{k} $<$ {\it{n\/}}) \\ 
$\rightarrow$$\;\;\;\;$(\=\+({\rm{length}}\,({\rm{stk}}\,({\rm{sm}}\,({\it{s0\/}}, {\rm{k}}\,({\it{s0\/}}, {\it{d\/}}, {\it{n\/}})\index{k})\index{sm})\index{stk})\index{length} $=$ {\it{d\/}}) \\ 
$\wedge$$\;\;\;\;$({\rm{car}}\,({\rm{fetch}}\,({\rm{pc}}\,({\rm{sm}}\,({\it{s0\/}}, {\rm{k}}\,({\it{s0\/}}, {\it{d\/}}, {\it{n\/}})\index{k})\index{sm})\index{pc}, {\rm{defs}}\,({\it{s0\/}})\index{defs})\index{fetch}) $=$ {\tt{'}}{\tt{ret}}))\-\-
\end{tabbing}

\begin{verbatim}; We also need that the kth state is still running, i.e., not itself halted.
; This takes a bit of work.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  haltedp-persists \\ \index{haltedp-persists}
{\rm{haltedp}}\,({\it{s\/}})\index{haltedp} $\rightarrow$ {\rm{haltedp}}\,({\rm{sm}}\,({\it{s\/}}, {\it{n\/}})\index{sm})\index{haltedp}
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  haltedp-k \\ \index{haltedp-k}
\=\+{\rm{haltedp}}\,({\it{s\/}})\index{haltedp} \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{k}}\,({\it{s\/}}, {\it{d\/}}, {\it{n\/}})\index{k} \\ 
$=$$\;\;\;\;$\=\+{\bf if }\=\+({\rm{length}}\,({\rm{stk}}\,({\it{s\/}})\index{stk})\index{length} $=$ {\it{d\/}}) \\ 
$\wedge$$\;\;\;\;$({\rm{car}}\,({\rm{fetch}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch}) $=$ {\tt{'}}{\tt{ret}})\-$\;\;${\bf then }{\tt{0}} \\ 
{\bf else }{\rm{fix}}\,({\it{n\/}})$\;${\bf  endif}\-)\-\-
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  halting-preserves-stk \\ \index{halting-preserves-stk}
{\rm{haltedp}}\,({\rm{step}}\,({\it{s0\/}})\index{step})\index{haltedp} $\rightarrow$ ({\rm{length}}\,({\rm{stk}}\,({\rm{step}}\,({\it{s0\/}})\index{step})\index{stk})\index{length} $=$ {\rm{length}}\,({\rm{stk}}\,({\it{s0\/}})\index{stk})\index{length})
\end{tabbing}

\begin{verbatim}; With that preamble, we can get that the kth state is still running.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  another-property-of-k \\ \index{another-property-of-k}
\=\+(($\neg$ {\rm{haltedp}}\,({\it{s0\/}})\index{haltedp}) $\wedge$ ({\rm{k}}\,({\it{s0\/}}, {\it{d\/}}, {\it{n\/}})\index{k} $<$ {\it{n\/}})) \\ 
$\rightarrow$$\;\;\;\;$($\neg$ {\rm{haltedp}}\,({\rm{sm}}\,({\it{s0\/}}, {\rm{k}}\,({\it{s0\/}}, {\it{d\/}}, {\it{n\/}})\index{k})\index{sm})\index{haltedp})\-
\end{tabbing}

\begin{verbatim}; We assemble the two lemmas establishing properties of k into one: if
; s0 is not halted and within n ticks the stack is less than its
; current size then (a) k exists, i.e., is less than n, (b) the kth
; state has the same stack size as s0, (c) the kth state is poised to
; execute a RET and (d) it is not halted.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  decreasing-stk-means-ret-exists \\ \index{decreasing-stk-means-ret-exists}
\=\+(($\neg$ {\rm{haltedp}}\,({\it{s0\/}})\index{haltedp}) $\wedge$ ({\rm{length}}\,({\rm{stk}}\,({\rm{sm}}\,({\it{s0\/}}, {\it{n\/}})\index{sm})\index{stk})\index{length} $<$ {\rm{length}}\,({\rm{stk}}\,({\it{s0\/}})\index{stk})\index{length})) \\ 
$\rightarrow$$\;\;\;\;$(\=\+({\rm{k}}\,({\it{s0\/}}, {\rm{length}}\,({\rm{stk}}\,({\it{s0\/}})\index{stk})\index{length}, {\it{n\/}})\index{k} $<$ {\it{n\/}}) \\ 
$\wedge$$\;\;\;\;$(\=\+{\rm{length}}\,({\rm{stk}}\,({\rm{sm}}\,({\it{s0\/}}, {\rm{k}}\,({\it{s0\/}}, {\rm{length}}\,({\rm{stk}}\,({\it{s0\/}})\index{stk})\index{length}, {\it{n\/}})\index{k})\index{sm})\index{stk})\index{length} \\ 
$=$$\;\;\;\;${\rm{length}}\,({\rm{stk}}\,({\it{s0\/}})\index{stk})\index{length})\- \\ 
$\wedge$$\;\;\;\;$(\=\+{\rm{car}}\,({\rm{fetch}}\,({\rm{pc}}\,({\rm{sm}}\,({\it{s0\/}}, {\rm{k}}\,({\it{s0\/}}, {\rm{length}}\,({\rm{stk}}\,({\it{s0\/}})\index{stk})\index{length}, {\it{n\/}})\index{k})\index{sm})\index{pc}, {\rm{defs}}\,({\it{s0\/}})\index{defs})\index{fetch}) \\ 
$=$$\;\;\;\;${\tt{'}}{\tt{ret}})\- \\ 
$\wedge$$\;\;\;\;$($\neg$ {\rm{haltedp}}\,({\rm{sm}}\,({\it{s0\/}}, {\rm{k}}\,({\it{s0\/}}, {\rm{length}}\,({\rm{stk}}\,({\it{s0\/}})\index{stk})\index{length}, {\it{n\/}})\index{k})\index{sm})\index{haltedp}))\-\-
\end{tabbing}

\begin{verbatim}; Now we'll disable the two independently proved lemmas about k.

\end{verbatim}
\noindent{\sc Event}:   Disable properties-of-k.

\addvspace{18pt}

\noindent{\sc Event}:   Disable another-property-of-k.

\addvspace{18pt}

\begin{verbatim}; Next, we develop the idea that under some conditions we can mess around with
; the stack of a computation without changing the outcome in some sense.  The only
; way we'll mess around is by growing the stack at the deep end by adding some
; arbitrary additional cells.  

\end{verbatim}\begin{tabbing}{\sc Definition}:\index{grow-stk} \\  
{\rm{grow-stk}}\,({\it{s\/}}, {\it{stk\/}})\index{grow-stk} $=$ {\rm{st}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{append}}\,({\rm{stk}}\,({\it{s\/}})\index{stk}, {\it{stk\/}}), {\rm{mem}}\,({\it{s\/}})\index{mem}, {\rm{haltedp}}\,({\it{s\/}})\index{haltedp}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{st}
\end{tabbing}

\begin{verbatim}; The lemma sm-grow-stk, just below, is the key result.  The intervening
; lemmas are just helpers.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  listp-append \\ \index{listp-append}
{\rm{listp}}\,({\rm{append}}\,({\it{a\/}}, {\it{b\/}})) $=$ ({\rm{listp}}\,({\it{a\/}}) $\vee$ {\rm{listp}}\,({\it{b\/}}))
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  step-grow-stk \\ \index{step-grow-stk}
\=\+($\neg$ {\rm{haltedp}}\,({\rm{step}}\,({\it{s\/}})\index{step})\index{haltedp}) \\ 
$\rightarrow$$\;\;\;\;$({\rm{step}}\,({\rm{grow-stk}}\,({\it{s\/}}, {\it{stk\/}})\index{grow-stk})\index{step} $=$ {\rm{grow-stk}}\,({\rm{step}}\,({\it{s\/}})\index{step}, {\it{stk\/}})\index{grow-stk})\-
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  sm-grow-stk \\ \index{sm-grow-stk}
\=\+($\neg$ {\rm{haltedp}}\,({\rm{sm}}\,({\it{s\/}}, {\it{n\/}})\index{sm})\index{haltedp}) \\ 
$\rightarrow$$\;\;\;\;$({\rm{sm}}\,({\rm{grow-stk}}\,({\it{s\/}}, {\it{stk\/}})\index{grow-stk}, {\it{n\/}})\index{sm} $=$ {\rm{grow-stk}}\,({\rm{sm}}\,({\it{s\/}}, {\it{n\/}})\index{sm}, {\it{stk\/}})\index{grow-stk})\-
\end{tabbing}

\begin{verbatim}; The above lemma is really nice.  It says that if a computation
; doesn't halt within n then growing the stack commutes with the
; computation, i.e., you can grow the stack before you start or after
; you finish.  This lets us consider a computation in either of two
; states, one with a shallow stack or one with a deep stack.  If s has
; a stack of nil then it is in top-level execution and thus (grow-stk
; s stk) is some continuing execution of the same program.

; A key fact we'll need is that if k is less than or equal to n and the
; computation halts in k then it halts in n.  This explains why the
; halt flag is set at the end of the long top-level computation, even if
; it became set fairly early.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  lessp-haltedp \\ \index{lessp-haltedp}
(({\it{n\/}} $\not<$ {\it{k\/}}) $\wedge$ {\rm{haltedp}}\,({\rm{sm}}\,({\it{s\/}}, {\it{k\/}})\index{sm})\index{haltedp}) $\rightarrow$ {\rm{haltedp}}\,({\rm{sm}}\,({\it{s\/}}, {\it{n\/}})\index{sm})\index{haltedp}
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  equal-length-0 \\ \index{equal-length-0}
({\rm{length}}\,({\it{x\/}})\index{length} $=$ {\tt{0}}) $=$ ({\it{x\/}} $\simeq\;${{\bf{nil}}}${}$)
\end{tabbing}
                                          
\begin{verbatim}; Again, because step will be disabled later, we need to expose the
; behavior of a halting RET.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  step-is-ret \\ \index{step-is-ret}
\=\+(\=\+($\neg$ {\rm{haltedp}}\,({\it{s\/}})\index{haltedp}) \\ 
$\wedge$$\;\;\;\;$({\rm{car}}\,({\rm{fetch}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch}) $=$ {\tt{'}}{\tt{ret}}) \\ 
$\wedge$$\;\;\;\;$({\rm{stk}}\,({\it{s\/}})\index{stk} $\simeq\;${{\bf{nil}}}${}$))\- \\ 
$\rightarrow$$\;\;\;\;${\rm{haltedp}}\,({\rm{step}}\,({\it{s\/}})\index{step})\index{haltedp}\-
\end{tabbing}

\begin{verbatim}; Oddly enough, though we proved that defs is preserved by step, above,
; we only now need that it is preserved by sm.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  defs-sm \\ \index{defs-sm}
{\rm{defs}}\,({\rm{sm}}\,({\it{s\/}}, {\it{n\/}})\index{sm})\index{defs} $=$ {\rm{defs}}\,({\it{s\/}})\index{defs}
\end{tabbing}

\begin{verbatim}; In a sense, the following theorem is the real key to our proof.  It
; gives us a way to show that the halted flag is on in the nth step of
; s, namely find some k less than n-1 such that the kth state is not
; yet halted but has a stack of length 0 and is poised to execute a
; RET.  If you imagine that s is the top-level run of our subroutine,
; then this focusses our attention on the k at which the halt flag first
; becomes set.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  expand-sm-n \\ \index{expand-sm-n}
\=\+(\=\+({\it{k\/}} $<$ ({\it{n\/}} $-\;1$)) \\ 
$\wedge$$\;\;\;\;$($\neg$ {\rm{haltedp}}\,({\rm{sm}}\,({\it{s\/}}, {\it{k\/}})\index{sm})\index{haltedp}) \\ 
$\wedge$$\;\;\;\;$({\rm{car}}\,({\rm{fetch}}\,({\rm{pc}}\,({\rm{sm}}\,({\it{s\/}}, {\it{k\/}})\index{sm})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch}) $=$ {\tt{'}}{\tt{ret}}) \\ 
$\wedge$$\;\;\;\;$({\rm{length}}\,({\rm{stk}}\,({\rm{sm}}\,({\it{s\/}}, {\it{k\/}})\index{sm})\index{stk})\index{length} $=$ {\tt{0}}))\- \\ 
$\rightarrow$$\;\;\;\;${\rm{haltedp}}\,({\rm{sm}}\,({\it{s\/}}, {\it{n\/}})\index{sm})\index{haltedp}\-
\end{tabbing}

\begin{verbatim}; Now there are various details to be worked out, and I never found a
; really nice way to handle them except by brute force.  The basic
; theme of these details is that from the hypothesis that the
; ``continuing computation'' eventually returns to the same stack
; depth we can get some information about the pc and stack depth in
; the continuing computation.  But we have to convert that to
; information about the pc and stack depth in the top-level
; computation.  We can get these results from our sm-grow-stk lemma,
; namely, we know that if a short stacked computation doesn't halt we
; can grow its stack either before or after.  If the short stacked
; computation is the top-level one, where the stack is nil, then we
; can grow the stack to whatever stack we have in the continuing
; computation.  From the equality of the two final states we can learn
; that the pc of the top-level computation is the same as that of the
; continuing one.  While I find this proof very neat, what with its
; use of sm-grow-stk, I find the event below ugly because of the
; explict hint and the explicit states involved.  But it just wasn't
; worth my time to figure out an elegant rewrite rule that would
; normalize the pc.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  pc-equiv \\ \index{pc-equiv}
\=\+($\neg$ {\rm{haltedp}}\,({\rm{sm}}\,({\rm{st}}\,({\rm{cons}}\,({\it{prog\/}}, {\tt{0}}), {\bf{nil}}, {\rm{mem}}\,({\it{s\/}})\index{mem}, {\bf{f}}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{st}, {\it{k\/}})\index{sm})\index{haltedp}) \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{pc}}\,({\rm{sm}}\,({\rm{st}}\,({\rm{cons}}\,({\it{prog\/}}, {\tt{0}}), {\bf{nil}}, {\rm{mem}}\,({\it{s\/}})\index{mem}, {\bf{f}}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{st}, {\it{k\/}})\index{sm})\index{pc} \\ 
$=$$\;\;\;\;${\rm{pc}}\,({\rm{sm}}\,(\=\+{\rm{st}}\,(\=\+{\rm{cons}}\,({\it{prog\/}}, {\tt{0}}), \\ 
{\rm{cons}}\,({\rm{cons}}\,({\rm{car}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}), $1\;+$ {\rm{cdr}}\,({\rm{pc}}\,({\it{s\/}})\index{pc})), {\rm{stk}}\,({\it{s\/}})\index{stk}), \\ 
{\rm{mem}}\,({\it{s\/}})\index{mem}, \\ 
{\bf{f}}, \\ 
{\rm{defs}}\,({\it{s\/}})\index{defs})\-\index{st}, \\ 
{\it{k\/}})\-\index{sm})\index{pc})\-\-
\end{tabbing}

\begin{verbatim}; We need to know a similar fact about the stacks after k steps.  In particular,
; we know from the continuing computation that at step k it is poised to RET on
; a stack of a certain depth.  We need to convert that to a fact about the top-level
; state at step k, namely that the stack there is nil -- so the RET will set the halt
; flag.  At first sight, this is a problem very similar to that above and one is
; tempted to try to solve it the same way.  But the problem above is insensitive to
; the value of k, as long as the computation is still running, while the one we
; are talking about now is our special k, the tick at which we execute the RET that
; balances the initial CALL.  But that raises a problem.  That existential k
; is computed with a given state.  Is that state from the continuing computation
; or from the top-level one?  What we prove below is that it doesn't matter, they
; are the same!  This is pretty subtle.  We need a few lemmas...

\end{verbatim}\begin{tabbing}{\sc Theorem}:  length-append \\ \index{length-append}
{\rm{length}}\,({\rm{append}}\,({\it{a\/}}, {\it{b\/}}))\index{length} $=$ ({\rm{length}}\,({\it{a\/}})\index{length} $+$ {\rm{length}}\,({\it{b\/}})\index{length})
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  grow-stk-props \\ \index{grow-stk-props}
\=\+({\rm{pc}}\,({\rm{grow-stk}}\,({\it{s\/}}, {\it{stk\/}})\index{grow-stk})\index{pc} $=$ {\rm{pc}}\,({\it{s\/}})\index{pc}) \\ 
$\wedge$$\;\;\;\;$({\rm{stk}}\,({\rm{grow-stk}}\,({\it{s\/}}, {\it{stk\/}})\index{grow-stk})\index{stk} $=$ {\rm{append}}\,({\rm{stk}}\,({\it{s\/}})\index{stk}, {\it{stk\/}})) \\ 
$\wedge$$\;\;\;\;$({\rm{mem}}\,({\rm{grow-stk}}\,({\it{s\/}}, {\it{stk\/}})\index{grow-stk})\index{mem} $=$ {\rm{mem}}\,({\it{s\/}})\index{mem}) \\ 
$\wedge$$\;\;\;\;$({\rm{haltedp}}\,({\rm{grow-stk}}\,({\it{s\/}}, {\it{stk\/}})\index{grow-stk})\index{haltedp} $=$ {\rm{haltedp}}\,({\it{s\/}})\index{haltedp}) \\ 
$\wedge$$\;\;\;\;$({\rm{defs}}\,({\rm{grow-stk}}\,({\it{s\/}}, {\it{stk\/}})\index{grow-stk})\index{defs} $=$ {\rm{defs}}\,({\it{s\/}})\index{defs})\-
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  step-grow-stk-revisited-1 \\ \index{step-grow-stk-revisited-1}
\=\+({\tt{0}} $<$ {\rm{length}}\,({\rm{stk}}\,({\it{s\/}})\index{stk})\index{length}) \\ 
$\rightarrow$$\;\;\;\;$({\rm{step}}\,({\rm{grow-stk}}\,({\it{s\/}}, {\it{stk\/}})\index{grow-stk})\index{step} $=$ {\rm{grow-stk}}\,({\rm{step}}\,({\it{s\/}})\index{step}, {\it{stk\/}})\index{grow-stk})\-
\end{tabbing}
                  
\begin{tabbing}{\sc Theorem}:  step-grow-stk-revisited-2 \\ \index{step-grow-stk-revisited-2}
\=\+({\rm{car}}\,({\rm{fetch}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch}) $\not=$ {\tt{'}}{\tt{ret}}) \\ 
$\rightarrow$$\;\;\;\;$({\rm{step}}\,({\rm{grow-stk}}\,({\it{s\/}}, {\it{stk\/}})\index{grow-stk})\index{step} $=$ {\rm{grow-stk}}\,({\rm{step}}\,({\it{s\/}})\index{step}, {\it{stk\/}})\index{grow-stk})\-
\end{tabbing}

\begin{verbatim}; So here is the key fact: k produces the same answer on the top-level
; state (here, s) and the continuing state, provided you bump the d
; appropriately.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  k-grow-stk \\ \index{k-grow-stk}
\=\+(({\it{d\/}} $\in\;${\bf{N}}${}$) $\wedge$ ({\rm{length}}\,({\rm{stk}}\,({\it{s\/}})\index{stk})\index{length} $\not<$ {\it{d\/}})) \\ 
$\rightarrow$$\;\;\;\;$({\rm{k}}\,({\rm{grow-stk}}\,({\it{s\/}}, {\it{stk\/}})\index{grow-stk}, {\it{d\/}} $+$ {\rm{length}}\,({\it{stk\/}})\index{length}, {\it{n\/}})\index{k} $=$ {\rm{k}}\,({\it{s\/}}, {\it{d\/}}, {\it{n\/}})\index{k})\-
\end{tabbing}

\begin{verbatim}; Once again, I couldn't find a useful rewrite rule, since grow-stk isn't
; really in our problem, and so I make this lemma of class nil and instantiate
; it when I need to show that the two states produce the same k.  Given that,
; we can now infer that the final, top-level stack is nil at step k,
; just by using properties of k on the top-level state, but appealing to
; the existence of k from the continuing state.  We then repeat the exercise
; to extract the information that the halt flag is still off at step k in
; the top-level state.

\end{verbatim}\begin{tabbing}{\sc Theorem}:  stk-is-nil \\ \index{stk-is-nil}
\=\+(\=\+{\rm{k}}\,(\=\+{\rm{st}}\,(\=\+{\rm{cons}}\,({\it{prog\/}}, {\tt{0}}), \\ 
{\rm{cons}}\,({\rm{cons}}\,({\rm{car}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}), $1\;+$ {\rm{cdr}}\,({\rm{pc}}\,({\it{s\/}})\index{pc})), {\rm{stk}}\,({\it{s\/}})\index{stk}), \\ 
{\rm{mem}}\,({\it{s\/}})\index{mem}, \\ 
{\bf{f}}, \\ 
{\rm{defs}}\,({\it{s\/}})\index{defs})\-\index{st}, \\ 
$1\;+$ {\rm{length}}\,({\rm{stk}}\,({\it{s\/}})\index{stk})\index{length}, \\ 
{\it{n\/}} $-\;1$)\-\index{k} \\ 
$<$$\;\;\;\;$({\it{n\/}} $-\;1$))\- \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{listp}}\,({\rm{stk}}\,({\rm{sm}}\,(\=\+{\rm{st}}\,({\rm{cons}}\,({\it{prog\/}}, {\tt{0}}), {\bf{nil}}, {\rm{mem}}\,({\it{s\/}})\index{mem}, {\bf{f}}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{st}, \\ 
{\rm{k}}\,(\=\+{\rm{st}}\,(\=\+{\rm{cons}}\,({\it{prog\/}}, {\tt{0}}), \\ 
{\rm{cons}}\,({\rm{cons}}\,({\rm{car}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}), $1\;+$ {\rm{cdr}}\,({\rm{pc}}\,({\it{s\/}})\index{pc})), {\rm{stk}}\,({\it{s\/}})\index{stk}), \\ 
{\rm{mem}}\,({\it{s\/}})\index{mem}, \\ 
{\bf{f}}, \\ 
{\rm{defs}}\,({\it{s\/}})\index{defs})\-\index{st}, \\ 
$1\;+$ {\rm{length}}\,({\rm{stk}}\,({\it{s\/}})\index{stk})\index{length}, \\ 
{\it{n\/}} $-\;1$)\-\index{k})\-\index{sm})\index{stk}) \\ 
$=$$\;\;\;\;${\bf{f}})\-\-
\end{tabbing}

\begin{tabbing}{\sc Theorem}:  haltedp-is-off \\ \index{haltedp-is-off}
\=\+(\=\+{\rm{k}}\,(\=\+{\rm{st}}\,(\=\+{\rm{cons}}\,({\it{prog\/}}, {\tt{0}}), \\ 
{\rm{cons}}\,({\rm{cons}}\,({\rm{car}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}), $1\;+$ {\rm{cdr}}\,({\rm{pc}}\,({\it{s\/}})\index{pc})), {\rm{stk}}\,({\it{s\/}})\index{stk}), \\ 
{\rm{mem}}\,({\it{s\/}})\index{mem}, \\ 
{\bf{f}}, \\ 
{\rm{defs}}\,({\it{s\/}})\index{defs})\-\index{st}, \\ 
$1\;+$ {\rm{length}}\,({\rm{stk}}\,({\it{s\/}})\index{stk})\index{length}, \\ 
{\it{n\/}} $-\;1$)\-\index{k} \\ 
$<$$\;\;\;\;$({\it{n\/}} $-\;1$))\- \\ 
$\rightarrow$$\;\;\;\;$(\=\+{\rm{haltedp}}\,({\rm{sm}}\,(\=\+{\rm{st}}\,({\rm{cons}}\,({\it{prog\/}}, {\tt{0}}), {\bf{nil}}, {\rm{mem}}\,({\it{s\/}})\index{mem}, {\bf{f}}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{st}, \\ 
{\rm{k}}\,(\=\+{\rm{st}}\,(\=\+{\rm{cons}}\,({\it{prog\/}}, {\tt{0}}), \\ 
{\rm{cons}}\,({\rm{cons}}\,({\rm{car}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}), $1\;+$ {\rm{cdr}}\,({\rm{pc}}\,({\it{s\/}})\index{pc})), {\rm{stk}}\,({\it{s\/}})\index{stk}), \\ 
{\rm{mem}}\,({\it{s\/}})\index{mem}, \\ 
{\bf{f}}, \\ 
{\rm{defs}}\,({\it{s\/}})\index{defs})\-\index{st}, \\ 
$1\;+$ {\rm{length}}\,({\rm{stk}}\,({\it{s\/}})\index{stk})\index{length}, \\ 
{\it{n\/}} $-\;1$)\-\index{k})\-\index{sm})\index{haltedp} \\ 
$=$$\;\;\;\;${\bf{f}})\-\-
\end{tabbing}

\begin{verbatim}; So, if you've followed all that, you are ready to get the main theorem:

\end{verbatim}\begin{tabbing}{\sc Theorem}:  standard-correctness-implies-termination \\ \index{standard-correctness-implies-te!rmination}
\=\+(\=\+($\neg$ {\rm{haltedp}}\,({\it{s\/}})\index{haltedp}) \\ 
$\wedge$$\;\;\;\;$({\rm{fetch}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch} $=$ {\rm{list}}\,({\tt{'}}{\tt{call}}, {\it{prog\/}})) \\ 
$\wedge$$\;\;\;\;$({\it{n\/}} $\not\simeq {\tt{0}}$) \\ 
$\wedge$$\;\;\;\;$({\rm{stk}}\,({\rm{sm}}\,({\it{s\/}}, {\it{n\/}})\index{sm})\index{stk} $=$ {\rm{stk}}\,({\it{s\/}})\index{stk}))\- \\ 
$\rightarrow$$\;\;\;\;${\rm{haltedp}}\,({\rm{sm}}\,({\rm{st}}\,({\rm{cons}}\,({\it{prog\/}}, {\tt{0}}), {\bf{nil}}, {\rm{mem}}\,({\it{s\/}})\index{mem}, {\bf{f}}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{st}, {\it{n\/}})\index{sm})\index{haltedp}\-
\end{tabbing}

\begin{verbatim}; As I said, the proof is not at all easy to follow.  I invite
; readers to find a better one!

        
; The next theorem establishes the effect of a one-instruction infinite loop.
; It says that if you have a running state and when you fetch the current
; instruction you get (JUMP i) where i is the location of the current program
; counter, then the halt flag is never set. 

\end{verbatim}\begin{tabbing}{\sc Theorem}:  infinite-loop \\ \index{infinite-loop}
\=\+(\=\+($\neg$ {\rm{haltedp}}\,({\it{s\/}})\index{haltedp}) \\ 
$\wedge$$\;\;\;\;$({\rm{fetch}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}, {\rm{defs}}\,({\it{s\/}})\index{defs})\index{fetch} $=$ {\rm{list}}\,({\tt{'}}{\tt{jump}}, {\it{i\/}})) \\ 
$\wedge$$\;\;\;\;$({\it{i\/}} $\in\;${\bf{N}}${}$) \\ 
$\wedge$$\;\;\;\;$({\rm{cdr}}\,({\rm{pc}}\,({\it{s\/}})\index{pc}) $=$ {\it{i\/}}))\- \\ 
$\rightarrow$$\;\;\;\;$($\neg$ {\rm{haltedp}}\,({\rm{sm}}\,({\it{s\/}}, {\it{n\/}})\index{sm})\index{haltedp})\-
\end{tabbing}
%\end{list}
\printindex
\end{document}
