perm filename BOOK.XGP[206,LSP]1 blob sn#381626 filedate 1978-09-20 generic text, type T, neo UTF8
/LMAR=0/XLINE=3/FONT#0=BAXL30[FNT,CLT]/FONT#1=BAXM30/FONT#2=BAXB30[FNT,CLT]/FONT#3=SUB/FONT#4=SUP/FONT#5=GACS25/FONT#6=FIX20/FONT#7=SYMB30[FNT,CLT]/FONT#8=FIX25/FONT#11=GRFX25/FONT#12=GRFX35/FONT#9=BEESIX















␈↓ ↓H␈↓	␈↓ ε≥LISP


␈↓ ↓H␈↓	␈↓ ∧eProgramming and Proving














␈↓ ↓H␈↓␈↓ ¬SCopyright ␈↓π@␈↓ 1978

␈↓ ↓H␈↓␈↓ ∧XJohn McCarthy and Carolyn Talcott


␈↓ ↓H␈↓␈↓ ¬GStanford University



␈↓ ↓H␈↓␈↓ βwThis version printed at 3:22 on September 20, 1978.
␈↓ ↓H␈↓␈↓ εH␈↓ ?i


␈↓ ↓H␈↓α␈↓ ¬NTable of Contents



␈↓ ↓H␈↓␈↓ 	Page



␈↓ ↓H␈↓INTRODUCTION


␈↓ ↓H␈↓I␈↓ α_INTRODUCTION TO LISP

␈↓ ↓H␈↓␈↓ α81␈↓ αxLists.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ ≠   1

␈↓ ↓H␈↓␈↓ α82␈↓ αxAtoms.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ ≠   2

␈↓ ↓H␈↓␈↓ α83␈↓ αxList structures.␈↓ ∧8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ ≠   3

␈↓ ↓H␈↓␈↓ α84␈↓ αxS-expressions.␈↓ ∧8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ ≠   4

␈↓ ↓H␈↓␈↓ α85␈↓ αxThe basic functions and predicates of LISP.␈↓ π8   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ ≠   7

␈↓ ↓H␈↓␈↓ α86␈↓ αxConditional expressions.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ ≠   9

␈↓ ↓H␈↓␈↓ α87␈↓ αxPropositional expressions.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    11

␈↓ ↓H␈↓␈↓ α88␈↓ αxRecursive function definitions.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    12

␈↓ ↓H␈↓␈↓ α89␈↓ αxNumerical computation.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    18

␈↓ ↓H␈↓␈↓ α810␈↓ αxBoolean Operations on Numbers␈↓ ε8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    20

␈↓ ↓H␈↓␈↓ α811␈↓ αxLambda expressions and functions with functions as arguments.␈↓ 	8   .   .   .   .   .␈↓    21

␈↓ ↓H␈↓␈↓ α812␈↓ αxLabel.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    25

␈↓ ↓H␈↓␈↓ α813␈↓ αxThe function ␈↓↓eval.␈↓␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    25


␈↓ ↓H␈↓II␈↓ α_WRITING RECURSIVE FUNCTION DEFINITIONS

␈↓ ↓H␈↓␈↓ α81␈↓ αxStatic and dynamic ways of programming.␈↓ π8   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    30

␈↓ ↓H␈↓␈↓ α82␈↓ αxRecursive definition of functions on natural numbers.␈↓ λ8   .   .   .   .   .   .   .   .   .␈↓    31

␈↓ ↓H␈↓␈↓ α83␈↓ αxSimple list recursion.␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    33

␈↓ ↓H␈↓␈↓ α84␈↓ αxSimple S-expression recursion.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    35

␈↓ ↓H␈↓␈↓ α85␈↓ αxOther structural recursions.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    37
␈↓ ↓H␈↓ii␈↓ ¬RTable of Contents␈↓ H


␈↓ ↓H␈↓␈↓ α86␈↓ αxGeneral tree recursion.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    38

␈↓ ↓H␈↓␈↓ α87␈↓ αxSolving a LISP programming problem.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    39

␈↓ ↓H␈↓␈↓ α88␈↓ αxLots of LISP functions to program.␈↓ ε8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    43


␈↓ ↓H␈↓III␈↓ α_PROVING FACTS ABOUT LISP PROGRAMS

␈↓ ↓H␈↓␈↓ α81␈↓ αxSimple properties of ␈↓↓append␈↓␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    49

␈↓ ↓H␈↓␈↓ α82␈↓ αxAbout Formalizing.␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    53

␈↓ ↓H␈↓␈↓ α83␈↓ αxTheory of LISP: the Language.␈↓ ε8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    55

␈↓ ↓H␈↓␈↓ α84␈↓ αxTheory of LISP: the Semantics.␈↓ ε8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    57

␈↓ ↓H␈↓␈↓ α85␈↓ αxTheory of LISP: Algebraic Axioms.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    59

␈↓ ↓H␈↓␈↓ α86␈↓ αxTheory of LISP: Induction Schemas.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    60

␈↓ ↓H␈↓␈↓ α87␈↓ αxDeduction Rules for First-Order Logic.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    62

␈↓ ↓H␈↓␈↓ α88␈↓ αxRules for using restricted variables.␈↓ ε8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    65

␈↓ ↓H␈↓␈↓ α89␈↓ αxConditional Expressions.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    65

␈↓ ↓H␈↓␈↓ α810␈↓ αxLambda-expressions.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    66

␈↓ ↓H␈↓␈↓ α811␈↓ αxIntroducing facts about recursively defined functions.␈↓ λ8   .   .   .   .   .   .   .   .   .␈↓    67

␈↓ ↓H␈↓␈↓ α812␈↓ αxRecursively Defined Predicates.␈↓ ε8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    73

␈↓ ↓H␈↓␈↓ α813␈↓ αxThe SAMEFRINGE problem.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    76

␈↓ ↓H␈↓␈↓ α814␈↓ αxCorrectness of a Program to Partition Lists.␈↓ π8   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    82

␈↓ ↓H␈↓␈↓ α815␈↓ αxPartial functions and the Minimization Schema.␈↓ πx   .   .   .   .   .   .   .   .   .   .   .␈↓    86

␈↓ ↓H␈↓␈↓ α816␈↓ αxExercises Part 1.␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    90

␈↓ ↓H␈↓␈↓ α817␈↓ αxExercises Part2.␈↓ ∧8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    91
␈↓ ↓H␈↓␈↓ ¬RTable of Contents␈↓ -iii


␈↓ ↓H␈↓IV␈↓ α_LISP PROGRAMS WITH SIDE EFFECTS

␈↓ ↓H␈↓␈↓ α81␈↓ αxSequential (ALGOL-like) programs.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    93

␈↓ ↓H␈↓␈↓ α82␈↓ αxArrays in LISP␈↓ ∧8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    97

␈↓ ↓H␈↓␈↓ α83␈↓ αxProperty Lists and Special Properties.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    98

␈↓ ↓H␈↓␈↓ α84␈↓ αxManipulating atomic symbols.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   102

␈↓ ↓H␈↓␈↓ α85␈↓ αxPseudo-functions that modify list structures.␈↓ π8   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   103

␈↓ ↓H␈↓␈↓ α86␈↓ αxRe-entrant List Structure.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   105

␈↓ ↓H␈↓␈↓ α87␈↓ αxExercises.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   108


␈↓ ↓H␈↓V␈↓ α_HOW LISP WORKS

␈↓ ↓H␈↓␈↓ α81␈↓ αxBasic data structures and operations of LISP.␈↓ π8   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   110

␈↓ ↓H␈↓␈↓ α82␈↓ αxThe Top level of LISP.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   114

␈↓ ↓H␈↓␈↓ α83␈↓ αxError Handling.␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   116

␈↓ ↓H␈↓␈↓ α84␈↓ αxDebugging Aids in LISP.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   118

␈↓ ↓H␈↓␈↓ α85␈↓ αxEditing.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   120

␈↓ ↓H␈↓␈↓ α86␈↓ αxCompiling.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   123

␈↓ ↓H␈↓␈↓ α87␈↓ αxExercises.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   125


␈↓ ↓H␈↓VI␈↓ α_LISP PROGRAMS FOR SEARCHING

␈↓ ↓H␈↓␈↓ α81␈↓ αxDepth First Tree Search.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   126

␈↓ ↓H␈↓␈↓ α82␈↓ αxGame trees.␈↓ ∧8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   129

␈↓ ↓H␈↓␈↓ α83␈↓ αxThe hidden board trick.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   136

␈↓ ↓H␈↓␈↓ α84␈↓ αx2-dimensional Tic Tac Toe.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   137
␈↓ ↓H␈↓iv␈↓ ¬RTable of Contents␈↓ H


␈↓ ↓H␈↓VII␈↓ α_TRANSFORMING LISP PROGRAMS

␈↓ ↓H␈↓␈↓ α81␈↓ αxPurifying LISP programs.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   143

␈↓ ↓H␈↓␈↓ α82␈↓ αxDerived programs.␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   146

␈↓ ↓H␈↓␈↓ α83␈↓ αxSubstantial exercises␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   150


␈↓ ↓H␈↓VIII␈↓ α_ABSTRACT SYNTAX

␈↓ ↓H␈↓␈↓ α81␈↓ αxWhat is Abstract Syntax?␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   151

␈↓ ↓H␈↓␈↓ α82␈↓ αxCorrectness of a Compiler for Arithmetic Expressions.␈↓ λ8   .   .   .   .   .   .   .   .   .␈↓ 
⎇   155

␈↓ ↓H␈↓␈↓ α83␈↓ αxThe source language.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   156

␈↓ ↓H␈↓␈↓ α84␈↓ αxThe object language.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   157

␈↓ ↓H␈↓␈↓ α85␈↓ αxThe compiler.␈↓ ∧8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   159

␈↓ ↓H␈↓␈↓ α86␈↓ αxProof of Correctness Theorem.␈↓ ε8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   160

␈↓ ↓H␈↓␈↓ α87␈↓ αxRemarks.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   162

␈↓ ↓H␈↓␈↓ α88␈↓ αxSome substantial exercises.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   163


␈↓ ↓H␈↓IX␈↓ α_COMPILING IN LISP

␈↓ ↓H␈↓␈↓ α81␈↓ αxSome facts about the PDP-10.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   165

␈↓ ↓H␈↓␈↓ α82␈↓ αxCode produced by LISP compilers.␈↓ ε8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   167

␈↓ ↓H␈↓␈↓ α83␈↓ αxLCOM0.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   172

␈↓ ↓H␈↓␈↓ α84␈↓ αxLCOM4.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   176


␈↓ ↓H␈↓X␈↓ α_COMPUTABILITY

␈↓ ↓H␈↓␈↓ α81␈↓ αxA Call-by-name LISP interpreter.␈↓ ε8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   182

␈↓ ↓H␈↓␈↓ α82␈↓ αxNon-computability.␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   183
␈↓ ↓H␈↓␈↓ ¬RTable of Contents␈↓ 7v


␈↓ ↓H␈↓BIBLIOGRAPHY


␈↓ ↓H␈↓A␈↓ α_LISP Compilers

␈↓ ↓H␈↓␈↓ α81␈↓ αxLCOM0: listing of MACLISP version.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ !   i

␈↓ ↓H␈↓␈↓ α82␈↓ αxLCOM4: listing of MACLISP version.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ ⊂   iv


␈↓ ↓H␈↓FUNCTION INDEX
␈↓ ↓H␈↓␈↓ εH␈↓ ?i


␈↓ ↓H␈↓α␈↓ ¬IINTRODUCTION


␈↓ ↓H␈↓        This␈α
book␈α
covers␈α∞recursive␈α
programming␈α
in␈α∞LISP,␈α
computation␈α
with␈α∞symbolic␈α
expressions
␈↓ ↓H␈↓represented␈α∀by␈α∀LISP␈α∀S-expressions,␈α∀representation␈α∀of␈α∀S-expressions␈α∀by␈α∀list␈α∀structure␈α∀in␈α∀the
␈↓ ↓H␈↓memory␈α⊃of␈α⊃a␈α⊃computer,␈α⊃and␈α⊃techniques␈α⊂for␈α⊃mathematically␈α⊃proving␈α⊃that␈α⊃programs␈α⊃meet␈α⊂their
␈↓ ↓H␈↓specifications.  We cover the following main topics.

␈↓ ↓H␈↓1.␈α∩Writing␈α∪recursive␈α∩programs.␈α∪ Most␈α∩students␈α∩will␈α∪have␈α∩already␈α∪learned␈α∩to␈α∪write␈α∩sequential
␈↓ ↓H␈↓programs␈α∂which␈α⊂change␈α∂an␈α⊂initial␈α∂state␈α⊂of␈α∂a␈α⊂computation␈α∂to␈α⊂reach␈α∂a␈α⊂state␈α∂satisfying␈α⊂a␈α∂desired
␈↓ ↓H␈↓condition.␈α Recursive␈αprogramming␈αrequires␈αinventing␈αfunctions␈αthat␈αgive␈αthe␈αanswer␈αin␈αterms␈αof
␈↓ ↓H␈↓the␈α
initial␈αinformation␈α
by␈αbuilding␈α
it␈αup␈α
from␈αalready␈α
available␈αfunctions␈α
and␈αsimpler␈α
cases␈αof␈α
the
␈↓ ↓H␈↓function␈αbeing␈αdefined.␈α Either␈αkind␈αof␈αprogram␈αis␈αuniversal,␈αand␈αthe␈αtwo␈αprogramming␈αstyles␈α
are
␈↓ ↓H␈↓complementary.␈α∪ The␈α∀kind␈α∪of␈α∪recursion␈α∀that␈α∪makes␈α∪good␈α∀programs␈α∪is␈α∀␈↓↓conditional␈α∪expression
␈↓ ↓H␈↓↓recursion␈↓␈α→which␈α_differs␈α→in␈α→important␈α_respects␈α→from␈α→the␈α_recursive␈α→definitions␈α→familiar␈α_to
␈↓ ↓H␈↓mathematicians.

␈↓ ↓H␈↓2.␈α
Computing␈α
with␈α
symbolic␈α
expressions␈α
represented␈α
by␈α
list␈α
structure␈α
in␈α
the␈α
memory␈α
of␈α
a␈α
computer.
␈↓ ↓H␈↓The␈αLISP␈αS-expressions␈αform␈αa␈αdata␈αdomain␈αwith␈αa␈αsimple␈αand␈αregular␈αstructure,␈αand␈αimportant
␈↓ ↓H␈↓computations␈α→with␈α→symbolic␈α→expressions␈α→are␈α→readily␈α→represented␈α→as␈α→functions␈α→defined␈α→by
␈↓ ↓H␈↓conditional␈α∩expression␈α∩recursion.␈α∩ The␈α∩examples␈α∩are␈α∩taken␈α∩from␈α∩tree␈α∩search,␈α∩computing␈α∩with
␈↓ ↓H␈↓algebraic␈αand␈αother␈αexpressions␈αinvolving␈αelementary␈αfunctions,␈αpattern␈αmatching,␈αand␈αcompiling,
␈↓ ↓H␈↓interpreting␈α
and␈α
transforming␈α
recursive␈αprograms.␈α
 Recursive␈α
programming␈α
is␈α
the␈αmain␈α
technique,
␈↓ ↓H␈↓but sequential programs are also discussed.

␈↓ ↓H␈↓        The␈α
student␈α∞who␈α
has␈α∞completed␈α
a␈α∞course␈α
based␈α
on␈α∞this␈α
book␈α∞should␈α
be␈α∞able␈α
to␈α∞use␈α
LISP
␈↓ ↓H␈↓proficiently for symbolic computation and in artificial intelligence applications.

␈↓ ↓H␈↓3.␈α_Proving␈α_programs␈α↔correct.␈α_ A␈α_major␈α↔applied␈α_task␈α_of␈α↔computer␈α_science␈α_is␈α_to␈α↔develop
␈↓ ↓H␈↓mathematical␈α
theory␈α
of␈α
computation␈α
to␈α
the␈αpoint␈α
where␈α
a␈α
program␈α
is␈α
not␈α
considered␈αcomplete␈α
until
␈↓ ↓H␈↓it␈αhas␈α
been␈αproven␈α
to␈αmeet␈α
its␈αspecification␈αand␈α
this␈αproof␈α
has␈αbeen␈α
checked␈αby␈α
a␈αproof-checker
␈↓ ↓H␈↓program.␈α∞ In␈α
particular,␈α∞a␈α
university␈α∞level␈α∞course␈α
in␈α∞programming␈α
for␈α∞computer␈α∞science␈α
students
␈↓ ↓H␈↓should teach them to prove correct the programs they write in the course.

␈↓ ↓H␈↓        While␈αprogram␈α
proving␈αhas␈α
not␈αadvanced␈α
to␈αthe␈α
point␈αwhere␈α
it␈αcan␈α
replace␈αdebugging␈α
most
␈↓ ↓H␈↓programs, it has advanced to the point where it should be studied by all comptter science students.

␈↓ ↓H␈↓        This␈α
book␈α
includes␈α
techniques␈α
for␈αproving␈α
the␈α
extensional␈α
correctness␈α
of␈αrecursive␈α
programs
␈↓ ↓H␈↓in␈αpure␈αLISP,␈αand␈αmost␈αof␈αthe␈αprogramming␈αexercises␈αare␈αwithin␈αeasy␈αrange␈αof␈α
these␈αtechniques.
␈↓ ↓H␈↓Thus␈α∃recent␈α∃Stanford␈α∃examinations␈α∃have␈α∃included␈α∃programming␈α∃problems␈α∃together␈α∃with␈α∃a
␈↓ ↓H␈↓requirement␈α∩that␈α⊃the␈α∩solution␈α⊃be␈α∩proved␈α⊃to␈α∩meet␈α⊃certain␈α∩specifications.␈α⊃ Some␈α∩techniques␈α⊃are
␈↓ ↓H␈↓available␈αfor␈α
non-extensional␈αproperties␈αsuch␈α
as␈αthe␈α
number␈αof␈αoperations␈α
executed,␈αand␈αsome␈α
are
␈↓ ↓H␈↓available for sequential programs.

␈↓ ↓H␈↓        Recursive␈αprograms␈αare␈αrepresented␈αas␈αfunctions␈αin␈αa␈αfirst␈αorder␈αtheory␈αand␈αare␈αdefined␈αby
␈↓ ↓H␈↓a␈α␈↓↓functional␈αequation␈↓␈αwhich␈αis␈αessentially␈αa␈αcopy␈αof␈αthe␈αrecursive␈αdefinition␈αand␈αby␈αa␈α␈↓↓minimization
␈↓ ↓H␈↓↓schema␈↓␈αwhich␈αis␈αa␈αsentence␈αschema␈αof␈αfirst␈αorder␈αlogic␈αwith␈αa␈αfree␈αfunction␈αparameter.␈α When␈αthe
␈↓ ↓H␈↓function␈α
is␈α
total␈αthe␈α
schema␈α
can␈α
be␈αdispensed␈α
with.␈α
 The␈αextensional␈α
properties␈α
proved␈α
are␈αthen
␈↓ ↓H␈↓just␈α∪sentences␈α∪in␈α∪first␈α∀order␈α∪logic,␈α∪and␈α∪its␈α∪familiar␈α∀apparatus␈α∪is␈α∪available␈α∪for␈α∀proofs.␈α∪ The
␈↓ ↓H␈↓technique␈α∩is␈α∩much␈α∩more␈α∩direct␈α∩and␈α∩easier␈α∩to␈α∩use␈α∩than␈α∩the␈α∩methods␈α∩available␈α∩for␈α⊃sequential
␈↓ ↓H␈↓programs such as ␈↓↓inductive assertions␈↓ and the Hoare axiomatization.
␈↓ ↓H␈↓ii␈↓ ¬FINTRODUCTION␈↓ H


␈↓ ↓H␈↓        In␈αfact␈αthe␈αproofs␈αare␈αreadily␈αcomputer-checked␈αby␈αRichard␈αWeyhrauch's␈αfirst␈αorder␈αproof-
␈↓ ↓H␈↓checker␈α∂FOL.␈α∞ However,␈α∂FOL␈α∞has␈α∂many␈α∂conventions␈α∞and␈α∂runs␈α∞rather␈α∂slowly,␈α∞so␈α∂we␈α∂didn't␈α∞feel
␈↓ ↓H␈↓justified␈αin␈αrequiring␈αcomputer␈αchecking␈αof␈αthe␈αproofs␈αin␈αthe␈αcourse␈αon␈αwhich␈αthis␈αbook␈αis␈αbased.
␈↓ ↓H␈↓We␈α∂may␈α∂be␈α⊂able␈α∂to␈α∂include␈α∂computer␈α⊂proof-checking␈α∂in␈α∂a␈α∂future␈α⊂version␈α∂of␈α∂the␈α∂course␈α⊂and␈α∂a
␈↓ ↓H␈↓future edition of the book.

␈↓ ↓H␈↓        Additional␈αtheoretical␈αtopics␈αinclude␈αthe␈αuniversal␈αLISP␈αfunction,␈α␈↓↓eval␈↓␈αwhich␈αalso␈αserves␈αas
␈↓ ↓H␈↓a␈αLISP␈αinterpreter,␈α␈↓↓abstract␈αsyntax␈↓␈αwhich␈αenables␈αconvenient␈αproofs␈αof␈αthe␈αcorrectness␈αof␈αa␈αsimple
␈↓ ↓H␈↓compiler, and a slight discussion of computability.

␈↓ ↓H␈↓        An additional applied topic is syntax directed computation.
␈↓ ↓H␈↓␈↓ εH␈↓ 91


␈↓ ↓H␈↓α␈↓ επChapter I

␈↓ ↓H␈↓α␈↓ ¬εINTRODUCTION TO LISP


␈↓ ↓H␈↓        LISP␈α⊂is␈α⊂a␈α∂language␈α⊂for␈α⊂writing␈α∂programs␈α⊂that␈α⊂do␈α∂symbolic␈α⊂computation.␈α⊂ Information␈α∂is
␈↓ ↓H␈↓coded␈α∩or␈α∩represented␈α∩as␈α∩S-expressions.␈α∩ A␈α∩LISP␈α∩program␈α∩can␈α∩also␈α∩be␈α∩represented␈α∩as␈α∩an␈α∩S-
␈↓ ↓H␈↓expression.␈α∞ This␈α∂gives␈α∞LISP␈α∞the␈α∂special␈α∞ability␈α∞to␈α∂easily␈α∞manipulate␈α∞programs␈α∂as␈α∞well␈α∂as␈α∞other
␈↓ ↓H␈↓sorts␈α∞of␈α∞symbolic␈α∂data.␈α∞ In␈α∞this␈α∂chapter␈α∞we␈α∞describe␈α∂S-expressions,␈α∞show␈α∞how␈α∂S-expressions␈α∞are
␈↓ ↓H␈↓represented␈α⊂in␈α⊃a␈α⊂computer␈α⊂and␈α⊃give␈α⊂the␈α⊂basic␈α⊃functions␈α⊂and␈α⊂predicates␈α⊃on␈α⊂the␈α⊂domain␈α⊃of␈α⊂S-
␈↓ ↓H␈↓expressions.␈α We␈αthen␈αdescribe␈αthe␈αbasic␈αconstructs␈αof␈αLISP␈αand␈αshow␈αhow␈αthey␈αare␈αused␈αto␈αform
␈↓ ↓H␈↓LISP programs.



␈↓ ↓H␈↓1.  ␈↓αLists.␈↓


␈↓ ↓H␈↓        Before␈αgiving␈αa␈α
formal␈αdefinition␈αof␈α
S-expressions,␈αwe␈αshall␈α
give␈αsome␈αexamples.␈α The␈α
most
␈↓ ↓H␈↓common␈α∂form␈α∞of␈α∂S-expression␈α∞is␈α∂the␈α∂list,␈α∞and␈α∂Figure␈α∞1␈α∂shows␈α∂some␈α∞lists.␈α∂ The␈α∞list␈α∂(i)␈α∂has␈α∞four
␈↓ ↓H␈↓elements.␈α
 The␈α
list␈α(ii)␈α
has␈α
four␈αelements␈α
one␈α
of␈αwhich␈α
is␈α
itself␈αa␈α
list.␈α
 The␈αlist␈α
(iii)␈α
has␈αone␈α
element.
␈↓ ↓H␈↓The␈α∞list␈α∞(iv)␈α∞also␈α∞has␈α∞one␈α∞element␈α∞which␈α∞itself␈α∞is␈α∞a␈α∞list.␈α∞ The␈α∞list␈α∞(v)␈α∞has␈α∞no␈α∞elements;␈α∞it␈α∞is␈α∞also
␈↓ ↓H␈↓written ␈↓¬NIL␈↓.


␈↓ ↓H␈↓␈↓ ¬_(i)␈↓ ε8␈↓¬(A B C E)    ␈↓
␈↓ ↓H␈↓␈↓ ¬_(ii)␈↓ ε8␈↓¬(A B (C D) E)␈↓
␈↓ ↓H␈↓␈↓ ¬_(iii)␈↓ ε8␈↓¬(A)          ␈↓
␈↓ ↓H␈↓␈↓ ¬_(iv)␈↓ ε8␈↓¬((A B C D))  ␈↓
␈↓ ↓H␈↓␈↓ ¬_(v)␈↓ ε8␈↓¬()           ␈↓

␈↓ ↓H␈↓␈↓ ¬∪␈↓αFigure 1.␈↓  Examples of lists.



␈↓ ↓H␈↓        Figure␈α∩2␈α∪shows␈α∩how␈α∩symbolic␈α∪information␈α∩can␈α∩be␈α∪represented␈α∩by␈α∩lists.␈α∪ Each␈α∩example
␈↓ ↓H␈↓consists␈αof␈αa␈α
list␈αand␈αa␈α
"mathematical"␈αrepresentation␈αof␈α
the␈αsame␈αinformation.␈α
 Examples␈α(i)-(iv)
␈↓ ↓H␈↓represent␈α⊂familiar␈α⊂mathematical␈α⊂and␈α⊃logical␈α⊂expressions.␈α⊂ The␈α⊂list␈α⊃(v)␈α⊂is␈α⊂used␈α⊂to␈α⊃represent␈α⊂the
␈↓ ↓H␈↓network␈α∩or␈α∩graph␈α∩shown␈α∩according␈α∩to␈α∩a␈α∩scheme␈α∩whereby␈α∩there␈α∩is␈α∩a␈α∩sublist␈α∩for␈α∩each␈α∩vertex
␈↓ ↓H␈↓consisting of the vertex itself followed by the vertices to which it is connected.

␈↓ ↓H␈↓        The␈αelements␈αof␈αa␈αlist␈αare␈αsurrounded␈αby␈αparentheses␈αand␈αseparated␈αby␈αspaces.␈α A␈α
list␈αmay
␈↓ ↓H␈↓have␈α
any␈αnumber␈α
of␈α
terms␈αand␈α
any␈αof␈α
these␈α
terms␈αmay␈α
themselves␈αbe␈α
lists.␈α
 In␈αthis␈α
case,␈αthe␈α
spaces
␈↓ ↓H␈↓surrounding␈α∞a␈α∞sublist␈α∞may␈α∂be␈α∞omitted,␈α∞and␈α∞extra␈α∞spaces␈α∂between␈α∞elements␈α∞of␈α∞a␈α∞list␈α∂are␈α∞allowed.
␈↓ ↓H␈↓Thus the lists  ␈↓¬(A B(C  D)    E)␈↓   and  ␈↓¬(A B (C D) E)␈↓  are regarded as the same.
␈↓ ↓H␈↓2␈↓ εβChapter  I␈↓ H





␈↓ ↓H␈↓␈↓ αX(i)␈↓ βx␈↓¬(PLUS X Y)␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓x + y␈↓

␈↓ ↓H␈↓␈↓ αX(ii)␈↓ βx␈↓¬(PLUS (TIMES X Y) X 3)␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓xy + x + 3␈↓.

␈↓ ↓H␈↓␈↓ αX(iii)␈↓ βx␈↓¬(EXIST X (ALL Y (IMPLIES (P X) (P Y))))␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓∃x: ∀y: [P(x)⊃P(y)]␈↓.

␈↓ ↓H␈↓␈↓ αX(iv)␈↓ βx␈↓¬(INTEGRAL 0 ∞ (TIMES (EXP (TIMES I X Y)) (F X)) X)␈↓
␈↓ ↓H␈↓␈↓ βx␈↓π&␈↓β0␈↓∧␈↓#
∞␈↓#␈↓↓e␈↓∧ixy␈↓↓f(x)dx␈↓.

␈↓ ↓H␈↓␈↓ αX(v)␈↓ βx␈↓¬((A B) (B A C D) (C B D E) (D B C E) (E C D F) (F E))␈↓

␈↓"␈↓ ↓H␈↓␈↓ ε8C
␈↓"␈↓ ↓H␈↓␈↓ ε_≤'~`≥
␈↓"␈↓ ↓H␈↓␈↓ ¬x≤'  ~  `≥
␈↓"␈↓ ↓H␈↓␈↓ ¬8B ≤'    ~    `≥ E
␈↓"␈↓ ↓H␈↓␈↓ ∧(A αααααααα␈↓ ¬H'␈↓ ¬H≥␈↓ ε8~␈↓ π(≤␈↓ π(`αααααααα F
␈↓"␈↓ ↓H␈↓␈↓ ¬X`≥    ~    ≤'
␈↓"␈↓ ↓H␈↓␈↓ ¬x`≥  ~  ≤'
␈↓"␈↓ ↓H␈↓␈↓ ε_`≥~≤'
␈↓"␈↓ ↓H␈↓␈↓ ε8D


␈↓ ↓H␈↓␈↓ ∧5␈↓αFigure 2.␈↓  Information represented as lists.





␈↓ ↓H␈↓2.  ␈↓αAtoms.␈↓


␈↓ ↓H␈↓        The␈α
expressions␈α
␈↓¬A,␈α
B,␈α∞X,␈α
Y,␈α
3,␈α
PLUS␈↓,␈α∞and␈α
␈↓¬ALL␈↓␈α
occurring␈α
in␈α
the␈α∞lists␈α
in␈α
Figures␈α
1␈α∞and␈α
2
␈↓ ↓H␈↓are␈α∂called␈α∂atoms.␈α∞ In␈α∂general,␈α∂an␈α∞atom␈α∂is␈α∂expressed␈α∞by␈α∂a␈α∂sequence␈α∞of␈α∂capital␈α∂letters,␈α∂digits,␈α∞and
␈↓ ↓H␈↓special␈αcharacters␈αwith␈αcertain␈αexclusions.␈α The␈αexclusions␈αare␈α<space>,␈α<carriage␈αreturn>,␈αand␈αthe
␈↓ ↓H␈↓other␈α⊗non-printing␈α⊗characters,␈α∃and␈α⊗also␈α⊗the␈α∃parentheses,␈α⊗brackets,␈α⊗semi-colon,␈α⊗and␈α∃comma.
␈↓ ↓H␈↓Numbers␈α
are␈α∞expressed␈α
as␈α∞signed␈α
decimal␈α∞or␈α
octal␈α∞numbers,␈α
the␈α∞exact␈α
convention␈α∞depending␈α
on
␈↓ ↓H␈↓the␈α↔implementation.␈α↔ Floating␈α↔point␈α↔numbers␈α↔are␈α↔written␈α↔with␈α↔decimal␈α↔points␈α_and,␈α↔when
␈↓ ↓H␈↓appropriate,␈αan␈αexponent␈αnotation␈αdepending␈α
on␈αthe␈αimplementation.␈α The␈αreader␈α
should␈αconsult
␈↓ ↓H␈↓the programmer's manual for the LISP implementation he intends to use.

␈↓ ↓H␈↓        Some further examples of atoms are
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ 93



␈↓ ↓H␈↓␈↓ ¬X␈↓¬THE-LAST-TRUMP␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓¬A307B         ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓¬345           ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓¬-47           ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓¬-45.21        ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓¬3.14159,      ␈↓

␈↓ ↓H␈↓␈↓ ¬¬␈↓αFigure 3.␈↓  Examples of atoms.



␈↓ ↓H␈↓and from these we can form lists like ␈↓¬((345 3.14159 -47) A307B THE-LAST-TRUMP -45.21)␈↓.



␈↓ ↓H␈↓3.  ␈↓αList structures.␈↓


␈↓ ↓H␈↓        Lists␈αare␈αrepresented␈αin␈αthe␈αmemory␈αof␈αthe␈αcomputer␈αby␈αlist␈αstructures.␈α A␈αlist␈αstructure␈αis␈αa
␈↓ ↓H␈↓collection␈αof␈αmemory␈α
words␈αeach␈αof␈α
which␈αis␈αdivided␈αinto␈α
two␈αparts,␈αand␈α
each␈αpart␈αis␈α
capable␈αof
␈↓ ↓H␈↓containing␈αan␈αaddress␈αin␈αmemory.␈αThe␈αtwo␈αparts␈αare␈αcalled␈αare␈αcalled␈αthe␈αa-part␈αand␈α
the␈αd-part.
␈↓ ↓H␈↓There␈α
is␈α∞one␈α
computer␈α∞word␈α
for␈α
each␈α∞element␈α
of␈α∞the␈α
list.␈α
 The␈α∞a-part␈α
of␈α∞the␈α
word␈α∞contains␈α
the
␈↓ ↓H␈↓address␈α∞of␈α
the␈α∞list␈α
or␈α∞atom␈α
representing␈α∞the␈α
element␈α∞and␈α
the␈α∞d-part␈α
contains␈α∞the␈α
address␈α∞of␈α
the
␈↓ ↓H␈↓word␈α
representing␈α
the␈αnext␈α
element␈α
of␈α
the␈αlist.␈α
 If␈α
the␈α
list␈αelement␈α
is␈α
itself␈α
a␈αlist,␈α
then,␈α
of␈αcourse,␈α
the
␈↓ ↓H␈↓address␈αof␈αthe␈αfirst␈αword␈αof␈αits␈αlist␈α
structure␈αis␈αgiven␈αin␈αthe␈αa-part␈αof␈αthe␈αword␈α
representing␈αthat
␈↓ ↓H␈↓element.␈α
 A␈α
diagram␈α
shows␈α
this␈α
more␈α
clearly␈α
than␈α
words,␈α
and␈α
the␈α
list␈α
structure␈α
corresponding␈α
to
␈↓ ↓H␈↓the␈α∞list␈α∞  ␈↓¬(PLUS (TIMES X Y) X 3)␈↓  which␈α∂may␈α∞represent␈α∞the␈α∞expression␈α∂  ␈↓↓xy + x + 3␈↓  is␈α∞shown
␈↓ ↓H␈↓in␈α∞Figure 4.␈α∞ A␈α∞partial␈α∞dump␈α∞of␈α∞the␈α∞memory␈α∞of␈α∞a␈α∞computer␈α∞containing␈α∞this␈α∞list␈α∂structure␈α∞might
␈↓ ↓H␈↓look as shown in Figure 5.
␈↓"
␈↓"
␈↓"␈↓ ↓H␈↓            ⊂αααπααα⊃     ⊂αααπααα⊃     ⊂αααπααα⊃     ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓      ααααα→~   ~   εαααα→~   ~   εαααα→~   ~   εαααα→~   ~   εααααααα⊃
␈↓"␈↓ ↓H␈↓            %απα∀ααα$     %απα∀ααα$     %απα∀ααα$     %απα∀ααα$       ~
␈↓"␈↓ ↓H␈↓              ↓             ~             ~             ↓             ~
␈↓"␈↓ ↓H␈↓             PLUS           ~             ~             3             ~
␈↓"␈↓ ↓H␈↓                            ~  ⊂αααπααα⊃  ~  ⊂αααπααα⊃     ⊂αααπααα⊃  ~
␈↓"␈↓ ↓H␈↓                            %α→~   ~   εααβα→~   ~   εαααα→~   ~   ~  ~
␈↓"␈↓ ↓H␈↓                               %απα∀ααα$  ~  %απα∀ααα$     %απα∀απα$  ~
␈↓"␈↓ ↓H␈↓                                 ↓        ~    ~             ↓   ~    ~
␈↓"␈↓ ↓H␈↓                               TIMES      %απαα$             Y   %ααπα$
␈↓"␈↓ ↓H␈↓                                            ↓                       ↓
␈↓"␈↓ ↓H␈↓                                            X                      NIL
␈↓"
␈↓ ↓H␈↓␈↓ αd␈↓αFigure 4.␈↓  Representation of ␈↓¬(PLUS (TIMES X Y) X 3)␈↓ as a list structure.␈↓¬



␈↓ ↓H␈↓        Atoms␈αare␈αrepresented␈αby␈αthe␈αaddresses␈αof␈αtheir␈αproperty␈αlists␈αwhich␈αare␈αlist␈αstructures␈αof␈αa
␈↓ ↓H␈↓special␈αkind␈αdepending␈α
on␈αthe␈αimplementation.␈α
 Thus␈α␈↓X␈↓␈αdenotes␈α
the␈αaddress␈αof␈α
the␈αproperty␈αlist␈α
of
␈↓ ↓H␈↓the␈αatom␈αnamed␈α␈↓¬X␈α␈↓and␈α␈↓/3␈↓␈αdenotes␈αthat␈αof␈αthe␈αatom␈αnamed␈α␈↓¬3␈↓.␈α (In␈αsome␈αimplementations,␈αthe␈αfirst
␈↓ ↓H␈↓word␈αof␈αa␈αproperty␈αlist␈αis␈αin␈αa␈αspecial␈αarea␈αof␈αmemory,␈αin␈αothers␈αthe␈αfirst␈αword␈αis␈αdistinguished␈αby
␈↓ ↓H␈↓4␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓sign,␈α
in␈α∞still␈α
others␈α∞it␈α
has␈α∞a␈α
special␈α∞a-part.␈α
 For␈α∞basic␈α
LISP␈α∞programming,␈α
it␈α∞is␈α
enough␈α∞to␈α
know
␈↓ ↓H␈↓that␈αatoms␈αare␈αdistinguishable␈αfrom␈αother␈αlist␈αstructures␈αby␈αa␈αpredicate␈αcalled␈α␈↓αat␈↓.)␈αNotice␈α
that␈αthe
␈↓ ↓H␈↓addresses␈αof␈αconsecutive␈αlist␈αelements␈αneed␈αnot␈αbe␈αconsecutive.␈α Also␈αthe␈αlast␈αword␈αof␈αa␈αlist␈αcannot
␈↓ ↓H␈↓have␈α
the␈α
address␈α
of␈αa␈α
next␈α
word␈α
in␈α
its␈αd-part␈α
since␈α
there␈α
isn't␈α
any␈αnext␈α
word,␈α
so␈α
it␈α
has␈αthe␈α
address
␈↓ ↓H␈↓of a special atom called ␈↓¬NIL␈↓.

␈↓ ↓H␈↓        address    a-part    d-part        address    a-part    d-part
␈↓ ↓H␈↓        _______    ______    ______        _______    ______    ______

␈↓ ↓H␈↓            86      TIMES       87           1000      PLUS      1001
␈↓ ↓H␈↓            87          X       88           1001        86      1002
␈↓ ↓H␈↓            88          Y      NIL           1002         X      2653
␈↓ ↓H␈↓                                             2653        /3       NIL

␈↓ ↓H␈↓␈↓ βG␈↓αFigure 5.␈↓  A memory map for the list structure of Figure 4.



␈↓ ↓H␈↓        A␈αprogram␈αrefers␈αto␈αa␈αlist␈αby␈αthe␈αaddress␈αof␈αits␈αfirst␈αelement.␈α According␈αto␈αthis␈αconvention,
␈↓ ↓H␈↓we␈α
see␈α
that␈α
the␈α∞a-part␈α
of␈α
a␈α
list␈α∞word␈α
is␈α
the␈α
list␈α∞element␈α
and␈α
the␈α
d-part␈α∞is␈α
a␈α
pointer␈α
to␈α∞a␈α
sublist
␈↓ ↓H␈↓formed␈αby␈α
deleting␈αthe␈αfirst␈α
element.␈α Thus␈αthe␈α
first␈αword␈α
of␈αthe␈αlist␈α
structure␈αof␈αFigure 4␈α
contains
␈↓ ↓H␈↓a␈α⊂pointer␈α⊂to␈α⊂the␈α⊂list␈α⊂structure␈α⊂representing␈α⊃the␈α⊂atom␈α⊂␈↓¬PLUS␈↓,␈α⊂ while␈α⊂its␈α⊂d-part␈α⊂points␈α⊂to␈α⊃the␈α⊂list
␈↓ ↓H␈↓␈↓¬((TIMES X Y) X 3)␈↓.␈α
 The␈α∞second␈α
word␈α∞contains␈α
the␈α
list␈α∞structure␈α
representing␈α∞␈↓¬(TIMES X Y)␈↓␈α
in
␈↓ ↓H␈↓its␈α
a-part␈α
and␈α
the␈α
list␈α
structure␈α
representing␈α
␈↓¬(X 3)␈↓␈α
in␈α
its␈α
d-part.␈α
 The␈α
last␈α
word␈α
points␈α
to␈αthe␈α
atom
␈↓ ↓H␈↓␈↓¬3␈↓␈α⊂in␈α∂its␈α⊂a-part␈α∂and␈α⊂has␈α⊂a␈α∂pointer␈α⊂to␈α∂the␈α⊂atom␈α∂␈↓¬NIL␈↓␈α⊂in␈α⊂its␈α∂d-part.␈α⊂ This␈α∂is␈α⊂consistent␈α⊂with␈α∂the
␈↓ ↓H␈↓convention that ␈↓¬NIL␈↓ represents the null list.



␈↓ ↓H␈↓4.  ␈↓αS-expressions.␈↓


␈↓ ↓H␈↓        When␈α∪we␈α∪examine␈α∩the␈α∪way␈α∪list␈α∩structures␈α∪represent␈α∪lists␈α∩we␈α∪see␈α∪a␈α∪curious␈α∩asymmetry.
␈↓ ↓H␈↓Namely,␈αthe␈αa-part␈αof␈αa␈αlist␈αword␈αcan␈αcontain␈αan␈αatom␈αor␈αa␈αlist,␈αbut␈αthe␈αd-part␈αcan␈αcontain␈αonly␈α
a
␈↓ ↓H␈↓list␈α
or␈αthe␈α
special␈αatom␈α
␈↓¬NIL␈↓.␈α This␈α
restriction␈α
is␈αquite␈α
unnatural␈αfrom␈α
the␈αcomputing␈α
point␈αof␈α
view,
␈↓ ↓H␈↓and␈αwe␈αshall␈α
allow␈αarbitrary␈αatoms␈α
to␈αinhabit␈αthe␈α
d-parts␈αof␈αwords,␈α
but␈αthen␈αwe␈α
must␈αgeneralize
␈↓ ↓H␈↓the␈α
way␈α
list␈αstructures␈α
are␈α
expressed␈αas␈α
character␈α
strings.␈α
To␈αdo␈α
this,␈α
we␈αintroduce␈α
the␈α
notion␈αof␈α
S-
␈↓ ↓H␈↓expression.

␈↓ ↓H␈↓        An␈α∩S-expression␈α∩is␈α∪either␈α∩an␈α∩atom␈α∪or␈α∩a␈α∩pair␈α∩of␈α∪S-expressions␈α∩separated␈α∩by␈α∪" . "␈α∩and
␈↓ ↓H␈↓surrounded by parentheses.  In BNF, we can write

␈↓ ↓H␈↓␈↓ β	        <S-expression> ::= <atom> | (<S-expression> . <S-expression>).
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ 95



␈↓ ↓H␈↓Some examples of S-expressions are

␈↓ ↓H␈↓␈↓ ¬␈↓¬A                        ␈↓
␈↓ ↓H␈↓␈↓ ¬␈↓¬(A . B)                  ␈↓
␈↓ ↓H␈↓␈↓ ¬␈↓¬(A . (B . A))            ␈↓
␈↓ ↓H␈↓␈↓ ¬␈↓¬(PLUS . (X . (Y . NIL))) ␈↓
␈↓ ↓H␈↓␈↓ ¬␈↓¬(3 . 3.4)                ␈↓

␈↓ ↓H␈↓␈↓ ∧R␈↓αFigure 6.␈↓  Examples of S-expressions.



␈↓ ↓H␈↓The␈α
spaces␈αaround␈α
the␈α" . "␈α
may␈αbe␈α
omitted␈α
when␈αthis␈α
will␈αnot␈α
cause␈αconfusion.␈α
 The␈αonly␈α
possible
␈↓ ↓H␈↓confusion␈αis␈αof␈αthe␈αdot␈αseparator␈αwith␈αa␈αdecimal␈αpoint␈αin␈αnumbers.␈α Thus,␈αin␈αthe␈αabove␈αcases,␈αwe
␈↓ ↓H␈↓may␈α⊂write␈α⊃␈↓¬(A.B)␈↓,␈α⊂␈↓¬(A.(B.A))␈↓,␈α⊃and␈α⊂␈↓¬(PLUS.(X.(Y.NIL)))␈↓,␈α⊃but␈α⊂if␈α⊃we␈α⊂wrote␈α⊃␈↓¬(3.3.4)␈↓␈α⊂confusion
␈↓ ↓H␈↓would result.

␈↓ ↓H␈↓        In␈α∂the␈α∂memory␈α∂of␈α∂a␈α∂computer,␈α∂an␈α∂S-expression␈α∂is␈α∂represented␈α∂by␈α∂the␈α∂address␈α∂of␈α⊂a␈α∂word
␈↓ ↓H␈↓whose␈αa-part␈αcontains␈αthe␈αfirst␈αelement␈αof␈αthe␈αpair␈αand␈αwhose␈αd-part␈αcontains␈αthe␈αsecond␈αelement
␈↓ ↓H␈↓of␈α_the␈α_pair.␈α_ Thus,␈α_the␈α_S-expressions␈α_␈↓¬(A.B)␈↓,␈α_␈↓¬(A.(B.A))␈↓,␈α_and␈α_␈↓¬(PLUS.(X.(Y.NIL)))␈↓␈α_are
␈↓ ↓H␈↓represented by the list structures of Figure 7.
␈↓"
␈↓"
␈↓"␈↓ ↓H␈↓            ⊂αααπααα⊃                             ⊂αααπααα⊃     ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓       αααα→~   ~   ~                        αααα→~   ~   εαααα→~   ~   ~
␈↓"␈↓ ↓H␈↓            %απα∀απα$                             %απα∀ααα$     %απα∀απα$
␈↓"␈↓ ↓H␈↓              ↓   ↓                                 ~             ↓   ~
␈↓"␈↓ ↓H␈↓              A   B                                 ~             B   ~
␈↓"␈↓ ↓H␈↓                                                    ~                 ~
␈↓"␈↓ ↓H␈↓                                                    ~                 ~
␈↓"␈↓ ↓H␈↓                                                    %ααααααααπαααααααα$
␈↓"␈↓ ↓H␈↓                                                             ↓
␈↓"␈↓ ↓H␈↓                                                             A
␈↓"
␈↓"␈↓ ↓H␈↓            ⊂αααπααα⊃     ⊂αααπααα⊃     ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓       αααα→~   ~   εαααα→~   ~   εαααα→~   ~   ~
␈↓"␈↓ ↓H␈↓            %απα∀ααα$     %απα∀ααα$     %απα∀απα$
␈↓"␈↓ ↓H␈↓              ↓             ↓             ↓   ↓
␈↓"␈↓ ↓H␈↓             PLUS           X             Y  NIL
␈↓"
␈↓"
␈↓ ↓H␈↓␈↓ βD␈↓αFigure 7.␈↓  Representation of S-expressions as list structures.



␈↓ ↓H␈↓        Note␈α∩that␈α∩the␈α∩list␈α∩␈↓¬(PLUS X Y)␈↓␈α∩and␈α∩the␈α∩S-expression␈α∩ ␈↓¬(PLUS . (X . (Y . NIL)))␈↓␈α∩are
␈↓ ↓H␈↓represented␈αin␈α
memory␈αby␈α
the␈αsame␈α
list␈αstructure.␈α
 The␈αsimplest␈α
way␈αto␈α
treat␈αthis␈α
is␈αto␈α
regard␈αS-
␈↓ ↓H␈↓expressions␈α⊂as␈α⊂primary␈α⊂and␈α⊂lists␈α⊂as␈α∂abbreviations␈α⊂for␈α⊂certain␈α⊂S-expressions,␈α⊂namely␈α⊂those␈α∂that
␈↓ ↓H␈↓never␈α
have␈α
any␈α
atom␈α
but␈α
␈↓¬NIL␈↓␈α
as␈α
the␈αsecond␈α
part␈α
of␈α
a␈α
pair.␈α
 In␈α
general,␈α
the␈α
list␈α␈↓¬(A B . . . Z)␈↓
␈↓ ↓H␈↓may␈α?␈α⊂be␈α?␈α∂regarded␈α?␈α⊂as␈α?␈α∂an␈α?␈α⊂abbreviation␈α?␈α∂of␈α?␈α⊂the␈α?␈α∂S-expression
␈↓ ↓H␈↓␈↓¬(A . (B . (C . (... (Z . NIL) ...)))␈↓.␈α⊃ In␈α⊂particular␈α⊃the␈α⊃a-part␈α⊂of␈α⊃a␈α⊃list␈α⊂can␈α⊃be␈α⊃any␈α⊂S-
␈↓ ↓H␈↓expression,␈αbut␈αthe␈αd-part␈α
of␈αa␈αlist␈αmust␈αbe␈α
a␈αlist␈αand␈αand␈αthe␈α
only␈αatomic␈αlist␈αis␈α␈↓¬NIL␈↓.␈α
 In␈αgiving
␈↓ ↓H␈↓input␈α
to␈α∞LISP,␈α
either␈α∞the␈α
list␈α∞form␈α
or␈α∞the␈α
S-expression␈α∞form␈α
may␈α∞be␈α
used␈α∞for␈α
lists.␈α∞ On␈α
output,
␈↓ ↓H␈↓LISP␈αwill␈αprint␈α
a␈αlist␈αstructure␈α
as␈αa␈αlist␈α
as␈αfar␈αas␈α
it␈αcan,␈αotherwise␈α
as␈αan␈αS-expression.␈α Thus,␈α
some
␈↓ ↓H␈↓6␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓list␈α
structures␈αwill␈α
be␈αprinted␈α
in␈αa␈α
mixed␈αnotation,␈α
e.g.␈α␈↓¬((A . B) (C . D) (3))␈↓.␈α
 Some␈αLISPs␈α
use
␈↓ ↓H␈↓and␈α
"extended"␈αlist␈α
notation␈α
for␈αprinting.␈α
 In␈αthis␈α
notation␈α
the␈αprint␈α
routine␈α␈↓↓cdr␈↓s␈α
through␈α
the␈αS-
␈↓ ↓H␈↓expression␈α⊂printing␈α⊂the␈α⊂␈↓↓car␈↓␈α∂at␈α⊂each␈α⊂step␈α⊂as␈α∂though␈α⊂it␈α⊂were␈α⊂printing␈α∂a␈α⊂list.␈α⊂ When␈α⊂an␈α⊂atom␈α∂is
␈↓ ↓H␈↓reached␈αif␈αthe␈αatom␈αis␈α␈↓¬NIL␈↓␈αthe␈αprinting␈αis␈α
terminated␈αwith␈α")"␈αas␈αusual,␈α other␈αwise␈αa␈α" . "␈α
followed
␈↓ ↓H␈↓by␈α→the␈α~atom␈α→followed␈α~by␈α→")"␈α→is␈α~printed.␈α→ Thus␈α~␈↓¬(A . (B . (C . D)))␈↓␈α→would␈α~print␈α→as
␈↓ ↓H␈↓␈↓¬(A B C . D)␈↓.


␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓        1.␈α≠If␈α≠we␈α≠represent␈α≠sums␈α≠and␈α≠products␈α≠as␈α≠indicated␈α≠above␈α≠and␈α≠use␈α~␈↓¬(MINUS X)␈↓,
␈↓ ↓H␈↓␈↓¬(QUOTIENT X Y)␈↓, and ␈↓¬(POWER X Y)␈↓ as representations of ␈↓↓-x,␈↓ ␈↓↓x/y,␈↓ and ␈↓↓x␈↓∧y␈↓ respectively, then

␈↓ ↓H␈↓                a. What do the following lists represent?

␈↓ ↓H␈↓␈↓ ∧␈↓↓␈↓¬(QUOTIENT 2 (POWER (PLUS X (MINUS Y)) 3))␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βp␈↓↓␈↓¬(PLUS -2 (MINUS 2) (TIMES X (POWER Y 3.3)))␈↓↓␈↓

␈↓ ↓H␈↓                b. How are the expressions ␈↓↓xyz+3(u+v)␈↓∧-3␈↓ and ␈↓↓(xy-yx)/(xy+yx)␈↓ to be represented?

␈↓ ↓H␈↓        2. In the above mentioned graph notation, what graph is represented by the list

␈↓ ↓H␈↓␈↓ αZ␈↓¬((A D E F) (B D E F) (C D E F) (D A B C) (E A B C) (F A B C))␈↓?

␈↓ ↓H␈↓        3.␈α∪Write␈α∀the␈α∪list␈α∪␈↓¬((PLUS␈α∀(TIMES␈α∪X␈α∪Y)␈α∀X␈α∪3)␈↓␈α∪as␈α∀an␈α∪S-expression.␈α∪ This␈α∀is␈α∪sometimes
␈↓ ↓H␈↓referred to as "dot-notation".

␈↓ ↓H␈↓        4.  Write the following S-expressions in list notation to whatever extent is possible:

␈↓ ↓H␈↓                a. ␈↓¬(A . NIL)␈↓
␈↓ ↓H␈↓                b. ␈↓¬(A . B)␈↓
␈↓ ↓H␈↓                c. ␈↓¬((A . NIL) . B)␈↓
␈↓ ↓H␈↓                d. ␈↓¬((A . B) . ((C . D) . NIL)␈↓.

␈↓ ↓H␈↓        5.␈α
How␈α
would␈α
you␈α
represent␈α
polynomials␈α
in␈αone␈α
variable␈α
as␈α
lists?␈α
 Can␈α
you␈α
think␈α
of␈αmore
␈↓ ↓H␈↓that␈α
one␈α∞way?␈α
 How␈α∞would␈α
you␈α∞represent␈α
polynomials␈α∞in␈α
several␈α∞variables?␈α
 Can␈α∞you␈α
tell␈α∞if␈α
two
␈↓ ↓H␈↓lists represent the same polynomial?

␈↓ ↓H␈↓        6.␈αProve␈αthat␈αthe␈αnumber␈αof␈α"shapes"␈αof␈αS-expressions␈αwith␈α␈↓↓n␈↓␈αatoms␈αis␈α␈↓↓(2n␈α-␈α2)!/(n!(n␈α-␈α1)!)␈↓.
␈↓ ↓H␈↓(The two shapes of S-expressions with three atoms are ␈↓¬(A.(B.C))␈↓ and ␈↓¬((A.B).C)␈↓.

␈↓ ↓H␈↓        7.␈αThe␈αabove␈αresult␈αcan␈αalso␈αbe␈αobtained␈αby␈αwriting␈α␈↓↓S = A + S␈↓π#␈↓↓S␈↓␈αas␈αan␈α"equation"␈αsatisfied
␈↓ ↓H␈↓by␈α∞the␈α∞set␈α∞of␈α
S-expressions␈α∞with␈α∞the␈α∞interpretation␈α
that␈α∞an␈α∞S-expression␈α∞is␈α
either␈α∞an␈α∞atom␈α∞or␈α
a
␈↓ ↓H␈↓pair␈αof␈α
S-expressions.␈α The␈αnext␈α
step␈αis␈α
to␈αregard␈αthe␈α
equation␈αas␈αthe␈α
quadratic␈α␈↓↓S␈↓∧2␈↓↓ - S + A␈α
=␈α0␈↓,
␈↓ ↓H␈↓solve␈α
it␈α
by␈α
the␈α
quadratic␈α
formula␈α
choosing␈α
the␈α
minus␈α
sign␈α
for␈α
the␈α
square␈α
root.␈α
 Then␈α
the␈αradical␈α
is
␈↓ ↓H␈↓regarded␈α
as␈α
the␈α
1/2␈α
power␈α
and␈α∞expanded␈α
by␈α
the␈α
binomial␈α
theorem.␈α
 Manipulating␈α∞the␈α
binomial
␈↓ ↓H␈↓coefficients␈α∞yields␈α∞the␈α∂above␈α∞formula␈α∞as␈α∞the␈α∂coefficient␈α∞of␈α∞␈↓↓A␈↓∧n␈↓␈α∞in␈α∂the␈α∞expansion.␈α∞ Why␈α∂does␈α∞this
␈↓ ↓H␈↓somewhat ill-motivated and irregular procedure give the right answer?

␈↓ ↓H␈↓        (The last two exercises are unconnected with subsequent material in this book.)
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ 97


␈↓ ↓H␈↓5.  ␈↓αThe basic functions and predicates of LISP.␈↓


␈↓ ↓H␈↓        A␈α∞LISP␈α∞function␈α∞or␈α∂predicate␈α∞is␈α∞a␈α∞function␈α∞or␈α∂predicate␈α∞on␈α∞the␈α∞domain␈α∂of␈α∞S-expressions
␈↓ ↓H␈↓which␈α∞is␈α∞either␈α∞one␈α∞of␈α∞the␈α∞basic␈α∞functions␈α∞or␈α∞predicates␈α∞described␈α∞in␈α∞this␈α∞section,␈α∞or␈α∂built␈α∞from
␈↓ ↓H␈↓these␈α
using␈α
methods␈αdescribed␈α
later␈α
in␈αthis␈α
chapter.␈α
 This␈α
is␈αanalogous␈α
to␈α
numeric␈αfunctions␈α
which
␈↓ ↓H␈↓are␈α
based␈αon␈α
the␈αfour␈α
arithmetic␈α
operations␈αof␈α
addition␈αsubtraction,␈α
multiplication,␈α
and␈αdivision,
␈↓ ↓H␈↓and the arithmetic predicates of equality and comparison.

␈↓ ↓H␈↓        In␈αthe␈α
remainder␈αof␈α
this␈αchapter␈α
we␈αwill␈αbe␈α
concerned␈αwith␈α
describing␈αLISP␈α
functions.␈α In
␈↓ ↓H␈↓this␈α∞section␈α∞we␈α∂explain␈α∞the␈α∞basic␈α∂predicates␈α∞and␈α∞functions␈α∞for␈α∂testing,␈α∞taking␈α∞apart␈α∂and␈α∞putting
␈↓ ↓H␈↓together␈α∃S-expressions.␈α∀ Later␈α∃we␈α∀show␈α∃how␈α∃to␈α∀build␈α∃expressions␈α∀and␈α∃definitions␈α∃of␈α∀more
␈↓ ↓H␈↓complicated functions from these basic operations.

␈↓ ↓H␈↓        We␈αwill␈α
use␈αtwo␈α
languages␈α-␈α␈↓↓external␈↓␈α
␈↓↓language␈↓␈αand␈α
␈↓↓internal␈↓␈α␈↓↓language.␈↓␈α
 External␈αlanguage
␈↓ ↓H␈↓is␈αused␈αfor␈αexpressing␈αLISP␈αfunction␈αdefinitions␈α(programs)␈αin␈αa␈αform␈αthat␈αis␈αeasier␈αfor␈αpeople␈αto
␈↓ ↓H␈↓read␈α⊃and␈α⊃write.␈α⊃ However␈α⊃external␈α⊃language␈α⊃function␈α⊃descriptions␈α⊃are␈α⊃not␈α⊃S-expressions,␈α⊃and
␈↓ ↓H␈↓therefore␈α∩it␈α∩is␈α∩not␈α∪easy␈α∩to␈α∩write␈α∩LISP␈α∩programs␈α∪to␈α∩generate,␈α∩interpret,␈α∩compile␈α∪or␈α∩otherwise
␈↓ ↓H␈↓manipulate␈α
functions␈αdefined␈α
in␈α
external␈αlanguage.␈α
 Internal␈αlanguage␈α
represents␈α
LISP␈αprograms
␈↓ ↓H␈↓and␈α
terms␈α
as␈α
S-expressions.␈α
 These␈α
are␈αharder␈α
for␈α
a␈α
person␈α
to␈α
read␈αand␈α
write,␈α
but␈α
it␈α
is␈α
easier␈αfor␈α
a
␈↓ ↓H␈↓person␈α
to␈αwrite␈α
a␈αprogram␈α
to␈αmanipulate␈α
␈↓↓object␈↓␈α␈↓↓programs␈↓␈α
when␈αthe␈α
object␈αprograms␈α
are␈αin␈α
internal
␈↓ ↓H␈↓language.␈α∂ Besides␈α∞all␈α∂that,␈α∞most␈α∂LISP␈α∞implementations␈α∂accept␈α∞only␈α∂internal␈α∂language␈α∞programs
␈↓ ↓H␈↓rather than some form of external language.

␈↓ ↓H␈↓        Now␈αwe␈αwill␈αdescribe␈αthe␈αthree␈αbasic␈αfunctions␈αand␈αtwo␈αbasic␈αpredicates␈αof␈αLISP␈αfirst␈αusing
␈↓ ↓H␈↓external language, then giving the corresponding internal language form.

␈↓ ↓H␈↓        First,␈αwe␈αhave␈αtwo␈αfunctions␈α␈↓αa␈↓␈αand␈α␈↓αd␈↓.␈α ␈↓αa|␈↓␈↓↓x␈↓␈αis␈αthe␈αa-part␈αof␈αthe␈αS-expression␈α␈↓↓x␈↓␈αand␈α␈↓αd|␈↓␈↓↓x␈↓␈αis␈αthe
␈↓ ↓H␈↓d-part of the S-expression ␈↓↓x.␈↓  Thus

␈↓ ↓H␈↓␈↓ ¬"␈↓↓      ␈↓αa|␈↓↓␈↓¬(A . B) = A␈↓↓,      ␈↓
␈↓ ↓H␈↓␈↓ ∧}␈↓↓␈↓αa|␈↓↓␈↓¬((A . B) . A) = (A . B)␈↓↓,␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ¬!␈↓↓      ␈↓αd|␈↓↓␈↓¬(A . B) = B␈↓↓,      ␈↓
␈↓ ↓H␈↓␈↓ ¬⊃␈↓↓␈↓αd|␈↓↓␈↓¬((A . B) . A) = A␈↓↓.      ␈↓

␈↓ ↓H␈↓␈↓αa|␈↓␈↓↓x␈↓␈α
and␈α
␈↓αd|␈↓␈↓↓x␈↓␈α
are␈α
undefined␈α
in␈α
basic␈α
LISP␈α
when␈α
␈↓↓x␈↓␈α
is␈α
an␈α
atom,␈α
but␈α
they␈α
may␈α
have␈α
a␈α
meaning␈αin␈α
some
␈↓ ↓H␈↓implementations.␈α∞ The␈α∞internal␈α∞forms␈α∞of␈α∞the␈α∂expressions␈α∞␈↓αa|␈↓␈↓↓x␈↓␈α∞and␈α∞␈↓αd|␈↓␈↓↓x␈↓␈α∞are␈α∞␈↓¬(CAR X)␈↓␈α∂and␈α∞␈↓¬(CDR X)␈↓
␈↓ ↓H␈↓respectively.␈α⊂ (Note␈α⊂that␈α∂we␈α⊂use␈α⊂␈↓αa␈↓␈α⊂and␈α∂␈↓αd␈↓␈α⊂as␈α⊂prefix␈α⊂operators,␈α∂ the␈α⊂parentheses␈α⊂occuring␈α⊂in␈α∂the
␈↓ ↓H␈↓above␈α
expressions␈α
are␈αpart␈α
of␈α
the␈αS-expression␈α
notation.)␈α
The␈α
names␈α"CAR"␈α
and␈α
"CDR"␈αstood␈α
for
␈↓ ↓H␈↓"contents␈α
of␈αthe␈α
address␈α
part␈αof␈α
register"␈α
and␈α"contents␈α
of␈α
the␈αdecrement␈α
part␈α
of␈αregister"␈α
in␈αa␈α
1957
␈↓ ↓H␈↓precursor␈αof␈αLISP␈αprojected␈αfor␈αthe␈αIBM␈α704␈αcomputer.␈α The␈αnames␈αhave␈αpersisted␈αfor␈αlack␈αof␈αa
␈↓ ↓H␈↓clearly preferable alternative.

␈↓ ↓H␈↓        Since␈α
lists␈α
are␈α∞a␈α
particular␈α
kind␈α∞of␈α
S-expression,␈α
the␈α∞meanings␈α
of␈α
 ␈↓αa␈↓ ␈α∞and␈α
 ␈↓αd␈↓ ␈α
for␈α∞lists␈α
are
␈↓ ↓H␈↓also␈α"determined␈α"by␈α"the␈α"above␈α"definitions.␈α" Thus,␈α"we␈α"have␈α" ␈↓↓␈↓αa|␈↓↓␈↓¬(A B C D)␈↓↓ = ␈↓¬A␈↓↓␈↓␈α"and
␈↓ ↓H␈↓␈↓↓␈↓αd|␈↓↓␈↓¬(A B C D)␈↓↓ = ␈↓¬(B C D)␈↓↓,␈↓␈αand␈α
we␈αsee␈αthat,␈α
in␈αgeneral,␈α
 ␈↓↓␈↓αa|␈↓↓u␈↓ ␈αis␈αthe␈α
first␈αelement␈αof␈α
the␈αlist␈α
␈↓↓u,␈↓␈αand
␈↓ ↓H␈↓␈↓↓␈↓αd|␈↓↓u␈↓␈αis␈αthe␈αrest␈αof␈αthe␈αlist,␈αdeleting␈αthat␈αfirst␈αelement.␈α Notice␈αthat␈αfor␈αS-expressions,␈αthe␈αdefinitions
␈↓ ↓H␈↓of␈α␈↓↓␈↓αa|␈↓↓x␈↓␈αand␈α␈↓↓␈↓αd|␈↓↓x␈↓␈αare␈α
symmetrical,␈αwhile␈αfor␈αlists␈αthe␈αsymmetry␈α
is␈αlost.␈α This␈αis␈αbecause␈αthe␈α
convention
␈↓ ↓H␈↓that defines lists in terms of S-expressions is unsymmetrical.
␈↓ ↓H␈↓8␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓        The␈α
third␈α
function␈α
 ␈↓↓cons[x,␈α
y]␈↓ ␈α
forms␈α
the␈α
S-expression␈α
whose␈α
a-part␈α
and␈α
d-part␈α
are␈α ␈↓↓x␈↓␈α
 and
␈↓ ↓H␈↓ ␈↓↓y␈↓␈α respectively.␈α We␈αsee␈αthat␈αfor␈αlists,␈α ␈↓↓cons␈↓ ␈αis␈αa␈αprefixing␈αoperation.␈α Namely,␈α ␈↓↓cons[x,␈αu]␈↓ ␈αis␈αthe
␈↓ ↓H␈↓list obtained by putting the element  ␈↓↓x␈↓  onto the front of the list  ␈↓↓u.␈↓  For example

␈↓ ↓H␈↓␈↓ ∧l␈↓↓    cons[␈↓¬(A.B), A␈↓↓] = ␈↓¬((A.B).A)␈↓↓  ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ∧W␈↓↓cons[␈↓¬A, (B C D E)␈↓↓] = ␈↓¬(A B C D E)␈↓↓␈↓.

␈↓ ↓H␈↓If␈αwe␈αwant␈α ␈↓↓cons[x,␈αy]␈↓ ␈αto␈αbe␈αa␈αlist,␈αthen␈α ␈↓↓y␈↓␈α must␈αbe␈αa␈αlist␈α(possibly␈αthe␈αnull␈αlist␈α␈↓¬NIL␈↓),␈αand␈α ␈↓↓x␈↓␈α may
␈↓ ↓H␈↓be␈α⊃any␈α⊃S-expression.␈α⊃ In␈α∩the␈α⊃case␈α⊃of␈α⊃S-expressions␈α⊃in␈α∩general,␈α⊃there␈α⊃is␈α⊃no␈α⊃restriction␈α∩on␈α⊃the
␈↓ ↓H␈↓arguments␈α∞of␈α∞␈↓↓cons.␈↓␈α∞ Usually,␈α∞we␈α∞shall␈α∞write␈α∞ ␈↓↓x␈α∞.␈α∞y␈↓ ␈α∞instead␈α∞of␈α∞  ␈↓↓cons[x,␈α∞y]␈↓ ␈α∞since␈α∞this␈α∞is␈α∞briefer.
␈↓ ↓H␈↓The␈α∪internal␈α∪language␈α∪form␈α∪of␈α∪␈↓↓cons[x, y]␈↓␈α∪is␈α∪␈↓¬(CONS X Y)␈↓.␈α∪ The␈α∪name␈α∪"CONS"␈α∀comes␈α∪from
␈↓ ↓H␈↓"construct" referring to the way ␈↓↓cons[x, y]␈↓ is constructed from the free storage list.

␈↓ ↓H␈↓        The␈αfirst␈αpredicate␈αis␈α ␈↓αat␈↓.␈α ␈↓αat|␈↓␈↓↓x␈↓␈αis␈αtrue␈αif␈αthe␈αS-expression␈α ␈↓↓x␈↓␈α is␈αatomic␈αand␈αfalse␈αotherwise.
␈↓ ↓H␈↓Thus␈α␈↓¬␈↓αat|␈↓¬A␈↓␈αis␈αtrue␈αand␈α␈↓¬␈↓αat|␈↓¬(A␈α.␈αB)␈↓␈αis␈αfalse.␈α The␈αinternal␈αform␈αis␈α␈↓¬(ATOM X)␈↓.␈α Since␈αan␈αS-expression
␈↓ ↓H␈↓is␈α
either␈α
an␈α
atom␈α
or␈α
a␈α
pair␈α
of␈αS-expressions␈α
then␈α
if␈α
␈↓↓¬␈↓αat|␈↓↓x␈↓␈α
there␈α
are␈α
S-expressions␈α
there␈α
must␈αbe␈α
S-
␈↓ ↓H␈↓expressions ␈↓↓y␈↓ and ␈↓↓z␈↓ such that ␈↓↓x = y . z␈↓.  In fact ␈↓↓y = ␈↓αa|␈↓↓x␈↓ and ␈↓↓z = ␈↓αd|␈↓↓x␈↓.

␈↓ ↓H␈↓        The␈α∀second␈α∀predicate␈α∀is␈α∀equality␈α∀of␈α∃atomic␈α∀symbols␈α∀written␈α∀ ␈↓↓x␈α∀␈↓αeq␈↓↓␈α∀y␈↓.␈α∀Equality␈α∃of␈α∀S-
␈↓ ↓H␈↓expressions␈α∞is␈α∞tested␈α∞by␈α∞a␈α∞program␈α∞based␈α
on␈α∞␈↓αeq␈↓.␈α∞ The␈α∞internal␈α∞form␈α∞is␈α∞␈↓¬(EQ X Y)␈↓.␈α∞ Actually␈α
the
␈↓ ↓H␈↓program␈αcomputing␈α␈↓αeq␈↓␈αdoes␈αa␈αbit␈αmore␈αthan␈αtesting␈αequality␈αof␈αatoms.␈α Namely,␈α ␈↓↓x␈α␈↓αeq␈↓↓␈αy␈↓ ␈αis␈αtrue␈αif
␈↓ ↓H␈↓and␈αonly␈αif␈αthe␈αaddresses␈αof␈αthe␈αfirst␈αwords␈αof␈αthe␈αlist␈αstructures␈α ␈↓↓x␈↓␈α and␈α ␈↓↓y␈↓␈α are␈αequal.␈α Therefore,
␈↓ ↓H␈↓if␈α  ␈↓↓x␈α␈↓αeq␈↓↓␈αy␈↓, ␈αthen␈α ␈↓↓x␈↓␈α and␈α ␈↓↓y␈↓␈α are␈αcertainly␈αthe␈αsame␈αS-expression␈αsince␈αthey␈αare␈αrepresented␈αby␈αthe
␈↓ ↓H␈↓same␈αstructure␈α
in␈αmemory.␈α
 The␈αconverse␈α
is␈αfalse␈α
because␈αthere␈α
is␈αno␈α
guarantee␈αin␈α
general␈αthat␈α
the
␈↓ ↓H␈↓same␈α
S-expression␈α
is␈αnot␈α
represented␈α
by␈αtwo␈α
different␈α
list␈αstructures.␈α
 However,␈α
in␈α
the␈αparticular
␈↓ ↓H␈↓case␈αwhere␈αat␈αleast␈αone␈αof␈αthe␈αS-expressions␈αis␈αknown␈αto␈αbe␈αan␈αatom,␈αthis␈αguarantee␈αcan␈αbe␈αgiven,
␈↓ ↓H␈↓because LISP represents atoms uniquely in memory.

␈↓ ↓H␈↓        The␈αabove␈αare␈α
all␈αthe␈αbasic␈αfunctions␈α
of␈αLISP;␈αall␈αother␈α
LISP␈αfunctions␈αcan␈αbe␈α
built␈αfrom
␈↓ ↓H␈↓them␈α∂using␈α∂recursive␈α∂conditional␈α∞expressions␈α∂as␈α∂will␈α∂shortly␈α∞be␈α∂explained.␈α∂However,␈α∂the␈α∂use␈α∞of
␈↓ ↓H␈↓certain abbreviations makes LISP programs easier to write and understand.

␈↓ ↓H␈↓        ␈↓αn|␈↓␈↓↓x␈↓␈α∞ is␈α∞an␈α∞abbreviation␈α∞for␈α∞ ␈↓↓x␈α∞␈↓αeq␈↓↓␈α∞␈↓¬NIL␈↓↓␈↓.␈α
 It␈α∞rates␈α∞a␈α∞special␈α∞notation␈α∞because␈α∞ ␈↓¬NIL␈↓ ␈α∞plays␈α
the
␈↓ ↓H␈↓same␈αrole␈αamong␈α
lists␈αthat␈αzero␈α
plays␈αamong␈αnumbers,␈αand␈α
list␈αvariables␈αoften␈α
have␈αto␈αbe␈αtested␈α
to
␈↓ ↓H␈↓see if their value is ␈↓¬NIL␈↓.  Its internal form is ␈↓¬(NULL X)␈↓.

␈↓ ↓H␈↓    The␈αnotation␈α ␈↓↓list[x1, x2, . . . ,xn]␈↓ ␈αis␈αused␈αto␈αdenote␈αthe␈αcomposition␈αof␈α␈↓↓cons␈↓'s␈αthat␈αforms␈αa
␈↓ ↓H␈↓list␈α∂from␈α∞its␈α∂elements.␈α∂ Thus␈α∞ ␈↓↓list[x, y, z]␈↓ ␈α∂denotes␈α∞ ␈↓↓cons[x, cons[y, cons[z, ␈↓¬NIL␈↓↓]]]␈↓ ␈α∂and␈α∂forms␈α∞a
␈↓ ↓H␈↓list␈α
of␈αthree␈α
elements␈αout␈α
of␈αthe␈α
quantities␈α
␈↓↓x,␈↓␈α␈↓↓y,␈↓␈α
and␈α␈↓↓z.␈↓␈α
 We␈αoften␈α
write␈α ␈↓↓<x, y, . . ., z>␈↓ ␈α
instead
␈↓ ↓H␈↓of␈α⊂ ␈↓↓list[x, y, . . . , z]␈↓ ␈α⊃when␈α⊂this␈α⊂will␈α⊃not␈α⊂cause␈α⊂confusion.␈α⊃ The␈α⊂experienced␈α⊃implementer␈α⊂of
␈↓ ↓H␈↓programming␈α∩languages␈α∪will␈α∩expect␈α∪that␈α∩since␈α∪␈↓↓list␈↓␈α∩has␈α∪a␈α∩variable␈α∪number␈α∩of␈α∪arguments,␈α∩its
␈↓ ↓H␈↓implementation␈αwill␈αpose␈αproblems.␈α That␈αis␈αindeed␈αtrue.␈α The␈αinternal␈αform␈αof␈α ␈↓↓<x, y, . . ., z>␈↓ 
␈↓ ↓H␈↓is  ␈↓¬(LIST X Y . . . Z)␈↓.

␈↓ ↓H␈↓        Compositions␈αof␈α␈↓αa␈↓␈αand␈α␈↓αd␈↓␈αare␈αwritten␈αby␈αconcatenating␈αthe␈αletters␈α␈↓αa␈↓␈αand␈α␈↓αd␈↓.␈α Thus,␈αit␈αis␈αeasily
␈↓ ↓H␈↓seen␈α
that␈α∞␈↓αad|␈↓␈↓↓u␈↓␈α
denotes␈α∞the␈α
second␈α∞element␈α
of␈α∞the␈α
list␈α∞␈↓↓u,␈↓␈α
and␈α∞␈↓αadd|␈↓␈↓↓u␈↓␈α
denotes␈α∞the␈α
third␈α∞element␈α
of
␈↓ ↓H␈↓that list.  The internal names of these functions are ␈↓¬CADR ␈↓and ␈↓¬CADDR ␈↓respectively.

␈↓ ↓H␈↓        Finally␈α
we␈α
summarize␈α∞our␈α
conventions␈α
for␈α
notation.␈α∞  In␈α
our␈α
language␈α
there␈α∞are␈α
constants,
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ 99


␈↓ ↓H␈↓variables,␈α∞names␈α∞for␈α∞functions␈α
and␈α∞predicates␈α∞and␈α∞expressions␈α
built␈α∞from␈α∞these␈α∞symbols.␈α∞ In␈α
the
␈↓ ↓H␈↓external␈αlanguage␈αS-expression␈αconstants␈αwill␈αstand␈αfor␈αthemselves␈αand␈αwill␈αappear␈αin␈αthe␈αspecial
␈↓ ↓H␈↓font␈α⊃reserved␈α⊃for␈α⊂S-expressions␈α⊃and␈α⊃used␈α⊂in␈α⊃the␈α⊃preceeding␈α⊂sections.␈α⊃ Variables␈α⊃and␈α⊂function
␈↓ ↓H␈↓names␈α∞will␈α∞appear␈α
in␈α∞lower␈α∞case␈α∞italics.␈α
 We␈α∞will␈α∞generally␈α
use␈α∞the␈α∞names␈α∞␈↓↓x,␈↓␈α
␈↓↓y,␈↓␈α∞␈↓↓z,␈↓␈α∞␈↓↓x1,␈↓␈α∞etc.␈α
for
␈↓ ↓H␈↓variables␈αranging␈α
over␈αS-expressions␈αand␈α
␈↓↓u,␈↓␈α␈↓↓v,␈↓␈α␈↓↓w,␈↓␈α
␈↓↓u1,␈↓␈αetc.␈αfor␈α
variables␈αranging␈αover␈α
lists.␈α In
␈↓ ↓H␈↓internal␈α∩language␈α∪a␈α∩variable␈α∪or␈α∩function␈α∪name␈α∩is␈α∪denoted␈α∩by␈α∪the␈α∩corresponding␈α∪upper␈α∩case
␈↓ ↓H␈↓identifier␈α∞(in␈α
S-expression␈α∞font).␈α
 A␈α∞certain␈α
ambiguity␈α∞arises␈α
when␈α∞we␈α
want␈α∞to␈α∞use␈α
S-expression
␈↓ ↓H␈↓constants␈αin␈αthe␈αinternal␈α
language.␈α Namely,␈αthe␈αS-expression␈α ␈↓¬X␈α
␈↓ ␈αcould␈αeither␈αstand␈αfor␈α
itself␈αor
␈↓ ↓H␈↓stand␈α∂for␈α⊂a␈α∂variable␈α∂ ␈↓¬X␈α⊂␈↓ whose␈α∂value␈α∂is␈α⊂wanted.␈α∂ More␈α∂generally␈α⊂an␈α∂S-expression␈α⊂could␈α∂either
␈↓ ↓H␈↓stand␈αfor␈α
itself␈αas␈αa␈α
constant␈αor␈αfor␈α
the␈αLISP␈αterm␈α
that␈αit␈αrepresents.␈α
 Internal␈αlanguage␈αavoids␈α
the
␈↓ ↓H␈↓ambiguity␈α∞by␈α∞using␈α∞ ␈↓¬(QUOTE ␈↓↓e␈↓¬)␈↓ ␈α∞to␈α∞stand␈α∞for␈α∞the␈α∞S-expression␈α∞ ␈↓↓e,␈↓␈α∞and␈α∞unquoted␈α
S-expressions
␈↓ ↓H␈↓stand␈α∂for␈α⊂the␈α∂corresponding␈α⊂LISP␈α∂term.␈α⊂ Thus␈α∂the␈α⊂internal␈α∂form␈α⊂of␈α∂the␈α⊂S-expression␈α∂constant
␈↓ ↓H␈↓␈↓¬(CAR X)␈↓ is ␈↓¬(QUOTE (CAR X))␈↓, while ␈↓¬(CAR X)␈↓ stands for ␈↓↓␈↓αa|␈↓↓x␈↓.

␈↓ ↓H␈↓        Besides␈α
the␈α
basic␈α
functions␈αof␈α
LISP,␈α
there␈α
will␈αbe␈α
user-defined␈α
functions.␈α
 We␈αhaven't␈α
given
␈↓ ↓H␈↓the␈α∞mechanism␈α
for␈α∞function␈α
definition␈α∞yet,␈α
but␈α∞suppose␈α
a␈α∞function␈α
␈↓↓subst␈↓␈α∞taking␈α∞three␈α
arguments
␈↓ ↓H␈↓has␈α→been␈α~defined.␈α→ It␈α→may␈α~be␈α→used␈α~in␈α→terms␈α→like␈α~ ␈↓↓subst[x,y,z]␈↓ ␈α→having␈α~internal␈α→form
␈↓ ↓H␈↓ ␈↓¬(SUBST X Y Z)␈↓.␈α⊃ Notice␈α⊃that␈α⊃the␈α⊂arguments␈α⊃to␈α⊃a␈α⊃function␈α⊂are␈α⊃surrounded␈α⊃by␈α⊃brackets␈α⊃as␈α⊂in
␈↓ ↓H␈↓␈↓↓subst[x, y, z]␈↓ thus reserving parthentheses for S-expressions.

␈↓ ↓H␈↓        As␈α∪in␈α∩other␈α∪programming␈α∩languages␈α∪and␈α∩in␈α∪mathematics␈α∩generally,␈α∪new␈α∩terms␈α∪can␈α∩be
␈↓ ↓H␈↓constructed␈α∂by␈α∂applying␈α∂functions␈α∂and␈α∂predicates␈α∂to␈α∂other␈α∂terms␈α∂and␈α∂not␈α∂just␈α∂to␈α∂variables␈α∂and
␈↓ ↓H␈↓constants.␈α⊂ Thus␈α⊂we␈α⊂have␈α⊂terms␈α⊂like␈α∂ ␈↓↓subst[x,y,␈↓αa|␈↓↓z]␈α⊂.␈α⊂subst[x,y,␈↓αd|␈↓↓z]␈↓ ␈α⊂involving␈α⊂a␈α⊂user␈α∂defined
␈↓ ↓H␈↓function ␈↓↓subst.␈↓  Its internal form is  ␈↓¬(CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z)))␈↓.

␈↓ ↓H␈↓        In␈α
external␈α
language,␈α∞we␈α
often␈α
omit␈α∞brackets␈α
for␈α
functions␈α∞of␈α
one␈α
argument.␈α∞ Thus␈α
 ␈↓↓alt x␈↓ 
␈↓ ↓H␈↓stands␈α∞for␈α∞ ␈↓↓alt[x]␈↓, ␈α∞and␈α∞ ␈↓↓alt alt x␈↓ ␈α∞stands␈α∞for␈α∞ ␈↓↓alt[alt[x]]␈↓ .␈α∞ This␈α∞convention␈α∞was␈α∞also␈α∞used␈α∞in␈α∞the
␈↓ ↓H␈↓descriptions of ␈↓αa␈↓, ␈↓αd␈↓, ␈↓αat␈↓, and ␈↓αn␈↓.



␈↓ ↓H␈↓α␈↓ ε⊂Exercise

␈↓ ↓H␈↓1.  What is the value of ␈↓↓< ␈↓¬A ␈↓↓. ␈↓αa|␈↓↓␈↓¬(B . C)␈↓↓, ␈↓¬D ␈↓↓. ␈↓αd|␈↓↓␈↓¬(B . C)␈↓↓>␈↓?



␈↓ ↓H␈↓6.  ␈↓αConditional expressions.␈↓


␈↓ ↓H␈↓        When␈α∩the␈α∩value␈α⊃of␈α∩a␈α∩function␈α∩depends␈α⊃on␈α∩whether␈α∩a␈α∩certain␈α⊃predicate␈α∩is␈α∩true␈α∩of␈α⊃the
␈↓ ↓H␈↓arguments, conditional terms are used to describe the function.  The conditional term

␈↓ ↓H␈↓6.1)␈↓ ¬/␈↓↓␈↓αif␈↓↓ p ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b        ␈↓ 

␈↓ ↓H␈↓is␈αevaluated␈αas␈αfollows:␈αfirst␈α ␈↓↓p␈↓␈α is␈αevaluated␈αand␈αdetermined␈αto␈αbe␈αtrue␈αor␈αfalse.␈α If␈α␈↓↓p␈↓␈αis␈αtrue,␈α
then
␈↓ ↓H␈↓␈↓↓a␈↓␈α
is␈α
evaluated␈α
and␈α
its␈α
value␈α
is␈α
the␈α
value␈α
of␈α
the␈α
expression.␈α
 If␈α
␈↓↓p␈↓␈α
is␈α
false,␈α
then␈α
␈↓↓b␈↓␈α
is␈α
evaluated␈αand␈α
its
␈↓ ↓H␈↓value␈αis␈αthe␈αvalue␈αof␈αthe␈αexpression.␈α Note␈αthat␈αif␈α␈↓↓p␈↓␈αis␈αtrue,␈α␈↓↓b␈↓␈αis␈αnever␈αevaluated,␈αand␈αif␈α␈↓↓p␈↓␈αis␈αfalse,
␈↓ ↓H␈↓then␈α␈↓↓a␈↓␈α
is␈αnever␈α
evaluated.␈α The␈αimportance␈α
of␈αthis␈α
will␈αbe␈αexplained␈α
later.␈α Notice␈α
also␈αthat␈α
␈↓↓p␈↓␈αis
␈↓ ↓H␈↓expected␈α
to␈α
have␈α
the␈αvalue␈α
␈↓αtrue␈↓␈α
or␈α
␈↓αfalse␈↓,␈αin␈α
fact␈α
␈↓↓p␈↓␈α
should␈α
be␈αa␈α
propositional␈α
(or␈α
Boolean)␈αterm.␈α
 A
␈↓ ↓H␈↓10␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓predicate␈α
applied␈α
to␈α
a␈α
list␈α
of␈α
arguments␈α
is␈α
a␈α
propositional␈α
term.␈α
 We␈α
will␈α
explain␈α
how␈α
to␈α
form␈α
such
␈↓ ↓H␈↓terms␈α
in␈αgeneral␈α
in␈α
the␈αnext␈α
section.␈α A␈α
familiar␈α
function␈αthat␈α
can␈αbe␈α
written␈α
conveniently␈αusing
␈↓ ↓H␈↓conditional expressions is the absolute value of a real number.  We have

␈↓ ↓H␈↓6.2)␈↓ ¬≤␈↓↓|x| = ␈↓αif␈↓↓ x<0 ␈↓αthen␈↓↓ -x ␈↓αelse␈↓↓ x␈↓. 

␈↓ ↓H␈↓More generally a conditional term has the form

␈↓ ↓H␈↓6.3)␈↓ β↑␈↓↓␈↓αif␈↓↓ p ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ ␈↓αif␈↓↓ q ␈↓αthen␈↓↓ b . . .  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ s ␈↓αthen␈↓↓ d ␈↓αelse␈↓↓ e␈↓. 

␈↓ ↓H␈↓There␈α∞can␈α∞be␈α∞any␈α∂number␈α∞of␈α∞clauses.␈α∞The␈α∞value␈α∂is␈α∞determined␈α∞by␈α∞evaluating␈α∂the␈α∞propositional
␈↓ ↓H␈↓terms␈α␈↓↓p,␈↓␈α␈↓↓q,␈↓␈αetc.␈αuntil␈αa␈αtrue␈αterm␈αis␈α
found,␈αthe␈αvalue␈αis␈αthen␈αthat␈αof␈αthe␈αterm␈αfollowing␈α
the␈αnext
␈↓ ↓H␈↓␈↓αthen␈↓.␈α If␈αnone␈αof␈αthe␈α
propositional␈αterms␈αis␈αtrue,␈αthen␈αthe␈α
value␈αis␈αthat␈αof␈αthe␈αterm␈α
following␈αthe
␈↓ ↓H␈↓last ␈↓αelse␈↓.

␈↓ ↓H␈↓        Figure␈α_8␈α→shows␈α_an␈α_example␈α→of␈α_a␈α_(numeric)␈α→function␈α_defined␈α_using␈α→a␈α_conditional
␈↓ ↓H␈↓expression.




␈↓"␈↓ ↓H␈↓                                           (0,1)
␈↓"␈↓ ↓H␈↓                                         ≤'`≥
␈↓"␈↓ ↓H␈↓                                       ≤'    `≥
␈↓"␈↓ ↓H␈↓            ↑                        ≤'        `≥
␈↓"␈↓ ↓H␈↓            ~                      ≤'            `≥
␈↓"␈↓ ↓H␈↓     tri[x] ~     αααααααααααααααα'                `αααααααααααααααα
␈↓"␈↓ ↓H␈↓            ~                  (-1,0)            (1,0)
␈↓"␈↓ ↓H␈↓             ααα→
␈↓"␈↓ ↓H␈↓              x


␈↓"␈↓ ↓H␈↓␈↓ αr␈↓↓tri[x] = ␈↓αif␈↓↓ x<-1 ␈↓αthen␈↓↓ 0 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ x<0 ␈↓αthen␈↓↓ 1+x ␈↓αelse␈↓↓ ␈↓αif␈↓↓ x<1 ␈↓αthen␈↓↓ 1-x ␈↓αelse␈↓↓ 0␈↓.

␈↓ ↓H␈↓␈↓ ∧t␈↓αFigure 8.␈↓  The triangle function.



␈↓ ↓H␈↓        The conditional term in internal language has the form

␈↓ ↓H␈↓6.4)␈↓ ∧g␈↓↓␈↓¬(COND ␈↓↓(p1 e1) (p2 e2) ... (pn en)␈↓¬)␈↓↓␈↓ 

␈↓ ↓H␈↓with␈α
any␈α
number␈αof␈α
␈↓↓p-e␈↓␈α
pairs.␈α
 Its␈αvalue␈α
is␈α
determined␈α
by␈αevaluating␈α
the␈α
␈↓↓p␈↓'s␈α
successively␈αuntil␈α
one
␈↓ ↓H␈↓is␈αfound␈αwith␈α
a␈αvalue␈αcorresponding␈α
to␈α␈↓αtrue␈↓.␈α Then␈αthe␈α
value␈αof␈αthe␈α
corresponding␈α␈↓↓e␈↓␈αis␈α
taken␈αas
␈↓ ↓H␈↓the␈α
value␈α
of␈α
the␈α
conditional␈α
term.␈α
 If␈α
none␈α
of␈α
the␈α
␈↓↓p␈↓'s␈α
is␈α
true,␈α
then␈α
the␈α
value␈α
of␈α
the␈αconditional␈α
term
␈↓ ↓H␈↓is␈αundefined.␈α In␈αmost␈αLISP␈αimplementations␈αthe␈α
atom␈α␈↓¬NIL␈↓␈αcorresponds␈αto␈α␈↓αfalse␈↓␈αand␈αany␈α
non-␈↓¬NIL␈↓
␈↓ ↓H␈↓S-expression␈α∩corresponds␈α∩to␈α⊃␈↓αtrue␈↓.␈α∩ (In␈α∩some␈α⊃implementations␈α∩the␈α∩conditional␈α⊃term␈α∩is␈α∩given␈α⊃a
␈↓ ↓H␈↓default␈αvalue␈αsuch␈αas␈α
␈↓¬NIL␈↓␈αif␈αnone␈αof␈α
the␈α␈↓↓p␈↓'s␈αare␈αtrue.)  ␈α
In␈αthe␈αinternal␈αform,␈α
all␈αthe␈α␈↓↓e␈↓'s␈αare␈α
treated
␈↓ ↓H␈↓the␈α∞same␈α∞which␈α∞makes␈α∞programs␈α∂for␈α∞interpreting␈α∞or␈α∞compiling␈α∞conditional␈α∞expressions␈α∂easier␈α∞to
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *11


␈↓ ↓H␈↓write.␈α Thus␈αin␈α
internal␈αlanguage␈αconditional␈α
expressions␈αare␈αmade␈α
in␈αa␈αmore␈α
regular␈αway␈αthan␈α
in
␈↓ ↓H␈↓external␈α∞language.␈α∂The␈α∞external␈α∞form␈α∂was␈α∞altered␈α∞to␈α∂conform␈α∞to␈α∞ALGOL.␈α∂ Putting␈α∞parentheses
␈↓ ↓H␈↓around␈α∂each␈α∂␈↓↓p-e␈↓␈α∞pair␈α∂was␈α∂probably␈α∂a␈α∞mistake␈α∂in␈α∂the␈α∂design␈α∞of␈α∂LISP,␈α∂because␈α∂it␈α∞unnecessarily
␈↓ ↓H␈↓increases␈α∂the␈α∂number␈α∂of␈α∂parentheses.␈α∂ It␈α∂should␈α∂have␈α∂been␈α∂ ␈↓¬(COND ␈↓↓p1 e1 p2 e2 ... pn en␈↓), ␈α∂but
␈↓ ↓H␈↓such a change should be made now only as part of a massive improvement.

␈↓ ↓H␈↓        Conditional expressions may be compounded with functions to get terms like

␈↓ ↓H␈↓6.5)␈↓ ∧2␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . append[␈↓αd|␈↓↓u, v]      ␈↓ 

␈↓ ↓H␈↓involving a yet to be defined function ␈↓↓append.␈↓  The internal form of this is

␈↓ ↓H␈↓6.6)␈↓ αy␈↓↓␈↓¬(COND ((NULL U) V) (T (CONS (CAR U) (APPEND (CDR U) V))))␈↓↓.␈↓ 

␈↓ ↓H␈↓One␈α
would␈αnormally␈α
have␈αexpected␈α
to␈αwrite␈α
␈↓¬(QUOTE␈α
T)␈↓,␈αbut␈α
there␈αis␈α
a␈αspecial␈α
convention␈α
that␈α␈↓¬T␈↓
␈↓ ↓H␈↓may␈α∂be␈α∂written␈α∂without␈α⊂␈↓¬QUOTE.␈α∂␈↓␈α∂This␈α∂convention␈α∂applies␈α⊂to␈α∂␈↓¬NIL␈↓␈α∂also.␈α∂ This␈α∂means␈α⊂that␈α∂these
␈↓ ↓H␈↓symbols␈αcannot␈αbe␈αused␈αas␈αvariables.␈α As␈α
mentioned␈αin␈αsection␈α␈↓π∞␈↓␈α5,␈α␈↓¬T␈↓␈αrepresents␈α
the␈αpropositional
␈↓ ↓H␈↓constant␈α∞␈↓αtrue␈↓,␈α∞i.e.␈α∞it␈α∞is␈α∞always␈α∞true␈α∞so␈α∞that␈α
it␈α∞is␈α∞impossible␈α∞to␈α∞"fall␈α∞off␈α∞the␈α∞end"␈α∞of␈α∞a␈α
conditional
␈↓ ↓H␈↓expression␈α∂with␈α∂␈↓¬T␈↓␈α⊂as␈α∂its␈α∂last␈α∂␈↓↓p.␈↓␈α⊂ It␈α∂is␈α∂the␈α∂translation␈α⊂into␈α∂internal␈α∂form␈α∂of␈α⊂the␈α∂final␈α∂␈↓αelse␈↓␈α⊂of␈α∂a
␈↓ ↓H␈↓conditional expression in external form.



␈↓ ↓H␈↓7.  ␈↓αPropositional expressions.␈↓


␈↓ ↓H␈↓        Propositional␈αterms␈αare␈αa␈αsubset␈αof␈αthe␈αclass␈αof␈αexpressions␈αevaluating␈αto␈α␈↓αtrue␈↓␈αor␈α␈↓αfalse␈↓.␈α As
␈↓ ↓H␈↓mentioned␈α∂earlier,␈α∂if␈α∂␈↓πf␈↓␈α⊂is␈α∂a␈α∂predicate␈α∂of␈α⊂n-arguments␈α∂then␈α∂␈↓↓␈↓πf␈↓↓[x1, . . . ,xn]␈↓␈α∂is␈α⊂a␈α∂propositional
␈↓ ↓H␈↓term.␈α Additional␈αpropositional␈αterms␈αcan␈αbe␈αformed␈αby␈αcombining␈αalready␈αformed␈αones␈αusing␈αthe
␈↓ ↓H␈↓logical␈αoperations␈αof␈α
conjunction(␈↓αand␈↓),␈αdisjunction(␈↓αor␈↓)␈αand␈αnegation(␈↓αnot␈↓).␈α
 We␈αwill␈αuse␈αthe␈α
symbols
␈↓ ↓H␈↓∧,␈α∨,␈α
and␈α¬␈αrespectively␈α
for␈αthese␈αoperators.␈α
 The␈αvalue␈α
of␈αa␈αpropositional␈α
term␈αcan␈αbe␈α
determined
␈↓ ↓H␈↓using␈α∀the␈α∀truth␈α∀table␈α∀given␈α∃in␈α∀Table␈α∀1.␈α∀ Both␈α∀∧␈α∀and␈α∃∨␈α∀are␈α∀associative,␈α∀so␈α∀we␈α∃can␈α∀write
␈↓ ↓H␈↓expressions like  ␈↓↓p1 ∧ p2 ∧ p3␈↓  without worrying about the grouping.


␈↓ ↓H␈↓␈↓ α8␈↓αtrue␈↓ ∧ ␈↓αtrue␈↓  = ␈↓αtrue␈↓␈↓ ¬X␈↓αtrue␈↓ ∨ ␈↓αtrue␈↓  = ␈↓αtrue␈↓
␈↓ ↓H␈↓␈↓ α8␈↓αtrue␈↓ ∧ ␈↓αfalse␈↓ = ␈↓αfalse␈↓␈↓ ¬X␈↓αtrue␈↓ ∨ ␈↓αfalse␈↓ = ␈↓αtrue␈↓␈↓ λx¬␈↓αtrue␈↓  = ␈↓αfalse␈↓
␈↓ ↓H␈↓␈↓ α8␈↓αfalse␈↓ ∧ ␈↓αtrue␈↓  = ␈↓αfalse␈↓␈↓ ¬X␈↓αfalse␈↓ ∨ ␈↓αtrue␈↓  = ␈↓αtrue␈↓␈↓ λx¬␈↓αfalse␈↓ = ␈↓αtrue␈↓
␈↓ ↓H␈↓␈↓ α8␈↓αfalse␈↓ ∧ ␈↓αfalse␈↓ = ␈↓αfalse␈↓␈↓ ¬X␈↓αfalse␈↓ ∨ ␈↓αfalse␈↓ = ␈↓αfalse␈↓


␈↓ ↓H␈↓␈↓ ∧"␈↓αTable 1.␈↓  Truth table for propositional terms.


␈↓ ↓H␈↓        In␈α∞order␈α∞to␈α∞compute␈α∞the␈α∞value␈α∞of␈α∞propositional␈α∞terms␈α∞we␈α∞introduce␈α∞the␈α∞operators␈α∞␈↓αand␈↓,␈α∞␈↓αor␈↓,
␈↓ ↓H␈↓and ␈↓αnot␈↓.  These operators are defined using conditional terms as follows:

␈↓ ↓H␈↓7.1)␈↓ ∧{␈↓↓p ␈↓αand␈↓↓ q = ␈↓αif␈↓↓ p ␈↓αthen␈↓↓ q ␈↓αelse␈↓↓ ␈↓αfalse␈↓↓␈↓

␈↓ ↓H␈↓7.2)␈↓ ¬↔␈↓↓ p ␈↓αor␈↓↓ q = ␈↓αif␈↓↓ p ␈↓αthen␈↓↓ p ␈↓αelse␈↓↓ q␈↓
␈↓ ↓H␈↓12␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓7.3)␈↓ ∧k␈↓↓  ␈↓αnot␈↓↓ p = ␈↓αif␈↓↓ p ␈↓αthen␈↓↓ ␈↓αfalse␈↓↓ ␈↓αelse␈↓↓ ␈↓αtrue␈↓↓␈↓


␈↓ ↓H␈↓The internal form of these definitions is

␈↓ ↓H␈↓␈↓ ∧H␈↓↓␈↓¬(AND P Q) = (COND (P Q) (T NIL))␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓↓␈↓¬ (OR P Q) = (COND (P P) (T Q))  ␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓↓␈↓¬  (NOT P) = (COND (P NIL) (T T))␈↓↓␈↓


␈↓ ↓H␈↓Note␈αthat␈αif␈α␈↓↓p␈↓␈αis␈αfalse␈αthen␈α ␈↓↓p ␈↓αand␈↓↓ q = ␈↓αfalse␈↓↓␈↓ ␈α
independent␈αof␈αthe␈αvalue␈αof␈α␈↓↓q,␈↓␈αand␈αlikewise␈αif␈α
␈↓↓p␈↓␈αis
␈↓ ↓H␈↓true,␈α
then␈α ␈↓↓p ␈↓αor␈↓↓ q = ␈↓αtrue␈↓↓␈↓ ␈α
independent␈α
of␈α␈↓↓q.␈↓␈α
 If␈α
␈↓↓p␈↓␈αhas␈α
been␈α
evaluated␈αand␈α
found␈α
to␈αbe␈α
false,␈αthen␈α
␈↓↓q␈↓
␈↓ ↓H␈↓does␈α∞not␈α∞have␈α∞to␈α∞be␈α∞evaluated␈α∞at␈α∞all␈α∞to␈α∂find␈α∞the␈α∞value␈α∞of␈α∞␈↓↓p ␈↓αand␈↓↓ q␈↓,␈α∞and,␈α∞in␈α∞fact,␈α∞LISP␈α∂does␈α∞not
␈↓ ↓H␈↓evaluate␈α⊃␈↓↓q␈↓␈α⊃in␈α⊃this␈α⊂case.␈α⊃ Similarly,␈α⊃␈↓↓q␈↓␈α⊃is␈α⊂not␈α⊃evaluated␈α⊃in␈α⊃evaluating␈α⊂␈↓↓p ␈↓αor␈↓↓ q␈↓␈α⊃if␈α⊃␈↓↓p␈↓␈α⊃is␈α⊃true.␈α⊂This
␈↓ ↓H␈↓procedure␈α∪is␈α∀in␈α∪accordance␈α∪with␈α∀the␈α∪above␈α∪conditional␈α∀expression␈α∪descriptions␈α∪of␈α∀the␈α∪these
␈↓ ↓H␈↓operators.␈α
 The␈α
importance␈α
of␈αthis␈α
convention,␈α
which␈α
contrasts␈α
with␈αthat␈α
of␈α
ALGOL␈α
60,␈α
will␈αbe
␈↓ ↓H␈↓apparent later when we discuss recursive definitions of functions and predicates.

␈↓ ↓H␈↓        Propositional␈α
expressions␈α
can␈α
be␈α
combined␈α
with␈α
functions␈α
and␈α
conditional␈α
expressions␈α
to␈α
get
␈↓ ↓H␈↓terms like

␈↓ ↓H␈↓7.4)␈↓ ∧E␈↓↓␈↓αif␈↓↓ [␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓u] ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . alt ␈↓αdd|␈↓↓u␈↓ 

␈↓ ↓H␈↓for which the translation into internal form is

␈↓ ↓H␈↓7.5)␈↓ ↓m␈↓↓␈↓¬  (COND ((OR (NULL U) (NULL (CDR U))) U) (T (CONS (CAR U) (ALT (CDDR U)))))␈↓↓␈↓. 

␈↓ ↓H␈↓        We␈α⊃conclude␈α⊃this␈α⊃section␈α⊃with␈α⊃a␈α⊃few␈α⊃remarks.␈α⊃ First,␈α⊃in␈α⊃the␈α⊃external␈α⊃language␈α∩we␈α⊃may
␈↓ ↓H␈↓consider␈α⊂that␈α⊂the␈α⊂values␈α⊂␈↓αtrue␈↓␈α⊂and␈α⊃␈↓αfalse␈↓␈α⊂belong␈α⊂to␈α⊂a␈α⊂separate␈α⊂domain␈α⊂from␈α⊃S-expressions,␈α⊂the
␈↓ ↓H␈↓domain␈α∂of␈α∂truth␈α∂values.␈α∂ However␈α∂in␈α⊂internal␈α∂form␈α∂we␈α∂only␈α∂have␈α∂S-expressions␈α⊂available␈α∂and
␈↓ ↓H␈↓must␈α∃interpret␈α∀S-expressions␈α∃as␈α∃a␈α∀truth␈α∃values␈α∀in␈α∃some␈α∃cases.␈α∀ As␈α∃mentioned␈α∃before␈α∀most
␈↓ ↓H␈↓implementations␈α∞interpret␈α
␈↓¬NIL␈↓␈α∞as␈α∞␈↓αfalse␈↓␈α
and␈α∞anything␈α
non-␈↓¬NIL␈↓␈α∞as␈α∞␈↓αtrue␈↓.␈α
 This␈α∞has␈α∞the␈α
additional
␈↓ ↓H␈↓effect that LISP cannot distinguish between propositional terms and other terms.

␈↓ ↓H␈↓        Most␈αLISP␈αimplementations␈αallow␈α␈↓αand␈↓␈αand␈α␈↓αor␈↓␈αto␈αhave␈αand␈αarbitrary␈αnumber␈αof␈αarguments.
␈↓ ↓H␈↓Since␈α∂these␈α∂operations␈α∂are␈α⊂associative,␈α∂this␈α∂can␈α∂be␈α∂viewed␈α⊂as␈α∂simply␈α∂omitting␈α∂parentheses␈α⊂as␈α∂it
␈↓ ↓H␈↓won't matter how the arguments are grouped.

␈↓ ↓H␈↓        Finally,␈α
although␈α
we␈α∞will␈α
continue␈α
to␈α∞use␈α
the␈α
usual␈α∞logical␈α
symbols␈α
∧,␈α∞∨,␈α
and␈α
¬␈α∞in␈α
writing
␈↓ ↓H␈↓propostional␈αterms,␈αwhen␈αevaluating␈αLISP␈αterms␈αwe␈αwill␈αuse␈αthe␈αrules␈αfor␈α␈↓αand␈↓,␈α␈↓αor␈↓,␈αand␈α␈↓αnot␈↓␈αgiven
␈↓ ↓H␈↓above.



␈↓ ↓H␈↓8.  ␈↓αRecursive function definitions.␈↓


␈↓ ↓H␈↓        In␈α∞such␈α∞languages␈α
as␈α∞FORTRAN␈α∞and␈α
ALGOL,␈α∞computer␈α∞programs␈α
are␈α∞expressed␈α∞in␈α
the
␈↓ ↓H␈↓main␈α⊃as␈α⊃sequences␈α⊃of␈α⊃assignment␈α⊃statements␈α⊃and␈α⊃conditional␈α⊃␈↓αgo␈α⊃to␈↓'s.␈α⊃ In␈α⊃LISP,␈α⊃programs␈α⊃are
␈↓ ↓H␈↓mainly expressed in the form of recursively defined functions.  We begin with an example.
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *13


␈↓ ↓H␈↓        We␈αwant␈α
a␈αfunction␈α␈↓↓alt␈↓␈α
such␈αthat␈α␈↓↓alt u␈↓␈α
 gives␈αa␈α
list␈αwhose␈αelements␈α
are␈αalternate␈αelements␈α
of
␈↓ ↓H␈↓the list ␈↓↓u␈↓ beginning with the first.  For example we want

␈↓ ↓H␈↓␈↓ ∧}␈↓↓   alt ␈↓¬(A B C D E)␈↓↓ = ␈↓¬(A C E)␈↓↓␈↓,
␈↓ ↓H␈↓␈↓ ∧x␈↓↓ alt ␈↓¬((A B) (C D))␈↓↓ = ␈↓¬((A B))␈↓↓␈↓,
␈↓ ↓H␈↓␈↓ ¬!␈↓↓           alt ␈↓¬(A)␈↓↓ = ␈↓¬(A)␈↓↓,   ␈↓
␈↓ ↓H␈↓␈↓ ¬∂␈↓↓             alt ␈↓¬NIL␈↓↓ = ␈↓¬NIL␈↓↓.     ␈↓


␈↓ ↓H␈↓and in LISP we can define ␈↓↓alt␈↓ by the recursion equation

␈↓ ↓H␈↓8.1) ␈↓ ∧~␈↓↓alt u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . alt ␈↓αdd|␈↓↓u␈↓. 

␈↓ ↓H␈↓In case you need a review of our precedence conventions, fully bracketed it looks like

␈↓ ↓H␈↓␈↓ βJ␈↓↓alt[u] ← ␈↓αif␈↓↓ [␈↓αn|␈↓↓[u] ∨ ␈↓αn|␈↓↓[␈↓αd|␈↓↓[u]]] ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ [␈↓αa|␈↓↓[u] . alt[␈↓αdd|␈↓↓[u]]]␈↓. 

␈↓ ↓H␈↓        The␈α⊃terms␈α∩appearing␈α⊃in␈α⊃the␈α∩recursion␈α⊃equation␈α⊃are␈α∩formed␈α⊃by␈α⊃the␈α∩methods␈α⊃previously
␈↓ ↓H␈↓discussed.␈α Notice␈αthat␈αthe␈αfunction␈α␈↓↓alt␈↓␈αoccurs␈αin␈αthe␈αright␈αhand␈αside␈αof␈αits␈αown␈αdefining␈αequation
␈↓ ↓H␈↓and␈αthat␈αwe␈αuse␈α"←"␈αinstead␈αof␈α"="␈α.␈α The␈αrecursion␈αequation␈αtells␈αus␈αthat␈α␈↓↓alt␈↓␈αis␈αa␈αfunction␈αof␈αone
␈↓ ↓H␈↓variable␈α∞␈↓↓u,␈↓␈α∞and␈α∞that␈α∞the␈α∞value␈α∞of␈α∞␈↓↓alt␈α∞u␈↓␈α∞for␈α∞some␈α∞particular␈α∞list␈α∞is␈α∞computed␈α∞by␈α∂evaluating␈α∞the
␈↓ ↓H␈↓right␈α
hand␈α
side␈α
of␈α
the␈αdefinition,␈α
substituting␈α
that␈α
list␈α
for␈α␈↓↓u␈↓␈α
whenever␈α
␈↓↓u␈↓␈α
is␈α
encountered␈α
and␈αre-
␈↓ ↓H␈↓evaluating␈αthe␈αright␈αhand␈αside␈αwith␈αa␈αnew␈α␈↓↓u␈↓␈αwhenever␈αa␈αterm␈α␈↓↓alt␈αe␈↓␈αis␈αencountered.␈α This␈αprocess
␈↓ ↓H␈↓is best illsutrated by evaluating some expressions.

␈↓ ↓H␈↓        Consider␈α
evaluating␈α
␈↓↓alt ␈↓¬NIL␈↓↓␈↓.␈α
 We␈αdo␈α
it␈α
by␈α
evaluating␈α
the␈αexpression␈α
to␈α
the␈α
right␈α
of␈αthe␈α
 "←" 
␈↓ ↓H␈↓in␈α
(8.1)␈α
remembering␈α
that␈αthe␈α
variable␈α
␈↓↓u␈↓␈α
has␈α
the␈αvalue␈α
␈↓¬NIL␈↓.␈α
 A␈α
conditional␈α
expression␈αis␈α
evaluated
␈↓ ↓H␈↓by␈α∂first␈α∂evaluating␈α∂the␈α∂first␈α∂propositional␈α∂term␈α∂-␈α∞in␈α∂this␈α∂case␈α∂␈↓↓␈↓αn|␈↓↓u␈α∂∨␈α∂␈↓αn|␈↓↓␈↓αd|␈↓↓u␈↓.␈α∂This␈α∂expression␈α∂is␈α∞a
␈↓ ↓H␈↓disjunction␈α∂so␈α∂we␈α∂first␈α∂evaluate␈α∂the␈α∂first␈α∞disjunct,␈α∂namely␈α∂␈↓αn|␈↓␈↓↓u.␈↓␈α∂(Recall␈α∂the␈α∂convention␈α∂given␈α∞in
␈↓ ↓H␈↓section␈α␈↓π∞␈↓␈α7).␈α Since␈α ␈↓↓u␈α=␈α␈↓¬NIL␈↓↓␈↓,␈α ␈↓↓␈↓αn|␈↓↓u␈↓ ␈αis␈αtrue,␈αthe␈αdisjunction␈αis␈αtrue,␈αand␈αthe␈αvalue␈αof␈αthe␈αconditional
␈↓ ↓H␈↓expression␈αis␈αthe␈αvalue␈αof␈αthe␈αterm␈αafter␈αthe␈αfirst␈αtrue␈αpropositional␈αterm.␈αThis␈αterm␈αis␈α␈↓↓u,␈↓␈αand␈αits
␈↓ ↓H␈↓value␈α
is␈α␈↓¬NIL␈↓,␈α
so␈α
the␈αvalue␈α
of␈α ␈↓↓alt[␈↓¬NIL␈↓↓]␈↓ ␈α
is␈α
␈↓¬NIL␈↓.␈α Obeying␈α
the␈αrules␈α
about␈α
the␈αorder␈α
of␈αevaluation␈α
of
␈↓ ↓H␈↓terms␈α∂in␈α∂conditional␈α∞and␈α∂Boolean␈α∂expressions␈α∂is␈α∞important␈α∂in␈α∂this␈α∂case␈α∞since␈α∂if␈α∂we␈α∂didn't␈α∞obey
␈↓ ↓H␈↓these␈α∞rules,␈α∂we␈α∞might␈α∂find␈α∞ourselves␈α∂trying␈α∞to␈α∞evaluate␈α∂ ␈↓↓␈↓αn|␈↓↓␈↓αd|␈↓↓u␈↓ ␈α∞or␈α∂ ␈↓↓alt[␈↓αdd|␈↓↓u]␈↓,␈α∞and␈α∂since␈α∞␈↓↓u␈↓␈α∂is␈α∞␈↓¬NIL␈↓,
␈↓ ↓H␈↓neither␈αof␈αthese␈αhas␈αa␈αvalue.␈α (An␈αattempt␈αto␈αevaluate␈αthem␈αin␈αLISP␈αwould␈αgenerally␈αgive␈αrise␈αto
␈↓ ↓H␈↓an error return.)

␈↓ ↓H␈↓        As␈α
a␈α
second␈α
example,␈α
consider␈α
 ␈↓↓alt ␈↓¬(A B)␈↓↓␈↓.␈α
 Since␈α
neither␈α
␈↓αn|␈↓␈↓↓u␈↓␈α
nor␈α
␈↓αn|␈↓␈↓αd|␈↓␈↓↓u␈↓␈α
is␈α
true␈α
in␈α
this␈αcase,
␈↓ ↓H␈↓the␈αdisjunction␈α ␈↓↓␈↓αn|␈↓↓u␈α∨␈α␈↓αn|␈↓↓␈↓αd|␈↓↓u␈↓ ␈αis␈αfalse␈αand␈α
the␈αvalue␈αof␈αthe␈αexpression␈αis␈αthe␈αvalue␈αof␈α
 ␈↓↓␈↓αa|␈↓↓u . alt ␈↓αdd|␈↓↓u␈↓.
␈↓ ↓H␈↓␈↓αa|␈↓␈↓↓u␈↓␈α⊂has␈α⊂the␈α⊂value␈α⊂␈↓¬A,␈α⊂␈↓and␈α⊂␈↓αdd|␈↓␈↓↓u␈↓␈α⊂has␈α⊂the␈α∂value␈α⊂␈↓¬NIL␈↓,␈α⊂so␈α⊂we␈α⊂must␈α⊂now␈α⊂evaluate␈α⊂␈↓↓alt ␈↓¬NIL␈↓↓␈↓,␈α⊂and␈α∂we
␈↓ ↓H␈↓already␈αknow␈αthat␈αthis␈αis␈α␈↓¬NIL␈↓.␈α Therefore,␈αthe␈αvalue␈αof␈α ␈↓↓alt ␈↓¬(A B)␈↓↓␈↓ ␈αis␈αthat␈αof␈α ␈↓¬A . NIL␈↓ ␈αwhich␈αis
␈↓ ↓H␈↓ ␈↓¬(A)␈↓.

␈↓ ↓H␈↓        We can describe this evaluation as a sequence of equations as follows

␈↓ ↓H␈↓␈↓ αX␈↓↓alt ␈↓¬(A B)␈↓↓␈↓␈↓ βX␈↓↓= ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓¬(A B)␈↓↓ ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬(A B)␈↓↓ ␈↓αthen␈↓↓ ␈↓¬(A B)␈↓↓  ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬(A B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓αif␈↓↓ ␈↓αfalse␈↓↓ ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬(A B)␈↓↓ ␈↓αthen␈↓↓ ␈↓¬(A B)␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬(A B)␈↓↓]      ␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓αif␈↓↓ ␈↓αfalse␈↓↓ ␈↓αthen␈↓↓ ␈↓¬(A B)␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬(A B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬(A B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓¬A ␈↓↓. alt[␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓¬A ␈↓↓. [␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓¬NIL␈↓↓ ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓  ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬NIL␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬NIL␈↓↓]]␈↓
␈↓ ↓H␈↓14␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓¬A ␈↓↓. [␈↓αif␈↓↓ ␈↓αtrue␈↓↓ ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓  ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬NIL␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬NIL␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓¬A ␈↓↓. [␈↓αif␈↓↓ ␈↓αtrue␈↓↓ ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓  ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬NIL␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬NIL␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓¬A ␈↓↓. [␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓¬(A)␈↓↓␈↓.


␈↓ ↓H␈↓        This␈αis␈α
still␈αvery␈α
long-winded,␈αand␈αnow␈α
that␈αthe␈α
reader␈αunderstands␈α
the␈αorder␈αof␈α
evaluation
␈↓ ↓H␈↓of conditional and Boolean expressions, we can proceed more briefly to evaluate

␈↓ ↓H␈↓␈↓ β(␈↓↓alt[␈↓¬(A B C D E)␈↓↓]␈↓␈↓ ¬_␈↓↓= ␈↓¬A ␈↓↓. alt[␈↓¬(C D E)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬A ␈↓↓. [␈↓¬C ␈↓↓. alt[␈↓¬(E)␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬A ␈↓↓. [␈↓¬(C E)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬(A C E)␈↓↓␈↓.


␈↓ ↓H␈↓        Here␈αare␈αthree␈αmore␈αexamples␈αof␈αrecursive␈αfunctions␈αand␈αtheir␈αapplication.␈α We␈αdefine␈α␈↓↓last␈↓
␈↓ ↓H␈↓by

␈↓ ↓H␈↓8.2) ␈↓ ∧U␈↓↓last u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ ␈↓αa|␈↓↓u ␈↓αelse␈↓↓ last ␈↓αd|␈↓↓u␈↓, 

␈↓ ↓H␈↓and we compute

␈↓ ↓H␈↓␈↓ βh␈↓↓last ␈↓¬(A B C)␈↓↓␈↓␈↓ ¬_␈↓↓= ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬(A B C)␈↓↓ ␈↓αthen␈↓↓ ␈↓αa|␈↓↓␈↓¬(A B C)␈↓↓  ␈↓αelse␈↓↓ last ␈↓αd|␈↓↓␈↓¬(A B C)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= last ␈↓¬(B C)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= last ␈↓¬(C)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬C␈↓↓␈↓.


␈↓ ↓H␈↓Clearly␈α ␈↓↓last␈↓␈α computes␈αthe␈αlast␈αelement␈αof␈αa␈αlist.␈α ␈↓↓last ␈↓¬NIL␈↓↓␈↓␈αis␈αundefined␈αin␈αthe␈αLISP␈αlanguage;␈αthe
␈↓ ↓H␈↓result␈αof␈αtrying␈αto␈αcompute␈αit␈αmay␈αbe␈αan␈αerror␈αmessage␈αor␈αmay␈αbe␈αsome␈αrandom␈αresult␈αdepending
␈↓ ↓H␈↓on␈αthe␈αimplementation.␈α (A␈αheavy␈αduty␈αversion␈αof␈α␈↓↓last␈↓␈αwould␈αexplicitly␈αcall␈αa␈αfunction␈αcalled␈α␈↓↓error␈↓
␈↓ ↓H␈↓with a string expressing the complaint that the program had tried to compute ␈↓↓last␈↓ ␈↓¬NIL␈↓.)  

␈↓ ↓H␈↓        The function  ␈↓↓subst␈↓  is defined by

␈↓ ↓H␈↓8.3) ␈↓ α∂␈↓↓subst[x, y, z] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ [␈↓αif␈↓↓ z ␈↓αeq␈↓↓ y ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ z] ␈↓αelse␈↓↓ subst[x,y,␈↓αa|␈↓↓z] . subst[x,y,␈↓αd|␈↓↓z]␈↓. 

␈↓ ↓H␈↓We have

␈↓ ↓H␈↓␈↓ ↓x␈↓↓subst[␈↓¬(A.B), X, ((X.A).X)␈↓↓] ␈↓␈↓ ¬_␈↓↓= subst[␈↓¬(A.B), X, (X.A)␈↓↓] . subst[␈↓¬(A.B), X, X␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= [subst[␈↓¬(A.B), X, X␈↓↓] . subst[␈↓¬(A.B), X, A␈↓↓]] . ␈↓¬(A.B)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= [[␈↓¬(A.B)␈↓↓].␈↓¬A␈↓↓].␈↓¬(A.B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬(((A.B).A).(A.B))␈↓↓␈↓.


␈↓ ↓H␈↓␈↓↓subst␈↓␈αcomputes␈αthe␈αresult␈αof␈αsubstituting␈αthe␈αS-expression␈α␈↓↓x␈↓␈αfor␈αthe␈αatom␈α␈↓↓y␈↓␈αin␈αthe␈αS-expression␈α␈↓↓z.␈↓
␈↓ ↓H␈↓This operation is important in all kinds of symbolic computation.

␈↓ ↓H␈↓        The␈α∩function␈α∩␈↓↓append[u,v]␈↓␈α∩which␈α∩gives␈α∩the␈α∪concatenation␈α∩of␈α∩the␈α∩lists␈α∩␈↓↓u␈↓␈α∩and␈α∩␈↓↓v␈↓␈α∪is␈α∩also
␈↓ ↓H␈↓important.  It is also denoted by the infixed expression  ␈↓↓u * v␈↓.  For example we have
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *15



␈↓ ↓H␈↓␈↓ ∧3␈↓↓␈↓¬  (A B C)␈↓↓ * ␈↓¬(D E F)␈↓↓ = ␈↓¬(A B C D E F)␈↓↓,␈↓

␈↓ ↓H␈↓␈↓ ∧S␈↓↓␈↓¬NIL␈↓↓ * ␈↓¬(A B)␈↓↓ =␈↓¬(A B)␈↓↓ = ␈↓¬(A B)␈↓↓ * ␈↓¬NIL␈↓↓,␈↓

␈↓ ↓H␈↓and the formal definition

␈↓ ↓H␈↓8.4) ␈↓ ∧C␈↓↓u * v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u] * v␈↓. 

␈↓ ↓H␈↓The␈α∞␈↓↓append␈↓␈α∞function␈α
is␈α∞machine␈α∞coded␈α
in␈α∞most␈α∞Lisp␈α
systems␈α∞in␈α∞a␈α
way␈α∞that␈α∞allows␈α∞an␈α
arbitrary
␈↓ ↓H␈↓number␈α∃of␈α∀arguments,␈α∃e.g.␈α∃ ␈↓¬(APPEND␈α∀(QUOTE␈α∃(A␈α∃B))␈α∀(QUOTE␈α∃(C␈α∃D))␈α∀(QUOTE␈α∃(E␈α∃F)))␈↓␈α∀is
␈↓ ↓H␈↓␈↓¬(A B C D E F)␈↓.

␈↓ ↓H␈↓        The␈α⊂Boolean␈α∂operations␈α⊂can␈α⊂also␈α∂be␈α⊂used␈α∂in␈α⊂making␈α⊂recursive␈α∂definitions␈α⊂since␈α⊂we␈α∂take
␈↓ ↓H␈↓advantage of the order of evaluation of constituents.  Thus, we define a predicate  ␈↓↓equal␈↓  by

␈↓ ↓H␈↓8.5) ␈↓ β↓␈↓↓equal[x,y] ← x ␈↓αeq␈↓↓ y ∨ [¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y ∧  equal[␈↓αa|␈↓↓x,␈↓αa|␈↓↓y] ∧ equal[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y]]␈↓. 

␈↓ ↓H␈↓␈↓↓equal[x,y]␈↓␈αis␈αtrue␈αif␈αand␈αonly␈αif␈α␈↓↓x␈↓␈αand␈α␈↓↓y␈↓␈αare␈αthe␈αsame␈αS-expression,␈αand␈αthe␈αuse␈αof␈αthis␈αpredicate
␈↓ ↓H␈↓makes␈αup␈αfor␈αthe␈αfact␈αthat␈αthe␈αbasic␈αpredicate␈α␈↓αeq␈↓␈αis␈αguaranteed␈αto␈αtest␈αequality␈αonly␈αwhen␈αone␈αof
␈↓ ↓H␈↓the operands is known to be an atom.  We shall also use the infixes  ␈↓α=␈↓  and  ␈↓α≠␈↓.

␈↓ ↓H␈↓        Membership of an S-expression ␈↓↓x␈↓ in a list ␈↓↓u␈↓ is tested by

␈↓ ↓H␈↓8.6) ␈↓ β{␈↓↓member[x, u] ← ¬␈↓αn|␈↓↓u ∧ [[x = ␈↓αa|␈↓↓u] ∨ member[x, ␈↓αd|␈↓↓u]]␈↓. 

␈↓ ↓H␈↓This relation is also denoted by ␈↓↓x ε u␈↓.  Here are some computations:

␈↓ ↓H␈↓␈↓ βλ␈↓↓member[␈↓¬B, ␈↓↓␈↓¬(A B)␈↓↓]␈↓␈↓ ¬_␈↓↓= ¬␈↓αn|␈↓↓␈↓¬(A B)␈↓↓ ∧ [[␈↓¬B ␈↓↓= ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓]  ∨ member[␈↓¬B, ␈↓↓␈↓αd|␈↓↓␈↓¬(A B)␈↓↓]]␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= member[␈↓¬B, ␈↓↓␈↓¬(B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓αtrue␈↓↓␈↓.


␈↓ ↓H␈↓        Sometimes␈α
a␈α
function␈α
is␈α
defined␈α
with␈α
the␈α
help␈α
of␈α
auxiliary␈α
functions.␈α
 Thus,␈α
the␈α
reverse␈α
of␈α
a
␈↓ ↓H␈↓list ␈↓↓u␈↓ , (e.g.  ␈↓↓reverse[␈↓¬(A B C D)␈↓↓] = ␈↓¬(D C B A)␈↓↓␈↓), is given by the pair of equations

␈↓ ↓H␈↓␈↓ ¬,␈↓↓reverse[u] ← rev[u, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓8.7)
␈↓ ↓H␈↓␈↓ ∧≥␈↓↓rev[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ rev[␈↓αd|␈↓↓u, [␈↓αa|␈↓↓u].v]␈↓.

␈↓ ↓H␈↓A computation is:

␈↓ ↓H␈↓␈↓ β8␈↓↓reverse[␈↓¬(A B C)␈↓↓]␈↓␈↓ ¬_␈↓↓= rev[␈↓¬(A B C)␈↓↓, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= rev[␈↓¬(B C), (A)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= rev[␈↓¬(C), (B A)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= rev[␈↓¬NIL␈↓↓, ␈↓¬(C B A)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬(C B A)␈↓↓␈↓.


␈↓ ↓H␈↓A more elaborate example of recursive definition is given by
␈↓ ↓H␈↓16␈↓ εβChapter  I␈↓ H



␈↓ ↓H␈↓␈↓ βh␈↓↓flatten[x] ← flat[x, ␈↓¬NIL␈↓↓]                                       ␈↓
␈↓ ↓H␈↓8.8)
␈↓ ↓H␈↓␈↓ βd␈↓↓flat[x, u] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x.u ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓x, flat[␈↓αd|␈↓↓x, u]]␈↓.

␈↓ ↓H␈↓We have

␈↓ ↓H␈↓␈↓ αx␈↓↓flatten[␈↓¬((A.B).C)␈↓↓]␈↓␈↓ ¬_␈↓↓= flat[␈↓¬((A.B).C)␈↓↓, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= flat[␈↓¬(A.B)␈↓↓, flat[␈↓¬C, ␈↓↓␈↓¬NIL␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= flat[␈↓¬(A.B), (C)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= flat[␈↓¬A, ␈↓↓flat[␈↓¬B, (C)␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= flat[␈↓¬A, (B C)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬(A B C)␈↓↓␈↓.


␈↓ ↓H␈↓The␈αreader␈αwill␈α
see␈αthat␈αthe␈αvalue␈α
of␈α␈↓↓flatten[x]␈↓␈αis␈αa␈α
list␈αof␈αthe␈αatoms␈α
of␈αthe␈αS-expression␈α
␈↓↓x␈↓␈αfrom
␈↓ ↓H␈↓left to right.  Thus ␈↓↓flatten[␈↓¬((A B) A)␈↓↓] = ␈↓¬(A B NIL A NIL)␈↓↓␈↓.

␈↓ ↓H␈↓        Many␈α⊃functions␈α⊃can␈α⊃be␈α∩conveniently␈α⊃written␈α⊃in␈α⊃more␈α∩than␈α⊃one␈α⊃way.␈α⊃ For␈α∩example,␈α⊃the
␈↓ ↓H␈↓function ␈↓↓reverse␈↓ mentioned above can be defined without an auxiliary function as follows:

␈↓ ↓H␈↓8.9) ␈↓ βn␈↓↓reverse1 u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ reverse1 ␈↓αd|␈↓↓u * <␈↓αa|␈↓↓u>␈↓, 

␈↓ ↓H␈↓but␈α⊃the␈α⊂earlier␈α⊃definition␈α⊂involves␈α⊃less␈α⊂computation,␈α⊃because␈α⊂*␈α⊃takes␈α⊂time␈α⊃proportional␈α⊃to␈α⊂the
␈↓ ↓H␈↓length␈αof␈αits␈αfirst␈αargument.␈α Similarly␈αthe␈αfunction␈αcomputed␈αby␈α␈↓↓flatten␈↓␈αcan␈αalso␈αbe␈αcomputed␈αby
␈↓ ↓H␈↓the simpler but less efficient program ␈↓↓fringe␈↓

␈↓ ↓H␈↓8.10) ␈↓ βi␈↓↓fringe x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x␈↓, 

␈↓ ↓H␈↓        The␈α∪use␈α∪of␈α∪conditional␈α∪expressions␈α∪for␈α∪recursive␈α∪function␈α∪definition␈α∪is␈α∪not␈α∀limited␈α∪to
␈↓ ↓H␈↓functions␈α
of␈α
S-expressions.␈α For␈α
example,␈α
the␈αfactorial␈α
function␈α
and␈αthe␈α
Euclidean␈α
algorithm␈αfor
␈↓ ↓H␈↓the greatest common divisor are expressed as follows:

␈↓ ↓H␈↓8.11) ␈↓ ∧t␈↓↓n! ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ n␈↓π*␈↓↓[n-␈↓¬1␈↓↓]!␈↓ 

␈↓ ↓H␈↓and

␈↓ ↓H␈↓8.12) ␈↓ αO␈↓↓gcd[m, n] ← ␈↓αif␈↓↓ m>n ␈↓αthen␈↓↓ gcd[n, m] ␈↓αelse␈↓↓ ␈↓αif␈↓↓ m=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ n ␈↓αelse␈↓↓ gcd[n mod m, m]␈↓ 

␈↓ ↓H␈↓where␈α⊂␈↓↓n␈α⊂mod␈α⊂m␈↓␈α⊂denotes␈α⊂the␈α⊂remainder␈α⊂when␈α⊂␈↓↓n␈↓␈α⊂is␈α⊂divided␈α⊂by␈α⊂␈↓↓m␈↓␈α⊂and␈α⊂may␈α⊂itself␈α⊂be␈α⊂expressed
␈↓ ↓H␈↓recursively by

␈↓ ↓H␈↓8.13)␈↓ ∧)␈↓↓n mod m ← ␈↓αif␈↓↓ n<m ␈↓αthen␈↓↓ n ␈↓αelse␈↓↓ [n-m] mod m␈↓. 

␈↓ ↓H␈↓(These␈α∂definitions␈α∂are␈α∂of␈α∂functions␈α∂on␈α∂the␈α∂natural␈α∞numbers␈α∂␈↓¬0,␈α∂␈↓␈↓¬1,␈α∂␈↓...␈α∂ and␈α∂are␈α∂not␈α∂intended␈α∞to
␈↓ ↓H␈↓apply to integers in general.)

␈↓ ↓H␈↓        The␈αinternal␈αform␈αof␈αfunction␈αdefinitions␈αdepends␈αon␈αthe␈αimplementation.␈α MACLISP␈αuses
␈↓ ↓H␈↓the form

␈↓ ↓H␈↓␈↓ β-(␈↓¬DEFUN ␈↓<function name> <list of variables> <right hand side>).
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *17


␈↓ ↓H␈↓Stanford␈αLISP␈αand␈αUCI␈αLISP␈αfor␈αthe␈αPDP-10␈αcomputer␈αuse␈αthe␈αsame␈αform␈αwith␈α␈↓¬DE␈α␈↓␈αin␈αplace␈αof
␈↓ ↓H␈↓␈↓¬DEFUN. ␈↓ Thus in MACLISP the definition of ␈↓↓subst␈↓ is

␈↓ ↓H␈↓¬␈↓ α(DEFUN SUBST (X Y Z)
␈↓ ↓H␈↓¬␈↓ α(  (COND ((ATOM Z) (COND ((EQ Z X) Y) (T Z)))
␈↓ ↓H␈↓¬␈↓ α(        (T (CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z)))))),


␈↓ ↓H␈↓and the definition of ␈↓↓alt␈↓ is

␈↓ ↓H␈↓¬␈↓ αH(DEFUN ALT (U)
␈↓ ↓H␈↓¬␈↓ αH  (COND ((OR (NULL U) (NULL (CDR U))) U)
␈↓ ↓H␈↓¬␈↓ αH        (T (CONS (CAR U) (ALT (CDDR U)))))).


␈↓ ↓H␈↓Yet␈α
another␈α
notation␈α
for␈α
function␈α
definition␈αcalled␈α
the␈α
␈↓¬DEFPROP␈α
␈↓notation␈α
will␈α
be␈α
explained␈αafter
␈↓ ↓H␈↓λ-expressions have been introduced.


␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓        1. Consider the function ␈↓↓drop␈↓ defined by

␈↓ ↓H␈↓␈↓ ∧⊃␈↓↓drop[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ <␈↓αa|␈↓↓u> . drop[␈↓αd|␈↓↓u]␈↓. 

␈↓ ↓H␈↓Compute␈α⊃(by␈α⊃hand)␈α⊃␈↓↓drop[␈↓¬(A␈α⊃B␈α⊃C)␈↓↓]␈↓.␈α⊃ What␈α∩does␈α⊃␈↓↓drop␈↓␈α⊃do␈α⊃to␈α⊃lists␈α⊃in␈α⊃general?␈α⊃  Write␈α∩␈↓↓drop␈↓␈α⊃in
␈↓ ↓H␈↓internal notation using ␈↓¬DEFUN. ␈↓

␈↓ ↓H␈↓        2. What does the function

␈↓ ↓H␈↓␈↓ ∧∞␈↓↓r2[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ reverse[␈↓αa|␈↓↓u] . r2[␈↓αd|␈↓↓u]␈↓ 

␈↓ ↓H␈↓do to lists of lists?  How about

␈↓ ↓H␈↓␈↓ ∧∞␈↓↓r3[x] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ reverse[r4[x]]            ␈↓ 

␈↓ ↓H␈↓␈↓ ∧∩␈↓↓r4[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ r3[␈↓αa|␈↓↓u] . r4[␈↓αd|␈↓↓u]?    ␈↓ 

␈↓ ↓H␈↓        3. Compare

␈↓ ↓H␈↓␈↓ ∧ε␈↓↓r3'[x] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ <r3'[␈↓αd|␈↓↓x]> * <r3'[␈↓αa|␈↓↓x]>␈↓ 

␈↓ ↓H␈↓with the function ␈↓↓r3␈↓ of the preceding example.

␈↓ ↓H␈↓        4. Consider ␈↓↓r5␈↓ defined by

␈↓ ↓H␈↓␈↓ β∪␈↓↓r5[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ u  ␈↓αelse␈↓↓ [␈↓αa|␈↓↓r5[␈↓αd|␈↓↓u]] . r5[␈↓αa|␈↓↓u . r5[␈↓αd|␈↓↓r5[␈↓αd|␈↓↓u]]]␈↓. 

␈↓ ↓H␈↓Compute␈α␈↓↓r5[␈↓¬(A␈αB␈αC␈αD)␈↓↓]␈↓.␈α What␈αdoes␈α␈↓↓r5␈↓␈αdo?␈α  Needless␈αto␈αsay,␈αthis␈αis␈αnot␈αa␈αgood␈αway␈αof␈αcomputing
␈↓ ↓H␈↓this␈α⊂function␈α⊃even␈α⊂though␈α⊂it␈α⊃involves␈α⊂no␈α⊂auxiliary␈α⊃functions.␈α⊂ [This␈α⊂ingeneous␈α⊃definition␈α⊂was
␈↓ ↓H␈↓discovered by S. Ness]
␈↓ ↓H␈↓18␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓9.  ␈↓αNumerical computation.␈↓


␈↓ ↓H␈↓        Numerical␈α
calculation␈α
and␈α
symbolic␈α
calculation␈α
must␈α
often␈α
be␈α
combined,␈α
so␈α
LISP␈αprovides
␈↓ ↓H␈↓for␈α⊃numerical␈α∩computation␈α⊃also.␈α∩ In␈α⊃the␈α∩first␈α⊃place,␈α∩we␈α⊃need␈α∩to␈α⊃include␈α∩numbers␈α⊃as␈α∩parts␈α⊃of
␈↓ ↓H␈↓symbolic␈α
expressions.␈α
 LISP␈αhas␈α
both␈α
integer␈αand␈α
floating␈α
point␈αnumbers␈α
which␈α
are␈α
regarded␈αas
␈↓ ↓H␈↓atoms.␈α
 These␈α∞numbers␈α
may␈α
be␈α∞included␈α
as␈α
atoms␈α∞in␈α
writing␈α
S-expressions.␈α∞ Thus␈α
we␈α∞can␈α
have
␈↓ ↓H␈↓the lists:

␈↓ ↓H␈↓¬␈↓ ¬_(1 3 5)
␈↓ ↓H␈↓¬␈↓ ¬_(3.5 6.1 -7.2E9)
␈↓ ↓H␈↓¬␈↓ ¬_(PLUS X 1.3).


␈↓ ↓H␈↓The␈αfirst␈αis␈αa␈αlist␈αof␈α
integers,␈αthe␈αsecond␈αa␈αlist␈αof␈α
floating␈αpoint␈αnumbers,␈αand␈αthe␈αthird␈αa␈α
symbolic
␈↓ ↓H␈↓list␈α∂containing␈α∂both␈α∂numerical␈α∂and␈α∂non-numerical␈α∂atoms.␈α∂ Integers␈α∂are␈α∂written␈α⊂without␈α∂decimal
␈↓ ↓H␈↓points␈αwhich␈αare␈α
used␈αto␈αsignal␈α
floating␈αpoint␈αnumbers.␈α As␈α
in␈αFORTRAN,␈αthe␈α
letter␈αE␈αis␈αused␈α
to
␈↓ ↓H␈↓signal␈αthe␈αexponent␈αof␈αa␈αfloating␈αpoint␈αnumber␈αwhich␈αis␈αa␈αsigned␈αinteger.␈α The␈αsizes␈α
of␈αnumbers
␈↓ ↓H␈↓admitted␈αdepends␈αon␈αthe␈αimplementation.␈α When␈αa␈αdotted␈αpair,␈αsay␈α␈↓¬(1␈α.␈α2)␈↓␈αis␈αwanted,␈αthe␈αspaces
␈↓ ↓H␈↓around␈αthe␈αdot␈αdistinguish␈αit␈αfrom␈αthe␈αlist␈α␈↓¬(1.2)␈↓␈αwhose␈αsole␈αelement␈αis␈αthe␈αfloating␈αpoint␈αnumber
␈↓ ↓H␈↓1.2.

␈↓ ↓H␈↓        In␈α∞external␈α∞language␈α∞we␈α∞will␈α∞use␈α∞ordinary␈α∞mathematical␈α∞notation␈α∞for␈α∂numerical␈α∞functions.
␈↓ ↓H␈↓As␈α
an␈α
example␈α
of␈αa␈α
function␈α
combining␈α
numeric␈α
and␈αsymbolic␈α
calculation␈α
we␈α
have␈α
the␈αfunction
␈↓ ↓H␈↓giving the length of a list defined by

␈↓ ↓H␈↓9.1)␈↓ ∧.␈↓↓length u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ 0 ␈↓αelse␈↓↓ 1 + length ␈↓αd|␈↓↓u␈↓. 

␈↓ ↓H␈↓The internal notation for numerical functions is shown in the following examples:

␈↓ ↓H␈↓␈↓ ¬_␈↓¬(PLUS X Y ... Z)␈↓ for ␈↓↓x+y+...+z␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(TIMES X ... Z)␈↓ for ␈↓↓xy...z␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(MINUS X)␈↓ for ␈↓↓-x␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(DIFFERENCE X Y)␈↓ for ␈↓↓x-y␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(QUOTIENT X Y)␈↓ for ␈↓↓x/y␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(POWER X Y)␈↓ for ␈↓↓x␈↓∧y␈↓.


␈↓ ↓H␈↓        Besides␈α
functions␈αof␈α
numbers␈α
we␈αneed␈α
predicates␈α
on␈αnumbers␈α
and␈α
the␈αusual␈α
=,␈α
<,␈α>,␈α
≤,␈αand␈α
≥
␈↓ ↓H␈↓are␈α∀used␈α∪with␈α∀the␈α∀internal␈α∪names␈α∀␈↓¬EQUAL,␈α∀␈↓␈↓¬LESSP,␈α∪␈↓␈↓¬GREATERP,␈α∀␈↓␈↓¬LESSEQP,␈α∀␈↓and␈α∪␈↓¬GREATEREQP,
␈↓ ↓H␈↓¬␈↓respectively.␈α
 Not␈αall␈α
are␈α
implemented␈αin␈α
all␈αLISP␈α
systems,␈α
but␈αof␈α
course␈αthe␈α
remaining␈α
ones␈αcan
␈↓ ↓H␈↓be defined.  An additional predicate, ␈↓↓numberp,␈↓ is used to distinguish numbers from other atoms.

␈↓ ↓H␈↓        Since␈α∂numbers␈α∂that␈α∂form␈α∂part␈α∂of␈α∂list␈α∂structures␈α∂must␈α∂be␈α∂represented␈α∂by␈α∂pointers␈α∂anyway,
␈↓ ↓H␈↓there␈α⊃is␈α∩room␈α⊃for␈α∩a␈α⊃flag␈α∩distinguishing␈α⊃floating␈α∩point␈α⊃numbers␈α∩and␈α⊃integers.␈α∩ Therefore,␈α⊃the
␈↓ ↓H␈↓arithmetic␈α⊃operations␈α⊂are␈α⊃programmed␈α⊂to␈α⊃treat␈α⊂types␈α⊃dynamically,␈α⊂i.e.␈α⊃a␈α⊂variable␈α⊃may␈α⊃take␈α⊂an
␈↓ ↓H␈↓integer␈αvalue␈αat␈α
one␈αstep␈αof␈αcomputation␈α
and␈αa␈αreal␈αvalue␈α
at␈αanother.␈α The␈α
subroutines␈αrealizing
␈↓ ↓H␈↓the arithmetic functions make the appropriate tests and create results of appropriate types.

␈↓ ↓H␈↓        It␈α_is␈α→worth␈α_remarking␈α→that␈α_including␈α_type␈α→flags␈α_in␈α→numbers␈α_would␈α→benefit␈α_many
␈↓ ↓H␈↓programming languages besides LISP and would not cost much in either storage or hardware.
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *19


␈↓ ↓H␈↓        Dynamic␈α⊂typing␈α⊂of␈α∂variables␈α⊂is␈α⊂slow␈α∂compared␈α⊂to␈α⊂direct␈α∂use␈α⊂of␈α⊂the␈α⊂machine's␈α∂arithmetic
␈↓ ↓H␈↓instructions,␈α∀so␈α∀that␈α∀LISP␈α∪can␈α∀be␈α∀efficiently␈α∀used␈α∪interpretatively␈α∀only␈α∀when␈α∀the␈α∪numerical
␈↓ ↓H␈↓calculations␈α
are␈αsmall␈α
or␈α
at␈αleast␈α
small␈αcompared␈α
to␈α
the␈αsymbolic␈α
calculations␈αin␈α
a␈α
problem.␈α The
␈↓ ↓H␈↓MACLSP␈α∪compiler␈α∩NCOMPLR␈α∪is␈α∩able␈α∪to␈α∩generate␈α∪efficient␈α∩arithmetic␈α∪code.␈α∪ In␈α∩particular,
␈↓ ↓H␈↓variables␈αcan␈α
be␈αdeclared␈αto␈α
be␈αof␈αa␈α
fixed␈αnumerical␈α
type␈αand␈αthe␈α
correct␈αmachine␈αinstructions␈α
are
␈↓ ↓H␈↓then␈α⊂generated␈α⊃so␈α⊂that␈α⊃runtime␈α⊂testing␈α⊂is␈α⊃not␈α⊂necessary.␈α⊃ Also,␈α⊂it␈α⊂uses␈α⊃separate␈α⊂stacks␈α⊃to␈α⊂store
␈↓ ↓H␈↓numerical␈αresults␈αso␈αthat␈αunecessary␈αconversion␈αfrom␈αthe␈αLISP␈αrepresentation␈αof␈αa␈αnumber␈αto␈αthe
␈↓ ↓H␈↓machine␈αrepresentation␈αcan␈αbe␈αavoided.␈α This␈αsaves␈αboth␈αtime␈αand␈αspace␈αas␈αeach␈αconversion␈αfrom
␈↓ ↓H␈↓an machine number to a LISP number requires a ␈↓↓cons␈↓ operation.

␈↓ ↓H␈↓        LISP␈α
can␈α
also␈α
deal␈α
with␈α
integers␈α
too␈α
large␈α
to␈α
be␈α
represented␈α
as␈α
a␈α
single␈α
machine␈α
word.␈α
 Such
␈↓ ↓H␈↓numbers␈α
are␈α∞called␈α
"bignums"␈α∞and␈α
are␈α∞repsented␈α
in␈α
LISP␈α∞by␈α
a␈α∞pointer␈α
to␈α∞a␈α
list␈α∞structure␈α
which
␈↓ ↓H␈↓contains␈αthe␈αsign,␈α
a␈αflag␈αsaying␈αthat␈α
this␈αa␈α"bignum",␈α
and␈αa␈αlist␈αof␈α
the␈αnumbers␈αcorresponding␈αto␈α
a
␈↓ ↓H␈↓base␈α∪B␈α∩representation␈α∪of␈α∩the␈α∪number␈α∩for␈α∪some␈α∩suitable␈α∪B␈α∩(depending␈α∪on␈α∩the␈α∪machine␈α∩and
␈↓ ↓H␈↓implementation).

␈↓ ↓H␈↓        As␈α∃another␈α∀example␈α∃of␈α∀a␈α∃combined␈α∀numeric␈α∃and␈α∀symbolic␈α∃computation,␈α∀we␈α∃give␈α∀an
␈↓ ↓H␈↓evaluator␈α⊂for␈α⊂expressions␈α⊂with␈α⊂sums␈α⊂and␈α⊃products.␈α⊂ The␈α⊂expressions␈α⊂are␈α⊂built␈α⊂up␈α⊃from␈α⊂atoms
␈↓ ↓H␈↓denoting variables and integer constants according to the syntax

␈↓ ↓H␈↓                <expression> ::= <variable> | <integer> | (␈↓¬PLUS ␈↓<explist>) | (␈↓¬TIMES ␈↓<explist>)
␈↓ ↓H␈↓9.2)
␈↓ ↓H␈↓                     <explist> ::= <expression> | <expression><explist>


␈↓ ↓H␈↓Thus␈αa␈αlist␈αof␈αexpressions␈αbeginning␈αwith␈αthe␈αatom␈α␈↓¬PLUS␈α␈↓represents␈αthe␈αsum␈αof␈αthese␈αexpressions
␈↓ ↓H␈↓and␈α∀a␈α∀list␈α∀of␈α∀expressions␈α∀beginning␈α∀with␈α∀the␈α∀atom␈α∀␈↓¬TIMES␈α∀␈↓represents␈α∀the␈α∀product␈α∃of␈α∀these
␈↓ ↓H␈↓expressions.␈α In␈αorder␈αto␈αevaluate␈αan␈αexpression␈α
we␈αneed␈αa␈αway␈αof␈αgiving␈αvalues␈αto␈α
the␈αvariables
␈↓ ↓H␈↓occuring␈α
in␈αthe␈α
expression.␈α  We␈α
will␈αuse␈α
an␈α␈↓↓association␈↓␈α
␈↓↓list␈↓␈αfor␈α
this␈αpurpose.␈α
 An␈α
association␈αlist
␈↓ ↓H␈↓(a-list)␈α
is␈α
a␈α∞list␈α
of␈α
pairs,␈α∞in␈α
our␈α
case␈α∞the␈α
first␈α
element␈α
of␈α∞each␈α
pair␈α
is␈α∞a␈α
variable␈α
and␈α∞the␈α
second
␈↓ ↓H␈↓element␈α⊂is␈α⊂the␈α∂value␈α⊂associated␈α⊂with␈α⊂it.␈α∂ For␈α⊂example␈α⊂ ␈↓¬((X␈α∂.␈α⊂5)␈α⊂(Y␈α⊂.␈α∂9.3)␈α⊂(Z␈α⊂.␈α⊂2.1))␈↓.␈α∂ To
␈↓ ↓H␈↓look␈α
up␈α
the␈αvalue␈α
of␈α
a␈αvariable␈α
in␈α
an␈αa-list␈α
we␈α
use␈αthe␈α
function␈α
␈↓↓assoc,␈↓␈αwhich␈α
returns␈α
the␈αfirst␈α
pair
␈↓ ↓H␈↓in␈α∞the␈α∞list␈α∞such␈α∞that␈α
the␈α∞variable␈α∞matches␈α∞the␈α∞variable␈α∞argument.␈α
 It␈α∞returns␈α∞␈↓¬NIL␈↓␈α∞if␈α∞no␈α∞value␈α
is
␈↓ ↓H␈↓found.  Thus

␈↓ ↓H␈↓␈↓ βG␈↓↓assoc[␈↓¬Y␈↓↓, ␈↓¬((X . 5) (Y . 9.3) (Z . 2.1))␈↓↓] = ␈↓¬(Y . 9.3)␈↓↓␈↓ 

␈↓ ↓H␈↓and the definition of ␈↓↓assoc␈↓ is given by

␈↓ ↓H␈↓9.3) ␈↓ α{␈↓↓assoc[x,a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓a ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓a ␈↓αeq␈↓↓ x ␈↓αthen␈↓↓ ␈↓αa|␈↓↓a ␈↓αelse␈↓↓ assoc[x,␈↓αd|␈↓↓a].␈↓ 

␈↓ ↓H␈↓Association␈αlists␈αare␈αgenerally␈αuseful␈αfor␈αassociating␈α"values"␈αwith␈α"symbols"␈αand␈αthey␈α
will␈αappear
␈↓ ↓H␈↓in␈α
many␈α∞later␈α
examples.␈α∞ We␈α
note␈α∞here␈α
two␈α∞features␈α
which␈α∞are␈α
due␈α∞to␈α
the␈α∞way␈α
␈↓↓assoc␈↓␈α∞is␈α
defined.
␈↓ ↓H␈↓Since␈α
that␈α
␈↓↓assoc␈↓␈αreturns␈α
the␈α
pair␈α
rather␈αthan␈α
the␈α
value␈αwhen␈α
the␈α
desired␈α
symbol␈αis␈α
found␈α
or␈α␈↓¬NIL␈↓␈α
if
␈↓ ↓H␈↓no␈α
value␈α
is␈α
found,␈α
the␈α∞case␈α
of␈α
no␈α
value␈α
found␈α
can␈α∞be␈α
detected.␈α
 If␈α
just␈α
the␈α
value␈α∞were␈α
returned
␈↓ ↓H␈↓there␈α
would␈α
be␈αno␈α
distinction␈α
between␈αno␈α
value␈α
and␈αa␈α
value␈α
of␈α␈↓¬NIL␈↓.␈α
 The␈α
calling␈α
program␈αmust
␈↓ ↓H␈↓check␈α∂to␈α∂see␈α∂if␈α∂a␈α∂value␈α∂was␈α∂returned␈α∂(␈↓↓¬␈↓αn|␈↓↓assoc[x,a]␈↓)␈α∂and␈α∂then␈α∂extract␈α∂that␈α∂value␈α∂(␈↓↓␈↓αd|␈↓↓assoc[x,a]␈↓).
␈↓ ↓H␈↓Also,␈α
␈↓↓assoc␈↓␈α
returns␈αthe␈α
first␈α
pair␈αit␈α
finds,␈α
thus␈α
a␈αsymbol␈α
can␈α
be␈αassociated␈α
with␈α
several␈α
values␈αin
␈↓ ↓H␈↓the a-list, but always the first occurence determines the result that is returned.   Thus

␈↓ ↓H␈↓␈↓ α1␈↓↓assoc[␈↓¬X␈↓↓,␈↓¬((U . 1.41) (X . 5) (Y . 9.3) (X . 1.2) (Z . 2.1))␈↓↓] = ␈↓¬(X . 5)␈↓↓␈↓. 
␈↓ ↓H␈↓20␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓␈↓↓assoc␈↓ is built into most LISP systems.

␈↓ ↓H␈↓        The interpreter, ␈↓↓numval,␈↓ can now be defined.

␈↓ ↓H␈↓␈↓ αx␈↓↓numval[e,a]␈↓␈↓ ∧8␈↓↓← ␈↓αif␈↓↓ numberp e ␈↓αthen␈↓↓ e␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ ␈↓αd|␈↓↓assoc[e,a]␈↓
␈↓ ↓H␈↓9.4)␈↓ ∧8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e ␈↓αeq␈↓↓ ␈↓¬PLUS ␈↓↓␈↓αthen␈↓↓ evplus[␈↓αd|␈↓↓e,a]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e ␈↓αeq␈↓↓ ␈↓¬TIMES ␈↓↓␈↓αthen␈↓↓ evtimes[␈↓αd|␈↓↓e,a]␈↓

␈↓ ↓H␈↓where

␈↓ ↓H␈↓9.5) ␈↓ αJ␈↓↓ evplus[u,a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ 0 ␈↓αelse␈↓↓ numval[␈↓αa|␈↓↓u,a] + evplus[␈↓αd|␈↓↓u,a],                  ␈↓

␈↓ ↓H␈↓9.6) ␈↓ αe␈↓↓evtimes[u,a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ numval[␈↓αa|␈↓↓u,a] ␈↓∧.␈↓↓ evtimes[␈↓αd|␈↓↓u,a],             ␈↓





␈↓ ↓H␈↓10.  ␈↓αBoolean Operations on Numbers␈↓


␈↓ ↓H␈↓        In␈α∪addition␈α∪to␈α∪the␈α∩usual␈α∪arithmetic␈α∪operations␈α∪on␈α∩numbers,␈α∪LISP␈α∪also␈α∪allows␈α∩boolean
␈↓ ↓H␈↓operations␈αon␈αnumbers.␈α If␈αyou␈αimagine␈αthat␈αthe␈αnumbers␈α␈↓↓m,␈↓␈αand␈α␈↓↓n␈↓␈αare␈αrepresented␈αas␈αstrings␈αof
␈↓ ↓H␈↓␈↓¬1␈↓s␈α∞and␈α∞␈↓¬0␈↓s␈α∞(of␈α∞some␈α∞fixed␈α∞length␈α∞depending␈α∞on␈α∞the␈α∞machine␈α∞word␈α∞size)␈α∞according␈α∞to␈α∞the␈α∞base␈α
␈↓¬2
␈↓ ↓H␈↓¬␈↓(binary)␈αrepresentation␈αthen␈α␈↓↓m␈α␈↓αbool-op␈↓↓␈αn␈↓␈αdenotes␈αthe␈αnumber␈αwhose␈αbinary␈αrepsentation␈α
is␈αgiven
␈↓ ↓H␈↓by␈α_performing␈α↔the␈α_boolean␈α↔operation␈α_on␈α↔each␈α_pair␈α↔of␈α_corresponding␈α↔bits.␈α_ (The␈α↔binary
␈↓ ↓H␈↓representation␈α⊃of␈α⊂numbers␈α⊃less␈α⊃than␈α⊂zero␈α⊃is␈α⊃dependent␈α⊂on␈α⊃the␈α⊃implementation,␈α⊂in␈α⊃the␈α⊃case␈α⊂of
␈↓ ↓H␈↓MACLISP on a PDP-10 or 20 2's compliment representation is used.)  Thus we have

␈↓ ↓H␈↓␈↓ ¬O␈↓¬4 ␈↓αbool-and␈↓¬ 6 = 4␈↓
␈↓ ↓H␈↓␈↓ ¬R␈↓¬ 4 ␈↓αbool-or␈↓¬ 6 = 6␈↓
␈↓ ↓H␈↓␈↓ ¬P␈↓¬4 ␈↓αbool-xor␈↓¬ 6 = 2␈↓

␈↓ ↓H␈↓Here␈α⊂␈↓αbool-xor␈↓␈α⊂is␈α⊃the␈α⊂exclusive␈α⊂or␈α⊃operation.␈α⊂ In␈α⊂general␈α⊂we␈α⊃will␈α⊂use␈α⊂terminology␈α⊃obtained␈α⊂by
␈↓ ↓H␈↓prefixing␈α␈↓αbool-␈↓␈αto␈αthe␈αusual␈αname␈αof␈αthe␈αcorresponding␈αlogical␈αoperation␈αfor␈αa␈αboolean␈αoperation.
␈↓ ↓H␈↓If␈αwe␈αtreat␈α␈↓¬1␈α
␈↓and␈α␈↓αtrue␈↓␈αand␈α␈↓¬0␈α
␈↓as␈α␈↓αfalse␈↓␈αthen␈αwe␈α
can␈αobtain␈αthe␈αboolean␈α
truth␈αtables␈αfrom␈αthe␈α
logical
␈↓ ↓H␈↓truth tables such as those given in section ␈↓π∞␈↓7.

␈↓ ↓H␈↓        The internal form for boolean operations on numbers is

␈↓ ↓H␈↓␈↓ ¬_(␈↓¬BOOLE ␈↓<code> <n1> <n2>)

␈↓ ↓H␈↓where␈α<code>␈αranges␈αfrom␈α␈↓¬0␈α␈↓to␈α␈↓¬15␈α␈↓(decimal)␈αand␈αeach␈αvalue␈αof␈α<code>␈αcorresponds␈αto␈αa␈αdifferent
␈↓ ↓H␈↓boolean␈α
operation.␈αWe␈α
have␈α
the␈αfollowing␈α
simple␈α
rule␈αfor␈α
determining␈α
what␈αoperation␈α
corresponds
␈↓ ↓H␈↓to␈α
a␈α
particular␈α
value␈α
of␈α
<code>.␈α
 If␈α
the␈αbinary␈α
representation␈α
of␈α
<code>␈α
is␈α
"␈↓¬abcd␈↓"␈α
then␈α
result␈αof␈α
the
␈↓ ↓H␈↓operation on a pair of bits ␈↓¬x ␈↓and ␈↓¬y ␈↓is given in table 2.
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *21



␈↓ ↓H␈↓¬␈↓ ∧8x␈↓ ∧xy␈↓ ¬x(BOOLE "abcd" x y)

␈↓ ↓H␈↓¬␈↓ ∧80␈↓ ∧x0␈↓ π_a
␈↓ ↓H␈↓¬␈↓ ∧80␈↓ ∧x1␈↓ π_c
␈↓ ↓H␈↓¬␈↓ ∧81␈↓ ∧x0␈↓ π_b
␈↓ ↓H␈↓¬␈↓ ∧81␈↓ ∧x1␈↓ π_d



␈↓ ↓H␈↓¬␈↓ ∧J␈↓αTable 2.␈↓¬  ␈↓Boolean operation encoding.␈↓¬


␈↓ ↓H␈↓In␈α∂particular␈α∂if␈α∂<code>␈α∂is␈α∂␈↓¬0␈α∂␈↓we␈α∂have␈α∂the␈α∂identically␈α∂␈↓¬0␈α∂␈↓operation,␈α∂if␈α∂<code>␈α∂is␈α∂␈↓¬15␈α∂␈↓we␈α⊂have␈α∂the
␈↓ ↓H␈↓identically␈α∞␈↓¬1␈α∞␈↓operation,␈α∞if␈α
<code>␈α∞is␈α∞␈↓¬1␈α∞␈↓we␈α
have␈α∞ ␈↓αbool-and␈↓,␈α∞and␈α∞if␈α
<code>␈α∞is␈α∞␈↓¬7␈α∞␈↓we␈α∞have␈α
 ␈↓αbool-or␈↓.
␈↓ ↓H␈↓Note that all 16 boolean functions of two variables are realized by the LISP ␈↓¬BOOLE ␈↓operation.

␈↓ ↓H␈↓        In␈α∞addition␈α
to␈α∞the␈α
above␈α∞operations␈α
LISP␈α∞has␈α
a␈α∞shift␈α
operation␈α∞␈↓↓lsh[n,d]␈↓␈α
which␈α∞shifts␈α
the
␈↓ ↓H␈↓bits␈α⊂of␈α⊂the␈α∂binary␈α⊂representation␈α⊂of␈α∂␈↓↓n,␈↓␈α⊂␈↓↓d␈↓␈α⊂positions␈α∂to␈α⊂the␈α⊂left,␈α∂filling␈α⊂the␈α⊂vacated␈α⊂spaces␈α∂with
␈↓ ↓H␈↓zeroes.␈α If␈α␈↓↓d␈↓␈αis␈αnegative␈αthe␈αshifting␈αis␈αto␈αthe␈αright␈αinstead.␈α The␈αinternal␈αform␈αof␈αthis␈αoperation␈α
is
␈↓ ↓H␈↓(␈↓¬LSH␈↓ <n> <d>).

␈↓ ↓H␈↓        Boolean␈α∞operations␈α∞are␈α∞particularly␈α∞useful␈α∞when␈α∞you␈α∞have␈α∞a␈α∞problem␈α∞involving␈α∞data␈α
that
␈↓ ↓H␈↓can␈α∞easily␈α∞be␈α
represented␈α∞by␈α∞bit␈α∞strings␈α
or␈α∞vectors.␈α∞ Using␈α∞the␈α
Boolean␈α∞operations␈α∞is␈α∞very␈α
much
␈↓ ↓H␈↓more␈α⊂efficient␈α⊃than␈α⊂representing␈α⊃the␈α⊂same␈α⊂data␈α⊃as␈α⊂lists␈α⊃of␈α⊂␈↓¬0␈↓s␈α⊂and␈α⊃␈↓¬1␈↓s,␈α⊂both␈α⊃in␈α⊂terms␈α⊃of␈α⊂space
␈↓ ↓H␈↓required␈α⊂and␈α⊂in␈α⊂terms␈α⊂of␈α∂computation␈α⊂time.␈α⊂ It␈α⊂does␈α⊂not␈α∂necessarily␈α⊂make␈α⊂the␈α⊂problem␈α⊂or␈α∂the
␈↓ ↓H␈↓programs␈α
easier␈α
to␈αunderstand,␈α
however.␈α
  We␈α
will␈αsee␈α
an␈α
example␈α
of␈αa␈α
problem␈α
solved␈αusing␈α
both
␈↓ ↓H␈↓list and bit represtations in Chapter VI.

␈↓ ↓H␈↓α␈↓ ε⊂Exercise

␈↓ ↓H␈↓        What␈α⊂boolean␈α⊂operation␈α⊂does␈α⊂␈↓¬BOOLE␈α⊂␈↓perform␈α⊂when␈α⊂<code>=␈↓¬11␈α⊂␈↓(decimal)?␈α⊂ What␈α⊂<code>
␈↓ ↓H␈↓correspond to the operation ␈↓¬x=y␈↓ ?



␈↓ ↓H␈↓11.  ␈↓αLambda expressions and functions with functions as arguments.␈↓


␈↓ ↓H␈↓        It␈αis␈αcommon␈αto␈α
use␈αphrases␈αlike␈α"the␈α
function␈α␈↓↓2x+y␈↓".␈α This␈αis␈α
not␈αa␈αprecise␈αnotation␈α
because
␈↓ ↓H␈↓we␈αcannot␈αsay␈α␈↓↓[2x+y][3, 4]␈↓␈αand␈αknow␈αwhether␈α
the␈αdesired␈αresult␈αis␈α2␈↓π*␈↓3+4␈αor␈α2␈↓π*␈↓4+3␈α
regarding␈αthe
␈↓ ↓H␈↓expression␈α⊃as␈α⊃a␈α⊃function␈α⊃of␈α⊃two␈α⊃variables.␈α⊃ Worse␈α⊃yet,␈α⊃we␈α⊃might␈α⊃have␈α⊃meant␈α∩a␈α⊃one-variable
␈↓ ↓H␈↓function of ␈↓↓x␈↓ wherein ␈↓↓y␈↓ is regarded as a parameter.

␈↓ ↓H␈↓        The␈αproblem␈αof␈αgiving␈αnames␈αto␈αfunctions␈αis␈αsolved␈αby␈αChurch's␈αλ-notation.␈α In␈αthe␈αabove
␈↓ ↓H␈↓example,␈αwe␈αwould␈αwrite␈α␈↓↓λx y: 2x+y␈↓␈αto␈αdenote␈αthe␈αfunction␈αof␈αtwo␈αvariables␈αwith␈αfirst␈αargument␈α␈↓↓x␈↓
␈↓ ↓H␈↓and␈α#second␈α"argument␈α#␈↓↓y␈↓␈α"whose␈α#value␈α#is␈α"given␈α#by␈α"the␈α#expression␈α#␈↓↓2x+y␈↓.␈α" Thus,
␈↓ ↓H␈↓␈↓↓[λx y: 2x+y][3, 4] = 10␈↓ and  ␈↓↓[λy x: 2x+y][3, 4]= 11.␈↓

␈↓ ↓H␈↓        Like␈α⊂variables␈α⊃of␈α⊂integration␈α⊂and␈α⊃the␈α⊂bound␈α⊂variables␈α⊃of␈α⊂quantifiers␈α⊂in␈α⊃logic,␈α⊂variables
␈↓ ↓H␈↓22␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓following␈α∪the␈α∪ λ ␈α∪are␈α∀bound␈α∪or␈α∪dummy␈α∪and␈α∪may␈α∀be␈α∪replaced␈α∪by␈α∪any␈α∪others␈α∀provided␈α∪the
␈↓ ↓H␈↓replacement␈α
is␈α
done␈α
consistently␈α
throughout␈α
the␈α
expression␈α
and␈α
does␈α
not␈α
make␈α
any␈αvariable␈α
bound
␈↓ ↓H␈↓by␈α
 λ ␈α
the␈α
same␈α
as␈α
a␈α
free␈α
variable␈α
in␈α
the␈α
expression.␈α
 Thus␈α
␈↓↓λx y: 2x+y␈↓␈α
represents␈α
the␈α
same␈α
function
␈↓ ↓H␈↓as␈α␈↓↓λy x: 2y+x␈↓␈αor␈α␈↓↓λu v: 2u+v␈↓␈α,␈αbut␈αin␈αthe␈αfunction␈αof␈αone␈αargument␈α␈↓↓λx: 2x+y␈↓,␈αwe␈αcannot␈αreplace␈αthe
␈↓ ↓H␈↓variable ␈↓↓x␈↓ by ␈↓↓y,␈↓ though we could replace it by ␈↓↓u.␈↓

␈↓ ↓H␈↓        λ-notation␈α∞plays␈α
two␈α∞important␈α
roles␈α∞in␈α
LISP.␈α∞ First,␈α
it␈α∞allows␈α
us␈α∞to␈α
rewrite␈α∞an␈α
expression
␈↓ ↓H␈↓containing␈αtwo␈αor␈αmore␈αoccurrences␈αof␈αthe␈αsame␈αsub-expression␈αin␈αsuch␈αa␈αway␈αthat␈αthe␈αexpression
␈↓ ↓H␈↓occurs␈α∪only␈α∩once.␈α∪Thus␈α∩␈↓↓(2x+1)␈↓∧4␈↓↓+3(2x+1)␈↓∧3␈↓␈α∪can␈α∩be␈α∪written␈α∩␈↓↓[λw: w␈↓∧4␈↓↓+3w␈↓∧3␈↓↓][2x+1]␈↓.␈α∪ This␈α∪can␈α∩save
␈↓ ↓H␈↓considerable␈αcomputation,␈α
and␈αcorresponds␈αto␈α
the␈αpractice␈αin␈α
sequential␈αprogramming␈αof␈α
assigning
␈↓ ↓H␈↓to␈αa␈α
variable␈αthe␈αvalue␈α
of␈αa␈αsub-expression␈α
that␈αoccurs␈αmore␈α
than␈αonce␈αin␈α
an␈αexpression␈αand␈α
then
␈↓ ↓H␈↓writing the expression in terms of the variable.  It is sometimes referred to as λ-binding.

␈↓ ↓H␈↓        The␈α∂second␈α∂use␈α∂of␈α⊂λ-expressions␈α∂is␈α∂in␈α∂using␈α⊂functions␈α∂that␈α∂take␈α∂functions␈α⊂as␈α∂arguments.
␈↓ ↓H␈↓Suppose␈αwe␈αwant␈αto␈αform␈αa␈αnew␈αlist␈αfrom␈αan␈αold␈αone␈αby␈αapplying␈αa␈αfunction␈α␈↓↓f␈↓␈αto␈αeach␈αelement␈αof
␈↓ ↓H␈↓the list.  This can be done using the function ␈↓↓mapcar␈↓ defined by

␈↓ ↓H␈↓11.1) ␈↓ βG␈↓↓mapcar[f, u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ f[␈↓αa|␈↓↓u] . mapcar[f, ␈↓αd|␈↓↓u]␈↓. 

␈↓ ↓H␈↓Such␈α∂a␈α∂function␈α∂is␈α∂called␈α∂a␈α∂␈↓↓functional.␈↓␈α∂ It␈α∂can␈α∂be␈α∂viewed␈α∂as␈α∂mapping␈α∂a␈α∂function␈α∂into␈α∞another
␈↓ ↓H␈↓function.␈α Suppose␈αthe␈αoperation␈αwe␈αwant␈αto␈αperform␈αis␈αsquaring,␈αand␈αwe␈αwant␈αto␈αapply␈αit␈αto␈αthe
␈↓ ↓H␈↓list ␈↓¬(1 2 3 4 5 6 7)␈↓.  We have

␈↓ ↓H␈↓␈↓ β7␈↓↓mapcar[λx: x␈↓∧2␈↓↓, ␈↓¬(1 2 3 4 5 6 7)␈↓↓] = ␈↓¬(1 4 9 16 25 36 49)␈↓↓␈↓. 

␈↓ ↓H␈↓        [Some␈α∞implementations␈α∞of␈α∞LISP␈α∞allow␈α∞mapping␈α∞functions␈α∞to␈α∞take␈α∞an␈α∞arbitrary␈α∞number␈α∞of
␈↓ ↓H␈↓lists␈α
as␈α
arguments.␈α
 The␈α∞number␈α
of␈α
lists␈α
is␈α
the␈α∞number␈α
of␈α
arguments␈α
expected␈α
by␈α∞the␈α
functional
␈↓ ↓H␈↓argument␈αand␈αthe␈αmapping␈αterminates␈αwhen␈αthe␈αshortest␈αlist␈αis␈αexhausted.␈α Some␈αimplementations
␈↓ ↓H␈↓of LISP require the arguments in reverse order - functional argument second.]

␈↓ ↓H␈↓        A␈αmore␈αgenerally␈αuseful␈αoperation␈αthan␈α␈↓↓mapcar␈↓␈αis␈α␈↓↓maplist␈↓␈αin␈αwhich␈αthe␈αfunction␈αis␈αapplied
␈↓ ↓H␈↓to the successive sublists of the list rather than to the elements.  ␈↓↓maplist␈↓ is defined by

␈↓ ↓H␈↓11.2) ␈↓ βN␈↓↓maplist[f, u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ f[u] . maplist[f, ␈↓αd|␈↓↓u]␈↓. 

␈↓ ↓H␈↓        As␈α⊃an␈α⊃application␈α⊂of␈α⊃␈↓↓maplist␈↓␈α⊃and␈α⊂functional␈α⊃arguments,␈α⊃we␈α⊂shall␈α⊃define␈α⊃a␈α⊃function␈α⊂for
␈↓ ↓H␈↓differentiating␈α_algebraic␈α_expressions␈α↔involving␈α_sums␈α_and␈α↔products.␈α_ The␈α_syntax␈α_for␈α↔such
␈↓ ↓H␈↓expressions␈α
was␈α
given␈α
in␈α
(9.2).␈α
 Recall,␈α
␈↓¬PLUS␈α
␈↓followed␈α
by␈α
a␈α
list␈α
of␈α
arguments␈α
denotes␈α
the␈α∞sum␈α
of
␈↓ ↓H␈↓these␈αarguments␈αand␈α
␈↓¬TIMES␈α␈↓followed␈αby␈α
a␈αlist␈αof␈αarguments␈α
denotes␈αtheir␈αproduct.␈α
 The␈αfunction
␈↓ ↓H␈↓␈↓↓diff[e, v]␈↓ gives the partial derivative of the expression ␈↓↓e␈↓ with respect to the variable ␈↓↓v.␈↓ We have
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *23




␈↓ ↓H␈↓␈↓ αx␈↓↓diff[e, v] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ [␈↓αif␈↓↓ e = v ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ 0]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬PLUS␈↓↓ ␈↓αthen␈↓↓ ␈↓¬PLUS␈↓↓ . mapcar[[λx: diff[x, v]], ␈↓αd|␈↓↓e]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬TIMES␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓11.3)␈↓ βX␈↓↓␈↓¬PLUS␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓. maplist[␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓[λx: ␈↓¬TIMES␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓↓. maplist[␈↓
␈↓ ↓H␈↓␈↓ ¬λ␈↓↓[λy: ␈↓αif␈↓↓ x = y ␈↓αthen␈↓↓ diff[␈↓αa|␈↓↓y, v] ␈↓αelse␈↓↓ ␈↓αa|␈↓↓y], ␈↓αd|␈↓↓e]], ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αd|␈↓↓e]␈↓


␈↓ ↓H␈↓The term that describes the rule for differentiating products corresponds to the rule

␈↓ ↓H␈↓␈↓ ∧&␈↓↓∂/∂v[␈↓πP␈↓↓␈↓βi␈↓↓ e␈↓βi␈↓↓] = ␈↓πS␈↓↓␈↓βi␈↓↓␈↓π P␈↓↓␈↓βj␈↓↓ [␈↓αif␈↓↓ i=j ␈↓αthen␈↓↓ ∂e␈↓βj␈↓↓/∂v ␈↓αelse␈↓↓ e␈↓βj␈↓↓] .␈↓ 

␈↓ ↓H␈↓and␈α∂␈↓↓maplist␈↓␈α⊂has␈α∂to␈α⊂be␈α∂used␈α⊂rather␈α∂than␈α⊂␈↓↓mapcar␈↓␈α∂since␈α⊂whether␈α∂to␈α⊂differentiate␈α∂in␈α⊂forming␈α∂the
␈↓ ↓H␈↓product is determined by equality of the indices ␈↓↓i␈↓ and ␈↓↓j␈↓ rather than equality of the terms ␈↓↓e␈↓βi␈↓ and ␈↓↓e␈↓βj␈↓.

␈↓ ↓H␈↓        The internal form for a λ-expression is

␈↓ ↓H␈↓␈↓ βO(␈↓¬LAMBDA ␈↓<list of variables> <expression to be evaluated>).

␈↓ ↓H␈↓Thus␈α
␈↓↓λx:␈α
diff[x,v]␈↓␈α
is␈α
written␈α
␈↓¬(LAMBDA␈α
(X)␈α
(DIFF␈α
X␈α
V))␈↓.␈α
 The␈α
internal␈α
form␈α
of␈α
of␈α
␈↓↓diff␈↓␈α
is␈α
given
␈↓ ↓H␈↓below.  Notice that the function arguments to ␈↓↓maplist␈↓ and ␈↓↓mapcar␈↓ have the form

␈↓ ↓H␈↓␈↓ ¬(␈↓¬FUNCTION ␈↓ <λ-expression>).

␈↓ ↓H␈↓This␈αis␈α
necessary␈αif␈α
the␈αdefinition␈α
is␈αto␈α
be␈αcompiled␈α
as␈αthe␈α
compiler␈αmust␈α
recognize␈αthe␈α
fact␈αthat
␈↓ ↓H␈↓the␈αfollowing␈α
code␈αmust␈α
be␈αcompiled␈αas␈α
a␈αfunction.␈α
 If␈αthe␈αdefinition␈α
is␈αonly␈α
to␈αbe␈αinterpreted␈α
then
␈↓ ↓H␈↓␈↓¬FUNCTION␈α␈↓␈αhas␈αthe␈αsame␈αeffect␈αas␈α␈↓¬QUOTE␈α␈↓and␈αin␈αfact␈αyou␈αmay␈αuse␈α␈↓¬QUOTE␈α␈↓in␈αinstead␈α
of␈α␈↓¬FUNCTION
␈↓ ↓H␈↓¬␈↓in definitions that will only be interpreted.

␈↓ ↓H␈↓¬(DEFUN DIFF (E V)
␈↓ ↓H␈↓¬  (COND ((ATOM E) (COND ((EQ E V) 1) (T 0)))
␈↓ ↓H␈↓¬        ((EQ (CAR E) 'PLUS)
␈↓ ↓H␈↓¬         (CONS 'PLUS
␈↓ ↓H␈↓¬               (MAPCAR (FUNCTION (LAMBDA (X) (DIFF X V))) (CDR E))))
␈↓ ↓H␈↓¬        ((EQ (CAR E) 'TIMES)
␈↓ ↓H␈↓¬         (CONS 'PLUS
␈↓ ↓H␈↓¬               (MAPLIST (FUNCTION
␈↓ ↓H␈↓¬                          (LAMBDA (X)
␈↓ ↓H␈↓¬                            (CONS 'TIMES
␈↓ ↓H␈↓¬                                  (MAPLIST (FUNCTION
␈↓ ↓H␈↓¬                                            (LAMBDA (Y)
␈↓ ↓H␈↓¬                                              (COND ((EQ X Y) (DIFF (CAR Y) V))
␈↓ ↓H␈↓¬                                                    (T (CAR Y)))))
␈↓ ↓H␈↓¬                                           (CDR E)))))
␈↓ ↓H␈↓¬                        (CDR E))))))
␈↓ ↓H␈↓24␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓        The␈α
above␈α
paragraphing␈α
(known␈α
as␈α"pretty␈α
printing")␈α
makes␈α
function␈α
definitions␈α
easier␈αto
␈↓ ↓H␈↓read because items beginning in the same column are at the same parenthetical level.

␈↓ ↓H␈↓        Two␈α∩additional␈α⊃useful␈α∩functions␈α⊃with␈α∩functions␈α⊃as␈α∩arguments␈α⊃are␈α∩the␈α⊃predicates  ␈↓↓andlis␈↓
␈↓ ↓H␈↓ and  ␈↓↓orlis␈↓  defined by the equations


␈↓ ↓H␈↓11.4) ␈↓ ∧#␈↓↓andlis[p, u] ← ␈↓αn|␈↓↓u ∨ [p[␈↓αa|␈↓↓u] ∧ andlis[p, ␈↓αd|␈↓↓u]]␈↓

␈↓ ↓H␈↓11.5) ␈↓ ∧)␈↓↓ orlis[p, u] ← ¬␈↓αn|␈↓↓u ∧ [p[␈↓αa|␈↓↓u] ∨ orlis[p, ␈↓αd|␈↓↓u]]␈↓.


␈↓ ↓H␈↓        Another␈α∂way␈α∂of␈α∂writing␈α∂function␈α∂definitions␈α∞in␈α∂internal␈α∂notation␈α∂uses␈α∂␈↓¬LAMBDA␈α∂␈↓to␈α∂make␈α∞a
␈↓ ↓H␈↓function of the right side of a definition.  It is like writing ␈↓↓subst␈↓ and ␈↓↓alt␈↓ as


␈↓ ↓H␈↓␈↓ α∪␈↓↓subst ← λx y z:[␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ [␈↓αif␈↓↓ z ␈↓αeq␈↓↓ y ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ z]  ␈↓αelse␈↓↓ subst[x,y,␈↓αa|␈↓↓z] . subst[x,y,␈↓αd|␈↓↓z]]␈↓

␈↓ ↓H␈↓␈↓ ∧π␈↓↓alt ← λu.[␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . alt ␈↓αdd|␈↓↓u]␈↓.


␈↓ ↓H␈↓Internally these definitions of ␈↓↓subst␈↓ and ␈↓↓alt␈↓ take the forms

␈↓ ↓H␈↓¬␈↓ α((DEFPROP SUBST
␈↓ ↓H␈↓¬␈↓ α(  (LAMBDA (X Y Z)
␈↓ ↓H␈↓¬␈↓ α(    (COND ((ATOM Z) (COND ((EQ Z X) Y) (T Z)))
␈↓ ↓H␈↓¬␈↓ α(          (T (CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z))))))
␈↓ ↓H␈↓¬␈↓ α( EXPR)


␈↓ ↓H␈↓¬␈↓ α((DEFPROP ALT
␈↓ ↓H␈↓¬␈↓ α(  (LAMBDA (U) (COND ((OR (NULL U) (NULL (CDR U))) U)
␈↓ ↓H␈↓¬␈↓ α(                    (T (CONS (CAR U) (ALT (CDDR U))))))
␈↓ ↓H␈↓¬␈↓ α( EXPR).


␈↓ ↓H␈↓        The general form for this manner of writing functon definitions is

␈↓ ↓H␈↓␈↓ β?(␈↓¬DEFPROP ␈↓<function name> <defining λ-expression> ␈↓¬EXPR) ␈↓

␈↓ ↓H␈↓and␈α⊂it␈α⊃is␈α⊂often␈α⊃used␈α⊂by␈α⊃programs␈α⊂that␈α⊃output␈α⊂LISP.␈α⊂ It␈α⊃is␈α⊂a␈α⊃special␈α⊂case␈α⊃of␈α⊂an␈α⊃operation␈α⊂on
␈↓ ↓H␈↓property␈α
lists.␈α It␈α
puts␈α
the␈αλ-expression␈α
on␈α
the␈α␈↓¬EXPR␈α
␈↓property␈αof␈α
the␈α
function␈αname␈α
(which␈α
is␈αan
␈↓ ↓H␈↓atom).␈α
 ␈↓¬EXPR␈α␈↓says␈α
that␈α
the␈αitem␈α
is␈α
a␈αLISP␈α
function␈α
defined␈αby␈α
an␈α
S-expression.␈α (Rather␈α
than␈αby␈α
a
␈↓ ↓H␈↓machine␈αlanguage␈αsubroutine,␈αfor␈αinstance).␈α The␈α␈↓¬DEFUN␈α␈↓form␈αof␈αfunction␈αdefinition␈αhas␈αthe␈αsame
␈↓ ↓H␈↓effect␈αin␈αthat␈α
it␈αforms␈αa␈αλ-expression␈α
out␈αof␈αthe␈αlist␈α
of␈αvariables␈αand␈αthe␈α
right␈αhand␈αside␈αand␈α
puts
␈↓ ↓H␈↓it on the ␈↓¬EXPR ␈↓property of the function name.
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *25


␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓        1.␈α∂Compute␈α∞␈↓↓diff[␈↓¬(TIMES␈α∂X␈α∂(PLUS␈α∞Y␈α∂1)␈α∞3),␈α∂X␈↓↓]␈↓␈α∂using␈α∞the␈α∂above␈α∞definition␈α∂of␈α∂␈↓↓diff.␈↓␈α∞ Now
␈↓ ↓H␈↓do you see why algebraic simplification is important?

␈↓ ↓H␈↓        2. Compute ␈↓↓orlis[␈↓αat␈↓↓, ␈↓¬((A B) (C D) E)␈↓↓]␈↓.



␈↓ ↓H␈↓12.  ␈↓αLabel.␈↓


␈↓ ↓H␈↓        The␈αλ␈α
mechanism␈αis␈α
not␈αadequate␈α
for␈αproviding␈α
names␈αfor␈α
recursive␈αfunctions,␈α
because␈αin
␈↓ ↓H␈↓this␈αcase␈αthere␈αhas␈αto␈αbe␈αa␈αway␈αof␈αreferring␈αto␈αthe␈αfunction␈αname␈αwithin␈αthe␈αfunction.␈α Therefore,
␈↓ ↓H␈↓we␈α∞use␈α∞the␈α∞notation  ␈↓↓label[f, e]␈↓  to␈α∞denote␈α∞the␈α∞expression␈α∞␈↓↓e␈↓␈α∞but␈α∞where␈α∞occurrences␈α∞of␈α∞␈↓↓f␈↓␈α∂within␈α∞␈↓↓e␈↓
␈↓ ↓H␈↓refer␈α∞to␈α∞the␈α
whole␈α∞expression.␈α∞ For␈α∞example,␈α
suppose␈α∞we␈α∞wished␈α∞to␈α
define␈α∞a␈α∞function␈α∞that␈α
takes
␈↓ ↓H␈↓alternate elements of each element of a list and makes a list of these.  Thus, we want

␈↓ ↓H␈↓␈↓ β→␈↓↓altlis[␈↓¬((A B C) (A B C D) (X Y Z))␈↓↓] = ␈↓¬((A C) (A C) (X Z))␈↓↓␈↓. 

␈↓ ↓H␈↓We can make the definition

␈↓ ↓H␈↓12.1) ␈↓ αU␈↓↓altlis[u] ← mapcar[label[alt, λu: ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . alt[␈↓αdd|␈↓↓u]], u]␈↓. 

␈↓ ↓H␈↓in internal form this would be written

␈↓ ↓H␈↓¬␈↓ α((DEFUN ALTLIS (X)
␈↓ ↓H␈↓¬␈↓ α(  (MAPCAR (QUOTE (LABEL ALT
␈↓ ↓H␈↓¬␈↓ α(                   (LAMBDA (X)
␈↓ ↓H␈↓¬␈↓ α(                     (COND (OR (NULL X) (NULL (CDR X))) X)
␈↓ ↓H␈↓¬␈↓ α(                           (T (CONS (CAR X) (ALT (CDDR X)))))))
␈↓ ↓H␈↓¬␈↓ α(           X)).

␈↓ ↓H␈↓        The general internal form of the label construct is

␈↓ ↓H␈↓␈↓ ∧F(␈↓¬LABEL ␈↓<name> <function expression>),

␈↓ ↓H␈↓        The␈α
identifier␈α
␈↓↓alt␈↓␈α
in␈α
the␈α
above␈α
example␈α
is␈α
bound␈α
by␈α
␈↓↓label␈↓␈α
and␈α
is␈α
local␈α
to␈α∞that␈α
expression,
␈↓ ↓H␈↓and␈α
this␈α
is␈α
the␈α
general␈α
rule.␈α
 The␈α
label␈α
construct␈αis␈α
not␈α
often␈α
used␈α
in␈α
LISP␈α
since␈α
it␈α
is␈α
more␈αusual␈α
to
␈↓ ↓H␈↓give functions global definitions.



␈↓ ↓H␈↓13.  ␈↓αThe function ␈↓↓eval.␈↓α ␈↓


␈↓ ↓H␈↓        ␈↓↓eval␈↓␈αplays␈αboth␈αa␈αtheoretical␈αand␈αa␈αpractical␈αrole␈αin␈αLISP.␈α Historically,␈αthe␈αlist␈αnotation␈αfor
␈↓ ↓H␈↓LISP␈αfunctions␈αand␈α␈↓↓eval␈↓␈α
were␈αfirst␈αdevised␈αin␈α
order␈αto␈αshow␈αhow␈αeasy␈α
it␈αis␈αto␈αdefine␈α
a␈αuniversal
␈↓ ↓H␈↓function␈α
in␈αLISP␈α
-␈αthe␈α
idea␈α
was␈αto␈α
advocate␈αLISP␈α
as␈α
an␈αalternative␈α
to␈αTuring␈α
machines␈αfor␈α
doing
␈↓ ↓H␈↓the␈α∂elementary␈α⊂theory␈α∂of␈α∂computability.␈α⊂ This␈α∂role␈α∂will␈α⊂be␈α∂discussed␈α∂in␈α⊂a␈α∂later␈α∂chapter.␈α⊂  S.␈α∂R.
␈↓ ↓H␈↓26␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓Russell␈α∞noted␈α∞that␈α∞␈↓↓eval␈↓␈α∞could␈α∞serve␈α∞as␈α∞an␈α∞interpreter␈α∞for␈α∞LISP␈α∞and␈α∞promptly␈α∞programmed␈α∞it␈α∞in
␈↓ ↓H␈↓machine␈α
language␈α
with␈α
minor␈α
modifications␈α
to␈α
make␈α
it␈α
more␈α
practical.␈α
 An␈α
interpreter␈α
based␈α
on
␈↓ ↓H␈↓␈↓↓eval␈↓␈αhas␈αremained␈αa␈αfeature␈αof␈αmost␈αLISP␈αsystems.␈α Thus␈αwhen␈αyou␈αtalking␈αto␈αLISP␈αthe␈αsystem␈αis
␈↓ ↓H␈↓in␈αa␈αloop␈αthat␈α␈↓↓read␈↓s␈αwhat␈αyou␈αtype,␈α␈↓↓eval␈↓s␈αit␈αand␈α␈↓↓print␈↓s␈αthe␈αresult.␈α  [Of␈αcourse␈αa␈αreal␈αLISP␈αsystem
␈↓ ↓H␈↓does many other things too, such a storage management, error handling, etc.]

␈↓ ↓H␈↓        ␈↓↓eval␈↓␈αfor␈αLISP␈αexpressions␈αis␈αanalogous␈αto␈αthe␈αinterpreter␈α␈↓↓numval␈↓␈αfor␈αarithmetic␈αexpressions
␈↓ ↓H␈↓given␈αin␈α(9.4).␈α The␈αfirst␈αargument␈αto␈α␈↓↓eval␈↓␈αis␈αa␈αLISP␈αexpression␈αin␈αinternal␈αnotation.␈α The␈αsecond
␈↓ ↓H␈↓argument␈α∞is␈α∂an␈α∞association␈α∞list␈α∂that␈α∞tells␈α∞␈↓↓eval␈↓␈α∂what␈α∞value␈α∞each␈α∂variable␈α∞has,␈α∞and␈α∂what␈α∞function
␈↓ ↓H␈↓definition␈αis␈αto␈αbe␈αassociated␈αwith␈αeach␈αfunction␈αname.␈α Thus␈αthe␈αassociation␈αlist␈αis␈αa␈αlist␈αof␈αpairs
␈↓ ↓H␈↓where␈αeach␈α
pair␈αconsists␈α
either␈αof␈α
a␈αvariable␈α
and␈αthe␈α
S-expression␈αcorresponding␈α
to␈αits␈α
value,␈αor␈α
a
␈↓ ↓H␈↓function␈α
name␈α∞and␈α
the␈α∞S-expression␈α
representing␈α∞the␈α
function␈α∞expression␈α
defining␈α∞the␈α
function.
␈↓ ↓H␈↓(Here␈α∂a␈α∂function␈α∂expression␈α∂is␈α∞either␈α∂a␈α∂function␈α∂name,␈α∂or␈α∞a␈α∂lambda␈α∂expression.)␈α∂The␈α∂result␈α∞of
␈↓ ↓H␈↓applying␈α␈↓↓eval␈↓␈αis␈αthe␈αvalue␈αof␈αthe␈α
term␈αrepresented␈αby␈αthe␈αS-expression␈αin␈αan␈α
environment␈αwhere
␈↓ ↓H␈↓the␈α∞free␈α∂variables␈α∞are␈α∞assigned␈α∂the␈α∞values␈α∞given␈α∂by␈α∞the␈α∞association␈α∂list␈α∞and␈α∞where␈α∂the␈α∞function
␈↓ ↓H␈↓names␈α∪occuring␈α∩free␈α∪(i.e.␈α∩not␈α∪bound␈α∩in␈α∪a␈α∩label␈α∪expression)␈α∩denote␈α∪functions␈α∩defined␈α∪by␈α∩the
␈↓ ↓H␈↓associated expressions in the association list.

␈↓ ↓H␈↓        Since␈αany␈αcomputation␈αcan␈αbe␈αdescribed␈αas␈αevaluating␈αan␈αexpression␈αwithout␈αfree␈αvariables
␈↓ ↓H␈↓or␈α∀function␈α∀names,␈α∀the␈α∀second␈α∀argument␈α∀theoretically␈α∀plays␈α∀a␈α∀role␈α∀mainly␈α∀in␈α∃the␈α∀recursive
␈↓ ↓H␈↓definition of ␈↓↓eval.␈↓  Thus we usually start our computations with the second argument ␈↓¬NIL␈↓.

␈↓ ↓H␈↓        To␈αillustrate␈αthis,␈αsuppose␈αwe␈αwant␈αto␈αapply␈αthe␈αfunction␈α␈↓↓alt␈↓␈αto␈αthe␈αlist␈α␈↓¬(A␈αB␈αC␈αD␈αE)␈↓,␈αi.e.␈αwe
␈↓ ↓H␈↓wish to evaluate ␈↓↓alt[␈↓¬(A B C D E)␈↓↓]␈↓.  This can be obtained by computing

␈↓ ↓H␈↓                      ␈↓↓eval[␈↓␈↓¬((LABEL ALT␈↓
␈↓ ↓H␈↓                                  ␈↓¬(LAMBDA (X) (COND ((OR (NULL X) (NULL (CDR X))) X)␈↓
␈↓ ↓H␈↓                                                           ␈↓¬(T (CONS (CAR X) (ALT (CDDR X)))))))␈↓
␈↓ ↓H␈↓                                 ␈↓¬(QUOTE (A B C D E)))␈↓
␈↓ ↓H␈↓                              ,␈↓↓␈↓¬NIL␈↓↓]␈↓,

␈↓ ↓H␈↓which gives the expected result ␈↓¬(A C E)␈↓.

␈↓ ↓H␈↓        This␈α∂manner␈α∞of␈α∂evaluating␈α∞requires␈α∂that␈α∞auxiliary␈α∂functions␈α∞must␈α∂be␈α∞defined␈α∂within␈α∞any
␈↓ ↓H␈↓expression␈αwhere␈αthey␈αare␈αused␈α(by␈αusing␈αthe␈αlabel␈αconstruct)␈αand␈αworse␈αyet,␈αthey␈αmust␈αbe␈αdefined
␈↓ ↓H␈↓separately␈α↔for␈α⊗separate␈α↔occurrences.␈α⊗  This␈α↔can␈α⊗become␈α↔very␈α⊗cumbersome␈α↔and␈α↔also␈α⊗makes
␈↓ ↓H␈↓expressions␈α∞fairly␈α∞difficult␈α
to␈α∞understand.␈α∞ Another␈α
approach␈α∞is␈α∞to␈α
use␈α∞an␈α∞association␈α∞list␈α
which
␈↓ ↓H␈↓associates␈α⊗each␈α∃function␈α⊗name␈α⊗appearing␈α∃in␈α⊗the␈α⊗expression␈α∃with␈α⊗the␈α⊗appropriate␈α∃defining
␈↓ ↓H␈↓expression.   Thus we could evaluate ␈↓↓alt[␈↓¬(A B C D E)␈↓↓]␈↓ by computing

␈↓ ↓H␈↓↓                        eval[␈↓¬(ALT (QUOTE (A B C D E)))␈↓↓,
␈↓ ↓H␈↓↓                              ␈↓¬((ALT LAMBDA (X) (COND ((OR (NULL X) (NULL (CDR X))) X)␈↓↓
␈↓ ↓H␈↓↓                                                           ␈↓¬(T (CONS (CAR X) (ALT (CDDR X)))))))␈↓↓].

␈↓ ↓H␈↓        A simplified version of the usual LISP ␈↓↓eval␈↓ is the following:
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *27



␈↓ ↓H␈↓␈↓ α8␈↓↓eval[e, a] ←␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ [␈↓αif␈↓↓ numberp e ∨ e  = ␈↓¬NIL␈↓↓ ∨ e = ␈↓¬T␈↓↓ ␈↓αthen␈↓↓ e  ␈↓αelse␈↓↓ ␈↓αd|␈↓↓assoc[e, a]]␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓e ␈↓αthen␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓[␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬QUOTE ␈↓↓␈↓αthen␈↓↓ ␈↓αad|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬COND ␈↓↓␈↓αthen␈↓↓ evcond[␈↓αd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬LIST ␈↓↓␈↓αthen␈↓↓ evlist[␈↓αd|␈↓↓e,a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CAR ␈↓↓␈↓αthen␈↓↓ ␈↓αa|␈↓↓eval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓13.1)␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CDR ␈↓↓␈↓αthen␈↓↓ ␈↓αd|␈↓↓eval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CONS ␈↓↓␈↓αthen␈↓↓ eval[␈↓αad|␈↓↓e, a] . eval[␈↓αadd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬ATOM ␈↓↓␈↓αthen␈↓↓ ␈↓αat|␈↓↓eval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬EQ ␈↓↓␈↓αthen␈↓↓  eval[␈↓αad|␈↓↓e, a] = eval[␈↓αadd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ eval[␈↓αd|␈↓↓assoc[␈↓αa|␈↓↓e, a] . ␈↓αd|␈↓↓e, a]]␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓e = ␈↓¬LAMBDA ␈↓↓␈↓αthen␈↓↓ eval[␈↓αadda|␈↓↓e, prup[␈↓αada|␈↓↓e, evlist[␈↓αd|␈↓↓e,a] * a]␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓e = ␈↓¬LABEL ␈↓↓␈↓αthen␈↓↓ eval[␈↓αadda|␈↓↓e . ␈↓αd|␈↓↓e, [␈↓αada|␈↓↓e . ␈↓αadda|␈↓↓e] . a]␈↓,


␈↓ ↓H␈↓where the auxiliary functions ␈↓↓evcond␈↓ and ␈↓↓evlist␈↓ are defined by

␈↓ ↓H␈↓13.2) ␈↓ α⎇␈↓↓evcond[u, a] ← ␈↓αif␈↓↓ eval[␈↓αaa|␈↓↓u, a] ␈↓αthen␈↓↓ eval[␈↓αada|␈↓↓u, a]  ␈↓αelse␈↓↓ evcond[␈↓αd|␈↓↓u, a]␈↓, 

␈↓ ↓H␈↓13.3) ␈↓ β<␈↓↓evlist[u, a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ eval[␈↓αa|␈↓↓u,a] . evlist[␈↓αd|␈↓↓u, a]␈↓, 

␈↓ ↓H␈↓and␈αthe␈αauxiliary␈αfunction␈α␈↓↓prup,␈↓␈αused␈αfor␈αpairing␈αup␈αthe␈αelements␈αof␈αtwo␈αlists␈αof␈αequal␈αlength,␈αis
␈↓ ↓H␈↓defined by

␈↓ ↓H␈↓13.4) ␈↓ βA␈↓↓prup[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [␈↓αa|␈↓↓u . ␈↓αa|␈↓↓v] . prup[␈↓αd|␈↓↓u,␈↓αd|␈↓↓v]␈↓. 

␈↓ ↓H␈↓␈↓↓assoc␈↓ was given in (9.3).

␈↓ ↓H␈↓        This␈α⊂simple␈α⊃␈↓↓eval␈↓␈α⊂expects␈α⊃that␈α⊂an␈α⊃expression␈α⊂is␈α⊃either␈α⊂a␈α⊃constant␈α⊂(␈↓↓number,␈↓␈α⊃␈↓¬T␈↓,␈α⊂␈↓¬NIL␈↓,␈α⊃or␈α⊂a
␈↓ ↓H␈↓␈↓¬QUOTE␈↓d␈αS-expression),␈αa␈αvariable␈α
whose␈αvalue␈αcan␈αbe␈αfound␈α
on␈αthe␈αassociation␈αlist,␈α
a␈αconditional
␈↓ ↓H␈↓expression,␈α
a␈αlist␈α
making␈αexpression,␈α
or␈αan␈α
application␈αof␈α
a␈αfunction,␈α
lambda␈αexpression␈α
or␈αlabel
␈↓ ↓H␈↓expression␈α∩to␈α∩a␈α∩list␈α∩of␈α∩arguments.␈α∩ Thus␈α∪␈↓↓eval␈↓␈α∩checks␈α∩to␈α∩see␈α∩which␈α∩of␈α∩the␈α∩above␈α∪classes␈α∩the
␈↓ ↓H␈↓expression␈α
to␈α
be␈α
evaluated␈α
falls␈α
into␈α
and␈α
proceeds␈α
accordingly.␈α
 If␈α
the␈α
expression,␈α
␈↓↓e,␈↓␈α
is␈α
atomic␈α
then
␈↓ ↓H␈↓it␈α∩is␈α∩either␈α∩a␈α∩non␈α⊃␈↓¬QUOTE␈↓d␈α∩constant␈α∩or␈α∩a␈α∩variable.␈α∩ If␈α⊃the␈α∩former␈α∩then␈α∩␈↓↓eval␈↓␈α∩just␈α∩returns␈α⊃the
␈↓ ↓H␈↓expression, if the latter it looks up the value on the association list, ␈↓↓a.␈↓

␈↓ ↓H␈↓        If␈α␈↓↓e␈↓␈αis␈αnon-atomic␈αbut␈α
␈↓αa|␈↓␈↓↓e␈↓␈αis␈αatomic␈αthen␈α␈↓↓e␈↓␈αis␈α
either␈αa␈α␈↓¬QUOTE␈↓d␈αconstant,␈αa␈αconditional,␈α
a␈αlist
␈↓ ↓H␈↓maker,␈αor␈αan␈αapplication␈αof␈αa␈αfunction␈αto␈αa␈αlist␈αof␈αarguments.␈α In␈αthe␈αconstant␈αcase␈αthe␈αexpression
␈↓ ↓H␈↓being␈α∂quoted␈α∞(␈↓αad|␈↓␈↓↓e)␈↓␈α∂is␈α∞returned.␈α∂ If␈α∞␈↓↓e␈↓␈α∂is␈α∂a␈α∞conditional␈α∂then␈α∞the␈α∂list␈α∞of␈α∂pairs␈α∞is␈α∂processed␈α∂by␈α∞the
␈↓ ↓H␈↓auxiliary␈α
evaluator␈α
␈↓↓evcond,␈↓␈α
which␈α
␈↓↓eval␈↓s␈α
the␈α
"if"␈α
parts␈α
(␈↓↓␈↓αaa|␈↓↓u␈↓)␈α
in␈α
order␈α
until␈α
a␈α
true␈α
one␈α
is␈α
found␈α
then
␈↓ ↓H␈↓returns␈α⊂the␈α∂result␈α⊂of␈α∂␈↓↓eval␈↓ing␈α⊂the␈α∂corresponding␈α⊂"then"␈α∂part␈α⊂(␈↓↓␈↓αada|␈↓↓u␈↓).␈α∂ In␈α⊂the␈α∂list␈α⊂case␈α∂the␈α⊂list␈α∂of
␈↓ ↓H␈↓expressions␈α
to␈α
be␈αevaluated␈α
is␈α
given␈αto␈α
␈↓↓evlist␈↓␈α
which␈α
returns␈αa␈α
list␈α
of␈αthe␈α
values.␈α
 In␈α
the␈αfunction
␈↓ ↓H␈↓application␈αcase␈αthere␈αare␈αtwo␈αpossibilities.␈α If␈αthe␈αfunction␈αto␈αbe␈αapplied␈αis␈αone␈αof␈αthe␈αelementary
␈↓ ↓H␈↓functions,␈α∀the␈α∀indicated␈α∀operation␈α∀is␈α∀performed␈α∀on␈α∀the␈α∀result␈α∀of␈α∀evaluating␈α∀the␈α∀arguments.
␈↓ ↓H␈↓Otherwise␈α
the␈α
function␈α
must␈α
be␈α
defined␈α
in␈α
␈↓↓a,␈↓␈α
so␈α
␈↓↓eval␈↓␈α
looks␈α
up␈α
the␈α
definition,␈α
replaces␈α
the␈α
function
␈↓ ↓H␈↓name by the function definition in the expression and restarts the evaluation.

␈↓ ↓H␈↓        If␈α
neither␈α␈↓↓e␈↓␈α
nor␈α␈↓αa|␈↓␈↓↓e␈↓␈α
are␈αatomic␈α
then␈αit␈α
must␈α
be␈αa␈α
lambda␈αor␈α
label␈αapplication.␈α
 In␈αthe␈α
lambda
␈↓ ↓H␈↓28␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓case␈αthe␈αargument␈α
list␈αis␈αgiven␈αto␈α
␈↓↓evlist␈↓␈αto␈αbe␈αevaluated,␈α
the␈αvalues␈αare␈αthen␈α
paired␈αwith␈αthe␈αlist␈α
of
␈↓ ↓H␈↓variables␈αto␈αbe␈αbound␈αby␈αthe␈αlambda␈α(␈↓αada|␈↓␈↓↓e)␈↓␈αusing␈α␈↓↓prup␈↓␈αand␈αput␈αon␈αthe␈αfront␈αof␈α␈↓↓a.␈↓␈αThe␈αbody␈αof
␈↓ ↓H␈↓the␈α∂lambda␈α∂(␈↓αadda|␈↓␈↓↓e)␈↓␈α∂is␈α∂then␈α⊂evaluated␈α∂using␈α∂this␈α∂new␈α∂association␈α⊂list.␈α∂ In␈α∂the␈α∂label␈α∂case␈α⊂a␈α∂new
␈↓ ↓H␈↓association␈α∩list␈α∩is␈α∩formed␈α∩by␈α∩pairing␈α⊃the␈α∩function␈α∩name␈α∩(␈↓αada|␈↓␈↓↓e)␈↓␈α∩with␈α∩the␈α∩defining␈α⊃expression
␈↓ ↓H␈↓(␈↓αadda|␈↓␈↓↓e)␈↓␈αand␈α
adding␈αthe␈α
result␈αto␈α
the␈αfront␈α
of␈α␈↓↓a.␈↓␈α
 Then␈αthe␈α
label␈αexpression␈α
is␈αreplaced␈α
in␈α␈↓↓e␈↓␈αby␈α
the
␈↓ ↓H␈↓defining expression and this is evaluated using the new association list.

␈↓ ↓H␈↓        If␈α∞␈↓↓e␈↓␈α
is␈α∞not␈α∞an␈α
expression␈α∞of␈α
the␈α∞sort␈α∞expected␈α
by␈α∞␈↓↓eval,␈↓␈α
then␈α∞the␈α∞result␈α
is␈α∞not␈α∞defined.␈α
  It
␈↓ ↓H␈↓would␈α
not␈α
be␈α
difficult␈α
to␈α
add␈α
additional␈α∞clauses␈α
to␈α
␈↓↓eval␈↓␈α
so␈α
that␈α
it␈α
would␈α
return␈α∞reasonable␈α
error
␈↓ ↓H␈↓messages␈αrather␈α
than␈αjust␈α
being␈αundefined␈α(or␈α
dying␈αin␈α
some␈αstrange␈αway␈α
as␈αwould␈α
be␈αlikely␈αin␈α
an
␈↓ ↓H␈↓actual computer).

␈↓ ↓H␈↓        Notice␈α⊗that␈α⊗␈↓¬COND␈α⊗␈↓and␈α⊗␈↓¬LIST␈α⊗␈↓considered␈α⊗as␈α⊗pseudo-functions␈α⊗behave␈α⊗differently␈α⊗than
␈↓ ↓H␈↓ordinary␈α⊃functions␈α⊃in␈α∩that␈α⊃both␈α⊃can␈α∩take␈α⊃an␈α⊃arbitrary␈α∩number␈α⊃of␈α⊃arguments␈α∩while␈α⊃functions
␈↓ ↓H␈↓defined␈α
by␈α
a␈α
lambda␈α
expression␈α
have␈α
a␈α
fixed␈α
number␈α
of␈α
arguments␈α
determined␈α
by␈α
the␈αvariable
␈↓ ↓H␈↓list␈αoccuring␈αin␈αthe␈αlambda␈αexpression.␈α Also,␈αthe␈αusual␈αmanner␈αof␈αevaluation␈αan␈αapplication␈αterm
␈↓ ↓H␈↓is␈αLISP␈αis␈αto␈αevaluate␈αall␈αof␈αthe␈αarguments␈αthen␈αapply␈αthe␈αfunction.␈α This␈αwill␈αnot␈αwork␈αfor␈α␈↓¬COND
␈↓ ↓H␈↓¬␈↓as␈αthe␈αmain␈αreason␈αfor␈αa␈αconditional␈αis␈αto␈αbe␈αable␈αto␈αselect␈αa␈αterm␈αto␈αevaluate␈αdepending␈αon␈αsome
␈↓ ↓H␈↓set of conditions and not to evaluate other terms under those conditions.

␈↓ ↓H␈↓        The␈α∞above␈α∞version␈α∞of␈α
␈↓↓eval␈↓␈α∞does␈α∞not␈α∞handle␈α∞the␈α
propositional␈α∞constructs␈α∞␈↓αand␈↓,␈α∞␈↓αor␈↓,␈α∞and␈α
␈↓αnot␈↓.
␈↓ ↓H␈↓The␈α
effect␈α∞of␈α
these␈α∞constructs␈α
can␈α
be␈α∞obtained␈α
by␈α∞appropriate␈α
use␈α
of␈α∞the␈α
condtional,␈α∞but␈α
simply
␈↓ ↓H␈↓defining␈α⊃functions␈α⊂for␈α⊃␈↓αand␈↓␈α⊂and␈α⊃␈↓αor␈↓␈α⊂will␈α⊃not␈α⊂work␈α⊃as␈α⊂the␈α⊃evaluation␈α⊂of␈α⊃a␈α⊃function␈α⊂application
␈↓ ↓H␈↓requires␈α
that␈α
all␈α
of␈α
the␈αarguments␈α
of␈α
of␈α
the␈α
function␈α
be␈αevaluated␈α
before␈α
it␈α
is␈α
applied␈α
while␈αthe
␈↓ ↓H␈↓specification␈α⊂of␈α⊂␈↓αand␈↓␈α⊂and␈α⊂␈↓αor␈↓␈α⊂require␈α⊂that␈α⊂only␈α⊂as␈α⊂many␈α⊂of␈α⊂the␈α⊂arguments␈α⊂be␈α⊂evaluated␈α⊃as␈α⊂are
␈↓ ↓H␈↓required␈α∂to␈α⊂determine␈α∂the␈α∂answer.␈α⊂ Another␈α∂problem␈α⊂is␈α∂that␈α∂in␈α⊂most␈α∂implementations␈α⊂they␈α∂are
␈↓ ↓H␈↓allowed␈α∞to␈α
take␈α∞an␈α
arbitrary␈α∞number␈α
of␈α∞arguments.␈α∞  Thus␈α
we␈α∞need␈α
to␈α∞build␈α
them␈α∞into␈α∞␈↓↓eval␈↓␈α
for
␈↓ ↓H␈↓things␈α
to␈α
work␈α∞properly.␈α
 We␈α
will␈α
see␈α∞later␈α
that␈α
LISP␈α
systems␈α∞provide␈α
alternate␈α
solutions␈α∞to␈α
this
␈↓ ↓H␈↓problem␈α
by␈α
providing␈α
a␈α
variety␈α
of␈α
modes␈α
of␈α
functions␈α
application.␈α
 (These␈α
are␈α
known␈α∞as␈α
␈↓¬EXPR,
␈↓ ↓H␈↓¬␈↓␈↓¬FEXPR␈α⊃␈↓and␈α⊃␈↓¬LEXPR␈↓s.)␈α⊃   Arithmetic␈α∩is␈α⊃also␈α⊃missing␈α⊃from␈α∩our␈α⊃␈↓↓eval.␈↓␈α⊃ Adding␈α⊃these␈α∩constructs␈α⊃is
␈↓ ↓H␈↓essentially␈α∩like␈α⊃combining␈α∩␈↓↓eval␈↓␈α⊃with␈α∩the␈α∩earlier␈α⊃evaluator␈α∩␈↓↓numval␈↓␈α⊃and␈α∩adding␈α∩any␈α⊃additional
␈↓ ↓H␈↓primitive operations that are desired.

␈↓ ↓H␈↓        We␈α
note␈α∞that␈α
␈↓↓eval␈↓␈α
can␈α∞evaluate␈α
itself␈α∞if␈α
it␈α
is␈α∞given␈α
an␈α
association␈α∞list␈α
containing␈α∞the␈α
pairs
␈↓ ↓H␈↓␈↓¬(EVAL . λeval),␈α(␈↓␈↓¬(EVCOND . λevcond),␈α(␈↓␈↓¬(EVLIST . λevlist),␈α(␈↓␈α(␈↓¬(ASSOC . λassoc),
␈↓ ↓H␈↓¬␈↓␈↓¬(PRUP . λprup),␈α␈↓and␈α
␈↓¬(APPEND . λappend)␈α␈↓␈α
where␈α␈↓¬λ<function name>␈α
␈↓stands␈αfor␈α
the␈αinternal
␈↓ ↓H␈↓form of the expression defining the named function.  Thus

␈↓ ↓H␈↓␈↓ ∧'␈↓↓eval[␈↓¬(EVAL '(CAR '(A.B)) NIL)␈↓↓,alist] = ␈↓¬A␈↓↓␈↓ 

␈↓ ↓H␈↓where␈α
␈↓↓alist␈↓␈αis␈α
some␈αassociation␈α
list␈αcontaining␈α
the␈α
above␈αmentioned␈α
pairs␈α(and␈α
no␈αother␈α
definitions
␈↓ ↓H␈↓of those functions).

␈↓ ↓H␈↓        When␈αyou␈αtalk␈αto␈αLISP␈αyou␈αdo␈αnot␈αexplicitly␈αtell␈αthe␈αinterpreter␈αwhat␈αassociation␈αlist␈αto␈αuse
␈↓ ↓H␈↓generally.␈α
 This␈α
is␈αbecause␈α
the␈α
LISP␈α
associates␈αwith␈α
each␈α
atom␈α
a␈αlist␈α
of␈α
properties,␈α
 among␈αthese
␈↓ ↓H␈↓are␈α∂the␈α∂value,␈α∂and/or␈α∂the␈α∂function␈α∞definition␈α∂associated␈α∂with␈α∂that␈α∂atom.␈α∂ The␈α∂LISP␈α∞interpreter
␈↓ ↓H␈↓typically␈α∂looks␈α∂up␈α∂variable␈α∂values␈α∂and␈α∞function␈α∂definitions␈α∂on␈α∂the␈α∂corresponding␈α∂property␈α∞lists.
␈↓ ↓H␈↓Thus,␈α⊃instead␈α⊃of␈α⊃making␈α⊃up␈α⊃an␈α⊂association␈α⊃list␈α⊃with␈α⊃the␈α⊃appropriate␈α⊃variables␈α⊃and␈α⊂functions
␈↓ ↓H␈↓defined,␈αthe␈αproperty␈αlists␈αmust␈αfirst␈αbe␈αprimed␈αby␈αdoing␈α␈↓¬SETQ␈↓s␈αfor␈αgiving␈αvariables␈αinitial␈αvalues
␈↓ ↓H␈↓and␈α⊂␈↓¬DEFUN␈↓s␈α⊂(or␈α⊂␈↓¬DEFPROP␈↓s)␈α⊂for␈α∂any␈α⊂functions␈α⊂that␈α⊂need␈α⊂to␈α∂be␈α⊂defined.␈α⊂ These␈α⊂features␈α⊂will␈α∂be
␈↓ ↓H␈↓discussed in more detail in Chapter IV.
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *29


␈↓ ↓H␈↓α␈↓ εεExercises.



␈↓ ↓H␈↓1.␈αModify␈α␈↓↓eval␈↓␈αto␈αreturn␈αreasonably␈αinformative␈αerror␈αmessages␈αwhen␈αit␈αis␈αgiven␈αa␈αnon-acceptable
␈↓ ↓H␈↓expression to evaluate.

␈↓ ↓H␈↓2. Modify ␈↓↓eval␈↓ to accept the propositional constructs ␈↓αand␈↓, ␈↓αor␈↓, and ␈↓αnot␈↓.
␈↓ ↓H␈↓30␈↓ εH␈↓ H


␈↓ ↓H␈↓α␈↓ εChapter II

␈↓ ↓H␈↓α␈↓ βZWRITING RECURSIVE FUNCTION DEFINITIONS


␈↓ ↓H␈↓        In␈αthe␈αChapter␈αI␈αwe␈αdiscussed␈αthe␈αbasic␈αconstructs␈αof␈αLISP␈αand␈αexplained␈αhow␈αto␈αevaluate
␈↓ ↓H␈↓terms␈αbuilt␈αup␈αusing␈αthese␈αconstructs.␈α The␈αnotion␈αof␈αrecursively␈αdefined␈αfunction␈αwas␈αintroduced
␈↓ ↓H␈↓and␈αthe␈αrules␈αfor␈αcomputing␈αthe␈αvalue␈α
of␈αa␈αrecursively␈αdefined␈αfunction␈αwere␈αgiven.␈α
 In␈αaddition
␈↓ ↓H␈↓we␈α∂showed␈α∞how␈α∂LISP␈α∂programs␈α∞are␈α∂represented␈α∞as␈α∂S-expressions␈α∂and␈α∞how␈α∂these␈α∂programs␈α∞are
␈↓ ↓H␈↓interpretered␈α
by␈α
the␈α
function␈α
␈↓↓eval.␈↓␈α  By␈α
now␈α
you␈α
should␈α
be␈αable␈α
to␈α
read␈α
and␈α
understand␈αsimple
␈↓ ↓H␈↓LISP␈α∂programs.␈α∂ The␈α⊂next␈α∂step␈α∂is␈α⊂learning␈α∂to␈α∂write␈α∂LISP␈α⊂programs.␈α∂ In␈α∂principle␈α⊂you␈α∂already
␈↓ ↓H␈↓know␈α
all␈α
that␈α
is␈α
necessary,␈αhowever␈α
there␈α
are␈α
some␈α
basic␈αideas␈α
and␈α
 techniques␈α
which␈α
are␈αuseful␈α
in
␈↓ ↓H␈↓solving␈αLISP␈αprogramming␈αproblems.␈α
 The␈αpurpose␈αof␈αthis␈αchapter␈α
is␈αto␈αhelp␈αyou␈αlearn␈α
to␈αthink
␈↓ ↓H␈↓recursively␈α⊃and␈α⊃to␈α⊃familiarize␈α⊃you␈α⊃with␈α⊃some␈α⊃of␈α⊃the␈α⊃basic␈α⊃techniques␈α⊃and␈α⊃standard␈α⊃forms␈α⊃of
␈↓ ↓H␈↓recursive␈α∀programs.␈α∀ The␈α∀final␈α∀section␈α∀contains␈α∀a␈α∀collection␈α∀of␈α∀problems␈α∀of␈α∃varing␈α∀degrees
␈↓ ↓H␈↓difficulty for you practice on.



␈↓ ↓H␈↓1.  ␈↓αStatic and dynamic ways of programming.␈↓


␈↓ ↓H␈↓        In␈α∃order␈α∀to␈α∃write␈α∀recursive␈α∃function␈α∀definitions,␈α∃one␈α∀must␈α∃think␈α∃about␈α∀programming
␈↓ ↓H␈↓differently␈α
than␈α
is␈α
customary␈α
when␈α
writing␈α
programs␈α
in␈α
languages␈α
like␈α
FORTRAN␈α
or␈αALGOL␈α
or
␈↓ ↓H␈↓in␈α⊃machine␈α∩language.␈α⊃ In␈α∩these␈α⊃languages,␈α⊃one␈α∩has␈α⊃in␈α∩mind␈α⊃the␈α⊃state␈α∩of␈α⊃the␈α∩computation␈α⊃as
␈↓ ↓H␈↓represented␈α∞by␈α∂the␈α∞values␈α∂of␈α∞certain␈α∞variables␈α∂or␈α∞locations␈α∂in␈α∞the␈α∞memory␈α∂of␈α∞the␈α∂machine,␈α∞and
␈↓ ↓H␈↓then␈α⊃one␈α⊃writes␈α⊃statements␈α⊃or␈α⊃machine␈α⊃instructions␈α∩in␈α⊃order␈α⊃to␈α⊃make␈α⊃the␈α⊃state␈α⊃change␈α∩in␈α⊃an
␈↓ ↓H␈↓appropriate␈α∂way.␈α⊂ When␈α∂writing␈α∂recursive␈α⊂function␈α∂definitions␈α∂one␈α⊂takes␈α∂a␈α⊂different␈α∂approach.
␈↓ ↓H␈↓Namely,␈α
one␈α∞thinks␈α
about␈α∞the␈α
value␈α∞of␈α
the␈α
function,␈α∞asks␈α
for␈α∞what␈α
values␈α∞of␈α
the␈α∞arguments␈α
the
␈↓ ↓H␈↓value␈α
of␈αthe␈α
function␈αis␈α
immediate,␈α
and,␈αgiven␈α
arbitrary␈αvalues␈α
of␈α
the␈αarguments,␈α
for␈αwhat␈α
simpler
␈↓ ↓H␈↓arguments␈α
must␈α
the␈α
function␈α
be␈αknown␈α
in␈α
order␈α
to␈α
give␈α
the␈αvalue␈α
of␈α
the␈α
function␈α
for␈α
the␈αgiven
␈↓ ↓H␈↓arguments.

␈↓ ↓H␈↓        Let␈αus␈αconsider␈αa␈αnumerical␈αexample;␈αnamely,␈αsuppose␈αwe␈αwant␈αto␈αcompute␈αthe␈αfunction␈α␈↓↓n!.␈↓
␈↓ ↓H␈↓For␈αwhat␈αargument␈αis␈αthe␈αvalue␈αof␈αthe␈αfunction␈αimmediate.␈α Clearly,␈αfor␈α␈↓↓n␈↓␈α=␈α0␈αor␈α␈↓↓n␈↓␈α=␈α1,␈αthe␈αvalue
␈↓ ↓H␈↓is␈α
immediately␈α
seen␈α
to␈α
be␈α
1.␈α Moreover,␈α
we␈α
can␈α
get␈α
the␈α
value␈αfor␈α
an␈α
arbitrary␈α
␈↓↓n␈↓␈α
if␈α
we␈α
know␈αthe
␈↓ ↓H␈↓value␈α⊂for␈α⊂␈↓↓n-1␈↓.␈α⊂ Also,␈α∂we␈α⊂see␈α⊂that␈α⊂knowing␈α⊂the␈α∂value␈α⊂for␈α⊂␈↓↓n␈↓␈α⊂=␈α⊂1␈α∂is␈α⊂redundant,␈α⊂since␈α⊂it␈α⊂can␈α∂be
␈↓ ↓H␈↓obtained␈αfrom␈αthe␈α␈↓↓n␈↓␈α=␈α0␈αcase␈αby␈αthe␈αsame␈αrule␈αas␈αgets␈αit␈αfor␈αa␈αgeneral␈α␈↓↓n␈↓␈αfrom␈αthe␈αvalue␈αfor␈α␈↓↓n-1␈↓.
␈↓ ↓H␈↓All this talk leads to the simple recursive definition:

␈↓ ↓H␈↓1.1) ␈↓ ∧q␈↓↓n! ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ n␈↓π*␈↓↓[n-␈↓¬1␈↓↓]!␈↓. 

␈↓ ↓H␈↓        We␈αmay␈αregard␈αthis␈αas␈αa␈αstatic␈αway␈αof␈αlooking␈αat␈αprogramming.␈α We␈αask␈αwhat␈αsimpler␈α
cases
␈↓ ↓H␈↓the␈αgeneral␈αcase␈αof␈αour␈αfunction␈αdepends␈αon␈αrather␈αthan␈αhow␈αwe␈αbuild␈αup␈αthe␈αdesired␈αstate␈αof␈αthe
␈↓ ↓H␈↓computation.

␈↓ ↓H␈↓        An␈α
example␈α
of␈αthe␈α
dynamic␈α
approach␈αto␈α
programming␈α
is␈αthe␈α
following␈α
obvious␈αALGOL␈α
60
␈↓ ↓H␈↓program for computing ␈↓↓n!:␈↓
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *31



␈↓ ↓H␈↓↓␈↓ ∧H␈↓αinteger procedure␈↓↓ factorial(n); ␈↓αinteger␈↓↓ s;
␈↓ ↓H␈↓↓␈↓ βx␈↓αbegin␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧Hs := ␈↓¬1␈↓↓;
␈↓ ↓H␈↓↓␈↓ βxloop:␈↓ ∧H␈↓αif␈↓↓ n = ␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ done;
␈↓ ↓H␈↓↓1.2)␈↓ ∧Hs := n*s;
␈↓ ↓H␈↓↓␈↓ ∧Hn := n-␈↓¬1␈↓↓;
␈↓ ↓H␈↓↓␈↓ ∧H␈↓αgo to␈↓↓ loop;
␈↓ ↓H␈↓↓␈↓ βxdone:␈↓ ∧Hfactorial := s;
␈↓ ↓H␈↓↓␈↓ βx␈↓αend␈↓↓;


␈↓ ↓H␈↓        One␈αoften␈αis␈αled␈αto␈αbelieve␈αthat␈αstatic␈α=␈αbad␈αand␈α dynamic␈α=␈αgood,␈αbut␈αin␈αthis␈α(particularly
␈↓ ↓H␈↓favorable)␈α∞case␈α∞the␈α∞LISP␈α∞program␈α∞is␈α∞shorter␈α∞and␈α∞clearer.␈α∞ In␈α∞general␈α∞this␈α∞style␈α∞of␈α∂thinking␈α∞and
␈↓ ↓H␈↓programming␈α⊃can␈α⊃produce␈α⊃clean␈α∩simple␈α⊃programs.␈α⊃ It␈α⊃also␈α∩provides␈α⊃a␈α⊃built␈α⊃in␈α∩mechanism␈α⊃of
␈↓ ↓H␈↓problem␈α∞solving␈α∞which␈α∞is␈α∞rather␈α∞like␈α∞what␈α
is␈α∞usually␈α∞called␈α∞␈↓↓subgoaling.␈↓␈α∞ We␈α∞shall␈α∞also␈α∞see␈α
later
␈↓ ↓H␈↓how this style leads to rather natural methods of proving statements about programs.

␈↓ ↓H␈↓         Actually,␈α⊂when␈α⊂we␈α⊂discuss␈α⊃the␈α⊂mechanism␈α⊂of␈α⊂recursion,␈α⊂it␈α⊃will␈α⊂turn␈α⊂out␈α⊂that␈α⊃this␈α⊂LISP
␈↓ ↓H␈↓program␈α⊃is␈α⊃inefficient␈α⊃because␈α⊃it␈α⊃uses␈α⊂the␈α⊃pushdown␈α⊃mechanism␈α⊃unnecessarily␈α⊃and␈α⊃should␈α⊂be
␈↓ ↓H␈↓replaced␈α⊃by␈α∩the␈α⊃following␈α∩somewhat␈α⊃longer␈α⊃program␈α∩that␈α⊃corresponds␈α∩to␈α⊃the␈α∩above␈α⊃ALGOL
␈↓ ↓H␈↓program rather precisely:

␈↓ ↓H␈↓␈↓ ∧<␈↓↓        n! ← fact[n,␈↓¬1␈↓↓],                          ␈↓
␈↓ ↓H␈↓1.3)
␈↓ ↓H␈↓␈↓ ∧≠␈↓↓fact[n, s] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ s ␈↓αelse␈↓↓ fact[n-␈↓¬1␈↓↓, n␈↓π*␈↓↓s]␈↓.

␈↓ ↓H␈↓Although␈αthis␈αmay␈α
not␈αseem␈αto␈αbe␈α
an␈αimprovement,␈αwe␈αshall␈α
see␈αin␈αlater␈αchapters␈α
how␈αcompilers
␈↓ ↓H␈↓and even interpreters can produce essentially the same computation from the two programs.

␈↓ ↓H␈↓        Perhaps␈α∞the␈α∞distinction␈α∞between␈α∞the␈α∞two␈α∞styles␈α∞is␈α∞equivalent␈α∞to␈α∞what␈α∞some␈α∞people␈α∞call␈α
the
␈↓ ↓H␈↓distinction␈α∩between␈α∩␈↓↓top-down␈↓␈α⊃and␈α∩␈↓↓bottom-up␈↓␈α∩programming␈α⊃with␈α∩␈↓↓static␈↓␈α∩corresponding␈α∩to␈α⊃␈↓↓top-
␈↓ ↓H␈↓↓down.␈↓  LISP offers both, but the static style is better developed in LISP, and we will emphasize it.



␈↓ ↓H␈↓2.  ␈↓αRecursive definition of functions on natural numbers.␈↓


␈↓ ↓H␈↓        In␈αthe␈αnext␈αseveral␈αsections␈αwe␈αexamine␈αvarious␈αforms␈αof␈αrecursive␈αfunction␈αdefinition␈αand
␈↓ ↓H␈↓programs␈α∀having␈α∀such␈α∀forms.␈α∀ We␈α∃begin␈α∀by␈α∀considering␈α∀recursive␈α∀definitions␈α∃of␈α∀numerical
␈↓ ↓H␈↓functions.␈α
 We␈αhave␈α
already␈αseen␈α
one␈αexample,␈α
namely␈αthe␈α
factorial␈αfunction.␈α
 The␈αbasic␈α
idea␈αis␈α
to
␈↓ ↓H␈↓give␈αa␈α
rule␈αfor␈αcomputing␈α
the␈αvalue␈αof␈α
the␈αfunction␈αfor␈α
a␈αparticular␈αvalue␈α
of␈αthe␈αargument␈α
(input)
␈↓ ↓H␈↓in␈αterms␈αof␈αsome␈αcollection␈αof␈αgiven␈α(defined␈αor␈αbuilt␈αin)␈αfunctions␈αand␈αvalues␈αof␈αthe␈αfunction␈αfor
␈↓ ↓H␈↓smaller␈α
arguments.␈α
  Notice␈α
that␈α
we␈α
will␈α
have␈α
to␈α
specify␈α
the␈α
value␈α
for␈α
␈↓¬0␈α
␈↓directly␈α
as␈α
there␈α∞are␈α
no
␈↓ ↓H␈↓smaller␈αnumbers.␈α The␈αmethod␈αof␈αfunction␈αdefinition␈αis␈αparallel␈αto␈αthe␈αdescription␈αof␈α
the␈αdomain
␈↓ ↓H␈↓of␈αnatural␈αnumbers␈αin␈αterms␈αof␈αthe␈αnumber␈α␈↓¬0␈α␈↓and␈αthe␈αoperation␈αof␈αsuccessor,␈αnamely␈αa␈αnumber␈αis
␈↓ ↓H␈↓either ␈↓¬0 ␈↓or obtained by applying successor to a previously constructed number.

␈↓ ↓H␈↓        Recursive␈α∞functions␈α∞of␈α∂natural␈α∞numbers␈α∞have␈α∂the␈α∞the␈α∞subject␈α∂of␈α∞much␈α∞study␈α∂by␈α∞logicians
␈↓ ↓H␈↓32␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓and␈α∞mathematicians.␈α∞ One␈α
fairly␈α∞nice␈α∞result␈α∞is␈α
that␈α∞any␈α∞such␈α
function␈α∞can␈α∞be␈α∞computed␈α
starting
␈↓ ↓H␈↓with␈α∂the␈α∂constant␈α⊂␈↓¬0,␈α∂␈↓the␈α∂basic␈α⊂functions␈α∂␈↓↓add1␈↓␈α∂(more␈α∂commonly␈α⊂known␈α∂as␈α∂␈↓↓successor)␈↓␈α⊂␈↓↓sub1␈↓␈α∂(also
␈↓ ↓H␈↓known␈αas␈α␈↓↓predecessor)␈↓␈α
and␈αthe␈αtools␈αfor␈α
recursive␈αdefinition␈αdescribed␈α
in␈αChapter␈αI.␈α (Actually␈α
one
␈↓ ↓H␈↓can␈α⊂get␈α∂by␈α⊂with␈α∂less,␈α⊂but␈α∂that␈α⊂is␈α⊂not␈α∂the␈α⊂point␈α∂of␈α⊂this␈α∂discussion.)␈α⊂ For␈α∂example␈α⊂the␈α⊂sum␈α∂and
␈↓ ↓H␈↓difference of two numbers are given by

␈↓ ↓H␈↓2.1)␈↓ ∧∂␈↓↓plus[n,m] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ m ␈↓αelse␈↓↓ plus[n-␈↓¬1␈↓↓,m]+␈↓¬1␈↓↓␈↓ 

␈↓ ↓H␈↓2.2)␈↓ α⎇␈↓↓differ[n,m] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ m=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ n ␈↓αelse␈↓↓ differ[n-␈↓¬1␈↓↓,m-␈↓¬1␈↓↓]␈↓ 

␈↓ ↓H␈↓while the predicate ␈↓↓greaterp␈↓ can be computed by

␈↓ ↓H␈↓2.3)␈↓ α?␈↓↓greaterp[n,m] ← ␈↓αif␈↓↓ n = ␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ m = ␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓  greaterp[n-␈↓¬1␈↓↓,m-␈↓¬1␈↓↓]␈↓ 

␈↓ ↓H␈↓Here␈α
we␈αuse␈α
␈↓¬0␈α␈↓to␈α
represent␈α
␈↓αfalse␈↓␈αand␈α
␈↓¬1␈α␈↓to␈α
represent␈α␈↓αtrue␈↓␈α
in␈α
order␈αto␈α
keep␈αwithin␈α
the␈α
domain␈αof
␈↓ ↓H␈↓numbers.  We also write ␈↓↓n+␈↓¬1␈↓↓␈↓ instead of ␈↓↓add1 n␈↓ and ␈↓↓n-␈↓¬1␈↓↓␈↓ instead of ␈↓↓sub1 n␈↓.

␈↓ ↓H␈↓        We␈αcould␈αcontinue␈αalong␈αthese␈αlines␈αusing␈α␈↓↓plus␈↓␈αto␈αdefine␈α␈↓↓times,␈↓␈α␈↓↓times␈↓␈αto␈αdefine␈α␈↓↓exp,␈↓␈α␈↓↓differ␈↓
␈↓ ↓H␈↓to␈αdefine␈α␈↓↓quot␈↓␈αand␈α␈↓↓rem,␈↓␈αand␈αso␈αforth␈αbuilding␈αup␈αa␈αcollection␈αof␈αuseful␈αfunctions.␈α We␈αwill␈αleave
␈↓ ↓H␈↓this␈α∂as␈α∂an␈α⊂exercise␈α∂for␈α∂the␈α⊂reader.␈α∂ The␈α∂point␈α∂is␈α⊂that␈α∂at␈α∂each␈α⊂stage␈α∂the␈α∂recursive␈α⊂definition␈α∂is
␈↓ ↓H␈↓expressed␈α
in␈αterms␈α
of␈αgiven␈α
functions␈αin␈α
a␈α
very␈αsimple␈α
form.␈α Perhaps␈α
the␈αsimplest␈α
such␈α
form␈αis
␈↓ ↓H␈↓the following schema:

␈↓ ↓H␈↓2.4)␈↓ ∧;␈↓↓f[n] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬a ␈↓↓␈↓αelse␈↓↓ h[n-␈↓¬1␈↓↓,f[n-␈↓¬1␈↓↓]]␈↓. 

␈↓ ↓H␈↓Here␈α∞␈↓↓f␈↓␈α∞is␈α∞defined␈α∂in␈α∞terms␈α∞of␈α∞a␈α∂fixed␈α∞constant␈α∞␈↓¬a␈α∞␈↓and␈α∞a␈α∂given␈α∞function␈α∞␈↓↓h.␈↓␈α∞This␈α∂corresponds␈α∞to
␈↓ ↓H␈↓"primitive␈αrecursion"␈αwith␈αout␈αparameters.␈α If␈αwe␈αtake␈α ␈↓↓h[k,m]=[k+␈↓¬1␈↓↓]␈α␈↓π*␈↓↓␈αm␈↓␈αand␈α␈↓¬a␈α␈↓=␈α␈↓¬1␈α␈↓we␈αhave␈αthe
␈↓ ↓H␈↓definition of ␈↓↓fact␈↓ given above (1.1).

␈↓ ↓H␈↓If␈α
we␈α
allow␈α
parameters␈α
to␈α
be␈α
carried␈α
along␈α
we␈α
get␈α
the␈α
usual␈α
form␈α
of␈α
primitive␈α
recursion␈α(shown
␈↓ ↓H␈↓with only one parameter for simplicity)

␈↓ ↓H␈↓2.5)␈↓ βr␈↓↓f[n,m] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ g[m] ␈↓αelse␈↓↓ h[n-␈↓¬1␈↓↓,m,f[n-␈↓¬1␈↓↓,m]]␈↓ 

␈↓ ↓H␈↓The␈α
definition␈αof␈α
␈↓↓plus␈↓␈αgiven␈α
above␈αhas␈α
this␈αform␈α
with␈α␈↓↓g[m]=m␈↓␈α
and␈α␈↓↓h[n,m,k]=k+␈↓¬1␈↓↓␈↓.␈α
  As␈αa␈α
further
␈↓ ↓H␈↓generalization␈αwe␈αallow␈α
the␈αparametric␈αargument␈α
of␈α␈↓↓f␈↓␈α(in␈α
the␈αrecursive␈αcall)␈α
to␈αbe␈αa␈αgiven␈α
function
␈↓ ↓H␈↓of the parameter and the first argument giving

␈↓ ↓H␈↓2.6)␈↓ β?␈↓↓f[n,m] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ g[m] ␈↓αelse␈↓↓ h[n-␈↓¬1␈↓↓,m,f[n-␈↓¬1␈↓↓,j[n-␈↓¬1␈↓↓,m]]]␈↓. 

␈↓ ↓H␈↓The␈α∞definitions␈α∞of␈α
␈↓↓differ␈↓␈α∞and␈α∞␈↓↓greaterp␈↓␈α∞given␈α
above␈α∞has␈α∞this␈α
form.␈α∞ Also␈α∞the␈α∞alternate␈α
recursive
␈↓ ↓H␈↓definition␈αof␈αthe␈αfactorial␈αfunction␈α(1.3)␈αis␈αof␈αthis␈αform.␈α We␈αmay␈αwish␈αto␈αexpress␈αthe␈αcomputation
␈↓ ↓H␈↓directly␈αin␈αterms␈αof␈αseveral␈αpreceeding␈αvalues␈αinstead␈αof␈αjust␈α␈↓↓f[n-␈↓¬1␈↓↓]␈↓.␈α  One␈αform␈αof␈αthis␈α"course␈α
of
␈↓ ↓H␈↓values" type recursion is given by

␈↓ ↓H␈↓␈↓ ∧8␈↓↓f[n] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬a␈↓↓␈↓β0␈↓↓ ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓   ␈↓αif␈↓↓ n=␈↓¬1 ␈↓↓␈↓αthen␈↓↓ ␈↓¬a␈↓↓␈↓β1␈↓↓ ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓2.7)␈↓ ¬8..........

␈↓ ↓H␈↓␈↓ ∧8␈↓↓   ␈↓αif␈↓↓ n=␈↓¬b ␈↓↓␈↓αthen␈↓↓ ␈↓¬a␈↓↓␈↓βb␈↓↓ ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓     h[n,f[p␈↓β1␈↓↓[n]],...f[[p␈↓βc␈↓↓[n]]]␈↓
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *33


␈↓ ↓H␈↓ where␈α∞␈↓↓␈↓¬0␈↓↓≤p␈↓βi␈↓↓[n]<n␈↓␈α∞for␈α
␈↓¬1≤i≤k␈α∞␈↓when␈α∞␈↓↓␈↓¬b␈↓↓<n␈↓.␈α∞ As␈α
an␈α∞example␈α∞we␈α∞have␈α
the␈α∞definition␈α∞of␈α∞the␈α
function
␈↓ ↓H␈↓giving the ␈↓↓n␈↓th Fibonacci number:

␈↓ ↓H␈↓2.8)␈↓ β⊗␈↓↓fib[n] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ n=␈↓¬1 ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ fib[n-␈↓¬1␈↓↓] + fib[n-␈↓¬2␈↓↓]␈↓ 

␈↓ ↓H␈↓We␈α⊃note␈α⊃that␈α⊃this␈α⊃is␈α⊃a␈α⊃particularly␈α⊂unfavorable␈α⊃case␈α⊃for␈α⊃recursively␈α⊃defined␈α⊃functions␈α⊃as␈α⊂the
␈↓ ↓H␈↓evaluation␈α∞from␈α∞this␈α
definition␈α∞takes␈α∞order␈α
of␈α∞␈↓↓fib n␈↓␈α∞ammount␈α
of␈α∞work␈α∞for␈α
input␈α∞of␈α∞␈↓↓n.␈↓␈α∞ A␈α
more
␈↓ ↓H␈↓efficient definition is:

␈↓ ↓H␈↓␈↓ ∧k␈↓↓fiba[n] ← fibb[n,␈↓¬1␈↓↓,␈↓¬1␈↓↓]                 ␈↓
␈↓ ↓H␈↓2.9)
␈↓ ↓H␈↓␈↓ βv␈↓↓fibb[n,k,m] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ m ␈↓αelse␈↓↓ fibb[n-␈↓¬1␈↓↓,m,m+k]␈↓

␈↓ ↓H␈↓Evaluation␈αof␈αthe␈α␈↓↓n␈↓th␈α
Fibonacci␈αnumber␈αusing␈αthis␈αdefinition␈α
requires␈αonly␈αorder␈αof␈α
␈↓↓n␈↓␈αammount
␈↓ ↓H␈↓of work.

␈↓ ↓H␈↓        The␈α∞above␈α
forms␈α∞of␈α
recursive␈α∞function␈α
definition␈α∞all␈α
have␈α∞a␈α
very␈α∞nice␈α∞property.␈α
 Namely,
␈↓ ↓H␈↓assuming␈αthat␈αthe␈αgiven␈αfunctions␈αappearing␈αin␈αthe␈αschemas␈αare␈αtotal,␈αthe␈αfunction␈αdefined␈αby␈α
the
␈↓ ↓H␈↓schema␈αis␈α
total.␈α That␈αis␈α
the␈αrules␈α
given␈αfor␈αevaluation␈α
of␈αsuch␈αfunctions␈α
will␈αalways␈α
produce␈αan
␈↓ ↓H␈↓answer in a finite number of steps.  The general form of recursive definition

␈↓ ↓H␈↓2.10)␈↓ ∧y␈↓↓f[n1,...,nk] ← ␈↓πt␈↓↓[f,n1,...,nk]␈↓ 

␈↓ ↓H␈↓where␈α∂␈↓πt␈↓␈α∂is␈α⊂some␈α∂term␈α∂involving␈α⊂␈↓↓f,␈↓␈α∂the␈α∂arguments␈α∂␈↓↓ni␈↓␈α⊂and␈α∂perhaps␈α∂additional␈α⊂given␈α∂functions.
␈↓ ↓H␈↓Definitions of this general form are not guaranteed to define total functions.   For example

␈↓ ↓H␈↓2.11)␈↓ ¬g␈↓↓loop n ← loop n␈↓ 

␈↓ ↓H␈↓is␈αa␈α
perfectly␈αgood␈α
definition,␈αhowever␈αthe␈α
rules␈αfor␈α
computation␈αwill␈αnot␈α
produce␈αan␈α
answer␈αfor
␈↓ ↓H␈↓any␈α⊂value␈α⊂of␈α⊂␈↓↓n.␈↓␈α⊂ There␈α⊂are␈α⊂of␈α⊃course␈α⊂many␈α⊂cases␈α⊂where␈α⊂the␈α⊂function␈α⊂defined␈α⊂by␈α⊃a␈α⊂recursive
␈↓ ↓H␈↓definition is total, but where the definition does not fit any of the above patterns.

␈↓ ↓H␈↓        The␈α⊂standard␈α⊂example␈α⊂of␈α⊂a␈α⊂total␈α⊂function␈α⊂on␈α⊂natural␈α⊂numbers␈α⊂that␈α⊂is␈α⊂not␈α⊂definable␈α∂by
␈↓ ↓H␈↓primitive recursion is known as Ackerman's function and is defined by

␈↓ ↓H␈↓2.12)␈↓ αβ␈↓↓   ack[m,n] ← ␈↓αif␈↓↓ m=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ n+␈↓¬1 ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ack[m-␈↓¬1␈↓↓,␈↓¬1␈↓↓] ␈↓αelse␈↓↓ ack[m-␈↓¬1␈↓↓,ack[m,n-␈↓¬1␈↓↓]]␈↓. 

␈↓ ↓H␈↓If␈αyou␈αwork␈αout␈α
the␈αvalues␈αof␈α␈↓↓ack[m,n]␈↓␈α
for␈αincreasing␈αvalues␈αof␈α
the␈αarguments,␈αyou␈αwill␈α
see␈αthat
␈↓ ↓H␈↓the␈α
ammount␈α
of␈αcomputation␈α
grows␈α
very␈αrapidly.␈α
  This␈α
is␈αrelated␈α
to␈α
the␈αfact␈α
that␈α
the␈αfunction␈α
can
␈↓ ↓H␈↓not be defined by any collection of primitive recursion schemas.



␈↓ ↓H␈↓3.  ␈↓αSimple list recursion.␈↓


␈↓ ↓H␈↓        About␈α
the␈α
simplest␈α
form␈α
of␈α
recursion␈α
in␈α
LISP␈αoccurs␈α
when␈α
one␈α
of␈α
the␈α
arguments␈α
is␈α
a␈αlist,
␈↓ ↓H␈↓the␈α
result␈α
is␈αimmediate␈α
when␈α
the␈αargument␈α
is␈α
null,␈α
and␈αotherwise␈α
we␈α
need␈αonly␈α
know␈α
the␈αresult␈α
for
␈↓ ↓H␈↓the␈α∂d-part␈α∂of␈α∂that␈α∂argument.␈α∂ Several␈α∂of␈α∂the␈α∂functions␈α∂defined␈α∂in␈α∂Chapter␈α∂I␈α∂are␈α∂of␈α⊂this␈α∂form.
␈↓ ↓H␈↓Consider,␈α
for␈α
example,␈α
␈↓↓u*v␈↓,␈α
the␈α
result␈α
of␈α
␈↓↓append␈↓ing␈α
the␈α
list␈α
␈↓↓v␈↓␈α
to␈α
the␈α
list␈α
␈↓↓u.␈↓␈α
The␈α
result␈αis␈α
immediate
␈↓ ↓H␈↓for the case ␈↓αn|␈↓␈↓↓u␈↓ and otherwise depends on the result for ␈↓αd|␈↓␈↓↓u.␈↓  Thus, we have
␈↓ ↓H␈↓34␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓3.1)␈↓ ∧M␈↓↓u*v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]␈↓. 

␈↓ ↓H␈↓Notice␈α
that␈α∞if␈α
we␈α∞had␈α
tried␈α
to␈α∞recur␈α
on␈α∞␈↓↓v␈↓␈α
rather␈α∞than␈α
on␈α
␈↓↓u␈↓␈α∞we␈α
would␈α∞not␈α
have␈α∞been␈α
successful.
␈↓ ↓H␈↓The␈α∞result␈α
would␈α∞be␈α∞immediate␈α
for␈α∞␈↓αn|␈↓␈↓↓v,␈↓␈α∞but␈α
␈↓↓u*v␈↓␈α∞cannot␈α
be␈α∞constructed␈α∞in␈α
any␈α∞direct␈α∞way␈α
from
␈↓ ↓H␈↓␈↓↓u*␈↓αd|␈↓↓v␈↓␈αwithout␈αa␈αfunction␈αthat␈αputs␈αan␈αelement␈αonto␈αthe␈αend␈αof␈αa␈αlist.␈α (From␈αa␈αstrictly␈αlist␈αpoint␈αof
␈↓ ↓H␈↓view,␈αsuch␈α
a␈αfunction␈αwould␈α
be␈αas␈αelementary␈α
as␈α␈↓↓cons␈↓␈αwhich␈α
puts␈αan␈αelement␈α
onto␈αthe␈αfront␈α
of␈αa
␈↓ ↓H␈↓list,␈αbut,␈α
when␈αwe␈α
consider␈αthe␈αimplementation␈α
of␈αlists␈α
by␈αlist␈αstructures,␈α
we␈αsee␈α
that␈αthe␈αfunction␈α
is
␈↓ ↓H␈↓not␈αso␈αelementary.␈α This␈αhas␈αled␈αsome␈αpeople␈αto␈αconstruct␈αsystems␈αin␈αwhich␈αlists␈αare␈αbi-directional,
␈↓ ↓H␈↓but,␈αin␈αthe␈α
main,␈αthis␈αhas␈α
turned␈αout␈αto␈α
be␈αa␈αbad␈αidea).␈α
 Anyway,␈αit␈αis␈α
usually␈αeasier␈αto␈α
recur␈αon
␈↓ ↓H␈↓one argument of a function than to recur on the other.

␈↓ ↓H␈↓Another␈α
example␈α
is␈αthe␈α
function␈α
␈↓↓member␈↓␈αthat␈α
tests␈α
for␈α
membership␈αin␈α
a␈α
list.␈α If␈α
the␈α
list␈α
is␈αempty
␈↓ ↓H␈↓then␈αthe␈αanswer␈αis␈α␈↓¬NIL␈↓␈αotherwise␈αeither␈αthe␈αelement␈αwe␈αare␈αsearching␈αfor␈αis␈αthe␈αfirst␈αthing␈αon␈αthe
␈↓ ↓H␈↓list, or it in the rest of the list.  This reasoning gives rise to the recursive definition

␈↓ ↓H␈↓3.2)␈↓ βC␈↓↓member[x,u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ x = ␈↓αa|␈↓↓u ∨ member[x,␈↓αd|␈↓↓u]␈↓. 

␈↓ ↓H␈↓The␈α∞reader␈α∞should␈α∞observe␈α∞that␈α∞this␈α∞definition␈α∞is␈α∞equivalent␈α∞to␈α∞that␈α∞given␈α∞by␈α∞(I.8.6)␈α∞using␈α∞only
␈↓ ↓H␈↓logical connectives.

␈↓ ↓H␈↓        The function ␈↓↓reverse␈↓ is another example of simple list recursion.

␈↓ ↓H␈↓3.3)␈↓ βC␈↓↓reverse[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ NIL ␈↓αelse␈↓↓ [reverse ␈↓αd|␈↓↓u] * [␈↓αa|␈↓↓u . NIL]␈↓ 

␈↓ ↓H␈↓This␈α
straight␈α
forward␈α
definition␈α
of␈α
␈↓↓reverse␈↓␈α
 involves␈α␈↓↓append␈↓ing␈α
the␈α
␈↓↓reverse␈↓␈α
of␈α
the␈α
rest␈α
of␈α
the␈αlist␈α
to
␈↓ ↓H␈↓the list containing only the first element of the list.

␈↓ ↓H␈↓        As␈α∀with␈α∀recursive␈α∪definition␈α∀of␈α∀numerical␈α∪functions␈α∀we␈α∀see␈α∪that␈α∀simple␈α∀list␈α∪recursion
␈↓ ↓H␈↓parallels␈α
the␈α
description␈αof␈α
the␈α
domain␈αof␈α
lists␈α
which␈αsays␈α
that␈α
a␈α
list␈αis␈α
either␈α
␈↓¬NIL␈↓␈αor␈α
it␈α
is␈αthe␈α
result
␈↓ ↓H␈↓of␈α␈↓↓cons␈↓ing␈α
an␈αS-expression␈αonto␈α
a␈α"smaller"␈α
list.␈α  We␈αcan␈α
give␈αrecursion␈α
schemas␈αfor␈αlist␈α
recursion
␈↓ ↓H␈↓analagous␈αto␈αthose␈αfor␈αnumerical␈αfunctions.␈α  In␈αgeneral,␈αthe␈αrecursive␈αdefinition␈αof␈αa␈αfunction␈αon
␈↓ ↓H␈↓lists␈αmust␈αtake␈α
into␈αaccount␈αthe␈αfact␈α
that␈αwe␈αhave␈αnot␈α
one␈α"successor"␈αof␈αa␈α
given␈αlist,␈αbut␈α
one␈αfor
␈↓ ↓H␈↓each␈α∂possible␈α∂S-expression.␈α⊂ For␈α∂example␈α∂"primitive␈α∂list␈α⊂recursion"␈α∂without␈α∂parameters␈α⊂has␈α∂the
␈↓ ↓H␈↓form

␈↓ ↓H␈↓3.4)␈↓ ∧+␈↓↓f[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ g0 ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓u, ␈↓αd|␈↓↓u, f[␈↓αd|␈↓↓u]]␈↓ 

␈↓ ↓H␈↓Taking␈αg0␈α=␈α␈↓¬NIL␈↓␈αand␈α␈↓↓h[x,v,w]=␈αw␈α*␈α[x␈α.␈α␈↓¬NIL␈↓↓]␈↓␈αwe␈αget␈αthe␈αdefinition␈αof␈α␈↓↓reverse␈↓␈αgiven␈αabove␈α(3.3).
␈↓ ↓H␈↓If we mix numerical and list computation  taking g0=␈↓¬0 ␈↓and ␈↓↓h[x,u,n]=n+␈↓¬1␈↓↓␈↓ we get

␈↓ ↓H␈↓3.5)␈↓ ∧-␈↓↓length[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ length[␈↓αd|␈↓↓u]+␈↓¬1␈↓↓␈↓ 

␈↓ ↓H␈↓The function ␈↓↓last␈↓  which computes the last element of a list is another example.

␈↓ ↓H␈↓3.6)␈↓ ∧U␈↓↓last u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ ␈↓αa|␈↓↓u ␈↓αelse␈↓↓ last ␈↓αd|␈↓↓u␈↓, 

␈↓ ↓H␈↓In␈αorder␈αto␈αfit␈αthe␈αschema␈αexactly␈αthe␈αabove␈αdefinition␈αneeds␈αto␈αbe␈αpatched␈αsomewhat.␈α See␈αif␈αyou
␈↓ ↓H␈↓can figure out what must be done and what the "given" function ␈↓↓h␈↓ should be.

␈↓ ↓H␈↓The schema for primitive list recursion with parameters (only one parameter shown) is
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *35


␈↓ ↓H␈↓3.7)␈↓ β`␈↓↓f[u,x] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ g[x] ␈↓αelse␈↓↓  h[␈↓αa|␈↓↓u, ␈↓αd|␈↓↓u, x, f[␈↓αd|␈↓↓u, v]]␈↓. 

␈↓ ↓H␈↓␈↓↓append␈↓␈α
(3.1),␈α
␈↓↓member␈↓␈α
(3.2)␈α
and␈α
␈↓↓assoc␈↓␈α
(I.9.3)␈αare␈α
examples␈α
of␈α
this␈α
form␈α
of␈α
definition.␈α
  Allowing␈αa
␈↓ ↓H␈↓given␈αfunction␈αof␈αthe␈α
arguments␈αto␈αoccur␈αin␈α
the␈αparamater␈αposition␈αin␈α
the␈αrecursive␈αcall␈αon␈α
␈↓↓f␈↓␈αwe
␈↓ ↓H␈↓obtain the schema

␈↓ ↓H␈↓3.8)␈↓ β≤␈↓↓f[u,x] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ g[x] ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓u, ␈↓αd|␈↓↓u, x, f[␈↓αd|␈↓↓u, j[␈↓αa|␈↓↓u, ␈↓αd|␈↓↓u, x]]␈↓. 

␈↓ ↓H␈↓The␈α⊃definition␈α⊃of␈α⊃␈↓↓reverse␈↓␈α⊃(I.8.9)␈α⊃in␈α⊃terms␈α⊃of␈α⊃the␈α⊃basic␈α⊃␈↓↓cons␈↓␈α⊃operation,␈α⊃itself␈α⊃and␈α⊃an␈α⊂auxiliary
␈↓ ↓H␈↓variable has this form.

␈↓ ↓H␈↓        The␈α
simple␈α
recursion␈α
on␈α
lists␈α
without␈α
parameters␈α
simply␈α
proceeds␈α
through␈α
a␈α
list␈αgathering
␈↓ ↓H␈↓results␈α∞to␈α∂be␈α∞used␈α∞in␈α∂constructing␈α∞the␈α∞answer,␈α∂but␈α∞provides␈α∞no␈α∂access␈α∞to␈α∂information␈α∞previously
␈↓ ↓H␈↓obtained.␈α
 When␈αwe␈α
allow␈αparameters␈α
to␈α
be␈αcarried␈α
along␈αthe␈α
information␈α
can␈αbe␈α
passed␈αdown␈α
the
␈↓ ↓H␈↓list␈α∂as␈α∂well␈α∂as␈α∂results␈α∂being␈α∂passed␈α∂back␈α∂up.␈α∂ The␈α∂appropriate␈α∂mixing␈α∂of␈α∂control␈α∂structure␈α∞and
␈↓ ↓H␈↓information␈αpassing␈αis␈α
often␈αthe␈αkey␈α
to␈αsolving␈αa␈α
programming␈αproblem.␈α We␈α
shall␈αsee␈αin␈α
a␈αlater
␈↓ ↓H␈↓sections␈α∞how␈α∞one␈α
can␈α∞sometimes␈α∞solve␈α
the␈α∞two␈α∞aspects␈α
of␈α∞the␈α∞problem␈α
separately␈α∞and␈α∞then␈α
piece
␈↓ ↓H␈↓together the results.

␈↓ ↓H␈↓        The␈α
function␈α
␈↓↓alt␈↓␈α(I.8.1)␈α
which␈α
returns␈αa␈α
list␈α
of␈α
alternate␈αelements␈α
of␈α
a␈αlist␈α
is␈α
also␈α
based␈αon
␈↓ ↓H␈↓list␈αrecursion,␈α
however␈αthe␈αform␈α
of␈αthe␈α
recursion␈αis␈αdifferent.␈α
 Here␈αthe␈αbase␈α
cases␈αare␈α
the␈αempty
␈↓ ↓H␈↓list␈αand␈αthe␈αlist␈αcontaining␈αone␈αelement.␈α In␈αthe␈αgeneral␈αcase␈αwe␈αuse␈αthe␈αresult␈αfor␈α␈↓αdd|␈↓␈↓↓u␈↓␈αto␈αcompute
␈↓ ↓H␈↓the result for ␈↓↓u.␈↓  This is analogous to the "course of values" recursion on numbers (2.7).

␈↓ ↓H␈↓        The␈α∂above␈α∂schemas,␈α⊂like␈α∂their␈α∂numerical␈α∂counterparts␈α⊂yield␈α∂definitions␈α∂of␈α⊂total␈α∂functions
␈↓ ↓H␈↓(assuming␈αthat␈αthe␈αgiven␈αfunctions␈αare␈αtotal).␈α The␈αmost␈αgeneral␈αform␈αof␈αlist␈αrecursion␈αis␈αthe␈αsame
␈↓ ↓H␈↓as␈αthat␈αfor␈αnumerical␈αrecursion␈α(2.10).␈α In␈αthe␈αcase␈αof␈αpure␈αlist␈αrecursion␈αwe␈αbuild␈αthe␈αterm␈αon␈αthe
␈↓ ↓H␈↓right␈αhand␈α
side␈αfrom␈α
basic␈αlist␈α
functions␈αand␈α
expect␈αthe␈α
arguments␈αto␈α
be␈αlists.␈α
 As␈αwe␈α
have␈αseen
␈↓ ↓H␈↓above␈α
we␈α
often␈α
mix␈αcomputation␈α
involving␈α
lists,␈α
numbers␈αand␈α
S-expression␈α
in␈α
general.␈α
 Also,␈αas
␈↓ ↓H␈↓with␈αnumerical␈αrecursion,␈αrecursive␈αdefinition␈αof␈αfunctions␈αon␈αlists␈αdoes␈αnot␈αin␈αgeneral␈αgive␈αrise␈αto
␈↓ ↓H␈↓total␈αfunctions␈αand␈α
some␈αcare␈αmust␈α
be␈αused␈αto␈α
make␈αsure␈αyour␈α
program␈αwill␈αterminate.␈α
  We␈αwill
␈↓ ↓H␈↓have more to say about proving termination in Chapter III.



␈↓ ↓H␈↓4.  ␈↓αSimple S-expression recursion.␈↓


␈↓ ↓H␈↓        In␈αthe␈αcase␈αof␈αrecursion␈α
on␈αthe␈αstructure␈αof␈αS-expressions,␈α
the␈αsimplest␈αsituation␈αis␈αwhen␈α
the
␈↓ ↓H␈↓value␈αof␈αthe␈αfunction␈αis␈αimmediate␈αfor␈αatoms,␈αand␈αfor␈αnon-atoms␈αdepends␈αonly␈αon␈αthe␈αvalues␈αfor
␈↓ ↓H␈↓the a-part and the d-part of the argument.  Thus ␈↓↓subst␈↓ was defined by

␈↓ ↓H␈↓4.1)␈↓ α→␈↓↓subst[x,y,z] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ [␈↓αif␈↓↓ z ␈↓αeq␈↓↓ y ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ z] ␈↓αelse␈↓↓ subst[x,y,␈↓αa|␈↓↓z] . subst[x,y,␈↓αd|␈↓↓z]␈↓. 

␈↓ ↓H␈↓More generally we have recursive definitions of the form

␈↓ ↓H␈↓4.2)␈↓ βq␈↓↓f[x] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ g[x] ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓x, ␈↓αd|␈↓↓x, f[␈↓αa|␈↓↓x], f[␈↓αd|␈↓↓x]]␈↓. 

␈↓ ↓H␈↓Here␈α⊂again␈α⊂the␈α⊂recursive␈α⊂definition␈α⊂of␈α⊂functions␈α⊂parallels␈α⊂the␈α⊂description␈α⊂of␈α⊂the␈α⊃domain.␈α⊂The
␈↓ ↓H␈↓definitions of ␈↓↓size␈↓ (the number of atoms in an S-expression) and ␈↓↓fringe␈↓
␈↓ ↓H␈↓36␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓4.3)␈↓ ∧'␈↓↓size[x] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x␈↓ 

␈↓ ↓H␈↓4.4)␈↓ βl␈↓↓fringe x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x␈↓ 

␈↓ ↓H␈↓are further examples of this form of definition.

␈↓ ↓H␈↓        The S-expression analogue of (2.6) and (3.8) (primitive S-expression recursion) is

␈↓ ↓H␈↓4.5)␈↓ αl␈↓↓f[x,y] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ g[x,y] ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓x,␈↓αd|␈↓↓x,y,f[␈↓αa|␈↓↓x,ja[x,y]],f[␈↓αd|␈↓↓x,jd[x,y]]]␈↓ 

␈↓ ↓H␈↓and the following definition of ␈↓↓equal␈↓ is of this form

␈↓ ↓H␈↓4.6)␈↓ ↓r␈↓↓   x=y ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ [␈↓αif␈↓↓ ␈↓αat|␈↓↓y ␈↓αthen␈↓↓ x ␈↓αeq␈↓↓ y ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓] ␈↓αelse␈↓↓  ␈↓αif␈↓↓ ␈↓αat|␈↓↓y ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓x=␈↓αa|␈↓↓y ∧ ␈↓αd|␈↓↓x=␈↓αd|␈↓↓y␈↓. 

␈↓ ↓H␈↓Note␈αthat␈αusing␈αthe␈αfact␈αthat␈α␈↓αeq␈↓␈αis␈αin␈αfact␈αdefined␈αon␈αnon-atoms␈αin␈αactual␈αLISP␈αimplementations
␈↓ ↓H␈↓and properties of the propositional functions, we can simplify the definition of ␈↓↓equal␈↓ to

␈↓ ↓H␈↓4.7)␈↓ βp␈↓↓x = y ← x ␈↓αeq␈↓↓ y ∨ [¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y ∧ ␈↓αa|␈↓↓x = ␈↓αa|␈↓↓y ∧ ␈↓αd|␈↓↓x = ␈↓αd|␈↓↓y]␈↓. 

␈↓ ↓H␈↓as was given in Chapter I.

␈↓ ↓H␈↓        Although␈αthe␈αabove␈αdefinition␈αof␈α␈↓↓equal␈↓␈αis␈αan␈αinstance␈αof␈αthe␈αschema␈α(4.5)␈αit␈αis␈αmore␈αnatural
␈↓ ↓H␈↓to␈α
think␈α
of␈α
it␈α
as␈α
a␈α
parallel␈α
recursion␈α
through␈α
the␈α
two␈α
arguments␈α
rather␈α
than␈α
thinking␈α
of␈α
one␈α
of
␈↓ ↓H␈↓them␈α∞as␈α∞a␈α∞parameter.␈α∞ This␈α∂could␈α∞of␈α∞course␈α∞be␈α∞elaborated␈α∂to␈α∞carry␈α∞along␈α∞parameters␈α∞as␈α∂well␈α∞as
␈↓ ↓H␈↓recurring␈α∂in␈α⊂parallel␈α∂through␈α⊂some␈α∂collection␈α⊂of␈α∂arguments.␈α∂In␈α⊂later␈α∂chapters␈α⊂we␈α∂will␈α⊂see␈α∂more
␈↓ ↓H␈↓examples␈αof␈αprograms␈αthat␈αrecur␈αon␈αmore␈αthan␈αone␈αargument.␈α (For␈αexample␈α␈↓↓samefringe␈↓␈α(III.13.1).
␈↓ ↓H␈↓The␈αimportant␈αpoint␈αto␈αcheck␈αin␈αsuch␈αprograms␈αis␈αthat␈αthe␈αcollection␈αof␈αarguments␈αbeing␈αrecurred
␈↓ ↓H␈↓on is always "simpler" in recursive calls to the program being defined.

␈↓ ↓H␈↓        The definition of ␈↓↓flatten␈↓ is an example of a double recursion.

␈↓ ↓H␈↓␈↓ ∧t␈↓↓flatten[x] ← flat[x, ␈↓¬NIL␈↓↓]           ␈↓
␈↓ ↓H␈↓4.8)
␈↓ ↓H␈↓␈↓ βr␈↓↓flat[x,y] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x . y ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,y]]␈↓

␈↓ ↓H␈↓Although␈α␈↓↓flatten␈↓␈αcomputes␈α
the␈αsame␈αfunction␈α
as␈α␈↓↓fringe,␈↓␈αit␈αis␈α
more␈αefficient␈α because␈α
the␈α␈↓↓append␈↓
␈↓ ↓H␈↓operation␈α∞used␈α∞by␈α∂␈↓↓fringe␈↓␈α∞copies␈α∞unnecessarily.␈α∂  As␈α∞we␈α∞shall␈α∂see␈α∞in␈α∞Chapter␈α∂,␈α∞␈↓↓flatten␈↓␈α∞is␈α∂also␈α∞an
␈↓ ↓H␈↓improvement␈α
over␈α
␈↓↓fringe␈↓␈α
in␈α
that␈α
it␈α
is␈α
partially␈α
amenable␈α
to␈α
tail␈α
recursive␈α
evaluation.␈α
  The␈α
kind␈α
of
␈↓ ↓H␈↓double␈αrecursion␈αused␈αin␈α␈↓↓flatten␈↓␈αis␈αoften␈αuseful.␈αOne␈αway␈αof␈αthinking␈αof␈αthis␈αform␈αof␈αrecursion␈αis
␈↓ ↓H␈↓that␈α∞results␈α∞are␈α∞computed␈α∞from␈α∂one␈α∞side␈α∞of␈α∞the␈α∞S-expression␈α∞(the␈α∂␈↓αd␈↓␈α∞side␈α∞in␈α∞this␈α∞case)␈α∂and␈α∞then
␈↓ ↓H␈↓passed␈αalong␈αto␈αthe␈αother␈αside.␈α Thus␈αwe␈αobtain␈α
a␈αflow␈αof␈αinformation␈αfrom␈αside␈αto␈αside␈αas␈αwell␈α
as
␈↓ ↓H␈↓top␈αto␈αbottom.␈α The␈αtechnique␈αof␈αwriting␈αa␈αsimple␈αand␈αeasily␈αunderstandable␈α
recursive␈αdefinition
␈↓ ↓H␈↓and␈αthen␈α
later␈αoptimizing␈αby␈α
the␈αkind␈α
of␈αtransformation␈αmade␈α
in␈αgoing␈αfrom␈α
␈↓↓fringe␈↓␈αto␈α
␈↓↓flatten␈↓␈αis
␈↓ ↓H␈↓often useful.

␈↓ ↓H␈↓        Note␈α∂that␈α∞the␈α∂definition␈α∞of␈α∂␈↓↓flat␈↓␈α∞does␈α∂not␈α∞fit␈α∂the␈α∞general␈α∂primitive␈α∞recursive␈α∂schema␈α∞(4.5).
␈↓ ↓H␈↓Thus␈α⊃we␈α⊂have␈α⊃an␈α⊂example␈α⊃where␈α⊂one␈α⊃definition␈α⊂of␈α⊃a␈α⊂function␈α⊃clearly␈α⊂fits␈α⊃one␈α⊂of␈α⊃the␈α⊂simple
␈↓ ↓H␈↓recursion␈αschemas␈α
and␈αthus␈α
defines␈αa␈α
total␈αfunction,␈αwhile␈α
for␈αanother␈α
definition␈αof␈α
the␈αfunction
␈↓ ↓H␈↓this␈α∞fact␈α
is␈α∞not␈α
immediate.␈α∞ Of␈α
course␈α∞showing␈α
that␈α∞both␈α
definitions␈α∞compute␈α
the␈α∞same␈α
function
␈↓ ↓H␈↓also␈α∩requires␈α⊃some␈α∩work.␈α∩  We␈α⊃could␈α∩add␈α⊃to␈α∩our␈α∩list␈α⊃of␈α∩schemas␈α⊃some␈α∩which␈α∩allow␈α⊃multiple
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *37


␈↓ ↓H␈↓recursions␈α
such␈αas␈α
exhibited␈αby␈α
␈↓↓flatten.␈↓␈α However␈α
no␈αmatter␈α
how␈αmany␈α
such␈αforms␈α
for␈αdefining
␈↓ ↓H␈↓total␈αfunctions␈αwe␈αallow,␈αwe␈αwill␈αeventually␈αhave␈αto␈αresort␈αto␈αthe␈αgeneral␈αform␈α(2.10)␈αwhich␈αis␈αalso
␈↓ ↓H␈↓the general form for S-expression recursion.



␈↓ ↓H␈↓5.  ␈↓αOther structural recursions.␈↓


␈↓ ↓H␈↓        When␈α
S-expressions␈α
are␈α
used␈α
to␈α
represent␈α
a␈α
class␈α
of␈α
expressions␈α
that␈α
are␈αdefined␈α
inductively
␈↓ ↓H␈↓then␈α∞functions␈α∞of␈α∞these␈α∞expressions␈α∞often␈α∞have␈α∞a␈α∞recursive␈α∞form␈α∞closely␈α∞related␈α∞to␈α∞the␈α∞inductive
␈↓ ↓H␈↓definition␈α⊃of␈α⊃the␈α⊃expressions.␈α⊃ For␈α⊂example␈α⊃the␈α⊃arithmetic␈α⊃interpreter␈α⊃␈↓↓numval␈↓␈α⊃(I.9.4)␈α⊂computes
␈↓ ↓H␈↓directly␈αthe␈αvalue␈αfor␈αconstants␈αand␈αvariables␈αand␈αfor␈αsums␈αand␈αproducts␈αit␈αcomputes␈αthe␈αvalue␈α
in
␈↓ ↓H␈↓terms␈α∂of␈α∂the␈α∂values␈α⊂of␈α∂the␈α∂subexpressions␈α∂from␈α∂which␈α⊂the␈α∂sum␈α∂or␈α∂product␈α∂is␈α⊂constructd.␈α∂ The
␈↓ ↓H␈↓function␈α␈↓↓diff␈↓␈α(I.11.3)␈αwhich␈αsymbolically␈αdifferentiates␈αthe␈αsame␈αclass␈αof␈αexpressions␈αhas␈α
a␈αsimilar
␈↓ ↓H␈↓recursive␈α
structure␈αin␈α
that␈αit␈α
knows␈αthe␈α
answers␈αimmediately␈α
for␈αconstants␈α
and␈αvariables␈α
and␈αfor
␈↓ ↓H␈↓sums␈α
and␈α
products␈αit␈α
uses␈α
the␈αresults␈α
for␈α
subexpressions␈αto␈α
obtain␈α
the␈αfinal␈α
answer.␈α
 If␈αwe␈α
consider
␈↓ ↓H␈↓the␈α∂arithmetic␈α∞expressions␈α∂where␈α∂sum␈α∞and␈α∂product␈α∂are␈α∞simple␈α∂binary␈α∂operators␈α∞we␈α∂can␈α∂write␈α∞a
␈↓ ↓H␈↓simple recursion schema as follows:

␈↓ ↓H␈↓␈↓ αx␈↓↓f[x,y] ←␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ num[x] ␈↓αthen␈↓↓ gnum[x,y] ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓5.1)␈↓ β8␈↓↓␈↓αif␈↓↓ isvar[x] ␈↓αthen␈↓↓ gvar[x,y] ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ issum[x] ␈↓αthen␈↓↓ gsum[s1 x, s2 x, y, f[s1 x, y], f[s2 x, y]] ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ispro[x] ␈↓αthen␈↓↓ gprod[p1 x, p2 x, y, f[p1 x, y], f[p2 x, y]]␈↓

␈↓ ↓H␈↓Then the evaluation function ␈↓↓numval␈↓

␈↓ ↓H␈↓␈↓ αx␈↓↓numval[x,y] ←␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ isnum[x] ␈↓αthen␈↓↓ cval[x] ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓5.2)␈↓ β8␈↓↓␈↓αif␈↓↓ isvar[x] ␈↓αthen␈↓↓ lookup[x,y] ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ issum[x] ␈↓αthen␈↓↓ sum[numval[s1 x,y] numval[s2 x,y]] ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ isprod[x] ␈↓αthen␈↓↓ prod[numval[s1 x,y] numval[s2 x,y]] ␈↓

␈↓ ↓H␈↓is an instance of this schema where ␈↓↓cval,␈↓ ␈↓↓lookup,␈↓ ␈↓↓sum␈↓ and ␈↓↓prod␈↓ are given.

␈↓ ↓H␈↓        A␈α∪more␈α∪complicated␈α∪example␈α∪of␈α∀an␈α∪interpreter␈α∪with␈α∪recursive␈α∪structure␈α∪based␈α∀on␈α∪the
␈↓ ↓H␈↓expression language structure is the LISP interpreter ␈↓↓eval␈↓ (I.13.1).

␈↓ ↓H␈↓        In␈α
general␈α
if␈α
we␈α
have␈α
a␈α
domain␈α
defined␈α
by␈α
starting␈α
with␈α
a␈α
base␈α
set␈α
(with␈α
computable␈αtest␈α
for
␈↓ ↓H␈↓membership)␈α∩and␈α∪a␈α∩collection␈α∪constructors␈α∩with␈α∪which␈α∩we␈α∪build␈α∩elements␈α∪of␈α∩the␈α∪domain␈α∩by
␈↓ ↓H␈↓repeated␈αapplication␈α
starting␈αwith␈αelements␈α
of␈αthe␈αbase␈α
set,␈αthen␈αthere␈α
is␈αa␈α
corresponding␈αschema
␈↓ ↓H␈↓for␈αrecursive␈αdefinition␈αof␈αfunctions␈αon␈αthis␈αdomain.␈α  We␈αwill␈αsee␈αmore␈αexamples␈αand␈αhave␈αmore
␈↓ ↓H␈↓to say about this in later chapters.
␈↓ ↓H␈↓38␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓6.  ␈↓αGeneral tree recursion.␈↓


␈↓ ↓H␈↓        A␈αtree␈αstructure␈αis␈αeither␈αa␈αleaf␈αor␈αa␈αnode␈αtogether␈αwith␈αa␈αcollection␈αof␈αimmediate␈αsuccessors
␈↓ ↓H␈↓which␈α
are␈α
also␈α∞trees.␈α
 Many␈α
data␈α∞structures␈α
can␈α
be␈α
viewed␈α∞as␈α
trees.␈α
 For␈α∞example,␈α
S-expressions
␈↓ ↓H␈↓with␈αleaves␈αbeing␈αatoms␈αand␈αeach␈αnon-leaf␈αtree␈αhas␈αtwo␈αsuccessors,␈αgiven␈αby␈αthe␈α␈↓↓car␈↓␈αand␈αthe␈α␈↓↓cdr.␈↓
␈↓ ↓H␈↓More␈αgenerally␈αthe␈αstructures␈αdescribed␈αin␈αsection␈α␈↓π∞␈↓5␈α
can␈αbe␈αthought␈αof␈αas␈αtree␈αlike␈αstructures␈α
with
␈↓ ↓H␈↓the␈α∂basic␈α∞elements␈α∂as␈α∞leaves,␈α∂and␈α∂each␈α∞non-leaf␈α∂tree␈α∞having␈α∂immediate␈α∞successors␈α∂given␈α∂by␈α∞the
␈↓ ↓H␈↓selector␈α
functions␈α
for␈α
the␈α
particular␈α
type␈α
of␈α
node.␈α
 These␈α
structures␈α
all␈α
have␈α
two␈α
things␈α
in␈α
common.
␈↓ ↓H␈↓Namely,␈α(i)␈α
each␈αkind␈αof␈α
node␈αhas␈αa␈α
fixed␈αnumber␈α
of␈αsuccessors␈αand␈α
(ii)␈αwhen␈αdoing␈α
computations
␈↓ ↓H␈↓each such tree, is constructed from parts already present.

␈↓ ↓H␈↓        A␈αanother␈α
example␈αis␈α
a␈αtree␈α
 which␈αis␈αgiven␈α
by␈αan␈α
initial␈αposition␈α
together␈αwith␈αa␈α
␈↓↓successors␈↓
␈↓ ↓H␈↓function␈αfrom␈α
which␈αwe␈αmay␈α
obtain␈αthe␈αimmediate␈α
successors␈αof␈αany␈α
position␈α(of␈αrelevance).␈α
 Here
␈↓ ↓H␈↓a␈α∞node␈α
may␈α∞have␈α
an␈α∞arbitrary␈α
number␈α∞of␈α
successors␈α∞and␈α
parts␈α∞of␈α
the␈α∞trees␈α
are␈α∞built␈α∞as␈α
needed.
␈↓ ↓H␈↓Functions␈α⊂on␈α⊂such␈α⊂trees␈α⊂typically␈α⊂involve␈α⊂two␈α⊂processes,␈α⊂one␈α⊂for␈α⊂recognizing␈α⊂and␈α⊃dealing␈α⊂with
␈↓ ↓H␈↓leaves␈α⊂and␈α⊂one␈α⊃for␈α⊂processing␈α⊂a␈α⊃collection␈α⊂of␈α⊂succesors.␈α⊂ (Of␈α⊃course␈α⊂the␈α⊂two␈α⊃processes␈α⊂interact
␈↓ ↓H␈↓recursively.)

␈↓ ↓H␈↓        Consider␈α
the␈α
problem␈α
for␈αsearching␈α
a␈α
tree␈α
for␈αa␈α
position␈α
having␈α
some␈α
particular␈αproperty.
␈↓ ↓H␈↓We␈αcan␈αwrite␈αa␈αprogram␈αthat␈αdescribes␈αa␈αdepth␈αfirst␈α search␈αindependent␈αof␈αthe␈αproperty␈αor␈αkind
␈↓ ↓H␈↓of␈α∀tree.␈α∪ It␈α∀can␈α∀be␈α∪used␈α∀to␈α∪search␈α∀specific␈α∀trees␈α∪ by␈α∀defining␈α∪the␈α∀three␈α∀auxiliary␈α∪functions
␈↓ ↓H␈↓␈↓↓successors,␈↓ ␈↓↓ter,␈↓ and ␈↓↓lose␈↓ for the particular problem.  We have

␈↓ ↓H␈↓6.1)  ␈↓ αQ␈↓↓search p ← ␈↓αif␈↓↓ lose p ␈↓αthen␈↓↓ ␈↓¬LOSE ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ p ␈↓αelse␈↓↓ searchlis[successors p]␈↓ 

␈↓ ↓H␈↓where

␈↓ ↓H␈↓6.2) ␈↓ αβ␈↓↓searchlis u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬LOSE ␈↓↓ ␈↓αelse␈↓↓ {search ␈↓αa|␈↓↓u}[λx: ␈↓αif␈↓↓ x = ␈↓¬LOSE ␈↓↓␈↓αthen␈↓↓ searchlis ␈↓αd|␈↓↓u ␈↓αelse␈↓↓ x]␈↓. 

␈↓ ↓H␈↓In␈α
the␈α
applications,␈α
we␈α
start␈α
with␈αa␈α
position␈α
␈↓↓p0,␈↓␈α
and␈α
we␈α
are␈αlooking␈α
for␈α
a␈α
win␈α
in␈α
the␈αsuccessor␈α
tree
␈↓ ↓H␈↓of␈α
␈↓↓p0.␈↓␈α Certain␈α
positions␈αlose␈α
and␈α
there␈αis␈α
no␈αpoint␈α
looking␈α
at␈αtheir␈α
successors.␈α This␈α
is␈αdecided␈α
by
␈↓ ↓H␈↓the␈α
predicate␈α∞␈↓↓lose.␈↓␈α
 A␈α
position␈α∞is␈α
a␈α
win␈α∞if␈α
it␈α
doesn't␈α∞lose␈α
and␈α
it␈α∞satisfies␈α
the␈α
predicate␈α∞␈↓↓ter.␈↓␈α
 The
␈↓ ↓H␈↓successors␈α∞of␈α∞a␈α∞position␈α∂are␈α∞given␈α∞by␈α∞the␈α∂function␈α∞␈↓↓successors,␈↓␈α∞and␈α∞the␈α∂value␈α∞of␈α∞␈↓↓search␈α∞p␈↓␈α∂is␈α∞the
␈↓ ↓H␈↓winning␈αposition.␈α No␈α
non-losing␈αposition␈αshould␈αhave␈α
the␈αname␈α␈↓¬LOSE␈α
␈↓or␈αthe␈αfunction␈αwon't␈α
work
␈↓ ↓H␈↓properly.

␈↓ ↓H␈↓        One␈α∪application␈α∪is␈α∪finding␈α∪a␈α∩path␈α∪from␈α∪an␈α∪initial␈α∪node␈α∩to␈α∪a␈α∪final␈α∪node␈α∪in␈α∪a␈α∩graph
␈↓ ↓H␈↓represented␈α
by␈α
a␈α
list␈α
structure␈α
as␈α
described␈α
in␈α∞chapter␈α
I.␈α
 A␈α
position␈α
is␈α
a␈α
path␈α
starting␈α∞from␈α
the
␈↓ ↓H␈↓initial␈αnode␈αand␈αcontinuing␈αto␈αsome␈αintermediate␈αnode␈αand␈αis␈αrepresented␈αby␈αa␈αlist␈αof␈αits␈αnodes␈αin
␈↓ ↓H␈↓reverse order.  The three functions for this application are

␈↓ ↓H␈↓6.3)␈↓ βx␈↓↓lose p ← ␈↓αa|␈↓↓p ε ␈↓αd|␈↓↓p ,     ␈↓
␈↓ ↓H␈↓6.4)␈↓ βx␈↓↓ter p ← [␈↓αa|␈↓↓p = final] ,    ␈↓
␈↓ ↓H␈↓6.5)␈↓ βx␈↓↓successors p ← mapcar[λx: x . p, ␈↓αd|␈↓↓assoc[␈↓αa|␈↓↓p, graph]]␈↓.

␈↓ ↓H␈↓The␈αnode␈αwe␈αare␈αproposing␈αto␈αvisit␈αnext␈αloses␈αif␈αit␈αis␈αalready␈αin␈αthe␈αpath,␈αas␈αthat␈αmeans␈αwe␈αhave
␈↓ ↓H␈↓been␈α∞here␈α∞before.␈α∞ The␈α
successors␈α∞to␈α∞a␈α∞position␈α∞are␈α
all␈α∞of␈α∞those␈α∞positions␈α∞immediately␈α
reachable
␈↓ ↓H␈↓from␈α
the␈αcurrent␈α
node.␈α
 In␈αour␈α
list␈αnotation␈α
for␈α
a␈αgraph␈α
the␈α
immediately␈αreachable␈α
nodes␈αare␈α
those
␈↓ ↓H␈↓on␈α
the␈α
list␈α
associated␈α
with␈α
the␈α
current␈α
node␈α
in␈α
␈↓↓graph.␈↓␈α
 A␈α
position␈α
is␈α
terminal␈α
if␈α
the␈α
current␈α
node␈α
is
␈↓ ↓H␈↓the desired goal ␈↓↓final.␈↓
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *39


␈↓ ↓H␈↓        ␈↓↓search␈↓␈α∞is␈α∞used␈α
when␈α∞we␈α∞want␈α
to␈α∞search␈α∞a␈α∞tree␈α
of␈α∞possibilities␈α∞for␈α
a␈α∞solution␈α∞to␈α∞a␈α
problem.
␈↓ ↓H␈↓Naturally␈α∞we␈α∞can␈α∞do␈α∞other␈α∞things␈α∞with␈α∞tree␈α∞search␈α∞recursion␈α∞than␈α∞just␈α∞search.␈α∞ For␈α∞example␈α
we
␈↓ ↓H␈↓may␈α
want␈αto␈α
find␈αall␈α
solutions␈α
to␈αa␈α
problem.␈α This␈α
can␈αbe␈α
done␈α
with␈αa␈α
function␈α␈↓↓allsol␈↓␈α
that␈αuses␈α
the
␈↓ ↓H␈↓same ␈↓↓lose,␈↓ ␈↓↓ter,␈↓ and ␈↓↓successors␈↓ functions as does ␈↓↓search.␈↓ The simplest way to write ␈↓↓allsol␈↓ is

␈↓ ↓H␈↓6.6) ␈↓ α!␈↓↓allsol p ← ␈↓αif␈↓↓ lose p ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ <p> ␈↓αelse␈↓↓ mapapp[allsol, successors p]␈↓, 

␈↓ ↓H␈↓where

␈↓ ↓H␈↓6.7) ␈↓ β≡␈↓↓mapapp[fn, u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ fn[␈↓αa|␈↓↓u] * mappap[fn, ␈↓αd|␈↓↓u]␈↓. 

␈↓ ↓H␈↓This␈αform␈αof␈αthe␈αfunction␈αis␈αsomewhat␈αinefficient␈αbecause␈αof␈αall␈αthe␈α␈↓↓append␈↓ing.␈α A␈αmore␈αefficient
␈↓ ↓H␈↓form uses an auxiliary function as follows:

␈↓ ↓H␈↓␈↓ β8␈↓↓allsol p ← allsola[p, ␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓allsola[p, found] ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ lose p ␈↓αthen␈↓↓ found␈↓
␈↓ ↓H␈↓6.8)␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ p . found␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ allsolb[successors p, found]␈↓,

␈↓ ↓H␈↓␈↓ β8␈↓↓allsolb[u, found] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ found ␈↓αelse␈↓↓ allsolb[␈↓αd|␈↓↓u, allsola[␈↓αa|␈↓↓u, found]]␈↓.

␈↓ ↓H␈↓The␈α⊃recursive␈α⊂program␈α⊃structure␈α⊂that␈α⊃arises␈α⊂here␈α⊃is␈α⊃common␈α⊂when␈α⊃a␈α⊂list␈α⊃is␈α⊂to␈α⊃be␈α⊃formed␈α⊂by
␈↓ ↓H␈↓recurring through a list structure.

␈↓ ↓H␈↓        We␈αwill␈αgive␈αfurther␈αapplications␈αof␈αand␈αvariations␈αon␈αthis␈αform␈αof␈αtree␈αsearch␈αrecursion␈αin
␈↓ ↓H␈↓Chapter VI.



␈↓ ↓H␈↓7.  ␈↓αSolving a LISP programming problem.␈↓


␈↓ ↓H␈↓        In␈αthis␈αsection␈αwe␈α
present␈αa␈αprogramming␈αproblem␈αthat␈α
is␈αsomewhat␈αmore␈αcomplex␈αthan␈α
the
␈↓ ↓H␈↓examples␈αof␈αearlier␈αsections␈α
and␈αwork␈αout␈αthe␈α
solution␈αin␈αdetail,␈αthe␈α
purpose␈αbeing␈αto␈αhelp␈αyou␈α
get
␈↓ ↓H␈↓started␈α∂thinking␈α∂recursively␈α∂and␈α⊂to␈α∂show␈α∂how␈α∂a␈α⊂typical␈α∂problem␈α∂might␈α∂be␈α⊂approached.␈α∂  After
␈↓ ↓H␈↓describing␈α
the␈α
function␈α∞we␈α
wish␈α
to␈α
compute,␈α∞we␈α
will␈α
write␈α
programs␈α∞for␈α
two␈α
simpler␈α∞but␈α
related
␈↓ ↓H␈↓functions␈α
in␈α
order␈α∞to␈α
better␈α
understand␈α∞various␈α
aspects␈α
of␈α∞the␈α
control␈α
structure␈α∞and␈α
information
␈↓ ↓H␈↓flow␈αneeded␈αin␈αthe␈αmain␈αcomputation.␈α This␈α
is␈αa␈αuseful␈αtechnique␈αin␈αproblem␈αsolving␈α
in␈αgeneral,
␈↓ ↓H␈↓the␈α
trick␈α
of␈α
course␈α
is␈α
to␈α
find␈α
a␈α
simplification␈α
which␈α
produces␈α
a␈α
problem␈α
that␈α
you␈α
can␈α
solve␈α
and
␈↓ ↓H␈↓whose␈α⊂solution␈α∂is␈α⊂useful␈α⊂in␈α∂solving␈α⊂the␈α∂original␈α⊂problem.␈α⊂  Having␈α∂written␈α⊂programs␈α⊂for␈α∂these
␈↓ ↓H␈↓simpler functions, we then use the insight gained to to solve the main problem.

␈↓ ↓H␈↓        The␈αproblem␈αhas␈αto␈α
do␈αwith␈α"lists␈αof␈αlists"␈α
or␈αL-lists.␈α An␈αL-list␈α
is␈αeither␈αthe␈αempty␈αlist,␈α
␈↓¬NIL␈↓,
␈↓ ↓H␈↓or␈α∞and␈α
atom␈α∞␈↓↓cons␈↓ed␈α
onto␈α∞the␈α
front␈α∞of␈α
an␈α∞L-list,␈α
or␈α∞an␈α
L-list␈α∞␈↓↓cons␈↓ed␈α
onto␈α∞the␈α
front␈α∞of␈α∞an␈α
L-list.
␈↓ ↓H␈↓Thus␈α∀␈↓¬(A (A B) C)␈↓␈α∃is␈α∀and␈α∃L-list,␈α∀but␈α∃␈↓¬(A (B . C) D)␈↓␈α∀is␈α∃not.␈α∀  Although␈α∃this␈α∀class␈α∃of␈α∀S-
␈↓ ↓H␈↓expressions␈α∃is␈α∃less␈α∃general␈α∃than␈α∃the␈α∃usual␈α∃LISP␈α∃notion␈α∃of␈α∃list,␈α∃it␈α∃is␈α∃more␈α∃uniform.␈α∃ The
␈↓ ↓H␈↓computation␈α
of␈α
a␈α
function␈α
on␈α
L-lists␈α
may␈α
use␈α
the␈α
value␈α
of␈α
the␈α
function␈α
on␈α
the␈α
␈↓↓car␈↓␈α
of␈α∞the␈α
L-list
␈↓ ↓H␈↓(when it is non-atomic) as well as on the ␈↓↓cdr␈↓ as both are again L-lists.
␈↓ ↓H␈↓40␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓        The␈α
function␈α∞␈↓↓allsubsub␈↓␈α
returns␈α
a␈α∞list␈α
of␈α∞all␈α
occurrences␈α
of␈α∞an␈α
L-list␈α∞␈↓↓u␈↓␈α
as␈α
a␈α∞sublist␈α
or␈α∞as␈α
a
␈↓ ↓H␈↓sublist␈αof␈αa␈αsublist␈αof␈αan␈αL-list␈α␈↓↓v␈↓␈α.␈α For␈αexample␈α␈↓¬(A␈αA)␈↓␈αoccurs␈αthree␈αtimes␈αas␈αa␈αsubsublist␈αof␈α␈↓¬(A␈αA
␈↓ ↓H␈↓¬A B A A)␈↓ and ␈↓¬(A)␈↓ occurs 4 times in ␈↓¬(A (B (A A)) (C (((A)))))␈↓.

␈↓ ↓H␈↓        Suppose␈α
we␈α
restrict␈α
the␈α
function␈α
to␈α
lists␈α∞of␈α
atoms.␈α
 Thus␈α
we␈α
wish␈α
to␈α
compute␈α∞the␈α
function
␈↓ ↓H␈↓␈↓↓allsub␈↓␈αthat␈αreturns␈αa␈αlist␈αof␈αall␈αoccurrences␈αof␈αa␈α
list␈αof␈αatoms␈α␈↓↓u␈↓␈αas␈αa␈αsublist␈αof␈αanother␈αlist␈αof␈α
atoms
␈↓ ↓H␈↓␈↓↓v.␈↓␈α
 The␈αfirst␈α
thing␈αto␈α
do␈α
is␈αdecide␈α
on␈αthe␈α
representation␈α
of␈αan␈α
occurrence␈αof␈α
one␈α
list␈αas␈α
a␈αsublist␈α
of
␈↓ ↓H␈↓another␈αlist.␈α In␈αthe␈αcase␈αof␈αlists␈αof␈αatoms,␈αa␈αfairly␈αnatural␈αsolution␈αis␈αto␈αrepresent␈αan␈αoccurrence␈α
of
␈↓ ↓H␈↓a␈αparticular␈αsublist␈αas␈αthe␈αnumber␈α␈↓↓n␈↓␈αcorresponding␈αto␈αposition␈αin␈αthe␈αlist␈αof␈αthe␈αbeginning␈αof␈αthat
␈↓ ↓H␈↓occurrence.  Thus

␈↓ ↓H␈↓␈↓ ∧0␈↓↓allsub[␈↓¬(A A),(A A A B A A)␈↓↓] = ␈↓¬(1 2 5)␈↓↓.␈↓ 

␈↓ ↓H␈↓        To␈α
compute␈α
␈↓↓allsub[u,v],␈↓␈α∞if␈α
␈↓↓v␈↓␈α
is␈α
␈↓¬NIL␈↓␈α∞then␈α
the␈α
answer␈α
is␈α∞␈↓¬NIL␈↓.␈α
 Otherwise,␈α
imagine␈α∞that␈α
we
␈↓ ↓H␈↓know␈αthe␈αanswer␈αfor␈α␈↓↓␈↓αd|␈↓↓v␈↓.␈α Then␈αthere␈αare␈αtwo␈αpossiblities.␈α Either␈α␈↓↓u␈↓␈α"matches"␈α␈↓↓v␈↓␈α(␈↓↓v␈↓␈αmay␈αbe␈αlonger
␈↓ ↓H␈↓than␈α
␈↓↓u,␈↓␈α
but␈α
up␈α
to␈α
the␈α
end␈α
of␈α
␈↓↓u␈↓␈α
the␈α
elements␈αof␈α
␈↓↓v␈↓␈α
and␈α
␈↓↓u␈↓␈α
are␈α
the␈α
same),␈α
or␈α
not.␈α
 If␈α
not,␈α
the␈αanswer␈α
is
␈↓ ↓H␈↓just␈α
the␈α∞result␈α
for␈α∞␈↓↓␈↓αd|␈↓↓v␈↓␈α
otherwise␈α∞we␈α
add␈α∞the␈α
current␈α∞position␈α
to␈α∞the␈α
result␈α∞for␈α
␈↓↓␈↓αd|␈↓↓v␈↓.␈α∞ This␈α
analysis
␈↓ ↓H␈↓suggests␈αthat␈αwe␈αwill␈αneed␈αan␈αadditional␈αargument␈αto␈αkeep␈αtrack␈αof␈αthe␈αcurrent␈αposition.␈α Thus␈αwe
␈↓ ↓H␈↓define␈αa␈αfunction␈α␈↓↓allsub1[u,v,p]␈↓␈αwhere␈αthe␈αargument␈α␈↓↓p␈↓␈αis␈αintended␈αto␈αcorrespond␈αto␈αthe␈αposition
␈↓ ↓H␈↓of␈α∞the␈α∞argument␈α
␈↓↓v␈↓␈α∞in␈α∞the␈α∞initial␈α
list.␈α∞ If␈α∞␈↓↓v␈↓␈α∞is␈α
␈↓¬NIL␈↓␈α∞then␈α∞the␈α∞result␈α
is␈α∞␈↓¬NIL␈↓.␈α∞ Otherwise␈α∞suppose␈α
we
␈↓ ↓H␈↓know␈α∀the␈α∀value␈α∀of␈α∀␈↓↓allsub1[u,␈↓αd|␈↓↓v,p+1]␈↓␈α∪then␈α∀we␈α∀either␈α∀add␈α∀␈↓↓p␈↓␈α∪onto␈α∀that␈α∀value␈α∀or␈α∀return␈α∪it
␈↓ ↓H␈↓unchanged␈αaccording␈αto␈αwhether␈αor␈αnot␈α␈↓↓u␈↓␈αmatches␈α␈↓↓v.␈↓␈α Assuming␈αwe␈αhave␈αa␈αsuitable␈αdefinition␈αof
␈↓ ↓H␈↓␈↓↓match,␈↓ the above analysis leads to the following definitions:

␈↓ ↓H␈↓␈↓ αx␈↓↓allsub[u, v] ← allsub1[u, v, 1]␈↓
␈↓ ↓H␈↓7.1)
␈↓ ↓H␈↓␈↓ αx␈↓↓allsub1[u, v, p] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ match[u, v] ␈↓αthen␈↓↓ p . allsub1[u, ␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ p]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ allsub1[u, ␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ p]␈↓.

␈↓ ↓H␈↓        It␈αremains␈α
for␈αus␈α
to␈αwrite␈αthe␈α
definition␈αof␈α
␈↓↓match[u,v]␈↓␈αwhich␈αdetermines␈α
whether␈αor␈α
not␈α␈↓↓u␈↓
␈↓ ↓H␈↓matches␈αthe␈αbeginning␈αof␈α␈↓↓v.␈↓␈α This␈αis␈αeasy.␈α If␈α␈↓↓u␈↓␈αis␈α␈↓¬NIL␈↓␈αthen␈αwe␈αhave␈αgotten␈αto␈αthe␈αend␈αof␈α␈↓↓u␈↓␈αwith
␈↓ ↓H␈↓out␈αfinding␈αa␈α
mismatch␈αso␈αwe␈α
return␈α␈↓¬T␈↓.␈α  If␈α
␈↓↓v␈↓␈αis␈α␈↓¬NIL␈↓␈α
(and␈α␈↓↓u␈↓␈αis␈α
not)␈αthen␈αwe␈α
return␈α␈↓¬NIL␈↓␈αand␈α␈↓↓u␈↓␈α
does
␈↓ ↓H␈↓not␈αmatch␈αthe␈αbeginning␈αof␈α␈↓↓v.␈↓␈α Otherwise␈αthe␈αresult␈αis␈α␈↓¬T␈↓␈αonly␈αif␈α␈↓↓␈↓αa|␈↓↓u = ␈↓αa|␈↓↓v␈↓␈αand␈αthe␈α␈↓↓cdr␈↓s␈αof␈α␈↓↓u␈↓␈αand␈α␈↓↓v␈↓
␈↓ ↓H␈↓match.  Thus

␈↓ ↓H␈↓7.2)␈↓ α;␈↓↓match[u, v] ←   ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬T␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u = ␈↓αa|␈↓↓v ∧ match[␈↓αd|␈↓↓u, ␈↓αd|␈↓↓v]␈↓. 

␈↓ ↓H␈↓The internal forms of the above definitions are

␈↓ ↓H␈↓¬        (DEFUN ALLSUB (U V) (ALLSUB1 U V 1))

␈↓ ↓H␈↓¬        (DEFUN ALLSUB1 (U V P)
␈↓ ↓H␈↓¬               (COND ((NULL V) NIL)
␈↓ ↓H␈↓¬                     ((MATCH U V) (CONS P (ALLSUB1 U (CDR V) (ADD1 P))))
␈↓ ↓H␈↓¬                     (T (ALLSUB1 U (CDR V) (ADD1 P)))))
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *41


␈↓ ↓H␈↓¬        (DEFUN MATCH (U V)
␈↓ ↓H␈↓¬               (COND ((NULL U) T)
␈↓ ↓H␈↓¬                     ((NULL V) NIL)
␈↓ ↓H␈↓¬                     (T (AND (EQUAL (CAR U) (CAR V))
␈↓ ↓H␈↓¬                             (MATCH (CDR U) (CDR V))))))

␈↓ ↓H␈↓        Now␈αwe␈α
return␈αto␈αthe␈α
␈↓↓allsubsub␈↓␈αproblem.␈αAs␈α
with␈αthe␈α
simpler␈αcase,␈αwe␈α
must␈αfirst␈αdecide␈α
how
␈↓ ↓H␈↓to␈αrepresent␈αan␈αoccurrence␈αof␈αan␈αL-list␈αas␈α
a␈αsubsublist␈αof␈αan␈αL-list.␈α Again␈αwe␈αneed␈αonly␈α
represent
␈↓ ↓H␈↓the␈αposition␈αwhere␈αthe␈αlist␈αmatch␈αbegins.␈α Consider␈αan␈αarbitrary␈αpoint,␈α␈↓↓p,␈↓␈α in␈αan␈αL-list.␈α Either␈αit
␈↓ ↓H␈↓is␈α
an␈α
element␈α
of␈α
the␈α
list,␈α∞or␈α
is␈α
contained␈α
in␈α
one␈α
of␈α
the␈α∞elements␈α
of␈α
the␈α
list.␈α
 In␈α
the␈α
first␈α∞case␈α
the
␈↓ ↓H␈↓position␈α
␈↓↓n␈↓␈α
of␈α
the␈α
element␈α
is␈α
sufficient,␈α
in␈α
the␈α
second␈α
case␈α
we␈α
need␈α
the␈α
position␈α
␈↓↓n␈↓␈α
of␈α∞the␈α
element
␈↓ ↓H␈↓together␈αwith␈αthe␈αposition␈αof␈αthe␈αpoint␈α␈↓↓p␈↓␈αrelative␈αto␈αthat␈αelement.␈α Thus␈αin␈αthe␈αfirst␈αcase␈α
we␈αtake
␈↓ ↓H␈↓the␈αlist␈αcontaining␈αonly␈αthe␈αnumber␈α␈↓↓n␈↓␈αand␈αin␈αthe␈αsecond␈αcase␈αwe␈αadd␈α␈↓↓n␈↓␈αonto␈αthe␈αlist␈αrepresenting
␈↓ ↓H␈↓the position of ␈↓↓p␈↓ in the ␈↓↓n␈↓th element of the list.  Using this representation we have

␈↓ ↓H␈↓␈↓ ↓z␈↓↓allsubsub[␈↓¬(A),(A (B (A A)) (C (((A)))))␈↓↓] = ␈↓¬((1) (2 2 1) (2 2 2) (3 2 1 1 1))␈↓↓␈↓ . 

␈↓ ↓H␈↓        To␈α
construct␈α∞the␈α
desired␈α∞list␈α
of␈α
positions␈α∞we␈α
check␈α∞for␈α
a␈α
match␈α∞of␈α
␈↓↓u␈↓␈α∞at␈α
each␈α∞position␈α
and
␈↓ ↓H␈↓add␈αmatching␈αpositions␈α
to␈αthe␈αresult.␈α
  (Actually␈αwe␈αwill␈α
see␈αthat␈αsome␈α
positions␈αcan␈αbe␈α
ruled␈αout
␈↓ ↓H␈↓with␈α∞out␈α∞explicitly␈α∞checking␈α∞for␈α∞a␈α∞match.)␈α
 In␈α∞particular,␈α∞we␈α∞will␈α∞need␈α∞to␈α∞pass␈α∞around␈α
sufficient
␈↓ ↓H␈↓information␈α
to␈αbe␈α
able␈αto␈α
construct␈αa␈α
representation␈αof␈α
the␈αcurrent␈α
position␈αwhenever␈α
a␈α
match␈αis
␈↓ ↓H␈↓found.␈α Let␈αus␈α
consider␈αthe␈αsimpler␈αproblem␈α
of␈αcomputing␈α␈↓↓allpos[v],␈↓␈αa␈α
list␈αof␈αall␈αthe␈α
positions␈αin
␈↓ ↓H␈↓␈↓↓v.␈↓␈α
 A␈αprogram␈α
for␈αthis␈α
 can␈αeasily␈α
be␈αmodified␈α
to␈αlist␈α
only␈αthose␈α
positions␈αsatisfying␈α
the␈α"match"
␈↓ ↓H␈↓condition.␈α∂ A␈α⊂definition␈α∂of␈α∂␈↓↓allpos␈↓␈α⊂can␈α∂be␈α∂obtained␈α⊂by␈α∂recalling␈α∂the␈α⊂discussion␈α∂about␈α∂of␈α⊂how␈α∂a
␈↓ ↓H␈↓position␈αis␈αto␈αbe␈αrepresented.␈α Namely␈αif␈α␈↓↓n␈↓␈αis␈αthe␈αposition␈αof␈α␈↓↓v␈↓␈αin␈αthe␈αlist␈αof␈αwhich␈α␈↓↓v␈↓␈αis␈αa␈αtail␈αthen
␈↓ ↓H␈↓(i)␈αif␈α␈↓↓v␈↓␈αis␈α␈↓¬NIL␈↓␈αthen␈αthere␈αare␈αno␈αpositions␈αand␈αthe␈αanswer␈αis␈α␈↓¬NIL␈↓,␈α (ii)␈α␈↓↓␈↓αa|␈↓↓v␈↓␈αis␈αan␈αatom␈αthen␈αadd␈α␈↓↓<n>␈↓
␈↓ ↓H␈↓to␈α
the␈α
list␈αof␈α
positions␈α
in␈α␈↓↓␈↓αd|␈↓↓v␈↓␈α
passing␈α
␈↓↓n+1␈↓␈αas␈α
the␈α
current␈αposition␈α
number␈α
(iii)␈α
otherwise␈αcompute
␈↓ ↓H␈↓the␈αlist␈αof␈αpositions␈αin␈α␈↓↓␈↓αa|␈↓↓v␈↓␈α(relative␈αto␈α␈↓↓␈↓αa|␈↓↓v␈↓),␈αtack␈α␈↓↓n␈↓␈αonto␈αthe␈αfront␈αof␈αeach␈αposition,␈αappend␈αthis␈α
onto
␈↓ ↓H␈↓the␈α
list␈α
of␈α
positions␈α
in␈α
␈↓↓␈↓αd|␈↓↓v␈↓␈α
(again␈α
passing␈α
␈↓↓n+1)␈↓␈α
and␈α
finally␈α
add␈α
the␈α
current␈α
position,␈α
␈↓↓<n>,␈↓␈α
to␈α
the
␈↓ ↓H␈↓result.   Thus we have

␈↓ ↓H␈↓␈↓ αx␈↓↓allpos v ← allpos1[v, 1]␈↓
␈↓ ↓H␈↓7.3)
␈↓ ↓H␈↓␈↓ αx␈↓↓allpos1[v, n] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓v ␈↓αthen␈↓↓ <n> . allpos1[␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ n]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ <n> . [tack[n, allpos ␈↓αa|␈↓↓v] * allpos1[␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ n]]␈↓

␈↓ ↓H␈↓7.4)␈↓ αx␈↓↓tack[n, w] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [n . ␈↓αa|␈↓↓w] . tack[n, ␈↓αd|␈↓↓w]␈↓

␈↓ ↓H␈↓Now␈α
we␈α
modify␈α
to␈α
␈↓↓allpos␈↓␈α
and␈α
␈↓↓allpos1␈↓␈α
to␈α
carry␈α
around␈α
the␈α
parameter␈α
␈↓↓u␈↓␈α
and␈α
rule␈α
out␈α
those␈α
positions
␈↓ ↓H␈↓that␈αdon't␈αmatch.␈α In␈αparticular␈αwe␈αonly␈αadd␈α␈↓↓<n>␈↓␈αto␈αthe␈αlist␈αif␈α␈↓↓match[u,v]␈↓␈αis␈αsatisfied,␈αand␈αin␈αthis
␈↓ ↓H␈↓case␈αwe␈αdon't␈αlook␈αfor␈αa␈αmatch␈αin␈α␈↓↓␈↓αa|␈↓↓v␈↓␈αas␈αthe␈αtop␈αlevel␈αmatch␈αmakes␈αthis␈αimpossible␈α(recall␈αour␈αlists
␈↓ ↓H␈↓are finite tree-like structures).  So we arrive at the following:
␈↓ ↓H␈↓42␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓␈↓ αx␈↓↓allsubsub[u, v] ← allsubsub1[u, v, 1]␈↓
␈↓ ↓H␈↓7.5)
␈↓ ↓H␈↓␈↓ αx␈↓↓allsubsub1[u, v, n] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ match[u, v] ␈↓αthen␈↓↓ <n> . allsubsub1[u, ␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ n]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓v ␈↓αthen␈↓↓ allsubsub1[u, ␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ n]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ tack[n, allsubsub[u, ␈↓αa|␈↓↓v]] * allsubsub1[u, ␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ n]␈↓

␈↓ ↓H␈↓An␈α⊃alternate␈α⊃solution␈α∩for␈α⊃the␈α⊃␈↓↓allpos␈↓␈α⊃and␈α∩the␈α⊃␈↓↓allsubsub␈↓␈α⊃problems␈α⊃is␈α∩to␈α⊃pass␈α⊃along␈α∩a␈α⊃complete
␈↓ ↓H␈↓representation␈αof␈αthe␈αcurrent␈αposition.␈α When␈αwe␈αpass␈α
it␈αin␈αthe␈α␈↓↓␈↓αd|␈↓↓v␈↓␈αdirection␈αwe␈αneed␈αto␈αadd␈α
1␈αto
␈↓ ↓H␈↓the␈αlast␈αelement␈α
and␈αwhen␈αwe␈αpass␈α
it␈αin␈αthe␈α␈↓↓␈↓αa|␈↓↓v␈↓␈α
direction␈αwe␈αneed␈αto␈α
add␈αa␈α1␈αto␈α
the␈αend␈αof␈αthe␈α
list.
␈↓ ↓H␈↓Since␈αall␈αof␈αthe␈αaction␈αis␈αat␈αthe␈αend␈αof␈αthe␈αlist␈αit␈αis␈αeasier␈αto␈αpass␈αalong␈αthe␈αreverse␈αof␈αthe␈αposition
␈↓ ↓H␈↓list␈α⊃and␈α⊃re-reverse␈α⊃it␈α⊃when␈α⊂returning␈α⊃the␈α⊃position␈α⊃as␈α⊃an␈α⊂answer.␈α⊃ Thus␈α⊃we␈α⊃get␈α⊃the␈α⊂following
␈↓ ↓H␈↓definitions

␈↓ ↓H␈↓␈↓ αx␈↓↓allpos v ← allpos1[v, ␈↓¬(1)␈↓↓]␈↓
␈↓ ↓H␈↓7.6)
␈↓ ↓H␈↓␈↓ αx␈↓↓allpos1[v, p] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓v ␈↓αthen␈↓↓ reverse p . allpos1[␈↓αd|␈↓↓v, [␈↓¬1 ␈↓↓+ ␈↓αa|␈↓↓p] . ␈↓αd|␈↓↓p]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ reverse p . [allpos1[␈↓αa|␈↓↓v, 1 . p]] * allpos1[␈↓αd|␈↓↓v, [␈↓¬1 ␈↓↓+ ␈↓αa|␈↓↓p] . ␈↓αd|␈↓↓p]]␈↓

␈↓ ↓H␈↓␈↓ αx␈↓↓allsubsub[u, v] ← allsubsub1[u, v, ␈↓¬(1)␈↓↓]␈↓
␈↓ ↓H␈↓7.7)
␈↓ ↓H␈↓␈↓ αx␈↓↓allsubsub1[u, v, p] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ match[u, v] ␈↓αthen␈↓↓ reverse p . allsubsub1[u, ␈↓αd|␈↓↓v, [␈↓¬1 ␈↓↓+ ␈↓αa|␈↓↓p] . ␈↓αd|␈↓↓p]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓v ␈↓αthen␈↓↓ allsubsub1[u, ␈↓αd|␈↓↓v, [␈↓¬1 ␈↓↓+ ␈↓αa|␈↓↓p] . ␈↓αd|␈↓↓p]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ allsubsub1[u, ␈↓αa|␈↓↓v, ␈↓¬1 ␈↓↓. p]] * allsubsub1[u, ␈↓αd|␈↓↓v, [␈↓¬1 ␈↓↓+ ␈↓αa|␈↓↓p] . ␈↓αd|␈↓↓p]␈↓

␈↓ ↓H␈↓Notice␈αthat␈αin␈αthe␈αfirst␈αsolution,␈αthe␈α
construction␈αof␈αthe␈αposition␈αrepresentation␈αwas␈αtaken␈α
care␈αof
␈↓ ↓H␈↓somewhat␈α∞automatically␈α∞by␈α∞the␈α∞recursive␈α∞structure␈α∂of␈α∞the␈α∞program,␈α∞while␈α∞in␈α∞the␈α∞second␈α∂case␈α∞we
␈↓ ↓H␈↓had to worry about the details of constructing the position representations.

␈↓ ↓H␈↓In␈αboth␈α
cases␈αthe␈αcompution␈α
works␈αby␈α
passing␈αinformation␈αboth␈α
down␈αand␈α
across␈αthe␈αlist␈α
structure
␈↓ ↓H␈↓and␈αgetting␈αresults␈αback.␈α What␈α
is␈αpassed␈αon␈αand␈αthe␈α
interpretation␈αof␈αthe␈αresult␈αreturned␈α
depends
␈↓ ↓H␈↓on␈α≠the␈α≠direction.␈α≠In␈α~this␈α≠example␈α≠there␈α≠is␈α~no␈α≠exchange␈α≠of␈α≠information␈α≠between␈α~the
␈↓ ↓H␈↓subcomputations.  In a more complicted situation this might also be necessary.
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *43


␈↓ ↓H␈↓8.  ␈↓αLots of LISP functions to program.␈↓


␈↓ ↓H␈↓        The␈α∞following␈α∞are␈α∞descriptions␈α
of␈α∞functions␈α∞on␈α∞lists␈α
and␈α∞S-expressions.␈α∞ You␈α∞should␈α
write
␈↓ ↓H␈↓LISP␈α⊂programs␈α⊂to␈α⊂compute␈α⊂these␈α∂functions.␈α⊂ A␈α⊂description␈α⊂of␈α⊂the␈α∂form␈α⊂ "␈↓↓foo[x]␈↓␈α⊂is␈α⊂true␈α⊂if␈α∂and
␈↓ ↓H␈↓only if ..."␈α means␈αyour␈αprogram␈αshould␈αreturn␈α␈↓¬T␈↓␈αin␈αthe␈αcase␈αthat␈α␈↓↓foo[x]␈↓␈αis␈αtrue␈αand␈α␈↓¬NIL␈↓␈αotherwise.
␈↓ ↓H␈↓Write␈αyour␈αsolutions␈αout␈αin␈αexternal␈αform,␈αif␈αyou␈αlike,␈αthen␈αconvert␈αthem␈αto␈αinternal␈αform␈αand␈αtry
␈↓ ↓H␈↓them␈α
out␈α∞in␈α
whatever␈α∞LISP␈α
system␈α
you␈α∞have␈α
access␈α∞to.␈α
  You␈α
should␈α∞convince␈α
yourself␈α∞by␈α
some
␈↓ ↓H␈↓informal␈αprocess␈αof␈αreasoning␈αthat␈αyour␈αsolutions␈αare␈αindeed␈αcorrect.␈α You␈αwill␈αbe␈αasked␈αto␈αprove
␈↓ ↓H␈↓various␈α∞facts␈α∞about␈α∞your␈α∞programs␈α∞after␈α∞you␈α∞have␈α∞studied␈α∞Chapter␈α∞III.␈α∞ This␈α∞should␈α
encourage
␈↓ ↓H␈↓you␈α
to␈αwrite␈α
clean,␈αunderstandable␈α
programs␈αand␈α
document␈αthem␈α
carefully␈αso␈α
you␈α
will␈αremember
␈↓ ↓H␈↓why you thought they were correct to begin with and what tricks you may have employed.

␈↓ ↓H␈↓        The␈α⊃problems␈α⊃are␈α∩classified␈α⊃according␈α⊃to␈α∩the␈α⊃intended␈α⊃interpretation␈α∩of␈α⊃the␈α⊃lists␈α∩or␈α⊃S-
␈↓ ↓H␈↓expressions.␈α∞ The␈α∞classes␈α∞include␈α∞lists,␈α∞S-expressions,␈α∞arithmetic␈α∞expressions,␈α∞polynomials,␈α∞(finite)
␈↓ ↓H␈↓sets,␈α∞permutations,␈α∞trees,␈α∂and␈α∞graphs.␈α∞ Each␈α∂group␈α∞begins␈α∞with␈α∂some␈α∞simple␈α∞problems␈α∂(with␈α∞one
␈↓ ↓H␈↓line␈α
solutions)␈αto␈α
get␈α
you␈αgoing.␈α
 For␈αthe␈α
more␈α
complicated␈αfunctions,␈α
you␈αwill␈α
find␈α
that␈αdefining
␈↓ ↓H␈↓auxiliary␈α∂functions␈α⊂is␈α∂useful.␈α⊂ Towards␈α∂the␈α⊂end␈α∂the␈α∂programs␈α⊂may␈α∂require␈α⊂a␈α∂fair␈α⊂ammount␈α∂of
␈↓ ↓H␈↓thinking␈α∪to␈α∪get␈α∪things␈α∪organized␈α∪correctly,␈α∪but␈α∪none␈α∪of␈α∪the␈α∪problems␈α∪require␈α∪really␈α∩lengthy
␈↓ ↓H␈↓programs.␈α Pay␈αattention␈αto␈αgetting␈αthe␈αtrivial␈αcases␈α(e.␈αg.␈αwhen␈αsomg␈αof␈αthe␈αarguments␈αare␈α␈↓¬NIL␈↓␈αor
␈↓ ↓H␈↓atomic␈α
or␈αotherwise␈α
to␈α
be␈αconsidered␈α
basic)␈αcorrect.␈α
 It␈α
is␈αin␈α
general␈αimportant␈α
to␈α
understand␈αthe
␈↓ ↓H␈↓trivial cases in the computation of a function.

␈↓ ↓H␈↓        The␈αobservant␈αreader␈αwill␈αno␈αdoubt␈αnotice␈αthat␈αsome␈αof␈αthese␈αfunctions␈αare␈αdefined␈αin␈αlater
␈↓ ↓H␈↓chapters.␈α∞ Thus␈α∞you␈α∞will␈α∞have␈α
some␈α∞cases␈α∞where␈α∞you␈α∞can␈α
compare␈α∞your␈α∞solution␈α∞to␈α∞those␈α
given.
␈↓ ↓H␈↓(Pointers to these definitions can be found in the function index.)

␈↓ ↓H␈↓α␈↓ ε(Lists

␈↓ ↓H␈↓1. ␈↓↓samelength[u,v]␈↓ tests whether the lists ␈↓↓u␈↓ and ␈↓↓v␈↓ have the same length.

␈↓ ↓H␈↓␈↓ ∧]␈↓↓samelength[␈↓¬(A B C), (D E F)␈↓↓] = ␈↓¬T␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧]␈↓↓samelength[␈↓¬(A B C), (A C)␈↓↓] = ␈↓¬NIL␈↓↓␈↓

␈↓ ↓H␈↓    Do not use any operations or test on numbers to write your program.

␈↓ ↓H␈↓2. ␈↓↓prup[u,v]␈↓ is the list formed by pairing the corresponding elements of the lists ␈↓↓u␈↓ and ␈↓↓v.␈↓  Thus

␈↓ ↓H␈↓␈↓ α`␈↓↓prup[␈↓¬(X Y Z), (1 A (FOO BAR))␈↓↓] = ␈↓¬((X . 1) (Y . A) (Z FOO BAR))␈↓↓␈↓. 

␈↓ ↓H␈↓3.␈α
␈↓↓istail[u,v]␈↓␈α
is␈αtrue␈α
if␈α
and␈α
only␈αif␈α
the␈α
list␈α
␈↓↓u␈↓␈αis␈α
a␈α
tail␈α
of␈α␈↓↓v.␈↓␈α
 That␈α
is␈α
when␈α␈↓↓cdr␈↓ing␈α
through␈α
␈↓↓v␈↓␈αyou␈α
will
␈↓ ↓H␈↓␈↓ ↓xeventually get to ␈↓↓u.␈↓

␈↓ ↓H␈↓4.␈α∞␈↓↓commontail[u,v]␈↓␈α∂is␈α∞the␈α∂longest␈α∞common␈α∂sublist␈α∞of␈α∂␈↓↓u␈↓␈α∞and␈α∂␈↓↓v␈↓␈α∞ending␈α∂with␈α∞the␈α∂ends␈α∞of␈α∂the␈α∞lists.
␈↓ ↓H␈↓␈↓ ↓xThus

␈↓ ↓H␈↓␈↓ ∧β␈↓↓commontail[␈↓¬(A B C D E),(A C D E)␈↓↓] = ␈↓¬(C D E)␈↓↓␈↓. 

␈↓ ↓H␈↓5.␈α∞ ␈↓↓upto[u,v]␈↓␈α∞when␈α
␈↓↓u␈↓␈α∞is␈α∞a␈α
tail␈α∞of␈α∞␈↓↓v,␈↓␈α
is␈α∞the␈α∞list␈α
of␈α∞elements␈α∞of␈α
␈↓↓v␈↓␈α∞upto␈α∞the␈α
point␈α∞where␈α∞␈↓↓u␈↓␈α
begins.
␈↓ ↓H␈↓␈↓ ↓xThus
␈↓ ↓H␈↓44␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓␈↓ ∧L␈↓↓upto[␈↓¬(C D E),(A B C D E)␈↓↓] = ␈↓¬(A B)␈↓↓␈↓. 

␈↓ ↓H␈↓6. ␈↓↓mapapp[f,u]␈↓ appends together the lists obtained by applying ␈↓↓f␈↓ to successive elements of ␈↓↓u.␈↓

␈↓ ↓H␈↓7. ␈↓↓mapchoose[p,u]␈↓ forms a list of those elements of ␈↓↓u␈↓ satisfying ␈↓↓p.␈↓

␈↓ ↓H␈↓8.␈α∪␈↓↓partition[u,n]␈↓␈α∪is␈α∪the␈α∪list␈α∪of␈α∪partitions␈α∪of␈α∪the␈α∩list␈α∪␈↓↓u␈↓␈α∪into␈α∪␈↓↓n␈↓␈α∪sublists␈α∪␈↓↓u1,␈↓␈α∪...␈α∪␈↓↓un␈↓␈α∪such␈α∩that
␈↓ ↓H␈↓␈↓ ↓x␈↓↓u1*[u2*...*un]=u␈↓.␈α If␈α␈↓↓n␈↓␈αis␈αgreater␈αthan␈αthe␈α
length␈αof␈α␈↓↓u␈↓␈αthen␈αyour␈αprogram␈αshould␈α
return␈αan
␈↓ ↓H␈↓␈↓ ↓xerror message of some kind.  Thus

␈↓ ↓H␈↓␈↓ β}␈↓↓partition[␈↓¬(A B C),2␈↓↓]=␈↓¬((A) (B C))((A B) (C)))␈↓↓␈↓ 

␈↓ ↓H␈↓   Write␈α
a␈α
program␈α
␈↓↓testpart␈↓␈α
to␈α
test␈α
the␈αresult␈α
returned␈α
by␈α
␈↓↓partition.␈↓␈α
 For␈α
each␈α
partition,␈α␈↓↓testpart␈↓
␈↓ ↓H␈↓␈↓ ↓xshould append together the lists ␈↓↓ui␈↓ and check that the result is ␈↓↓u.␈↓

␈↓ ↓H␈↓α␈↓ ¬nS-expressions

␈↓ ↓H␈↓9. ␈↓↓mirror[x]␈↓ returns the mirror image of an S-expression ␈↓↓x.␈↓  Thus

␈↓ ↓H␈↓␈↓ ∧;␈↓↓mirror[␈↓¬((A.B).(C.D))␈↓↓]=␈↓¬((D.C).(B.A))␈↓↓␈↓. 

␈↓ ↓H␈↓10. ␈↓↓occur[x,y]␈↓ is true if and only if the atom ␈↓↓x␈↓ occurs in the S-expression ␈↓↓y.␈↓  For example

␈↓ ↓H␈↓␈↓ ¬!␈↓↓occur[␈↓¬B, ␈↓↓␈↓¬((A.B).C)␈↓↓] = ␈↓¬T␈↓↓␈↓. 

␈↓ ↓H␈↓11. ␈↓↓multiplicity[x,y]␈↓ is the number of times the atom ␈↓↓x␈↓ occurs in the S-expression ␈↓↓y.␈↓

␈↓ ↓H␈↓        A␈αpath␈αin␈αan␈αS-expression␈α␈↓↓x␈↓␈αis␈αa␈αlist␈αof␈α␈↓¬A␈↓'s␈αand␈α␈↓¬D␈↓'s␈αsuch␈αthat␈αyou␈αcan␈αapply␈αa␈αsuccession␈αof
␈↓ ↓H␈↓␈↓ ↓x␈↓αa␈↓'s␈αor␈α␈↓αd␈↓'s␈αto␈α␈↓↓x␈↓␈α(according␈αto␈αwhether␈αthe␈αnext␈αlist␈αelement␈αis␈α␈↓¬A␈α␈↓or␈α␈↓¬D␈↓)␈α without␈αtrying␈αto␈αapply␈α␈↓αa␈↓
␈↓ ↓H␈↓␈↓ ↓xor␈α␈↓αd␈↓␈αto␈α
an␈αatom.␈α We␈α
say␈αthat␈αthe␈αresulting␈α
subexpression␈αis␈αat␈α
the␈αend␈αof␈α
the␈αpath␈αor␈αthat␈α
the
␈↓ ↓H␈↓␈↓ ↓xpath␈α
leads␈α
to␈α
that␈α
subexpression.␈α
 Thus␈α␈↓¬(A D)␈↓␈α
is␈α
a␈α
path␈α
in␈α
␈↓¬((X . (Y . Z)) . F)␈↓␈α but␈α
␈↓¬(D A)␈↓
␈↓ ↓H␈↓␈↓ ↓xis not.  Also ␈↓¬(Y . Z)␈↓ is at the end of the path ␈↓¬(A D)␈↓ in ␈↓¬((X . (Y . Z)) . F)␈↓.

␈↓ ↓H␈↓12. ␈↓↓ispath[p,x]␈↓ is true if and only if ␈↓↓p␈↓ is a path in ␈↓↓x.␈↓

␈↓ ↓H␈↓13. ␈↓↓depth[x]␈↓ is the length of the longest path leading to an atom in the S-expression ␈↓↓x.␈↓

␈↓ ↓H␈↓␈↓ ∧m␈↓↓depth[␈↓¬(((A . B) . C) . D␈↓↓] = ␈↓¬3␈↓↓␈↓. 

␈↓ ↓H␈↓        We␈α∞say␈α∂that␈α∞an␈α∂S-expression␈α∞is␈α∂balanced␈α∞if␈α∞it␈α∂is␈α∞an␈α∂atom␈α∞or␈α∂if␈α∞␈↓↓depth[␈↓αa|␈↓↓x]␈↓␈α∂and␈α∞␈↓↓depth[␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ ↓xdiffer by at most 1 and ␈↓αa|␈↓␈↓↓x␈↓ and ␈↓αd|␈↓␈↓↓x␈↓ are both balanced.

␈↓ ↓H␈↓14. ␈↓↓balanced[x]␈↓ is true if and only if the S-expression ␈↓↓x␈↓ is balanced.

␈↓ ↓H␈↓15.  ␈↓↓balance␈↓ x is balanced and has the same fringe as x

␈↓ ↓H␈↓16. ␈↓↓get[y,p]␈↓ is the subexpression at the end of the path ␈↓↓p␈↓ in the S-expression ␈↓↓y.␈↓  Thus

␈↓ ↓H␈↓␈↓ ∧>␈↓↓get[␈↓¬(A ((B) C) (B)), (D A A)␈↓↓] = ␈↓¬(B)␈↓↓␈↓. 
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *45


␈↓ ↓H␈↓17.␈α∂␈↓↓point[x,y]␈↓␈α∂is␈α⊂the␈α∂path␈α∂in␈α⊂the␈α∂S-expression␈α∂␈↓↓y␈↓␈α⊂leading␈α∂to␈α∂the␈α⊂left-most␈α∂occurrence␈α∂of␈α⊂the␈α∂S-
␈↓ ↓H␈↓␈↓ ↓xexpression ␈↓↓x␈↓ in the S-expression ␈↓↓y.␈↓ Thus

␈↓ ↓H␈↓␈↓ ∧0␈↓↓point[␈↓¬(B), (A ((B) C) (B))␈↓↓] = ␈↓¬(D A A)␈↓↓␈↓. 

␈↓ ↓H␈↓18. ␈↓↓allpoint[x,y]␈↓ is a list of all paths ␈↓↓p␈↓ such that ␈↓↓get[y,p] = x␈↓.  Thus

␈↓ ↓H␈↓␈↓ βO␈↓↓allpoint[␈↓¬(B), (A ((B) C) (B))␈↓↓] = ␈↓¬((D A A) (D D A))␈↓↓␈↓. 

␈↓ ↓H␈↓19.␈α␈↓↓commons␈αx␈↓␈αis␈α
a␈αlist␈αof␈αthe␈α
subexpressions␈αof␈αthe␈αS-expression␈α␈↓↓x␈↓␈α
that␈αoccur␈αmore␈αthan␈α
once␈αin
␈↓ ↓H␈↓␈↓ ↓x␈↓↓x,␈↓ each followed by a list of the points (paths leading to the points) where it occurs.

␈↓ ↓H␈↓α␈↓ ∧jSymbolic arithmetic and algebra

␈↓ ↓H␈↓20.␈αLet␈αa␈αpolynomial␈αin␈α␈↓↓x␈↓␈αbe␈αrepresented␈αby␈αa␈αlist␈αof␈αits␈αcoefficients␈αin␈αorder␈αof␈αascending␈αpowers
␈↓ ↓H␈↓␈↓ ↓xof ␈↓↓x.␈↓  Thus ␈↓↓x␈↓∧3␈↓↓+x+5␈↓ is represented by ␈↓¬(5 1 0 1)␈↓.

␈↓ ↓H␈↓␈↓ αH        20.1. ␈↓↓sum[u,v]␈↓  is the sum
␈↓ ↓H␈↓␈↓ αH        20.2. ␈↓↓prod[u,v]␈↓  is the product
␈↓ ↓H␈↓␈↓ αH        20.3. ␈↓↓quot[u,v]␈↓ is the quotient
␈↓ ↓H␈↓␈↓ αH        20.4. ␈↓↓rem[u,v]␈↓ is the remainder

␈↓ ↓H␈↓    of the polynomials ␈↓↓u␈↓ and ␈↓↓v␈↓ in the same notation.

␈↓ ↓H␈↓21.  Consider arithmetic expressions as represented in this chapter.  Namely an expression is

␈↓ ↓H␈↓␈↓ αH        (i) a number (satisfies ␈↓↓numberp),␈↓
␈↓ ↓H␈↓␈↓ αH        (ii) a variable (not a number and satisfies ␈↓αat␈↓),
␈↓ ↓H␈↓␈↓ αH        (iii) a sum : ␈↓¬PLUS ␈↓. < list of expressions > or
␈↓ ↓H␈↓␈↓ αH        (iv) a product : ␈↓¬TIMES ␈↓. < list of expressions >.

␈↓ ↓H␈↓    (For simplicity, assume the sum and product lists always have at least 2 elements.)

␈↓ ↓H␈↓        The␈α∂function␈α∂␈↓↓sop␈↓␈α∂converts␈α∂such␈α∂expressions␈α∂into␈α∂sum␈α∂of␈α∂products␈α∂form,␈α∂eg.␈α⊂the␈α∂resulting
␈↓ ↓H␈↓␈↓ ↓xexpression␈α↔is␈α↔either␈α↔a␈α↔monomial␈α↔or␈α↔a␈α↔sum␈α↔of␈α↔monomial␈α↔terms␈α↔which␈α↔has␈α↔the␈α⊗form
␈↓ ↓H␈↓␈↓ ↓x␈↓¬PLUS␈↓ . <list of monomials>.␈α
 A␈α
monomial␈α∞is␈α
either␈α
a␈α∞number,␈α
a␈α
variable,␈α∞or␈α
a␈α
product␈α∞of␈α
the
␈↓ ↓H␈↓␈↓ ↓xform ␈↓¬TIMES␈↓ . < list of numbers or variables >.

␈↓ ↓H␈↓        Try your simplifier on expressions returned by ␈↓↓diff.␈↓

␈↓ ↓H␈↓α␈↓ ε-Sets

␈↓ ↓H␈↓22.␈α∞Using␈α∞the␈α
function␈α∞␈↓↓member[x,␈α∞u]␈↓␈α∞defined␈α
earlier␈α∞in␈α∞this␈α∞chapter,␈α
which␈α∞may␈α∞also␈α∞be␈α
written
␈↓ ↓H␈↓␈↓ ↓x␈↓↓x ε u␈↓,␈α∞write␈α∞function␈α∞definitions␈α∞for␈α
the␈α∞union␈α∞␈↓↓u ∪ v␈↓␈α∞of␈α∞lists␈α
␈↓↓u␈↓␈α∞and␈α∞␈↓↓v,␈↓␈α∞the␈α∞intersection␈α
␈↓↓u ∩ v␈↓,
␈↓ ↓H␈↓␈↓ ↓xand the set difference  ␈↓↓u-v␈↓.   What is wanted should be clear from the examples:

␈↓ ↓H␈↓␈↓ ∧W␈↓↓␈↓¬(A B C) ∪ (B C D) = (A B C D)␈↓↓,␈↓
␈↓ ↓H␈↓␈↓ ∧c␈↓↓␈↓¬(A B C) ∩ (B C D) = (B C)␈↓↓,    ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ∧k␈↓↓␈↓¬(A B C) - (B C D) = (A)␈↓↓.      ␈↓
␈↓ ↓H␈↓46␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓23.␈αSuppose␈α␈↓↓x␈↓␈αtakes␈αnumbers␈αas␈αvalues␈αand␈α␈↓↓u␈↓␈αtakes␈αas␈αvalues␈αlists␈αof␈αnumbers␈αin␈αascending␈αorder,
␈↓ ↓H␈↓␈↓ ↓xe.g.␈α␈↓¬(2␈α4␈α7)␈↓.␈α Write␈αa␈αfunction␈α␈↓↓insert[x,␈αu]␈↓␈αwhose␈αvalue␈αis␈αobtained␈αfrom␈αthat␈αof␈α␈↓↓u␈↓␈αby␈αputting
␈↓ ↓H␈↓␈↓ ↓x␈↓↓x␈↓ in ␈↓↓u␈↓ in its proper place.   Thus

␈↓ ↓H␈↓␈↓ ¬
␈↓↓insert[␈↓¬3, ␈↓↓␈↓¬(2 4)␈↓↓] = ␈↓¬(2 3 4)␈↓↓␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ¬
␈↓↓insert[␈↓¬3, ␈↓↓␈↓¬(2 3)␈↓↓] = ␈↓¬(2 3 3)␈↓↓␈↓.

␈↓ ↓H␈↓24.␈αWrite␈αfunctions␈αgiving␈αthe␈αunion,␈αintersection,␈αand␈αset␈αdifference␈αof␈αordered␈αlists;␈αthe␈αresult␈αis
␈↓ ↓H␈↓␈↓ ↓xwanted as an ordered list.

␈↓ ↓H␈↓        Note␈α⊃that␈α⊃computing␈α⊃these␈α⊂functions␈α⊃of␈α⊃unordered␈α⊃lists␈α⊂takes␈α⊃a␈α⊃number␈α⊃of␈α⊂comparisons
␈↓ ↓H␈↓␈↓ ↓xproportional␈αto␈αthe␈αsquare␈αof␈αthe␈αnumber␈α
of␈αelements␈αof␈αa␈αtypical␈αlist,␈αwhile␈αfor␈α
ordered␈αlists,
␈↓ ↓H␈↓␈↓ ↓xthe number of comparisons is proportional to the number of elements.

␈↓ ↓H␈↓25. Using ␈↓↓insert,␈↓ write a function ␈↓↓sort␈↓ that transforms an unordered list into an ordered list.

␈↓ ↓H␈↓26.␈α∂Write␈α⊂a␈α∂function␈α⊂␈↓↓goodsort␈↓␈α∂that␈α⊂sorts␈α∂a␈α⊂list␈α∂using␈α⊂a␈α∂number␈α⊂of␈α∂comparisons␈α⊂proportional␈α∂to
␈↓ ↓H␈↓␈↓ ↓x␈↓↓n log n␈↓, where ␈↓↓n␈↓ is the length of the list to be sorted.

␈↓ ↓H␈↓α␈↓ ¬mPermutations

␈↓ ↓H␈↓27.  ␈↓↓rcycle[u]␈↓  is obtained from the list ␈↓↓u␈↓ by moving the last element to the first position.  Thus

␈↓ ↓H␈↓␈↓ ∧w␈↓↓rcycle[␈↓¬(A B C D)␈↓↓] = ␈↓¬(D A B C)␈↓↓␈↓. 

␈↓ ↓H␈↓28. ␈↓↓lcycle[u]␈↓  is obtained from the list ␈↓↓u␈↓ by moving the first element to the last position.  Thus

␈↓ ↓H␈↓␈↓ ∧x␈↓↓lcycle[␈↓¬(A B C D)␈↓↓] = ␈↓¬(B C D A)␈↓↓␈↓. 

␈↓ ↓H␈↓        We␈α
can␈α
think␈α
of␈α
a␈α
permutation␈α
on␈α
␈↓↓n␈↓␈α
objects␈α
as␈α
a␈α
function␈α
which␈α
maps␈α
a␈α
list␈α
␈↓¬(l1␈α∞l2␈α
...
␈↓ ↓H␈↓¬␈↓ ↓xlN)␈↓␈α
of␈α␈↓↓n␈↓␈α
distinct␈αelements␈α
to␈αa␈α
list␈α
containing␈αthe␈α
same␈αelements␈α
in␈α a␈α
different␈α
order.␈α Thus
␈↓ ↓H␈↓␈↓ ↓x␈↓¬(3 4 1 2)␈↓␈α↔is␈α_a␈α↔permutation␈α↔of␈α_␈↓¬(1 2 3 4)␈↓␈α↔and␈α↔␈↓¬(FOO BAR BAZ)␈↓␈α_is␈α↔a␈α_permutation␈α↔of
␈↓ ↓H␈↓␈↓ ↓x␈↓¬(BAR BAZ FOO)␈↓.␈α∂ The␈α⊂permutation␈α∂itself␈α⊂can␈α∂be␈α⊂represented␈α∂as␈α⊂a␈α∂list␈α⊂in␈α∂several␈α⊂ways.␈α∂ We
␈↓ ↓H␈↓␈↓ ↓xdescribe two possible representations of a permutation ␈↓↓pi.␈↓

␈↓ ↓H␈↓        ␈↓¬REP1:␈α
␈↓␈α␈↓↓pi␈↓␈α
is␈αrepresented␈α
as␈α
a␈αlist␈α
of␈αnumbers␈α
␈↓¬(j1 j2 ... jN)␈↓␈α
which␈αis␈α
itself␈αa␈α
permutation
␈↓ ↓H␈↓␈↓ ↓xof␈α
the␈α
list␈α
␈↓¬(1 2 ... N)␈↓␈α
and␈α
the␈α
result␈α
of␈α
applying␈α
such␈α
a␈α
permutation␈α
to␈α
a␈α
list␈α
␈↓↓u␈↓␈α
of␈α
length␈α
␈↓↓n␈↓␈α
is
␈↓ ↓H␈↓␈↓ ↓xthe␈αlist␈α␈↓↓u'␈↓␈αwhere␈αthe␈α␈↓↓k␈↓th␈αelement␈αof␈α␈↓↓u'␈↓␈αis␈αthe␈α␈↓↓jk␈↓th␈αelement␈αof␈α␈↓↓u.␈↓␈α Thus␈αthe␈αpermutation␈α␈↓¬(2␈α3
␈↓ ↓H␈↓¬␈↓ ↓x4␈α∪1)␈↓␈α∩applied␈α∪to␈α∪the␈α∩list␈α∪␈↓¬(A␈α∪B␈α∩C␈α∪D)␈↓␈α∪is␈α∩the␈α∪list␈α∪␈↓¬(B␈α∩C␈α∪D␈α∪A)␈↓.␈α∩ ␈↓¬(1␈α∪2␈α∪3␈α∩4)␈↓␈α∪is␈α∪the␈α∩identity
␈↓ ↓H␈↓␈↓ ↓xpermutation.

␈↓ ↓H␈↓        ␈↓¬REP2:␈α␈↓␈α
␈↓↓pi␈↓␈αis␈α
represented␈αas␈α
a␈αlist␈α
of␈αdisjoint␈α
cycles,␈αwhere␈α
a␈αcycle␈α
is␈αa␈α
a␈αlist␈α
of␈αlength␈αat␈α
most
␈↓ ↓H␈↓␈↓ ↓x␈↓↓n␈↓␈α
of␈αnumbers␈α
between␈α1␈α
and␈α␈↓↓n␈↓␈α
(with␈αno␈α
two␈αelements␈α
the␈αsame).␈α
 The␈αpermutation␈α
represented
␈↓ ↓H␈↓␈↓ ↓xby␈αsuch␈αa␈αlist␈αof␈αcycles␈αis␈αthe␈αpermutation␈αthat␈αresults␈αfrom␈αapplying␈αeach␈αof␈αthe␈αcycles.␈α (The
␈↓ ↓H␈↓␈↓ ↓xorder␈αof␈αapplication␈αdoesn't␈αmatter␈αsince␈αthe␈α
cycles␈αare␈αdisjoint.)␈αThe␈αresult␈αof␈αapplying␈αa␈α
cycle
␈↓ ↓H␈↓␈↓ ↓x␈↓¬(j1 j2 ... jk)␈↓␈αto␈αa␈αlist␈α␈↓↓u␈↓␈αof␈α
lenght␈α␈↓↓n␈↓␈αis␈αthe␈αlist␈α␈↓↓u'␈↓␈αwhere␈α
the␈αelement␈αin␈αposition␈α␈↓↓jm␈↓␈αin␈α␈↓↓u'␈↓␈α
is
␈↓ ↓H␈↓␈↓ ↓xthe␈α
element␈α
in␈α
position␈α
␈↓↓jm-1␈↓␈α
in␈α
␈↓↓u␈↓␈α
for␈α
␈↓↓1≤m≤k␈↓␈α
(taking␈α
␈↓↓j0␈↓␈α
to␈α
be␈α
␈↓↓jk).␈↓␈α
 Elements␈α
in␈α
positions␈αnot
␈↓ ↓H␈↓␈↓ ↓xmentioned␈αin␈αthe␈αcycle␈αare␈αunchanged.␈α Thus␈αthe␈αcycle␈α ␈↓¬(1 4 2)␈↓␈αapplied␈αto␈α
␈↓¬(A B C D)␈↓␈αgives
␈↓ ↓H␈↓␈↓ ↓x␈↓¬(B D C A)␈↓.  The empty list of cycles is the identity permutation in this representation.
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *47


␈↓ ↓H␈↓29.␈α␈↓↓isperm1[pi,n]␈↓␈α(␈↓↓isperm2[pi,n]␈↓)␈α
is␈αtrue␈αjust␈αif␈α
the␈αlist␈α␈↓↓pi␈↓␈αrepresents␈α
a␈αpermutation␈αon␈α
␈↓↓n␈↓␈αobjects
␈↓ ↓H␈↓␈↓ ↓xaccording to ␈↓¬REP1 ␈↓(␈↓¬REP2␈↓).

␈↓ ↓H␈↓30.␈α␈↓↓sameperm2[pi1,pi2]␈↓␈αis␈αtrue␈αif␈αand␈αonly␈αif␈α␈↓↓pi1␈↓␈αand␈α␈↓↓pi2␈↓␈αrepresent␈αthe␈αsame␈αpermutation.␈α (Note
␈↓ ↓H␈↓␈↓ ↓xthat the representation by ␈↓¬REP2 ␈↓is not unique while in ␈↓¬REP1 ␈↓it is.)

␈↓ ↓H␈↓31.␈α∞␈↓↓rho12[pi]␈↓␈α∞maps␈α
a␈α∞list␈α∞␈↓↓pi␈↓␈α
representing␈α∞a␈α∞permutation␈α
according␈α∞to␈α∞␈↓¬REP1␈α
␈↓to␈α∞a␈α∞list␈α
representing
␈↓ ↓H␈↓␈↓ ↓xthe same permutation according to ␈↓¬REP2. ␈↓ ␈↓↓rho21[u]␈↓ is the inverse map.

␈↓ ↓H␈↓32.␈α
␈↓↓compose1[pi1,pi2]␈↓␈αrepresents␈α
the␈α
permutation␈αthat␈α
results␈α
if␈α␈↓↓pi2␈↓␈α
is␈α
applied␈αfollowed␈α
by␈α␈↓↓pi1,␈↓␈α
all
␈↓ ↓H␈↓␈↓ ↓xrepresented according to ␈↓¬REP1. ␈↓ ␈↓↓compose2[u,v]␈↓ does the same but using ␈↓¬REP2. ␈↓

␈↓ ↓H␈↓33.␈α⊗␈↓↓invert1[pi]␈↓␈α∃(␈↓↓invert2[pi]␈↓)␈α⊗is␈α∃the␈α⊗inverse␈α∃to␈α⊗the␈α∃permutation␈α⊗␈↓↓pi,␈↓␈α∃in␈α⊗␈↓¬REP1␈α⊗␈↓(␈↓¬REP2␈↓).␈α∃ Thus
␈↓ ↓H␈↓␈↓ ↓x␈↓↓compose1[invert1[pi],pi]␈↓ is the identitiy permutation.

␈↓ ↓H␈↓        Note␈α∞that␈α∂one␈α∞way␈α∂to␈α∞solve␈α∂the␈α∞above␈α∞problems␈α∂would␈α∞be␈α∂to␈α∞write␈α∂the␈α∞programs␈α∂for␈α∞one
␈↓ ↓H␈↓␈↓ ↓xrepresentation␈α⊂and␈α⊂use␈α⊃the␈α⊂transformations␈α⊂␈↓↓rho12␈↓␈α⊃and␈α⊂␈↓↓rho21␈↓␈α⊂to␈α⊃obtain␈α⊂those␈α⊂for␈α⊃the␈α⊂other
␈↓ ↓H␈↓␈↓ ↓xrepresentation.␈α⊃ However␈α∩part␈α⊃of␈α∩the␈α⊃purpose␈α∩of␈α⊃the␈α⊃exercise␈α∩is␈α⊃to␈α∩see␈α⊃how␈α∩the␈α⊃different
␈↓ ↓H␈↓␈↓ ↓xrepresentations behave and this solution would defeat that purpose.

␈↓ ↓H␈↓α␈↓ ε$Trees

␈↓ ↓H␈↓         Consider␈α
a␈α
tree␈α
structure␈αrepresented␈α
as␈α
a␈α
list␈αin␈α
the␈α
following␈α
way.␈α
 A␈αleaf␈α
is␈α
any␈α
list␈αof␈α
the
␈↓ ↓H␈↓␈↓ ↓xform ␈↓¬(LEAF e)␈↓ where ␈↓¬e ␈↓is any S-expression.  A tree is either a leaf, or a list of trees.

␈↓ ↓H␈↓34.␈α ␈↓↓leaves␈↓␈αt␈αis␈αa␈αlist␈αof␈αall␈αthe␈αleaves␈αin␈αthe␈αtree␈α␈↓↓t,␈↓␈αin␈αthe␈αsame␈αorder␈αas␈αthey␈αappear␈αin␈α the␈αtree.
␈↓ ↓H␈↓␈↓ ↓x(Compare to ␈↓↓fringe␈↓ for S-expressions.)

␈↓ ↓H␈↓35.␈α
 Let␈α
␈↓↓leafval␈↓␈α
be␈α
an␈α
evaluation␈α
function␈αon␈α
leaves.␈α
 (Assume␈α
␈↓↓leafval␈α
returns␈↓␈α
integers.)␈α
␈↓↓bestleaf␈αt␈↓␈α
is
␈↓ ↓H␈↓␈↓ ↓xa␈αleaf␈αof␈α␈↓↓t␈↓␈αhaving␈αa␈αmaximal␈αvalue␈αaccording␈α␈↓↓leafval.␈αThat␈↓␈αis␈αno␈αother␈αleaf␈αof␈α␈↓↓t␈↓␈αhas␈αa␈αbetter
␈↓ ↓H␈↓␈↓ ↓xvalue.

␈↓ ↓H␈↓36.␈α␈↓↓bestleaf1␈αt␈↓␈αis␈αa␈αleaf␈αof␈α␈↓↓t␈↓␈αof␈αmaximal␈αvalue␈αand␈αamong␈αthose␈αof␈αthe␈αsame␈αvalue,␈α␈↓↓bestleaf1␈αt␈↓␈αhas
␈↓ ↓H␈↓␈↓ ↓xleast depth (is closest to the root).

␈↓ ↓H␈↓α␈↓ ε~graphs

␈↓ ↓H␈↓        Let ␈↓↓g␈↓ be a directed graph represented as a list of lists as described earlier in this chapter.

␈↓ ↓H␈↓37. ␈↓↓isnext[u,v,g]␈↓ is true if and only if ␈↓↓g␈↓ contains an edge from ␈↓↓u␈↓ to ␈↓↓v.␈↓

␈↓ ↓H␈↓38. ␈↓↓successors[u,g]␈↓ is the list of vertices, ␈↓↓v,␈↓ in ␈↓↓g␈↓ such that ␈↓↓isnext[u,v,g].␈↓

␈↓ ↓H␈↓39. ␈↓↓predecessors[u,g]␈↓ is the list of vertices ␈↓↓v,␈↓ in ␈↓↓g␈↓ such that ␈↓↓isnext[v,u,g].␈↓

␈↓ ↓H␈↓40.␈α
␈↓↓undir[g]␈↓␈α
is␈α
true␈αif␈α
and␈α
only␈α
if␈α
␈↓↓g␈↓␈αis␈α
undirected.␈α
 That␈α
is,␈αif␈α
for␈α
every␈α
edge␈α
from␈α␈↓↓u␈↓␈α
to␈α
␈↓↓v␈↓␈α
in␈α␈↓↓g␈↓␈α
there
␈↓ ↓H␈↓␈↓ ↓xis also and edge from ␈↓↓v␈↓ to ␈↓↓u.␈↓

␈↓ ↓H␈↓41.␈α ␈↓↓mkundir[g]␈↓␈α
is␈αthe␈α
graph␈α␈↓↓g1␈↓␈αwith␈α
the␈αsame␈α
vertices␈αas␈α
␈↓↓g,␈↓␈αand␈α such␈α
that␈αthere␈α
is␈αan␈αedge␈α
from
␈↓ ↓H␈↓␈↓ ↓x␈↓↓u␈↓ to ␈↓↓v␈↓ in ␈↓↓g1␈↓ if and only if ␈↓↓g␈↓ has either an edge from ␈↓↓v␈↓ to ␈↓↓u␈↓ or and edge from ␈↓↓u␈↓ to ␈↓↓v.␈↓
␈↓ ↓H␈↓48␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓42.␈α
 If␈α
␈↓↓g␈↓␈αis␈α
undirected␈α
then␈α
␈↓↓delete_vertex[v,g]␈↓␈α returns␈α
a␈α
graph␈α
␈↓↓g1␈↓␈αwith␈α
vertices␈α
those␈α
of␈α␈↓↓g␈↓␈α
omitting
␈↓ ↓H␈↓␈↓ ↓x␈↓↓v,␈↓ and edges the same as ␈↓↓g␈↓ omitting those connecting ␈↓↓v␈↓ to another vertex.

␈↓ ↓H␈↓43.␈α∞If␈α
␈↓↓g␈↓␈α∞is␈α
undirected␈α∞then␈α
␈↓↓complement[g]␈↓␈α∞ returns␈α
a␈α∞graph␈α
␈↓↓g1␈↓␈α∞with␈α
vertices␈α∞the␈α
same␈α∞as␈α∞␈↓↓g,␈↓␈α
but
␈↓ ↓H␈↓␈↓ ↓xvertices␈α␈↓↓v␈↓␈αand␈α␈↓↓w␈↓␈αare␈αjoined␈αby␈αan␈αedge␈αin␈α␈↓↓g1␈↓␈αif␈αand␈αonly␈αif␈αthey␈αare␈αnot␈αjoined␈αby␈αan␈αedge␈αin
␈↓ ↓H␈↓␈↓ ↓x␈↓↓g.␈↓

␈↓ ↓H␈↓44.␈α
For␈α
any␈α
graph␈α␈↓↓g,␈↓␈α
␈↓↓reachable[u,v,g]␈↓␈α
is␈α
true␈α
if␈αand␈α
only␈α
if␈α
there␈αis␈α
a␈α
sequence␈α
of␈α
vertices␈α␈↓↓u1,␈↓
␈↓ ↓H␈↓␈↓ ↓x␈↓↓u2,␈↓ ... ,␈↓↓un␈↓  in ␈↓↓g␈↓ with ␈↓↓u␈↓ = ␈↓↓u1,␈↓ ␈↓↓v␈↓ = ␈↓↓un␈↓ and ␈↓↓isnext[ui,ui+1,g]␈↓ for ␈↓↓1≤i≤n-1.␈↓

␈↓ ↓H␈↓45.␈αFor␈αany␈αgraph␈α␈↓↓g,␈↓␈α␈↓↓conn[g]␈↓␈αis␈αtrue␈αif␈αand␈αonly␈αif␈αthe␈αdirected␈αgraph␈α␈↓↓g␈↓␈αis␈αconnected␈αin␈αthe␈αsense
␈↓ ↓H␈↓␈↓ ↓xthat every vertex is reachable from every other vertex.

␈↓ ↓H␈↓NB:␈α
 Graphs␈αin␈α
general␈αhave␈α
cycles,␈αso␈α
when␈αyou␈α
are␈αrecurring␈α
through␈αa␈α
graph␈αyou␈α
need␈αto␈α
keep
␈↓ ↓H␈↓␈↓ ↓xtrack of where you have been in order to avoid a forever looping program.
␈↓ ↓H␈↓␈↓ εH␈↓ *49


␈↓ ↓H␈↓α␈↓ ¬zChapter III

␈↓ ↓H␈↓α␈↓ ∧εPROVING FACTS ABOUT LISP PROGRAMS


␈↓ ↓H␈↓        The␈αtheory␈αof␈αcomputation␈α
may␈αbe␈αdivided␈αinto␈αtwo␈α
parts.␈α The␈αfirst␈αis␈αthe␈α
general␈αtheory
␈↓ ↓H␈↓of␈α
computability␈α
including␈α
topics␈α
like␈α
universal␈α
functions,␈α
the␈α
existence␈α
of␈αuncomputable␈α
functions,
␈↓ ↓H␈↓lambda␈α∪calculus,␈α∪call-by-name␈α∪and␈α∪call-by-value,␈α∪and␈α∪the␈α∪relation␈α∪of␈α∪conditional␈α∩expression
␈↓ ↓H␈↓recursion␈α∞to␈α∞other␈α∞formalisms␈α∞for␈α∂describing␈α∞computable␈α∞functions.␈α∞ The␈α∞second␈α∞part,␈α∂which␈α∞we
␈↓ ↓H␈↓will␈α∂discuss␈α∞in␈α∂this␈α∞chapter␈α∂emphasizes␈α∞techniques␈α∂for␈α∞proving␈α∂particular␈α∞facts␈α∂about␈α∞particular
␈↓ ↓H␈↓computable␈αfunctions.␈α
 We␈αwill␈αemphasize␈α
the␈αuse␈α
of␈αthe␈αtechniques␈α
more␈αthan␈αtheir␈α
mathematical
␈↓ ↓H␈↓background.

␈↓ ↓H␈↓        In␈αparticular␈αwe␈α
will␈αexplain␈αa␈αmethod␈α
for␈αproving␈α␈↓↓extensional␈↓␈α
properties␈αof␈αa␈αrestricted␈α
but
␈↓ ↓H␈↓powerful␈α⊂class␈α⊃of␈α⊂LISP␈α⊂programs␈α⊃which␈α⊂we␈α⊃call␈α⊂␈↓↓clean,␈↓␈α⊂␈↓↓pure␈↓␈α⊃LISP␈α⊂programs.␈α⊃ An␈α⊂␈↓↓extensional␈↓
␈↓ ↓H␈↓␈↓↓property␈↓␈α
is␈αone␈α
that␈αdepends␈α
only␈αon␈α
the␈α
function␈αcomputed␈α
by␈αthe␈α
program.␈α Thus␈α
it␈αincludes␈α
the
␈↓ ↓H␈↓fact␈αthat␈αtwo␈αsort␈αprograms␈αcompute␈αthe␈αsame␈αfunction␈αor␈αthat␈α␈↓↓append␈↓␈αis␈αassociative␈αbut␈αdoes␈αnot
␈↓ ↓H␈↓include␈α
the␈α
fact␈α
that␈αone␈α
sort␈α
program␈α
does␈α
␈↓↓n␈↓∧2␈↓↓␈↓␈αcomparisons␈α
and␈α
another␈α
␈↓↓n log n␈↓␈α
comparisons␈αor
␈↓ ↓H␈↓that␈α
one␈αprogram␈α
for␈α␈↓↓reverse␈↓␈α
does␈αfewer␈α
␈↓↓cons␈↓es␈αthan␈α
another.␈α These␈α
latter␈αfacts␈α
are␈α
examples␈αof
␈↓ ↓H␈↓␈↓↓intensional␈↓␈α∩properties.␈α∩ ␈↓↓Clean␈↓␈α∩LISP␈α∩programs␈α∪have␈α∩no␈α∩side␈α∩effects␈α∩(our␈α∩methods␈α∪require␈α∩the
␈↓ ↓H␈↓freedom␈α∪to␈α∪replace␈α∪a␈α∪subexpression␈α∪by␈α∪an␈α∪equal␈α∪expression),␈α∪and␈α∪equality␈α∪refers␈α∪to␈α∪the␈α∩S-
␈↓ ↓H␈↓expressions␈αand␈αnot␈αto␈αthe␈αlist␈αstructures.␈α ␈↓↓Pure␈↓␈αLISP␈αinvolves␈αonly␈αrecursive␈αfunction␈αdefinitions
␈↓ ↓H␈↓and␈α
doesn't␈α
use␈α
assignment␈α
statements.␈α
 So␈α
far␈α
we␈α
have␈α
only␈α
discussed␈α
how␈α
to␈α
write␈α
clean␈α
and␈α
pure
␈↓ ↓H␈↓LISP␈α
programs.␈α
  The␈α	constructs␈α
to␈α
be␈α	explained␈α
in␈α
Chapter␈α
IV␈α	will␈α
take␈α
us␈α	out␈α
of␈α
this␈α
realm.␈α	 We
␈↓ ↓H␈↓will␈α∂see␈α∂in␈α∂later␈α∞chapters␈α∂how␈α∂the␈α∂proof␈α∞techniques␈α∂can␈α∂be␈α∂extended␈α∞to␈α∂cover␈α∂a␈α∂larger␈α∂class␈α∞of
␈↓ ↓H␈↓properties and a larger class of programs.

␈↓ ↓H␈↓        We␈α⊂shall␈α⊂begin␈α∂with␈α⊂an␈α⊂informal␈α⊂example␈α∂and␈α⊂a␈α⊂discussion␈α∂of␈α⊂the␈α⊂ideas␈α⊂and␈α∂principles
␈↓ ↓H␈↓involved.␈α We␈αthen␈αproceed␈αto␈αformalize␈αthese␈α
ideas.␈α We␈αwill␈αgive␈αa␈αfairly␈αformal␈α
description␈αof
␈↓ ↓H␈↓the␈αmethod␈αwhich␈αis␈αbased␈αon␈αthe␈αuse␈α
of␈αfirst-order␈αlogic.␈α There␈αwill␈αalso␈αbe␈αmany␈α
examples␈αof
␈↓ ↓H␈↓application␈α
of␈αthe␈α
proof␈α
technique.␈α The␈α
working␈αout␈α
of␈α
example␈αproofs␈α
will␈αbe␈α
generally␈α
in␈αthe
␈↓ ↓H␈↓usual␈α⊃informal␈α∩mathematical␈α⊃style␈α⊃and␈α∩should␈α⊃be␈α⊃understandable␈α∩intuitively␈α⊃as␈α⊃well␈α∩as␈α⊃being
␈↓ ↓H␈↓formally correct when all the detail is filled in.

␈↓ ↓H␈↓        The␈α∩methods␈α∩of␈α∩first-order␈α∩logic␈α∩are␈α∩well␈α∩developed␈α∩and␈α∩understood.␈α∩ The␈α∩reason␈α∩for
␈↓ ↓H␈↓wanting␈α
a␈α
formal␈α
system␈α
is␈α∞that␈α
such␈α
a␈α
system␈α
can␈α
easily␈α∞be␈α
described␈α
to␈α
and␈α
manipulated␈α∞by␈α
a
␈↓ ↓H␈↓computer.␈α∞ Given␈α∞a␈α∞program␈α
that␈α∞can␈α∞understand␈α∞the␈α
description␈α∞of␈α∞a␈α∞first-order␈α∞language␈α
and
␈↓ ↓H␈↓can␈α⊂understand␈α⊂proofs␈α⊂about␈α⊂facts␈α⊂stated␈α⊂in␈α∂this␈α⊂language,␈α⊂we␈α⊂can␈α⊂describe␈α⊂our␈α⊂technique␈α∂for
␈↓ ↓H␈↓proving␈αfacts␈α
about␈αLISP␈αprograms␈α
to␈αthis␈αprogram␈α
and␈αit␈αwill␈α
be␈αable␈αto␈α
check␈αthat␈α
our␈αproofs
␈↓ ↓H␈↓are␈αcorrect.␈α
 Such␈αa␈αprogram␈α
exists␈αat␈α
Stanford␈α[Weyhrauch␈α1977].␈α
 Other␈αprograms␈α
exist␈αwhich
␈↓ ↓H␈↓are␈α
designed␈α
to␈α
prove␈α
facts␈α
about␈α
LISP␈αprograms,␈α
for␈α
example␈α
[Boyer␈α
and␈α
Moore␈α
1978].␈α Much␈α
of
␈↓ ↓H␈↓the material in this chapter is based on [Cartwright 1977] and the rest on [McCarthy 1978b].



␈↓ ↓H␈↓1.  ␈↓αSimple properties of ␈↓↓append␈↓α␈↓


␈↓ ↓H␈↓        Before␈α
getting␈α
into␈α
the␈α
details␈α
of␈α∞our␈α
technique␈α
for␈α
proving␈α
properties␈α
of␈α∞LISP␈α
programs,
␈↓ ↓H␈↓we shall consider a few examples.  Recall the familiar program for ␈↓↓append␈↓ing two lists given by
␈↓ ↓H␈↓50␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓1.1)␈↓ ∧M␈↓↓u*v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]␈↓. 

␈↓ ↓H␈↓We␈αmight␈αlike␈αto␈αprove␈αthat␈αfor␈αany␈αlists␈α␈↓↓u␈↓␈αand␈α␈↓↓v,␈↓␈αthe␈αprogram␈αterminates␈αand␈αthat␈αthe␈αfunction
␈↓ ↓H␈↓␈↓↓u*v,␈↓ computed by this program, has the following properties:

␈↓ ↓H␈↓1.2)␈↓ ¬↑␈↓↓ ␈↓¬NIL␈↓↓*u = u,     ␈↓
␈↓ ↓H␈↓1.3)␈↓ ¬↑␈↓↓ u*␈↓¬NIL␈↓↓ = u,     ␈↓
␈↓ ↓H␈↓1.4)␈↓ ¬J␈↓↓u*[v*w] = [u*v]*w␈↓.

␈↓ ↓H␈↓The␈αfirst␈αtwo␈α
statements␈αexpress␈αthe␈α
fact␈αis␈αthat␈α␈↓¬NIL␈↓␈α
is␈αthe␈α"zero"␈α
of␈αthe␈αappend␈α
operation.␈α The
␈↓ ↓H␈↓standard␈α
mathematical␈α
terminology␈α
is␈α
to␈α
say␈α
that␈α
␈↓¬NIL␈↓␈αis␈α
both␈α
a␈α
left␈α
identity␈α
and␈α
a␈α
right␈αidentity
␈↓ ↓H␈↓for␈α
␈↓↓append.␈↓␈α The␈α
last␈α
equation␈αexpresses␈α
the␈αfact␈α
that␈α
␈↓↓append␈↓␈αis␈α
associative␈α-␈α
like␈α
addition␈αand
␈↓ ↓H␈↓multiplication␈α∀of␈α∀numbers.␈α∪ [Unlike␈α∀addition␈α∀and␈α∀multiplication␈α∪of␈α∀numbers,␈α∀␈↓↓append␈↓␈α∀is␈α∪not
␈↓ ↓H␈↓commutative,␈α
since␈α
␈↓¬(A B)␈↓*␈↓¬(C D)␈↓␈α
=␈α
␈↓¬(A B C D)␈↓␈α
≠␈α
␈↓¬(C D)␈↓*␈↓¬(A B)␈↓].␈α
 Associativity␈α
allows␈α
us␈α
to␈α
omit
␈↓ ↓H␈↓brackets and write ␈↓↓u*v*w,␈↓ because it doesn't matter how the expression is parenthesized.

␈↓ ↓H␈↓        As␈αwe␈αsaw␈αin␈αChapter␈αII␈αthe␈αdefinition␈αof␈α␈↓↓append␈↓␈αhas␈αthe␈αform␈αof␈αrecursion␈αthat␈αdefines␈αa
␈↓ ↓H␈↓total␈αfunction␈αon␈αlists.␈α Thus␈αgiven␈αlists␈α␈↓↓u␈↓␈αand␈α␈↓↓v␈↓␈αif␈αwe␈αuse␈αthis␈αdefinition␈αof␈α␈↓↓append␈↓␈αand␈αthe␈αrules
␈↓ ↓H␈↓for␈α∞evaluation␈α∞given␈α∞in␈α∞Chapter␈α∞I␈α∞we␈α∞will␈α∞arrive␈α
at␈α∞an␈α∞answer␈α∞in␈α∞a␈α∞finite␈α∞number␈α∞of␈α∞steps.␈α
 In
␈↓ ↓H␈↓order to describe the function thus determined we write the equation

␈↓ ↓H␈↓1.5)␈↓ ∧N␈↓↓u*v = ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]␈↓. 

␈↓ ↓H␈↓All␈αwe␈αhave␈αdone␈αis␈αto␈αreplace␈αthe␈α"←"␈αsign␈αby␈α"=".␈α  We␈αwill␈αassume␈αthat␈αthe␈αequation␈α
holds␈αfor
␈↓ ↓H␈↓all lists (lists not S-expressions in general) ␈↓↓u␈↓ and ␈↓↓v.␈↓

␈↓ ↓H␈↓        Proving␈α
␈↓↓␈↓¬NIL␈↓↓*u␈α=␈α
u␈↓,␈αi.e.␈α
that␈α␈↓¬NIL␈↓␈α
is␈αa␈α
left␈α
identity␈αfor␈α
␈↓↓append,␈↓␈αis␈α
easy.␈α We␈α
just␈αsubstitute␈α
in
␈↓ ↓H␈↓(1.5) and get

␈↓ ↓H␈↓↓␈↓ βx␈↓¬NIL␈↓↓*u␈↓ ∧X= ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬NIL␈↓↓ . [␈↓αd|␈↓↓␈↓¬NIL␈↓↓ * u]
␈↓ ↓H␈↓↓␈↓ ∧X= u.

␈↓ ↓H␈↓That␈α⊂was␈α⊂untypically␈α⊂easy,␈α⊃because␈α⊂we␈α⊂won't␈α⊂often␈α⊂succeed␈α⊃in␈α⊂proving␈α⊂what␈α⊂we␈α⊂want␈α⊃just␈α⊂by
␈↓ ↓H␈↓substituting␈αin␈αfunction␈αdefinitions␈αand␈αusing␈αknown␈αproperties␈αof␈αthe␈αelementary␈αLISP␈αfunctions
␈↓ ↓H␈↓and␈αconditional␈αexpressions.␈α In␈αfact,␈αwhen␈αwe␈αwant␈αto␈αprove␈αsomething␈αabout␈αa␈αfunction␈αdefined
␈↓ ↓H␈↓by␈αrecursion,␈αwe␈αalmost␈αalways␈αhave␈αto␈αuse␈αsome␈αtechnique␈αof␈αmathematical␈αinduction␈αin␈αmaking
␈↓ ↓H␈↓the␈α
proof.␈α
 Proving␈α∞that␈α
␈↓¬NIL␈↓␈α
is␈α∞a␈α
right␈α
identity␈α∞(1.3)␈α
requires␈α
such␈α∞an␈α
induction.␈α
 As␈α∞before,␈α
we
␈↓ ↓H␈↓begin by substituting into (1.5), but this time we get

␈↓ ↓H␈↓␈↓ ∧≠␈↓↓u*␈↓¬NIL␈↓↓ = ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * ␈↓¬NIL␈↓↓]␈↓, 

␈↓ ↓H␈↓and␈α∞this␈α∞time␈α∞the␈α∞conditional␈α
expression␈α∞does␈α∞not␈α∞just␈α∞collapse.␈α
 However,␈α∞notice␈α∞that␈α∞if␈α∞␈↓↓u␈↓␈α
were
␈↓ ↓H␈↓␈↓¬NIL␈↓,␈α
(1.3)␈α
would␈α
follow.␈α
 Notice␈α
also␈α
that␈α
if␈α
␈↓↓u␈↓␈α
were␈α
not␈α
␈↓¬NIL␈↓,␈α
but␈α
we␈α
knew␈α
that␈α
␈↓¬NIL␈↓␈α
were␈α
a␈α
right
␈↓ ↓H␈↓identity for ␈↓↓␈↓αd|␈↓↓u␈↓, we could make the calculation

␈↓ ↓H␈↓↓␈↓ ∧8u*␈↓¬NIL␈↓↓␈↓ ¬_= ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * ␈↓¬NIL␈↓↓]
␈↓ ↓H␈↓↓␈↓ ¬_= ␈↓αa|␈↓↓u . ␈↓αd|␈↓↓u
␈↓ ↓H␈↓↓␈↓ ¬_= u,

␈↓ ↓H␈↓and this is what we want to prove.
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *51


␈↓ ↓H␈↓        The␈α∞induction␈α∞technique␈α∞for␈α∞proving␈α∞some␈α∞proposition␈α∂for␈α∞all␈α∞␈↓↓u␈↓␈α∞is␈α∞to␈α∞prove␈α∞it␈α∂under␈α∞the
␈↓ ↓H␈↓assumption␈α
that␈α∞it␈α
holds␈α
for␈α∞all␈α
"smaller"␈α∞␈↓↓u.␈↓␈α
 We␈α
then␈α∞conclude␈α
that␈α∞it␈α
holds␈α
for␈α∞all␈α
␈↓↓u.␈↓␈α∞ In␈α
this
␈↓ ↓H␈↓case,␈αwe␈αare␈αcalling␈αa␈αlist␈α␈↓↓w1␈↓␈α"smaller"␈αthan␈αa␈αlist␈α␈↓↓w2␈↓␈αif␈αit␈αis␈αa␈α"tail"␈αof␈α␈↓↓w2.␈↓␈α In␈αparticular␈α␈↓↓␈↓αd|␈↓↓u␈↓␈αis␈αa
␈↓ ↓H␈↓"tail" of ␈↓↓u␈↓ and hence "smaller" than ␈↓↓u.␈↓

␈↓ ↓H␈↓        The␈α∃correctness␈α∃of␈α∀such␈α∃an␈α∃induction␈α∀technique␈α∃depends␈α∃on␈α∀the␈α∃notion␈α∃of␈α∀"smaller"
␈↓ ↓H␈↓satisfying␈αwhat␈αmathematicians␈αcall␈αa␈α"descending␈α
chain␈αcondition".␈α Namely,␈αgiven␈αan␈αelement␈α
␈↓↓u,␈↓
␈↓ ↓H␈↓there␈αmust␈αnot␈αbe␈αan␈αinfinite␈αdescending␈αchain␈αof␈αelements␈αstarting␈αwith␈α␈↓↓u␈↓␈αsuch␈αthat␈αeach␈αelement
␈↓ ↓H␈↓is␈α∂smaller␈α∂than␈α∂the␈α∂preceding␈α∂one.␈α∂ In␈α∂the␈α∞case␈α∂of␈α∂lists,␈α∂this␈α∂is␈α∂informally␈α∂obvious;␈α∂if␈α∂you␈α∞keep
␈↓ ↓H␈↓taking sublists, you will eventually arrive at the null list.

␈↓ ↓H␈↓        A␈α∀descending␈α∀chain␈α∀condition␈α∀is␈α∀all␈α∃we␈α∀need␈α∀to␈α∀make␈α∀induction␈α∀work,␈α∀because␈α∃if␈α∀a
␈↓ ↓H␈↓proposition␈α⊂concerning␈α⊂␈↓↓u␈↓␈α⊂follows␈α⊂from␈α⊂the␈α⊂same␈α⊂proposition␈α⊂for␈α⊂all␈α⊂smaller␈α⊂elements,␈α⊂then␈α∂the
␈↓ ↓H␈↓converse␈α
of␈α
the␈α
same␈α
argument␈α
would␈αderive␈α
from␈α
any␈α
counter␈α
example␈α
a␈α
smaller␈αcounter␈α
example
␈↓ ↓H␈↓and␈α∩hence␈α∩an␈α∩infinite␈α⊃descending␈α∩chain␈α∩of␈α∩counter␈α∩examples.␈α⊃ If␈α∩there␈α∩can't␈α∩be␈α∩any␈α⊃infinite
␈↓ ↓H␈↓descending chains at all, then there can't be an infinite descending chain of counter examples.

␈↓ ↓H␈↓        In␈α∞the␈α∞above␈α
argument,␈α∞we␈α∞have␈α
avoided␈α∞mathematical␈α∞symbolism.␈α
 In␈α∞order␈α∞to␈α∞make␈α
the
␈↓ ↓H␈↓method easy to apply, we shall express all this formally in the following sections.

␈↓ ↓H␈↓        Now␈α∂let␈α∂us␈α∂return␈α∂to␈α∂our␈α∂proof␈α∂of␈α∂the␈α∂three␈α∂properties␈α∂of␈α∂␈↓↓append.␈↓␈α∂ The␈α∂associativity␈α∂of
␈↓ ↓H␈↓␈↓↓append␈↓␈α(1.4)␈α
is␈αproved␈αby␈α
exactly␈αthe␈αsame␈α
kind␈αof␈αinductive␈α
argument␈αas␈αwe␈α
use␈αfor␈αproving␈α
␈↓¬NIL␈↓
␈↓ ↓H␈↓is␈α⊂a␈α⊂right␈α⊂identity.␈α∂ In␈α⊂the␈α⊂case␈α⊂of␈α∂associativity,␈α⊂the␈α⊂algebra␈α⊂is␈α∂a␈α⊂little␈α⊂more␈α⊂complicated.␈α∂ The
␈↓ ↓H␈↓induction hypothesis is that ␈↓↓append␈↓ is associative for all "smaller" ␈↓↓u.␈↓  There are two cases.

␈↓ ↓H␈↓        If␈α␈↓↓u␈↓␈αis␈α␈↓¬NIL␈↓,␈αthe␈αtheorem␈αis␈αan␈αobvious␈αconsequence␈αof␈αthe␈αfact␈αthat␈α␈↓¬NIL␈↓␈αis␈αa␈αleft␈αidentity,␈α
i.e.
␈↓ ↓H␈↓the␈αfirst␈αtheorem␈αwe␈αproved.␈α (The␈αhypothesis␈αthat␈αthe␈αtheorem␈αis␈αtrue␈αfor␈αall␈αsmaller␈α␈↓↓u␈↓␈αplayed␈α
no
␈↓ ↓H␈↓role, because there is no list smaller than ␈↓¬NIL␈↓).  If ␈↓↓u␈↓ is not ␈↓¬NIL␈↓, we have

␈↓ ↓H␈↓␈↓ ↓x␈↓↓[u*v]*w␈↓␈↓ αx␈↓↓= [␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]] * w␈↓,␈↓ ¬8using the definition of ␈↓↓u*v␈↓,

␈↓ ↓H␈↓␈↓ αx␈↓↓= ␈↓αa|␈↓↓[␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]] . [[␈↓αd|␈↓↓[␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]] * w]␈↓,
␈↓ ↓H␈↓␈↓ ∧8using the definition of ␈↓↓append␈↓ to expand the second * of the
␈↓ ↓H␈↓␈↓ ∧8previous line together with the fact that ␈↓↓[␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]]␈↓
␈↓ ↓H␈↓␈↓ ∧8cannot be ␈↓¬NIL␈↓, since it is formed by a ␈↓↓cons.␈↓

␈↓ ↓H␈↓␈↓ αx␈↓↓= ␈↓αa|␈↓↓u . [[␈↓αd|␈↓↓u * v] * w]␈↓,␈↓ ¬8simplifying the above using facts about
␈↓ ↓H␈↓␈↓ ∧8␈↓αa␈↓ and ␈↓αd␈↓ applied to ␈↓↓[␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]],␈↓

␈↓ ↓H␈↓␈↓ αx␈↓↓= ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * [v * w]]␈↓,␈↓ ¬8using the induction hypothesis and the fact
␈↓ ↓H␈↓␈↓ ∧8that ␈↓↓␈↓αd|␈↓↓u␈↓ is smaller than ␈↓↓u.␈↓

␈↓ ↓H␈↓␈↓ αx␈↓↓= u * [v * w]␈↓,␈↓ ∧xusing the definition of ␈↓↓append␈↓ applied to
␈↓ ↓H␈↓␈↓ ∧8the first * in ␈↓↓u*[v*w]␈↓, i.e. going backwards.

␈↓ ↓H␈↓        The␈α
algebra␈αof␈α
this␈α
proof␈αis␈α
rather␈α
typical.␈α We␈α
used␈α
the␈αdefinition␈α
of␈α
the␈αfunction␈α
involved
␈↓ ↓H␈↓several␈αtimes,␈α
we␈αused␈α
the␈αinduction␈α
hypothesis␈αonce,␈αand␈α
we␈αused␈α
the␈αelementary␈α
algebraic␈αfacts
␈↓ ↓H␈↓about␈αconditional␈αexpressions␈αand␈αthe␈αbasic␈αfunctions␈αof␈αLISP.␈α Note␈αthat␈αin␈αthe␈α␈↓¬NIL␈↓␈αcase␈αwe␈αdid
␈↓ ↓H␈↓not␈α
need␈α
the␈α
induction␈α
hypothesis.␈α
 With␈α
induction␈α
proofs␈α
it␈α
is␈α
generally␈α
the␈α
case␈α
that␈α∞there␈α
are
␈↓ ↓H␈↓some␈α
"base"␈α
cases␈α
which␈α
are␈α
proved␈α
directly.␈α
 The␈α
remaining␈α
cases␈α
have␈α
the␈α
property␈α
that␈α
they␈α
can
␈↓ ↓H␈↓be reduced to simpler cases, for which the induction hypothesis applies.
␈↓ ↓H␈↓52␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓        If␈αone␈αis␈α
willing␈αto␈αdo␈αwithout␈α
equations,␈αproving␈αthe␈αtermination␈α
of␈α␈↓↓u*v␈↓␈αis␈αalso␈α
easy.␈α We
␈↓ ↓H␈↓first␈αnote␈αthat␈αwhen␈α␈↓↓u␈↓␈α
is␈α␈↓¬NIL␈↓,␈αthe␈αcomputation␈αterminates.␈α
 Next␈αwe␈αnote␈αthat␈αif␈α
the␈αcomputation
␈↓ ↓H␈↓terminates␈α
for␈α
␈↓αd|␈↓u␈α
and␈α
all␈α␈↓↓v,␈↓␈α
then␈α
the␈α
computation␈α
of␈α
␈↓↓u*v␈↓␈αterminates.␈α
 Just␈α
as␈α
before,␈α
we␈αcan␈α
make
␈↓ ↓H␈↓the␈α
argument␈α
that␈α
if␈α
there␈α
were␈α
a␈α
counter␈αexample␈α
to␈α
the␈α
termination␈α
of␈α
␈↓↓u*v,␈↓␈α
then␈α
there␈αwould␈α
be
␈↓ ↓H␈↓an␈α∀infinite␈α∃decreasing␈α∀chain␈α∀of␈α∃counter-examples,␈α∀and␈α∀this␈α∃is␈α∀impossible.␈α∀ Notice␈α∃that␈α∀this
␈↓ ↓H␈↓argument␈α∀is␈α∀even␈α∃more␈α∀informal␈α∀than␈α∃the␈α∀preceding,␈α∀because␈α∀we␈α∃haven't␈α∀given␈α∀a␈α∃way␈α∀of
␈↓ ↓H␈↓expressing termination as an equation.

␈↓ ↓H␈↓        The␈αmethod␈αwe␈αused␈αabove␈αworks␈αwell␈αfor␈αprograms␈αthat␈αdefine␈αtotal␈αfunctions␈αon␈αlists,␈αS-
␈↓ ↓H␈↓expressions,␈αnumbers,␈αetc.␈α If␈αwe␈αrestricted␈αourselves␈αto␈αsuch␈αprograms␈αwe␈αwould␈αbe␈αset.␈α However,
␈↓ ↓H␈↓it␈αis␈αwell␈αknown␈αthat␈αLISP␈αprograms␈αdo␈αnot␈αalways␈αterminate.␈α (You␈αhave␈αprobably␈αwritten␈αa␈αfew
␈↓ ↓H␈↓such␈α⊃programs␈α⊃yourself!)␈α⊃ Even␈α⊃if␈α⊃the␈α⊃program␈α∩does␈α⊃terminate,␈α⊃a␈α⊃large␈α⊃part␈α⊃of␈α⊃our␈α∩effort␈α⊃in
␈↓ ↓H␈↓proving␈αproperties␈αis␈αoften␈αdevoted␈αto␈αproving␈αthe␈αtermination␈αpart␈αas␈αthere␈αare␈αmany␈αinteresting
␈↓ ↓H␈↓programs␈α∂that␈α∂do␈α∂not␈α∂match␈α∂one␈α∂of␈α∂the␈α∂schemas␈α∂that␈α∂are␈α∂guaranteed␈α∂to␈α∂define␈α∂total␈α∞functions.
␈↓ ↓H␈↓Also␈α⊃many␈α⊃interesting␈α⊃programs␈α⊃terminate␈α⊃on␈α⊃some␈α⊃but␈α⊃not␈α⊃all␈α⊃values␈α⊃of␈α⊃the␈α∩arguments␈α⊃(for
␈↓ ↓H␈↓example ␈↓↓eval␈↓) and we would like to be able to say things about these programs.

␈↓ ↓H␈↓        The␈α∂argument␈α∂we␈α∂made␈α∂above␈α∂to␈α∂show␈α∂directly␈α∂that␈α∂␈↓↓append␈↓␈α∂terminated␈α∂was␈α∂correct,␈α∂but
␈↓ ↓H␈↓informal.␈α
 We␈α
want␈α
to␈α
introduce␈α
a␈α
formal␈αnotation␈α
so␈α
that␈α
we␈α
can␈α
prove␈α
termination␈α
by␈αexplicit
␈↓ ↓H␈↓rules␈αand␈α
thus␈αmeet␈α
our␈αgoal␈αof␈α
having␈αa␈α
computer␈αbe␈α
able␈αto␈αcheck␈α
our␈αproofs.␈α
  It␈αturns␈αout␈α
that
␈↓ ↓H␈↓we␈α⊃will␈α⊃be␈α⊃able␈α⊃to␈α⊃describe␈α⊃a␈α⊃general␈α⊃form␈α⊃of␈α⊃recursive␈α⊃definition␈α⊃which␈α⊃will␈α⊃have␈α∩the␈α⊃nice
␈↓ ↓H␈↓property␈α∞that␈α∞we␈α∂can␈α∞replace␈α∞"←"␈α∞by␈α∂"="␈α∞and␈α∞compute␈α∞with␈α∂the␈α∞resulting␈α∞equation␈α∂regardless␈α∞of
␈↓ ↓H␈↓whether␈αor␈αnot␈αthe␈αprogram␈αalways␈αhalts.␈α  However␈αwe␈αhave␈αto␈αcareful␈αabout␈αusing␈αthe␈αresulting
␈↓ ↓H␈↓expressions␈αin␈αcontexts␈αwhere␈αS-expressions␈α(or␈αlists␈αor␈αnumbers)␈αare␈αexpected.␈α  To␈αsee␈αwhy␈αthis
␈↓ ↓H␈↓is so consider the following definition

␈↓ ↓H␈↓1.6)␈↓ ¬3␈↓↓omega x ← omega x . ␈↓¬A␈↓↓␈↓. 

␈↓ ↓H␈↓It␈α
is␈α
easy␈α
to␈α
see␈α
that␈α
this␈α
program␈αnever␈α
terminates.␈α
 (In␈α
a␈α
computer␈α
you␈α
would␈α
probably␈α
get␈αan
␈↓ ↓H␈↓overflow␈α
of␈αsome␈α
kind␈αas␈α
the␈αevaluator␈α
attempted␈α
to␈αconstruct␈α
an␈αinfinite␈α
tree␈αof␈α
␈↓¬A␈↓s.␈α
)␈α  Suppose
␈↓ ↓H␈↓we now form the corresponding equation

␈↓ ↓H␈↓1.7)␈↓ ¬4␈↓↓omega x = omega x . ␈↓¬A␈↓↓␈↓. 

␈↓ ↓H␈↓and␈α∂further␈α∞assume␈α∂that␈α∞everything␈α∂is␈α∞an␈α∂S-expression.␈α∞  Then␈α∂in␈α∞particular␈α∂␈↓↓omega␈α∞x␈↓␈α∂is␈α∂an␈α∞S-
␈↓ ↓H␈↓expression␈αfor␈αany␈α␈↓↓x.␈↓␈α  The␈αequation␈α(1.7)␈αsays␈αthat␈αis␈αnot␈αan␈αatom␈αand␈αthus␈αwe␈αcan␈αapply␈α␈↓↓car␈↓␈αto
␈↓ ↓H␈↓both sides of the equation obtaining

␈↓ ↓H␈↓␈↓ ¬C␈↓↓␈↓αa|␈↓↓omega x = omega x␈↓. 

␈↓ ↓H␈↓This␈α
is␈αnot␈α
possible␈αfor␈α
a␈α
finite␈αtree␈α
like␈αstructure.␈α
  In␈α
fact,␈αapplying␈α
our␈αinduction␈α
principle␈αto␈α
S-
␈↓ ↓H␈↓expressions␈αwhere␈α␈↓↓␈↓αa|␈↓↓x␈↓␈αis␈α"smaller"␈αthan␈α␈↓↓x␈↓␈αfor␈αnon-atomic␈α␈↓↓x␈↓␈αwe,␈αcan␈αarrive␈αat␈αa␈αcontradiction␈αfrom
␈↓ ↓H␈↓this␈α⊂equation.␈α⊂ Having␈α⊂thus␈α⊂proved␈α⊂␈↓αfalse␈↓␈α⊂we␈α⊂can␈α⊂prove␈α⊂anything␈α⊂we␈α⊂like␈α⊂(and␈α⊃no-one␈α⊂would
␈↓ ↓H␈↓believe␈α
anything␈α
we␈α
proved)!␈α
  The␈α
way␈α
out␈αof␈α
this␈α
dilemma␈α
is␈α
to␈α
admit␈α
non-S-expressions␈αinto
␈↓ ↓H␈↓the␈α
domain␈αof␈α
things␈α
we␈αare␈α
describing␈α
by␈αthe␈α
equations␈α
we␈αwrite.␈α
 We␈α
will␈αin␈α
fact␈α
consider␈αthe
␈↓ ↓H␈↓domain␈α⊃of␈α⊃"extended"␈α⊃S-expressions␈α⊃formed␈α⊃by␈α⊃adding␈α⊃an␈α⊃element,␈α⊃␈↓π|␈↓,␈α⊃called␈α⊃"bottom"␈α⊃to␈α⊂the
␈↓ ↓H␈↓domain␈αof␈α
S-expressions.␈α When␈α
a␈αfunction␈α
doesn't␈αterminate␈αwe␈α
will␈αassign␈α
it␈αthis␈α
value.␈α Then
␈↓ ↓H␈↓we␈αmust␈αextend␈αall␈αof␈αour␈αfunctions␈αto␈αthis␈αlarger␈αdomain.␈α For␈αexample␈αwe␈αwill␈αneed␈αto␈αsay␈αwhat
␈↓ ↓H␈↓happens␈αwhen␈α
␈↓αa␈↓␈αand␈α␈↓αd␈↓␈α
are␈αapplied␈α
to␈α␈↓π|␈↓.␈α If␈α
we␈αdo␈α
this␈αjust␈αright␈α
we␈αwill␈α
have␈αa␈αconsistent␈α
system,
␈↓ ↓H␈↓and our proofs will be meaningful.
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *53


␈↓ ↓H␈↓α␈↓ εεExercises.

␈↓ ↓H␈↓Prove the following properties of ␈↓↓reverse␈↓

␈↓ ↓H␈↓1.      ␈↓↓∀u: reverse u = reverse1 u␈↓

␈↓ ↓H␈↓2.      ␈↓↓∀u v: reverse[u * v] = [reverse v] * [reverse u]␈↓

␈↓ ↓H␈↓3.      ␈↓↓∀u: reverse reverse u = u␈↓.

␈↓ ↓H␈↓Recall that ␈↓↓reverse␈↓ and ␈↓↓reverse1␈↓ are defined by

␈↓ ↓H␈↓        ␈↓↓reverse1 u ← if ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [reverse1 ␈↓αd|␈↓↓u] * <␈↓αa|␈↓↓u>␈↓

␈↓ ↓H␈↓        ␈↓↓reverse u ← rev[u, ␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓        ␈↓↓rev[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ rev[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . v]␈↓

␈↓ ↓H␈↓As␈α∞with␈α∞the␈α∞definition␈α∞of␈α
␈↓↓append,␈↓␈α∞these␈α∞definitions␈α∞are␈α∞of␈α
a␈α∞form␈α∞guaranteed␈α∞to␈α∞define␈α∞a␈α
total
␈↓ ↓H␈↓funtion␈α
on␈αlists.␈α
  Assume␈αthat␈α
the␈α
defined␈αfunctions␈α
in␈αeach␈α
case␈αsatisfy␈α
the␈α
functional␈αequation
␈↓ ↓H␈↓obtained␈α⊂by␈α⊂replacing␈α⊂"←"␈α∂by␈α⊂ "=".␈α⊂ (The␈α⊂first␈α⊂proof␈α∂requires␈α⊂inventing␈α⊂a␈α⊂suitable␈α⊂sentence␈α∂on
␈↓ ↓H␈↓which to do an induction.)



␈↓ ↓H␈↓2.  ␈↓αAbout Formalizing.␈↓


␈↓ ↓H␈↓        Before␈αdescribing␈αthe␈αformal␈αdetails␈αof␈αthe␈α
ideas␈αused␈αin␈αthe␈αprevious␈αsection␈αit␈α
is␈αperhaps
␈↓ ↓H␈↓appropriate␈αto␈αgive␈αa␈αpreview␈αof␈αwhat␈αwe␈αare␈αabout␈αto␈αdo␈αand␈αsome␈αexplanation␈αas␈αto␈αwhy.␈α The
␈↓ ↓H␈↓main␈α
point␈αis␈α
to␈αbe␈α
able␈αto␈α
make␈αthe␈α
arguments␈α
clear␈αand␈α
precise.␈α This␈α
wins␈αin␈α
several␈αways.␈α
 For
␈↓ ↓H␈↓the␈α
logicians␈α
the␈αdevelopment␈α
of␈α
formal␈α
systems␈αand␈α
methods␈α
of␈αreasoning␈α
allowed␈α
them␈α
to␈αdeal
␈↓ ↓H␈↓with␈α∂paradoxes␈α∂which␈α∂arose␈α⊂in␈α∂informal␈α∂arguments␈α∂and␈α⊂also␈α∂to␈α∂prove␈α∂many␈α⊂interesting␈α∂things
␈↓ ↓H␈↓about␈α⊂their␈α⊂systems.␈α⊂ For␈α∂the␈α⊂computer␈α⊂scientist␈α⊂formalization␈α∂provides␈α⊂a␈α⊂way␈α⊂of␈α⊂making␈α∂your
␈↓ ↓H␈↓notions␈α∞and␈α∂reasoning␈α∞precise␈α∂enough␈α∞to␈α∂be␈α∞understood␈α∂and␈α∞manipulated␈α∂by␈α∞a␈α∂computer.␈α∞ One
␈↓ ↓H␈↓goal, of course, it to have the computer do as much of the work for you as possible.

␈↓ ↓H␈↓        Our␈α
particular␈α
goal␈α
is␈α
a␈α
system␈α
in␈α
which␈α
we␈α
can␈α
prove␈α
facts␈α
about␈α
LISP␈α
programs␈αand␈α
have
␈↓ ↓H␈↓our␈α
proofs␈αchecked␈α
on␈αa␈α
computer.␈α What␈α
do␈αwe␈α
need␈α
in␈αsuch␈α
a␈αsystem?␈α
 First␈αwe␈α
need␈αa␈α
language
␈↓ ↓H␈↓in␈α∃which␈α∃to␈α∃express␈α∀facts␈α∃about␈α∃S-expressions␈α∃and␈α∀about␈α∃functions␈α∃and␈α∃predicates␈α∃on␈α∀S-
␈↓ ↓H␈↓expressions.␈α
  We␈α
will␈α
assign␈α
"meaning"␈α
to␈α
some␈α
of␈α
the␈α
expressions␈α
of␈α
our␈α
language␈αby␈α
interpreting
␈↓ ↓H␈↓them␈α
as␈α
elements␈α
of␈α
the␈α
domain␈α
of␈α
extended␈α
S-expressions␈α
(S-expressions␈α
∪␈α
{␈↓π|␈↓})␈α
or␈α
as␈αfunctions␈α
or
␈↓ ↓H␈↓predicates␈α
on␈αthis␈α
domain.␈α Some␈α
of␈αthe␈α
expressions␈α
(sentences)␈αwill␈α
be␈αinterpreted␈α
as␈αhaving␈α
truth
␈↓ ↓H␈↓values.

␈↓ ↓H␈↓        Having␈α∩specified␈α⊃our␈α∩language␈α∩and␈α⊃given␈α∩meaning␈α⊃to␈α∩some␈α∩of␈α⊃the␈α∩expressions␈α∩of␈α⊃this
␈↓ ↓H␈↓language␈α
we␈αhave␈α
in␈αsome␈α
sense␈αspecified␈α
which␈αsentences␈α
are␈αtrue.␈α
 Given␈αa␈α
particular␈αsentence
␈↓ ↓H␈↓we␈αcan␈α
try␈αto␈α
decide␈αwhether␈αor␈α
not␈αit␈α
is␈αtrue␈αof␈α
our␈αextended␈α
S-expression␈αdomain.␈α However␈α
the
␈↓ ↓H␈↓argument␈α
would␈α∞still␈α
be␈α
informal␈α∞and␈α
it␈α
would␈α∞not␈α
be␈α
easy␈α∞to␈α
check␈α
that␈α∞it␈α
is␈α
valid.␈α∞  Thus␈α
the
␈↓ ↓H␈↓next␈αstep␈αis␈αto␈αdevelop␈αa␈αformal␈αmethod␈αof␈αproof.␈α The␈αidea␈αis␈αto␈αfind␈αa␈αset␈αof␈αrules␈αfor␈αderiving
␈↓ ↓H␈↓54␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓true␈αsentences␈αfrom␈αa␈α
given␈αcollection␈αof␈αsentences␈αby␈α
successive␈αapplication␈αof␈αthe␈αrules.␈α
 Such␈αa
␈↓ ↓H␈↓derivation␈αis␈αthen␈αa␈αformal␈αproof␈αthat␈αthe␈αsentence␈αderived␈αfollows␈αfrom␈αthe␈αgiven␈αsentences.␈α For
␈↓ ↓H␈↓a␈α
notion␈α
of␈α
formal␈α∞proof␈α
to␈α
be␈α
useful␈α∞we␈α
will␈α
need␈α
to␈α
find␈α∞a␈α
collection␈α
of␈α
sentences␈α∞or␈α
"axioms"
␈↓ ↓H␈↓which␈α∞will␈α∞characterize␈α∞the␈α
domain␈α∞we␈α∞are␈α∞studying␈α
in␈α∞the␈α∞sense␈α∞that␈α
the␈α∞true␈α∞sentences␈α∞can␈α
be
␈↓ ↓H␈↓derived␈α⊗from␈α⊗the␈α⊗axioms␈α∃using␈α⊗the␈α⊗given␈α⊗proof␈α⊗rules.␈α∃ (The␈α⊗sad␈α⊗fact␈α⊗is␈α⊗that␈α∃completely
␈↓ ↓H␈↓characterizing␈α∞a␈α∞domain␈α∞as␈α∞rich␈α∞as␈α∂S-expressions␈α∞by␈α∞a␈α∞finite␈α∞collection␈α∞of␈α∞axioms␈α∂is␈α∞impossible;
␈↓ ↓H␈↓however we can come close enough to provide a useful system.)

␈↓ ↓H␈↓        The␈αabove␈α
may␈αseem␈α
like␈αa␈αformidable␈α
task,␈αhowever␈α
we␈αare␈αin␈α
luck.␈α Most␈α
of␈αthe␈αwork␈α
has
␈↓ ↓H␈↓been␈α⊂done␈α⊂already␈α⊂as␈α⊂our␈α⊂requirements␈α⊂are␈α⊃met␈α⊂nicely␈α⊂by␈α⊂what␈α⊂is␈α⊂known␈α⊂as␈α⊃first-order␈α⊂logic.
␈↓ ↓H␈↓Logicians␈αhave␈αdeveloped␈αa␈αnotion␈αof␈αfirst-order␈αlogic␈αwith␈αthe␈αform␈αof␈αthe␈αlanguage␈αdetermined,
␈↓ ↓H␈↓a␈α
method␈α
of␈α
assigning␈α
meaning␈α
to␈α
expressions␈α
of␈α
the␈α
language␈α
(model␈α
theory),␈α
and␈α
a␈α"complete"␈α
set
␈↓ ↓H␈↓of␈α
proof␈α
rules␈α
for␈α∞proving␈α
sentences␈α
of␈α
the␈α∞language␈α
true.␈α
 Our␈α
task␈α∞is␈α
reduced␈α
to␈α
filling␈α∞in␈α
the
␈↓ ↓H␈↓details␈α
of␈α
the␈α
language␈α
specification␈α
and␈α
deciding␈α
on␈α
a␈α
collection␈α
of␈α
axioms.␈α
 We␈α
will␈α
do␈α
this␈αin
␈↓ ↓H␈↓the following sections.   We shall also discuss proof rules in more detail.

␈↓ ↓H␈↓        At␈αthis␈αpoint␈αwe␈αwill␈αhave␈αdeveloped␈αa␈αsystem␈αin␈αwhich␈αwe␈αstate␈αand␈αprove␈αfacts␈αabout␈αthe
␈↓ ↓H␈↓extended␈αdomain␈αof␈αS-expressions␈αand␈αthe␈αprimitive␈αfunctions␈αand␈αpredicates.␈α We␈αreally␈αwish␈αto
␈↓ ↓H␈↓be␈αable␈αto␈αstate␈αand␈αprove␈αfacts␈αabout␈αprograms␈αwe␈αhave␈αwritten.␈α For␈αthe␈αtime␈αbeing␈αwe␈α
will␈αbe
␈↓ ↓H␈↓satisfied␈αwith␈αproving␈αthings␈αabout␈αthe␈αfunction␈α
computed␈αby␈αa␈αgiven␈αprogram.␈α Thus␈αwe␈αneed␈α
to
␈↓ ↓H␈↓be␈αable␈αto␈αassociate␈αa␈αparticular␈αfunction␈αwith␈αeach␈αprogram␈αand␈αto␈αcharacterize␈αthis␈αfunction␈αby
␈↓ ↓H␈↓sentences␈α∞in␈α∞the␈α∂language,␈α∞which␈α∞we␈α∞then␈α∂add␈α∞as␈α∞additional␈α∞axioms.␈α∂ First␈α∞we␈α∞must␈α∂restrict␈α∞the
␈↓ ↓H␈↓general␈αform␈α(schema)␈αfor␈αrecursive␈αdefinition␈αso␈αthat␈αwe␈αcan␈αalways␈αchoose␈αon␈αthe␈αcorresponding
␈↓ ↓H␈↓function␈α∂in␈α∞a␈α∂uniform␈α∞manner.␈α∂  We␈α∞choose␈α∂as␈α∞the␈α∂corresponding␈α∞function,␈α∂the␈α∂function␈α∞whose
␈↓ ↓H␈↓value␈αis␈αthat␈αdetermined␈α
by␈αthe␈αprogram␈αif␈α
it␈αterminates␈αand␈αwhose␈α
value␈αis␈α␈↓π|␈↓␈αotherwise.␈α
 Having
␈↓ ↓H␈↓made␈α⊃a␈α⊃suitable␈α⊃choice␈α⊃ of␈α⊃allowed␈α⊃programs,␈α∩we␈α⊃can␈α⊃then␈α⊃introduce␈α⊃as␈α⊃an␈α⊃axiom␈α∩the␈α⊃(fully
␈↓ ↓H␈↓quantified)␈α∞equation␈α∞obtained␈α∂replacing␈α∞"←"␈α∞in␈α∂the␈α∞recursive␈α∞definition␈α∞by␈α∂"="␈α∞.␈α∞If␈α∂the␈α∞program
␈↓ ↓H␈↓always␈α
terminates␈α
with␈α
an␈α
S-expression␈α
value␈αthen␈α
this␈α
equation␈α
is␈α
sufficient␈α
to␈α
characterize␈αthe
␈↓ ↓H␈↓function␈α∂completely.␈α∞ Otherwise␈α∂we␈α∂need␈α∞to␈α∂add␈α∞additional␈α∂axioms␈α∂expressing␈α∞the␈α∂fact␈α∂that␈α∞the
␈↓ ↓H␈↓function␈α
is␈αnot␈α
only␈α
a␈αsolution␈α
to␈α
the␈αequation,␈α
but␈α
that␈αits␈α
value␈α
is␈αan␈α
S-expression␈α
only␈αwhen␈α
the
␈↓ ↓H␈↓the␈α∃computation␈α∃procedure␈α⊗actually␈α∃returns␈α∃an␈α∃S-expression.␈α⊗ This␈α∃collection␈α∃of␈α⊗axioms␈α∃is
␈↓ ↓H␈↓generated␈α
from␈α
a␈α
schema␈α
called␈α
the␈α
␈↓↓minimization␈αschema␈↓.␈α
 The␈α
name␈α
refers␈α
to␈α
the␈α
fact␈αthe␈α
function
␈↓ ↓H␈↓being described is the least defined or minimal solution to the functional equation.

␈↓ ↓H␈↓        Again␈αit␈αseems␈αthat␈αwe␈αhave␈αa␈αfair␈αammount␈αof␈αwork␈αto␈αdo.␈α But,␈αwe␈αwill␈αbe␈αable␈αto␈αuse␈αor
␈↓ ↓H␈↓parallel several results of recursive function theory to reach our goal.

␈↓ ↓H␈↓        We␈α
conclude␈α
this␈α
section␈α
with␈α
a␈α
summary␈α
of␈α
steps␈α
to␈α
be␈α
carried␈α
out␈α
in␈α
order␈α
to␈α
describe␈α
fully
␈↓ ↓H␈↓our "theory of LISP".  They are

␈↓ ↓H␈↓␈↓ αH1.  Define a language and semantics
␈↓ ↓H␈↓␈↓ αH2.  Give axioms characterizing extended S-expressions
␈↓ ↓H␈↓␈↓ αH3.  Give rules for proving
␈↓ ↓H␈↓␈↓ αH4.  Specify the allowed programs
␈↓ ↓H␈↓␈↓ αH5.  Characterize the function corresponding to an allowed program .

␈↓ ↓H␈↓In␈αthe␈αremainder␈αof␈αthis␈αchapter␈αwe␈αwill␈αcarry␈αout␈αthese␈αsteps.␈α We␈αwill␈αalso␈αgive␈αseveral␈αexample
␈↓ ↓H␈↓proofs␈α
using␈α
the␈α
techniques␈α
we␈α
are␈α∞describing.␈α
 The␈α
reader␈α
unfamiliar␈α
with␈α
formal␈α∞systems␈α
may
␈↓ ↓H␈↓find␈α⊃the␈α⊃formal␈α⊃description␈α⊃and␈α⊃development␈α⊃of␈α∩the␈α⊃proof␈α⊃system␈α⊃a␈α⊃bit␈α⊃heavy.␈α⊃ One␈α∩way␈α⊃to
␈↓ ↓H␈↓approach␈αit␈αis␈αto␈αskim␈αthrough␈αthe␈αformalities,␈αkeeping␈αin␈αmind␈αthe␈αplan␈αand␈αmotivation␈αoutlined
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *55


␈↓ ↓H␈↓above.␈α
 Then␈αput␈α
serious␈αeffort␈α
into␈αunderstanding␈α
the␈αexamples.␈α
 After␈αobtaining␈α
some␈αfacility␈α
in
␈↓ ↓H␈↓using the methods the formalities should be more meaningful and easier to understand.



␈↓ ↓H␈↓3.  ␈↓αTheory of LISP: the Language.␈↓


␈↓ ↓H␈↓        The␈αusual␈αfirst-order␈αlogic␈α
has␈αa␈αlanguage␈αconsisting␈αof␈α
terms␈αand␈αformulas␈αbuilt␈αfrom␈α
four
␈↓ ↓H␈↓collections␈αof␈α(non-logical)␈αsymbols:␈αconstants,␈αvariables,␈αfunctions␈αsymbols␈αand␈α
predicate␈αsymbols,
␈↓ ↓H␈↓using␈α⊂application␈α∂(of␈α⊂a␈α∂function␈α⊂or␈α⊂predicate␈α∂to␈α⊂a␈α∂list␈α⊂of␈α∂terms),␈α⊂propositional␈α⊂connectives␈α∂and
␈↓ ↓H␈↓quantifiers.␈α∩  A␈α∪term␈α∩is␈α∩an␈α∪expression␈α∩whose␈α∩value␈α∪(meaning)␈α∩will␈α∩be␈α∪an␈α∩object␈α∩like␈α∪an␈α∩S-
␈↓ ↓H␈↓expression␈α
while␈α
a␈α
formula␈α
is␈α
either␈α
true␈α
or␈α
false␈α
for␈α
any␈α
particular␈α
assignment␈α
of␈α
values␈α
to␈αthe
␈↓ ↓H␈↓variables␈α⊃occuring␈α⊃free.␈α⊃ A␈α⊃first-order␈α⊃logic␈α⊃with␈α⊃equality␈α⊃has␈α⊃among␈α⊃its␈α⊃predicate␈α⊃symbols␈α⊃a
␈↓ ↓H␈↓binary symbol (often "=") whose interpretation is that of equality between objects of the domain.

␈↓ ↓H␈↓        The␈αlogic␈α
we␈αshall␈α
use␈αis␈α
a␈αfirst␈α
order␈αlogic␈α
with␈αequality,␈α
extended␈αby␈αadmitting␈α
conditional
␈↓ ↓H␈↓expressions␈α
and␈α
first␈αorder␈α
lambda-expressions.␈α
 Our␈α
language␈αwill␈α
have␈α
function␈α
and␈αpredicate
␈↓ ↓H␈↓expressions␈αin␈αaddition␈αto␈αterms␈αand␈αformulas.␈α These␈αexpressions␈αwill␈αbe␈αtreated␈αlike␈αcomplicated
␈↓ ↓H␈↓function␈α⊃and␈α∩predicate␈α⊃symbols.␈α⊃ These␈α∩extensions␈α⊃do␈α∩not␈α⊃change␈α⊃the␈α∩logical␈α⊃strength␈α∩of␈α⊃the
␈↓ ↓H␈↓theory,␈αbecause,␈αas␈αwe␈αshall␈αsee,␈αevery␈αformula␈αthat␈αincludes␈αconditional␈αexpressions␈αor␈αfirst␈αorder
␈↓ ↓H␈↓lambdas␈α
can␈α
be␈αtransformed␈α
into␈α
an␈αequivalent␈α
formula␈α
without␈αthem.␈α
 However,␈α
the␈αextensions
␈↓ ↓H␈↓are␈αpractically␈α
important,␈αbecause␈αthey␈α
permit␈αus␈α
to␈αuse␈αrecursive␈α
definitions␈αdirectly␈α
as␈αformulas
␈↓ ↓H␈↓of␈α
the␈α
logic.␈α Also␈α
we␈α
will␈αhave␈α
various␈α
"sorts"␈α
of␈αvariables,␈α
each␈α
sort␈αrestricted␈α
to␈α
range␈αover␈α
some
␈↓ ↓H␈↓subset␈α
of␈α
the␈α
domain␈α
of␈α
interest␈α
rather␈α
than␈α
over␈α
all␈α
objects.␈α
 We␈α
can␈α
then␈α
restrict␈α
the␈α
domain␈α
of␈α
a
␈↓ ↓H␈↓function␈α⊂to␈α⊂some␈α⊂fixed␈α⊃subdomian.␈α⊂ This␈α⊂is␈α⊂mainly␈α⊂a␈α⊃formal␈α⊂shorthand,␈α⊂it␈α⊂allows␈α⊂us␈α⊃to␈α⊂write
␈↓ ↓H␈↓formulas␈αthat␈αare␈αmuch␈αshorter␈α
and␈αeasier␈αto␈αread.␈α Again␈α
any␈αformula␈αwritten␈αin␈αthis␈αshort␈α
hand
␈↓ ↓H␈↓notation␈αcan␈αbe␈αexpanded␈αin␈αterms␈αof␈αunrestricted␈αvariables.␈α When␈αusing␈αthe␈αshorthand␈αnotation
␈↓ ↓H␈↓one␈αneeds␈αto␈αbe␈αsomewhat␈αmore␈αcareful␈αin␈αapplying␈αthe␈αproof␈αrules,␈αbut␈αit␈αseems␈αto␈αbe␈αworth␈αthe
␈↓ ↓H␈↓effort␈α∂in␈α∂the␈α∂long␈α∂run.␈α∂ We␈α∂have␈α∂in␈α∞fact␈α∂been␈α∂using␈α∂the␈α∂sorting␈α∂conventions␈α∂informally␈α∂in␈α∞the
␈↓ ↓H␈↓preceeding␈α
chapters,␈αso␈α
you␈αare␈α
probably␈α
already␈αused␈α
to␈αthem.␈α
 For␈α
example,␈αwe␈α
use␈αthe␈α
variables
␈↓ ↓H␈↓␈↓↓u␈↓␈αand␈α
␈↓↓v␈↓␈αto␈α
denote␈αlists,␈α
and␈αour␈α
recursive␈αdefinitions␈αfor␈α
functions␈αon␈α
lists␈αare␈α
given␈αin␈α
terms␈αof
␈↓ ↓H␈↓these list variables to indicate that we are not specifying what the result will be for non-list input.

␈↓ ↓H␈↓        The four classes of symbols of our language are as follows.

␈↓ ↓H␈↓␈↓αConstants␈↓:␈αWe␈αwill␈αuse␈αS-expresssions␈αas␈αconstants␈αstanding␈αfor␈αthemselves.␈α In␈αparticular␈αwe␈αwill
␈↓ ↓H␈↓have␈α∞the␈α∞atom␈α∞␈↓¬NIL␈↓.␈α∞We␈α∞will␈α∞use␈α∞the␈α
usual␈α∞symbols␈α∞␈↓¬0,␈α∞␈↓␈↓¬1,␈α∞␈↓...,␈α∞for␈α∞atoms␈α∞that␈α∞represent␈α
numbers.
␈↓ ↓H␈↓Among the non-S-expression  constants we will have ␈↓π|␈↓ (read "bottom").

␈↓ ↓H␈↓␈↓αVariables␈↓:␈α∞We␈α∞will␈α∞use␈α∞the␈α∂letters␈α∞␈↓↓X,␈↓␈α∞␈↓↓Y,␈↓␈α∞␈↓↓Z␈↓␈α∞as␈α∂general␈α∞variables␈α∞ranging␈α∞over␈α∞the␈α∂extended␈α∞S-
␈↓ ↓H␈↓expressions␈α
 (the␈α
set␈α
consisting␈α
of␈α
S-expressions␈α
togther␈α
with␈α
the␈α
constant␈α
␈↓π|␈↓).␈α
 The␈α
variables␈α
␈↓↓u,␈↓␈α
␈↓↓v,␈↓
␈↓ ↓H␈↓␈↓↓w␈↓␈α∂ will␈α∂range␈α∂over␈α∞lists,␈α∂while␈α∂␈↓↓x,␈↓␈α∂␈↓↓y,␈↓␈α∂␈↓↓z␈↓␈α∞will␈α∂range␈α∂over␈α∂S-expressions.␈α∞ ␈↓↓k,␈↓␈α∂␈↓↓l,␈↓␈α∂␈↓↓m,␈↓␈α∂␈↓↓n␈↓␈α∂are␈α∞integer
␈↓ ↓H␈↓variables.  The variables may appear with or without subscripts.

␈↓ ↓H␈↓␈↓αFunction␈α
symbols␈↓:␈α
 The␈α
collection␈αof␈α
function␈α
symbols␈α
contains␈α
the␈αLISP␈α
function␈α
names␈α
␈↓αa␈↓,␈α␈↓αd␈↓␈α
and
␈↓ ↓H␈↓"."␈α(as␈αan␈αinfix).␈α
 Other␈αfunction␈αsymbols␈αincluding␈α
function␈αparameter␈αsymbols␈αwill␈αbe␈α
introduced
␈↓ ↓H␈↓as needed.

␈↓ ↓H␈↓␈↓αPredicate␈α
symbols␈↓:␈α
As␈α
predicate␈αconstant␈α
symbols␈α
we␈α
will␈α
use␈αthe␈α
logical␈α
equality␈α
symbol␈α
=,␈αand
␈↓ ↓H␈↓56␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓the␈αLISP␈αpredicate␈αnames␈α
␈↓αat␈↓,␈α␈↓αn␈↓␈αand␈α␈↓αeq␈↓.␈α We␈α
will␈αuse␈α␈↓πF␈↓␈αas␈α
a␈αpredicate␈αparameter␈αsymbol.␈α We␈α
will
␈↓ ↓H␈↓also␈α∞use␈α∞the␈α∞unary␈α
predicate␈α∞symbols␈α∞ ␈↓↓sexp,␈↓␈α∞␈↓↓list␈↓␈α∞and␈α
␈↓↓natnum.␈↓␈α∞ Other␈α∞predicate␈α∞symbols␈α∞will␈α
be
␈↓ ↓H␈↓introduced when needed.

␈↓ ↓H␈↓        We␈α∂suppose␈α∂that␈α∂each␈α∂function␈α∂and␈α∞predicate␈α∂symbol␈α∂takes␈α∂the␈α∂same␈α∂definite␈α∂number␈α∞of
␈↓ ↓H␈↓arguments␈αevery␈α
time␈αit␈αis␈α
used.␈α (This␈α
number␈αis␈αcalled␈α
the␈α"arity".)␈α We␈α
will␈αuse␈α
informally␈αthe
␈↓ ↓H␈↓notation␈α
such␈α
as␈α
␈↓↓<x␈↓β1␈↓↓,␈α
...␈α,x␈↓βn␈↓↓>␈↓␈α
to␈α
represent␈α
the␈α
list␈αforming␈α
functions,␈α
 but␈α
formally␈α
this␈αshould␈α
be
␈↓ ↓H␈↓considered␈α
as␈αan␈α
abbreviation␈αfor␈α
␈↓↓x␈↓β1␈↓↓␈α.␈α
[␈α...␈α
[x␈↓βn␈↓↓␈α.␈α
␈↓¬NIL␈↓↓]␈α...␈α
]]␈↓.␈α  We␈α
will␈αoften␈α
use␈α
one␈αargument
␈↓ ↓H␈↓function␈αand␈αpredicate␈αsymbols␈αas␈αprefixes,␈αi.e.␈α without␈αbrackets,␈αjust␈αas␈α␈↓αa␈↓,␈α␈↓αd␈↓,␈αetc.␈αhave␈αbeen␈αused
␈↓ ↓H␈↓up␈α∂to␈α∂now.␈α∂ Two␈α∂argument␈α∂function␈α∂and␈α∂predicate␈α∂symbols␈α∂will␈α∂also␈α∂be␈α∂used␈α∂as␈α⊂infixes␈α∂where
␈↓ ↓H␈↓customary.

␈↓ ↓H␈↓        Next␈αwe␈αdefine␈αterms,␈αformulas,␈αfunction␈αexpressions␈αand␈αpredicate␈αexpressions␈αinductively.
␈↓ ↓H␈↓Terms␈α⊂are␈α⊂used␈α⊂in␈α⊂making␈α⊂formulas,␈α⊂and␈α⊂formulas␈α⊂occur␈α⊂in␈α⊂terms␈α⊂so␈α⊂that␈α⊂the␈α⊂definitions␈α∂are
␈↓ ↓H␈↓␈↓↓mutually␈↓␈α∞␈↓↓recursive␈↓␈α
where␈α∞this␈α∞use␈α
of␈α∞the␈α
word␈α∞␈↓↓recursive␈↓␈α∞should␈α
be␈α∞distinguished␈α
from␈α∞its␈α∞use␈α
in
␈↓ ↓H␈↓recursive␈α∞definitions␈α∂of␈α∞functions.␈α∂ Function␈α∞and␈α∞predicate␈α∂expressions␈α∞are␈α∂also␈α∞involved␈α∂in␈α∞the
␈↓ ↓H␈↓mutual recursion.

␈↓ ↓H␈↓␈↓αTerms␈↓:␈α⊂Constants␈α⊂are␈α⊂terms,␈α⊂and␈α⊂variables␈α⊂are␈α∂terms.␈α⊂ If␈α⊂␈↓πy␈↓␈α⊂is␈α⊂a␈α⊂function␈α⊂expression␈α⊂taking␈α∂␈↓↓n␈↓
␈↓ ↓H␈↓arguments,␈α
and␈α
␈↓↓t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓␈↓␈α
are␈α
terms,␈α
then␈α
␈↓↓␈↓πy␈↓↓[t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓␈α
is␈α
a␈α
term.␈α
 If␈α
␈↓↓s␈↓␈α
is␈α
a␈α
formula␈α
and␈α
␈↓↓t␈↓β1␈↓↓␈↓␈αand␈α
␈↓↓t␈↓β2␈↓↓␈↓
␈↓ ↓H␈↓are terms, then [␈↓↓␈↓αif␈↓↓ s ␈↓αthen␈↓↓ t␈↓β1␈↓↓ ␈↓αelse␈↓↓ t␈↓β2␈↓↓␈↓] is a term.  Some examples of terms in our language are:

␈↓ ↓H␈↓␈↓ ∧8(i)   ␈↓¬NIL␈↓
␈↓ ↓H␈↓␈↓ ∧8(ii)   ␈↓↓x␈↓
␈↓ ↓H␈↓␈↓ ∧8(iii)   ␈↓↓␈↓αa|␈↓↓x␈↓
␈↓ ↓H␈↓␈↓ ∧8(iv)   ␈↓↓[λx: ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ z ␈↓αelse␈↓↓ ␈↓αd|␈↓↓x][assoc[z,u]]␈↓


␈↓ ↓H␈↓␈↓αFunction␈α∞expressions␈↓:␈α
A␈α∞function␈α
symbol␈α∞is␈α
a␈α∞function␈α
expression.␈α∞ If␈α
␈↓↓␈↓πx␈↓↓␈↓β1␈↓↓, ... ,␈↓πx␈↓↓␈↓βn␈↓↓␈↓␈α∞are␈α
variables
␈↓ ↓H␈↓and ␈↓↓t␈↓ is a term, then ␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓, ... ,␈↓πx␈↓↓␈↓βn␈↓↓: t]␈↓ is a function expression.

␈↓ ↓H␈↓␈↓αPredicate␈αexpressions␈↓:␈αA␈αpredicate␈αsymbol␈αis␈αa␈αpredicate␈αexpression.␈α If␈α␈↓↓␈↓πx␈↓↓␈↓β1␈↓↓, ... ,␈↓πx␈↓↓␈↓βn␈↓↓␈↓␈αare␈αvariables
␈↓ ↓H␈↓and ␈↓↓s␈↓ is a formula, then ␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓, ... ,␈↓πx␈↓↓␈↓βn␈↓↓: s]␈↓ is a predicate expression.

␈↓ ↓H␈↓Some examples of function and predicate expressions are:

␈↓ ↓H␈↓␈↓ ∧8(v)   qa
␈↓ ↓H␈↓␈↓ ∧8(vi)   ␈↓↓issexp␈↓
␈↓ ↓H␈↓␈↓ ∧8(vii)   ␈↓↓λx: ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ z ␈↓αelse␈↓↓ ␈↓αd|␈↓↓x␈↓


␈↓ ↓H␈↓␈↓αFormulas␈↓:␈αIf␈α␈↓↓t␈↓β1␈↓↓␈↓␈α
and␈α␈↓↓t␈↓β2␈↓↓␈↓␈αare␈α
terms␈αthen␈α␈↓↓t␈↓β1␈↓↓ = t␈↓β2␈↓↓␈↓␈α
is␈αa␈αformula.␈α
 If␈α␈↓πf␈↓␈αis␈α
a␈αpredicate␈αexpression␈αtaking␈α
␈↓↓n␈↓
␈↓ ↓H␈↓arguments␈α
and␈α
␈↓↓t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓␈↓␈α
are␈α
terms,␈α
then␈α
␈↓↓␈↓πf␈↓↓[t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓␈α
is␈αa␈α
formula.␈α
 If␈α
␈↓↓s␈↓␈α
is␈α
a␈α
formula,␈α
then␈α¬␈↓↓s␈↓␈α
is
␈↓ ↓H␈↓also␈αa␈αformula.␈α If␈α␈↓↓s␈↓β1␈↓↓␈↓␈αand␈α␈↓↓s␈↓β2␈↓↓␈↓␈αare␈αformulas,␈αthen␈α␈↓↓s␈↓β1␈↓↓␈↓ ∧ ␈↓↓s␈↓β2␈↓↓␈↓,␈α␈↓↓s␈↓β1␈↓↓␈↓ ∨ ␈↓↓s␈↓β2␈↓↓␈↓,␈α␈↓↓s␈↓β1␈↓↓␈↓ ⊃ ␈↓↓s␈↓β2␈↓↓␈↓,␈αand␈α␈↓↓s␈↓β1␈↓↓␈↓ ≡ ␈↓↓s␈↓β2␈↓↓␈↓␈αare␈αformulas.
␈↓ ↓H␈↓If␈α
␈↓↓s␈↓β0␈↓↓␈↓,␈α
␈↓↓s␈↓β1␈↓↓␈↓␈αand␈α
␈↓↓s␈↓β2␈↓↓␈↓␈α
are␈α
formulas,␈αthen␈α
[␈↓↓␈↓αif␈↓↓ s␈↓β0␈↓↓ ␈↓αthen␈↓↓ s␈↓β1␈↓↓ ␈↓αelse␈↓↓ s␈↓β2␈↓↓␈↓]␈α
is␈α
a␈αformula.␈α
 If␈α
␈↓↓␈↓πx␈↓↓␈↓β1␈↓↓, ..., ␈↓πx␈↓↓␈↓βn␈↓↓␈↓␈αare␈α
variables,
␈↓ ↓H␈↓and␈α∩␈↓↓s␈↓␈α∩is␈α∪a␈α∩formula,␈α∩then␈α∪␈↓↓[∃␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: s]␈↓␈α∩and␈α∩␈↓↓[∀␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: s]␈↓␈α∪are␈α∩formulas.␈α∩ Some␈α∪examples␈α∩of
␈↓ ↓H␈↓formulas are:
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *57



␈↓ ↓H␈↓␈↓ ∧8(viii)   ␈↓↓␈↓αa|␈↓↓x = ␈↓αd|␈↓↓x␈↓
␈↓ ↓H␈↓␈↓ ∧8(ix)   ␈↓↓issexp X␈↓
␈↓ ↓H␈↓␈↓ ∧8(x)   ␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ ␈↓αtrue␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u = ␈↓αad|␈↓↓u␈↓
␈↓ ↓H␈↓␈↓ ∧8(xi)   ␈↓↓∀x: [¬␈↓αat|␈↓↓x ⊃ ∃y z: x = y . z]␈↓


␈↓ ↓H␈↓        An␈αoccurrence␈α
of␈αa␈αvariable␈α
␈↓πx␈↓␈αis␈αcalled␈α
␈↓↓bound␈↓␈αif␈α
it␈αis␈αin␈α
an␈αexpression␈αof␈α
one␈αof␈α
the␈αforms
␈↓ ↓H␈↓␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: t]␈↓,␈α
␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: s]␈↓,␈α
␈↓↓[∀␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: s]␈↓␈α
or␈α
␈↓↓[∃␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: s]␈↓␈α
where␈α␈↓πx␈↓␈α
is␈α
one␈α
of␈α
the␈α
numbered␈α␈↓πx␈↓'s.
␈↓ ↓H␈↓An␈α
occurrence␈α
that␈α
is␈α
not␈α
bound␈α
is␈α
called␈α
␈↓↓free.␈↓␈α
 A␈α
formula␈α
having␈α
no␈α
free␈α
variables␈α
is␈α∞called␈α
a
␈↓ ↓H␈↓␈↓↓sentence.␈↓␈α Thus␈α␈↓↓x␈↓␈αis␈αbound␈αin␈αexample␈α(iv)␈α(of␈αterms)␈αbut␈α␈↓↓z␈↓␈αand␈α␈↓↓u␈↓␈αare␈αnot.␈α ␈↓↓u␈↓␈αis␈αfree␈αin␈αexample
␈↓ ↓H␈↓(x) and example (xi) is a sentence.



␈↓ ↓H␈↓4.  ␈↓αTheory of LISP: the Semantics.␈↓


␈↓ ↓H␈↓        The␈α␈↓↓semantics␈↓␈αof␈αfirst␈αorder␈αlogic␈αconsists␈αof␈αthe␈αrules␈αthat␈αdetermine␈αwhether␈αa␈αformula␈αis
␈↓ ↓H␈↓true␈αor␈αfalse.␈α However,␈αthe␈αtruth␈αor␈αfalsity␈αof␈αa␈αformula␈αis␈αrelative␈αto␈αthe␈αinterpretation␈αassigned
␈↓ ↓H␈↓to␈α∞the␈α∞constants,␈α∞free␈α∞variables,␈α∞function␈α∞and␈α
predicate␈α∞symbols␈α∞of␈α∞the␈α∞formula␈α∞as␈α∞objects␈α∞of,␈α
or
␈↓ ↓H␈↓functions and predicats on, some domain.

␈↓ ↓H␈↓        The␈αdomain␈αwe␈αare␈αinterested␈αin␈αis␈αthe␈αextended␈αS-expressions,␈αeg.␈α S-expressions␈αtogether
␈↓ ↓H␈↓with␈α⊃the␈α⊂element␈α⊃␈↓π|␈↓.␈α⊃  We␈α⊂assign␈α⊃meaning␈α⊂to␈α⊃an␈α⊃expression␈α⊂of␈α⊃our␈α⊂language␈α⊃inductively␈α⊃in␈α⊂a
␈↓ ↓H␈↓manner␈αsimilar␈αto␈αthe␈α
method␈αof␈αdefining␈αthe␈αlanguage␈α
itself.␈α Terms␈αwill␈αdenote␈αS-expressions␈α
or
␈↓ ↓H␈↓␈↓π|␈↓,␈α→function␈α→expressions␈α→will␈α→be␈α→assigned␈α→functions␈α→on␈α→extended␈α~S-expressions,␈α→predicate
␈↓ ↓H␈↓expressions␈α↔will␈α↔be␈α↔assigned␈α↔predicates␈α↔on␈α↔extended␈α↔S-expressions,␈α↔and␈α↔formulas␈α↔will␈α↔be
␈↓ ↓H␈↓determined to be true or false.

␈↓ ↓H␈↓        The␈α∞meaning␈α∂assigned␈α∞to␈α∞constants,␈α∂and␈α∞to␈α∂function␈α∞and␈α∞predicate␈α∂symbols␈α∞will␈α∂be␈α∞fixed.
␈↓ ↓H␈↓The␈α∀symbol␈α∀␈↓π|␈↓␈α∃and␈α∀any␈α∀S-expression␈α∃constants␈α∀appearing␈α∀in␈α∃an␈α∀expression␈α∀will␈α∃stand␈α∀for
␈↓ ↓H␈↓themselves.␈α Each␈αfunction␈αor␈α
predicate␈αconstant␈αsymbol␈αis␈αassigned␈α
a␈αfunction␈αor␈αpredicate␈αon␈α
the
␈↓ ↓H␈↓domain.␈α∃ We␈α∃will␈α⊗normally␈α∃assign␈α∃to␈α⊗the␈α∃basic␈α∃LISP␈α⊗function␈α∃and␈α∃predicate␈α⊗symbols␈α∃the
␈↓ ↓H␈↓corresponding␈α
basic␈α
LISP␈α
functions␈αand␈α
predicates.␈α
 Thus␈α
␈↓αa␈↓,␈α␈↓αd␈↓,␈α
".",␈α
␈↓αat␈↓,␈α
␈↓αn␈↓,␈αand␈α
␈↓αeq␈↓␈α
will␈α
have␈αtheir
␈↓ ↓H␈↓usual␈αmeaning.␈α The␈α
equality␈αsymbol,␈α"=",␈α
will␈αalso␈αhave␈αits␈α
usual␈αmeaning.␈α  The␈α
predicate␈α␈↓↓sexp␈↓
␈↓ ↓H␈↓will␈αbe␈αtrue␈αonly␈αfor␈αarguments␈αthat␈αdenote␈αS-expressions,␈αsimilarly␈α␈↓↓list␈↓␈αwill␈αbe␈αtrue␈αonly␈α
for␈αlists
␈↓ ↓H␈↓and␈α
␈↓↓numberp␈↓␈α
will␈α
be␈αtrue␈α
only␈α
for␈α
atoms␈αrepresenting␈α
numbers.␈α
 Function␈α
and␈αpredicate␈α
parameter
␈↓ ↓H␈↓symbols␈α
do␈α
not␈αget␈α
assigned␈α
any␈αmeaning.␈α
 Their␈α
use␈αis␈α
mainly␈α
in␈αwriting␈α
axiom␈α
schemata␈αfrom
␈↓ ↓H␈↓which␈α
an␈α
axiom␈α
can␈α
be␈α
obtained␈α
by␈α
substituting␈α
an␈α
actual␈α
function␈α
or␈α
predicate␈α
expression␈αfor␈α
the
␈↓ ↓H␈↓parameter.

␈↓ ↓H␈↓        A␈αparticular␈αinterpretation␈αis␈αdetermined␈αby␈αthe␈αvalues␈αassigned␈αto␈αvariables.␈α  In␈αprinciple
␈↓ ↓H␈↓to␈α∩completely␈α∩determine␈α⊃an␈α∩interpretation␈α∩each␈α⊃variable␈α∩must␈α∩be␈α⊃assigned␈α∩an␈α∩element␈α∩of␈α⊃the
␈↓ ↓H␈↓domain.␈α In␈αpractice␈α
we␈αwill␈αonly␈α
worry␈αabout␈αvariables␈α
appearing␈αfree␈αin␈α
the␈αexpression␈αto␈α
which
␈↓ ↓H␈↓we␈α∞are␈α∞interested␈α∂in␈α∞assigning␈α∞a␈α∞value.␈α∂ A␈α∞general␈α∞variable␈α∞maybe␈α∂assigned␈α∞any␈α∞element␈α∂of␈α∞the
␈↓ ↓H␈↓domain.␈α∂ A␈α∂variable␈α∂restricted␈α∂to␈α∂range␈α∂over␈α∂S-expressions␈α∂may␈α∂only␈α∂be␈α∂assigned␈α∞S-expression
␈↓ ↓H␈↓values.␈α Similarly␈αvariables␈αrestricted␈αto␈αrange␈αover␈αlists␈αor␈αnumbers␈αmay␈αonly␈αbe␈αassigned␈αvalues
␈↓ ↓H␈↓that are list or atoms denoting numbers, respectively.
␈↓ ↓H␈↓58␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓        If␈α␈↓πy␈↓␈αis␈αa␈αfunction␈αexpression␈αtaking␈α␈↓↓n␈↓␈αarguments,␈αand␈α␈↓↓t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓␈↓␈αare␈αterms,␈αthen␈αthe␈αvalue
␈↓ ↓H␈↓assigned␈α
to␈α
␈↓↓␈↓πy␈↓↓[t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓␈α
is␈α
the␈αvalue␈α
of␈α
the␈α
function␈α
assigned␈αto␈α
␈↓πy␈↓␈α
when␈α
applied␈α
to␈αarguments
␈↓ ↓H␈↓having␈αthe␈αvalues␈αassigned␈αto␈αthe␈αterms␈α␈↓↓t␈↓βi␈↓↓␈↓.␈α If␈α␈↓↓s␈↓␈αis␈αa␈αformula␈αand␈α␈↓↓t␈↓β1␈↓↓␈↓␈αand␈α␈↓↓t␈↓β2␈↓↓␈↓␈αare␈αterms,␈αthen␈αif␈α␈↓↓s␈↓␈αis
␈↓ ↓H␈↓true␈αthe␈αvalue␈αof␈αthe␈αterm␈α[␈↓↓␈↓αif␈↓↓ s ␈↓αthen␈↓↓ t␈↓β1␈↓↓ ␈↓αelse␈↓↓ t␈↓β2␈↓↓␈↓]␈αis␈αthe␈αvalue␈αof␈α ␈↓↓t␈↓β1␈↓↓␈↓␈αotherwise␈α(␈↓↓s␈↓␈αmust␈αbe␈αfalse␈αand)
␈↓ ↓H␈↓the value of the term is the value of ␈↓↓t␈↓β2␈↓↓␈↓.

␈↓ ↓H␈↓        ␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: e]␈↓␈α∂is␈α⊂assigned␈α∂a␈α⊂function␈α∂or␈α⊂predicate␈α∂according␈α⊂to␈α∂whether␈α⊂␈↓↓e␈↓␈α∂is␈α⊂a␈α∂term␈α⊂or␈α∂a
␈↓ ↓H␈↓formula.␈α The␈αvalue␈αof␈α␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: e][t␈↓β1␈↓↓,...,t␈↓βn␈↓↓]␈↓␈αis␈αobtained␈αby␈αevaluating␈αthe␈α␈↓↓t␈↓'s␈αand␈αusing␈αthese
␈↓ ↓H␈↓values␈α
as␈α
values␈α
of␈α
the␈α␈↓πx␈↓'s␈α
in␈α
the␈α
evaluation␈α
of␈α␈↓↓e.␈↓␈α
 If␈α
␈↓↓e␈↓␈α
has␈α
free␈αvariables␈α
in␈α
addition␈α
to␈α
the␈α␈↓πx␈↓'s,␈α
the
␈↓ ↓H␈↓function or predicate assigned will depend on them too.

␈↓ ↓H␈↓        If␈α
␈↓↓t␈↓β1␈↓↓␈↓␈α
and␈α
␈↓↓t␈↓β2␈↓↓␈↓␈α
are␈α
terms␈α
then␈α
␈↓↓t␈↓β1␈↓↓ = t␈↓β2␈↓↓␈↓␈α
is␈α
true␈α
exactly␈α
if␈α
␈↓↓t␈↓β1␈↓↓␈↓␈α
and␈α
␈↓↓t␈↓β2␈↓↓␈↓␈α
are␈α
are␈α
assigned␈α
the␈α
same␈α
value
␈↓ ↓H␈↓in␈αthe␈αdomain.␈α If␈α␈↓πf␈↓␈αis␈αa␈αpredicate␈αexpression␈αtaking␈α␈↓↓n␈↓␈αarguments␈αand␈α␈↓↓t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓␈↓␈αare␈αterms,␈αthen
␈↓ ↓H␈↓␈↓↓␈↓πf␈↓↓[t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓␈α∞is␈α
true␈α∞exactly␈α
when␈α∞the␈α
tuple␈α∞of␈α
values␈α∞assigned␈α
to␈α∞the␈α
␈↓↓t␈↓βi␈↓↓␈↓'s␈α∞satisfies␈α∞the␈α
predicate
␈↓ ↓H␈↓assigned␈αto␈α␈↓πf␈↓.␈α If␈α␈↓↓s␈↓␈αis␈αa␈αformula,␈αthen␈α¬␈↓↓s␈↓␈αis␈αtrue␈αexactly␈αwhen␈α␈↓↓s␈↓␈αis␈αfalse.␈αIf␈α␈↓↓s␈↓β1␈↓↓␈↓␈αand␈α␈↓↓s␈↓β2␈↓↓␈↓␈αare␈α
formulas,
␈↓ ↓H␈↓then␈αthe␈αtruthvalues␈αof␈αthe␈αformulas␈α␈↓↓s␈↓β1␈↓↓␈↓ ∧ ␈↓↓s␈↓β2␈↓↓␈↓,␈α␈↓↓s␈↓β1␈↓↓␈↓ ∨ ␈↓↓s␈↓β2␈↓↓␈↓,␈α␈↓↓s␈↓β1␈↓↓␈↓ ⊃ ␈↓↓s␈↓β2␈↓↓␈↓,␈αand␈α␈↓↓s␈↓β1␈↓↓␈↓ ≡ ␈↓↓s␈↓β2␈↓↓␈↓␈αare␈αdetermined␈αfrom␈αthe
␈↓ ↓H␈↓truth values of the formulas ␈↓↓s␈↓β1␈↓↓␈↓ and ␈↓↓s␈↓β2␈↓↓␈↓ according to the following table:

␈↓ ↓H␈↓␈↓ ∧λ␈↓↓s␈↓β1␈↓↓␈↓␈↓ ¬λ␈↓↓s␈↓β2␈↓↓␈↓␈↓ ελ∧␈↓ πλ∨␈↓ λλ⊃␈↓ 	λ≡

␈↓ ↓H␈↓␈↓ βx␈↓αtrue␈↓␈↓ ∧x␈↓αtrue␈↓␈↓ ¬x␈↓αtrue␈↓␈↓ εx␈↓αtrue␈↓␈↓ πx␈↓αtrue␈↓␈↓ λx␈↓αtrue␈↓
␈↓ ↓H␈↓␈↓ βx␈↓αtrue␈↓␈↓ ∧x␈↓αfalse␈↓␈↓ ¬x␈↓αfalse␈↓␈↓ εx␈↓αtrue␈↓␈↓ πx␈↓αfalse␈↓␈↓ λx␈↓αfalse␈↓
␈↓ ↓H␈↓␈↓ βx␈↓αfalse␈↓␈↓ ∧x␈↓αtrue␈↓␈↓ ¬x␈↓αfalse␈↓␈↓ εx␈↓αtrue␈↓␈↓ πx␈↓αtrue␈↓␈↓ λx␈↓αfalse␈↓
␈↓ ↓H␈↓␈↓ βx␈↓αfalse␈↓␈↓ ∧x␈↓αfalse␈↓␈↓ ¬x␈↓αfalse␈↓␈↓ εx␈↓αfalse␈↓␈↓ πx␈↓αtrue␈↓␈↓ λx␈↓αtrue␈↓


␈↓ ↓H␈↓␈↓ ∧≠␈↓αTable 3.␈↓  The semantics of logical connectives.


␈↓ ↓H␈↓If␈α␈↓↓s␈↓β0␈↓↓␈↓,␈α␈↓↓s␈↓β1␈↓↓␈↓␈α
and␈α␈↓↓s␈↓β2␈↓↓␈↓␈αare␈αformulas,␈α
then␈αif␈α␈↓↓s␈↓␈αis␈α
true,␈α[␈↓↓␈↓αif␈↓↓ s␈↓β0␈↓↓ ␈↓αthen␈↓↓ s␈↓β1␈↓↓ ␈↓αelse␈↓↓ s␈↓β2␈↓↓␈↓]␈αis␈αtrue␈α
exactly␈αwhen␈α␈↓↓s␈↓β1␈↓↓␈↓␈αis␈α
true
␈↓ ↓H␈↓and␈αif␈α␈↓↓s␈↓␈αis␈αfalse␈α[␈↓↓␈↓αif␈↓↓ s␈↓β0␈↓↓ ␈↓αthen␈↓↓ s␈↓β1␈↓↓ ␈↓αelse␈↓↓ s␈↓β2␈↓↓␈↓]␈αis␈αtrue␈αexactly␈αwhen␈α␈↓↓s␈↓β2␈↓↓␈↓␈αis␈αtrue.␈α If␈α␈↓↓␈↓πx␈↓↓␈↓β1␈↓↓, ..., ␈↓πx␈↓↓␈↓βn␈↓↓␈↓␈αare␈αvariables,
␈↓ ↓H␈↓and␈α␈↓↓s␈↓␈αis␈αa␈αformula,␈αthen␈α␈↓↓[∀␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: s]␈↓␈αis␈α␈↓αtrue␈↓␈αif␈αand␈αonly␈αif␈α␈↓↓s␈↓␈αis␈α␈↓αtrue␈↓␈αfor␈α␈↓↓all␈↓␈αallowed␈αassignments
␈↓ ↓H␈↓of␈α
elements␈αof␈α
the␈αdomain␈α
to␈αthe␈α
␈↓πx␈↓'s.␈α An␈α
assignment␈α
is␈αallowed␈α
if␈αthe␈α
variable␈αis␈α
general␈αor␈α
if␈αit␈α
is
␈↓ ↓H␈↓restricted␈α∂and␈α∂it␈α⊂is␈α∂assigned␈α∂a␈α∂value␈α⊂in␈α∂the␈α∂subdomain␈α⊂to␈α∂which␈α∂it␈α∂is␈α⊂restricted.␈α∂ If␈α∂␈↓↓s␈↓␈α⊂has␈α∂free
␈↓ ↓H␈↓variables␈α
that␈α
are␈α
not␈α
among␈α
the␈α
␈↓πx␈↓'s,␈α
then␈αthe␈α
truth␈α
of␈α
the␈α
formula␈α
depends␈α
on␈α
the␈αvalues␈α
assigned
␈↓ ↓H␈↓to␈α
these␈αremaining␈α
free␈αvariables.␈α
 ␈↓↓[∃␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: s]␈↓␈α
is␈αtrue␈α
if␈αand␈α
only␈α
if␈α␈↓↓s␈↓␈α
is␈αtrue␈α
for␈α
␈↓↓some␈↓␈αallowed
␈↓ ↓H␈↓assignment of elements of the domain to the ␈↓πx␈↓'s.  Free variables are handled just as before.

␈↓ ↓H␈↓        Those␈α
who␈αare␈α
familiar␈αwith␈α
the␈α
lambda␈αcalculus␈α
should␈αnote␈α
that␈α
λ␈αis␈α
being␈αused␈α
here␈αin␈α
a
␈↓ ↓H␈↓very␈α
limited␈α
way.␈α Namely,␈α
the␈α
variables␈αin␈α
a␈α
lambda-expression␈α
take␈αonly␈α
elements␈α
of␈αthe␈α
domain
␈↓ ↓H␈↓as␈α⊂values,␈α⊂whereas␈α∂the␈α⊂essence␈α⊂of␈α∂the␈α⊂lambda␈α⊂calculus␈α∂is␈α⊂that␈α⊂they␈α∂take␈α⊂arbitrary␈α⊂functions␈α∂as
␈↓ ↓H␈↓values.  We may call these restricted lambda expressions ␈↓↓first-order lambdas␈↓.
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *59


␈↓ ↓H␈↓5.  ␈↓αTheory of LISP: Algebraic Axioms.␈↓


␈↓ ↓H␈↓         We␈α∞now␈α∞turn␈α∞to␈α∞the␈α∞task␈α∞of␈α∞giving␈α∞the␈α∞axioms␈α∞of␈α∞our␈α∞theory␈α∞of␈α∞LISP.␈α∞ The␈α∞axioms␈α∞are
␈↓ ↓H␈↓divided␈α∀into␈α∀several␈α∀groups.␈α∀ First␈α∀are␈α∀some␈α∀"bookkeeping"␈α∀axioms␈α∀describing␈α∀the␈α∀relations
␈↓ ↓H␈↓between␈αsubdomains,␈αparticular␈αelements␈αof␈αthese␈αsubdomains␈αand␈αthe␈αdomains␈αand␈αranges␈αof␈αthe
␈↓ ↓H␈↓basic␈α
functions␈α
of␈α
LISP.␈α
 Next␈α
there␈α∞are␈α
the␈α
"algebraic"␈α
axioms␈α
giving␈α
the␈α
relations␈α∞satisfied␈α
by
␈↓ ↓H␈↓the␈αbasic␈α
LISP␈αfunctions.␈α
 Finally␈αcome␈α
the␈αinduction␈αaxioms.␈α
 They␈αare␈α
in␈αfact␈α
axiom␈αschemata
␈↓ ↓H␈↓and␈α⊂have␈α⊂the␈α⊂effect␈α⊂of␈α∂providing␈α⊂an␈α⊂infinite␈α⊂collection␈α⊂of␈α∂axioms,␈α⊂one␈α⊂for␈α⊂each␈α⊂instance␈α⊂of␈α∂a
␈↓ ↓H␈↓schema.␈α  The␈αaxioms␈αare␈α
given␈αnames␈αeither␈αindividually␈αor␈α
in␈αsmall␈αgroups␈αfor␈α
later␈αreference.
␈↓ ↓H␈↓To refer to the second axiom in a group named ␈↓¬AXNAME ␈↓we write ␈↓¬AXNAME2. ␈↓


␈↓ ↓H␈↓αBookkeeping axioms.

␈↓ ↓H␈↓        We␈α∞recall␈α∞that␈α∂␈↓↓x,␈↓␈α∞␈↓↓y␈↓␈α∞and␈α∂␈↓↓z␈↓␈α∞are␈α∞asserted␈α∂to␈α∞range␈α∞over␈α∂S-expressions␈α∞and␈α∞thus␈α∂will␈α∞satisfy
␈↓ ↓H␈↓␈↓↓issexp.␈↓  Similiarly ␈↓↓u,␈↓ ␈↓↓v␈↓ and ␈↓↓w␈↓ will satisfy ␈↓↓islist␈↓

␈↓ ↓H␈↓␈↓¬SEXP: ␈↓ ␈↓ β8␈↓↓∀X:[islist X ⊃ issexp X]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓∀X: ␈↓αat|␈↓↓X ⊃ issexp X␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓¬issexp ␈↓π|␈↓↓␈↓

␈↓ ↓H␈↓␈↓¬NIL: ␈↓ ␈↓ β8␈↓↓islist ␈↓¬NIL␈↓↓␈↓

␈↓ ↓H␈↓␈↓¬NULL: ␈↓ ␈↓ β8␈↓↓∀u: [␈↓αn|␈↓↓u ≡ u=␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓∀u: [␈↓αn|␈↓↓u ≡ ␈↓αat|␈↓↓u]␈↓

␈↓ ↓H␈↓␈↓¬CONS: ␈↓ ␈↓ β8␈↓↓∀x y: ¬␈↓αat|␈↓↓[x . y]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓∀x y: issexp [x . y]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓∀x u: islist [x . u]␈↓

␈↓ ↓H␈↓␈↓¬CAR: ␈↓ ␈↓ β8␈↓↓∀x: [¬␈↓αat|␈↓↓x ⊃ issexp ␈↓αa|␈↓↓x]␈↓

␈↓ ↓H␈↓␈↓¬CDR: ␈↓ ␈↓ β8␈↓↓∀x: [¬␈↓αat|␈↓↓x ⊃ issexp ␈↓αd|␈↓↓x]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓∀u: [¬␈↓αn|␈↓↓u ⊃ islist ␈↓αd|␈↓↓u]␈↓


␈↓ ↓H␈↓αAlgebraic axioms.

␈↓ ↓H␈↓␈↓¬CAR-CONS: ␈↓ ␈↓ β8␈↓↓∀x y: ␈↓αa|␈↓↓[x . y] = x␈↓

␈↓ ↓H␈↓␈↓¬CDR-CONS: ␈↓ ␈↓ β8␈↓↓∀x y: ␈↓αd|␈↓↓[x . y] = y␈↓

␈↓ ↓H␈↓␈↓¬EQ-CONS: ␈↓ ␈↓ β8␈↓↓∀x: [¬␈↓αat|␈↓↓x ⊃ [␈↓αa|␈↓↓x . ␈↓αd|␈↓↓x] = x]␈↓
␈↓ ↓H␈↓60␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓        An alternate form of ␈↓¬EQ-CONS ␈↓which we will find useful is

␈↓ ↓H␈↓␈↓¬EQ-SEXP: ␈↓ ␈↓ β8␈↓↓∀x y: [[¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y] ⊃ [␈↓αa|␈↓↓x = ␈↓αa|␈↓↓y ∧ ␈↓αd|␈↓↓x = ␈↓αd|␈↓↓y ≡ x = y]] ␈↓.

␈↓ ↓H␈↓        These␈α
axioms␈α
are␈α
analogous␈α
to␈α
the␈αalgebraic␈α
part␈α
of␈α
Peano's␈α
axioms␈α
for␈α
the␈αnon-negative
␈↓ ↓H␈↓integers.␈α The␈αanalogy␈αcan␈αbe␈αmade␈αclear␈αif␈αwe␈αwrite␈αPeano's␈αaxioms␈αusing␈α␈↓↓n'␈↓␈αfor␈αthe␈αsuccessor␈α
of
␈↓ ↓H␈↓␈↓↓n␈↓ and ␈↓↓n␈↓␈↓∧-␈↓ for the predecessor of ␈↓↓n.␈↓  Peano's algebraic axioms then become

␈↓ ↓H␈↓␈↓¬ZERO: ␈↓ ␈↓ β8␈↓↓numberp ␈↓¬0␈↓↓␈↓

␈↓ ↓H␈↓␈↓¬ADD1: ␈↓ ␈↓ β8␈↓↓∀n: n' ≠ ␈↓¬0 ␈↓↓␈↓

␈↓ ↓H␈↓␈↓¬SUB1: ␈↓ ␈↓ β8␈↓↓∀n: (n')␈↓∧-␈↓↓ = n ␈↓

␈↓ ↓H␈↓␈↓¬SUB ADD: ␈↓ ␈↓ β8␈↓↓∀n: n ≠ ␈↓¬0 ␈↓↓⊃ (n␈↓∧-␈↓↓)' = n ␈↓.

␈↓ ↓H␈↓Lists␈α
can␈αmodel␈α
integers␈αif␈α
we␈αidentify␈α
␈↓¬0␈α␈↓with␈α
␈↓¬NIL␈↓␈αand␈α
assume␈αthat␈α
there␈αis␈α
only␈αone␈α
object␈α(say
␈↓ ↓H␈↓␈↓¬NIL␈↓ again) that can serve as a list element.  Then ␈↓↓n' = cons[␈↓¬NIL␈↓↓,n]␈↓, and ␈↓↓n␈↓∧-␈↓↓ = ␈↓αd|␈↓↓n␈↓.



␈↓ ↓H␈↓6.  ␈↓αTheory of LISP: Induction Schemas.␈↓


␈↓ ↓H␈↓        Clearly␈α∞S-expressions␈α
and␈α∞lists␈α
satisfy␈α∞the␈α∞axioms␈α
given␈α∞for␈α
them,␈α∞but␈α∞unfortunately␈α
these
␈↓ ↓H␈↓algebraic␈α∞axioms␈α∞are␈α∞insufficient␈α∞to␈α∞characterize␈α∂them.␈α∞ For␈α∞example,␈α∞consider␈α∞a␈α∞domain␈α∂of␈α∞one
␈↓ ↓H␈↓element  ␈↓↓a␈↓ satisfying

␈↓ ↓H␈↓␈↓ ¬↓␈↓↓␈↓αa|␈↓↓a = ␈↓αd|␈↓↓a = a . a = a .            ␈↓ 

␈↓ ↓H␈↓It␈αsatisfies␈αthe␈αalgebraic␈αaxioms␈αfor␈αS-expressions.␈α We␈αcan␈αexclude␈αit␈αby␈αan␈αaxiom␈α␈↓↓∀x:␈α[␈↓αa|␈↓↓x␈α≠␈αx]␈↓,
␈↓ ↓H␈↓but␈αthis␈αwon't␈α
exclude␈αother␈αcircular␈αlist␈α
structures␈αthat␈αeventually␈αreturn␈α
to␈αthe␈αsame␈α
element␈αby
␈↓ ↓H␈↓some␈α∞␈↓αa␈↓-␈↓αd␈↓␈α∂chain.␈α∞ Actually␈α∂we␈α∞want␈α∂to␈α∞exclude␈α∞all␈α∂infinite␈α∞chains,␈α∂because␈α∞most␈α∂LISP␈α∞programs
␈↓ ↓H␈↓won't␈αterminate␈αunless␈α
every␈α␈↓αa␈↓-␈↓αd␈↓␈αchain␈α
eventually␈αterminates␈αin␈α
an␈αatom.␈α This␈α
cannot␈αbe␈αdone␈α
by
␈↓ ↓H␈↓any␈α
finite␈α
set␈α
of␈α
axioms.␈α
  As␈α
we␈α
mentioned␈α
earlier␈α
most␈α
proofs␈α
of␈α
properties␈α
of␈α∞LISP␈α
programs
␈↓ ↓H␈↓will␈α∪require␈α∀applying␈α∪some␈α∀induction␈α∪principle.␈α∀  Requiring␈α∪that␈α∀our␈α∪domain␈α∀satisfy␈α∪certain
␈↓ ↓H␈↓induction principles allows us to exclude circular and infinite list structures.

␈↓ ↓H␈↓        The␈α
general␈αform␈α
of␈α
a␈αproof␈α
by␈α
induction␈αuses␈α
a␈α
relation␈αwhich␈α
tells␈α
us␈αwhen␈α
one␈αelement␈α
is
␈↓ ↓H␈↓"smaller"␈αthan␈αanother.␈α If␈αwe␈αcan␈αprove␈αthat␈αa␈αproperty,␈α␈↓πF␈↓,␈αholds␈αfor␈αsome␈αelement␈α
by␈αassuming
␈↓ ↓H␈↓that␈α∞it␈α
holds␈α∞for␈α
all␈α∞smaller␈α
elements␈α∞then␈α
we␈α∞can␈α
conclude␈α∞that␈α
␈↓πF␈↓␈α∞holds␈α
for␈α∞all␈α
elements␈α∞of␈α
the
␈↓ ↓H␈↓domain.␈α  This␈α
a␈αvalid␈α
proof␈αas␈αlong␈α
as␈αthe␈α
relation␈αis␈α"well-founded",␈α
e.g.,␈αas␈α
long␈αas␈α
it␈αsatisfies
␈↓ ↓H␈↓the descending chain condition described in section ␈↓π∞␈↓1.

␈↓ ↓H␈↓        We␈α⊃will␈α⊃express␈α⊃our␈α⊃induction␈α⊃principles␈α⊃in␈α⊃the␈α⊃form␈α⊃of␈α⊃axiom␈α⊃schemas␈α⊃using␈α⊃␈↓πF␈↓␈α⊃as␈α⊃a
␈↓ ↓H␈↓predicate␈αparameter.␈α  We␈αobtain␈αinstances␈αof␈αthe␈αschema␈αby␈αsubstituting␈αparticular␈αpredicates␈αfor
␈↓ ↓H␈↓␈↓πF␈↓.␈α It␈αis␈αcalled␈αan␈αaxiom␈αschema␈αrather␈αthan␈αan␈αaxiom,␈αbecause␈αwe␈αconsider␈αthe␈αschema,␈αwhich␈αis
␈↓ ↓H␈↓not␈α
properly␈α
a␈α
sentence␈α
of␈αfirst␈α
order␈α
logic,␈α
as␈α
standing␈αfor␈α
the␈α
infinite␈α
collection␈α
of␈α
axioms␈αthat
␈↓ ↓H␈↓arise from it by substituting all possible predicate expressions of our language for ␈↓πF␈↓.
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *61


␈↓ ↓H␈↓        For␈α
example␈α
over␈α
the␈α
domain␈α
of␈α
natural␈α
numbers␈α
the␈α
usual␈α
"<"␈α
relation␈α
is␈α
well-founded␈α
and
␈↓ ↓H␈↓gives␈α⊃rise␈α⊃to␈α⊃the␈α⊃form␈α⊃of␈α⊃induction␈α⊃usually␈α⊃called␈α⊃"course␈α⊃of␈α⊃values"␈α⊃induction.␈α∩ The␈α⊃schema
␈↓ ↓H␈↓obtained using this relation is

␈↓ ↓H␈↓␈↓¬NUMBINDUCTION-CVI: ␈↓␈↓ ∧x␈↓↓∀n: [∀m: [m < n ⊃ ␈↓πF␈↓↓ m] ⊃ ␈↓πF␈↓↓ n] ⊃ ∀n: ␈↓πF␈↓↓ n␈↓

␈↓ ↓H␈↓If␈αwe␈αuse␈αthe␈αpredecessor␈αrelation␈αand␈αnotice␈αthat␈αeach␈αnon-zero␈αnumber␈αhas␈αjust␈αone␈αpredecessor
␈↓ ↓H␈↓we obtain the schema

␈↓ ↓H␈↓␈↓¬NUMINDUCTION: ␈↓␈↓ ∧8␈↓↓␈↓πF␈↓↓ ␈↓¬0 ␈↓↓∧ ∀n: [n≠␈↓¬0 ␈↓↓∧ ␈↓πF␈↓↓ n-␈↓¬1 ␈↓↓⊃ ␈↓πF␈↓↓ n] ⊃ ∀n: ␈↓πF␈↓↓ n␈↓

␈↓ ↓H␈↓which␈αcorresponds␈αto␈αthe␈αusual␈αform␈αof␈αinduction␈αgiven␈αin␈αaxiomatizations␈αof␈αnumber␈αtheory␈α(for
␈↓ ↓H␈↓example Peano's axiomatization).

␈↓ ↓H␈↓        For␈αlists␈αthe␈αpredecessor␈αof␈αa␈αlist␈α␈↓↓u␈↓␈αis␈α␈↓↓␈↓αd|␈↓↓u␈↓,␈αand␈αwe␈αshall␈αuse␈αthe␈αrelation␈α␈↓↓istail␈↓␈αcorresponding
␈↓ ↓H␈↓to the "≤" relation to obtain an analogue to <.  ␈↓↓istail␈↓ satisfies the equivalence

␈↓ ↓H␈↓6.1)␈↓ ∧≤␈↓↓∀u v: [u istail v ≡ [u=v] ∨ [¬␈↓αn|␈↓↓v ∧ u istail ␈↓αd|␈↓↓v]]␈↓. 

␈↓ ↓H␈↓As␈α
we␈α
shall␈α
see␈αlater␈α
this␈α
is␈α
a␈α
well-defined␈αrelation.␈α
 In␈α
fact␈α
we␈α
will␈αsee␈α
that␈α
it␈α
is␈α
"computable"␈αby␈α
a
␈↓ ↓H␈↓simple␈α⊂recursive␈α⊂program.␈α⊃ Like␈α⊂≤␈α⊂it␈α⊂is␈α⊃reflexive,␈α⊂anti-symmetric␈α⊂and␈α⊂transitive.␈α⊃ The␈α⊂relation
␈↓ ↓H␈↓␈↓↓isptail␈↓ given by

␈↓ ↓H␈↓6.2)␈↓ ∧Q␈↓↓∀u v: [u isptail v ≡ [u≠v] ∧ u istail v]␈↓. 

␈↓ ↓H␈↓is␈α∂the␈α⊂desired␈α∂well-founded␈α∂relation.␈α⊂ (The␈α∂"p"␈α∂stands␈α⊂for␈α∂"proper".)␈α∂The␈α⊂corresponding␈α∂axiom
␈↓ ↓H␈↓schemas for lists are:

␈↓ ↓H␈↓␈↓¬LISTINDUCTION: ␈↓␈↓ βx␈↓↓∀u: [␈↓αn|␈↓↓u ⊃ ␈↓πF␈↓↓ u] ∧ ∀u: [¬␈↓αn|␈↓↓u ∧ ␈↓πF␈↓↓ ␈↓αd|␈↓↓u ⊃ ␈↓πF␈↓↓ u] ⊃ ∀u: ␈↓πF␈↓↓ u ␈↓.

␈↓ ↓H␈↓␈↓¬LISTINDUCTION-CVI: ␈↓␈↓ ∧8␈↓↓∀u: [∀v: [v istail u ⊃ ␈↓πF␈↓↓ v] ⊃ ␈↓πF␈↓↓ u] ⊃ ∀u: ␈↓πF␈↓↓ u␈↓

␈↓ ↓H␈↓        Each␈α∩S-expression␈α∩has␈α∩two␈α∩predecessors␈α∩obtained␈α∩by␈α⊃taking␈α∩its␈α∩␈↓αa␈↓␈α∩or␈α∩its␈α∩␈↓αd␈↓␈α∩part.␈α⊃  The
␈↓ ↓H␈↓analogues to ␈↓↓istail␈↓ and ␈↓↓isptail␈↓ are ␈↓↓subexp␈↓ and ␈↓↓psubexp.␈↓  ␈↓↓subexp␈↓ is given by the equivalence

␈↓ ↓H␈↓6.3)␈↓ β∨␈↓↓∀x y: [x subexp y ≡ [x=y] ∨ [¬␈↓αat|␈↓↓y ∧ [x subexp ␈↓αa|␈↓↓y ∨ x subexp ␈↓αd|␈↓↓y]]]␈↓. 

␈↓ ↓H␈↓As␈αwith␈α␈↓↓istail␈↓␈αwe␈αshall␈α
see␈αlater␈αhow␈αto␈αdefine␈αthe␈α
␈↓↓subexp␈↓␈αrelation␈αstarting␈αwith␈αan␈αLISP␈α
program.
␈↓ ↓H␈↓We now define ␈↓↓psubexp␈↓ by

␈↓ ↓H␈↓6.4)␈↓ ∧;␈↓↓∀u v: [u psubexp v ≡ [u≠v] ∧ u subexp v]␈↓. 

␈↓ ↓H␈↓The corresponding S-expression induction schemas are

␈↓ ↓H␈↓␈↓¬SEXPINDUCTION: ␈↓␈↓ βx␈↓↓∀x: [␈↓αat|␈↓↓x ⊃ ␈↓πF␈↓↓ x] ∧ ∀x: [¬␈↓αat|␈↓↓x ∧ ␈↓πF␈↓↓ ␈↓αa|␈↓↓x ∧ ␈↓πF␈↓↓ ␈↓αd|␈↓↓x ⊃ ␈↓πF␈↓↓ x] ⊃ ∀x: ␈↓πF␈↓↓ x ␈↓.

␈↓ ↓H␈↓␈↓¬SEXPINDUCTION-CVI: ␈↓␈↓ ∧x␈↓↓∀x: [∀y: [y subexp x ⊃ ␈↓πF␈↓↓ y] ⊃ ␈↓πF␈↓↓ x] ⊃ ∀x: ␈↓πF␈↓↓ x␈↓

␈↓ ↓H␈↓        These␈α∞schemas␈α∞are␈α
called␈α∞principles␈α∞of␈α
␈↓↓structural␈↓␈α∞␈↓↓induction,␈↓␈α∞since␈α
the␈α∞induction␈α∞is␈α∞on␈α
the
␈↓ ↓H␈↓structure␈α
of␈α
the␈α
entities␈α
involved.␈α
Other␈α
schemas␈α
derived␈α
from␈α
principles␈α
of␈α∞␈↓↓structural␈↓␈α
␈↓↓induction␈↓
␈↓ ↓H␈↓62␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓are␈αpossible␈αand␈αthe␈αbest␈αschema␈αto␈αuse␈αdepends␈αstongly␈αon␈αthe␈αproblem␈αat␈αhand.␈α Some␈αexamples
␈↓ ↓H␈↓will be given in a later section.

␈↓ ↓H␈↓        Even␈αthe␈αaxiom␈αschemas␈αdon't␈αassure␈αus␈αthat␈αthe␈αonly␈αdomain␈αsatisfying␈αthe␈αaxioms␈αis␈αthat
␈↓ ↓H␈↓of␈αthe␈αintegers␈αor␈αthe␈αS-expressions␈αas␈αthe␈α
case␈αmay␈αbe.␈α Any␈αfirst␈αorder␈αtheory␈αwhose␈αaxioms␈α
can
␈↓ ↓H␈↓be␈αgiven␈αeffectively␈α
admits␈αthe␈αso-called␈α␈↓↓non-standard models.␈↓␈α
 However,␈αit␈αseems␈αlikely␈α
that␈αthe
␈↓ ↓H␈↓non-standard␈α
models␈α
of␈α
S-expressions,␈α
like␈α
the␈α
non-standard␈α
models␈α
of␈α
integers,␈α
will␈α
agree␈αwith
␈↓ ↓H␈↓the standard model for sentences of practical interest.



␈↓ ↓H␈↓7.  ␈↓αDeduction Rules for First-Order Logic.␈↓


␈↓ ↓H␈↓        In␈αsection␈α␈↓π∞␈↓4␈αwe␈αdescribed␈αhow␈αto␈αassign␈αmeaning␈αto␈αexpressions␈αof␈αour␈αlanguage␈αrelative␈α
to
␈↓ ↓H␈↓a␈α⊂particular␈α⊂domain␈α⊂---␈α∂the␈α⊂extended␈α⊂S-expressions.␈α⊂ This␈α∂process␈α⊂could␈α⊂be␈α⊂carried␈α⊂for␈α∂other
␈↓ ↓H␈↓suitable␈α∀domains␈α∪and␈α∀leads␈α∀to␈α∪the␈α∀study␈α∀of␈α∪"model␈α∀theory".␈α∀ (A␈α∪domain␈α∀together␈α∀with␈α∪the
␈↓ ↓H␈↓assignments␈αto␈αconstants,␈αfunction␈αand␈αpredicate␈αsymbols␈αis␈αcalled␈αa␈αmodel␈αof␈αthe␈αlanguage.)␈α This
␈↓ ↓H␈↓is␈α∂the␈α∂"semantic"␈α∂approach␈α∞to␈α∂investigating␈α∂notions␈α∂such␈α∞as␈α∂truth␈α∂and␈α∂validity.␈α∂ Any␈α∞particular
␈↓ ↓H␈↓statement␈α⊃may␈α⊃be␈α⊃true␈α⊃in␈α⊂all␈α⊃models,␈α⊃in␈α⊃some␈α⊃models,␈α⊂or␈α⊃in␈α⊃no␈α⊃models.␈α⊃ Deciding␈α⊃whether␈α⊂a
␈↓ ↓H␈↓particular␈α
statement␈α
is␈α
true␈α
in␈α
a␈α
particular␈α
model␈α
will␈α
mean␈α
reasoning␈α
about␈α
the␈α
model,␈αperhaps
␈↓ ↓H␈↓doing␈αsome␈α"calculation"␈αin␈αthe␈αmodel,␈αor␈αfinding␈αa␈αparticular␈αobject␈αwith␈αsome␈αgiven␈αproperty␈αor
␈↓ ↓H␈↓showing that some property holds for all objects in the model.

␈↓ ↓H␈↓        So␈α
far␈α
we␈α
have␈α
no␈α
precise␈α
statement␈α
of␈α
what␈α
it␈α
means␈α
to␈α
show␈α
(prove)␈α
that␈α
a␈α
statement␈αis
␈↓ ↓H␈↓true.␈α  This␈α
is␈αnecessary␈α
if␈αthere␈α
is␈αto␈α
be␈αany␈α
hope␈αof␈α
having␈αthe␈α
computer␈αhelp␈α
us␈αwith␈αproofs.␈α
 If
␈↓ ↓H␈↓we␈α
study␈αthe␈α
rules␈α
for␈αdetermining␈α
the␈α
truth␈αvalue␈α
of␈α
a␈αstatement␈α
relative␈α
to␈αa␈α
particular␈αmodel␈α
we
␈↓ ↓H␈↓can␈α
arrive␈α
at␈αvarious␈α
rules␈α
that␈αsay␈α
whenever␈α
some␈α
collection␈αof␈α
formulas␈α
␈↓↓s␈↓β1␈↓↓␈↓,␈α...␈α
,␈↓↓s␈↓βn␈↓↓␈↓␈α
are␈α
true␈αthen
␈↓ ↓H␈↓some␈α
formula␈α␈↓↓s␈↓␈α
is␈αalso␈α
true.␈α For␈α
example␈α
if␈α␈↓↓s␈↓β1␈↓↓␈↓␈α
and␈α␈↓↓s␈↓β1␈↓↓⊃s␈↓β2␈↓↓␈↓␈α
are␈αtrue␈α
then␈α
we␈αsee␈α
that␈α␈↓↓s␈↓β2␈↓↓␈↓␈α
must␈αalso␈α
be
␈↓ ↓H␈↓true.␈α∂ We␈α⊂can␈α∂also␈α∂see␈α⊂that␈α∂some␈α⊂formulas␈α∂are␈α∂true␈α⊂regardless␈α∂of␈α∂the␈α⊂meaning␈α∂assigned␈α⊂to␈α∂the
␈↓ ↓H␈↓symbols␈α∞occuring␈α∂in␈α∞them.␈α∞ Thus␈α∂if␈α∞␈↓↓s␈↓␈α∞is␈α∂a␈α∞formula,␈α∂then␈α∞␈↓↓s␈α∞⊃␈α∂s␈↓␈α∞is␈α∞always␈α∂true.␈α∞ A␈α∂formal␈α∞proof
␈↓ ↓H␈↓system␈αis␈αa␈αcollection␈αof␈αformulas␈α(axioms)␈αtogether␈αwith␈αa␈αcollection␈αof␈αdeduction␈αrules.␈α A␈αformal
␈↓ ↓H␈↓proof␈αof␈αa␈αformula␈α␈↓↓s␈↓␈αis␈αa␈αsequence␈αof␈αformulas␈α␈↓↓s␈↓β1␈↓↓␈↓,␈α...␈α,␈↓↓s␈↓βn␈↓↓␈↓␈αwhere␈α␈↓↓s␈↓βn␈↓↓␈↓␈αis␈α␈↓↓s␈↓␈αand␈αeach␈α␈↓↓s␈↓βi␈↓↓␈↓␈αis␈αeither␈αone␈αof
␈↓ ↓H␈↓the␈αaxioms,␈αor␈αfollows␈αfrom␈αthe␈αpreceeding␈αformulas␈αby␈αone␈αof␈αthe␈αdeduction␈αrules.␈α The␈αstudy␈αof
␈↓ ↓H␈↓such␈αsystems␈αis␈αknown␈αas␈α"proof␈αtheory".␈α It␈αis␈αthe␈αsyntatic␈αapproach␈αto␈αthe␈αinvestigation␈αof␈αtruth
␈↓ ↓H␈↓and␈α
validity.␈α The␈α
truth␈αof␈α
the␈αbasic␈α
axioms␈α
follows␈αfrom␈α
their␈αsyntatic␈α
form␈αand␈α
does␈αnot␈α
depend
␈↓ ↓H␈↓on␈α∂what␈α∂meaning␈α∂is␈α∂assigned␈α∂to␈α∂particular␈α∂symbols.␈α∂ Similarly␈α∂the␈α∂validity␈α∂of␈α∂the␈α⊂rules␈α∂follows
␈↓ ↓H␈↓from the form of the formulae to which they apply and not their meaning.

␈↓ ↓H␈↓        Below␈αwe␈αdescribe␈αa␈αcollection␈αof␈αbasic␈αaxioms␈αand␈αdeduction␈αrules␈αfor␈αone␈αpossible␈αformal
␈↓ ↓H␈↓proof␈α_system␈α_for␈α_first-order␈α_logic␈α_with␈α_equality␈α_(without␈α_conditional␈α_expressions,␈α↔lambda
␈↓ ↓H␈↓expressions␈αor␈αvariables␈αwith␈α
restricted␈αranges).␈α In␈αthe␈α
following␈αthree␈αsections␈αwe␈αgive␈α
additional
␈↓ ↓H␈↓rules␈α~for␈α≠manipulating␈α~formulas␈α≠containing␈α~variables␈α≠with␈α~restricted␈α≠ranges,␈α~conditional
␈↓ ↓H␈↓expressions␈α
and␈α
lambda␈α
expressions␈α
which␈α
allow␈α
us␈α
to␈α
extend␈α
the␈α
system␈α
to␈α
our␈α
logic.␈α
 To␈α
obtain␈α
a
␈↓ ↓H␈↓proof␈α∞system␈α∞for␈α∞our␈α
theory␈α∞of␈α∞LISP␈α∞we␈α∞need␈α
only␈α∞add␈α∞the␈α∞axioms␈α
given␈α∞in␈α∞sections␈α∞␈↓π∞␈↓5␈α∞and␈α
␈↓π∞␈↓6
␈↓ ↓H␈↓(plus␈α
any␈α
additional␈α
axioms␈α
resulting␈α
from␈α
the␈α
introducion␈α
of␈α
function␈α
definitions).␈α∞ The␈α
system
␈↓ ↓H␈↓we␈αgive␈αis␈αvery␈αsimple␈αto␈αdescribe,␈αand␈αnice␈αif␈αwe␈αwanted␈αto␈αprove␈αthings␈αabout␈αthe␈αsystem␈αitself.
␈↓ ↓H␈↓For␈αa␈αsystem␈αto␈αbe␈αuseful␈αas␈αa␈αproof␈αsystem␈αin␈αwhich␈αto␈αcarry␈αout␈αproofs␈αwe␈αwould␈αprefer␈αa␈α
larger
␈↓ ↓H␈↓collection␈α→of␈α_rules␈α→that␈α→would␈α_allow␈α→more␈α_natural␈α→proofs.␈α→ Such␈α_systems␈α→exist,␈α→but␈α_are
␈↓ ↓H␈↓correspondingly more complicated to explain and we will not discuss them here.
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *63


␈↓ ↓H␈↓α␈↓ ¬kBasic axioms.

␈↓ ↓H␈↓        The␈α
axioms␈αof␈α
our␈αbasic␈α
theory␈αfall␈α
into␈α three␈α
groups:␈αtautologies,␈α
axioms␈α
for␈αquantifiers,
␈↓ ↓H␈↓and axioms for equality.


␈↓ ↓H␈↓αTautologies

␈↓ ↓H␈↓        A␈α⊂tautology␈α⊂is␈α⊂a␈α⊂formula␈α⊂that␈α⊂is␈α∂true␈α⊂by␈α⊂virtue␈α⊂of␈α⊂its␈α⊂propositional␈α⊂form.␈α⊂ For␈α∂example
␈↓ ↓H␈↓␈↓↓s␈↓β1␈↓↓ ⊃ s␈↓β1␈↓↓␈↓,␈α␈↓↓s␈↓β1␈↓↓ ⊃ [s␈↓β2␈↓↓ ⊃ s␈↓β1␈↓↓]␈↓␈α
and␈α␈↓↓s␈↓β1␈↓↓ ⊃ [s␈↓β1␈↓↓ ∨ s␈↓β2␈↓↓]␈↓␈α
are␈αtautologies␈α
for␈αany␈α
formulas␈α␈↓↓s␈↓β1␈↓↓␈↓␈α
and␈α␈↓↓s␈↓β2␈↓↓␈↓.␈α
 Let␈α␈↓↓s␈↓β1␈↓↓␈↓,␈α
␈↓↓s␈↓β2␈↓↓␈↓, ... ␈↓↓s␈↓βn␈↓↓␈↓
␈↓ ↓H␈↓be␈αformulas␈αand␈αlet␈α␈↓↓s␈↓␈αbe␈αa␈αformula␈αbuilt␈αfrom␈αthe␈α␈↓↓s␈↓βi␈↓↓␈↓␈αusing␈α¬,␈α∧,␈α∨,␈α⊃,␈αand␈α≡.␈α Then␈αWe␈αsay␈αthat␈α␈↓↓s␈↓
␈↓ ↓H␈↓is␈αa␈αtautology␈αif␈αunder␈αall␈αpossible␈αcombinations␈αof␈αassignments␈αof␈α␈↓αtrue␈↓␈αand␈α␈↓αfalse␈↓␈αto␈αthe␈αformulas
␈↓ ↓H␈↓␈↓↓s␈↓βi␈↓↓␈↓␈αthe␈α
value␈αof␈α
␈↓↓s␈↓␈αis␈α
␈↓αtrue␈↓␈α(according␈α
to␈αthe␈α
truth␈αtables).␈α
 Every␈αformula␈α
that␈αis␈α
a␈αtautology␈α
is␈αone␈α
of
␈↓ ↓H␈↓our basic axioms.


␈↓ ↓H␈↓αAxioms for quantifiers

␈↓ ↓H␈↓Let ␈↓↓s␈↓β1␈↓↓␈↓ and ␈↓↓s␈↓β2␈↓↓␈↓ be formulas and ␈↓πx␈↓ be a variable that is not free in ␈↓↓s␈↓β1␈↓↓␈↓ then any formula of the form

␈↓ ↓H␈↓7.1)␈↓ ¬≤␈↓↓∀␈↓πx␈↓↓: [s␈↓β1␈↓↓ ⊃ s␈↓β2␈↓↓] ⊃ [s␈↓β1␈↓↓ ⊃ ∀␈↓πx␈↓↓: s␈↓β2␈↓↓]␈↓ 

␈↓ ↓H␈↓is␈α∞one␈α∞of␈α
the␈α∞basic␈α∞axioms.␈α∞ Let␈α
␈↓↓s␈↓␈α∞be␈α∞a␈α∞formula,␈α
␈↓πx␈↓␈α∞a␈α∞variable␈α
and␈α∞␈↓↓t␈↓␈α∞a␈α∞term␈α
such␈α∞that␈α∞␈↓↓t␈↓␈α∞may␈α
be
␈↓ ↓H␈↓substituted␈α
for␈α
all␈α
free␈α
occurrences␈α
of␈α
␈↓πx␈↓␈α
in␈α
␈↓↓s␈↓␈α
without␈α
any␈α
"catching␈α
of␈α
free␈α
variables".␈α
 Let␈α
␈↓↓s'␈↓␈αbe␈α
the
␈↓ ↓H␈↓result of this substitution then any formula of the form

␈↓ ↓H␈↓7.2)␈↓ ¬→␈↓↓∀␈↓πx␈↓↓: s ⊃ s'                       ␈↓ 

␈↓ ↓H␈↓is␈αone␈αof␈αthe␈αbasic␈αaxioms.␈α
 Finally,␈αif␈α␈↓↓s␈↓␈αis␈αany␈αformula␈αand␈α
␈↓πx␈↓␈αis␈αany␈αvariable␈αthen␈αany␈αformula␈α
of
␈↓ ↓H␈↓the form

␈↓ ↓H␈↓7.3)␈↓ ¬∀␈↓↓[∃␈↓πx␈↓↓: s] ≡ [¬∀␈↓πx␈↓↓: ¬s]             ␈↓ 

␈↓ ↓H␈↓is one of the basic axioms.


␈↓ ↓H␈↓αAxioms for equality

␈↓ ↓H␈↓        The formula (7.4) is one of the basic axioms.

␈↓ ↓H␈↓7.4)␈↓ ¬ε␈↓↓∀X: X = X                         ␈↓ 

␈↓ ↓H␈↓        Let␈α␈↓↓s␈↓␈αbe␈αa␈αformula,␈αand␈α␈↓πx␈↓,␈α␈↓πx␈↓␈↓β1␈↓,␈α␈↓πx␈↓␈↓β2␈↓␈αbe␈αvariables␈αsuch␈αthat␈αsubstituting␈α␈↓πx␈↓␈↓β1␈↓␈αor␈α␈↓πx␈↓␈↓β2␈↓␈αfor␈α␈↓πx␈↓␈αin␈α␈↓↓s␈↓␈αdoes
␈↓ ↓H␈↓not␈αresult␈αin␈αbinding␈αof␈αeither␈αvariable,␈αand␈αlet␈α
␈↓↓s␈↓β1␈↓↓␈↓␈αand␈α␈↓↓s␈↓β2␈↓↓␈↓␈αbe␈αthe␈αresults␈αof␈αthat␈αsubsitution.␈α
 Then
␈↓ ↓H␈↓any formula of the form (7.5) is one of the basic axioms.

␈↓ ↓H␈↓7.5)␈↓ ¬#␈↓↓∀␈↓πx␈↓↓␈↓β1␈↓↓ ␈↓πx␈↓↓␈↓β2␈↓↓: [␈↓πx␈↓↓␈↓β1␈↓↓ = ␈↓πx␈↓↓␈↓β2␈↓↓ ⊃ s␈↓β1␈↓↓ = s␈↓β2␈↓↓]␈↓ 
␈↓ ↓H␈↓64␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓        Similarly,␈αlet␈α␈↓↓t␈↓␈αbe␈αa␈αformula,␈αand␈α␈↓πx␈↓,␈α␈↓πx␈↓␈↓β1␈↓,␈α␈↓πx␈↓␈↓β2␈↓␈αbe␈αvariables␈αsuch␈αthat␈αsubstituting␈α␈↓πx␈↓␈↓β1␈↓␈αor␈α␈↓πx␈↓␈↓β2␈↓␈αfor␈α␈↓πx␈↓
␈↓ ↓H␈↓in␈α
␈↓↓t␈↓␈α
does␈α
not␈α
result␈α
in␈α
binding␈α
of␈α
either␈α
variable,␈α
and␈α
let␈α
␈↓↓t␈↓β1␈↓↓␈↓␈α
and␈α
␈↓↓t␈↓β2␈↓↓␈↓␈α
be␈α
the␈α
results␈α
of␈α
the␈α
subsitution.
␈↓ ↓H␈↓Then a formula of the form (7.6) is one of the basic axioms.

␈↓ ↓H␈↓7.6)␈↓ ¬%␈↓↓∀␈↓πx␈↓↓␈↓β1␈↓↓ ␈↓πx␈↓↓␈↓β2␈↓↓: [␈↓πx␈↓↓␈↓β1␈↓↓ = ␈↓πx␈↓↓␈↓β2␈↓↓ ⊃ t␈↓β1␈↓↓ = t␈↓β2␈↓↓]␈↓ 

␈↓ ↓H␈↓α␈↓ ¬←Deduction rules

␈↓ ↓H␈↓        There␈α≠are␈α≤two␈α≠deduction␈α≤rules␈α≠for␈α≤the␈α≠formal␈α≤proof␈α≠system:␈α≤␈↓↓modus ponens␈↓␈α≠and
␈↓ ↓H␈↓␈↓↓universal generalizaton.␈↓


␈↓ ↓H␈↓αModus Ponens

␈↓ ↓H␈↓        The␈αrule␈αknown␈αas␈αmodus␈αponens␈α(␈↓¬MP␈↓)␈αstates␈αthat␈αif␈α␈↓↓s␈↓β1␈↓↓␈↓␈αand␈αs␈↓β2␈↓␈↓↓␈↓␈αare␈αformulas␈αand␈αif␈αwe␈αhave
␈↓ ↓H␈↓proved ␈↓↓s␈↓β1␈↓↓␈↓ and ␈↓↓s␈↓β1␈↓↓⊃s␈↓β2␈↓↓␈↓ then we can deduce ␈↓↓s␈↓β2␈↓↓␈↓.  This is the rule that we discussed above.


␈↓ ↓H␈↓αUniversal Generalization

␈↓ ↓H␈↓        The␈αuniversal␈αgeneralization␈αrule␈α(␈↓¬UG␈↓),␈αalso␈αknown␈αas␈α"∀"␈αintroduction,␈αsays␈αthat␈αif␈αwe␈αhave
␈↓ ↓H␈↓proved the formula ␈↓↓s␈↓ then we may deduce ␈↓↓∀␈↓πx␈↓↓.s␈↓ for any variable ␈↓πx␈↓.

␈↓ ↓H␈↓α␈↓ εExample

␈↓ ↓H␈↓        As␈αa␈αsimple␈αdemonstration␈α
of␈αthe␈αuse␈αof␈α
this␈αformal␈αsystem␈αwe␈α
will␈αgive␈αa␈αformal␈α
proof␈αof
␈↓ ↓H␈↓␈↓↓issexp␈α∪␈↓¬NIL␈↓↓␈↓␈α∪in␈α∪the␈α∪system␈α∪obtained␈α∪by␈α∪adding␈α∪the␈α∪axioms␈α∪␈↓¬SEXP1␈α∪␈↓and␈α∪␈↓¬NIL␈α∪␈↓(from␈α∀the␈α∪LISP
␈↓ ↓H␈↓algebraic axioms section ␈↓π∞␈↓5) to the basic system.  The proof is as follows:

␈↓ ↓H␈↓␈↓ αx1.  ␈↓↓∀X:[islist X ⊃ issexp X]␈↓␈↓ λx␈↓¬SEXP1␈↓
␈↓ ↓H␈↓␈↓ αx2.  ␈↓↓∀X:[islist X ⊃ issexp X] ⊃ [islist ␈↓¬NIL␈↓↓ ⊃ issexp ␈↓¬NIL␈↓↓]␈↓␈↓ λx(7.2)
␈↓ ↓H␈↓␈↓ αx3.  ␈↓↓islist ␈↓¬NIL␈↓↓ ⊃ issexp ␈↓¬NIL␈↓↓␈↓␈↓ λx␈↓¬MP␈↓ 1,2
␈↓ ↓H␈↓␈↓ αx4.  ␈↓↓islist ␈↓¬NIL␈↓↓␈↓␈↓ λx␈↓¬NIL␈↓
␈↓ ↓H␈↓␈↓ αx5.  ␈↓↓issexp ␈↓¬NIL␈↓↓␈↓␈↓ λx␈↓¬MP␈↓ 3,4

␈↓ ↓H␈↓        The␈αmain␈αpoint␈αof␈αintroducing␈αformal␈αproofs␈αis␈αto␈αshow␈αyou␈αthat␈αthe␈αrules␈αare␈αfairly␈α
simple
␈↓ ↓H␈↓and␈αcan␈αbe␈αmade␈αsufficiently␈αprecise␈αthat␈αa␈αcomputer␈αprogram␈αcould␈αbe␈αwritten␈αto␈αdecide␈αwhether
␈↓ ↓H␈↓or␈α⊂not␈α⊃a␈α⊂give␈α⊂rule␈α⊃applies␈α⊂to␈α⊂a␈α⊃particular␈α⊂situation␈α⊂and␈α⊃when␈α⊂it␈α⊂does␈α⊃apply␈α⊂to␈α⊂carry␈α⊃out␈α⊂the
␈↓ ↓H␈↓corresponding proof step.

␈↓ ↓H␈↓        You␈α∞may␈α∂at␈α∞this␈α∂point␈α∞ask␈α∂what␈α∞such␈α∂a␈α∞formal␈α∞proof␈α∂system␈α∞has␈α∂to␈α∞do␈α∂with␈α∞our␈α∂goal␈α∞of
␈↓ ↓H␈↓proving␈αstatements␈α
about␈αthe␈α
domain␈αof␈αextended␈α
S-expressions.␈α The␈α
connection␈αis␈αthe␈α
following.
␈↓ ↓H␈↓We␈αhave␈αalready␈αfixed␈αa␈αlanguage.␈α Suppose␈αwe␈αfix␈αa␈αcollection␈αof␈αstatements␈α(axioms)␈αwhich␈αare
␈↓ ↓H␈↓true␈αfor␈αS-expressions.␈α  Then␈αany␈αstatement␈αformally␈αprovable␈αfrom␈αthese␈αaxioms␈αwill␈αbe␈αtrue␈αof
␈↓ ↓H␈↓S-expressions␈αand␈αany␈α
statement␈αwhich␈αis␈αtrue␈α
in␈α␈↓αall␈↓␈αdomains␈αwhich␈α
"model"␈αour␈αlanguage␈αand␈α
in
␈↓ ↓H␈↓which␈α
our␈α
axioms␈α
are␈α
true␈α
will␈α
be␈α
formally␈α
provable␈α
from␈α
the␈α
axioms.␈α
 The␈α
snag␈α
is␈α
that␈α
in␈α
the
␈↓ ↓H␈↓case␈αof␈αstructures␈αas␈α
complex␈αas␈αS-expressions,␈αfor␈αany␈α
"reasonable"␈αcollection␈αof␈αaxioms␈αthere␈α
will
␈↓ ↓H␈↓always␈αbe␈αdomains␈αother␈αthan␈αthe␈αextended␈αS-expression␈αdomain␈αwhich␈α"model"␈αthe␈αaxioms,␈αand
␈↓ ↓H␈↓there␈αwill␈αbe␈αstatements␈αtrue␈αfor␈αS-expressions␈αthat␈αwill␈αnot␈αbe␈αtrue␈αfor␈αfor␈αsome␈αother␈αmodel␈αand
␈↓ ↓H␈↓hence␈αthe␈α
statement␈αwill␈αnot␈α
be␈αformally␈αprovable.␈α
 Fortunately␈αit␈α
is␈αpossible␈αto␈α
pick␈αa␈αcollection␈α
of
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *65


␈↓ ↓H␈↓axioms␈αsuch␈αthese␈αstatements␈αdo␈αnot␈αarise␈αvery␈αoften␈αand␈αwe␈αwill␈αgenerally␈αbe␈αable␈αto␈αfind␈αformal
␈↓ ↓H␈↓proofs for the statements we are interested in proving.



␈↓ ↓H␈↓8.  ␈↓αRules for using restricted variables.␈↓


␈↓ ↓H␈↓        The␈αrules␈αwe␈αgave␈αfor␈αmanipulating␈αquantifiers␈αin␈αthe␈αprevious␈αsection␈α apply␈αto␈αvariables
␈↓ ↓H␈↓that␈α∞range␈α∂over␈α∞the␈α∞entire␈α∂domian.␈α∞ In␈α∞order␈α∂to␈α∞handle␈α∞variables␈α∂restricted␈α∞to␈α∞range␈α∂over␈α∞some
␈↓ ↓H␈↓subset␈α
we␈α
must␈α
either␈αmodify␈α
the␈α
rules␈α
or␈αshow␈α
how␈α
to␈α
eliminate␈αthe␈α
restricted␈α
variables␈α
from␈αa
␈↓ ↓H␈↓formula.

␈↓ ↓H␈↓        Suppose␈α∂the␈α∂variable␈α∂␈↓πx␈↓␈α∞is␈α∂restricted␈α∂to␈α∂range␈α∂over␈α∞the␈α∂subdomain␈α∂satisfying␈α∂␈↓↓isD␈↓␈α∂and␈α∞the
␈↓ ↓H␈↓variable␈α
␈↓πx␈↓␈↓β1␈↓␈αis␈α
a␈α
general␈αvariable.␈α
 For␈αexample,␈α
in␈α
the␈αcase␈α
of␈α
extended␈αS-expressions␈α
we␈αhave␈α
the
␈↓ ↓H␈↓subdomain␈αcharacterized␈αby␈α
␈↓↓issexp,␈↓␈αthe␈αvariable␈α
␈↓↓x␈↓␈αranging␈αover␈α
this␈αdomain␈αand␈α
␈↓↓X␈↓␈α  is␈αa␈α
general
␈↓ ↓H␈↓variable.␈α∪ The␈α∪formula␈α∪␈↓↓∀␈↓πx␈↓↓.s␈↓␈α∪is␈α∪then␈α∪equivalent␈α∪to␈α∪␈↓↓∀␈↓πx␈↓↓␈↓β1␈↓↓:[isD ␈↓πx␈↓↓␈↓β1␈↓↓ ⊃ s]␈↓␈α∪and␈α∪the␈α∪formual␈α∪␈↓↓∃␈↓πx␈↓↓.s␈↓␈α∩is
␈↓ ↓H␈↓equivalent␈α⊃to␈α⊃␈↓↓∃␈↓πx␈↓↓␈↓β1␈↓↓.[isD␈α⊃␈↓πx␈↓↓␈↓β1␈↓↓␈α⊂∧␈α⊃s]␈↓.␈α⊃ If␈α⊃we␈α⊂use␈α⊃the␈α⊃this␈α⊃elimination␈α⊂process␈α⊃directly␈α⊃it␈α⊃defeats␈α⊂the
␈↓ ↓H␈↓purpose␈αof␈αintroducing␈αthe␈αrestricted␈αvariables␈αin␈αthe␈αfirst␈αplace.␈α As␈αwe␈αnow␈αhave␈αback␈αall␈αof␈αthe
␈↓ ↓H␈↓extra␈α
implications␈αthat␈α
we␈α
did␈αnot␈α
want␈αto␈α
see␈α
in␈αthe␈α
first␈αplace.␈α
 Thus␈α
we␈αwould␈α
like␈α
to␈αmodify
␈↓ ↓H␈↓the proof rules to make better use of the abbreviations.

␈↓ ↓H␈↓        The␈α
only␈αmodification␈α
required␈αis␈α
the␈αaxiom␈α
schema␈α
(7.2)␈αwhich␈α
is␈αthe␈α
rule␈αfor␈α
instantiating
␈↓ ↓H␈↓a␈αuniversially␈α
quantified␈αvariable␈αto␈α
some␈αterm.␈α Here␈α
we␈αrequire␈αthat␈α
we␈αhave␈αalso␈α
proved␈αthat
␈↓ ↓H␈↓the␈αterm␈α␈↓↓t␈↓␈αis␈αan␈αelement␈αof␈αthe␈αdomain␈αto␈αwhich␈αthe␈αvariable␈α␈↓πx␈↓␈αis␈αrestricted,␈αeg.,␈αthat␈α␈↓↓isD␈αt␈↓␈αis␈αtrue.
␈↓ ↓H␈↓Alternately we could write the schema

␈↓ ↓H␈↓7.2 ') ␈↓ ¬∞␈↓↓[∀␈↓πx␈↓↓: s] ⊃ [isD t ⊃ s']           ␈↓ 

␈↓ ↓H␈↓and remove the second implication by an application of modus ponens.



␈↓ ↓H␈↓9.  ␈↓αConditional Expressions.␈↓


␈↓ ↓H␈↓        All the properties we shall use of conditional terms follow from the relation

␈↓ ↓H␈↓9.1)␈↓ β>␈↓↓[s ⊃ [␈↓αif␈↓↓ s ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] = a] ∧ [¬s ⊃ [␈↓αif␈↓↓ s ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] = b] ␈↓. 

␈↓ ↓H␈↓        It␈α
is␈α∞worthwhile␈α
to␈α
list␈α∞separately␈α
some␈α
properties␈α∞of␈α
conditional␈α
terms.␈α∞ First␈α
we␈α∞have␈α
the
␈↓ ↓H␈↓obvious

␈↓ ↓H␈↓␈↓ ∧\␈↓↓[␈↓αif␈↓↓ ␈↓αtrue␈↓↓ ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] = a              ␈↓
␈↓ ↓H␈↓9.2)
␈↓ ↓H␈↓␈↓ ∧\␈↓↓[␈↓αif␈↓↓ ␈↓αfalse␈↓↓ ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] = b .           ␈↓

␈↓ ↓H␈↓Next we have a ␈↓↓distributive␈↓ ␈↓↓law␈↓ for functions applied to conditional terms, namely

␈↓ ↓H␈↓9.3)␈↓ ∧#␈↓↓f[␈↓αif␈↓↓ s ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] = ␈↓αif␈↓↓ s ␈↓αthen␈↓↓ f[a] ␈↓αelse␈↓↓ f[b] ␈↓. 
␈↓ ↓H␈↓66␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓This␈α
applies␈αto␈α
predicates␈αas␈α
well␈αas␈α
functions␈αand␈α
can␈α
also␈αbe␈α
used␈αwhen␈α
one␈αof␈α
the␈αarguments␈α
of
␈↓ ↓H␈↓a␈αfunction␈αof␈αseveral␈αarguments␈αis␈αa␈αconditional␈αterm.␈α It␈αalso␈αapplies␈αwhen␈αone␈αof␈αthe␈αterms␈αof␈αa
␈↓ ↓H␈↓conditional term is itself a conditional term.

␈↓ ↓H␈↓Thus

␈↓ ↓H␈↓9.4)␈↓ β&␈↓↓␈↓αif␈↓↓ [␈↓αif␈↓↓ r ␈↓αthen␈↓↓ s␈↓β1␈↓↓ ␈↓αelse␈↓↓ s␈↓β2␈↓↓] ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b =                                  ␈↓
␈↓ ↓H␈↓␈↓ βl␈↓↓␈↓αif␈↓↓ r ␈↓αthen␈↓↓ [␈↓αif␈↓↓ s␈↓β1␈↓↓ ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] ␈↓αelse␈↓↓ [␈↓αif␈↓↓ s␈↓β2␈↓↓ ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ αn␈↓↓␈↓αif␈↓↓ r ␈↓αthen␈↓↓ [␈↓αif␈↓↓ s ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] ␈↓αelse␈↓↓ c =                                                ␈↓
␈↓ ↓H␈↓9.5)␈↓ β ␈↓↓␈↓αif␈↓↓ s ␈↓αthen␈↓↓ [␈↓αif␈↓↓ r ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ c] ␈↓αelse␈↓↓ [␈↓αif␈↓↓ r ␈↓αthen␈↓↓ b ␈↓αelse␈↓↓ c] .                ␈↓

␈↓ ↓H␈↓        When␈αthe␈α
expressions␈αfollowing␈α
␈↓αthen␈↓␈αand␈α␈↓αelse␈↓␈α
are␈αformulas,␈α
then␈αthe␈αconditional␈α
expression
␈↓ ↓H␈↓can be replaced by a formula according to

␈↓ ↓H␈↓9.6)␈↓ ∧1␈↓↓[␈↓αif␈↓↓ r ␈↓αthen␈↓↓ s␈↓β1␈↓↓ ␈↓αelse␈↓↓ s␈↓β2␈↓↓] ≡ [r ∧ s␈↓β1␈↓↓] ∨ [¬r ∧ s␈↓β2␈↓↓] ␈↓. 

␈↓ ↓H␈↓These␈αrules␈αpermit␈αeliminating␈αconditional␈αexpressions␈αfrom␈αformulas␈αby␈αfirst␈αusing␈αdistributivity
␈↓ ↓H␈↓to␈α∂move␈α∂the␈α⊂conditionals␈α∂to␈α∂the␈α∂outside␈α⊂of␈α∂any␈α∂functions␈α∂or␈α⊂predicates␈α∂and␈α∂then␈α⊂replacing␈α∂the
␈↓ ↓H␈↓conditional␈α⊃expression␈α⊃by␈α⊃a␈α⊃Boolean␈α⊃expression.␈α⊃  They␈α⊃could␈α⊃be␈α⊃added␈α⊃to␈α⊃our␈α⊃formal␈α⊃proof
␈↓ ↓H␈↓system either as rules or as axiom schemata.

␈↓ ↓H␈↓        Note␈αthat␈αthe␈αelimination␈αof␈α
conditional␈αterms␈αmay␈αincrease␈αthe␈α
size␈αof␈αa␈αsentence,␈αbecause␈α
␈↓↓r␈↓
␈↓ ↓H␈↓occurs␈αtwice␈αin␈αthe␈α
right␈αhand␈αside␈αof␈α
the␈αabove␈αequivalence.␈α In␈α
the␈αmost␈αunfavorable␈αcase,␈α
␈↓↓r␈↓␈αis
␈↓ ↓H␈↓dominates␈α∩the␈α⊃size␈α∩of␈α⊃the␈α∩expression␈α⊃so␈α∩that␈α⊃writing␈α∩it␈α⊃twice␈α∩almost␈α⊃doubles␈α∩the␈α⊃size␈α∩of␈α⊃the
␈↓ ↓H␈↓expression.

␈↓ ↓H␈↓        Suppose␈αthat␈α␈↓↓a␈↓␈αand␈α␈↓↓b␈↓␈αin␈α␈↓↓␈↓αif␈↓↓ s ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b␈↓␈αare␈αexpressions␈αthat␈αmay␈αcontain␈αthe␈αsentence␈α␈↓↓s.␈↓
␈↓ ↓H␈↓Occurrences␈αof␈α␈↓↓s␈↓␈αin␈α␈↓↓a␈↓␈αcan␈αbe␈αreplaced␈αby␈α␈↓αtrue␈↓,␈αand␈αoccurrences␈αof␈α␈↓↓s␈↓␈αin␈α␈↓↓b␈↓␈αcan␈αbe␈αreplaced␈αby␈α␈↓αfalse␈↓.
␈↓ ↓H␈↓This follows from the fact that ␈↓↓a␈↓ is only evaluated if ␈↓↓s␈↓ is true and ␈↓↓b␈↓ is evaluated only if ␈↓↓s␈↓ is false.

␈↓ ↓H␈↓        This␈α∂leads␈α⊂to␈α∂a␈α⊂strengthened␈α∂form␈α⊂of␈α∂the␈α⊂law␈α∂of␈α⊂replacement␈α∂of␈α⊂equals␈α∂by␈α⊂equals.␈α∂ The
␈↓ ↓H␈↓ordinary␈αform␈αof␈αthe␈αlaw␈αsays␈αthat␈αif␈αwe␈αhave␈α␈↓↓e = e'␈↓,␈αthen␈αwe␈αcan␈αreplace␈αany␈αoccurrence␈αof␈α
␈↓↓e␈↓␈αin
␈↓ ↓H␈↓an␈α
expression␈α∞by␈α
an␈α
occurrence␈α∞of␈α
␈↓↓e'.␈↓␈α
 However,␈α∞if␈α
we␈α
want␈α∞to␈α
replace␈α
␈↓↓e␈↓␈α∞by␈α
␈↓↓e'␈↓␈α
within␈α∞␈↓↓a␈↓␈α
within
␈↓ ↓H␈↓␈↓↓␈↓αif␈↓↓ s ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b␈↓,␈α∞then␈α∞we␈α∞need␈α∞only␈α∞prove␈α∞␈↓↓s ⊃ e =e'␈↓,␈α∞and␈α∞to␈α∞make␈α∞the␈α∞replacement␈α∞within␈α∞␈↓↓b␈↓␈α
we
␈↓ ↓H␈↓need only prove ␈↓↓¬s ⊃ e = e'␈↓.

␈↓ ↓H␈↓        More␈αfacts␈αabout␈αconditional␈αterms␈αare␈αgiven␈αin␈α[McCarthy␈α1963]␈αincluding␈αa␈αdiscussion␈αof
␈↓ ↓H␈↓canonical␈αforms␈αthat␈αparallels␈α
the␈αcanonical␈αforms␈αof␈α
Boolean␈αterms.␈α Any␈αquestion␈αof␈α
equivalence
␈↓ ↓H␈↓of␈α
conditional␈α∞terms␈α
is␈α
decidable␈α∞by␈α
truth␈α∞tables␈α
analogously␈α
to␈α∞the␈α
decidability␈α∞of␈α
propositional
␈↓ ↓H␈↓sentences.



␈↓ ↓H␈↓10.  ␈↓αLambda-expressions.␈↓


␈↓ ↓H␈↓        The␈α∪only␈α∩rule␈α∪required␈α∩for␈α∪handling␈α∪lambda-expressions␈α∩in␈α∪first␈α∩order␈α∪logic␈α∪is␈α∩called
␈↓ ↓H␈↓␈↓↓lambda-conversion.␈↓  Essentially it is

␈↓ ↓H␈↓␈↓ βt␈↓↓[λx: e][a] =␈↓ < the result of substituting ␈↓↓a␈↓ for ␈↓↓x␈↓ in ␈↓↓e␈↓ >. 
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *67


␈↓ ↓H␈↓As examples of this rule, we have

␈↓ ↓H␈↓␈↓ ∧+␈↓↓[λx: ␈↓αa|␈↓↓x . y][u . v] = [␈↓αa|␈↓↓[u . v]] . y .           ␈↓ 

␈↓ ↓H␈↓However,␈αa␈α
complication␈αrequires␈α
modifying␈αthe␈α
rule.␈α Namely,␈α
we␈αcan't␈α
substitute␈αfor␈α
a␈αvariable␈α
␈↓↓x␈↓
␈↓ ↓H␈↓an␈αexpression␈α␈↓↓e␈↓␈αthat␈αhas␈αa␈αfree␈αvariable␈α␈↓↓y␈↓␈αinto␈αa␈αcontext␈αin␈αwhich␈α␈↓↓y␈↓␈αis␈αbound.␈α Thus␈αit␈αwould␈αbe
␈↓ ↓H␈↓wrong␈αto␈αsubstitute␈α␈↓↓y + z␈↓␈αfor␈α␈↓↓x␈↓␈αin␈α␈↓↓∀y: [x + y = z]␈↓␈αor␈αinto␈αthe␈αterm␈α␈↓↓[λy: x + y][u + v]␈↓.␈α Before␈αdoing
␈↓ ↓H␈↓the␈αsubstitution,␈αthe␈αvariable␈α␈↓↓y␈↓␈αwould␈αhave␈αto␈αbe␈αreplaced␈αin␈αall␈αits␈αbound␈αoccurrences␈αby␈αa␈αfresh
␈↓ ↓H␈↓variable.␈α∞  In␈α∞order␈α∂to␈α∞handle␈α∞lambda␈α∞expressions␈α∂in␈α∞our␈α∞formal␈α∞proof␈α∂system␈α∞we␈α∞would␈α∂add␈α∞a
␈↓ ↓H␈↓"reduction"␈α∂rule␈α∂which␈α∂would␈α⊂derive␈α∂a␈α∂formula␈α∂␈↓↓s'␈↓␈α⊂from␈α∂a␈α∂formula␈α∂␈↓↓s␈↓␈α⊂where␈α∂␈↓↓s'␈↓␈α∂is␈α∂the␈α⊂result␈α∂of
␈↓ ↓H␈↓carrying out the above substitutions (carefully) on some subexpression of ␈↓↓s.␈↓

␈↓ ↓H␈↓        Lambda-expressions␈α∀can␈α∀always␈α∃be␈α∀eliminated␈α∀from␈α∃sentences␈α∀and␈α∀terms␈α∃by␈α∀lambda-
␈↓ ↓H␈↓conversion,␈αbut␈αthe␈αexpression␈αmay␈α
increase␈αgreatly␈αin␈αlength␈αif␈α
a␈αlengthy␈αterm␈αreplaces␈αa␈α
variable
␈↓ ↓H␈↓that␈α∞occurs␈α∞more␈α∞than␈α∞once␈α∞in␈α
␈↓↓e.␈↓␈α∞ It␈α∞is␈α∞easy␈α∞to␈α∞make␈α
an␈α∞expression␈α∞of␈α∞length␈α∞proportional␈α∞to␈α
␈↓↓n␈↓
␈↓ ↓H␈↓whose␈α⊃length␈α⊃is␈α⊃proportional␈α∩to␈α⊃2␈↓∧n␈↓␈α⊃after␈α⊃conversion␈α∩of␈α⊃its␈α⊃␈↓↓n␈↓␈α⊃nested␈α∩lambda-expressions.␈α⊃ For
␈↓ ↓H␈↓example

␈↓ ↓H␈↓        ␈↓ ∧U␈↓↓λx␈↓β1␈↓↓: [x␈↓β1␈↓↓.x␈↓β1␈↓↓][ ... [λx␈↓βn␈↓↓: [x␈↓βn␈↓↓.x␈↓βn␈↓↓][␈↓¬A␈↓↓]] ... ]␈↓ 

␈↓ ↓H␈↓becomes

␈↓ ↓H␈↓                ␈↓¬(A . A)␈↓,

␈↓ ↓H␈↓                ␈↓¬((A . A) . (A . A))␈↓,

␈↓ ↓H␈↓or

␈↓ ↓H␈↓                ␈↓¬((((A . A) . (A . A)) . ((A . A) . (A . A))) ␈↓
␈↓ ↓H␈↓                ␈↓¬  . ((A . A) . (A . A)) . ((A . A) . (A . A))))␈↓

␈↓ ↓H␈↓for ␈↓↓n␈↓ = 1, 2, or 4 respectively.



␈↓ ↓H␈↓11.  ␈↓αIntroducing facts about recursively defined functions.␈↓


␈↓ ↓H␈↓        The␈αonly␈α
remaining␈αtask␈αin␈α
our␈αdevelopment␈αof␈α
a␈αtechnique␈αfor␈α
proving␈αproperties␈αof␈α
LISP
␈↓ ↓H␈↓programs␈α∪is␈α∪to␈α∪explain␈α∪how␈α∪to␈α∪extend␈α∪the␈α∪theory␈α∪we␈α∪have␈α∪built␈α∪so␈α∪far␈α∪to␈α∀include␈α∪axioms
␈↓ ↓H␈↓characterizing␈α∞such␈α∞programs.␈α∞ In␈α∞this␈α∞section␈α∞we␈α∞will␈α∞explain␈α∞the␈α∞connection␈α∞between␈α
programs
␈↓ ↓H␈↓and␈αfunctions.␈α
  In␈αorder␈αto␈α
do␈αthis␈α
in␈αa␈α clean␈α
and␈αuniform␈α
manner,␈αwe␈αwill␈α
restrict␈αthe␈α
class␈αof
␈↓ ↓H␈↓programs␈αto␈αhave␈αa␈αparticular␈αform.␈α  This␈αdoesn't␈αreally␈αrestrict␈αour␈αcomputing␈αpower,␈α although
␈↓ ↓H␈↓it␈αdoes␈αlimit␈αthe␈αbag␈αof␈αtricks␈αfrom␈αwhich␈αwe␈αmay␈αchoose␈αwhen␈αwriting␈αa␈αprogram.␈α We␈αwill␈αthen
␈↓ ↓H␈↓treat␈αa␈αspecial␈αclass␈αof␈αprograms␈αwhich␈α"terminate␈αon␈αform".␈α Finally␈αwe␈αdescribe␈αthe␈αgeneral␈αform
␈↓ ↓H␈↓of␈α∞program␈α
and␈α∞show␈α∞how␈α
to␈α∞prove␈α∞that␈α
a␈α∞program␈α
terminates.␈α∞ We␈α∞will␈α
postpone␈α∞until␈α∞a␈α
later
␈↓ ↓H␈↓section␈α∂(␈↓π∞␈↓15)␈α∂discussing␈α⊂the␈α∂characterization␈α∂of␈α⊂programs␈α∂that␈α∂do␈α⊂not␈α∂terminate␈α∂for␈α⊂all␈α∂possible
␈↓ ↓H␈↓input␈α⊃values.␈α⊃  In␈α⊃later␈α⊃chapters␈α⊃we␈α⊃will␈α∩see␈α⊃how␈α⊃to␈α⊃extend␈α⊃our␈α⊃methods␈α⊃to␈α⊃larger␈α∩classes␈α⊃of
␈↓ ↓H␈↓programs and to properties that depend on the actual computation, not just the value obtained.

␈↓ ↓H␈↓        Suppose we have a recursive definition (program) of the form
␈↓ ↓H␈↓68␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓11.1)␈↓ ¬[␈↓↓f[x]←␈↓πt␈↓↓[f,x]       ␈↓ 

␈↓ ↓H␈↓where␈α␈↓πt␈↓␈αis␈αa␈αsuitably␈αnice␈αLISP␈αterm␈αinvolving␈αthe␈αvariable␈α␈↓↓x,␈↓␈αthe␈αfunction␈αname␈α␈↓↓f␈↓␈αand␈αperhaps
␈↓ ↓H␈↓other␈α
known␈α
functions␈α
and␈α
S-expression␈α
constants.␈α
 (The␈α
phrase␈α
"suitably␈α
nice"␈α
will␈α
be␈αmade␈α
more
␈↓ ↓H␈↓precise␈αshortly.)␈α
 Then␈αusing␈αthe␈α
rules␈αfor␈αcomputation␈α
given␈αin␈α
Chapter␈αI␈αwe␈α
can␈αshow␈αthat␈α
there
␈↓ ↓H␈↓is a unique function, which we also call ␈↓↓f,␈↓ on the extended S-expressions such that

␈↓ ↓H␈↓␈↓ αλ(i)  ␈↓↓f[x] = ␈↓πt␈↓↓[f,x]␈↓ for all S-expressions ␈↓↓x.␈↓

␈↓ ↓H␈↓␈↓ αλ(ii)␈α
 ␈↓↓f␈↓␈α
is␈α
the␈α
"least␈α
defined"␈α
such␈α
function.␈α
 That␈α
is,␈α
if␈α
␈↓↓g␈↓␈α
is␈α
any␈α
other␈α
function␈α
satisfying␈α
(i),
␈↓ ↓H␈↓␈↓ βλthen␈α
whenever␈α∞␈↓↓f[x]␈↓␈α
is␈α∞defined␈α
(eg.␈α∞not␈α
␈↓π|␈↓)␈α∞for␈α
an␈α∞S-expression␈α
␈↓↓x␈↓␈α∞then␈α
␈↓↓g[x]␈↓␈α∞is␈α
also
␈↓ ↓H␈↓␈↓ βλdefined and ␈↓↓f[x] = g[x]␈↓.

␈↓ ↓H␈↓␈↓ αλ(iii)␈α For␈αany␈α
particular␈αS-expression␈α␈↓↓a,␈↓␈αthe␈α
value␈αof␈α␈↓↓f[a]␈↓␈αis␈α
an␈αS-expression␈α(defined)␈αif␈α
and
␈↓ ↓H␈↓␈↓ βλonly if the computation prescribed by (11.1) terminates with that same value.

␈↓ ↓H␈↓Thus␈α∞we␈α
see␈α∞that␈α
if␈α∞the␈α
computation␈α∞defined␈α
by␈α∞(11.1)␈α
always␈α∞terminates␈α
then␈α∞there␈α
is␈α∞only␈α
one
␈↓ ↓H␈↓solution␈αto␈αthe␈α
corresponding␈αfunctional␈αequation␈α
and␈αthat␈αis␈α
precisely␈αthe␈αfunction␈α
computed␈αby
␈↓ ↓H␈↓the␈αprogram.␈α Notice␈αalso␈αthat␈αif␈αwe␈αcan␈α
prove␈αthat␈α␈↓↓issexp␈αf[x]␈↓␈α using␈αthe␈αfunctional␈αequation␈α
and
␈↓ ↓H␈↓other␈α⊃facts␈α∩about␈α⊃LISP,␈α⊃then␈α∩we␈α⊃will␈α⊃have␈α∩shown␈α⊃that␈α⊃the␈α∩computation␈α⊃prescribed␈α∩by␈α⊃(11.1)
␈↓ ↓H␈↓terminates␈α∞with␈α∞a␈α∞well␈α∂defined␈α∞answer.␈α∞ In␈α∞any␈α∞case␈α∂there␈α∞is␈α∞a␈α∞"distinguished"␈α∞function␈α∂that␈α∞we
␈↓ ↓H␈↓associate␈αwith␈αthe␈αprogram␈αand␈αwe␈αare␈α
allowed␈αto␈αadd␈αthe␈αnew␈αfunction␈αsymbol␈αto␈α
our␈αlanguage,
␈↓ ↓H␈↓and␈α⊃add␈α∩to␈α⊃the␈α∩set␈α⊃of␈α∩axioms␈α⊃the␈α⊃functional␈α∩equation␈α⊃corresponding␈α∩to␈α⊃the␈α∩definition.␈α⊃ The
␈↓ ↓H␈↓meaning␈α∀of␈α∀this␈α∀new␈α∪symbol␈α∀will␈α∀be␈α∀the␈α∪distinguished␈α∀function␈α∀on␈α∀extended␈α∪S-expressions
␈↓ ↓H␈↓described above.

␈↓ ↓H␈↓        In␈α∞Chapter␈α∞II␈α
we␈α∞discussed␈α∞various␈α
forms␈α∞of␈α∞recursive␈α
definitions.␈α∞  Some␈α∞these␈α∞forms␈α
we
␈↓ ↓H␈↓claimed␈α
had␈αthe␈α
property␈αthat␈α
any␈αprogram␈α
fitting␈αthat␈α
form␈αterminates␈α
for␈αall␈α
allowed␈αinput.␈α
 We
␈↓ ↓H␈↓will␈α⊂not␈α⊂prove␈α⊂this␈α⊂statement,␈α⊂however␈α⊂the␈α∂method␈α⊂we␈α⊂give␈α⊂below␈α⊂for␈α⊂proving␈α⊂that␈α∂particular
␈↓ ↓H␈↓programs␈α
terminate␈αcan␈α
be␈αused␈α
to␈αjustify␈α
the␈αstatement␈α
in␈αthe␈α
sense␈αthat␈α
using␈αthis␈α
method␈αyou
␈↓ ↓H␈↓could␈α
prove␈α
termination␈α
for␈α
any␈α
particular␈α
program␈α
that␈α
fits␈α
one␈α
of␈α
these␈α
forms.␈α
  For␈α
reference
␈↓ ↓H␈↓we recall some of these forms for number, list and S-expression recursions.

␈↓ ↓H␈↓11.2)␈↓ βQ␈↓↓f[n,y] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ g[y] ␈↓αelse␈↓↓ h[n-␈↓¬1␈↓↓,y,f[n-␈↓¬1␈↓↓,j[n-␈↓¬1␈↓↓,y]]]␈↓. 

␈↓ ↓H␈↓11.3)␈↓ β ␈↓↓f[u,y] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ g[y] ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓u, ␈↓αd|␈↓↓u, y, f[␈↓αd|␈↓↓u, j[␈↓αa|␈↓↓u, ␈↓αd|␈↓↓u, y]]␈↓. 

␈↓ ↓H␈↓11.4)␈↓ α∩␈↓↓   f[x,y] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ g[x,y] ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓x,␈↓αd|␈↓↓x,y,f[␈↓αa|␈↓↓x,j1[␈↓αa|␈↓↓x,␈↓αd|␈↓↓x,y]], f[␈↓αd|␈↓↓x,j2[␈↓αa|␈↓↓x,␈↓αd|␈↓↓x,y]]]␈↓ 

␈↓ ↓H␈↓The␈αsymbols␈α␈↓↓g,␈↓␈α␈↓↓h,␈↓␈α␈↓↓j,␈↓␈α␈↓↓j1,␈↓␈α␈↓↓j2␈↓␈αoccuring␈αin␈αthese␈αdefinitions␈αmust␈αbe␈αfunctions␈αexpressions␈αdenoting
␈↓ ↓H␈↓known␈α∞functions␈α∂ whose␈α∞values␈α∂are␈α∞of␈α∂the␈α∞appropriate␈α∞sort.␈α∂ In␈α∞particular␈α∂the␈α∞␈↓↓j␈↓'s␈α∂should␈α∞return
␈↓ ↓H␈↓values␈α∩matching␈α∩the␈α∩sort␈α∩of␈α∩the␈α∩second␈α∩argument␈α∩of␈α∩␈↓↓f,␈↓␈α∩and␈α∩␈↓↓g␈↓␈α∩and␈α∩␈↓↓h␈↓␈α∩should␈α∩return␈α∩values
␈↓ ↓H␈↓matching␈αthe␈α
sort␈αof␈αthe␈α
last␈αargument␈αof␈α
␈↓↓h.␈↓␈α The␈αterm␈α
"known␈αfunction"␈αmeans␈α
the␈αexpressions
␈↓ ↓H␈↓involve␈α
only␈α
previously␈αdefined␈α
function,␈α
predicate␈α
and␈αconstant␈α
 symbols.␈α
 Programs␈α
of␈αthe␈α
above
␈↓ ↓H␈↓forms␈αare␈α
in␈αthe␈αcategory␈α
of␈α"suitably␈αnice"␈α
Further,␈αthe␈αvalue␈α
of␈αthe␈αfunction␈α
computed␈αby␈αsuch␈α
a
␈↓ ↓H␈↓program␈α∞will␈α
be␈α∞in␈α
the␈α∞subdomain␈α
containing␈α∞the␈α∞values␈α
of␈α∞␈↓↓g␈↓␈α
and␈α∞␈↓↓h.␈↓␈α
 (For␈α∞readibility␈α∞we␈α
have
␈↓ ↓H␈↓included␈α
just␈α
a␈α
single␈α
parameter␈α
␈↓↓y␈↓␈α
in␈α
each␈α
of␈α
the␈α
above␈α
forms.␈α
 In␈α
general␈α
we␈α
allow␈α
any␈αnumber␈α
of
␈↓ ↓H␈↓parameters (including none) to appear, and they may be S-expression, list or number variables.)

␈↓ ↓H␈↓        Given␈αa␈α
recursive␈αdefinition␈αhaving␈α
one␈αof␈α
the␈αabove␈αforms␈α
we␈αmay␈α
add␈αthe␈αfunction␈α
name
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *69


␈↓ ↓H␈↓to␈α
our␈αlanguage␈α
and␈αthe␈α
corresponding␈αfunctional␈α
equation␈α
(fully␈αquantified)␈α
to␈αour␈α
list␈αof␈α
axioms.
␈↓ ↓H␈↓Furthermore␈αwe␈αmay␈αadd␈αan␈αaxiom␈αstating␈αthat␈αthe␈αvalue␈αof␈α␈↓↓f␈↓␈αis␈αin␈αthe␈αsubdomain␈α which␈αis␈αthe
␈↓ ↓H␈↓union of the possible values of ␈↓↓g␈↓ and ␈↓↓h.␈↓  An example should make this procedure clearer.

␈↓ ↓H␈↓α␈↓ εExample

␈↓ ↓H␈↓        Consider␈αthe␈αrecursive␈αdefinitions␈αof␈α␈↓↓append,␈↓␈α␈↓↓size,␈↓␈α␈↓↓fringe␈↓␈αand␈α␈↓↓length.␈↓␈α (We␈αassume␈αat␈αthis
␈↓ ↓H␈↓point␈α⊃that␈α∩"+"␈α⊃and␈α⊃simple␈α∩properties␈α⊃of␈α⊃numbers␈α∩are␈α⊃known␈α⊃and␈α∩will␈α⊃mainly␈α∩concentrate␈α⊃on
␈↓ ↓H␈↓techniques for proving facts about functions on lists and S-expressions.)

␈↓ ↓H␈↓11.5)␈↓ ∧M␈↓↓u*v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]␈↓. 

␈↓ ↓H␈↓11.6)␈↓ ∧&␈↓↓size x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x␈↓, 

␈↓ ↓H␈↓11.7)␈↓ βi␈↓↓fringe x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x␈↓, 

␈↓ ↓H␈↓11.8)␈↓ ∧:␈↓↓length u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓¬1␈↓↓ length ␈↓αd|␈↓↓u␈↓ 

␈↓ ↓H␈↓By␈α
the␈αrules␈α
give␈αabove,␈α
since␈αeach␈α
of␈α
these␈αdefinitions␈α
has␈αone␈α
of␈αthe␈α
forms␈α(11.3)␈α
or␈α
(11.4),␈αwe
␈↓ ↓H␈↓may add the following axioms to our theory:

␈↓ ↓H␈↓␈↓¬APPEND: ␈↓␈↓ βx␈↓↓∀u v:[u*v = ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓∀u v: islist u*v ␈↓

␈↓ ↓H␈↓␈↓¬SIZE: ␈↓␈↓ βx␈↓↓∀x:[size x  = ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓∀x: numberp size x␈↓

␈↓ ↓H␈↓␈↓¬FRINGE: ␈↓␈↓ βx␈↓↓∀x:[fringe x = ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓∀x: issexp fringe x␈↓

␈↓ ↓H␈↓␈↓¬LENGTH: ␈↓␈↓ βx␈↓↓∀u:[length u = ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓¬1␈↓↓+length ␈↓αd|␈↓↓u]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓∀u: numberp length u␈↓

␈↓ ↓H␈↓        Now we can use these new axioms to prove some properties.  In particular we shall prove

␈↓ ↓H␈↓␈↓ ∧?␈↓↓∀x: length fringe x = size x.                 ␈↓ 

␈↓ ↓H␈↓Since␈α
the␈αdefinitions␈α
of␈α
␈↓↓fringe␈↓␈αand␈α
␈↓↓size␈↓␈αare␈α
simple␈α
S-expression␈αrecursions,␈α
we␈αwill␈α
try␈α
using␈αthe
␈↓ ↓H␈↓simple␈α⊂S-expression␈α⊂induction␈α⊂schema,␈α⊂␈↓¬SEXPINDUCTION,␈α⊂␈↓␈α⊂given␈α⊂in␈α⊂section␈α⊂␈↓π∞␈↓6␈α⊂to␈α⊂carry␈α⊂out␈α⊂the
␈↓ ↓H␈↓proof.  Thus we let ␈↓πF␈↓ be given by

␈↓ ↓H␈↓␈↓ ∧=␈↓↓␈↓πF␈↓↓[x] ≡ [length fringe x = size x].           ␈↓ 

␈↓ ↓H␈↓        There are two cases to consider.  First if ␈↓↓x␈↓ is an atom then we have

␈↓ ↓H␈↓␈↓ α8␈↓↓length fringe x = length <x> ␈↓␈↓ ¬xby ␈↓¬FRINGE ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓= ␈↓¬1␈↓↓+length ␈↓αd|␈↓↓<x> ␈↓␈↓ ¬xby ␈↓¬LENGTH, ␈↓since ␈↓↓<x> = x . ␈↓¬NIL␈↓↓␈↓ is a non-null list.
␈↓ ↓H␈↓␈↓ β8␈↓↓= ␈↓¬1␈↓↓+length ␈↓¬NIL␈↓↓ ␈↓␈↓ ¬xby ␈↓¬CDR-CONS ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓= ␈↓¬1 ␈↓↓␈↓␈↓ ¬xby ␈↓¬LENGTH ␈↓and properties of +.
␈↓ ↓H␈↓␈↓ β8␈↓↓= size x ␈↓␈↓ ¬xby ␈↓¬SIZE ␈↓
␈↓ ↓H␈↓70␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓In the second case ␈↓↓x␈↓ is non-atomic and we have the induction hypothesis

␈↓ ↓H␈↓␈↓ βN␈↓↓[length fringe ␈↓αa|␈↓↓x = size ␈↓αa|␈↓↓x] ∧ [length fringe ␈↓αd|␈↓↓x = size ␈↓αd|␈↓↓x]␈↓ 

␈↓ ↓H␈↓We begin by expanding the definitions of ␈↓↓fringe␈↓ and ␈↓↓size␈↓ to obtain

␈↓ ↓H␈↓␈↓ α8␈↓↓length fringe x = length[fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x]␈↓␈↓ πXby ␈↓¬FRINGE ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α8␈↓↓size x = size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x ␈↓␈↓ πXby ␈↓¬SIZE ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓= [length fringe ␈↓αa|␈↓↓x] + [length fringe ␈↓αd|␈↓↓x]␈↓␈↓ πXby the induction hypohesis

␈↓ ↓H␈↓Since␈α␈↓↓fringe␈α␈↓αa|␈↓↓x␈↓␈αand␈α␈↓↓fringe␈α␈↓αd|␈↓↓x␈↓␈α are␈αlists␈α(¬␈↓αat|␈↓␈↓↓x␈↓␈αmeans␈α␈↓αa|␈↓␈↓↓x␈↓␈αand␈α␈↓αd|␈↓␈↓↓x␈↓␈αare␈αS-expressions␈αso␈αwe␈αmay␈α
use
␈↓ ↓H␈↓the␈α_second␈α_of␈α_the␈α↔␈↓¬FRINGE␈α_␈↓axioms)␈α_we␈α_are␈α_reduced␈α↔to␈α_proving␈α_something␈α_of␈α_the␈α↔form
␈↓ ↓H␈↓␈↓↓length[u*v]=length u + length v␈↓.␈α∞ This␈α∞suggests␈α∞that␈α∞we␈α∞prove␈α∞a␈α∞general␈α∞lemma,␈α∞ then␈α∂the␈α∞proof
␈↓ ↓H␈↓will be complete.  Thus we will prove

␈↓ ↓H␈↓␈↓ ∧,␈↓↓∀u v:[length[u*v]=length u + length v].      ␈↓ 

␈↓ ↓H␈↓Since␈α
the␈α
definition␈α
of␈α
␈↓↓append␈↓␈α
is␈α
by␈α
recursion␈α
on␈α
␈↓↓u␈↓␈α
we␈α
will␈α
use␈α
 simple␈α
list␈α
induction␈α
for␈α
the␈α
proof.
␈↓ ↓H␈↓Again there are two cases. In the first case we have ␈↓αn|␈↓␈↓↓u␈↓ and

␈↓ ↓H␈↓␈↓ α8␈↓↓length[␈↓¬NIL␈↓↓*v] = length v ␈↓␈↓ π8by ␈↓¬APPEND. ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓= ␈↓¬0 ␈↓↓+ length v ␈↓␈↓ π8by properties of ␈↓¬0 ␈↓and "+".
␈↓ ↓H␈↓␈↓ β8␈↓↓= length ␈↓¬NIL␈↓↓ + length v ␈↓␈↓ π8by ␈↓¬LENGTH. ␈↓

␈↓ ↓H␈↓In the second case we have ¬␈↓αn|␈↓␈↓↓u␈↓ and the induction hypothesis

␈↓ ↓H␈↓␈↓ ∧T␈↓↓length [␈↓αd|␈↓↓u]*v = length ␈↓αd|␈↓↓u + length v␈↓. 

␈↓ ↓H␈↓thus

␈↓ ↓H␈↓␈↓ α8␈↓↓length[u*v] = length[␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u]*v]␈↓␈↓ π8by ␈↓¬APPEND. ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓= ␈↓¬1␈↓↓ + length [␈↓αd|␈↓↓u]*v ␈↓␈↓ π8by ␈↓¬LENGTH ␈↓and ␈↓¬EQ-CONS. ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓= ␈↓¬1␈↓↓ + [length ␈↓αd|␈↓↓u + length v]␈↓␈↓ π8by induction hypothesis.
␈↓ ↓H␈↓␈↓ β8␈↓↓= length u + length v␈↓␈↓ π8by associativity of + and ␈↓¬LENGTH. ␈↓

␈↓ ↓H␈↓this completes the proof of the lemma and thus of the original theorem.

␈↓ ↓H␈↓        As␈αwe␈αmentioned␈αearlier,␈αthere␈αare␈αmany␈αprograms␈αthat␈αterminate␈αfor␈αall␈αallowed␈αinput,␈αbut
␈↓ ↓H␈↓do␈α∪not␈α∪fit␈α∪into␈α∩one␈α∪of␈α∪the␈α∪forms␈α∩(11.2)␈α∪to␈α∪(11.4).␈α∪ We␈α∩could␈α∪give␈α∪more␈α∪general␈α∪forms.␈α∩ [A
␈↓ ↓H␈↓particularly␈α
nice␈α
example␈α∞can␈α
be␈α
found␈α∞in␈α
Boyer␈α
and␈α
Moore[1978]].␈α∞ There␈α
are␈α
two␈α∞reasons␈α
for
␈↓ ↓H␈↓not␈α∂proceeding␈α∞in␈α∂this␈α∞direction␈α∂now.␈α∞ One␈α∂is␈α∞that␈α∂the␈α∞more␈α∂general␈α∞forms␈α∂are␈α∞correspondingly
␈↓ ↓H␈↓more␈α
difficult␈αto␈α
recognize,␈αthe␈α
other␈αis␈α
that␈αin␈α
order␈αto␈α
include␈αthe␈α
most␈αgeneral␈α
class␈αof␈α
programs
␈↓ ↓H␈↓we␈α∂will␈α∂eventually␈α∂have␈α∂to␈α∂allow␈α∂a␈α∂recursive␈α∂definitions␈α∂that␈α∂are␈α∂not␈α∂necessarily␈α∂total␈α∂on␈α∞form.
␈↓ ↓H␈↓(This is a consequence of the "halting problem" which we will say more about in Chapter X.)

␈↓ ↓H␈↓        Our␈αmost␈αgeneral␈αform␈αof␈αrecursive␈αdefinition␈αin␈αthe␈αcategory␈αof␈α"suitably␈αnice"␈αdefinitions
␈↓ ↓H␈↓has the following basic form

␈↓ ↓H␈↓11.9)␈↓ ∧q␈↓↓f[x1, ... xn] ← ␈↓πt␈↓↓[f,x1, ... ,xn]␈↓ 
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *71


␈↓ ↓H␈↓where␈α⊃the␈α⊃term␈α⊃␈↓↓␈↓πt␈↓↓[f,x1,␈α⊃...␈α⊃,xn]␈↓␈α⊃is␈α⊃formed␈α⊃like␈α⊃a␈α⊃term␈α⊃in␈α⊃our␈α⊃language␈α⊃with␈α⊃the␈α⊂following
␈↓ ↓H␈↓restrictions.␈α
 No␈αquantifiers␈α
appear.␈α
 The␈αsymbol␈α
␈↓π|␈↓␈αdoes␈α
not␈α
appear.␈α The␈α
function␈α
symbols␈αthat
␈↓ ↓H␈↓appear␈αmust␈αbe␈αthose␈αof␈αthe␈αbasic␈αlanguage,␈αpreviously␈αrecursively␈αdefined,␈αor␈αthe␈αsymbol␈α␈↓↓f.␈↓␈α
 The
␈↓ ↓H␈↓predicate␈αsymbols␈αare␈α
those␈αof␈αthe␈α
basic␈αlanguage.␈α (We␈α
will␈αrelax␈αthis␈α
when␈αwe␈αdevelop␈αa␈α
method
␈↓ ↓H␈↓of␈α⊃defining␈α⊂new␈α⊃predicates.)␈α⊃ Formulas␈α⊂appearing␈α⊃in␈α⊃the␈α⊂␈↓αif␈↓␈α⊃part␈α⊃of␈α⊂a␈α⊃conditional␈α⊃are␈α⊂further
␈↓ ↓H␈↓restricted␈α∀by␈α∀not␈α∀allowing␈α∀␈↓↓f␈↓␈α∀to␈α∀appear␈α∀and␈α∀further␈α∀all␈α∀terms␈α∀that␈α∀appear␈α∀must␈α∃denote␈α∀S-
␈↓ ↓H␈↓expressions.␈α The␈αpoint␈αof␈αthese␈αlatter␈αrestrictions␈αis␈αmainly␈αto␈αavoid␈αthe␈αpossibility␈αof␈αhaving␈αthe
␈↓ ↓H␈↓␈↓αif␈↓␈α
part␈α
of␈αa␈α
conditional␈α
undefined.␈α  If␈α
we␈α
think␈αof␈α
the␈α
term␈α␈↓πt␈↓␈α
as␈α
arising␈αfrom␈α
the␈α
constructs␈αof
␈↓ ↓H␈↓our␈α
programming␈α
language␈αthe␈α
problem␈α
with␈αquantifiers␈α
does't␈α
arise,␈αas␈α
we␈α
have␈α
no␈αquantifiers.
␈↓ ↓H␈↓However␈α
it␈α
is␈α
convenient␈α
to␈α
be␈α
able␈α
to␈α
imbed␈α
the␈α
description␈α
in␈α
the␈α
language␈α
of␈α
our␈α
theory␈αas␈α
well.
␈↓ ↓H␈↓We␈α⊃can␈α∩further␈α⊃generalize␈α⊃the␈α∩form␈α⊃of␈α⊃definition␈α∩by␈α⊃allowing␈α⊃mutually␈α∩recursive␈α⊃definitions.
␈↓ ↓H␈↓Thus

␈↓ ↓H␈↓␈↓ ∧8␈↓↓f1[x1, ... xn] ← ␈↓πt␈↓↓1[f1, ... ,fm,x1, ... ,xn]␈↓
␈↓ ↓H␈↓␈↓ ¬8.
␈↓ ↓H␈↓11.10)␈↓ ¬8.
␈↓ ↓H␈↓␈↓ ¬8.
␈↓ ↓H␈↓␈↓ ∧8␈↓↓fm[x1, ... xn] ← ␈↓πt␈↓↓m[f1, ... ,fm,x1, ... ,xn]␈↓

␈↓ ↓H␈↓is␈αa␈α
"suitably␈αnice"␈αcollection␈α
of␈αdefinitions␈αif␈α
the␈αterms␈α␈↓πt␈↓␈↓↓i␈↓␈α
satisfy␈αthe␈αrestrictions␈α
above,␈αallowing
␈↓ ↓H␈↓any␈α
the␈α␈↓↓fi␈↓␈α
to␈αappear␈α
where␈α
␈↓↓f␈↓␈αwas␈α
allowed␈αthe␈α
single␈α
definition␈αcase.␈α
  We␈αwill␈α
see␈αseveral␈α
examples
␈↓ ↓H␈↓of mutually recursive definitions in later sections.

␈↓ ↓H␈↓α␈↓ εExample

␈↓ ↓H␈↓        As␈α
a␈α
final␈α
example␈α
for␈α
this␈α
section␈α
we␈α
will␈α
prove␈α
that␈α
the␈α
alternate␈α
definition,␈α∞␈↓↓flatten,␈↓␈α
of
␈↓ ↓H␈↓␈↓↓fringe␈↓␈α∃is␈α∃"correct",␈α⊗namely␈α∃that␈α∃it␈α⊗computes␈α∃the␈α∃same␈α∃function␈α⊗as␈α∃␈↓↓fringe␈↓␈α∃does.␈α⊗ This␈α∃will
␈↓ ↓H␈↓demonstrate␈αtwo␈αmore␈αideas.␈α First,␈αthe␈αdefinition␈αis␈α
allowed␈αby␈αthe␈αgeneral␈αschema␈α(11.9),␈αbut␈αit␈α
is
␈↓ ↓H␈↓not␈α
one␈α
of␈α
the␈α
forms␈α
(11.2),␈α
(11.3),␈α
or␈α
(11.4).␈α
 Thus␈α
we␈α
may␈α
introduce␈α
the␈α
appropriate␈α
functional
␈↓ ↓H␈↓equation(s)␈α∂as␈α∂axioms␈α∂in␈α∂our␈α∂theory,␈α∂but␈α∂we␈α⊂will␈α∂have␈α∂to␈α∂prove␈α∂that␈α∂the␈α∂value␈α∂of␈α⊂the␈α∂defined
␈↓ ↓H␈↓function␈α
is␈α∞always␈α
a␈α
list.␈α∞  Second,␈α
the␈α
definition␈α∞of␈α
␈↓↓flatten␈↓␈α
involves␈α∞an␈α
auxiliary␈α∞definition␈α
and
␈↓ ↓H␈↓more␈αimportantly␈αthis␈αauxiliary␈αdefinition␈αuses␈α
an␈αauxiliary␈αvariable,␈αthus␈αwe␈αwill␈αhave␈α
to␈αprove
␈↓ ↓H␈↓something␈α∞more␈α
general␈α∞than␈α
we␈α∞might␈α
have␈α∞imagined␈α
in␈α∞order␈α
for␈α∞the␈α
induction␈α∞step␈α∞to␈α
work.
␈↓ ↓H␈↓Recall the definitions

␈↓ ↓H␈↓␈↓ ∧t␈↓↓flatten[x] ← flat[x, ␈↓¬NIL␈↓↓]           ␈↓
␈↓ ↓H␈↓11.11)
␈↓ ↓H␈↓␈↓ βs␈↓↓flat[x,u] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x.u ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,u]]␈↓.

␈↓ ↓H␈↓We add to our theory the axioms

␈↓ ↓H␈↓␈↓¬FLATTEN: ␈↓␈↓ βx␈↓↓∀x:[flatten[x] = flat[x, ␈↓¬NIL␈↓↓]]␈↓

␈↓ ↓H␈↓␈↓¬FLAT: ␈↓␈↓ βx␈↓↓∀x u:[flat[x,u] = ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x.u ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,u]]]␈↓.

␈↓ ↓H␈↓First we will prove

␈↓ ↓H␈↓␈↓ ∧y␈↓↓∀x u: islist flat[x,u].              ␈↓ 

␈↓ ↓H␈↓This␈α∞tells␈α∞us␈α∞that␈α
the␈α∞progam␈α∞for␈α∞␈↓↓flat␈↓␈α
terminates␈α∞for␈α∞all␈α∞input␈α
pairs␈α∞␈↓↓[x,u]␈↓␈α∞and␈α∞further␈α∞that␈α
the
␈↓ ↓H␈↓72␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓term␈α∂␈↓↓flat[x,u]␈↓␈α∂may␈α∂be␈α∂used␈α∞whereever␈α∂a␈α∂list␈α∂is␈α∂expected.␈α∞ Since␈α∂the␈α∂definition␈α∂is␈α∂by␈α∂simple␈α∞S-
␈↓ ↓H␈↓expression␈αrecursion␈αwe␈αuse␈α
the␈αsimple␈αS-expression␈αinduction␈α
schema␈αfor␈αthe␈αproof.␈α Again␈α
there
␈↓ ↓H␈↓are␈α∞two␈α
cases.␈α∞ In␈α
the␈α∞case␈α
␈↓αat|␈↓␈↓↓x␈↓␈α∞we␈α∞have␈α
␈↓↓flat[x,u] = x . u␈↓␈α∞and␈α
by␈α∞␈↓¬CONS␈α
␈↓we␈α∞have␈α∞␈↓↓islist␈α
flat[x,u]␈↓.
␈↓ ↓H␈↓In the case ¬␈↓αat|␈↓␈↓↓x,␈↓ we have the induction hypothesis

␈↓ ↓H␈↓␈↓ ∧4␈↓↓∀u: islist flat[␈↓αa|␈↓↓x,u] ∧ ∀u: islist flat[␈↓αd|␈↓↓x,u]␈↓. 

␈↓ ↓H␈↓By␈α␈↓¬FLAT␈α
␈↓we␈αhave␈α␈↓↓flat[x,u]␈α
=␈αflat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,u]]␈↓.␈α
Using␈αthe␈α␈↓αd␈↓-part␈α
of␈αthe␈α
hypothesis␈αwe␈αthen␈α
can
␈↓ ↓H␈↓apply␈α⊂the␈α⊂␈↓αa␈↓-part␈α⊂with␈α⊂␈↓↓u␈↓␈α⊂replaced␈α⊂by␈α∂␈↓↓flat[␈↓αd|␈↓↓x,u]␈↓␈α⊂and␈α⊂this␈α⊂completes␈α⊂the␈α⊂proof.␈α⊂ An␈α∂immediate
␈↓ ↓H␈↓consequence of this proof and ␈↓¬FLATTEN ␈↓is that ␈↓↓∀x: islist flatten x␈↓.

␈↓ ↓H␈↓        Now␈α↔we␈α↔will␈α↔prove␈α↔␈↓↓∀x:␈α↔flatten␈α↔x␈α_=␈α↔fringe␈α↔x␈↓.␈α↔  By␈α↔␈↓¬FLATTEN␈α↔␈↓this␈α↔is␈α↔the␈α_same␈α↔as
␈↓ ↓H␈↓␈↓↓∀x: flat[x,␈↓¬NIL␈↓↓] = fringe x␈↓.␈α∞  The␈α∞simple␈α∞principles␈α∞we␈α∞have␈α∞used␈α∞so␈α∞far␈α∞suggest␈α∞that␈α∞we␈α
proceed
␈↓ ↓H␈↓directly␈αwith␈αa␈αproof␈αby␈αS-expression␈αinduction␈αon␈α␈↓↓x.␈↓␈α This␈αunfortunately␈αarrives␈αat␈αa␈αdead␈αend.
␈↓ ↓H␈↓(The␈α∂reader␈α∂is␈α∂encouraged␈α∂to␈α∞try␈α∂it␈α∂and␈α∂see.)␈α∂ The␈α∂difficulty␈α∞is␈α∂that␈α∂we␈α∂begin␈α∂with␈α∂the␈α∞second
␈↓ ↓H␈↓argument␈α␈↓¬NIL␈↓,␈α
but␈αafter␈α
expanding␈αthe␈αdefinition␈α
once␈αthis␈α
is␈αno␈αlonger␈α
the␈αcase.␈α
 Just␈αas␈αwe␈α
have
␈↓ ↓H␈↓to␈α
carry␈α
along␈α
an␈αextra␈α
variable␈α
in␈α
the␈αcomputation␈α
we␈α
also␈α
do␈αin␈α
the␈α
proof.␈α
 There␈α
are␈αseveral
␈↓ ↓H␈↓possible routes to take.  We will prove the following more general statement

␈↓ ↓H␈↓␈↓ ∧F␈↓↓∀x u: flat[x,u] = fringe x * u.            ␈↓ 

␈↓ ↓H␈↓Clearly␈α⊃the␈α⊂statement␈α⊃we␈α⊂started␈α⊃to␈α⊂prove␈α⊃is␈α⊃a␈α⊂direct␈α⊃consequence␈α⊂of␈α⊃this␈α⊂one.␈α⊃ Now␈α⊃we␈α⊂may
␈↓ ↓H␈↓proceed by simple S-expression induction.   In the case that ␈↓↓x␈↓ is an atom we have:

␈↓ ↓H␈↓␈↓ ↓x␈↓↓flat[x,u] = x . u␈↓␈↓ ε8by ␈↓¬FLAT ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓= <x> * u␈↓␈↓ ε8by ␈↓¬APPEND, ␈↓since ␈↓↓<x> = x . ␈↓¬NIL␈↓↓␈↓.
␈↓ ↓H␈↓␈↓ αx␈↓↓= fringe x * u␈↓␈↓ ε8by ␈↓¬FRINGE ␈↓

␈↓ ↓H␈↓In the case that ␈↓↓x␈↓ is non-atomic we have the induction hypothesis:

␈↓ ↓H␈↓␈↓ β&␈↓↓∀u: flat[␈↓αa|␈↓↓x,u] = fringe ␈↓αa|␈↓↓x * u ∧ ∀u: flat[␈↓αd|␈↓↓x,u] = fringe ␈↓αd|␈↓↓x * u␈↓. 

␈↓ ↓H␈↓and we have

␈↓ ↓H␈↓␈↓ ↓x␈↓↓flat[x,u] = flat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,u]]␈↓␈↓ ε8by ␈↓¬FLAT ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓= flat[␈↓αa|␈↓↓x,fringe ␈↓αd|␈↓↓x * u]␈↓␈↓ ε8by induction hypothesis
␈↓ ↓H␈↓␈↓ αx␈↓↓= fringe ␈↓αa|␈↓↓x * [fringe ␈↓αd|␈↓↓x * u]␈↓␈↓ ε8by induction hypothesis using ␈↓↓islist fringe ␈↓αd|␈↓↓x * u␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓= fringe x * u␈↓␈↓ ε8by associativity of ␈↓↓append,␈↓

␈↓ ↓H␈↓This␈α∪completes␈α∪the␈α∩proof.␈α∪ Notice␈α∪that␈α∪in␈α∩order␈α∪to␈α∪apply␈α∩the␈α∪induction␈α∪hypothesis␈α∪and␈α∩the
␈↓ ↓H␈↓associativity of ␈↓↓append␈↓ it is neseccary to know that ␈↓↓islist fringe ␈↓αd|␈↓↓x ␈↓ and ␈↓↓islist fringe ␈↓αa|␈↓↓x␈↓.



␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.␈α Review␈αyour␈αproofs␈αof␈αproperties␈αof␈α␈↓↓reverse␈↓␈αfrom␈αthe␈αexercises␈αof␈αsection␈α␈↓π∞␈↓1␈αto␈αsee␈αthat␈αthey␈αdo
␈↓ ↓H␈↓indeed fit into our formalism.
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *73



␈↓ ↓H␈↓2.␈α You␈α
were␈αasked␈α
to␈αwrite␈α
definitions␈αfor␈α
the␈αfunction␈α
␈↓↓mirror␈↓␈αin␈α
the␈αexercises␈α
of␈αChapter␈α
II␈α␈↓π∞␈↓8.
␈↓ ↓H␈↓Use␈α
the␈α
previously␈α
given␈α
definitions␈α
of␈α
␈↓↓fringe␈↓␈α
and␈α
␈↓↓reverse␈↓␈α
(and␈α
any␈α
facts␈α
proved␈α
about␈α∞them␈α
so
␈↓ ↓H␈↓far) to show that your program satisfies

␈↓ ↓H␈↓␈↓ ∧S␈↓↓∀x:[reverse fringe x = fringe mirror x]␈↓. 



␈↓ ↓H␈↓12.  ␈↓αRecursively Defined Predicates.␈↓


␈↓ ↓H␈↓        In␈α
the␈α
last␈α
section␈α
we␈α
discussed␈α
how␈α
to␈α
introduce␈α
facts␈α
about␈α
recursively␈α
defined␈αfunctions
␈↓ ↓H␈↓into␈α⊂our␈α∂theory␈α⊂of␈α⊂LISP.␈α∂ We␈α⊂discovered␈α⊂that␈α∂for␈α⊂suitably␈α⊂nice␈α∂recursive␈α⊂definitions␈α⊂we␈α∂could
␈↓ ↓H␈↓translate␈αthe␈α
definition␈αdirectly␈αinto␈α
a␈αfirst-order␈αsentence␈α
by␈αreplacing␈α"←"␈α
by␈α"="␈αand␈α
quantifying
␈↓ ↓H␈↓over␈αthe␈αformal␈αparameters.␈α In␈αChapter␈αI␈αwe␈αintroduced␈αpropositional␈αconstructs␈α␈↓↓and,␈↓␈α␈↓↓or␈↓␈αand␈α
␈↓↓not␈↓
␈↓ ↓H␈↓into␈α⊃the␈α⊂programing␈α⊃language␈α⊃with␈α⊂the␈α⊃intent␈α⊃that␈α⊂they␈α⊃should␈α⊃behave␈α⊂like␈α⊃the␈α⊃usual␈α⊂logical
␈↓ ↓H␈↓operators.␈α∪ We␈α∪even␈α∪represented␈α∪these␈α∩operators␈α∪by␈α∪the␈α∪corresponding␈α∪logical␈α∪symbols␈α∩when
␈↓ ↓H␈↓describing␈α⊃programs␈α⊃by␈α∩recursive␈α⊃definitions␈α⊃in␈α∩external␈α⊃form.␈α⊃ We␈α∩also␈α⊃treated␈α⊃some␈α∩of␈α⊃the
␈↓ ↓H␈↓recursive␈αdefinitions␈αas␈α
though␈αthey␈αdefined␈α
predicates␈αrather␈αthan␈α
functions␈α(for␈αexample␈α
␈↓↓member␈↓
␈↓ ↓H␈↓and␈α␈↓↓equal).␈↓␈α
 Thus␈αwe␈α
have␈αexpressions␈α
in␈αexternal␈α
form␈αsuch␈α
as␈α␈↓↓␈↓αif␈↓↓␈α
member[x,y]␈α␈↓αthen␈↓↓␈α
...␈↓␈αrather
␈↓ ↓H␈↓than ␈↓↓␈↓αif␈↓↓ member[x,y]≠␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ ... ␈↓.

␈↓ ↓H␈↓        How␈α⊃can␈α⊃we␈α⊃translate␈α⊃a␈α⊃recursive␈α⊃definition␈α⊃into␈α⊃a␈α⊃ first␈α⊃order␈α⊃sentence␈α⊃that␈α⊃defines␈α⊂a
␈↓ ↓H␈↓predicate?␈α∂  Suppose␈α∞we␈α∂try␈α∞just␈α∂replacing␈α∞"←"␈α∂by␈α∞"≡"␈α∂in␈α∞the␈α∂recursive␈α∞definition.␈α∂ Consider␈α∞the
␈↓ ↓H␈↓following definition:

␈↓ ↓H␈↓12.1) ␈↓ ¬]␈↓↓lose x ← ¬lose x ␈↓. 

␈↓ ↓H␈↓This would translate into

␈↓ ↓H␈↓12.2)␈↓ ¬B␈↓↓∀x:[lose x ≡ ¬lose x] ␈↓. 

␈↓ ↓H␈↓which␈α
clearly␈α
loses!␈α
 We␈α
avoided␈α
a␈α
similiar␈α
problem␈α
for␈α
function␈α
definitions␈α
by␈α
adding␈αthe␈α
element
␈↓ ↓H␈↓␈↓π|␈↓␈α∪to␈α∪the␈α∀domain␈α∪thus␈α∪providing␈α∪a␈α∀non␈α∪S-expression␈α∪value␈α∪to␈α∀assign␈α∪to␈α∪a␈α∪term␈α∀when␈α∪the
␈↓ ↓H␈↓computation␈αdid␈αnot␈αproduce␈αa␈αvalue.␈α An␈αanalogous␈αsolution␈αfor␈αpredicates␈αis␈αpossible.␈α Namely,
␈↓ ↓H␈↓we␈α⊂could␈α⊂use␈α⊂a␈α⊂three␈α⊂valued␈α⊂logic␈α⊂where␈α⊂one␈α⊂of␈α⊂the␈α⊂values␈α⊂represents␈α⊂the␈α⊂undefined␈α∂element.
␈↓ ↓H␈↓This␈α∞makes␈α∞reasoning␈α∞in␈α∞the␈α∞logic␈α∞unnecessarily␈α∞complicated␈α∞as␈α∞there␈α∞are␈α∞now␈α∞have␈α∂three␈α∞cases
␈↓ ↓H␈↓instead␈αof␈αtwo␈αwhen␈αexamining␈αpossible␈αtruthvalues.␈α  An␈αalternate␈αsolution,␈αwhich␈αwe␈αwill␈αuse,␈αis
␈↓ ↓H␈↓the␈α∞following.␈α∂ We␈α∞continue␈α∂to␈α∞view␈α∂recursive␈α∞definitions␈α∂as␈α∞defining␈α∂functions.␈α∞ Some␈α∂of␈α∞these
␈↓ ↓H␈↓functions␈α
will␈α
be␈αintended␈α
to␈α
represent␈αpredicates␈α
in␈α
the␈αsense␈α
that␈α
the␈αpredicate␈α
is␈α
true␈α
only␈αfor
␈↓ ↓H␈↓those values of the arguments for which the function has a non ␈↓¬NIL␈↓ value.

␈↓ ↓H␈↓        For example consider the recursive program

␈↓ ↓H␈↓12.3)␈↓ βε␈↓↓subexpf[x, y] ← [x = y] ∨ [¬␈↓αat|␈↓↓y ∧ [subexpf[x, ␈↓αa|␈↓↓y] ∨ subexpf[x, ␈↓αd|␈↓↓y]]]␈↓. 

␈↓ ↓H␈↓Our intent is to define a predicate ␈↓↓subexp␈↓ satisfying the equivalence

␈↓ ↓H␈↓12.4)␈↓ αm␈↓↓∀x y: [subexp[x, y] ≡ [x = y] ∨ [¬␈↓αat|␈↓↓y ∧ [subexp[x, ␈↓αa|␈↓↓y] ∨ subexp[x, ␈↓αd|␈↓↓y]]]]␈↓. 
␈↓ ↓H␈↓74␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓First␈α
we␈α
must␈α
express␈α
the␈α
function␈α
definition␈α
as␈α
an␈α
allowed␈α
term.␈α
 We␈α
add␈α
primitive␈α
functions␈α
␈↓↓iff,␈↓
␈↓ ↓H␈↓␈↓↓and,␈↓␈α␈↓↓or,␈↓␈α␈↓↓not,␈↓␈α␈↓↓equal,␈↓␈α␈↓↓atom,␈↓␈αand␈α␈↓↓null␈↓␈α
to␈αour␈αlanguage␈αand␈αaxiomatize␈αthem␈αto␈αcorrespond␈α
to␈αthe
␈↓ ↓H␈↓LISP␈αprograms␈αas␈αdescribed␈αin␈αChapter␈αI.␈α Then␈αwe␈αcan␈αtranslate␈αthe␈αrecursive␈αdefinition␈αinto␈αa
␈↓ ↓H␈↓first order sentence as follows:

␈↓ ↓H␈↓12.5)␈↓ αε␈↓↓   ∀x y: subexpf[x, y] = [x equal y] or [not atom y and [subexpf[x, ␈↓αa|␈↓↓y] or subexpf[x, ␈↓αd|␈↓↓y]]]␈↓ 

␈↓ ↓H␈↓We can define the predicate ␈↓↓subexp␈↓ by

␈↓ ↓H␈↓12.6)␈↓ β≥␈↓↓∀x y: [subexp[x, y] ≡ issexp subexpf[x, y] ∧ subexpf[x, y] ≠ ␈↓¬NIL␈↓↓]␈↓. 

␈↓ ↓H␈↓Using this equation for ␈↓↓subexpf␈↓ and the S-expression induction axiom we can show

␈↓ ↓H␈↓12.7)␈↓ ∧y␈↓↓∀x y: issexp subexpf[x, y].       ␈↓ 

␈↓ ↓H␈↓which is the statement that ␈↓↓subexpf␈↓ is total.  It then follows that

␈↓ ↓H␈↓12.8)␈↓ ∧2␈↓↓∀x y: [subexp[x, y] ≡ subexpf[x, y] ≠ ␈↓¬NIL␈↓↓]␈↓. 

␈↓ ↓H␈↓and␈α
we␈αcan␈α
prove␈α
the␈αoriginal␈α
sentence␈α
proposed␈αfor␈α
␈↓↓subexp␈↓␈α
using␈αthe␈α
definition␈α
and␈αproperties␈α
of
␈↓ ↓H␈↓the␈α⊃propositional␈α⊃functions.␈α⊂ We␈α⊃will␈α⊃sketch␈α⊃the␈α⊂proof␈α⊃after␈α⊃giving␈α⊂the␈α⊃axioms␈α⊃for␈α⊃the␈α⊂newly
␈↓ ↓H␈↓introduced functions and stating some useful consequences.


␈↓ ↓H␈↓αAxioms for propositional functions.

␈↓ ↓H␈↓␈↓¬IFF: ␈↓␈↓ α8␈↓↓∀Z X Y: [iff[Z, X, Y] = ␈↓αif␈↓↓ ¬issexp Z ␈↓αthen␈↓↓ ␈↓π|␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ [Z ≠ ␈↓¬NIL␈↓↓]  ␈↓αthen␈↓↓ X ␈↓αelse␈↓↓ Y]␈↓

␈↓ ↓H␈↓␈↓¬NOT: ␈↓␈↓ α8␈↓↓∀X: [not X = iff[X, ␈↓¬NIL␈↓↓, ␈↓¬T␈↓↓]]␈↓

␈↓ ↓H␈↓␈↓¬AND: ␈↓␈↓ α8␈↓↓∀X Y: [X and Y = iff[X,Y,␈↓¬NIL␈↓↓]]␈↓

␈↓ ↓H␈↓␈↓¬OR: ␈↓␈↓ α8␈↓↓∀X Y: [X or Y = iff[X, X, Y]]␈↓

␈↓ ↓H␈↓␈↓¬EQUAL: ␈↓␈↓ α8␈↓↓∀X Y: [X equal Y] = ␈↓αif␈↓↓ [¬issexp X ∨ ¬issexp Y] ␈↓αthen␈↓↓ ␈↓π|␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ [X = Y] ␈↓αthen␈↓↓ ␈↓¬T␈↓↓ ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓␈↓¬ATOM: ␈↓␈↓ α8␈↓↓∀X: [atom X = ␈↓αif␈↓↓ [¬issexp X] ␈↓αthen␈↓↓ ␈↓π|␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓X ␈↓αthen␈↓↓ ␈↓¬T␈↓↓ ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓␈↓¬NULL: ␈↓␈↓ α8␈↓↓∀X: [null X = ␈↓αif␈↓↓ [¬issexp X] ␈↓αthen␈↓↓ ␈↓π|␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓X ␈↓αthen␈↓↓ ␈↓¬T␈↓↓ ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓]␈↓


␈↓ ↓H␈↓αUseful properties of propositional functions.

␈↓ ↓H␈↓␈↓¬SEXPIFF: ␈↓␈↓ βx␈↓↓∀x y z:  issexp iff[x, y, z]␈↓

␈↓ ↓H␈↓␈↓¬SEXPNOT: ␈↓␈↓ βx␈↓↓∀x: issexp not x␈↓

␈↓ ↓H␈↓␈↓¬SEXPAND: ␈↓␈↓ βx␈↓↓∀x y: issexp [x and y]␈↓

␈↓ ↓H␈↓␈↓¬SEXPOR: ␈↓␈↓ βx␈↓↓∀x y: issexp [x or y]␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *75


␈↓ ↓H␈↓␈↓¬SEXPEQUAL: ␈↓␈↓ βx␈↓↓∀x y: issexp [x equal y]␈↓

␈↓ ↓H␈↓␈↓¬SEXPATOM: ␈↓␈↓ βx␈↓↓∀x: issexp atom x␈↓

␈↓ ↓H␈↓␈↓¬EQIFF: ␈↓␈↓ βx␈↓↓∀x y z: iff[x, y, z] = ␈↓αif␈↓↓ x ≠ ␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ y ␈↓αelse␈↓↓ z␈↓

␈↓ ↓H␈↓␈↓¬EQNOT: ␈↓␈↓ βx␈↓↓∀x: [not x = ␈↓¬NIL␈↓↓ ≡ x ≠ ␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓␈↓¬EQAND: ␈↓␈↓ βx␈↓↓∀x y: [x and y ≠ ␈↓¬NIL␈↓↓ ≡ [x ≠ ␈↓¬NIL␈↓↓ ∧ y ≠ ␈↓¬NIL␈↓↓]]␈↓

␈↓ ↓H␈↓␈↓¬EQOR: ␈↓␈↓ βx␈↓↓∀x y: [x or y ≠ ␈↓¬NIL␈↓↓ ≡ [x ≠ ␈↓¬NIL␈↓↓ ∨ y ≠ ␈↓¬NIL␈↓↓]]␈↓

␈↓ ↓H␈↓␈↓¬EQEQUAL: ␈↓␈↓ βx␈↓↓∀x y: [x equal y ≠ ␈↓¬NIL␈↓↓ ≡ x = y]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓∀x y: [x equal y = ␈↓¬T␈↓↓ ≡ x = y]␈↓

␈↓ ↓H␈↓␈↓¬EQATOM: ␈↓␈↓ βx␈↓↓∀x: [atom x ≠ ␈↓¬NIL␈↓↓ ≡ ␈↓αat|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓∀x: [atom x = ␈↓¬T␈↓↓ ≡ ␈↓αat|␈↓↓x]␈↓

␈↓ ↓H␈↓␈↓¬EQNULL: ␈↓␈↓ βx␈↓↓∀x: [null x ≠ ␈↓¬NIL␈↓↓ ≡ ␈↓αn|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓∀x: [null x = ␈↓¬T␈↓↓ ≡ ␈↓αn|␈↓↓x]␈↓

␈↓ ↓H␈↓The␈αabove␈α
facts␈αfollow␈α
directly␈αfrom␈α
the␈αdefining␈αaxioms.␈α
 They␈αessentially␈α
express␈αthe␈α
fact␈αthat
␈↓ ↓H␈↓the propositional functons represent the logical operations in the intended manner.

␈↓ ↓H␈↓        We␈α⊗remark␈α⊗that␈α⊗our␈α⊗method␈α↔of␈α⊗interpreting␈α⊗functions␈α⊗as␈α⊗predicates␈α⊗was␈α↔chosen␈α⊗to
␈↓ ↓H␈↓correspond␈α∂with␈α∂the␈α∞usual␈α∂LISP␈α∂convention.␈α∞ Other␈α∂conventions␈α∂are␈α∞possible.␈α∂ For␈α∂example␈α∞we
␈↓ ↓H␈↓could␈αrestrict␈αthe␈αfunctions␈αthat␈α
are␈αto␈αbe␈αinterpreted␈αas␈α
predicates␈αto␈αhave␈αvalues␈αin␈α
the␈αdomain
␈↓ ↓H␈↓{␈↓¬T␈↓,␈α␈↓¬NIL␈↓,␈α␈↓π|␈↓}.␈α The␈αaxioms␈α
for␈α␈↓αand␈↓␈αetc.␈αwould␈αbe␈αslightly␈α
different,␈αbut␈αnot␈αmuch␈αelse␈αchanges.␈α
 One
␈↓ ↓H␈↓difference␈α
is␈α
in␈α
the␈α
form␈α
of␈α
the␈α
facts␈αwe␈α
can␈α
prove␈α
about␈α
the␈α
propostional␈α
operations.␈α
 In␈αthe␈α
latter
␈↓ ↓H␈↓case␈α∂we␈α∂restrict␈α∞our␈α∂proofs␈α∂to␈α∂arguments␈α∞ranging␈α∂over␈α∂the␈α∂three␈α∞element␈α∂domain␈α∂and␈α∂they␈α∞will
␈↓ ↓H␈↓appear␈α
much␈α
like␈α
the␈α
standard␈α
facts␈α
about␈α
propositional␈α
operations.␈α
 Thus␈α
we␈α
could␈α
prove␈αfacts
␈↓ ↓H␈↓such␈αas␈α␈↓↓not not x = x␈↓␈αfor␈αall␈α␈↓↓x␈↓␈α
in␈αthe␈αthree␈αelement␈αdomain.␈α In␈αour␈α
case␈αthis␈αfact␈αis␈αalso␈αtrue␈αfor␈α
␈↓↓x␈↓
␈↓ ↓H␈↓in␈αthe␈αthree␈αelement␈αdomain,␈αbut␈αnot␈αfor␈αmore␈αgeneral␈α␈↓↓x.␈↓␈α What␈αwe␈αcan␈αprove␈αis␈αthe␈αfact␈αthat␈αfor
␈↓ ↓H␈↓S-expressions␈α∂␈↓↓x,␈↓␈α∞␈↓↓not not x ≠ ␈↓¬NIL␈↓↓ ≡ x ≠ ␈↓¬NIL␈↓↓␈↓.␈α∂  Other␈α∂standard␈α∞laws␈α∂(such␈α∞as␈α∂de␈α∂Morgan's␈α∞laws)
␈↓ ↓H␈↓have similar analogues.

␈↓ ↓H␈↓α␈↓ ∧vExample: definition of ␈↓↓subexp.␈↓α

␈↓ ↓H␈↓        Now␈αwe␈αcan␈αshow␈αthat␈α␈↓↓subexpf␈↓␈αand␈α␈↓↓subexp␈↓␈αas␈αdefined␈αby␈α(12.5)␈αand␈α(12.6)␈αhave␈αthe␈αdesired
␈↓ ↓H␈↓properties.␈α∞ First␈α∂we␈α∞prove␈α∞that␈α∂␈↓↓subexpf␈↓␈α∞is␈α∞total␈α∂(12.7).␈α∞ Since␈α∞the␈α∂recursion␈α∞in␈α∞the␈α∂definition␈α∞of
␈↓ ↓H␈↓␈↓↓subexpf␈↓ is on the second argument, we will use the simple S-expression induction schema with

␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓πF␈↓↓[y] ≡ ∀z. issexp subexpf[z,y].              ␈↓ 

␈↓ ↓H␈↓In␈αthe␈αcase␈αthat␈α␈↓↓y␈↓␈αis␈αan␈αatom␈αwe␈αhave␈α␈↓↓not␈αatom␈αy␈α=␈α␈↓¬NIL␈↓↓␈↓␈αso␈αthe␈αfinal␈α␈↓↓and␈↓␈αevaluates␈αto␈α␈↓¬NIL␈↓.␈α Thus
␈↓ ↓H␈↓we␈αcan␈α
apply␈α␈↓¬SEXPEQUAL␈α␈↓and␈α
␈↓¬SEXPOR␈α␈↓␈α
to␈αshow␈αthat␈α
␈↓↓issexp␈↓␈αholds␈α
in␈αthis␈αcase.␈α
 In␈αthe␈α
case␈αthat␈α␈↓↓y␈↓␈α
is
␈↓ ↓H␈↓non-atomic we have the induction hypothesis

␈↓ ↓H␈↓␈↓ ∧⊂␈↓↓issexp subexpf[x,␈↓αa|␈↓↓y] ␈↓and␈↓↓ issexp subexpf[x,␈↓αd|␈↓↓y].␈↓ 
␈↓ ↓H␈↓76␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓Thus␈α
we␈α
can␈α
use␈α∞␈↓¬SEXPEQUAL,␈α
␈↓␈↓¬SEXPOR,␈α
␈↓and␈α
␈↓¬SEXPAND␈α
␈↓to␈α∞complete␈α
the␈α
proof.␈α
  As␈α∞we␈α
mentioned
␈↓ ↓H␈↓above (12.8) now follows from (12.7) and (12.6).

␈↓ ↓H␈↓        Now␈α∞we␈α∞can␈α∂prove␈α∞that␈α∞␈↓↓subexp␈↓␈α∂satisfies␈α∞the␈α∞equivalence␈α∞that␈α∂we␈α∞intended␈α∞from␈α∂the␈α∞start.
␈↓ ↓H␈↓The␈αproof␈αis␈α
just␈αa␈αsequence␈αof␈α
simplifications␈αbased␈αon␈αthe␈α
use␈αthe␈α␈↓¬EQ-␈α␈↓lemmas,␈α
the␈αdefinitions
␈↓ ↓H␈↓and the totality of ␈↓↓subexpf.␈↓  We wish to prove

␈↓ ↓H␈↓␈↓ αp␈↓↓∀x y: [subexp[x, y] ≡ [x = y] ∨ [¬␈↓αat|␈↓↓y ∧ [subexp[x, ␈↓αa|␈↓↓y] ∨ subexp[x, ␈↓αd|␈↓↓y]]]]␈↓

␈↓ ↓H␈↓which by (12.8) reduces to proving

␈↓ ↓H␈↓␈↓ αF␈↓↓∀x y: [ [[x equal y] or [not atom y and [subexpf[x, ␈↓αa|␈↓↓y] or subexpf[x, ␈↓αd|␈↓↓y]]]]≠␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ αW␈↓↓        ≡ [x = y] ∨ [¬␈↓αat|␈↓↓y ∧ [subexp[x, ␈↓αa|␈↓↓y] ∨ subexp[x, ␈↓αd|␈↓↓y]]] ]                     ␈↓

␈↓ ↓H␈↓In the case that ␈↓↓y␈↓ is atomic the left-hand side reduces as before to

␈↓ ↓H␈↓␈↓ ¬$␈↓↓ [[x equal y] or ␈↓¬NIL␈↓↓]≠␈↓¬NIL␈↓↓␈↓ 

␈↓ ↓H␈↓which␈αby␈α␈↓¬EQOR␈α␈↓and␈α␈↓¬EQEQUAL␈α␈↓reduces␈αto␈α␈↓↓x=y␈↓.␈α As␈α␈↓↓¬␈↓αat|␈↓↓y␈↓␈αis␈αfalse␈αthe␈αright-hand␈αside␈αalso␈αreduces␈αto
␈↓ ↓H␈↓␈↓↓x=y␈↓, so we are done with the atomic case.

␈↓ ↓H␈↓In␈αthe␈αcase␈αthat␈α␈↓↓y␈↓␈αis␈αnon-atomic␈αwe␈αhave␈α␈↓↓issexp␈αsubexpf[x,␈↓αa|␈↓↓y]␈↓␈αand␈α␈↓↓issexp␈αsubexpf[x,␈↓αd|␈↓↓y]␈↓␈αthus␈αwe
␈↓ ↓H␈↓can apply ␈↓¬EQOR, ␈↓␈↓¬EQAND ␈↓and ␈↓¬EQEQUAL ␈↓ to reduce the left-hand side to

␈↓ ↓H␈↓␈↓ αW␈↓↓[x = y] ∨ [[not atom y]≠␈↓¬NIL␈↓↓ ∧ [subexpf[x, ␈↓αa|␈↓↓y]≠␈↓¬NIL␈↓↓ ∨ subexpf[x, ␈↓αd|␈↓↓y]≠␈↓¬NIL␈↓↓]]␈↓. 

␈↓ ↓H␈↓Finally␈α∞we␈α
use␈α∞␈↓¬EQATOM␈α
␈↓and␈α∞␈↓¬EQNOT␈α
␈↓to␈α∞replace␈α
␈↓↓[not atom y]≠␈↓¬NIL␈↓↓␈↓␈α∞by␈α
␈↓↓¬␈↓αat|␈↓↓y␈↓␈α∞and␈α
(12.8)␈α∞to␈α∞finish␈α
the
␈↓ ↓H␈↓transformation␈α∞of␈α∞the␈α∞left-hand␈α∞side␈α
to␈α∞the␈α∞expression␈α∞on␈α∞the␈α
right-hand␈α∞side␈α∞of␈α∞the␈α∞"≡".␈α
 This
␈↓ ↓H␈↓completes the proof.

␈↓ ↓H␈↓α␈↓ ε⊂Exercise

␈↓ ↓H␈↓        You␈αwere␈αasked␈α
to␈αwrite␈αthe␈αdefinition␈α
for␈αthe␈αpredicate␈α␈↓↓istail␈↓␈α
in␈αthe␈αexercises␈αof␈α
Chapter II
␈↓ ↓H␈↓␈↓π∞␈↓8.  Prove the following facts about ␈↓↓istail.␈↓

␈↓ ↓H␈↓␈↓ βλ␈↓↓∀v: istail[␈↓¬NIL␈↓↓,v]␈↓
␈↓ ↓H␈↓␈↓ βλ␈↓↓∀u v: [¬␈↓αn|␈↓↓u ∧ istail[u,v] ⊃ istail[␈↓αd|␈↓↓u,v]]␈↓
␈↓ ↓H␈↓␈↓ βλ␈↓↓∀u v w: [istail[u,v] ∧ istail[v,w] ⊃ istail[u,w]]␈↓

␈↓ ↓H␈↓First␈α
translate␈αyour␈α
definition␈αof␈α
␈↓↓istail␈↓␈α
into␈αa␈α
functional␈αequation␈α
for␈αthe␈α
function␈α
␈↓↓istailf.␈↓␈α Show
␈↓ ↓H␈↓␈↓↓istailf␈↓␈α⊂is␈α⊂total␈α⊂and␈α⊂that␈α⊂␈↓↓istail␈↓␈α⊂satisfies␈α⊂the␈α⊂equivalence␈α⊂you␈α⊂intended.␈α⊂ All␈α⊂of␈α⊂this␈α⊂parallels␈α⊂the
␈↓ ↓H␈↓␈↓↓subexp␈↓ example above.  For the rest you are on your own.



␈↓ ↓H␈↓13.  ␈↓αThe SAMEFRINGE problem.␈↓


␈↓ ↓H␈↓         As␈α
a␈α
non␈α∞trivial␈α
application␈α
of␈α∞the␈α
techniques␈α
described␈α
in␈α∞the␈α
previous␈α
sections,␈α∞we␈α
will
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *77


␈↓ ↓H␈↓give␈αa␈αproof␈αof␈αcorrectness␈αof␈αthe␈αpredicate␈α␈↓↓samefringe␈↓␈αwhich␈αhas␈αbeen␈αproposed␈αas␈αa␈αsolution␈αof
␈↓ ↓H␈↓the␈α∪SAMEFRINGE␈α∀problem.␈α∪ This␈α∀is␈α∪the␈α∀problem␈α∪of␈α∀determining␈α∪whether␈α∀or␈α∪not␈α∀two␈α∪S-
␈↓ ↓H␈↓expressions␈αhave␈αthe␈αsame␈αfringe␈αusing␈αa␈αminimum␈α
of␈αspace.␈α We␈αwill␈αbe␈αconcerned␈αonly␈αwith␈α
the
␈↓ ↓H␈↓correctness of ␈↓↓samefringe␈↓ since the efficiency is not an ␈↓↓extensional␈↓ property.

␈↓ ↓H␈↓        We␈α∩have␈α∪already␈α∩studied␈α∩two␈α∪programs␈α∩for␈α∩computing␈α∪the␈α∩fringe␈α∩of␈α∪an␈α∩S-expression,
␈↓ ↓H␈↓namely ␈↓↓fringe␈↓ and ␈↓↓flatten.␈↓   The predicate ␈↓↓samefringe␈↓ is computed by the LISP program

␈↓ ↓H␈↓13.1) ␈↓ β∧␈↓↓samefringe[x, y] ← x = y ∨ [¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y ∧ same[gopher x, gopher y]]␈↓

␈↓ ↓H␈↓13.2) ␈↓ ∧,␈↓↓same[x, y] ← ␈↓αa|␈↓↓x = ␈↓αa|␈↓↓y ∧ samefringe[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y]␈↓

␈↓ ↓H␈↓13.3) ␈↓ βQ␈↓↓gopher x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ gopher [␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x]]␈↓.

␈↓ ↓H␈↓We␈α
translate␈α
the␈α∞definitions␈α
into␈α
 first␈α
order␈α∞sentences␈α
characterizing␈α
the␈α
functions␈α∞computed␈α
by
␈↓ ↓H␈↓these programs in the manner of the previous section as follows

␈↓ ↓H␈↓13.4)␈↓ β$␈↓↓∀x y:[samefringef[x, y] = [x equal y] or                                  ␈↓
␈↓ ↓H␈↓            ␈↓ αh␈↓↓                 [[not atom x and not atom y] and samef[gopher x, gopher y]]]␈↓

␈↓ ↓H␈↓13.5) ␈↓ βL␈↓↓∀x y:[samef[x, y] = [␈↓αa|␈↓↓x equal ␈↓αa|␈↓↓y] and samefringef[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y]␈↓

␈↓ ↓H␈↓13.6)␈↓ β8␈↓↓∀x:[gopher x = ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ gopher [␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x]]]␈↓

␈↓ ↓H␈↓The predicates are defined by

␈↓ ↓H␈↓13.7)␈↓ αn␈↓↓∀x y:[samefringe[x,y] ≡ issexp samefringef[x,y] ∧ samefringef[x,y]≠␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓13.8)␈↓ βY␈↓↓∀x y: [same[x, y] ≡ issexp samef[x,y] ∧ samef[x,y]≠␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓Now if we prove

␈↓ ↓H␈↓13.9)␈↓ ¬λ␈↓↓∀x y: issexp samefringef[x,y] ␈↓ 

␈↓ ↓H␈↓then

␈↓ ↓H␈↓13.10)␈↓ ∧∀␈↓↓∀x y:[samefringe[x,y] ≡  samefringef[x,y]≠␈↓¬NIL␈↓↓]␈↓ 

␈↓ ↓H␈↓follows immediately using (13.7) and it is easy to show (as we did for ␈↓↓subexp)␈↓ that

␈↓ ↓H␈↓13.11)␈↓ αh␈↓↓∀x y:[samefringe[x,y] ≡ x = y ∨ [¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y ∧ same[gopher x, gopher y]]␈↓ 

␈↓ ↓H␈↓        The correctness of ␈↓↓samefringe␈↓ can now be expressed by the statement

␈↓ ↓H␈↓13.12)␈↓ ∧.␈↓↓∀x y:[samefringe[x,y] ≡ fringe x = fringe y]␈↓. 

␈↓ ↓H␈↓Recall the following axioms for ␈↓↓fringe␈↓

␈↓ ↓H␈↓␈↓¬FRINGE: ␈↓␈↓ β8␈↓↓∀x: [fringe x = ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ [fringe ␈↓αa|␈↓↓x] * [fringe ␈↓αd|␈↓↓x]]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓∀x: islist fringe x␈↓.
␈↓ ↓H␈↓78␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓        Due␈α⊂to␈α⊂the␈α⊂complicated␈α⊂form␈α∂of␈α⊂the␈α⊂recursion␈α⊂defining␈α⊂␈↓↓samefringe,␈↓␈α⊂simple␈α∂S-expression
␈↓ ↓H␈↓induction␈α∞is␈α
not␈α∞adequate␈α
to␈α∞prove␈α∞the␈α
above␈α∞theorems.␈α
 Therefore␈α∞we␈α
must␈α∞be␈α∞somewhat␈α
more
␈↓ ↓H␈↓clever␈αand␈αdevise␈αan␈αinduction␈αappropriate␈αfor␈αthe␈αproblem.␈α One␈αway␈αis␈αto␈αuse␈αinduction␈αon␈αthe
␈↓ ↓H␈↓size of the S-expressions involved.  We recall the axioms for size given in section ␈↓π∞␈↓11.

␈↓ ↓H␈↓␈↓¬SIZE: ␈↓␈↓ β8␈↓↓∀x: [size x = ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓∀x: numberp size x␈↓.

␈↓ ↓H␈↓␈↓↓size␈↓␈α
provides␈α
a␈α
mapping␈α
of␈α
S-expressions␈α
into␈α
the␈α
domain␈α
of␈α
natural␈α
numbers␈α
and␈α
allows␈α
us␈α
to
␈↓ ↓H␈↓transform the problem into one of natural number induction.  Namely, we will prove

␈↓ ↓H␈↓13.13)␈↓ ∧5␈↓↓∀n: ∀x y: [[size x + size y] = n ⊃ ␈↓¬THM␈↓↓[x, y]]␈↓ 

␈↓ ↓H␈↓using␈α
the␈α
induction␈α
schema␈α
␈↓¬NUMINDUCTION-CVI.␈α
␈↓␈α
Here␈α
␈↓¬THM␈α
␈↓is␈α
the␈α
statement␈α
of␈α
totality␈α∞(13.9)␈α
or
␈↓ ↓H␈↓of␈α⊗correctness␈α⊗(13.12).␈α↔  To␈α⊗complete␈α⊗the␈α↔proof␈α⊗of␈α⊗␈↓¬THM␈α⊗␈↓␈α↔we␈α⊗instantiate␈α⊗(13.13)␈α↔by␈α⊗letting
␈↓ ↓H␈↓␈↓↓n=size x + size y␈↓ and obtain

␈↓ ↓H␈↓␈↓ ¬/␈↓↓∀x y: ␈↓¬THM␈↓↓[x, y] .         ␈↓ 

␈↓ ↓H␈↓        Now␈αwe␈αproceed␈αwith␈αthe␈αproof.␈α In␈αaddition␈αto␈αthe␈αlemmas␈α about␈α␈↓↓append␈↓␈αand␈αthe␈αlemmas
␈↓ ↓H␈↓proved␈α⊂in␈α∂the␈α⊂last␈α∂section␈α⊂about␈α∂the␈α⊂propositional␈α∂functions,␈α⊂we␈α∂will␈α⊂need␈α∂some␈α⊂lemmas␈α∂about
␈↓ ↓H␈↓␈↓↓gopher,␈↓ ␈↓↓fringe,␈↓ ␈↓↓size␈↓ and combinations thereof. They are

␈↓ ↓H␈↓␈↓¬GOOD-GOPHER: ␈↓  ␈↓↓∀x: [¬␈↓αat|␈↓↓x ⊃ [issexp gopher x ∧ issexp ␈↓αa|␈↓↓gopher x ∧ issexp ␈↓αd|␈↓↓gopher x]]␈↓

␈↓ ↓H␈↓␈↓¬FRINGE-ATM: ␈↓   ␈↓↓∀x y: [[␈↓αat|␈↓↓x ∨ ␈↓αat|␈↓↓y] ⊃ [fringe x = fringe y ≡ x = y]]␈↓

␈↓ ↓H␈↓␈↓¬FRINGE-GOPHER: ␈↓␈↓↓∀x: [¬␈↓αat|␈↓↓x ⊃ [␈↓αa|␈↓↓fringe x = ␈↓αa|␈↓↓gopher x] ∧ [␈↓αd|␈↓↓fringe x = fringe ␈↓αd|␈↓↓gopher x]]␈↓

␈↓ ↓H␈↓␈↓¬SIZE-GOPHER: ␈↓  ␈↓↓∀x y: [[¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y] ⊃ [size ␈↓αd|␈↓↓gopher x +size ␈↓αd|␈↓↓gopher y]  < [size x + size y]]␈↓

␈↓ ↓H␈↓The␈α∩first␈α∩two␈α∩are␈α∩just␈α∩bookkeeping␈α∩and␈α∩computational␈α∩lemmas␈α∩to␈α∩fill␈α∩in␈α∩some␈α∩details.␈α⊃ The
␈↓ ↓H␈↓␈↓¬FRINGE-GOPHER␈α∞␈↓lemma␈α∞is␈α∞really␈α∞the␈α∞key␈α∞to␈α
why␈α∞this␈α∞program␈α∞for␈α∞␈↓↓samefringe␈↓␈α∞works.␈α∞ There␈α
are
␈↓ ↓H␈↓several␈αproofs␈α
of␈αthe␈α
correctness␈αof␈α␈↓↓samefringe␈↓␈α
based␈αon␈α
various␈αinductions,␈αbut␈α
they␈αall␈α
use␈αthis
␈↓ ↓H␈↓lemma.␈α∂ The␈α∂␈↓¬SIZE-GOPHER␈α∞␈↓␈α∂lemma␈α∂is␈α∂what␈α∞allows␈α∂us␈α∂to␈α∂apply␈α∞the␈α∂induction␈α∂hypothesis␈α∂at␈α∞the
␈↓ ↓H␈↓appropriate time.

␈↓ ↓H␈↓        We␈αwill␈α
give␈αonly␈α
a␈αbrief␈αindication␈α
of␈αthe␈α
proof␈αof␈αthese␈α
lemmas.␈α The␈α
key␈αidea␈αin␈α
proving
␈↓ ↓H␈↓things␈α
about␈α
␈↓↓gopher␈↓␈α
is␈α
to␈αfirst␈α
prove␈α
properties␈α
of␈α
␈↓↓gopher[x . y]␈↓␈αas␈α
gopher␈α
is␈α
only␈α
defined␈αfor␈α
non-
␈↓ ↓H␈↓atoms␈α
and␈α
every␈α
non-atom␈α
can␈α
be␈α
expressed␈α
as␈α
␈↓↓x . y␈↓␈α
for␈α
suitable␈α
␈↓↓x␈↓␈α
and␈α
␈↓↓y.␈↓␈α
For␈α
this␈α∞purpose␈α
we
␈↓ ↓H␈↓start with some useful facts derived from ␈↓¬GOPHER, ␈↓ ␈↓¬FRINGE, ␈↓␈↓¬SIZE ␈↓and the LISP axioms.

␈↓ ↓H␈↓␈↓¬GOPHER-CONS-ATM: ␈↓      ␈↓↓∀x y: [␈↓αat|␈↓↓x ⊃ gopher[x . y] = [x . y]]␈↓

␈↓ ↓H␈↓␈↓¬GOPHER-CONS-NOTATM: ␈↓   ␈↓↓∀x y: [¬␈↓αat|␈↓↓x ⊃ gopher[x . y] = gopher[␈↓αa|␈↓↓x. [␈↓αd|␈↓↓x . y]]]␈↓

␈↓ ↓H␈↓␈↓¬FRINGE-CONS: ␈↓          ␈↓↓∀x y: [fringe[x . y] = fringe x * fringe y]␈↓

␈↓ ↓H␈↓␈↓¬SIZE-CONS: ␈↓            ␈↓↓∀x y:  [size[x . y] = size x + size y]␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *79


␈↓ ↓H␈↓        To␈α∂prove␈α∂␈↓¬GOOD-GOPHER␈α∂␈↓first␈α∂prove,␈α∂by␈α∂a␈α∂straight␈α∂forward␈α∂use␈α∂of␈α∂␈↓¬SEXPINDUCTION␈α∂␈↓on␈α∞␈↓↓x,␈↓
␈↓ ↓H␈↓that

␈↓ ↓H␈↓                ␈↓↓∀x y: [issexp gopher[x . y] ∧ ¬␈↓αat|␈↓↓gopher[x . y]]␈↓

␈↓ ↓H␈↓The lemma then follows from the LISP axioms.

␈↓ ↓H␈↓        For␈α∂␈↓¬FRINGE-ATM␈α∞␈↓we␈α∂assume␈α∞␈↓↓␈↓αat|␈↓↓x␈↓␈α∂and␈α∞show␈α∂by␈α∞computation␈α∂and␈α∞using␈α∂the␈α∂algebraic␈α∞facts
␈↓ ↓H␈↓about␈αS-expressions␈αthat␈α␈↓↓fringe x = fringe y ≡ x = y␈↓.␈α The␈αlemma␈αthen␈αfollows␈αfrom␈αthe␈αsymmetry
␈↓ ↓H␈↓of␈α_the␈α↔occurrences␈α_of␈α↔␈↓↓x␈↓␈α_and␈α↔␈↓↓y.␈↓␈α_ In␈α↔particular,␈α_from␈α↔the␈α_definition␈α↔of␈α_␈↓↓fringe␈↓␈α_we␈α↔have
␈↓ ↓H␈↓␈↓↓fringe x = <x> = [x . ␈↓¬NIL␈↓↓]␈↓.␈α If␈α␈↓↓␈↓αat␈↓↓ y␈↓␈αthe␈αresult␈αfollows␈αusing␈α␈↓¬EQ-SEXP.␈α␈↓␈αIf␈α␈↓↓¬␈↓αat|␈↓↓y␈↓␈αthen␈α␈↓↓x ≠ y␈↓␈αand␈αby
␈↓ ↓H␈↓the␈α
LISP␈α
axiom␈α∞␈↓¬EQ-SEXP␈α
␈↓we␈α
need␈α
only␈α∞prove␈α
␈↓↓¬␈↓αn|␈↓↓␈↓αd|␈↓↓fringe␈α
y␈↓.␈α
But␈α∞this␈α
follows␈α
from␈α∞properties␈α
of
␈↓ ↓H␈↓␈↓↓append.␈↓

␈↓ ↓H␈↓        To prove ␈↓¬FRINGE-GOPHER ␈↓we use the "gopher trick" again and first prove

␈↓ ↓H␈↓␈↓ αK␈↓↓∀x y: [␈↓αa|␈↓↓fringe[x . y] = ␈↓αa|␈↓↓gopher[x . y] ∧ ␈↓αd|␈↓↓fringe[x . y] = fringe ␈↓αd|␈↓↓gopher[x . y]]␈↓ 

␈↓ ↓H␈↓using simple S-expression induction.  By properties of ␈↓↓append␈↓  and ␈↓¬FRINGE-CONS ␈↓ we have

␈↓ ↓H␈↓␈↓ ∧Q␈↓↓␈↓αa|␈↓↓fringe[x . y] = ␈↓αa|␈↓↓fringe x,              ␈↓ 

␈↓ ↓H␈↓and

␈↓ ↓H␈↓␈↓ ∧D␈↓↓␈↓αd|␈↓↓fringe[x . y] = [␈↓αd|␈↓↓fringe x] * fringe y. ␈↓ 

␈↓ ↓H␈↓In␈αthe␈αcase␈α␈↓↓␈↓αat|␈↓↓x␈↓␈α the␈αresult␈αfollows␈αfrom␈αthe␈αfacts␈αthat␈α␈↓¬NIL␈↓␈αis␈αa␈αleft␈αidentity␈αfor␈α␈↓↓append,␈↓␈α␈↓¬GOPHER-
␈↓ ↓H␈↓¬CONS-ATM, ␈↓and ␈↓↓fringe x = [x . ␈↓¬NIL␈↓↓]␈↓ .  In the case ␈↓↓¬␈↓αat|␈↓↓x␈↓ we show

␈↓ ↓H␈↓␈↓ ∧M␈↓↓fringe[x . y] = fringe[␈↓αa|␈↓↓x . [␈↓αd|␈↓↓x . y]]    ␈↓ 

␈↓ ↓H␈↓using␈α⊃properties␈α∩of␈α⊃␈↓↓append␈↓␈α⊃and␈α∩␈↓¬FRINGE-CONS.␈α⊃␈↓␈α∩Then␈α⊃the␈α⊃result␈α∩follows␈α⊃from␈α∩the␈α⊃induction
␈↓ ↓H␈↓hypothesis␈α∪and␈α∀␈↓¬GOPHER-CONS-NOTATM.␈α∪␈↓␈α∀␈↓¬FRINGE-GOPHER␈α∪␈↓now␈α∪follows␈α∀from␈α∪the␈α∀above␈α∪using
␈↓ ↓H␈↓␈↓¬CONS. ␈↓

␈↓ ↓H␈↓To prove ␈↓¬SIZE-GOPHER ␈↓we first show

␈↓ ↓H␈↓␈↓ ∧P␈↓↓∀x y: [size ␈↓αd|␈↓↓gopher[x . y] < size[x . y]]␈↓ 

␈↓ ↓H␈↓using simple S-expression induction.  In the case ␈↓↓␈↓αat|␈↓↓x␈↓ we have

␈↓ ↓H␈↓␈↓ β←␈↓↓size ␈↓αd|␈↓↓gopher[x . y] = size y␈↓  and  ␈↓↓size[x . y] = 1 + size y␈↓ 

␈↓ ↓H␈↓by␈α␈↓¬SIZE,␈α␈↓␈↓¬SIZE-CONS␈α␈↓and␈α␈↓¬GOPHER-CONS-ATM␈α␈↓and␈αthe␈αresult␈αfollows␈αby␈αproperties␈αof␈αnumbers.␈α In
␈↓ ↓H␈↓the case ␈↓↓¬␈↓αat|␈↓↓x␈↓ we show

␈↓ ↓H␈↓␈↓ ¬↓␈↓↓size[x . y] = size[␈↓αa|␈↓↓x . [␈↓αd|␈↓↓x . y]]␈↓ 

␈↓ ↓H␈↓using␈α␈↓¬SIZE-CONS,␈α␈↓properties␈αof␈αnumbers␈αand␈αthe␈αLISP␈αaxioms.␈αThe␈αresult␈αthen␈αfollows␈αfrom␈αthe
␈↓ ↓H␈↓induction␈α∩hypothesis␈α∩and␈α⊃␈↓¬GOPHER-CONS-NOTATM.␈α∩␈↓␈α∩␈↓¬SIZE-GOPHER␈α⊃␈↓now␈α∩follows␈α∩from␈α∩␈↓¬CONS␈α⊃␈↓and
␈↓ ↓H␈↓properties of numbers.
␈↓ ↓H␈↓80␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓        Now␈α
we␈α
are␈α
ready␈α
to␈α
begin␈α
the␈α
proof␈α
of␈α
the␈α
totality␈α
and␈α
correctness␈α
of␈α
␈↓↓samefringe.␈↓␈α Since
␈↓ ↓H␈↓the␈α
form␈αof␈α
the␈α
proofs␈αof␈α
(13.9)␈α
and␈α(13.12)␈α
are␈αsimilar␈α
we␈α
will␈αdo␈α
the␈α
proofs␈α"in␈α
parallel".␈α That␈α
is
␈↓ ↓H␈↓we␈α.will␈α.prove␈α-␈↓↓∀x␈α.y:␈↓¬THM␈↓↓[x,y]␈↓␈α.where␈α-␈↓¬THM␈α.␈↓is␈α.either␈α.␈↓↓issexp samefrf[x,y]␈↓␈α- or
␈↓ ↓H␈↓␈↓↓samefringe[x,y]≡fringe x=fringe y␈↓.␈α (We␈αin␈αfact␈αuse␈αthe␈αfirst␈αtheorem␈αto␈αprove␈αthe␈αsecond,␈αbut␈αnot
␈↓ ↓H␈↓visa␈α
versa,␈α
so␈α
the␈α
argument␈α
isn't␈α
cyclic.␈α
 We␈αcould␈α
just␈α
a␈α
easily␈α
do␈α
them␈α
separately,␈α
it␈α
would␈αjust␈α
be
␈↓ ↓H␈↓somewhat repetative.)

␈↓ ↓H␈↓        Recall␈αthat␈αwe␈αare␈αgoing␈αto␈αdo␈αa␈α"course␈αof␈αvalues"␈αinduction␈αon␈αthe␈αsum␈αof␈αthe␈αsizes␈αof␈αthe
␈↓ ↓H␈↓arguments.␈α As␈αis␈αusual,␈αthe␈αproof␈αis␈αdivided␈αinto␈αtwo␈αcases,␈αone␈αin␈αwhich␈αthe␈αresult␈αfollows␈αby␈αa
␈↓ ↓H␈↓fairly␈αdirect␈αcomputation␈αand␈αthe␈αsecond␈αin␈αwhich␈αwe␈αare␈αable␈αto␈αreduce␈αthe␈αproblem␈αto␈αa␈αsmaller
␈↓ ↓H␈↓case␈αand␈αmake␈αuse␈αof␈αthe␈αinduction␈αhypothesis.␈α In␈αthe␈αfirst␈αcase␈αwe␈αassume␈α␈↓↓␈↓αat|␈↓↓x␈α∨␈α␈↓αat|␈↓↓y␈↓.␈α Then␈αby
␈↓ ↓H␈↓the propositional axioms

␈↓ ↓H␈↓␈↓ ∧u␈↓↓not atom x and not atom y = ␈↓¬NIL␈↓↓␈↓, 

␈↓ ↓H␈↓and hence

␈↓ ↓H␈↓␈↓ ¬	␈↓↓samefringef[x, y] = x equal y␈↓. 

␈↓ ↓H␈↓Thus ␈↓↓issexp samefringef[x,y]␈↓ follows from ␈↓¬SEXPEQUAL. ␈↓  Further

␈↓ ↓H␈↓␈↓ ∧9␈↓↓samefringe[x,y] ≡ [x equal y]≠␈↓¬NIL␈↓↓ ≡ x = y␈↓ 

␈↓ ↓H␈↓by (13.10) ␈↓¬EQEXPEQUAL ␈↓and ␈↓↓samefringe[x,y]≡fringe x=fringe y␈↓ follows from ␈↓¬FRINGE-ATM. ␈↓

␈↓ ↓H␈↓        In␈α⊂the␈α⊂second␈α⊃case␈α⊂we␈α⊂assume␈α⊃␈↓↓¬␈↓αat|␈↓↓x␈α⊂∧␈α⊂¬␈↓αat|␈↓↓y␈↓␈α⊃and␈α⊂make␈α⊂use␈α⊃of␈α⊂the␈α⊂induction␈α⊃schema␈α⊂as
␈↓ ↓H␈↓discussed above.  In particular we have the induction hypothesis

␈↓ ↓H␈↓␈↓ βN␈↓↓∀m: [m < n ⊃ ∀x␈↓β1␈↓↓ y␈↓β1␈↓↓: [size x␈↓β1␈↓↓ + size y␈↓β1␈↓↓ = m ⊃ ␈↓¬THM␈↓↓[x␈↓β1␈↓↓, y␈↓β1␈↓↓]] ␈↓. 

␈↓ ↓H␈↓Taking␈α∀ ␈↓↓n␈α∀=␈α∀size␈α∀x␈α∀+␈α∀size␈α∀y␈↓,␈α∃␈↓↓m␈α∀=␈α∀size␈α∀␈↓αd|␈↓↓gopher␈α∀x␈α∀+␈α∀size␈α∀␈↓αd|␈↓↓gopher␈α∀y␈↓,␈α∃␈↓↓x␈↓β1␈↓↓ = ␈↓αd|␈↓↓gopher x␈↓␈α∀and
␈↓ ↓H␈↓␈↓↓y␈↓β1␈↓↓ = ␈↓αd|␈↓↓gopher y␈↓ we have by ␈↓¬SIZE-GOPHER ␈↓

␈↓ ↓H␈↓␈↓ ¬⊗␈↓↓␈↓¬THM␈↓↓[␈↓αd|␈↓↓gopher x, ␈↓αd|␈↓↓gopher y]␈↓ 

␈↓ ↓H␈↓for␈α
either␈α
theorem␈α
that␈α
we␈α
are␈α
proving.␈α
 Assigning␈α
these␈α
values␈α
to␈α
␈↓↓n␈↓␈α
and␈α
␈↓↓m␈↓␈α
is␈α
allowed␈α
since␈α +␈α
and
␈↓ ↓H␈↓␈↓↓size␈↓ return numbers and ␈↓¬GOOD-GOPHER ␈↓says the argments to ␈↓↓size␈↓ are S-expressions.

␈↓ ↓H␈↓        Using the axioms (13.4) and (13.5) we expand ␈↓↓samefringef[x,y]␈↓ and obtain

␈↓ ↓H␈↓␈↓ ↓x␈↓↓[samefringef[x, y] =␈↓ ∧λ[x equal y] or [[not atom x and not atom y] ␈↓
␈↓ ↓H␈↓␈↓ ¬H␈↓↓and [␈↓αa|␈↓↓gopher x equal ␈↓αa|␈↓↓gopher y] ␈↓
␈↓ ↓H␈↓␈↓ ¬H␈↓↓and samefringef[␈↓αd|␈↓↓gopher x, ␈↓αd|␈↓↓gopher y]␈↓

␈↓ ↓H␈↓␈↓↓issexp␈α∂samefringef[x,y]␈↓␈α∞is␈α∂immediate␈α∞from␈α∂the␈α∞␈↓¬SEXP-␈α∂␈↓propositional␈α∞axioms,␈α∂␈↓¬GOOD-GOPHER␈α∂␈↓␈α∞and
␈↓ ↓H␈↓the induction hypothesis.  By (13.10) we need only show that

␈↓ ↓H␈↓␈↓ ↓x␈↓↓fringe x = fringe y ≡␈↓ ∧_[[x equal y] or [[not atom x and not atom y] ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓↓and [␈↓αa|␈↓↓gopher x equal ␈↓αa|␈↓↓gopher y] ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓↓and samefringef[␈↓αd|␈↓↓gopher x, ␈↓αd|␈↓↓gopher y]]≠␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *81


␈↓ ↓H␈↓to␈α
prove␈αthe␈α
correctness␈α
version␈αof␈α
␈↓¬THM.␈α␈↓␈α
Since␈α
the␈αarguments␈α
to␈αthe␈α
propositional␈α
functions␈αare
␈↓ ↓H␈↓all S-expressions we apply the ␈↓¬EQ- ␈↓propositional axioms and (13.10) to reduce to

␈↓ ↓H␈↓␈↓ ↓x␈↓↓fringe x = fringe y ≡␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓x = y ∨ [[¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y] ∧ [␈↓αa|␈↓↓gopher x = ␈↓αa|␈↓↓gopher y] ∧ samefringe[␈↓αd|␈↓↓gopher x, ␈↓αd|␈↓↓gopher y]]␈↓


␈↓ ↓H␈↓Using the induction hypothesis and simplifying the ␈↓↓¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y␈↓ to ␈↓αtrue␈↓ we need only show

␈↓ ↓H␈↓␈↓ ↓x␈↓↓fringe x = fringe y ≡␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓x = y ∨ [[␈↓αa|␈↓↓gopher x = ␈↓αa|␈↓↓gopher y] ∧ fringe[␈↓αd|␈↓↓gopher x]=fringe[␈↓αd|␈↓↓gopher y]]␈↓

␈↓ ↓H␈↓which follows easily from ␈↓¬FRINGE-GOPHER. ␈↓ This completes the proof.

␈↓ ↓H␈↓        In the above proof we could have equally well used the predicate

␈↓ ↓H␈↓␈↓ ∧T␈↓↓␈↓πF␈↓↓ n ≡ ∀x y: [size x = n ⊃ THM[x, y]]␈↓ 

␈↓ ↓H␈↓since the recursive call to ␈↓↓samefringe␈↓ always reduces the size of the first argument.

␈↓ ↓H␈↓        Another version of ␈↓↓samefringe␈↓ without any auxiliary functions is

␈↓ ↓H␈↓␈↓ β8␈↓↓samefringe[x, y] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓    x = y ∨ ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓    [¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y ∧ ␈↓
␈↓ ↓H␈↓13.14)␈↓ β8␈↓↓        [␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ [␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓y ␈↓αthen␈↓↓ ␈↓αa|␈↓↓x = ␈↓αa|␈↓↓y ∧ samefringe[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y] ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓                             ␈↓αelse␈↓↓ samefringe[x, ␈↓αaa|␈↓↓y. [␈↓αda|␈↓↓y . ␈↓αd|␈↓↓y]]]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓         ␈↓αelse␈↓↓ samefringe[␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x], y]. ␈↓

␈↓ ↓H␈↓Note that a recursive call to ␈↓↓samefringe␈↓ does one of the following three things:

␈↓ ↓H␈↓        1)  decreases  ␈↓↓size x + size y␈↓
␈↓ ↓H␈↓        2)  leaves  ␈↓↓size x + size y␈↓ and ␈↓↓size ␈↓αa|␈↓↓x␈↓ invariant and decreases ␈↓↓size ␈↓αa|␈↓↓y␈↓
␈↓ ↓H␈↓        3)  leaves  ␈↓↓sixe x + size y␈↓ and ␈↓↓size ␈↓αa|␈↓↓y␈↓ invariant and decreases ␈↓↓size ␈↓αa|␈↓↓x ␈↓.

␈↓ ↓H␈↓This␈α∪can␈α∩lead␈α∪to␈α∪a␈α∩choice␈α∪of␈α∩an␈α∪induction␈α∪axiom␈α∩schema␈α∪in␈α∩at␈α∪least␈α∪two␈α∩ways.␈α∪ If␈α∪in␈α∩the
␈↓ ↓H␈↓␈↓¬NUMINDUCTION-CVI␈α⊃␈↓schema␈α⊂we␈α⊃let␈α⊂␈↓↓n␈↓␈α⊃and␈α⊂␈↓↓m␈↓␈α⊃range␈α⊂over␈α⊃all␈α⊂ordinals␈α⊃less␈α⊂than␈α⊃a␈α⊂given␈α⊃one␈α⊂it
␈↓ ↓H␈↓becomes␈αa␈αschema␈αof␈α
transfinite␈αinduction.␈αOrdinary␈αinduction␈αis␈α
obtained␈αas␈αa␈αspecial␈αcase␈α
where
␈↓ ↓H␈↓the␈αbounding␈αordinal␈αis␈α
␈↓πw␈↓␈αthe␈αleast␈αtransfinite␈α
ordinal.␈α If␈αwe␈αtake␈α
the␈αbounding␈αordinal␈αto␈αbe␈α
␈↓πw␈↓␈↓#
␈↓π␈↓#
w␈↓␈↓#
␈↓#
␈↓ ↓H␈↓then␈α
the␈α
SAMEFRINGE␈α
theorem␈α
for␈α
the␈α
above␈α
version␈α
of␈α
␈↓↓samefringe␈↓␈α
can␈α
be␈α
proved␈α
using␈α
the
␈↓ ↓H␈↓predicate

␈↓ ↓H␈↓␈↓ β≤␈↓↓␈↓πF␈↓↓ n  ≡ ∀x y: [[size x +size y]␈↓πw␈↓↓ + size ␈↓αa|␈↓↓x + size ␈↓αa|␈↓↓y = n ⊃ ␈↓¬THM␈↓↓[x, y]]␈↓ 

␈↓ ↓H␈↓        Alternately one could axiomatize the lexicographic ordering of triples of numbers by

␈↓ ↓H␈↓␈↓ β0␈↓↓∀l␈↓β1␈↓↓ m␈↓β1␈↓↓ n␈↓β1␈↓↓ l m n: [(l␈↓β1␈↓↓, m␈↓β1␈↓↓, n␈↓β1␈↓↓) < (l, m, n) ≡                         ␈↓
␈↓ ↓H␈↓␈↓ β]␈↓↓[l␈↓β1␈↓↓ < l] ∨ [l␈↓β1␈↓↓ = l ∧ m␈↓β1␈↓↓ < m] ∨ [l␈↓β1␈↓↓ = l ∧ m␈↓β1␈↓↓ = m ∧ n␈↓β1␈↓↓ < n]]␈↓

␈↓ ↓H␈↓This␈α∞ordering␈α∞is␈α
well-founded␈α∞(has␈α∞no␈α∞infinite␈α
decreasing␈α∞sequences)␈α∞and␈α
so␈α∞we␈α∞have␈α∞a␈α
schema
␈↓ ↓H␈↓analogous to ␈↓¬NUMINDUCTION-CVI ␈↓given by
␈↓ ↓H␈↓82␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓␈↓ α]␈↓↓∀l m n: [∀l␈↓β1␈↓↓ m␈↓β1␈↓↓ n␈↓β1␈↓↓: [(l␈↓β1␈↓↓, m␈↓β1␈↓↓, n␈↓β1␈↓↓) < (l, m, n) ⊃␈↓πF␈↓↓(l␈↓β1␈↓↓, m␈↓β1␈↓↓, n␈↓β1␈↓↓)] ⊃ ␈↓πF␈↓↓(l, m, n)]␈↓
␈↓ ↓H␈↓␈↓ ¬A␈↓↓⊃ ∀l m n: ␈↓πF␈↓↓(l, m, n)␈↓

␈↓ ↓H␈↓The SAMEFRINGE theorems can now be proved using this schema with the predicate

␈↓ ↓H␈↓␈↓ αS␈↓↓␈↓πF␈↓↓(l, m, n) ≡ ∀x y: [l = size x +size y ∧ m = size ␈↓αa|␈↓↓y ∧ n = size ␈↓αa|␈↓↓x ⊃ ␈↓¬THM␈↓↓[x, y]]␈↓ 



␈↓ ↓H␈↓14.  ␈↓αCorrectness of a Program to Partition Lists.␈↓


␈↓ ↓H␈↓        As␈αa␈αsecond␈αnon␈αtrivial␈αexample␈αwe␈αconsider␈αa␈αprogram␈αthat␈αcomputes␈αall␈αof␈αthe␈αpartitions
␈↓ ↓H␈↓of␈αa␈αlist␈αinto␈αa␈αgiven␈αnumber␈αof␈αsublists.␈α  For␈αa␈αlist␈α␈↓↓w␈↓␈αto␈αbe␈αa␈αpartition␈αof␈αa␈αlist␈α␈↓↓u␈↓␈αinto␈α␈↓↓n␈↓␈αparts,␈α␈↓↓w␈↓
␈↓ ↓H␈↓must␈αbe␈αa␈αlist␈αof␈α␈↓↓n␈↓␈αnon-empty␈αlists␈αsuch␈αthat␈α␈↓↓append␈↓ing␈αthe␈αelements␈αof␈α␈↓↓w␈↓␈αtogether␈αgives␈α␈↓↓u.␈αThus␈↓
␈↓ ↓H␈↓␈↓¬((A B) (C D))␈↓ is a 2-partition of ␈↓¬(A B C D)␈↓ and

␈↓ ↓H␈↓␈↓ α9␈↓↓partition[␈↓¬(A B C D), 2␈↓↓] = ␈↓¬(((A) (B C D)) ((A B) (C D)) ((A B C) (D)))␈↓↓␈↓. 

␈↓ ↓H␈↓The definition of partition is

␈↓ ↓H␈↓␈↓ β8␈↓↓partition[u, n] ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ n = ␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬ERROR␈↓↓␈↓
␈↓ ↓H␈↓14.1)␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ n > length u ␈↓αthen␈↓↓ ␈↓¬ERROR␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = ␈↓¬1 ␈↓↓␈↓αthen␈↓↓ <<u>>␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ partn[<␈↓αa|␈↓↓u>, ␈↓αd|␈↓↓u, n-␈↓¬1␈↓↓]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ β8␈↓↓partn[u, v, n] ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ n > length v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓14.2)␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = ␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = 1 ␈↓αthen␈↓↓ [tack[u, <<v>>] * partn[u * <␈↓αa|␈↓↓v>, ␈↓αd|␈↓↓v, n]]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ tack[u, partn[<␈↓αa|␈↓↓v>, ␈↓αd|␈↓↓v, n-␈↓¬1␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓* partn[u * <␈↓αa|␈↓↓v>, ␈↓αd|␈↓↓v, n]␈↓
␈↓ ↓H␈↓and

␈↓ ↓H␈↓14.3)␈↓ β8␈↓↓tack[x, w] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [x . ␈↓αa|␈↓↓w] . tack[x, ␈↓αd|␈↓↓w]␈↓


␈↓ ↓H␈↓Notice␈α
that␈αwe␈α
can␈αonly␈α
partition␈αa␈α
list␈αinto␈α
␈↓↓n␈↓␈α
parts␈αif␈α
the␈αlength␈α
of␈αthe␈α
list␈αis␈α
at␈αleast␈α
␈↓↓n,␈↓␈α
also␈αit
␈↓ ↓H␈↓makes␈α
no␈αsense␈α
to␈αpartition␈α
a␈αlist␈α
into␈α
␈↓¬0␈α␈↓parts.␈α
  ␈↓↓partition␈↓␈αworks␈α
by␈αconsidering␈α
all␈αpossible␈α
"heads"
␈↓ ↓H␈↓of␈α␈↓↓u␈↓␈αas␈αthe␈αfirst␈αelement␈αof␈αthe␈αpartition␈αand␈αparitioning␈αthe␈αrest␈αof␈α␈↓↓u␈↓␈αinto␈αone␈αfewer␈αparts.␈α The
␈↓ ↓H␈↓arguments to ␈↓↓partn␈↓ are the current head, rest and numbers of parts to partition the rest into.

␈↓ ↓H␈↓These definitions translate into first order sentences as follows:

␈↓ ↓H␈↓␈↓ αx␈↓↓∀u n:[partition[u, n] = ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ n = ␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬ERROR␈↓↓␈↓
␈↓ ↓H␈↓14.4)␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ n > length u ␈↓αthen␈↓↓ ␈↓¬ERROR␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = ␈↓¬1 ␈↓↓␈↓αthen␈↓↓ <<u>>␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ partn[<␈↓αa|␈↓↓u>, ␈↓αd|␈↓↓u, n-␈↓¬1␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *83


␈↓ ↓H␈↓␈↓ αx␈↓↓∀u v n:[partn[u, v, n] = ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ n > length v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓14.5)␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = ␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = 1 ␈↓αthen␈↓↓ [tack[u, <<v>>] * partn[u * <␈↓αa|␈↓↓v>, ␈↓αd|␈↓↓v, n]]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ tack[u, partn[<␈↓αa|␈↓↓v>, ␈↓αd|␈↓↓v, n-␈↓¬1␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓* partn[u * <␈↓αa|␈↓↓v>, ␈↓αd|␈↓↓v, n]]␈↓

␈↓ ↓H␈↓14.6)␈↓ αx␈↓↓∀x w:[tack[x, w] = ␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [x . ␈↓αa|␈↓↓w] . tack[x, ␈↓αd|␈↓↓w]]␈↓


␈↓ ↓H␈↓We also have

␈↓ ↓H␈↓14.7)␈↓ β8␈↓↓∀u n:[length u > n > 0 ⊃ islist partition[u,n]]␈↓
␈↓ ↓H␈↓14.8)␈↓ β8␈↓↓∀u v n:islist partn[u,v,n]␈↓
␈↓ ↓H␈↓14.9)␈↓ β8␈↓↓∀x w:islist tack[x,w]␈↓


␈↓ ↓H␈↓as␈α(14.9)␈αfollows␈αfrom␈αthe␈αform␈αof␈αthe␈αdefintion,␈α(14.8)␈αfollows␈αby␈αan␈αeasy␈αlist␈αinduction␈αon␈α␈↓↓v␈↓␈αand
␈↓ ↓H␈↓(14.7) then follows from (14.8).

␈↓ ↓H␈↓We␈α∞wish␈α∞to␈α∞prove␈α∞that␈α∞␈↓↓partition␈↓␈α∞is␈α∞correct␈α∞according␈α∞to␈α∞some␈α∞reasonable␈α∞criterion.␈α∞  That␈α∂is␈α∞we
␈↓ ↓H␈↓would␈α
like␈α
to␈α
show␈α
that␈α∞every␈α
partition␈α
of␈α
␈↓↓u␈↓␈α
into␈α
␈↓↓n␈↓␈α∞parts␈α
is␈α
a␈α
member␈α
of␈α
the␈α∞list␈α
␈↓↓partition[u,n]␈↓
␈↓ ↓H␈↓and␈α∂that␈α∞every␈α∂member␈α∂of␈α∞␈↓↓partition[u,n]␈↓␈α∂is␈α∂a␈α∞partition␈α∂of␈α∂␈↓↓u␈↓␈α∞into␈α∂␈↓↓n␈↓␈α∂parts␈α∞(assuming␈α∂ ␈↓↓u␈↓␈α∂can␈α∞be
␈↓ ↓H␈↓partitioned␈αinto␈α␈↓↓n␈↓␈αparts).␈α Thus␈αwe␈αmust␈αformalize␈αwhat␈αit␈αmeans␈αfor␈αa␈αlist␈α␈↓↓w␈↓␈αto␈αbe␈αpartition␈αof␈α␈↓↓u␈↓
␈↓ ↓H␈↓into ␈↓↓␈↓ parts.  Let ␈↓↓ispartition[u,w,n]␈↓ express this fact.  Then the statement we wish to prove is

␈↓ ↓H␈↓14.10)␈↓ β∧␈↓↓∀u w n:[length u ≥ n > ␈↓¬0 ␈↓↓⊃ [wεpartition[u,n] ≡ ispartition[u,w,n]] ]␈↓ . 

␈↓ ↓H␈↓We␈α∂can␈α∂formalize␈α∂our␈α∂discussion␈α∂at␈α∞the␈α∂beginning␈α∂of␈α∂this␈α∂section␈α∂as␈α∞to␈α∂what␈α∂it␈α∂means␈α∂to␈α∂be␈α∞a
␈↓ ↓H␈↓partition of a given list as follows:

␈↓ ↓H␈↓14.11)␈↓ αu␈↓↓∀v w n:[ispartition[v, w, n] ≡ length w = n ∧ listlist w ∧ combine w = v]␈↓. 

␈↓ ↓H␈↓where

␈↓ ↓H␈↓14.12)␈↓ ∧%␈↓↓∀u:[listlist u ≡ ␈↓αn|␈↓↓u ∨ [plistp ␈↓αa|␈↓↓u ∧ listlist ␈↓αd|␈↓↓u]]␈↓ 

␈↓ ↓H␈↓14.13)␈↓ ¬∞␈↓↓∀x:[plistp x ≡ ¬␈↓αn|␈↓↓x ∧ islist x]␈↓ 

␈↓ ↓H␈↓14.14)␈↓ βc␈↓↓∀w:[combine w = ␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓w * combine ␈↓αd|␈↓↓w]␈↓ 

␈↓ ↓H␈↓␈↓↓listlist u␈↓␈α
expresses␈α
the␈α
fact␈α
that␈α
 ␈↓↓u␈↓␈αis␈α
a␈α
list␈α
of␈α
non-empty␈α
lists.␈α That␈α
it␈α
is␈α
consistent␈α
to␈α
add␈αsuch
␈↓ ↓H␈↓axioms␈αto␈αour␈αtheory␈αis␈αa␈αstraighforward␈α
application␈αof␈αthe␈αmethods␈αof␈αsections␈α␈↓π∞␈↓11␈αand␈α
␈↓π∞␈↓12␈αand
␈↓ ↓H␈↓we will not give the details.

␈↓ ↓H␈↓        There␈α
is␈αa␈α
second␈α
aspect␈αto␈α
the␈α
correctness␈αof␈α
␈↓↓partition,␈↓␈α
namely␈αthat␈α
each␈α
partition␈αoccurs
␈↓ ↓H␈↓only once in the answer, e.g.

␈↓ ↓H␈↓14.15)␈↓ β9␈↓↓∀u w n:[length u ≥n > ␈↓¬0 ␈↓↓⊃ multiplicity[w,partition[u,n]] ≤ ␈↓¬1␈↓↓]␈↓ 
␈↓ ↓H␈↓84␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓where␈α⊂␈↓↓multiplicity[x,u]␈↓␈α⊂is␈α⊂the␈α⊂number␈α⊂of␈α⊂occurrences␈α⊂of␈α∂␈↓↓x␈↓␈α⊂in␈α⊂the␈α⊂list␈α⊂␈↓↓u.␈↓␈α⊂ We␈α⊂will␈α⊂prove␈α∂only
␈↓ ↓H␈↓(14.10), and leave (14.15) as an exercise.

␈↓ ↓H␈↓        Thus␈α∂we␈α∂will␈α∞proceed␈α∂with␈α∂our␈α∞proof␈α∂of␈α∂(14.10).␈α∞  First␈α∂note␈α∂that␈α∞using␈α∂the␈α∂definition␈α∞of
␈↓ ↓H␈↓␈↓↓member␈↓␈αgiven␈αby␈α(I.8.6)␈αwe␈αmay␈αdefine␈αthe␈αrelation␈α"ε"␈αand␈αshow␈α(using␈αthe␈αmethod␈αof␈α␈↓π∞␈↓12)␈αthat␈αit
␈↓ ↓H␈↓satisfies the equivalence

␈↓ ↓H␈↓14.16)␈↓ ∧?␈↓↓∀x u: [x ε u ≡ ¬␈↓αn|␈↓↓u ∧ [x = ␈↓αa|␈↓↓u ∨ x ε ␈↓αd|␈↓↓u] ]␈↓. 

␈↓ ↓H␈↓We recall the axioms for the defintion of ␈↓↓length␈↓

␈↓ ↓H␈↓␈↓¬LENGTH: ␈↓␈↓ β8␈↓↓∀u:[length u = ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓¬1␈↓↓+length ␈↓αd|␈↓↓u]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓∀u: numberp length u␈↓

␈↓ ↓H␈↓        The␈αfollowing␈αlemmas␈αare␈αeasy␈αto␈αprove␈α(either␈αby␈αdirect␈αcomputation␈αor␈αsimple␈αinduction)
␈↓ ↓H␈↓and will be left as exercises.

␈↓ ↓H␈↓14.17)␈↓ β8␈↓↓∀w:[length w = ␈↓¬1 ␈↓↓∧ listlist w ⊃ combine w = ␈↓αa|␈↓↓w]␈↓

␈↓ ↓H␈↓14.18)␈↓ β8␈↓↓∀w u v:[w ε u*v ≡ w ε u ∨ w ε v]␈↓

␈↓ ↓H␈↓14.19)␈↓ β8␈↓↓∀w x v:[w ε tack[x,v] ≡ ∃w1:[w1 ε v ∧ w = x . w1]]␈↓

␈↓ ↓H␈↓14.20)␈↓ β8␈↓↓∀u v:[¬␈↓αn|␈↓↓u ⊃ ∀w:[w ε v ⊃ listlist w] ⊃ ∀w:[w ε tack[u,v] ⊃ listlist w]]␈↓

␈↓ ↓H␈↓14.21)␈↓ β8␈↓↓∀v w:[length w > 1 ∧ listlist w ∧ combine w = v ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓⊃ ∃v1:[␈↓αa|␈↓↓w = <␈↓αa|␈↓↓v>*v1 ∧ ␈↓αd|␈↓↓v = v1*[combine ␈↓αd|␈↓↓w]]]␈↓


␈↓ ↓H␈↓To prove (14.10) we assume that ␈↓↓u␈↓ and n␈↓↓␈↓ satisfy ␈↓↓length u ≥ n ≥ ␈↓¬1␈↓↓␈↓ and show that

␈↓ ↓H␈↓14.22)␈↓ ∧'␈↓↓∀w:[wεpartition[u,n] ≡ ispartition[u,w,n] ]␈↓ . 

␈↓ ↓H␈↓We consider two cases ␈↓↓n=␈↓¬1␈↓↓␈↓ and ␈↓↓n>␈↓¬1␈↓↓␈↓.  In the case that ␈↓↓n=␈↓¬1␈↓↓␈↓ we have

␈↓ ↓H␈↓␈↓ α8␈↓↓partition[u,n] = <<u>> ␈↓␈↓ π8by (14.4)
␈↓ ↓H␈↓␈↓ α8␈↓↓w ε <<u>> ≡ w= <u>␈↓␈↓ π8by (14.16)

␈↓ ↓H␈↓thus ␈↓↓w ε partition[u,n]␈↓ gives

␈↓ ↓H␈↓␈↓ α8␈↓↓length w = ␈↓¬1 ␈↓↓␈↓␈↓ π8by ␈↓¬LENGTH ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓listlist w  ␈↓␈↓ π8by (14.12) since by hypothesis ␈↓↓plistp u␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓combine w = u␈↓␈↓ π8by (14.14) and properties of ␈↓↓append␈↓


␈↓ ↓H␈↓which␈αis␈αequivalent␈αto␈α␈↓↓ispartition[u,w,␈↓¬1␈↓↓]␈↓,␈αand␈αconversely␈αif␈α␈↓↓ispartition[u,w,␈↓¬1␈↓↓]␈↓␈αthen␈αusing␈α(14.17)
␈↓ ↓H␈↓we get ␈↓↓u=␈↓αa|␈↓↓w␈↓ or ␈↓↓w=<u>,␈↓ which completes the proof when ␈↓↓n␈↓ is ␈↓¬1. ␈↓

␈↓ ↓H␈↓        In␈αthe␈α
case␈αthat␈α
␈↓↓n␈↓␈αis␈α
greater␈αthan␈α
␈↓¬1␈α␈↓we␈α
have␈α␈↓↓partition[u,n]=partn[<␈↓αa|␈↓↓u>,␈↓αd|␈↓↓u,n-␈↓¬1␈↓↓]␈↓␈α
and␈αthe
␈↓ ↓H␈↓appropriate␈α
thing␈αto␈α
do␈αis␈α
to␈αprove␈α
a␈α
theorem␈αabout␈α
␈↓↓partn␈↓␈αwhich␈α
applied␈αto␈α
this␈α
particular␈αcase
␈↓ ↓H␈↓will give us the desired result.  We might try
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *85


␈↓ ↓H␈↓␈↓ α/␈↓↓∀u v w n:[¬␈↓αn|␈↓↓u ∧ length v ≥ n ≥ ␈↓¬1 ␈↓↓⊃ [w ε partn[u,v,n] ≡ ispartition[u*v,w,n+␈↓¬1␈↓↓]]␈↓ . 

␈↓ ↓H␈↓It␈αwould␈αindeed␈αgive␈αus␈αthe␈αproof␈αthat␈αwe␈αwish,␈αhowever␈αit␈αis␈αnot␈αtrue.␈α The␈αreason␈αis␈αthat␈α␈↓↓partn␈↓
␈↓ ↓H␈↓produces␈αonly␈αthose␈αpartitions␈αin␈αwhich␈αthe␈αfirst␈αelement␈αis␈αan␈αextension␈αof␈αthe␈αargument␈α␈↓↓u,␈↓␈αand
␈↓ ↓H␈↓this␈α∞condition␈α∞must␈α∞be␈α∞added␈α∞to␈α∞the␈α∞right␈α∞hand␈α∞side␈α∞of␈α∞the␈α∞equivalence␈α∞before␈α∞we␈α∞have␈α∞a␈α∞true
␈↓ ↓H␈↓theorem.  Thus we wish to show the following

␈↓ ↓H␈↓14.23)␈↓ αI␈↓↓∀u v w m:[¬␈↓αn|␈↓↓u ∧ length v ≥ m ≥ ␈↓¬1 ␈↓↓⊃ [w ε partn[u,v,m] ≡ ispartn[u,v,w,m]]␈↓ .

␈↓ ↓H␈↓where

␈↓ ↓H␈↓14.24)␈↓ αd␈↓↓∀u v w m:[ispartn[u, v, w, m] ≡ ispartition[u*v,w,m+␈↓¬1␈↓↓] ∧ ishead[u,v,w]␈↓

␈↓ ↓H␈↓and

␈↓ ↓H␈↓14.25)␈↓ β9␈↓↓∀u v w:[ishead[u,v,w] ≡ ∃u1:[u*u1=␈↓αa|␈↓↓w ∧ u1*combine[␈↓αd|␈↓↓w]=v]]␈↓

␈↓ ↓H␈↓This␈α∞we␈α
will␈α∞prove␈α∞by␈α
an␈α∞induction␈α∞on␈α
␈↓↓v.␈↓␈α∞  As␈α
above␈α∞we␈α∞will␈α
assume␈α∞that␈α∞␈↓↓length v ≥ m ≥␈↓¬1␈↓↓␈↓␈α
and
␈↓ ↓H␈↓␈↓↓¬␈↓αn|␈↓↓u␈↓ and show that

␈↓ ↓H␈↓14.26)␈↓ ∧&␈↓↓∀w:[w ε partn[u,v,m] ≡ ispartn[u,v,w,m]]␈↓ . 

␈↓ ↓H␈↓First␈α∂we␈α∂will␈α∂prove␈α∂the␈α∂forward␈α∂implication.␈α∂ Thus␈α∂we␈α∂assume␈α∂␈↓↓wεpartn[u,v,m]␈↓␈α∂and␈α∂show␈α∂that
␈↓ ↓H␈↓␈↓↓ispartn[u,v,w,m]␈↓ holds.   In the case that ␈↓↓m=␈↓¬1␈↓↓␈↓ we have

␈↓ ↓H␈↓␈↓ βE␈↓↓partn[u,v,m]=tack[u, <<v>>] * partn[u * <␈↓αa|␈↓↓v>, ␈↓αd|␈↓↓v, m]]␈↓ . 

␈↓ ↓H␈↓As␈α
␈↓↓tack␈↓␈α∞and␈α
␈↓↓partn␈↓␈α
produce␈α∞lists␈α
we␈α
may␈α∞use␈α
lemma␈α
(14.18)␈α∞and␈α
thus␈α
reduce␈α∞to␈α
one␈α
of␈α∞the␈α
cases
␈↓ ↓H␈↓␈↓↓wεtack[u,<<v>>]␈↓ and ␈↓↓wεpartn[u*<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,m]␈↓.  In the first case we have

␈↓ ↓H␈↓␈↓ α8␈↓↓w=<u v>␈↓␈↓ π8by (14.6) and (14.16)
␈↓ ↓H␈↓␈↓ α8␈↓↓length w = ␈↓¬2 ␈↓↓␈↓␈↓ π8by ␈↓¬LENGTH ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓listlist w  ␈↓␈↓ π8by (14.12) since by
␈↓ ↓H␈↓␈↓ π8hypothesis ␈↓↓plistp u␈↓  and ␈↓↓plistp v␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓combine w = u*v␈↓␈↓ π8by (14.14) and properties of ␈↓↓append␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓u*␈↓¬NIL␈↓↓=␈↓αa|␈↓↓w ∧ ␈↓¬NIL␈↓↓*combine ␈↓αd|␈↓↓w=v␈↓␈↓ π8by (14.14) and properties of ␈↓↓append␈↓

␈↓ ↓H␈↓and␈α␈↓↓ispartn[u,v,w,␈↓¬1␈↓↓]␈↓␈αfollows.␈α In␈αthe␈αsecond␈αcase␈α␈↓↓wεpartn[u*<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,␈↓¬1␈↓↓]␈↓␈αimplies␈αthat␈α
the␈αresult
␈↓ ↓H␈↓is not ␈↓¬NIL␈↓ and hence ␈↓↓length ␈↓αd|␈↓↓v ≥ ␈↓¬1␈↓↓␈↓, so we may apply the induction hypothesis which says that

␈↓ ↓H␈↓␈↓ α8␈↓↓w ε partn[u*<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,␈↓¬1␈↓↓] ≡ ispartn[u*<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,w,␈↓¬1␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ π8by induction
␈↓ ↓H␈↓thus
␈↓ ↓H␈↓␈↓ αx␈↓↓≡ispartition[[u*<␈↓αa|␈↓↓v>]*␈↓αd|␈↓↓v,w,␈↓¬2␈↓↓] ∧ ∃u1:[[u*≤␈↓αa|␈↓↓v>]*u1]=␈↓αa|␈↓↓w ∧ u1*combine[␈↓αd|␈↓↓w]=␈↓αd|␈↓↓v]␈↓
␈↓ ↓H␈↓␈↓ π8by (14.24)
␈↓ ↓H␈↓␈↓ αx␈↓↓≡ ispartition[u*v,w,␈↓¬2␈↓↓] ∧ ∃u2:[[u*u2]=␈↓αa|␈↓↓w ∧ u2*combine[␈↓αd|␈↓↓w]= v]␈↓
␈↓ ↓H␈↓␈↓ π8by (14.11) using ␈↓↓u2=<␈↓αa|␈↓↓v>*u1␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓≡ ispartn[u,v,w,␈↓¬1␈↓↓]␈↓␈↓ π8by (14.24)

␈↓ ↓H␈↓In the case ␈↓↓m>␈↓¬1␈↓↓␈↓ we have
␈↓ ↓H␈↓86␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓␈↓ αc␈↓↓partn[u,v,m]=tack[u, partn[<␈↓αa|␈↓↓v>, ␈↓αd|␈↓↓v, m-␈↓¬1␈↓↓]] * partn[u * <␈↓αa|␈↓↓v>, ␈↓αd|␈↓↓v, m]␈↓ 

␈↓ ↓H␈↓from␈α((14.5).␈α) Again␈α(we␈α)have␈α(the␈α)two␈α(cases␈α)␈↓↓wεtack[u,partn[<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,m-␈↓¬1␈↓↓]]␈↓␈α(or
␈↓ ↓H␈↓␈↓↓wεpartn[u*<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,m]␈↓␈α≥.␈α≡Suppose␈α≥␈↓↓wεtack[u,partn[<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,m-␈↓¬1␈↓↓]]␈↓,␈α≥then␈α≡␈↓↓w=u . w1␈↓␈α≥where
␈↓ ↓H␈↓␈↓↓w1εpartn[<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,m-␈↓¬1␈↓↓]␈↓␈αby␈α(14.19).␈α By␈αinduction␈αwe␈αhave␈α␈↓↓ispartn[<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,w1,m-␈↓¬1␈↓↓]␈↓␈α
and␈αthus
␈↓ ↓H␈↓␈↓↓ispartition[v,w1,m]␈↓.  Taking ␈↓↓u2␈↓ as ␈↓¬NIL␈↓ and using ␈↓↓w=u . w1␈↓ we get

␈↓ ↓H␈↓␈↓ β≥␈↓↓ispartition[u*v,w,m+␈↓¬1␈↓↓] ∧ ∃u2: [[u*u2]=␈↓αa|␈↓↓w ∧ u2*combine[␈↓αd|␈↓↓w]= v]␈↓ 

␈↓ ↓H␈↓thus␈α
 proving␈α
␈↓↓ispartn[u,v,w,m]␈↓␈α
as␈α
desired.␈α In␈α
the␈α
case␈α
that␈α
␈↓↓wεpartn[u*<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,m]␈↓␈α
the␈αfact␈α
that
␈↓ ↓H␈↓␈↓↓ispartn[u,v,w,m]␈↓ follows by induction as in the case ␈↓↓m=␈↓¬1␈↓↓␈↓

␈↓ ↓H␈↓To␈αprove␈α
the␈αbackward␈α
implication␈αwe␈α
assume␈α␈↓↓ispartn[u,v,w,m]␈↓␈α
Thus␈α␈↓↓listlist w␈↓,␈α
␈↓↓length w = m+␈↓¬1␈↓↓␈↓,
␈↓ ↓H␈↓␈↓↓combine w =u*v␈↓,␈α⊂␈↓↓u*u1=␈↓αa|␈↓↓w␈↓␈α⊃and␈α⊂␈↓↓u1*combine ␈↓αd|␈↓↓w = v␈↓␈α⊂for␈α⊃some␈α⊂list␈α⊂␈↓↓u1.␈↓␈α⊃ Now␈α⊂there␈α⊂are␈α⊃two␈α⊂cases
␈↓ ↓H␈↓depending on whether or not ␈↓↓u1␈↓ is ␈↓¬NIL␈↓.

␈↓ ↓H␈↓        If␈α
␈↓↓u1=␈↓¬NIL␈↓↓␈↓␈α
then␈α␈↓↓u=␈↓αa|␈↓↓w␈↓␈α
and␈α
␈↓↓v = combine ␈↓αd|␈↓↓w␈↓.␈α In␈α
the␈α
case␈α
␈↓↓m=␈↓¬1␈↓↓␈↓␈αwe␈α
see␈α
that␈α␈↓↓w=<u␈α
v>␈↓␈α
and␈αthus␈α
is
␈↓ ↓H␈↓in ␈↓↓partn[u,v,m]␈↓ (recall the forward argument for the case ␈↓↓m=␈↓¬1␈↓↓␈↓).  If ␈↓↓m>␈↓¬1␈↓↓␈↓ then we have

␈↓ ↓H␈↓␈↓ α8␈↓↓w=u . w1␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓length w1 = m ␈↓␈↓ π8by ␈↓¬LENGTH ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓listlist w1 ␈↓␈↓ π8by (14.12)
␈↓ ↓H␈↓␈↓ α8␈↓↓combine w1 = <␈↓αa|␈↓↓v>*␈↓αd|␈↓↓v␈↓␈↓ π8since ␈↓↓combine ␈↓αd|␈↓↓w = v␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓<␈↓αa|␈↓↓v>*␈↓αda|␈↓↓w1=␈↓αa|␈↓↓w1 ∧ ␈↓αda|␈↓↓w1*combine ␈↓αd|␈↓↓w1=␈↓αd|␈↓↓v␈↓␈↓ π8by (14.14) and properties of ␈↓↓append␈↓

␈↓ ↓H␈↓thus␈α∞we␈α∞see␈α∞that␈α∞␈↓↓ispartn[<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,w1,m-␈↓¬1␈↓↓]␈↓␈α∞and␈α∞by␈α∞induction␈α∞␈↓↓w1εpartn[<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,m-␈↓¬1␈↓↓]␈↓.␈α
  Thus
␈↓ ↓H␈↓␈↓↓wεtack[u,partn[<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,m-␈↓¬1␈↓↓]]␈↓.

␈↓ ↓H␈↓        In␈α
the␈α
case␈α
that␈α
␈↓↓u1␈↓␈α
is␈α
not␈α∞␈↓¬NIL␈↓␈α
then␈α
 it␈α
is␈α
easy␈α
to␈α
see␈α
that␈α∞␈↓↓ispartn[u*<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,w,m]␈↓.␈α
 The
␈↓ ↓H␈↓only␈αpossible␈αtrick␈αis␈αto␈αprove␈αthe␈α"∃"␈αclause.␈α This␈αis␈αdone␈αby␈αtaking␈α␈↓↓u1␈↓␈αin␈αthe␈αlatter␈αcase␈αto␈αbe␈α␈↓αd␈↓
␈↓ ↓H␈↓of␈α
the␈α
␈↓↓u1␈↓␈αguaranteed␈α
to␈α
exist␈α
by␈αthe␈α
fact␈α
that␈α
␈↓↓ispartn[u,v,w,m]␈↓.␈α Therefore␈α
by␈α
induction␈αwe␈α
have
␈↓ ↓H␈↓␈↓↓wεpartn[u*<␈↓αa|␈↓↓v>,␈↓αd|␈↓↓v,m]␈↓ which completes the proof.

␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.  Prove the lemmas (14.17) to (14.21).

␈↓ ↓H␈↓2.  Prove the remaining criterion (14.15) for correctness of ␈↓↓partition.␈↓



␈↓ ↓H␈↓15.  ␈↓αPartial functions and the Minimization Schema.␈↓


␈↓ ↓H␈↓        So␈α⊃far␈α⊃we␈α⊃have␈α⊃dealt␈α⊃only␈α⊃with␈α⊃recursive␈α⊃programs␈α⊃that␈α⊃defined␈α⊃total␈α⊃functions␈α⊃on␈α⊃S-
␈↓ ↓H␈↓expressions.␈α⊃  In␈α⊃these␈α⊂cases␈α⊃the␈α⊃functional␈α⊃equation␈α⊂corresponding␈α⊃to␈α⊃the␈α⊃recursive␈α⊂definition
␈↓ ↓H␈↓completely␈α∃characterized␈α∃the␈α∃function␈α∃on␈α∀S-expressions.␈α∃ Frequently␈α∃we␈α∃are␈α∃interested␈α∃in␈α∀a
␈↓ ↓H␈↓program␈α
that␈αdoes␈α
not␈αalways␈α
terminate␈αand␈α
in␈αthis␈α
case␈αthere␈α
are␈αlikely␈α
to␈αbe␈α
many␈αfunctions␈α
that
␈↓ ↓H␈↓satisfy␈α∞the␈α∞corresponding␈α∞functional␈α∞equation.␈α∞ What␈α
ever␈α∞we␈α∞prove␈α∞using␈α∞this␈α∞equation␈α∞will␈α
be
␈↓ ↓H␈↓true␈αfor␈αall␈αof␈αthese␈αfunctions,␈αbut␈αthere␈αare␈αalso␈αfacts␈αabout␈αthe␈αfunction␈αwe␈αintend␈αto␈αdefine␈αthat
␈↓ ↓H␈↓will not be provable using only the functional equation.  For example, the program
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *87


␈↓ ↓H␈↓15.1)␈↓ ¬
␈↓↓loop x ← loop x                   ␈↓ 

␈↓ ↓H␈↓leads to the sentence

␈↓ ↓H␈↓15.2)␈↓ ¬␈↓↓∀x: [loop x = loop x]            ␈↓ 

␈↓ ↓H␈↓which␈αprovides␈αno␈αinformation␈α(all␈α
functions␈αsatisfy␈αit)␈αalthough␈αthe␈αfunction␈α
␈↓↓loop␈↓␈αcorresponding
␈↓ ↓H␈↓to the program is undefined for all ␈↓↓x.␈↓  This is not always the case.   Recall the program

␈↓ ↓H␈↓15.3)␈↓ ∧Q␈↓↓omega x ← [omega x] . ␈↓¬NIL␈↓↓               ␈↓ 

␈↓ ↓H␈↓which has the functional equation

␈↓ ↓H␈↓15.4)␈↓ ∧R␈↓↓∀x: [omega x = [omega x] . ␈↓¬NIL␈↓↓] .      ␈↓ 

␈↓ ↓H␈↓We␈αshowed␈α(informally)␈αin␈α␈↓π∞␈↓␈α1␈αthat␈αassuming␈α␈↓↓issexp␈αomega␈αx␈↓␈αleads␈αto␈αa␈αcontradiction␈αthus␈αwe␈αcan
␈↓ ↓H␈↓show ␈↓↓∀x: [¬issexp omega x]␈↓, using the functional equation and induction.

␈↓ ↓H␈↓        In␈α
order␈αto␈α
characterize␈α
recursive␈αprograms,␈α
we␈α
need␈αsome␈α
way␈α
of␈αexpressing␈α
the␈α
fact␈αthat
␈↓ ↓H␈↓the␈αfunction␈αwe␈α
mean␈αis␈αthe␈α
least␈αdefined␈αsolution␈αof␈α
the␈αfunctional␈αequation.␈α
  We␈αwill␈αdo␈αthis␈α
by
␈↓ ↓H␈↓introducing,␈α∩in␈α∩addition␈α∩to␈α∩the␈α⊃functional␈α∩equation,␈α∩a␈α∩schema␈α∩called␈α∩the␈α⊃␈↓↓minimization schema␈↓
␈↓ ↓H␈↓which␈α
expresses␈α
the␈αfact␈α
that␈α
every␈α
function␈αsatisfying␈α
the␈α
equation␈α
is␈αdefined␈α
at␈α
least␈αevery␈α
where
␈↓ ↓H␈↓that the function assigned to the program is defined, and has the same value in those cases.

␈↓ ↓H␈↓        Recall the general form for a recursive definition

␈↓ ↓H␈↓15.5) ␈↓ ¬8␈↓↓f x ← ␈↓πt␈↓↓[f,x]             ␈↓ 

␈↓ ↓H␈↓and the corredsponing functional equation

␈↓ ↓H␈↓15.6) ␈↓ ¬⊃␈↓↓∀x: f x = ␈↓πt␈↓↓[f,x] .              ␈↓ 

␈↓ ↓H␈↓The minimization schema for this form of recursive definition has the form

␈↓ ↓H␈↓15.7)␈↓ β ␈↓↓∀x: [issexp ␈↓πt␈↓↓[F] x ⊃ F x = ␈↓πt␈↓↓[F] x] ⊃ ∀x: [issexp f x ⊃ f x = F x] ␈↓. 

␈↓ ↓H␈↓Here␈α
␈↓↓F␈↓␈α
is␈αa␈α
function␈α
parameter.␈α
  This␈αschema␈α
is␈α
really␈α␈↓↓schema schema,␈↓␈α
since␈α
for␈α
any␈αparticular
␈↓ ↓H␈↓term␈α⊂␈↓πt␈↓␈α⊂and␈α⊂function␈α⊂symbol␈α⊂␈↓↓f␈↓␈α⊂we␈α⊂produce␈α⊂from␈α⊂the␈α⊂schema␈α⊂a␈α⊂formula␈α⊂that␈α⊂still␈α⊃contains␈α⊂the
␈↓ ↓H␈↓paramater␈α∩␈↓↓F.␈↓␈α⊃ We␈α∩then␈α⊃must␈α∩substitute␈α⊃some␈α∩function␈α⊃expression␈α∩of␈α⊃our␈α∩language␈α⊃for␈α∩␈↓↓F␈↓␈α⊃to
␈↓ ↓H␈↓produce␈α
an␈α
axiom.␈α
 (This␈αis␈α
similar␈α
to␈α
the␈α
induction␈αschemas,␈α
except␈α
there␈α
we␈αsubstituted␈α
formulas
␈↓ ↓H␈↓for predicate parameters.)

␈↓ ↓H␈↓        The␈αsimplest␈α
application␈αof␈αthe␈α
minimization␈αschema␈αis␈α
to␈αshow␈αthat␈α
the␈αprogram␈α
for␈α␈↓↓loop␈↓
␈↓ ↓H␈↓given above computes the totally undefined function.  The minimization schema for loop is

␈↓ ↓H␈↓15.8)␈↓ β$␈↓↓∀x:[issexp F x ⊃ F x = F x] ⊃ ∀x: [issexp loop x ⊃ loop x = F x] ␈↓. 

␈↓ ↓H␈↓If we let ␈↓↓F␈↓ be the function expression λx: ␈↓π|␈↓ then we obtain the axiom

␈↓ ↓H␈↓␈↓ βZ␈↓↓∀x:[issexp ␈↓π|␈↓↓ ⊃ ␈↓π|␈↓↓ = ␈↓π|␈↓↓] ⊃ ∀x: [issexp loop x ⊃ loop x = ␈↓π|␈↓↓] ␈↓. 
␈↓ ↓H␈↓88␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓The left side of the implication is identically true.  Thus we have

␈↓ ↓H␈↓␈↓ ∧x␈↓↓∀x: [issexp loop x ⊃ loop x = ␈↓π|␈↓↓] ␈↓. 

␈↓ ↓H␈↓If we assume ␈↓↓issexp loop x␈↓ we find that issexp ␈↓π|␈↓ which contradicts ␈↓¬SEXP3. ␈↓ Thus we have shown

␈↓ ↓H␈↓␈↓ ¬∧␈↓↓∀x: ¬issepx loop x                ␈↓ 

␈↓ ↓H␈↓as desired.

␈↓ ↓H␈↓        The␈α⊃minimization␈α⊃schema␈α∩provides␈α⊃an␈α⊃alternate␈α⊃method␈α∩for␈α⊃proving␈α⊃the␈α∩correctness␈α⊃of
␈↓ ↓H␈↓␈↓↓samefringe.␈↓␈α To␈αsimplify␈αmatters␈αwe␈αeliminate␈αthe␈αauxiliary␈αfunction␈α␈↓↓same␈↓␈αfrom␈αthe␈αdefinition␈αof
␈↓ ↓H␈↓␈↓↓samefringe␈↓␈α⊂thus␈α⊂eliminating␈α⊂the␈α∂problem␈α⊂of␈α⊂having␈α⊂to␈α∂deal␈α⊂with␈α⊂mutually␈α⊂recursive␈α∂programs.
␈↓ ↓H␈↓The functional defining ␈↓↓samefringe␈↓ now becomes

␈↓ ↓H␈↓␈↓ αB␈↓↓␈↓πt␈↓↓␈↓βsf␈↓↓ = λf: λx y: [[x equal y] or [[not atom x and not atom y] and                        ␈↓
␈↓ ↓H␈↓␈↓ αs␈↓↓             [[␈↓αa|␈↓↓gopher x equal ␈↓αa|␈↓↓gopher y] and f[␈↓αd|␈↓↓gopher x, ␈↓αd|␈↓↓gopher y]]]] ␈↓.

␈↓ ↓H␈↓Now␈αwe␈αform␈αan␈αaxiom␈αschema␈αfrom␈αthe␈αminimization␈αschema␈αby␈αusing␈αthe␈α␈↓πt␈↓␈↓βsf␈↓␈αgiven␈αabove␈αand
␈↓ ↓H␈↓␈↓↓samefringe␈↓ for ␈↓↓f.␈↓ We instantiate this schema with

␈↓ ↓H␈↓␈↓ ∧<␈↓↓F[x, y] = fringe x eq fringe y  .            ␈↓ 

␈↓ ↓H␈↓The proof then consists of the following steps

␈↓ ↓H␈↓        1)      ␈↓↓∀x y: [issexp ␈↓πt␈↓↓␈↓βsf␈↓↓[F] [x, y]]␈↓
␈↓ ↓H␈↓        2)      ␈↓↓∀x y: [F[x, y] = ␈↓πt␈↓↓␈↓βsf␈↓↓[F] [x, y]]␈↓
␈↓ ↓H␈↓        3)      ␈↓↓∀x y:[issexp samefringef[x,y]␈↓

␈↓ ↓H␈↓from 1) - 3) and the axiom instantiation we conclude

␈↓ ↓H␈↓        4)      ␈↓↓∀x y: [fringe x equal fringe y = samefringef[x, y]]␈↓

␈↓ ↓H␈↓from the definition of ␈↓↓samefringe␈↓ and 4) we conclude

␈↓ ↓H␈↓        5)      ␈↓↓∀x y: [samefringe[x, y] ≡ fringe x = fringe y]␈↓

␈↓ ↓H␈↓as desired.

␈↓ ↓H␈↓        The␈αminimization␈αschema␈αcan␈αsometimes␈αbe␈αused␈αto␈αshow␈αpartial␈αcorrectness.␈α For␈αexample,
␈↓ ↓H␈↓the well known 91-function is defined by the recursive program over the integers

␈↓ ↓H␈↓␈↓ βw␈↓↓f␈↓β91␈↓↓ x ← ␈↓αif␈↓↓ x > 100 ␈↓αthen␈↓↓ x - 10 ␈↓αelse␈↓↓ f␈↓β91␈↓↓ f␈↓β91␈↓↓ [x + 11] ␈↓. 

␈↓ ↓H␈↓The goal is to show that

␈↓ ↓H␈↓␈↓ β␈␈↓↓∀x: [f␈↓β91␈↓↓ x = ␈↓αif␈↓↓ x > 100 ␈↓αthen␈↓↓ x - 10 ␈↓αelse␈↓↓ 91] .        ␈↓ 

␈↓ ↓H␈↓We apply the minimization schema with
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *89


␈↓ ↓H␈↓␈↓ ∧∩␈↓↓F x = ␈↓αif␈↓↓ x > 100 ␈↓αthen␈↓↓ x - 10 ␈↓αelse␈↓↓ 91 ,            ␈↓ 

␈↓ ↓H␈↓and␈α
it␈αcan␈α
be␈α
shown␈αby␈α
an␈α
explicit␈αcalculation␈α
without␈α
induction␈αthat␈α
the␈α
premiss␈αof␈α
the␈αschema␈α
is
␈↓ ↓H␈↓satisfied, and this shows that ␈↓↓f␈↓β91␈↓↓␈↓, whenever defined has the desired value.

␈↓ ↓H␈↓        The␈α∂method␈α∂of␈α∂␈↓↓recursion␈↓␈α∂␈↓↓induction␈↓␈α∂is␈α∂also␈α∂an␈α∂immediate␈α∂application␈α∂of␈α∂the␈α∞minimization
␈↓ ↓H␈↓schema.␈α
 If␈αwe␈α
show␈αthat␈α
two␈αfunctions␈α
satisfy␈αthe␈α
schema␈αof␈α
a␈αrecursive␈α
program,␈αwe␈α
show␈αthat
␈↓ ↓H␈↓they both equal the function computed by the program whereever the function is defined.

␈↓ ↓H␈↓        The␈α
utility␈α
of␈α
the␈α
minimization␈αschema␈α
for␈α
proving␈α
partial␈α
correctness␈α
or␈αnon-termination
␈↓ ↓H␈↓depends␈α
on␈α
our␈α
ability␈α
to␈α
name␈α∞suitable␈α
comparison␈α
functions.␈α
 ␈↓↓loop␈↓␈α
and␈α
␈↓↓f␈↓β91␈↓↓␈↓␈α
were␈α∞easily␈α
treated,
␈↓ ↓H␈↓because␈α⊃the␈α⊃necessary␈α⊃comparison␈α⊃functions␈α⊃could␈α⊃be␈α⊃given␈α⊃explicitly␈α⊃without␈α⊃recursion.␈α⊂ Any
␈↓ ↓H␈↓extension␈αof␈αthe␈αlanguage␈αthat␈αprovides␈αnew␈αtools␈αfor␈αnaming␈αcomparison␈αfunctions,␈αe.g.␈αgoing␈αto
␈↓ ↓H␈↓higher order logic, will improve our ability to use the schema in proofs.
␈↓ ↓H␈↓90␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓16.  ␈↓αExercises Part 1.␈↓


␈↓ ↓H␈↓        The␈α∃following␈α∀are␈α∃properties␈α∀to␈α∃prove␈α∃about␈α∀programs␈α∃that␈α∀you␈α∃have␈α∃written.␈α∀ The
␈↓ ↓H␈↓functions␈αand␈αpredicates␈αreferred␈αto␈αare␈αdescribed␈αin␈αthe␈αExercises␈αof␈αChapter␈αII␈α␈↓π∞␈↓8.␈α  You␈αshould
␈↓ ↓H␈↓carry␈αout␈α
the␈αproofs␈αfor␈α
your␈αdefinitions␈α
of␈αthe␈αindicated␈α
functions␈αor␈α
predicates.␈α You␈αmay␈α
decide
␈↓ ↓H␈↓to␈α
rewrite␈α
your␈α
definition␈αbecause␈α
(1)␈α
you␈α
find␈αthat␈α
your␈α
function␈α
definition␈αis␈α
not␈α
correct␈α
or␈α(2)
␈↓ ↓H␈↓another␈α
definition␈α
is␈αcleaner␈α
and␈α
more␈αamenable␈α
to␈α
proofs.␈α This␈α
is␈α
quite␈αnatural␈α
if␈α
you␈α
are␈αnot
␈↓ ↓H␈↓thinking in terms of proving it correct when you write the definition.

␈↓ ↓H␈↓1. Lists

␈↓ ↓H␈↓        1.1. ␈↓↓∀u. samelength[u,reverse u]␈↓

␈↓ ↓H␈↓        1.2. ␈↓↓∀u v:istail[commontail[u,v],v]␈↓
␈↓ ↓H␈↓        1.3. ␈↓↓∀u v:istail[commontail[u,v],u]␈↓

␈↓ ↓H␈↓        1.4. ␈↓↓∀u v: commontail[u,v]=commontail[v,u]␈↓

␈↓ ↓H␈↓        1.5. ␈↓↓∀u v:[append[upto[commontail[u,v],v],commontail[u,v]]=v]␈↓
␈↓ ↓H␈↓        1.6. ␈↓↓∀u v:[append[upto[commontail[u,v],u],commontail[u,v]]=u]␈↓

␈↓ ↓H␈↓Note that ␈↓↓istail␈↓ has already been worked on in earlier exercises.

␈↓ ↓H␈↓2. Sexpressions

␈↓ ↓H␈↓        2.1. ␈↓↓∀x y:[x = get[y,point[x,y]]]␈↓

␈↓ ↓H␈↓        2.2. ␈↓↓∀x:[balanced[balance[x]] samefringe[balance[x],x]]␈↓

␈↓ ↓H␈↓3. Algebra and arithmetic

␈↓ ↓H␈↓        3.1. ␈↓↓∀u v: poly u ∧ poly v ⊃ sum[prod[quot[u,v],v],rem[u,v]␈↓

␈↓ ↓H␈↓        3.2. ␈↓↓∀x: arith x ⊃ numval[sop x]]=numval[x]␈↓

␈↓ ↓H␈↓        Part␈α∞of␈α∞the␈α∞exercise␈α∞is␈α∞to␈α∞invent␈α∞suitable␈α∞predicates␈α∞␈↓↓poly␈↓␈α∞and␈α∞␈↓↓arith␈α∞which␈↓␈α∂characterize␈α∞the
␈↓ ↓H␈↓domain on which the functions are valid.

␈↓ ↓H␈↓4. Sets

␈↓ ↓H␈↓        4.1. ␈↓↓∀x u v:[xε[u∪v] ⊃ xεu ∨ xεv]␈↓

␈↓ ↓H␈↓        4.2. ␈↓↓∀x u v:[[xεu] ⊃ ¬xε[v - u]]␈↓

␈↓ ↓H␈↓5. Permutations

␈↓ ↓H␈↓        5.1. ␈↓↓∀u:[lcycle rcycle u = u]␈↓

␈↓ ↓H␈↓        5.2. ␈↓↓∀u:[isperm1 u ⊃ [compose1[p,invert1[p]] = id1␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *91


␈↓ ↓H␈↓        5.3. ␈↓↓∀u:[isperm1 u ⊃ [compose1[invert1[p],p] = id1␈↓

␈↓ ↓H␈↓        5.4. ␈↓↓u: [isperm1 u ⊃ [invert1 invert1 p = p]]␈↓

␈↓ ↓H␈↓        5.5. ␈↓↓∀u v:[isperm2 u ∧ isperm2 v ⊃ rho21[compose2[u,v]] = compose1[rho21 u,rho21 v2]]␈↓



␈↓ ↓H␈↓17.  ␈↓αExercises Part2.␈↓


␈↓ ↓H␈↓        This␈α∂collection␈α∞of␈α∂exercises␈α∂treats␈α∞properties␈α∂of␈α∂association␈α∞lists,␈α∂substitutions,␈α∂and␈α∞pattern
␈↓ ↓H␈↓matching.␈α∪ These␈α∩topics␈α∪are␈α∪strongly␈α∩interrelated␈α∪and␈α∩the␈α∪ability␈α∪to␈α∩treat␈α∪properties␈α∪of␈α∩such
␈↓ ↓H␈↓programs is fundamental to the general area of symbolic computation.

␈↓ ↓H␈↓1. Association lists

␈↓ ↓H␈↓        Prove the following facts about association lists.

␈↓ ↓H␈↓        1.1.     ␈↓↓∀u v: [alist u ∧ alist v ⊃ alist[u * v]␈↓

␈↓ ↓H␈↓        1.2.     ␈↓↓∀z u v: alist u ∧ alist v ⊃ ␈↓
␈↓ ↓H␈↓                          ␈↓↓[assoc[z,s1*s2] = ␈↓αif␈↓↓ ␈↓αn|␈↓↓assoc[z,s1] ␈↓αthen␈↓↓ assoc[z,s2] ␈↓αelse␈↓↓ assoc[z,s1]]␈↓

␈↓ ↓H␈↓where

␈↓ ↓H␈↓        ␈↓↓alist u ← ␈↓αn|␈↓↓u ∨ [¬␈↓αat|␈↓↓␈↓αa|␈↓↓u ∧ alist ␈↓αd|␈↓↓u]]]␈↓

␈↓ ↓H␈↓        ␈↓↓assoc[x, s] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓s ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ x = ␈↓αaa|␈↓↓s ␈↓αthen␈↓↓ ␈↓αa|␈↓↓s ␈↓αelse␈↓↓ assoc[x, ␈↓αd|␈↓↓s]␈↓

␈↓ ↓H␈↓2. Properties of substitutions and substitution lists.

␈↓ ↓H␈↓        With␈α∩function␈α∪definitions␈α∩as␈α∩given␈α∪below,␈α∩␈↓↓subst[x, y, z]␈↓␈α∩is␈α∪the␈α∩result␈α∩of␈α∪replacing␈α∩the
␈↓ ↓H␈↓variable␈α
␈↓↓y␈↓␈α
by␈α
the␈α
S-expression␈α
␈↓↓x␈↓␈α
whereever␈α
␈↓↓y␈↓␈α
occurs␈αin␈α
␈↓↓z.␈↓␈α
 If␈α
␈↓↓s␈↓␈α
is␈α
a␈α
list␈α
of␈α
substitutions␈α
of␈αthe␈α
form
␈↓ ↓H␈↓␈↓↓y . x␈↓␈α
then␈α
␈↓↓sublis[z, s]␈↓␈αis␈α
the␈α
result␈α
of␈α"simultaneously"␈α
performing␈α
all␈αof␈α
the␈α
substitutions␈α
on␈αthe
␈↓ ↓H␈↓list␈α
to␈α␈↓↓z.␈↓␈α
If␈α
␈↓↓s1␈↓␈αand␈α
␈↓↓s2␈↓␈α
are␈αlists␈α
of␈α
substitutions␈αthen␈α
␈↓↓s1␈↓␈α
@␈α␈↓↓s2␈↓␈α
is␈α
the␈α"composition"␈α
 of␈α
the␈αtwo.␈α
 Prove
␈↓ ↓H␈↓the following properties.

␈↓ ↓H␈↓        2.1.     ␈↓↓∀x y z: subst[x, y, z] = sublis[z, <y . x>]␈↓

␈↓ ↓H␈↓        2.2.     ␈↓↓∀z s1 s2: sublis[z, s1 @ s2] = sublis[sublis[z, s1], s2]␈↓

␈↓ ↓H␈↓        2.3.     ␈↓↓∀z s1 s2 s3: sublis[z, s1 @ [s2 @ s3]] = sublis[z, [s1 @ s2] @ s3]␈↓

␈↓ ↓H␈↓        2.4.     ␈↓↓∀x x1 y y1 z: ((y ≠  y1 ∧ ¬occur[y, x1]) ⊃ ␈↓
␈↓ ↓H␈↓                        ␈↓↓subst[x1, y1, subst[x, y, z]] = subst[subst[x1, y1, x], y, subst[x1, y1, z]])␈↓


␈↓ ↓H␈↓where

␈↓ ↓H␈↓        ␈↓↓subst[x, y, z] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ [␈↓αif␈↓↓ y = z ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ z] ␈↓
␈↓ ↓H␈↓92␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓                        ␈↓↓␈↓αelse␈↓↓ subst[x, y, ␈↓αa|␈↓↓z] . subst[x, y, ␈↓αd|␈↓↓z]␈↓

␈↓ ↓H␈↓        ␈↓↓sublis[x, s] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ {assoc[x, s]}[λz: ␈↓αif␈↓↓ ␈↓αn|␈↓↓z ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ ␈↓αd|␈↓↓z]␈↓
␈↓ ↓H␈↓                        ␈↓↓␈↓αelse␈↓↓ sublis[␈↓αa|␈↓↓x, s] . sublis[␈↓αd|␈↓↓x, s]␈↓

␈↓ ↓H␈↓        ␈↓↓s1 @ s2 ← subsub[s1, s2] * s2␈↓

␈↓ ↓H␈↓        ␈↓↓subsub[s1, s2] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓s1 ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [␈↓αaa|␈↓↓s1 . sublis[␈↓αda|␈↓↓s1, s2]] . subsub[␈↓αd|␈↓↓s1, s2]␈↓

␈↓ ↓H␈↓3.  Pattern matching

␈↓ ↓H␈↓        The␈α∞function␈α∞␈↓↓inst␈↓␈α∞is␈α
one␈α∞sort␈α∞of␈α∞"pattern␈α
matcher".␈α∞ We␈α∞say␈α∞that␈α
an␈α∞S-expression␈α∞␈↓↓x␈↓␈α∞is␈α
an
␈↓ ↓H␈↓instance␈αof␈αanother␈αS-expression␈α␈↓↓y␈↓␈αif␈α␈↓↓y␈↓␈αcan␈αbe␈αtransformed␈αinto␈α␈↓↓x␈↓␈αby␈αreplacing␈αsome␈αof␈αthe␈αatoms
␈↓ ↓H␈↓of␈α⊃␈↓↓y␈↓␈α⊂which␈α⊃satisfy␈α⊂␈↓↓isvar␈↓␈α⊃by␈α⊂suitable␈α⊃values.␈α⊂ (All␈α⊃occurrences␈α⊂of␈α⊃the␈α⊂same␈α⊃variable␈α⊃should␈α⊂be
␈↓ ↓H␈↓replaced␈αby␈αthe␈αsame␈αvalue.)␈α ␈↓↓inst[x, y, ␈↓¬NIL␈↓↓]␈↓␈αis␈α␈↓¬NO␈α␈↓if␈α␈↓↓x␈↓␈αis␈αnot␈αand␈αinstance␈αof␈αthe␈αpattern␈α␈↓↓y,␈↓␈αand
␈↓ ↓H␈↓otherwise␈α∩is␈α⊃the␈α∩list␈α⊃of␈α∩substitions␈α∩which␈α⊃will␈α∩convert␈α⊃␈↓↓y␈↓␈α∩into␈α∩␈↓↓x.␈↓␈α⊃ In␈α∩the␈α⊃latter␈α∩case␈α∩we␈α⊃have
␈↓ ↓H␈↓␈↓↓x = sublis[y, inst[x, y, ␈↓¬NIL␈↓↓]␈↓.  Write a definition for ␈↓↓inst␈↓ and prove that

␈↓ ↓H␈↓␈↓ βQ␈↓↓∀x y u: (inst[x, y, u] ≠ ␈↓¬NO ␈↓↓⊃ x = sublis[y, inst[x, y, u]])␈↓ 

␈↓ ↓H␈↓4. Unification

␈↓ ↓H␈↓        (This is a fairly substantial exercise.)

␈↓ ↓H␈↓        ␈↓↓unify[x,y]␈↓␈αattempts␈αto␈αfind␈αthe␈αmost␈αgeneral␈αpattern␈αwhich␈αis␈αan␈αinstance␈αof␈αboth␈α␈↓↓x␈↓␈αand␈α␈↓↓y.␈↓
␈↓ ↓H␈↓If␈α
no␈α
such␈αpattern␈α
exists␈α
the␈αit␈α
returns␈α
␈↓¬NO,␈α
␈↓otherwise␈αit␈α
returns␈α
a␈αlist␈α
of␈α
substitutions␈α
which␈αwill
␈↓ ↓H␈↓convert both ␈↓↓x␈↓ and ␈↓↓y␈↓ into that pattern.  Write a definition of ␈↓↓unify␈↓ and prove

␈↓ ↓H␈↓        4.1.     ␈↓↓∀x y: (unify[x, y] ≠ ␈↓¬NO ␈↓↓⊃ sublis[x, unify[x, y]] = sublis[y, unify[x, y]])␈↓

␈↓ ↓H␈↓        4.2.     ␈↓↓∀x y: (unify[x, y] = ␈↓¬NO ␈↓↓⊃ ∀s: sublis[x, s] ≠ sublis[y, s])␈↓

␈↓ ↓H␈↓        4.3.     ␈↓↓∀x y s: (sublis[x, s] = sublis[y, s] ⊃ ∃s1: ∀z: sublis[z, s] = sublis[z, unify[x, y] @ s1])␈↓
␈↓ ↓H␈↓␈↓ εH␈↓ *93


␈↓ ↓H␈↓α␈↓ ¬{Chapter IV

␈↓ ↓H␈↓α␈↓ ∧%LISP PROGRAMS WITH SIDE EFFECTS


␈↓ ↓H␈↓        Pure␈α∞clean␈α∂LISP␈α∞programs␈α∂such␈α∞as␈α∞we␈α∂described␈α∞in␈α∂Chapters␈α∞I␈α∞and␈α∂II␈α∞admit␈α∂the␈α∞elegant
␈↓ ↓H␈↓mathematical␈αcharacterization␈αdescribed␈αand␈αapplied␈αin␈αChapter␈αIII.␈α Specifically,␈αeach␈αrecursively
␈↓ ↓H␈↓defined␈αpure␈αclean␈αLISP␈αprogram␈αcan␈αbe␈αtranslated␈αinto␈αa␈αfunctional␈αequation␈αand␈αminimization
␈↓ ↓H␈↓schema␈α∞in␈α∞a␈α∞first␈α∞order␈α∞language,␈α∞and␈α∞the␈α∂equation␈α∞and␈α∞schema␈α∞can␈α∞be␈α∞used␈α∞to␈α∞prove␈α∂that␈α∞the
␈↓ ↓H␈↓program meets its extensional specifications.

␈↓ ↓H␈↓        LISP␈αsystems␈αalso␈αprovide␈αadditional␈αfeatures␈α
for␈αexamining␈αand␈αmodifying␈αthe␈αstate␈αof␈α
the
␈↓ ↓H␈↓LISP␈αworld.␈α
 The␈αextreme␈α
practical␈α"LISP␈α
hacker"␈αtakes␈α
the␈αfollowing␈α
point␈αof␈α
view.␈α Executing
␈↓ ↓H␈↓the␈αcode␈αcorresponding␈αto␈αa␈αLISP␈α"function"␈αcan␈αhave␈αeffects␈αthat␈αgo␈αbeyond␈αproviding␈αthe␈αvalue
␈↓ ↓H␈↓of␈α⊃the␈α⊂function␈α⊃applied␈α⊃to␈α⊂its␈α⊃arguments␈α⊂for␈α⊃further␈α⊃computation.␈α⊂ These␈α⊃effects␈α⊃may␈α⊂include
␈↓ ↓H␈↓modification␈αof␈αthe␈αLISP␈αstack,␈αthe␈αfree␈αstorage␈αmechanism,␈αthe␈αcontents␈αof␈αproperty␈αlists,␈αand␈αthe
␈↓ ↓H␈↓values␈α
of␈αvariables.␈α
 By␈αincluding␈α
"functions"␈αwhose␈α
programs␈αhave␈α
such␈αside-effects␈α
in␈α
a␈αlarger
␈↓ ↓H␈↓program␈αone␈α
can␈αoften␈αcompute␈α
answers␈αmore␈αconveniently␈α
than␈αin␈αpure␈α
LISP.␈α From␈α
this␈αpoint
␈↓ ↓H␈↓of␈α⊂view,␈α⊂the␈α⊂fact␈α⊂that␈α⊂LISP␈α⊂programs␈α⊂look␈α⊂like␈α⊂pure␈α⊂function␈α⊂definitions␈α⊂is␈α⊂only␈α⊂a␈α⊂notational
␈↓ ↓H␈↓convenience.

␈↓ ↓H␈↓        Programs␈α⊃that␈α⊂take␈α⊃totally␈α⊂uninhibited␈α⊃advantage␈α⊂of␈α⊃side-effects␈α⊂are␈α⊃usually␈α⊃difficult␈α⊂to
␈↓ ↓H␈↓understand,␈αdebug␈α
and␈αmodify.␈α This␈α
has␈αled␈αto␈α
experienced␈αprogrammers␈αto␈α
use␈αthese␈αfeatures␈α
in
␈↓ ↓H␈↓moderation.␈α∞ That␈α∞such␈α
programs␈α∞are␈α∞not␈α
accessible␈α∞to␈α∞present␈α
proof␈α∞techniques␈α∞is␈α∞of␈α
secondary
␈↓ ↓H␈↓interest␈α
to␈αmost␈α
applied␈αprogrammers.␈α
 In␈αthe␈α
future␈α
we␈αexpect␈α
that␈αlanguage␈α
designers␈αwill␈α
reduce
␈↓ ↓H␈↓the␈αneed␈αfor␈αside-effect␈αprogramming␈αby␈αproviding␈αmore␈αdirect␈αmeans␈αfor␈αachieving␈αtheir␈αresults,
␈↓ ↓H␈↓and␈α∂programmers␈α∂will␈α∂come␈α∂to␈α∂see␈α∂debugging␈α∞a␈α∂correctness␈α∂proof␈α∂as␈α∂a␈α∂more␈α∂worthwile␈α∞activity
␈↓ ↓H␈↓than␈α∞debugging␈α∞a␈α∞program␈α∞by␈α∞examples,␈α∞because␈α
it␈α∞terminates␈α∞with␈α∞a␈α∞conclusive␈α∞proof␈α∞that␈α
the
␈↓ ↓H␈↓program meets its specifications.

␈↓ ↓H␈↓        In␈α⊂this␈α∂chapter␈α⊂we␈α∂shall␈α⊂describe␈α∂some␈α⊂additional␈α∂features␈α⊂of␈α∂practical␈α⊂LISP␈α⊂systems␈α∂for
␈↓ ↓H␈↓which␈α
good␈α
simple␈α
mathematical␈α
characterizations␈α
have␈α
not␈α
yet␈α
been␈α
found.␈α
 Every␈α
computation
␈↓ ↓H␈↓that␈αcan␈αbe␈αdone␈α
with␈αthese␈αfeatures␈αcan␈α
be␈αdone␈αin␈αpure␈α
clean␈αLISP,␈αbut␈αnevertheless␈α
there␈αare
␈↓ ↓H␈↓often␈αgood␈αreasons␈αfor␈αusing␈α
these␈αfeatures.␈α All␈αthese␈αfeatures␈α
can␈αbe␈αformalized␈αin␈αterms␈αof␈α
their
␈↓ ↓H␈↓effect␈α
on␈α
the␈α"state␈α
of␈α
the␈αcomputation",␈α
and␈α
this␈αhas␈α
been␈α
done␈αby␈α
Michael␈α
Gordon␈α
[1975],␈αbut
␈↓ ↓H␈↓his␈α∀formalizations␈α∀seem␈α∀too␈α∀complicated␈α∀for␈α∀elementary␈α∀exposition.␈α∀ The␈α∀general␈α∀idea␈α∀is␈α∪to
␈↓ ↓H␈↓introduce␈α
a␈α
state␈αvariable␈α
␈↓πx␈↓␈α
and␈αto␈α
describe␈α
the␈α"execution"␈α
of␈α
expressions␈αas␈α
functions␈α
on␈αstates
␈↓ ↓H␈↓that produce new states.

␈↓ ↓H␈↓        We␈α∞shall␈α∞examine␈α∞the␈α∞features␈α∞themselves␈α
and␈α∞also␈α∞the␈α∞criteria␈α∞that␈α∞determine␈α∞when␈α
they
␈↓ ↓H␈↓should␈αbe␈αused␈αin␈αpreference␈αto␈αpure␈αclean␈αLISP␈αand␈αwe␈αshall␈αpresent␈αsome␈αideas␈αon␈αhow␈αto␈αtreat
␈↓ ↓H␈↓them theoretically.



␈↓ ↓H␈↓1.  ␈↓αSequential (ALGOL-like) programs.␈↓


␈↓ ↓H␈↓        Sequential␈α∪programs␈α∪are␈α∀impure␈α∪(by␈α∪definition),␈α∪but␈α∀can␈α∪be␈α∪clean␈α∪-␈α∀provided␈α∪certain
␈↓ ↓H␈↓restrictions␈αare␈αobserved.␈α The␈αexternal␈αnotation␈αfor␈αsequential␈αprograms␈αis␈αadapted␈αfrom␈αthat␈αof
␈↓ ↓H␈↓ALGOL 60.  We allow as a term an expression of the form
␈↓ ↓H␈↓94␈↓ ¬wChapter  IV␈↓ H


␈↓ ↓H␈↓␈↓ βx␈↓αprogram␈↓[[<variable list>] <statement list>],

␈↓ ↓H␈↓where␈α
<variable␈αlist>␈α
is␈αa␈α
list␈αof␈α
variables␈αlocal␈α
to␈αthe␈α
program,␈αand␈α
<statement␈αlist>␈α
is␈αa␈α
list␈αof␈α
the
␈↓ ↓H␈↓statements␈α∞of␈α∞the␈α
program.␈α∞ As␈α∞in␈α∞ALGOL␈α
60,␈α∞the␈α∞statements␈α
are␈α∞separated␈α∞by␈α∞semicolons,␈α
and
␈↓ ↓H␈↓any statement may be preceded by a label followed by a colon.

␈↓ ↓H␈↓        The statements are of the following kinds:

␈↓ ↓H␈↓        1. Assignment statements of the form

␈↓ ↓H␈↓␈↓ βx<left hand side> ← <right hand side>,

␈↓ ↓H␈↓where␈α⊃<left␈α⊂hand␈α⊃side>␈α⊃is␈α⊂a␈α⊃variable,␈α⊂possibly␈α⊃subscripted,␈α⊃and␈α⊂<right␈α⊃hand␈α⊂side>␈α⊃is␈α⊃a␈α⊂LISP
␈↓ ↓H␈↓expression that can be evaluated.

␈↓ ↓H␈↓        2. ␈↓αgo to␈↓ statements of the form

␈↓ ↓H␈↓␈↓ βx␈↓αgo to␈↓ <label>

␈↓ ↓H␈↓where␈α⊂<label>␈α⊂is␈α⊂ an␈α⊂expression␈α⊂that␈α⊂evaluates␈α∂to␈α⊂a␈α⊂label.␈α⊂  Since␈α⊂labels␈α⊂are␈α⊂atoms␈α⊂in␈α∂internal
␈↓ ↓H␈↓notation,␈α
any␈α
expression␈α
evaluating␈α
to␈α
an␈α
atom␈α
may␈αbe␈α
used,␈α
but␈α
the␈α
usual␈α
case␈α
is␈α
a␈αconditional
␈↓ ↓H␈↓expression␈αwherein␈α
the␈αsecond␈α
element␈αof␈α
each␈αpair␈αis␈α
an␈αatom.␈α
 Should␈αthe␈α
resulting␈αexpression
␈↓ ↓H␈↓not be an atom or should that atom not be used as a label, an error message will be generated.

␈↓ ↓H␈↓        3. Conditional statements which have the form

␈↓ ↓H␈↓␈↓ βx␈↓αif␈↓ <p1> ␈↓αthen␈↓ <s1> ␈↓αelse␈↓ ␈↓αif␈↓ ... ␈↓αelse␈↓ ␈↓αif␈↓ <pn> ␈↓αthen␈↓ <sn>,

␈↓ ↓H␈↓where␈α∞the␈α
<pi>␈α∞are␈α
propositional␈α∞terms␈α
having␈α∞truth␈α
values,␈α∞and␈α
<si>␈α∞is␈α
any␈α∞statement.␈α
  Notice
␈↓ ↓H␈↓that␈αconditional␈α
statements␈αterminate␈αwith␈α
a␈α␈↓αthen␈↓␈α
clause␈αas␈αdo␈α
conditional␈αexpressions␈α
in␈αgeneral
␈↓ ↓H␈↓in␈α
internal␈α
form␈α
If␈α
none␈αof␈α
the␈α
propositional␈α
terms␈α
are␈α
true␈αthe␈α
statement␈α
has␈α
no␈α
effect.␈α (Unless␈α
of
␈↓ ↓H␈↓course there were side effects in the evaluation of the propositional terms.)

␈↓ ↓H␈↓        4. ␈↓αreturn␈↓ statements of the form

␈↓ ↓H␈↓␈↓ βx␈↓αreturn␈↓ <expression>

␈↓ ↓H␈↓where␈α<expression>␈αis␈αan␈αarbitrary␈αLISP␈αterm.␈α The␈αeffect␈αof␈αexecuting␈αthis␈αstatement␈αis␈αto␈αreturn
␈↓ ↓H␈↓from the program giving the program as a term the value of <expression>.

␈↓ ↓H␈↓        As an example, we might write ␈↓↓reverse␈↓ as follows:

␈↓ ↓H␈↓↓␈↓ βxreverse u ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[v]
␈↓ ↓H␈↓↓␈↓ ∧xv ← ␈↓¬NIL␈↓↓;
␈↓ ↓H␈↓↓1.1)␈↓ ∧8a:␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ v;
␈↓ ↓H␈↓↓␈↓ ∧xv ← ␈↓αa|␈↓↓u . v;
␈↓ ↓H␈↓↓␈↓ ∧xu ← ␈↓αd|␈↓↓u;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ a].

␈↓ ↓H␈↓        The internal form of the same program is
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ *95



␈↓ ↓H␈↓¬␈↓ βx(DEFUN REVERSE (U)
␈↓ ↓H␈↓¬␈↓ ∧8(PROG (V)
␈↓ ↓H␈↓¬␈↓ ∧x(SETQ V NIL)
␈↓ ↓H␈↓¬␈↓ ∧xA
␈↓ ↓H␈↓¬␈↓ ∧x(COND ((NULL U) (RETURN V)))
␈↓ ↓H␈↓¬␈↓ ∧x(SETQ V (CONS (CAR U) V))
␈↓ ↓H␈↓¬␈↓ ∧x(SETQ U (CDR U))
␈↓ ↓H␈↓¬␈↓ ∧x(GO A)))

␈↓ ↓H␈↓where␈αthe␈αparagraphing␈αis␈αonly␈αfor␈αthe␈αreader's␈αconvenience.␈α Notice␈αthat␈αin␈αinternal␈αform,␈αlabels
␈↓ ↓H␈↓are statements rather than attachments to statements.

␈↓ ↓H␈↓        In general the internal form of a program term is the following:

␈↓ ↓H␈↓␈↓ ∧b(␈↓¬PROG ␈↓ <variable list> <s1> ... <sn>)

␈↓ ↓H␈↓and the four types of statements have the forms:

␈↓ ↓H␈↓␈↓ βx(␈↓¬SETQ ␈↓<variable> <expression>)
␈↓ ↓H␈↓␈↓ βx(␈↓¬GO ␈↓<label>)
␈↓ ↓H␈↓␈↓ βx(␈↓¬COND ␈↓(<p1> <s1>) ... (<pn> <sn>))
␈↓ ↓H␈↓␈↓ βx(␈↓¬RETURN ␈↓<expression>)


␈↓ ↓H␈↓In␈αaddition␈αa␈α␈↓¬PROG␈α␈↓statement␈αmay␈αbe␈αan␈αatom␈αwhich␈αis␈αinterpreted␈αas␈αa␈αlabel.␈α Thus␈α(␈↓¬GO␈α␈↓<label>)
␈↓ ↓H␈↓transfers control to the list of statements beginning with the statement following <label>.

␈↓ ↓H␈↓        In␈αmost␈αLISP␈αimplementations␈α
arrays␈αare␈αtreated␈αspecially␈α
and␈αsubscripted␈αvariables␈αare␈α
not
␈↓ ↓H␈↓allowed␈αas␈α
first␈αarguments␈αto␈α
␈↓¬SETQ.␈α␈↓␈αThe␈α
␈↓¬STORE␈α␈↓command␈αmust␈α
be␈αused␈α in␈α
order␈αto␈α
change␈αan
␈↓ ↓H␈↓array element.  We will say more about arrays in the next section.

␈↓ ↓H␈↓        As␈α
further␈α
examples␈α
of␈α
how␈α
␈↓αprogram␈↓␈α
might␈α∞be␈α
used,␈α
here␈α
are␈α
some␈α
ways␈α
we␈α∞might␈α
write
␈↓ ↓H␈↓␈↓↓append:␈↓

␈↓ ↓H␈↓1.2)␈↓ βA␈↓↓u * v ← ␈↓αprogram␈↓↓[[] ␈↓αreturn␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]]␈↓, 

␈↓ ↓H␈↓is just a trivial rewrite of the recursive definition.

␈↓ ↓H␈↓↓␈↓ βxu * v ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[w]
␈↓ ↓H␈↓↓1.3)␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ v;
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v];
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αreturn␈↓↓ w]


␈↓ ↓H␈↓is␈αalmost␈α
as␈αclose␈α
to␈αthe␈α
pure␈αLISP␈α
form.␈α If␈αwe␈α
want␈αto␈α
replace␈αthe␈α
recursion␈αby␈α
a␈αloop,␈α
we␈αcan
␈↓ ↓H␈↓write
␈↓ ↓H␈↓96␈↓ ¬wChapter  IV␈↓ H



␈↓ ↓H␈↓↓␈↓ βxu * v ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[u1,v1,w]
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓¬NIL␈↓↓;
␈↓ ↓H␈↓↓␈↓ ∧xu1 ← u;
␈↓ ↓H␈↓↓␈↓ ∧xv1 ← v;
␈↓ ↓H␈↓↓␈↓ ∧8a:␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓u1 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ b;
␈↓ ↓H␈↓↓1.4)␈↓ ∧xw ← ␈↓αa|␈↓↓u1 . w;
␈↓ ↓H␈↓↓␈↓ ∧xu1 ← ␈↓αd|␈↓↓u;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ a;
␈↓ ↓H␈↓↓␈↓ ∧8b:␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ v1;
␈↓ ↓H␈↓↓␈↓ ∧xv1 ← ␈↓αa|␈↓↓w . v1;
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓αd|␈↓↓w;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ b].


␈↓ ↓H␈↓This corresponds to the recursive program


␈↓ ↓H␈↓↓␈↓ βxu * v ← app[u,v,␈↓¬NIL␈↓↓]
␈↓ ↓H␈↓↓1.5)
␈↓ ↓H␈↓↓␈↓ βxapp[u,v,w] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ [␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ app[u,␈↓αa|␈↓↓w . v,␈↓αd|␈↓↓w]]
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ app[␈↓αd|␈↓↓u,v,␈↓αa|␈↓↓u . w]


␈↓ ↓H␈↓which can save some storage if it is compiled or interpreted without using the stack.

␈↓ ↓H␈↓        We␈αshall␈αnot␈αtreat␈αthe␈αmathematics␈αof␈αsequential␈αprograms␈αfully␈αat␈αthis␈αpoint.␈α However,␈αwe
␈↓ ↓H␈↓note␈α
that␈α
under␈α
certain␈α
conditions␈α
a␈α
sequential␈α
program␈α
that␈α
occurs␈α
as␈α
a␈α
term␈α
can␈α
be␈α
replaced␈αby␈α
a
␈↓ ↓H␈↓term␈α
involving␈α
only␈α
functional␈α
programs.␈α
 This␈α
means␈α
that␈α
the␈α
theory␈α
developed␈α
in␈α
Chapter␈α
III
␈↓ ↓H␈↓can␈α∞be␈α∞applied␈α∞to␈α∞such␈α∞programs.␈α∞ The␈α
main␈α∞property␈α∞that␈α∞a␈α∞sequential␈α∞program␈α∞must␈α∞have␈α
in
␈↓ ↓H␈↓order␈αto␈αbe␈αreplaced␈α
when␈αit␈αoccurs␈αas␈αa␈α
term␈αby␈αa␈αfunctional␈α
program␈αcomputing␈αits␈αvalue␈αis␈α
that
␈↓ ↓H␈↓the␈α⊂value␈α⊂of␈α⊃any␈α⊂expression␈α⊂containing␈α⊃the␈α⊂program␈α⊂as␈α⊂a␈α⊃term␈α⊂remains␈α⊂unchanged␈α⊃when␈α⊂the
␈↓ ↓H␈↓program␈α
is␈α
replaced␈α
by␈α
its␈α
value.␈α
 Or␈α
more␈α
briefly,␈α
the␈α
term␈α
must␈α
be␈α
without␈α
(external)␈α
side␈α
effects.
␈↓ ↓H␈↓It␈αis␈αsufficient␈αto␈αknow␈αthat␈αall␈αassignments␈αare␈αto␈αvariables␈αlocal␈αto␈αthe␈αprogram␈αWe␈αwill␈αdiscuss
␈↓ ↓H␈↓transforming␈αsequential␈αprograms␈αinto␈αfunctional␈αprograms␈αand␈αrelated␈αtopics␈αin␈αgreater␈αdetail␈αin
␈↓ ↓H␈↓Chapter VII.

␈↓ ↓H␈↓        There␈α⊂are␈α⊂three␈α⊂circumstances␈α⊂in␈α∂which␈α⊂sequential␈α⊂programs␈α⊂are␈α⊂preferred␈α⊂to␈α∂functional
␈↓ ↓H␈↓programs:

␈↓ ↓H␈↓        1.␈αIn␈αsome␈αcases␈αand␈αfor␈αsome␈αpeople,␈αit␈αis␈αeasier␈αto␈αthink␈αabout␈αhow␈αto␈αgo␈αfrom␈αthe␈αinitial
␈↓ ↓H␈↓conditions␈α
step-by-step␈α
to␈α
the␈α
final␈α
result␈α
than␈α
to␈α
think␈α
about␈α
how␈α
the␈α
final␈α
result␈α
can␈αbe␈α
obtained
␈↓ ↓H␈↓from␈α∩easier␈α∩cases.␈α∩ There␈α∩seems␈α∩to␈α∩be␈α∩a␈α∩matter␈α∩of␈α∩taste␈α∩to␈α∩a␈α∩substantial␈α∩extent,␈α∪though␈α∩the
␈↓ ↓H␈↓functional form seems to have clear conceptual advantages for functions like ␈↓↓append␈↓ and ␈↓↓subst.␈↓

␈↓ ↓H␈↓        2.␈αWhen␈αthere␈αare␈αa␈αlarge␈αnumber␈αof␈αvariables,␈αit␈αcan␈αturn␈αout␈αthat␈αthe␈αnecessary␈αfunctions
␈↓ ↓H␈↓have␈α∞an␈α∞unwieldy␈α∞number␈α∂of␈α∞arguments.␈α∞ Frequently␈α∞in␈α∞such␈α∂cases␈α∞only␈α∞a␈α∞few␈α∞of␈α∂the␈α∞variables
␈↓ ↓H␈↓actually change in any "loop" , so it is inefficient to pass them all around.
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ *97


␈↓ ↓H␈↓        3.␈α⊃When␈α∩we␈α⊃are␈α∩considering␈α⊃a␈α∩program␈α⊃that␈α∩interacts␈α⊃with␈α∩an␈α⊃environment␈α∩instead␈α⊃of
␈↓ ↓H␈↓producing an answer, the sequential form may be required, although this hasn't been proved.

␈↓ ↓H␈↓Remarks:

␈↓ ↓H␈↓        1.␈α
Although␈α
␈↓αreturn␈↓␈α
 and␈α
␈↓αgo to␈↓␈α
statements␈α
in␈α
LISP␈α
have␈α
no␈α
meaning␈α
unless␈α
they␈α
occur␈α
inside
␈↓ ↓H␈↓a␈α␈↓αprogram␈↓,␈αan␈αassignment␈α
statement␈αmay␈αbe␈αregarded␈αas␈α
an␈αprogram␈αterm.␈α Thus␈α
␈↓↓[x ← ␈↓αa|␈↓↓y] . z␈↓␈αis
␈↓ ↓H␈↓an abbreviation for ␈↓↓␈↓αprogram␈↓↓[[] x ← ␈↓αa|␈↓↓y; ␈↓αreturn␈↓↓ x] . z␈↓.

␈↓ ↓H␈↓        2.␈α
Besides␈α
␈↓¬SETQ,␈α
␈↓most␈α
LISPs␈α
allow␈α∞assignments␈α
of␈α
the␈α
form␈α
 (␈↓¬SET␈α
␈↓<exp1>␈α∞<exp2>),␈α
where
␈↓ ↓H␈↓<exp1>␈α⊂is␈α⊂evaluated␈α⊂to␈α⊂determine␈α⊂to␈α⊂what␈α⊃variable␈α⊂the␈α⊂assignment␈α⊂is␈α⊂to␈α⊂be␈α⊂made.␈α⊃ If␈α⊂<exp1>
␈↓ ↓H␈↓doesn't evaluate to a variable, an error is signalled.

␈↓ ↓H␈↓        3.␈α
In␈α
the␈α
conditional␈α
statement␈α
it␈α
is␈α
convenient␈α
to␈α
allow␈α
a␈α
list␈α
of␈α
statements␈α
as␈α
the␈α
␈↓αthen␈↓␈α
clause
␈↓ ↓H␈↓rather␈α
than␈αa␈α
single␈αstatement.␈α
 This␈α
often␈αallows␈α
a␈αmore␈α
natural␈α
organization␈αof␈α
the␈αprogram␈α
and
␈↓ ↓H␈↓causes␈αno␈αdifficulty␈αin␈αthe␈αrewriting␈αof␈αclean␈αsequential␈αprograms␈αas␈αfunctional␈αprograms.␈α In␈αfact
␈↓ ↓H␈↓LISP␈α∞systems␈α∞usually␈α∞allow␈α∂this␈α∞more␈α∞general␈α∞form␈α∞of␈α∂conditional,␈α∞thus␈α∞the␈α∞conditional␈α∂has␈α∞the
␈↓ ↓H␈↓general form

␈↓ ↓H␈↓␈↓ ∧1(␈↓¬COND ␈↓<p1 e11 ... e1k>  ... <pn en1 ... enm>)

␈↓ ↓H␈↓The value returned in the case one is wanted is the value of the last <eij> evaluated.

␈↓ ↓H␈↓        4.␈α∪In␈α∪writing␈α∪␈↓αprogram␈↓s␈α∪in␈α∪external␈α∪form␈α∩we␈α∪will␈α∪often␈α∪use␈α∪the␈α∪convention␈α∪that␈α∩each
␈↓ ↓H␈↓statement␈αis␈αwritten␈αon␈αa␈αseparate␈αline.␈α This␈αmeans␈αthat␈αthe␈α";"␈αseparator␈αin␈αnot␈αneeded␈αand␈αmay
␈↓ ↓H␈↓be omitted in this case.

␈↓ ↓H␈↓        5.␈α⊗For␈α∃additional␈α⊗discussion␈α⊗of␈α∃the␈α⊗method␈α⊗for␈α∃converting␈α⊗sequential␈α⊗programs␈α∃into
␈↓ ↓H␈↓functional␈αprograms␈αsee␈α
McCarthy␈α[1962b].␈α Algorthims␈α
for␈αdoing␈αthis␈α
in␈αthe␈αcase␈αLISP␈α
programs
␈↓ ↓H␈↓are discussed in Chapter VII as an example of programs that transform  programs.



␈↓ ↓H␈↓2.  ␈↓αArrays in LISP␈↓


␈↓ ↓H␈↓        As␈α
mentioned␈α
in␈α∞section␈α
␈↓π∞␈↓1␈α
in␈α∞addition␈α
to␈α
simple␈α
variables␈α∞which␈α
may␈α
be␈α∞assigned␈α
values
␈↓ ↓H␈↓using␈α
the␈αassignment␈α
statement,␈α
LISP␈αadmits␈α
arrays.␈α
 An␈αarray␈α
may␈α
in␈αprinciple␈α
have␈αany␈α
number
␈↓ ↓H␈↓of␈α
dimensions.␈α∞ If␈α
␈↓↓foo␈↓␈α∞is␈α
a␈α∞2-dimensional␈α
(say␈α∞3␈α
x␈α∞4)␈α
array␈α∞then␈α
␈↓↓foo[i,j]␈↓␈α∞can␈α
be␈α∞thought␈α
of␈α∞as␈α
a
␈↓ ↓H␈↓complicated␈α
variable␈α
name.␈α
  It␈α∞evaluates␈α
to␈α
the␈α
jth␈α∞element␈α
of␈α
the␈α
ith␈α
row␈α∞of␈α
␈↓↓foo␈↓␈α
and␈α
it␈α∞can␈α
be
␈↓ ↓H␈↓assigned␈α∃a␈α∃value␈α∀using␈α∃the␈α∃function␈α∃␈↓↓store.␈↓␈α∀ Thus␈α∃␈↓↓store[foo,<2,3>,␈↓¬APPLE␈↓↓]␈↓␈α∃assigns␈α∃to␈α∀row 2
␈↓ ↓H␈↓column 3␈α
of␈α
␈↓↓foo␈↓␈αthe␈α
value␈α
␈↓¬APPLE␈↓.␈α
 Recall␈αthat␈α
in␈α
external␈α
form␈αwe␈α
allowed␈α
subscripted␈αvariables␈α
in
␈↓ ↓H␈↓assignment␈αstatements.␈αThus␈α␈↓↓store[foo,<i,j>,e]␈↓␈αcan␈αalso␈αbe␈αwritten␈α␈↓↓foo[i,j] ← e␈↓.␈α (␈↓↓foo[i,j]␈↓␈α
appearing
␈↓ ↓H␈↓on␈α
the␈α
left␈α
of␈α
an␈α
assignment␈α
denotes␈α
the␈αposition␈α
in␈α
the␈α
array␈α
while␈α
in␈α
other␈α
contexts␈α
it␈αdenotes
␈↓ ↓H␈↓the␈αcontents␈αof␈α
that␈αposition.)␈α It␈α
would␈αnot␈αin␈αprinciple␈α
be␈αdifficult␈αto␈α
modify␈αthe␈α␈↓¬SETQ␈α
␈↓of␈αLISP
␈↓ ↓H␈↓to accept subscripted variables, although there may be practical reasons for not doing so.

␈↓ ↓H␈↓        Before␈α∂using␈α∂a␈α∂array,␈α∂it␈α∂must␈α∂be␈α∂declared␈α⊂to␈α∂LISP.␈α∂  ␈↓¬(ARRAY␈α∂FOO␈α∂T␈α∂3␈α∂4)␈↓␈α∂tells␈α⊂the␈α∂LISP
␈↓ ↓H␈↓system␈αthat␈αthe␈αatom␈α␈↓¬FOO␈α␈↓is␈αto␈αdenote␈αa␈α3␈αx␈α4␈αarray␈αof␈αS-expressions.␈α (The␈αthird␈αitem␈α␈↓¬T␈↓␈αspecifies
␈↓ ↓H␈↓the␈αtype␈αof␈αthe␈αarray.␈αThere␈αare␈αother␈αpossibilities␈αsuch␈αas␈α␈↓¬FIXNUM␈α␈↓or␈α␈↓¬FLONUM␈α␈↓is␈αMACLISP.␈α The
␈↓ ↓H␈↓98␈↓ ¬wChapter  IV␈↓ H


␈↓ ↓H␈↓form␈α
of␈α
the␈αarray␈α
declaration␈α
may␈α
vary␈αwith␈α
implementation.)␈α
The␈αinternal␈α
forms␈α
for␈α
the␈αabove
␈↓ ↓H␈↓expressions␈α0for␈α0accessing␈α0and␈α1storing␈α0into␈α0arrays␈α0are␈α0␈↓¬(FOO I J)␈α1␈↓␈α0and
␈↓ ↓H␈↓␈↓¬(STORE (FOO 2 3) 'APPLE).␈α⊂␈↓␈α⊂Note␈α⊃that␈α⊂accessing␈α⊂an␈α⊃array␈α⊂element␈α⊂has␈α⊃the␈α⊂same␈α⊂form␈α⊃as␈α⊂a
␈↓ ↓H␈↓function␈αcall.␈α
 LISP␈αknows␈α
that␈α␈↓¬FOO␈α
␈↓is␈αan␈αarray␈α
because␈αyou␈α
declared␈αit␈α
to␈αbe␈α
one␈αusing␈αthe␈α
␈↓¬ARRAY
␈↓ ↓H␈↓¬␈↓command.␈α
 This␈αinformation␈α
is␈αusually␈α
kept␈αon␈α
the␈αproperty␈α
list␈αof␈α
the␈αatom␈α
denoting␈α
the␈αarray.
␈↓ ↓H␈↓We␈α
will␈α
say␈α∞more␈α
about␈α
this␈α
in␈α∞section␈α
␈↓π∞␈↓3␈α
on␈α
property␈α∞lists.␈α
 For␈α
additional␈α
array␈α∞functions␈α
and
␈↓ ↓H␈↓features␈α
the␈αreader␈α
should␈αconsult␈α
the␈αprogrammers␈α
manual␈αfor␈α
the␈αversion␈α
of␈αLISP␈α
being␈αused,
␈↓ ↓H␈↓as these vary.

␈↓ ↓H␈↓        We␈α∃can␈α∃begin␈α∀to␈α∃characterize␈α∃arrays␈α∀in␈α∃LISP␈α∃by␈α∀modifying␈α∃ the␈α∃characterization␈α∀of
␈↓ ↓H␈↓assignment␈α
and␈αcontents␈α
functions␈α
on␈α state␈α
vectors␈αgiven␈α
in␈α
McCarthy␈α[1962b].␈α
  Thus␈α
we␈αhave
␈↓ ↓H␈↓functions␈α≠␈↓↓a[array,pos,exp]␈↓␈α≠and␈α≠␈↓↓c[array,pos]␈↓␈α≠where␈α≠␈↓↓a␈↓␈α≠returns␈α≠the␈α≠array␈α≠resulting␈α~from
␈↓ ↓H␈↓␈↓↓store[array,pos,exp]␈↓␈α
(in␈α
LISP␈α
␈↓↓store␈↓␈α
returns␈α
the␈α
value␈α
of␈α
␈↓↓exp␈↓)␈α
and␈α
␈↓↓c␈↓␈α
returns␈α
the␈α
contents␈α
of␈α␈↓↓array␈↓␈α
at
␈↓ ↓H␈↓␈↓↓pos␈↓ (like writing ␈↓↓array[pos]␈↓).  The functions ␈↓↓a␈↓ and ␈↓↓c␈↓ satisfy  the following relations

␈↓ ↓H␈↓2.1)␈↓ α8␈↓↓c[a[array,pos,exp],pos1] = ␈↓αif␈↓↓ pos=pos1 ␈↓αthen␈↓↓ exp ␈↓αelse␈↓↓ c[array,pos1]␈↓

␈↓ ↓H␈↓2.2)␈↓ α8␈↓↓array = a[array,pos,c[array,pos]]␈↓

␈↓ ↓H␈↓2.3)␈↓ α8␈↓↓a[a[array,pos0,exp0],pos1,exp1] =␈↓ ε8␈↓αif␈↓↓ pos0=pos1 ␈↓αthen␈↓↓ a[array,pos0,exp1]␈↓
␈↓ ↓H␈↓␈↓ ε8␈↓↓␈↓αelse␈↓↓ a[a[array,pos1,exp1],pos0,exp0]␈↓

␈↓ ↓H␈↓These␈αequations␈αcharacterize␈αthe␈αabstract␈αnotion␈αof␈αarray.␈α They␈αcan␈αbe␈αused␈αto␈αprove␈αpropertied
␈↓ ↓H␈↓of␈α⊃programs␈α⊃that␈α⊃have␈α⊃array␈α⊃type␈α⊃arguments.␈α⊃ We␈α⊃will␈α⊃see␈α⊃and␈α⊃example␈α⊃of␈α⊃such␈α⊃a␈α⊃proof␈α⊂in
␈↓ ↓H␈↓Chapter␈α	VIII.␈α	 They␈α	do␈α	not␈α	deal␈α	with␈α	the␈α	fact␈α	that␈α	the␈α	␈↓↓store␈↓␈α	function␈α	of␈α	LISP␈α	destroys␈α	the␈α	old␈α	array
␈↓ ↓H␈↓in␈α∂the␈α∂process␈α∂of␈α∂creating␈α∂the␈α∂new␈α∂version.␈α⊂ We␈α∂also␈α∂need␈α∂to␈α∂extend␈α∂the␈α∂notion␈α∂of␈α⊂equality␈α∂to
␈↓ ↓H␈↓arrays␈α
in␈α
the␈α
obvious␈α
way.␈α
 (Namely␈α
two␈α
arrays␈α
are␈α
equal␈α
if␈α
and␈α
only␈α
if␈α
the␈α
they␈α
have␈α
the␈αsame␈α
set
␈↓ ↓H␈↓of allowed positions and the contents of any allowed position is the same for both arrays.)

␈↓ ↓H␈↓Arrays␈αare␈αare␈αpractical␈αfor␈αtwo␈αreasons.␈α One␈αis␈αquick␈αaccess␈αto␈αindividual␈αelements,␈αthe␈αother␈αis
␈↓ ↓H␈↓that␈αthey␈αare␈αupdated␈αrather␈αthan␈αcopied␈αwhich␈αsaves␈αspace.␈α  One␈αcould␈αcompare␈αthe␈αuse␈αof␈αlists
␈↓ ↓H␈↓vs␈α∞arrays␈α
for␈α∞storing␈α∞data␈α
to␈α∞the␈α∞use␈α
of␈α∞tapes␈α∞vs␈α
disks.␈α∞  Lists␈α∞are␈α
somewhat␈α∞more␈α∞flexible␈α
than
␈↓ ↓H␈↓tape␈α
in␈α∞that␈α
you␈α
can␈α∞splice␈α
and␈α∞chop␈α
somewhat␈α
more␈α∞readily␈α
but␈α
the␈α∞sequential␈α
access␈α∞vs␈α
direct
␈↓ ↓H␈↓access analogy is good.



␈↓ ↓H␈↓3.  ␈↓αProperty Lists and Special Properties.␈↓


␈↓ ↓H␈↓        It is necessary to associate at least the following kinds of information with atoms:

␈↓ ↓H␈↓        1.␈α␈↓αPrint␈αnames.␈↓␈α Programs␈αthat␈αprint␈αlists␈αor␈αS-expressions␈αin␈αan␈αexternal␈αmedium␈αmust␈αbe
␈↓ ↓H␈↓able␈α
to␈α
find␈α
the␈α
string␈αof␈α
characters␈α
constituting␈α
the␈α
␈↓↓print␈α
name␈↓␈αof␈α
an␈α
atom.␈α
 Likewise,␈α
when␈αan␈α
S-
␈↓ ↓H␈↓expression␈α
is␈α
read␈αfrom␈α
an␈α
external␈α
medium,␈αthe␈α
read␈α
program␈α
must␈αbe␈α
able␈α
to␈αdetermine␈α
whether
␈↓ ↓H␈↓there␈αis␈αalready␈αan␈αatom␈αwith␈αa␈αgiven␈αprint␈αname;␈αif␈αso␈αit␈αreturns␈αa␈αpointer␈αto␈αthat␈αatom;␈αif␈αnot␈αit
␈↓ ↓H␈↓creates a new atom with the given print name.

␈↓ ↓H␈↓        2.␈α
␈↓αValues.␈↓␈α When␈α
an␈α
atom␈αdenotes␈α
a␈α
variable␈αthe␈α
LISP␈αinterpreter␈α
will␈α
need␈αto␈α
be␈α
able␈αto
␈↓ ↓H␈↓look up the value associated with the atom in order to evaluate a term containing that variable.
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ *99


␈↓ ↓H␈↓        3.␈α␈↓αFunction␈αdefinitions.␈↓␈α In␈αthe␈αcase␈αthat␈αan␈αatom␈αdenotes␈αa␈αfunction,␈αthe␈αdefinition␈αof␈αthe
␈↓ ↓H␈↓function must be associated with the atom.

␈↓ ↓H␈↓        4.␈α␈↓αMacro␈αdefinitions.␈↓␈α
 Similarly,␈αin␈αthe␈αcase␈α
that␈αan␈αatom␈αdenotes␈α
a␈αmacro,␈αthe␈αdefinition␈α
of
␈↓ ↓H␈↓the macro must be associated with the atom.

␈↓ ↓H␈↓        5.␈α⊂␈↓αArray␈α∂pointers.␈↓␈α⊂ An␈α∂atom␈α⊂may␈α∂also␈α⊂denote␈α⊂an␈α∂array␈α⊂in␈α∂LISP.␈α⊂ In␈α∂this␈α⊂case␈α⊂an␈α∂array
␈↓ ↓H␈↓pointer or similar object must be associated with the atom in order for the array to be accessed.

␈↓ ↓H␈↓        LISP␈α
provides␈αa␈α
uniform␈αmanner␈α
for␈α
associating␈αinformation␈α
with␈αatoms,␈α
namely␈αeach␈α
atom
␈↓ ↓H␈↓has␈αa␈α␈↓↓property list.␈↓␈α  LISP␈αuses␈αthe␈αproperty␈αlist␈αof␈αan␈αatom␈αto␈αstore␈αvarious␈αstandard␈αproperties.
␈↓ ↓H␈↓We␈αwill␈αdiscuss␈αsome␈αof␈αthese␈αproperties␈αbelow.␈α Besides␈αthese␈αstandard␈αuses␈αof␈αproperty␈αlists,␈αit␈αis
␈↓ ↓H␈↓convenient␈αto␈αallow␈αthe␈αprogrammer␈αto␈αassociate␈αany␈αinformation␈αhe␈αchooses␈αwith␈αan␈αatom.␈α Such
␈↓ ↓H␈↓information␈α
can␈α
be␈α∞retrieved␈α
for␈α
a␈α∞particular␈α
atom␈α
in␈α
a␈α∞time␈α
that␈α
depends␈α∞on␈α
the␈α
length␈α∞of␈α
the
␈↓ ↓H␈↓property␈αlist␈αof␈αthe␈αatom,␈αand␈αthis␈αis␈αusually␈αquite␈αshort.␈α The␈αtime␈αis␈αindependent␈αof␈αthe␈αnumber
␈↓ ↓H␈↓of atoms.

␈↓ ↓H␈↓        Property␈α⊂lists␈α∂are␈α⊂usually␈α⊂realized␈α∂as␈α⊂ordinary␈α⊂lists.␈α∂ Each␈α⊂"property"␈α⊂has␈α∂an␈α⊂atom␈α⊂as␈α∂its
␈↓ ↓H␈↓name,␈α∂and␈α∂the␈α∞name␈α∂of␈α∂the␈α∞property␈α∂is␈α∂ordinarily␈α∞followed␈α∂by␈α∂a␈α∞pointer␈α∂to␈α∂the␈α∂information␈α∞in
␈↓ ↓H␈↓question.␈α∞ Because␈α∞this␈α∞information␈α∞can␈α
be␈α∞a␈α∞string␈α∞of␈α∞characters,␈α
a␈α∞floating␈α∞point␈α∞number␈α∞or␈α
a
␈↓ ↓H␈↓pointer␈α
to␈αa␈α
subroutine,␈αthe␈α
property␈αlist␈α
is␈α
ordinarily␈αnot␈α
a␈αproper␈α
LISP␈αlist,␈α
and␈α
functions␈αthat
␈↓ ↓H␈↓operate␈αon␈αproper␈αlists␈αcan␈αcause␈αerrors␈αif␈αapplied␈αto␈αproperty␈αlists.␈α For␈αexample,␈αa␈αprogram␈αthat
␈↓ ↓H␈↓interpreted␈α
a␈α
floating␈α
point␈α
number␈α
as␈α
a␈α
pair␈α
of␈α
pointers␈α
interpret␈α
random␈α
places␈α
in␈α
memory␈αas
␈↓ ↓H␈↓list structure.

␈↓ ↓H␈↓        For␈αthis␈αreason,␈αproperty␈αlists␈αare␈αusually␈αread␈αand␈αmodified␈αby␈αspecial␈αfunctions.␈α We␈αhave
␈↓ ↓H␈↓already␈α
seen␈αtwo␈α
such␈α
functions,␈αnamely␈α
␈↓¬DEFUN␈α␈↓and␈α
␈↓¬DEFPROP␈α
␈↓which␈αcan␈α
be␈αused␈α
to␈α
put␈αfunction
␈↓ ↓H␈↓definitions on property lists.  The three basic functions on property lists are:

␈↓ ↓H␈↓␈↓ ∧8(␈↓¬GET ␈↓<atom> <property>)
␈↓ ↓H␈↓␈↓ ∧8(␈↓¬PUTPROP ␈↓<atom> <value> <property>)
␈↓ ↓H␈↓␈↓ ∧8(␈↓¬REMPROP ␈↓<atom> <property>)

␈↓ ↓H␈↓where␈α<atom>␈αshould␈αevaluate␈αto␈αan␈αatom,␈α<property>␈αmust␈αevaluate␈αto␈αan␈αatom␈αwhich␈αnames␈α
the
␈↓ ↓H␈↓property,␈α∂and␈α⊂<value>␈α∂can␈α⊂be␈α∂any␈α∂LISP␈α⊂expression␈α∂that␈α⊂can␈α∂be␈α∂evaluated.␈α⊂ [In␈α∂some␈α⊂LISPs␈α∂a
␈↓ ↓H␈↓property␈α∀list␈α∀is␈α∪also␈α∀an␈α∀accepted␈α∀value␈α∪for␈α∀<atom>.]␈α∀The␈α∀exact␈α∪form␈α∀of␈α∀these␈α∀functions␈α∪is
␈↓ ↓H␈↓implementation dependent, in particular the order of the arguments tends to vary.

␈↓ ↓H␈↓        ␈↓¬GET␈α∃␈↓returns␈α∃the␈α∃<property>␈α∃value␈α∃(if␈α∀any)␈α∃associated␈α∃with␈α∃<atom>.␈α∃ ␈↓¬PUTPROP␈α∃␈↓puts␈α∀a
␈↓ ↓H␈↓property␈αname␈α<property>␈αfollowed␈αby␈αthe␈αproperty␈αvalue␈α<value>␈αon␈αthe␈αproperty␈αlist␈αof␈α<atom>,
␈↓ ↓H␈↓and␈α∞␈↓¬REMPROP␈α∞␈↓removes␈α∞a␈α∞property.␈α∞ ␈↓¬DEFPROP␈α∂␈↓is␈α∞like␈α∞␈↓¬PUTPROP␈α∞␈↓except␈α∞that␈α∞the␈α∞arguments␈α∂are␈α∞not
␈↓ ↓H␈↓evaluated.␈α⊂ Thus␈α⊂if␈α⊂␈↓¬(PUTPROP 'C 12 'AT-WT)␈↓␈α⊂is␈α⊂executed␈α⊂then␈α⊂␈↓¬(GET 'C 'AT-WT)␈↓␈α⊃will␈α⊂return
␈↓ ↓H␈↓␈↓¬12.␈α␈↓␈αIf␈α␈↓¬(REMPROP 'C 'AT-WT)␈↓␈αis␈αexecuted␈α
then␈α␈↓¬(GET 'C 'AT-WT)␈↓␈αwill␈αreturn␈α␈↓¬NIL␈↓␈αas␈α
the␈α␈↓¬AT-WT
␈↓ ↓H␈↓¬␈↓property␈α∂is␈α∂no␈α∂longer␈α∂on␈α∂the␈α∂property␈α∂list␈α∂of␈α∂␈↓¬C.␈α∂␈↓␈α∂If␈α∂␈↓¬(DEFPROP APPLE RED COLOR)␈↓␈α∂is␈α∂executed
␈↓ ↓H␈↓then␈α∩␈↓¬(GET 'APPLE 'COLOR)␈↓␈α∩will␈α∩return␈α∩␈↓¬RED.␈α∩␈↓␈α∩Thus␈α∩␈↓¬DEFPROP␈α∩␈↓can␈α∩be␈α∩used␈α∩to␈α∩put␈α∩arbitrary
␈↓ ↓H␈↓properties␈α⊃on␈α⊃property␈α∩lists.␈α⊃ The␈α⊃difference␈α⊃between␈α∩␈↓¬DEFPROP␈α⊃and␈α⊃␈↓␈↓¬PUTPROP␈α⊃␈↓is␈α∩that␈α⊃␈↓¬DEFPROP
␈↓ ↓H␈↓¬␈↓does not evaluate its arguments, but ␈↓¬PUTPROP ␈↓does.

␈↓ ↓H␈↓        Most␈αLISPs␈αallow␈αyou␈αto␈αexamine␈αthe␈αproperty␈αlist␈αof␈αan␈αatom.␈α It␈αis␈αtypically␈αstored␈αas␈αthe
␈↓ ↓H␈↓␈↓↓cdr␈↓␈α∞of␈α∞the␈α∞atom␈α∞so␈α∞(␈↓¬CDR␈α∞␈↓<atom>)␈α∞returns␈α∂the␈α∞property␈α∞list␈α∞of␈α∞<atom>.␈α∞ In␈α∞MACLISP␈α∞there␈α∂is␈α∞a
␈↓ ↓H␈↓100␈↓ ¬wChapter  IV␈↓ H


␈↓ ↓H␈↓special␈αfunction␈α␈↓¬PLIST␈α␈↓for␈αdoing␈αthis.␈α Thus␈α(␈↓¬PLIST␈α␈↓<atom>)␈αreturns␈αthe␈αproperty␈αlist␈αof␈α<atom>.
␈↓ ↓H␈↓In␈α
fact␈α
the␈α
normal␈α
mode␈α
of␈α
MACLISP␈α
is␈α
such␈α
that␈α
evaluating␈α
␈↓↓cdr␈↓␈α
of␈α
an␈α
atom␈α
(other␈α
than␈α␈↓¬NIL␈↓)
␈↓ ↓H␈↓will signal an error.

␈↓ ↓H␈↓        From␈α∀a␈α∃mathematical␈α∀point␈α∃of␈α∀view␈α∀␈↓¬PUTPROP␈α∃␈↓acts␈α∀like␈α∃an␈α∀assignment␈α∃statement␈α∀and
␈↓ ↓H␈↓␈↓¬REMPROP␈α∩␈↓like␈α∩a␈α∩special␈α∩kind␈α∩of␈α∩an␈α∩assignment␈α∩statement.␈α∩ ␈↓¬GET␈α∩␈↓gets␈α∩the␈α∩value␈α∩of␈α∩a␈α⊃variable.
␈↓ ↓H␈↓However,␈α∀the␈α∀mathematical␈α∀properties␈α∀of␈α∀these␈α∀LISP␈α∀operations␈α∀haven't␈α∃been␈α∀systematically
␈↓ ↓H␈↓studied.

␈↓ ↓H␈↓        The␈α
particular␈α
collection␈α
of␈α
standard␈α
properties␈α
stored␈α
by␈α
LISP␈α
on␈α
the␈α
property␈α
list␈α∞of␈α
an
␈↓ ↓H␈↓atom␈α⊃depends␈α∩on␈α⊃the␈α∩implementation.␈α⊃ Some␈α⊃LISPs␈α∩store␈α⊃␈↓¬VALUE␈α∩␈↓and␈α⊃␈↓¬PNAME␈α∩␈↓properties␈α⊃there,
␈↓ ↓H␈↓others␈α∞(MACLISP␈α
in␈α∞particular)␈α
store␈α∞these␈α
properties␈α∞in␈α
some␈α∞"hidden"␈α
place.␈α∞  Properties␈α
that
␈↓ ↓H␈↓are␈α∩kept␈α∪on␈α∩the␈α∩property␈α∪list␈α∩by␈α∩most␈α∪LISPs␈α∩include␈α∩␈↓¬EXPR,␈α∪␈↓␈↓¬FEXPR,␈α∩␈↓␈↓¬LEXPR,␈α∪␈↓␈↓¬SUBR,␈α∩␈↓␈↓¬FSUBR,
␈↓ ↓H␈↓¬␈↓␈↓¬LSUBR, ␈↓ ␈↓¬MACRO ␈↓and ␈↓¬ARRAY. ␈↓ We these system properties below.

␈↓ ↓H␈↓        An␈α∞atom␈α∞with␈α∞an␈α∞␈↓¬EXPR␈α∞␈↓property␈α∞is␈α∞assumed␈α∞by␈α∞LISP␈α∞to␈α∞denote␈α∞an␈α∞interpretable␈α∞function
␈↓ ↓H␈↓and␈αthe␈αvalue␈αof␈αthe␈α␈↓¬EXPR␈α␈↓property␈αshould␈αbe␈αa␈αfunction␈αexpression␈α(such␈αas␈αa␈αfunction␈αname,␈αa
␈↓ ↓H␈↓λ-expression,␈αor␈αa␈αlabel-expression)␈αsuitable␈αfor␈αbeing␈αinterpreted.␈α It␈αcan␈αbe␈αset␈αusing␈αone␈αof␈αthe
␈↓ ↓H␈↓operations␈α␈↓¬DEFUN␈α␈↓or␈α␈↓¬DEFPROP␈α␈↓␈αdescribed␈αin␈αChapter␈αI.␈α An␈αatom␈αwith␈αa␈α␈↓¬SUBR␈α␈↓property␈αdenotes␈αa
␈↓ ↓H␈↓compiled␈α∀function␈α∀which␈α∀may␈α∀be␈α∀directly␈α∀executed.␈α∪ The␈α∀value␈α∀of␈α∀the␈α∀␈↓¬SUBR␈α∀␈↓property␈α∀is␈α∪a
␈↓ ↓H␈↓subroutine␈α
pointer␈α
which␈α
points␈αto␈α
the␈α
compiled␈α
program.␈α Atoms␈α
denoting␈α
built␈α
in␈αfunctions␈α
such
␈↓ ↓H␈↓as␈α␈↓¬REVERSE␈α␈↓or␈α␈↓¬APPEND␈α␈↓have␈αa␈α␈↓¬SUBR␈α␈↓property␈αautomatically.␈α When␈αa␈αcompiled␈αfunction␈αis␈αloaded
␈↓ ↓H␈↓into␈αLISP␈αthe␈αname␈αof␈αthat␈αfunction␈αgets␈αa␈α␈↓¬SUBR␈α␈↓property.␈α The␈αproperties␈α␈↓¬FEXPR␈α␈↓and␈α␈↓¬LEXPR␈αare
␈↓ ↓H␈↓¬␈↓similar␈α∂to␈α∞␈↓¬EXPR␈α∂␈↓except␈α∞that␈α∂the␈α∞arguments␈α∂to␈α∞the␈α∂function␈α∞are␈α∂treated␈α∞differently.␈α∂ In␈α∂the␈α∞␈↓¬EXPR
␈↓ ↓H␈↓¬␈↓case␈α⊃a␈α⊃fixed␈α⊃number␈α⊃of␈α⊃arguments␈α⊃are␈α⊃expected␈α⊃and␈α⊃they␈α⊃are␈α⊃evaluated␈α⊃before␈α⊃applying␈α⊂the
␈↓ ↓H␈↓function.␈α∂ In␈α∂the␈α∂␈↓¬FEXPR␈α∂␈↓case␈α∂the␈α∂list␈α∂of␈α∂arguments␈α∂(eg.␈α∂the␈α∂d-part␈α∂of␈α∂the␈α∂expression)␈α⊂is␈α∂passed
␈↓ ↓H␈↓unevaluated␈α∂to␈α∞the␈α∂function␈α∞as␈α∂a␈α∞single␈α∂entity.␈α∞ In␈α∂the␈α∞␈↓¬LEXPR␈α∂␈↓case␈α∞the␈α∂number␈α∞of␈α∂arguments␈α∞is
␈↓ ↓H␈↓arbitrary,␈α∀ the␈α∀arguments␈α∀are␈α∀evaluated␈α∀and␈α∀saved␈α∀(typically␈α∀on␈α∀the␈α∀stack),␈α∀the␈α∀number␈α∪of
␈↓ ↓H␈↓arguments␈α∂is␈α∂passed␈α∂to␈α∂the␈α∂function␈α∂and␈α⊂the␈α∂function␈α∂must␈α∂use␈α∂a␈α∂special␈α∂auxiliary␈α⊂function␈α∂to
␈↓ ↓H␈↓access␈α∂the␈α∂argument␈α∂values.␈α∂ The␈α∂three␈α∂␈↓¬SUBR␈α∂␈↓cases␈α∞are␈α∂analogous␈α∂to␈α∂those␈α∂for␈α∂␈↓¬EXPR.␈α∂␈↓␈α∂We␈α∞can
␈↓ ↓H␈↓express␈α∞the␈α∞difference␈α
between␈α∞␈↓¬DEFPROP␈α∞␈↓and␈α
␈↓¬PUTPROP␈α∞␈↓explained␈α∞above␈α
by␈α∞saying␈α∞that␈α
␈↓¬DEFPROP
␈↓ ↓H␈↓¬␈↓is an ␈↓¬FSUBR ␈↓and ␈↓¬PUTPROP ␈↓is a ␈↓¬SUBR. ␈↓

␈↓ ↓H␈↓        If␈α⊃an␈α⊃atom␈α⊃has␈α⊃a␈α⊃␈↓¬MACRO␈α⊃␈↓property␈α⊃then␈α⊃that␈α⊃property␈α⊃should␈α⊃be␈α⊃a␈α⊃ function-expression
␈↓ ↓H␈↓taking␈α
one␈α
argument.␈α
 When␈α
such␈α
an␈α
atom␈α∞is␈α
encountered␈α
as␈α
the␈α
a-part␈α
of␈α
an␈α∞expression␈α
being
␈↓ ↓H␈↓evaluated,␈α∂the␈α∂corresponding␈α∞macro␈α∂definition␈α∂is␈α∞applied␈α∂to␈α∂the␈α∞entire␈α∂expression,␈α∂as␈α∂the␈α∞single
␈↓ ↓H␈↓argument,␈αand␈αthe␈αvalue␈αis␈αa␈αnew␈αexpression␈αthat␈αis␈αevaluated␈αin␈αplace␈αof␈αthe␈αoriginal␈α
one.␈α The
␈↓ ↓H␈↓␈↓¬MACRO␈α⊂␈↓property␈α⊂can␈α⊂be␈α⊂set␈α⊂in␈α⊂MACLISP␈α⊂using␈α⊂␈↓¬DEFUN␈α⊂␈↓(in␈α⊂other␈α⊂implementations␈α⊂the␈α∂function
␈↓ ↓H␈↓definition␈α⊂facility␈α⊂is␈α⊂not␈α⊂so␈α⊂versatile).␈α⊂ ␈↓¬DEFPROP␈α⊂␈↓or␈α⊂␈↓¬PUTPROP␈α⊂␈↓will␈α⊂work␈α⊂more␈α⊂generally.␈α⊃ As␈α⊂an
␈↓ ↓H␈↓example␈α∞suppose␈α
we␈α∞wished␈α
that␈α∞the␈α
form␈α∞␈↓¬(MKPAIRS␈α
e1␈α∞e2␈α
...␈α∞e2n-1␈α
e2n)␈↓␈α∞would␈α∞evaluate␈α
to
␈↓ ↓H␈↓a list of pairs ␈↓¬((e1 . e2) ... (e2n-1 . e2n))␈↓ .  This could be done by defining the macro

␈↓ ↓H␈↓¬␈↓ βλ  (DEFUN MACRO MKPAIRS (L)
␈↓ ↓H␈↓¬␈↓ βλ         (COND ((NULL (CDR L)) NIL)
␈↓ ↓H␈↓¬␈↓ βλ               (T (LIST 'CONS
␈↓ ↓H␈↓¬␈↓ βλ                        (LIST 'CONS (CADR L) (CADDR L))
␈↓ ↓H␈↓¬␈↓ βλ                        (CONS 'MKPAIRS (CDDDR L))))))

␈↓ ↓H␈↓Then␈α∂evaluating␈α⊂␈↓¬(MKPAIRS␈α∂1␈α⊂2␈α∂3␈α⊂4␈α∂5␈α⊂6)␈↓␈α∂would␈α∂produce␈α⊂␈↓¬((1␈α∂.␈α⊂2)␈α∂(3␈α⊂.␈α∂4)␈α⊂(5␈α∂.␈α⊂6))␈↓.␈α∂ One
␈↓ ↓H␈↓reason␈α
for␈α
using␈α
macro␈α
definitions␈α
is␈α
that␈α∞compilers␈α
will␈α
treat␈α
them␈α
in␈α
a␈α
special␈α
way.␈α∞ Namely␈α
a
␈↓ ↓H␈↓compiler␈α∂will␈α∂expand␈α∂the␈α∂definition␈α∂and␈α∂compile␈α∂the␈α∂resulting␈α∂code.␈α∂ This␈α∂eliminates␈α⊂the␈α∂extra
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ ≠101


␈↓ ↓H␈↓level␈αof␈α
evaluation.␈α It␈α
can␈αbe␈α
used␈αvery␈α
effectively␈αwhen␈α
writing␈αprograms␈α
that␈αmanipulate␈α
highly
␈↓ ↓H␈↓structured␈α⊂data␈α⊃represented␈α⊂as␈α⊂lists␈α⊃or␈α⊂S-expressions.␈α⊃ Here␈α⊂one␈α⊂can␈α⊃give␈α⊂mnemonic␈α⊃names␈α⊂to
␈↓ ↓H␈↓strings␈α
of␈α␈↓αa␈↓'s␈α
and␈α
␈↓αd␈↓'s␈αthat␈α
select␈α
components␈αand␈α
write␈α
corresponding␈αmacro␈α
definitions␈α
for␈αthese
␈↓ ↓H␈↓selector␈α∞names.␈α∂ Using␈α∞these␈α∂names␈α∞may␈α∂help␈α∞you␈α∂remember␈α∞what␈α∂a␈α∞particular␈α∂piece␈α∞of␈α∂code␈α∞is
␈↓ ↓H␈↓doing,␈αand␈αthe␈αextra␈αlevel␈αof␈αevaluation␈αwill␈αdisappear␈αwhen␈αthe␈αcode␈αis␈αcompiled,␈αthus␈αrestoring
␈↓ ↓H␈↓the␈α⊂efficiency.␈α⊂  Higher␈α⊂level␈α⊂constructs␈α⊂such␈α⊂as␈α⊂␈↓¬FOR␈α⊂␈↓loops␈α⊂or␈α⊂␈↓¬WHILE␈α⊂␈↓loops␈α⊂or␈α⊂␈↓¬CASE␈α⊂␈↓statements
␈↓ ↓H␈↓could␈αalso␈αbe␈αdefined␈αin␈αthis␈αmanner.␈α Conceptually␈αhaving␈αa␈αmacro␈αproperty␈αsays␈αthis␈αexpression
␈↓ ↓H␈↓is␈αshorthand␈αfor␈αa␈αmore␈αcomplicated␈αexpression␈αwhich␈αis␈αthe␈αone␈αthat␈αis␈αintended␈αto␈αbe␈α
evaluated.
␈↓ ↓H␈↓Expanding␈αthe␈αmacro␈αproduces␈αthis␈αmore␈αcomplicated␈αexpression.␈α People␈αuse␈αmacros␈αall␈αthe␈αtime
␈↓ ↓H␈↓in expressing algorithms and carrying out semi-formal arguments.

␈↓ ↓H␈↓        An␈αatom␈αwith␈αan␈α␈↓¬ARRAY␈α␈↓property␈αdenotes␈αan␈αarray.␈α The␈αvalue␈αof␈αthe␈α␈↓¬ARRAY␈α␈↓property␈αis␈αan
␈↓ ↓H␈↓array␈αpointer␈αwhich␈αprovides␈αLISP␈αwith␈αa␈αmeans␈αof␈αaccessing␈αthe␈αarray.␈α The␈αarray␈αproperty␈αcan
␈↓ ↓H␈↓be␈α
set␈α
by␈α∞executing␈α
a␈α
statement␈α∞of␈α
the␈α
form␈α∞(␈↓¬ARRAY␈α
␈↓<name>␈α
<type>␈α∞<b1>␈α
...␈α
<bn>).␈α∞ This␈α
causes
␈↓ ↓H␈↓LISP␈αto␈αallocate␈αspace␈αfor␈αan␈αn-dimensional␈αarray␈αof␈αtype␈αgiven␈αby␈α<type>␈αhaving␈αbounds␈αgiven
␈↓ ↓H␈↓by␈α∞the␈α∞<bi>'s␈α∂ and␈α∞to␈α∞set␈α∂the␈α∞␈↓¬ARRAY␈α∞␈↓property␈α∂of␈α∞<name>.␈α∞  The␈α∂value␈α∞of␈α∞the␈α∂assigned␈α∞property
␈↓ ↓H␈↓includes type, bounds, and location information.  (See section ␈↓π∞␈↓2 for more details about arrays.)

␈↓ ↓H␈↓        A␈α∩major␈α∩use␈α∩of␈α⊃property␈α∩lists␈α∩in␈α∩programming␈α∩is␈α⊃in␈α∩the␈α∩creation␈α∩and␈α∩modification␈α⊃of
␈↓ ↓H␈↓networks␈α∂of␈α∂data.␈α∂ Values␈α∂attached␈α∂to␈α∂atoms␈α∂point␈α∂to␈α∂expressions␈α∂that␈α∂include␈α∂other␈α⊂atoms.␈α∂ A
␈↓ ↓H␈↓program␈α
manipulating␈α
chemical␈α
structures␈α
might␈α
use␈α
property␈α
lists␈α
to␈α
associate␈α
such␈α
information␈α
as
␈↓ ↓H␈↓valence,␈αatomic␈αweight,␈αnuclear␈αspin,␈αetc.␈α with␈αeach␈αchemical␈αatom␈αof␈αinterest.␈α It␈αcould␈αalso␈αtreat
␈↓ ↓H␈↓fragments␈αas␈αquasi␈αatoms␈αby␈αnaming␈αthem,␈αputting␈αthe␈αstructure␈αof␈αthe␈αfragment␈αon␈αthe␈αproperty
␈↓ ↓H␈↓list,␈α∩as␈α∩well␈α∩as␈α∩analogues␈α∩to␈α∩other␈α∩atomic␈α∩properties.␈α∩ These␈α∩properties␈α∩can␈α∩then␈α∩be␈α∩used␈α⊃as
␈↓ ↓H␈↓required␈α⊃by␈α⊃programs␈α⊃for␈α⊃building␈α⊃or␈α⊃analysing␈α⊃complex␈α⊃molecules.␈α⊃ Another␈α⊃example␈α∩is␈α⊃the
␈↓ ↓H␈↓program␈αused␈αto␈αprint␈αthe␈αLISP␈αprograms␈αappearing␈αin␈αthis␈αbook␈αin␈αexternal␈αform.␈α Each␈αspecial
␈↓ ↓H␈↓LISP␈α∂atom␈α∂(such␈α∂as␈α∞␈↓¬CAR,␈α∂␈↓␈↓¬COND,␈α∂␈↓␈↓¬APPEND,␈α∂␈↓␈↓¬QUOTE,␈α∞␈↓...)␈α∂has␈α∂on␈α∂its␈α∞property␈α∂list␈α∂the␈α∂name␈α∂of␈α∞the
␈↓ ↓H␈↓function␈α∞used␈α∞to␈α∞compile␈α∞the␈α∞construct␈α∞it␈α
signals,␈α∞type␈α∞information,␈α∞external␈α∞print␈α∞name␈α∞(so␈α
that
␈↓ ↓H␈↓␈↓¬CAR␈α␈↓prints␈αas␈α␈↓αa␈↓␈αfor␈αexample)␈αand␈αother␈αcontrol␈αinformation.␈α This␈αis␈αan␈αexample␈αof␈α"data␈αdriven"
␈↓ ↓H␈↓programming.␈α
 Another␈α
example␈α
of␈α
a␈α
data␈α
driven␈αprogram␈α
would␈α
be␈α
a␈α
compiler␈α
that␈α
looked␈αup
␈↓ ↓H␈↓the␈α⊂function␈α⊂to␈α∂compile␈α⊂special␈α⊂constructs␈α⊂on␈α∂the␈α⊂␈↓¬COMPFN␈α⊂␈↓property␈α∂of␈α⊂the␈α⊂atom␈α⊂signalling␈α∂that
␈↓ ↓H␈↓construct.␈α∪ Such␈α∪a␈α∪compiler␈α∪is␈α∪quite␈α∪flexible␈α∪and␈α∪allows␈α∪you␈α∪to␈α∪extend␈α∪the␈α∪language␈α∪easily.
␈↓ ↓H␈↓[Diffie???]

␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.␈α Write␈αa␈αprogram␈α␈↓↓saveup␈↓␈αthat␈αtakes␈αa␈αlist␈α
of␈αatoms␈αand␈αa␈αlist␈αof␈αproperties␈αand␈αcollects␈αfor␈α
each
␈↓ ↓H␈↓atom and association list of property/value pairs.  ␈↓↓saveup␈↓ returns a list of atom/alist pairs.

␈↓ ↓H␈↓2.␈α⊃ Write␈α⊃a␈α⊃program␈α⊃␈↓↓removem␈↓␈α⊃that␈α⊃takes␈α⊃the␈α⊃same␈α⊃arguments␈α⊃as␈α⊃␈↓↓saveup␈↓␈α⊃and␈α⊃removes␈α⊃all␈α⊂the
␈↓ ↓H␈↓properties from the property lists of all the atoms.

␈↓ ↓H␈↓3.␈α Write␈αa␈αprogram␈α␈↓↓restorem␈↓␈αthat␈αtakes␈αa␈αlist␈αof␈αthe␈αform␈αoutput␈αby␈α␈↓↓saveup␈↓␈αand␈αputs␈αback␈αall␈αof
␈↓ ↓H␈↓the properties on the property lists of all the atoms.

␈↓ ↓H␈↓The␈α∞above␈α∞collection␈α∞of␈α∞functions␈α∞are␈α∂useful␈α∞for␈α∞switching␈α∞environments␈α∞in␈α∞a␈α∞system␈α∂written␈α∞in
␈↓ ↓H␈↓LISP.
␈↓ ↓H␈↓102␈↓ ¬wChapter  IV␈↓ H


␈↓ ↓H␈↓4.  ␈↓αManipulating atomic symbols.␈↓


␈↓ ↓H␈↓        Frequently␈α
when␈α
writing␈α
LISP␈α
programs␈α
it␈α
is␈α
useful␈α
to␈α
be␈α
able␈α
to␈α
create␈α
new␈αatomic␈α
symbols.
␈↓ ↓H␈↓For␈α∞example␈α∞a␈α∞program␈α∞to␈α∞compile␈α∞LISP␈α∞into␈α∞a␈α∞symbolic␈α∞machine␈α∞language␈α∞will␈α∞need␈α∂to␈α∞create
␈↓ ↓H␈↓labels␈α
to␈α
use␈α
a␈α
symbolic␈α
addresses.␈α
 One␈α
possibility␈αis␈α
to␈α
keep␈α
around␈α
a␈α
list␈α
of␈α
available␈αsymbols
␈↓ ↓H␈↓and␈α
when␈α
one␈α
is␈α∞needed␈α
take␈α
it␈α
from␈α
the␈α∞list.␈α
 This␈α
takes␈α
up␈α
space␈α∞and␈α
the␈α
list␈α
may␈α
run␈α∞out␈α
at
␈↓ ↓H␈↓inconvenient␈αtimes.␈α Also␈αwe␈α
may␈αwish␈αto␈αmake␈α
a␈αname␈αwhich␈αhas␈α
some␈αrelation␈αto␈αanother␈α
name.
␈↓ ↓H␈↓For␈αexample,␈αa␈αprogram␈αthat␈αtakes␈αa␈αLISP␈αfunction␈αdefinition␈αand␈αreturns␈αan␈αalternate␈αdefinition
␈↓ ↓H␈↓which␈αhas␈αno␈α
␈↓αprogram␈↓␈αterms␈αwill␈α
need␈αa␈αname␈αfor␈α
this␈αnewly␈αdefined␈α
function␈αand␈αyou␈αmay␈α
wish
␈↓ ↓H␈↓to␈αform␈α
the␈αnew␈αname␈α
by␈αadding␈α
a␈αsuitable␈αsuffix␈α
to␈αthe␈α
name␈αof␈αthe␈α
original␈αprogram.␈α
 In␈αthis
␈↓ ↓H␈↓case␈αthe␈αgeneral␈αlist␈αwould␈αnot␈αdo␈αand␈αyou␈αwould␈αhave␈αto␈αtell␈αthe␈αprogram␈αexplicitly␈αfor␈αeach␈α
case
␈↓ ↓H␈↓what␈αthe␈αnew␈αname␈αshould␈αbe.␈α This␈αstill␈αdoes␈αnot␈αsolve␈αthe␈αproblem␈αtotally␈αas␈αthe␈α
program␈αmay
␈↓ ↓H␈↓generate␈αauxiliary␈αdefinitions␈αand␈αyou␈αmay␈αnot␈αwish␈αto␈αfigure␈αout␈αin␈αadvance␈αhow␈αmany␈αor␈αwhat
␈↓ ↓H␈↓to␈αcall␈αthem.␈α There␈αare␈αmany␈αother␈αexamples␈αin␈αsymbolic␈αcomptation␈αwhen␈αit␈αis␈αuseful␈αto␈αinvent
␈↓ ↓H␈↓names for various purposes.

␈↓ ↓H␈↓        There␈α∞are␈α∞really␈α∞two␈α∞issues␈α
involved,␈α∞one␈α∞is␈α∞the␈α∞ability␈α
to␈α∞create␈α∞atoms␈α∞and␈α∞the␈α∞other␈α
the
␈↓ ↓H␈↓ability␈αto␈α
manipulate␈αpnames.␈α
 The␈αtwo␈α
are␈αstrongly␈α
connected.␈α Since␈α
every␈αatom␈α
has␈αa␈αpname␈α
we
␈↓ ↓H␈↓cannot␈α∞create␈α∞an␈α∞atom␈α∞without␈α∞giving␈α∞it␈α∞a␈α∞pname.␈α∞ LISP␈α∞provides␈α∞several␈α∞primitives␈α∞which␈α
are
␈↓ ↓H␈↓useful␈α∞for␈α∞creating␈α∞atoms␈α∞and/or␈α∞manipulating␈α∞the␈α∞pnames.␈α∞ We␈α∞will␈α∞discuss␈α∞a␈α∞few␈α∞of␈α∞the␈α∞basic
␈↓ ↓H␈↓ones␈α
here.␈α You␈α
should␈α
consult␈αthe␈α
manual␈αfor␈α
your␈α
favorite␈αLISP␈α
to␈αfind␈α
out␈α
more,␈αas␈α
this␈αsort␈α
of
␈↓ ↓H␈↓feature tends to be quite implementation dependent.

␈↓ ↓H␈↓        We␈α∞begin␈α
with␈α∞␈↓↓gemsym.␈↓␈α
  Evaluating␈α∞␈↓¬(GENSYM)␈↓␈α∞is␈α
like␈α∞asking␈α
LISP␈α∞to␈α
make␈α∞a␈α∞new␈α
atom
␈↓ ↓H␈↓and␈α
saying␈αyou␈α
don't␈αcare␈α
what␈αit␈α
is␈αcalled.␈α
 LISP␈αdoes␈α
indeed␈αcreate␈α
a␈αnew␈α
atom␈αand␈α
gives␈α
it␈αa
␈↓ ↓H␈↓pname␈α⊂of␈α∂the␈α⊂form␈α∂␈↓¬Gdddd␈α⊂␈↓where␈α∂each␈α⊂␈↓¬d␈α⊂␈↓is␈α∂a␈α⊂decimal␈α∂digit␈α⊂and␈α∂each␈α⊂successive␈α⊂evaluation␈α∂of
␈↓ ↓H␈↓␈↓¬(GENSYM)␈α∞␈↓returns␈α∞an␈α∞atom␈α∞whose␈α∞pname␈α∞corresponds␈α∞to␈α∞the␈α∞next␈α∞larger␈α∞decimal␈α∞number.␈α∞If␈α
we
␈↓ ↓H␈↓type␈α⊃␈↓¬(GENSYM)␈α⊃␈↓to␈α⊃LISP␈α⊃and␈α⊃it␈α⊃types␈α∩back␈α⊃␈↓¬GOO69␈α⊃␈↓then␈α⊃typing␈α⊃␈↓¬(GENSYM)␈α⊃␈↓again␈α⊃will␈α∩result␈α⊃in
␈↓ ↓H␈↓␈↓¬G0070␈α␈↓being␈α
typed␈αback.␈α
 (No␈αprediction␈α
is␈αmade␈α
as␈αto␈α
what␈αwill␈α
happen␈αwhen␈α
the␈αsymbol␈α
reaches
␈↓ ↓H␈↓␈↓¬G9999␈↓).␈α Thus,␈αexcept␈αin␈αthe␈αunlikely␈αevent␈αthat␈αthe␈αuser␈αhas␈αuse␈αsuch␈αa␈αname␈αelsewhere,␈αthe␈αnew
␈↓ ↓H␈↓atoms␈α
will␈α
also␈α∞have␈α
new␈α
names.␈α∞ (LISP␈α
implementations␈α
usually␈α∞provide␈α
the␈α
user␈α∞a␈α
mechanism
␈↓ ↓H␈↓for␈αresetting␈αthe␈α␈↓¬GENSYM␈α␈↓count␈αand␈αprefix␈α
character(s).)␈α ␈↓↓gensym␈↓␈αdoes␈αnot␈αcorrespond␈αto␈αa␈α
function
␈↓ ↓H␈↓in␈αthe␈αtraditional␈αmathematical␈αsense,␈αas␈αit␈αreturns␈αa␈αdifferent␈αvalue␈αeach␈αtime␈αit␈αis␈αevaluated.␈α In
␈↓ ↓H␈↓particular␈α␈↓↓gensym[]≠gensym[].␈↓␈α (Try␈αtyping␈α␈↓¬(EQ␈α(GENSYM)␈α(GENSYM))␈↓␈αto␈αLISP.)␈α␈↓↓gensym␈↓␈αis␈αuseful
␈↓ ↓H␈↓in␈α
places␈α
where␈αyou␈α
need␈α
a␈αlabel,␈α
but␈α
don't␈α
care␈αwhat␈α
it␈α
is␈αcalled.␈α
 The␈α
compiler␈α
labels␈αdenoting
␈↓ ↓H␈↓symbolic␈αaddresses␈αmentioned␈αabove␈αis␈αone␈αexample.␈α ␈↓↓gensym␈↓␈αhas␈αother␈αpeculiarities␈αhaving␈αto␈αdo
␈↓ ↓H␈↓with␈αthe␈αway␈αLISP␈αkeeps␈αtrack␈αof␈αthe␈αatoms␈αit␈αknows␈αby␈αname.␈α Atoms␈αcreated␈αby␈α␈↓↓gensym␈↓␈αare␈αnot
␈↓ ↓H␈↓among␈α
those␈α
known␈α
by␈α∞name,␈α
thus␈α
the␈α
only␈α
way␈α∞you␈α
have␈α
of␈α
refering␈α
to␈α∞to␈α
it␈α
is␈α
by␈α∞the␈α
pointer
␈↓ ↓H␈↓returned when it was created.

␈↓ ↓H␈↓        Next␈αwe␈αhave␈αa␈αpair␈αof␈αfunctions␈αfor␈αmanipulating␈αpnames.␈α ␈↓↓explode␈↓␈αmaps␈αatoms␈αto␈αlists␈αof
␈↓ ↓H␈↓characters␈α∂(atoms␈α∂with␈α∂single␈α∞character␈α∂pnames),␈α∂namely␈α∂the␈α∞list␈α∂of␈α∂characters␈α∂that␈α∂makeup␈α∞the
␈↓ ↓H␈↓pname␈αof␈α
the␈αatom.␈α
 ␈↓↓implode␈↓␈αmaps␈α
lists␈αof␈αcharacters␈α
to␈αatoms,␈α
namely␈αthe␈α
atom␈αwhose␈α
pname␈αis
␈↓ ↓H␈↓the␈α≠concatenation␈α~of␈α≠the␈α~characters␈α≠in␈α~the␈α≠list.␈α~ Thus␈α≠␈↓↓explode[ABC]␈↓␈α~is␈α≠␈↓¬(A B C)␈↓␈α~ and
␈↓ ↓H␈↓␈↓↓implode[␈↓¬(A B C)␈↓↓]␈↓␈αis␈αthe␈αatom␈α␈↓¬ABC␈↓.␈α ␈↓↓implode␈↓␈αis␈αmore␈αlike␈αa␈αfunction␈αthan␈α␈↓↓gensym␈↓␈αin␈αthat␈α␈↓↓impod␈↓ing
␈↓ ↓H␈↓the␈α
same␈α
list␈α
of␈α
characters␈α
twice␈α
always␈α
returns␈αthe␈α
same␈α
answer.␈α
 There␈α
is␈α
a␈α
variant␈α
of␈α␈↓↓implode␈↓
␈↓ ↓H␈↓called␈α
␈↓↓maknam␈↓␈α
which␈αbehaves␈α
like␈α
␈↓↓gensym␈↓␈α
except␈αthat␈α
you␈α
specify␈α
the␈αpname␈α
that␈α
the␈α
new␈αatom␈α
is
␈↓ ↓H␈↓to␈αget.␈α Like␈α␈↓↓gensym␈↓ed␈α
atoms␈αthe␈αresult␈αof␈αa␈α
␈↓↓maknam␈↓␈αwill␈αnot␈αbe␈αknown␈α
to␈αLISP␈αby␈αname,␈αonly␈α
by
␈↓ ↓H␈↓the pointer returned when the atom is created.
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ ≠103


␈↓ ↓H␈↓        We will explain more about ␈↓↓gensym␈↓ and friend when we discuss "oblists" in Chapter V.



␈↓ ↓H␈↓5.  ␈↓αPseudo-functions that modify list structures.␈↓


␈↓ ↓H␈↓        In␈α∂pure␈α∂LISP,␈α⊂list␈α∂structure␈α∂is␈α⊂never␈α∂changed.␈α∂ ␈↓↓car␈↓␈α⊂and␈α∂␈↓↓cdr␈↓␈α∂merely␈α⊂move␈α∂about␈α∂in␈α⊂a␈α∂list
␈↓ ↓H␈↓structure,␈αand␈α␈↓↓cons␈↓␈αcreates␈αnew␈αlist␈αstructure␈α
from␈αthe␈αfree␈αstorage␈αlist.␈α Of␈αcourse,␈αa␈α
variable␈αcan
␈↓ ↓H␈↓get␈αa␈αnew␈α
value␈αthat␈αcorresponds,␈αfor␈α
instance,␈αto␈αputting␈αan␈α
element␈αon␈αthe␈αend␈α
of␈αa␈αlist,␈α
but␈αin
␈↓ ↓H␈↓pure␈α∞LISP␈α
this␈α∞can␈α
only␈α∞be␈α
accomplished␈α∞by␈α∞creating␈α
new␈α∞list␈α
structure.␈α∞ In␈α
this␈α∞section␈α∞we␈α
will
␈↓ ↓H␈↓discuss␈αoperations␈αthat␈αactually␈αmodify␈αlist␈αstructure.␈α They␈αoften␈αhave␈αefficiency␈α
advantages,␈αbut
␈↓ ↓H␈↓their␈α∩use␈α∩interferes␈α∩with␈α∩saving␈α∩memory␈α⊃by␈α∩using␈α∩merging␈α∩list␈α∩structure,␈α∩and␈α∩techniques␈α⊃for
␈↓ ↓H␈↓proving correctness of such programs are not well developed.

␈↓ ↓H␈↓        The␈α∀simplest␈α∀way␈α∀to␈α∀express␈α∀operations␈α∀that␈α∀modify␈α∀list␈α∀structure␈α∀is␈α∀in␈α∀the␈α∀form␈α∪of
␈↓ ↓H␈↓assignments␈α∀to␈α∀␈↓↓car-cdr␈↓␈α∀chains␈α∀of␈α∀variables,␈α∀e.g.␈α∀we␈α∀may␈α∀write␈α∀␈↓↓␈↓αada|␈↓↓x ← y . z␈↓.␈α∀ The␈α∀effect␈α∀of
␈↓ ↓H␈↓executing␈α
this␈α
statement␈α
is␈α
to␈α
replace␈α
the␈α
␈↓αada|␈↓␈α
part␈α
of␈α
the␈α
list␈α
structure␈α
pointed␈α
to␈α
by␈α
␈↓↓x␈↓␈α∞with␈α
the
␈↓ ↓H␈↓value␈α⊂of␈α∂the␈α⊂right␈α∂hand␈α⊂side.␈α∂ As␈α⊂a␈α∂term,␈α⊂the␈α∂statement␈α⊂takes␈α∂the␈α⊂value␈α∂assigned.␈α⊂ In␈α∂internal
␈↓ ↓H␈↓notation,␈α
we␈αuse␈α
the␈αpseudo-functions␈α
␈↓¬(RPLACA X Y)␈↓␈αand␈α
␈↓¬(RPLACD X Y)␈↓␈αwhich␈α
correspond␈αto␈α
the
␈↓ ↓H␈↓statements ␈↓↓␈↓αa|␈↓↓x ← y␈↓ and ␈↓↓␈↓αd|␈↓↓x ← y␈↓ respectively.

␈↓ ↓H␈↓        ␈↓¬RPLACA␈α␈↓and␈α␈↓¬RPLACD␈α
␈↓are␈αhighly␈αunclean.␈α The␈α
effect␈αof␈αevaluating␈αan␈α
expression␈αinvolving
␈↓ ↓H␈↓␈↓¬RPLACA␈α∞␈↓or␈α∞␈↓¬RPLACD␈α∂␈↓␈α∞depends␈α∞on␈α∂the␈α∞state␈α∞of␈α∂list␈α∞structure␈α∞and␈α∂not␈α∞merely␈α∞on␈α∂the␈α∞S-expressions
␈↓ ↓H␈↓that␈α
the␈α
variables␈α∞have␈α
as␈α
values.␈α
 Suppose,␈α∞for␈α
example,␈α
that␈α
the␈α∞variables␈α
␈↓¬X␈α
␈↓and␈α
␈↓¬Y␈α∞␈↓each␈α
have
␈↓ ↓H␈↓the␈α∞value␈α
␈↓¬(A B),␈α∞␈↓and␈α
consider␈α∞the␈α
effect␈α∞of␈α
␈↓¬(SETQ Z (RPLACA (CDR X) C))␈↓.␈α∞ ␈↓¬Z␈α
␈↓gets␈α∞the␈α
value
␈↓ ↓H␈↓␈↓¬(C),␈α⊂␈↓␈α∂and␈α⊂␈↓¬X␈α⊂␈↓gets␈α∂the␈α⊂value␈α⊂␈↓¬(A C),␈α∂␈↓but␈α⊂the␈α∂new␈α⊂value␈α⊂of␈α∂␈↓¬Y␈α⊂␈↓depends␈α⊂on␈α∂whether␈α⊂its␈α⊂copy␈α∂of
␈↓ ↓H␈↓␈↓¬(A B)␈α
␈↓is␈αthe␈α
same␈α
list␈αstructure␈α
as␈α
␈↓¬X␈↓'s␈αcopy.␈α
 If␈αyes,␈α
the␈α
new␈αvalue␈α
of␈α
␈↓¬Y␈α␈↓is␈α
also␈α
␈↓¬(A C);␈α␈↓otherwise
␈↓ ↓H␈↓its value remains ␈↓¬(A B). ␈↓

␈↓ ↓H␈↓        The␈α∞uncleanliness␈α
of␈α∞␈↓¬RPLACA␈α
␈↓and␈α∞␈↓¬RPLACD␈α∞␈↓means␈α
that␈α∞the␈α
programmer␈α∞must␈α∞know␈α
exactly
␈↓ ↓H␈↓what list structures merge.  A typical application is the pseudo-function ␈↓↓nconc␈↓ defined by


␈↓ ↓H␈↓↓␈↓ βxnconc[u,v] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[w]
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αn|␈↓↓u ← ␈↓αreturn␈↓↓ v;
␈↓ ↓H␈↓↓␈↓ ∧xw ← u;
␈↓ ↓H␈↓↓5.1)␈↓ ∧8loop:
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓w ␈↓αthen␈↓↓ [␈↓αd|␈↓↓w ← v; ␈↓αreturn␈↓↓ u];
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓αd|␈↓↓w;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ loop].


␈↓ ↓H␈↓␈↓↓nconc␈↓ can also be given a definition that has the form of a recursive program, namely
␈↓ ↓H␈↓104␈↓ ¬wChapter  IV␈↓ H



␈↓ ↓H␈↓↓␈↓ βxnconc[u, v] ← nconc1[u, u, v]
␈↓ ↓H␈↓↓5.2)
␈↓ ↓H␈↓↓␈↓ βxnconc1[u, w, v] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ v
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓w ␈↓αthen␈↓↓ [λz: u][rplacd[w, v]]
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ nconc1[u, ␈↓αd|␈↓↓w, v].


␈↓ ↓H␈↓        The␈α∂value␈α∂of␈α∞␈↓↓nconc[u, v]␈↓␈α∂is␈α∂just␈α∞␈↓↓u * v,␈↓␈α∂but␈α∂it␈α∞achieves␈α∂this␈α∂result␈α∞by␈α∂modifying␈α∂the␈α∞last
␈↓ ↓H␈↓element␈α
of␈α
␈↓↓u␈↓␈α
to␈α
point␈α
to␈α␈↓↓v.␈↓␈α
 It␈α
is␈α
typically␈α
used␈α
when␈αno␈α
other␈α
variable␈α
points␈α
to␈α
a␈α
list␈αstructure
␈↓ ↓H␈↓that␈α⊃merges␈α⊃into␈α∩␈↓↓u,␈↓␈α⊃and␈α⊃the␈α∩old␈α⊃value␈α⊃of␈α∩␈↓↓u␈↓␈α⊃will␈α⊃not␈α∩be␈α⊃used␈α⊃again.␈α∩ In␈α⊃that␈α⊃case,␈α∩␈↓↓nconc␈↓␈α⊃is
␈↓ ↓H␈↓advantageous,␈α∂because␈α∂it␈α⊂does␈α∂no␈α∂␈↓↓cons␈↓es,␈α⊂and␈α∂thus␈α∂can't␈α⊂initiate␈α∂garbage␈α∂collection.␈α⊂ No␈α∂formal
␈↓ ↓H␈↓methods exist at present for proving that these conditions are met.

␈↓ ↓H␈↓        ␈↓¬RPLACD␈α⊃␈↓can␈α⊃also␈α∩be␈α⊃used␈α⊃to␈α⊃insert␈α∩an␈α⊃element␈α⊃into␈α⊃the␈α∩middle␈α⊃of␈α⊃a␈α⊃list.␈α∩ Suppose,␈α⊃for
␈↓ ↓H␈↓example,␈αthat␈αwe␈αwish␈αto␈αinsert␈αthe␈αatom␈α␈↓¬B␈α␈↓after␈αevery␈αoccurrence␈αof␈αthe␈αatom␈α␈↓¬A␈α␈↓in␈αa␈αlist␈α␈↓↓u.␈↓␈α We
␈↓ ↓H␈↓can␈αdefine␈αa␈αpure␈αLISP␈αfunction␈αthat␈αgives␈αa␈αlist␈αobtained␈αfrom␈αthe␈αlist␈α␈↓↓u␈↓␈αby␈αinserting␈αsuch␈α␈↓¬B␈↓'s␈αas
␈↓ ↓H␈↓follows:

␈↓ ↓H␈↓5.3)␈↓ ↓n␈↓↓    insertb u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬A ␈↓↓␈↓αthen␈↓↓ ␈↓¬A ␈↓↓. [␈↓¬B ␈↓↓. insertb ␈↓αd|␈↓↓u] ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . insertb ␈↓αd|␈↓↓u.␈↓ 

␈↓ ↓H␈↓Computing␈α␈↓↓insertb u␈↓␈αdoes␈αnot␈αchange␈αthe␈αvalue␈αof␈α␈↓↓u,␈↓␈αbecause␈αnew␈αlist␈αstructure␈αis␈αmanufactured.
␈↓ ↓H␈↓On the other hand, if we define


␈↓ ↓H␈↓↓␈↓ βxinsertb u ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[w];
␈↓ ↓H␈↓↓␈↓ ∧xw ← u;
␈↓ ↓H␈↓↓5.4)␈↓ ∧8loop:
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ u;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αa|␈↓↓w = ␈↓¬A ␈↓↓␈↓αthen␈↓↓ [␈↓αd|␈↓↓w ← ␈↓¬B ␈↓↓. ␈↓αd|␈↓↓w; w ← ␈↓αd|␈↓↓w];
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓αd|␈↓↓w;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ loop],


␈↓ ↓H␈↓we␈αget␈α
a␈αprogram␈α
that␈αdoes␈α
the␈αactual␈α
insertions.␈α It␈αis␈α
faster,␈αbecause␈α
it␈αuses␈α
fewer␈α␈↓↓cons␈↓es,␈α
but␈αit
␈↓ ↓H␈↓will␈α⊗damage␈α↔any␈α⊗list␈α⊗structure␈α↔that␈α⊗merges␈α⊗with␈α↔the␈α⊗structure␈α⊗representing␈α↔␈↓↓u,␈↓␈α⊗and␈α↔it␈α⊗is
␈↓ ↓H␈↓mathematically recalcitrant.

␈↓ ↓H␈↓        As␈α
a␈α
final␈αexample␈α
we␈α
have␈αthe␈α
function␈α
␈↓↓prune␈↓␈αthat␈α
destructively␈α
removes␈αfrom␈α
the␈α
list␈α␈↓↓u␈↓
␈↓ ↓H␈↓any elements that are members of the list ␈↓↓seen.␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ ≠105




␈↓ ↓H␈↓↓␈↓ βxprune[u,seen]
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[v]
␈↓ ↓H␈↓↓␈↓ ∧xu ← ␈↓¬NIL␈↓↓ . u;
␈↓ ↓H␈↓↓␈↓ ∧xv ← u;
␈↓ ↓H␈↓↓␈↓ ∧8ploop:
␈↓ ↓H␈↓↓5.5)␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓v ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓αd|␈↓↓u;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ [␈↓αad|␈↓↓v ε seen] ␈↓αthen␈↓↓ [␈↓αd|␈↓↓v ← ␈↓αdd|␈↓↓v; ␈↓αgo to␈↓↓  ploop];
␈↓ ↓H␈↓↓␈↓ ∧xv ← ␈↓αd|␈↓↓v;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ ploop]

␈↓ ↓H␈↓In␈αorder␈αto␈αmake␈α
the␈αremovals␈αgo␈αuniformly␈α
we␈αbegin␈αby␈αtacking␈α
a␈αdummy␈αelement␈α␈↓¬NIL␈↓␈α
onto␈α␈↓↓u.␈↓
␈↓ ↓H␈↓Thus␈α∞the␈α∂list␈α∞is␈α∂non-empty␈α∞and␈α∂the␈α∞next␈α∂element␈α∞of␈α∂interest␈α∞is␈α∂the␈α∞"second"␈α∂element␈α∞of␈α∂the␈α∞list
␈↓ ↓H␈↓currently pointed to.



␈↓ ↓H␈↓6.  ␈↓αRe-entrant List Structure.␈↓


␈↓ ↓H␈↓        So␈α
far␈αwe␈α
have␈α
only␈αconsidered␈α
list␈α
structure␈αin␈α
which␈α
all␈α␈↓↓car-cdr␈↓␈α
chains␈α
terminate␈αin␈α
atoms.
␈↓ ↓H␈↓Mathematically,␈αinfinite␈αlist␈αstructures␈αare␈αalso␈αpossible.␈α They␈αcan't␈αbe␈αrepresented␈αin␈αa␈αcomputer,
␈↓ ↓H␈↓but␈α⊃since␈α⊃they␈α⊃can␈α⊃satisfy␈α⊃the␈α⊃LISP␈α∩algebraic␈α⊃axioms,␈α⊃we␈α⊃have␈α⊃had␈α⊃to␈α⊃exclude␈α⊃them␈α∩in␈α⊃our
␈↓ ↓H␈↓axiomatization␈αof␈α
LISP␈αby␈αaxiom␈α
schemata␈αof␈α
induction.␈α Another␈αpossibility␈α
is␈αthe␈αre-entrant␈α
list
␈↓ ↓H␈↓structure.␈α∂ These␈α∂can␈α∂be␈α∂created␈α∂by␈α∂the␈α∂␈↓¬RPLACA␈α∂␈↓and␈α∂␈↓¬RPLACD␈α∂␈↓operations.␈α∂ Figure␈α∂9␈α∂shows␈α∂some
␈↓ ↓H␈↓examples␈α∞of␈α
re-entrant␈α∞list␈α
structures.␈α∞ For␈α∞example,␈α
if␈α∞the␈α
variable␈α∞␈↓↓x␈↓␈α
has␈α∞value␈α∞␈↓¬(A),␈α
␈↓executing
␈↓ ↓H␈↓the␈α
statement␈α
␈↓↓␈↓αa|␈↓↓x␈α
←␈α
x␈↓␈α
gives␈α
rise␈α
to␈α
a␈α
circular␈α
structure␈α
(i)␈α
while␈α
if␈α
we␈α
execute␈α
␈↓↓␈↓αd|␈↓↓x␈α
←␈α
x␈↓␈α
we␈α
get␈αthe
␈↓ ↓H␈↓structure␈α(ii).␈α If␈αwe␈αexecute␈α␈↓↓␈↓αd|␈↓↓x␈α←␈αx␈↓␈αwith␈α␈↓↓x␈↓␈αas␈αin␈α(i)␈αor␈α␈↓↓␈↓αa|␈↓↓x␈α←␈αx␈↓␈αwith␈α␈↓↓x␈↓␈αas␈αin␈α(ii)␈αwe␈αget␈αthe␈αdoubly
␈↓ ↓H␈↓re-entrant␈αstructure␈α(iii).␈α  Notice␈αthat␈αin␈α(i)␈αwe␈αhave␈α␈↓↓x ␈↓αeq␈↓↓ ␈↓αa|␈↓↓x␈↓,␈αin␈α(ii)␈α␈↓↓x ␈↓αeq␈↓↓ ␈↓αd|␈↓↓x␈↓␈αand␈αin␈α(iii)␈αwe␈αhave
␈↓ ↓H␈↓␈↓↓x ␈↓αeq␈↓↓ ␈↓αa|␈↓↓x␈↓ and ␈↓↓x ␈↓αeq␈↓↓ ␈↓αd|␈↓↓x␈↓.  If the statements

␈↓ ↓H␈↓␈↓ β␈↓↓foo ← ␈↓¬(LAMBDA (X) (COND ((ATOM X) X) (T (FOO (CAR X))) ))␈↓↓␈↓
␈↓ ↓H␈↓followed by
␈↓ ↓H␈↓␈↓ ¬ ␈↓↓␈↓αa|␈↓↓␈↓αad|␈↓↓␈↓αadd|␈↓↓␈↓αadd|␈↓↓foo ← foo       ␈↓

␈↓ ↓H␈↓are␈α≥executed␈α≥then␈α≥␈↓↓foo␈↓␈α≥will␈α≥have␈α≥the␈α≥structure␈α≥shown␈α≥in␈α≥Figure␈α≥10.␈α≥ Observe␈α≥that
␈↓ ↓H␈↓␈↓↓apply[foo <x>] = left[x]␈↓ where

␈↓ ↓H␈↓6.1) ␈↓ ∧l␈↓↓left x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ left ␈↓αa|␈↓↓x␈↓. 

␈↓ ↓H␈↓If␈α∞the␈α∂reader␈α∞wishes␈α∞to␈α∂construct␈α∞re-entrant␈α∞list␈α∂structures␈α∞in␈α∞LISP,␈α∂this␈α∞had␈α∞best␈α∂be␈α∞done␈α∂in␈α∞a
␈↓ ↓H␈↓context␈αwhere␈αthe␈αresult␈αis␈αnot␈αprinted␈α(e.g.␈αreturned␈αas␈αa␈αvalue)␈αsince␈αif␈αLISP␈αattempts␈αto␈αprint␈αa
␈↓ ↓H␈↓re-entrant␈αlist␈αstructure␈αit␈αwill␈αnever␈αfinish.␈α One␈αway␈αto␈αget␈αaround␈αthis␈αis␈αto␈αdo␈αthe␈αconstruction
␈↓ ↓H␈↓inside a ␈↓¬PROG. ␈↓
␈↓ ↓H␈↓106␈↓ ¬wChapter  IV␈↓ H


␈↓"
␈↓"␈↓ ↓H␈↓        ⊂αααπααα⊃                   ⊂αααπααα⊃                       ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓  ααααα→~   ~   ~             ααααα→~   ~   ~              αααααααα→~   ~   ~
␈↓"␈↓ ↓H␈↓    ↑   %απα∀απα$               ↑   %απα∀απα$                ↑  ↑   %απα∀απα$
␈↓"␈↓ ↓H␈↓    ~     ~   ↓                 ~     ↓   ~                  ~  ~     ~   ~
␈↓"␈↓ ↓H␈↓    %ααααα$  NIHα0              ~     A   ~                  ~  %ααααα$   ~
␈↓"␈↓ ↓H␈↓                                %ααααααααα$                  %αααααααααααα$
␈↓"
␈↓"
␈↓"␈↓ ↓H␈↓    (RPLACA X X)                (RPLACD X X)             (RPLACA (RPLACDA X X))
␈↓"
␈↓"␈↓ ↓H␈↓         (i)                        (ii)                          (iii)
␈↓"
␈↓ ↓H␈↓␈↓ ∧∂␈↓αFigure 9.␈↓  Examples of re-entrant list structures.


␈↓"
␈↓"␈↓ ↓H␈↓    ⊂αααπααα⊃   ⊂αααπααα⊃   ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ααα→~   ~   εαα→~   ~   εαα→~   ~   εαα→NIL
␈↓"␈↓ ↓H␈↓ ↑  %απα∀ααα$   %απα∀ααα$   %απα∀ααα$
␈↓"␈↓ ↓H␈↓ ~    ↓           ↓           ~   
␈↓"␈↓ ↓H␈↓ ~ LAMBDA       ⊂αααπααα⊃     ~  
␈↓"␈↓ ↓H␈↓ ~              ~   ~   ~     ~ 
␈↓"␈↓ ↓H␈↓ ~              %απα∀απα$     ~ 
␈↓"␈↓ ↓H␈↓ ~                ↓   ↓       ~ 
␈↓"␈↓ ↓H␈↓ ~                X  NIL      ~ 
␈↓"␈↓ ↓H␈↓ ~                            ~
␈↓"␈↓ ↓H␈↓ ~           ⊂αααααααααααααααα$ 
␈↓"␈↓ ↓H␈↓ ~           ↓
␈↓"␈↓ ↓H␈↓ ~         ⊂αααπααα⊃   ⊂αααπααα⊃   ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~         ~   ~   εαα→~   ~   εαα→~   ~   εαα→NIL
␈↓"␈↓ ↓H␈↓ ~         %απα∀ααα$   %απα∀ααα$   %απα∀ααα$
␈↓"␈↓ ↓H␈↓ ~           ↓           ~           ↓
␈↓"␈↓ ↓H␈↓ ~         COND          ~         ⊂αααπααα⊃   ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~                       ~         ~   ~   εαα→~   ~   εαα→NIL
␈↓"␈↓ ↓H␈↓ ~                       ~         %απα∀ααα$   %απα∀ααα$
␈↓"␈↓ ↓H␈↓ ~                       ~           ↓           ↓
␈↓"␈↓ ↓H␈↓ ~                       ~           T         ⊂αααπααα⊃   ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~                       ↓                     ~   ~   εαα→~   ~   εαα→NIL
␈↓"␈↓ ↓H␈↓ ~                     ⊂αααπααα⊃   ⊂αααπααα⊃   %απα∀ααα$   %απα∀ααα$
␈↓"␈↓ ↓H␈↓ ~                     ~   ~   εαα→~   ~   ~     ~           ↓
␈↓"␈↓ ↓H␈↓ ~                     %απα∀ααα$   %απα∀απα$     ~         ⊂αααπααα⊃   ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~                       ~           ↓   ↓       ~         ~   ~   εαα→~   ~   ~
␈↓"␈↓ ↓H␈↓ ~                       ~           X  NIL      ~         %απα∀ααα$   %απα∀απα$
␈↓"␈↓ ↓H␈↓ ~                       ↓                       ~           ↓           ↓   ↓ 
␈↓"␈↓ ↓H␈↓ ~                     ⊂αααπααα⊃   ⊂αααπααα⊃     ~          CAR          X  NIL
␈↓"␈↓ ↓H␈↓ ~                     ~   ~   εαα→~   ~   ~     ~
␈↓"␈↓ ↓H␈↓ ~                     %απα∀ααα$   %απα∀απα$     ~
␈↓"␈↓ ↓H␈↓ ~                       ↓           ↓   ↓       ~
␈↓"␈↓ ↓H␈↓ ~                      ATOM         X  NIL      ~
␈↓"␈↓ ↓H␈↓ ~                                               ~
␈↓"␈↓ ↓H␈↓ ~                                               ~
␈↓"␈↓ ↓H␈↓ %ααααααααααααααααααααααααααααααααααααααααααααααα$ 

␈↓ ↓H␈↓␈↓ αP␈↓↓␈↓¬(SETQ FOO '(LAMBDA (X) (COND ((ATOM X) X) (T (FOO (CAR X))) )))␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧⊂␈↓↓␈↓¬(RPLACA (CADR (CADDR (CADDR FOO))) FOO)␈↓↓␈↓

␈↓ ↓H␈↓␈↓ ∧)␈↓αFigure 10.␈↓  Another re-entrant list structure.
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ ≠107


␈↓ ↓H␈↓        As␈α
we␈αhave␈α
just␈αnoted,␈α
re-entrant␈αlist␈α
structures␈αdo␈α
not␈αsatisfy␈α
the␈αinduction␈α
axioms␈αgiven
␈↓ ↓H␈↓for␈α
S-expressions,␈α
and␈α
the␈α
correctness␈α
of␈α
most␈α
of␈α
programs␈α
given␈α
in␈α
this␈α
book␈α
depends␈α
on␈αthe␈α
data
␈↓ ↓H␈↓being␈α
non-re-entrant.␈α
 Re-entrant␈α
structures␈αcan␈α
be␈α
represented␈α
by␈α
S-expressions␈αprovided␈α
certain
␈↓ ↓H␈↓atoms␈α⊃are␈α⊂reserved␈α⊃for␈α⊃use␈α⊂as␈α⊃labels␈α⊃and␈α⊂a␈α⊃suitable␈α⊃convention␈α⊂is␈α⊃adopted␈α⊃for␈α⊂distinguishing
␈↓ ↓H␈↓ordinary␈αatoms␈αfrom␈αlabels␈αand␈αpointers␈αto␈αthe␈αlabels.␈α For␈αexample,␈αthe␈αstructure␈αof␈αfigure␈α9␈α(iii)
␈↓ ↓H␈↓might␈α∞be␈α∞represented␈α∂by␈α∞␈↓¬((LABEL A).((POINT A).(POINT A)))␈↓.␈α∞ Programs␈α∞that␈α∂compute␈α∞with
␈↓ ↓H␈↓re-entrant␈α
structures␈α
need␈αto␈α
keep␈α
lists␈αof␈α
vertices␈α
they␈α
have␈αvisited␈α
so␈α
that␈αthey␈α
can␈α
tell␈αwhen␈α
they
␈↓ ↓H␈↓are␈α∩about␈α∩to␈α∩re-enter.␈α∩ Thus␈α⊃the␈α∩equivalence␈α∩of␈α∩merging␈α∩list␈α⊃structure␈α∩can␈α∩be␈α∩tested␈α∩by␈α⊃the
␈↓ ↓H␈↓predicate ␈↓↓equiv␈↓ defined by:


␈↓ ↓H␈↓↓␈↓ β8equiv[x, y] ← ¬[equiv1[x, y, ␈↓¬NIL␈↓↓] = ␈↓¬LOSE␈↓↓]

␈↓ ↓H␈↓↓␈↓ β8equiv1[x, y, u] ←
␈↓ ↓H␈↓↓␈↓ βx␈↓αif␈↓↓ u = ␈↓¬LOSE␈↓↓ ␈↓αthen␈↓↓ ␈↓¬LOSE␈↓↓
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ x = y ∨ match[x, y, u] ␈↓αthen␈↓↓ u
␈↓ ↓H␈↓↓6.2)␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ∨ ␈↓αat|␈↓↓y ∨ unmatch[x, y, u] ␈↓αthen␈↓↓ ␈↓¬LOSE␈↓↓
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ equiv1[␈↓αa|␈↓↓x, ␈↓αa|␈↓↓y, equiv1[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y, [x . y] . u]]

␈↓ ↓H␈↓↓␈↓ β8match[x, y, u] ← ¬␈↓αn|␈↓↓u ∧ [[x = ␈↓αaa|␈↓↓u ∧ y = ␈↓αda|␈↓↓u] ∨ match[x, y, ␈↓αd|␈↓↓u]]

␈↓ ↓H␈↓↓␈↓ β8unmatch[x, y, u] ← ¬␈↓αn|␈↓↓u ∧ [x = ␈↓αaa|␈↓↓u ∨ y = ␈↓αda|␈↓↓u ∨ unmatch[x, y, ␈↓αd|␈↓↓u]]

␈↓ ↓H␈↓The␈αargument␈α␈↓↓u␈↓␈αin␈αequiv1␈α
is␈αa␈αlist␈αof␈αpositions␈α
pairs␈αseen␈αso␈αfar␈αin␈α
the␈αparallel␈αtraversal␈αof␈α␈↓↓x␈↓␈α
and
␈↓ ↓H␈↓␈↓↓y.␈↓␈α It␈αis␈αnecessary␈αto␈αcarry␈αaround␈αinformation␈αof␈αthis␈αsort␈αin␈αorder␈αto␈αavoid␈αrepeating␈αsome␈αpart
␈↓ ↓H␈↓of the computation forever.


␈↓ ↓H␈↓α␈↓ ε⊂Exercise

␈↓ ↓H␈↓1.  Consider the following definition of the fibonnaci function.

␈↓ ↓H␈↓␈↓ α8␈↓↓        fibon n ← ␈↓αif␈↓↓ n = ␈↓¬0 ␈↓↓∨ n = ␈↓¬1 ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ fibloop[n, ␈↓¬(1 1)␈↓↓]␈↓
␈↓ ↓H␈↓6.3)
␈↓ ↓H␈↓␈↓ α8␈↓↓        fibloop[n, l] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓            ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αdd|␈↓↓l ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                [␈↓αif␈↓↓ n = ␈↓¬2 ␈↓↓␈↓αthen␈↓↓ ␈↓αad|␈↓↓rplacd[␈↓αd|␈↓↓l, <␈↓αa|␈↓↓l + ␈↓αad|␈↓↓l>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αelse␈↓↓ fibloop[sub1 n, rplacd[␈↓αd|␈↓↓l, <␈↓αa|␈↓↓l + ␈↓αad|␈↓↓l>]]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓            ␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = ␈↓¬2 ␈↓↓␈↓αthen␈↓↓ ␈↓αadd|␈↓↓l ␈↓αelse␈↓↓ fibloop[n-␈↓¬1, ␈↓↓␈↓αd|␈↓↓l]␈↓

␈↓ ↓H␈↓After␈αevaluating␈α␈↓↓fibon␈α5␈↓␈α if␈αwe␈αexamine␈αthe␈αdefinition␈αon␈αthe␈αproperty␈αlist␈αof␈α␈↓↓fibon␈↓␈α it␈α
corresponds
␈↓ ↓H␈↓to the definition

␈↓ ↓H␈↓␈↓ β␈↓↓fibon n ← ␈↓αif␈↓↓ n = ␈↓¬0 ␈↓↓∨ n = ␈↓¬1 ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ fibloop[n, ␈↓¬(1 1 2 3 5 8)␈↓↓]␈↓ . 

␈↓ ↓H␈↓Thus ␈↓↓fibon␈↓ seems to be getting smarter.  What has happened?
␈↓ ↓H␈↓108␈↓ ¬wChapter  IV␈↓ H


␈↓ ↓H␈↓7.  ␈↓αExercises.␈↓


␈↓ ↓H␈↓α␈↓ ¬=Sequential programs

␈↓ ↓H␈↓1.␈α
 Extend␈α∞␈↓↓eval␈↓␈α
(I.13.1)␈α
to␈α∞handle␈α
␈↓¬PROG,␈α
␈↓␈↓¬SETQ,␈α∞␈↓␈↓¬GO,␈α
␈↓and␈α
␈↓¬RETURN.␈α∞␈↓␈α
Is␈α
it␈α∞necessary␈α
to␈α∞change␈α
the
␈↓ ↓H␈↓evaluation␈α
of␈α
␈↓¬COND␈↓s?␈α  There␈α
are␈α
several␈αpossible␈α
ways␈α
of␈αhandling␈α
labels.␈α
 Try␈αto␈α
think␈α
of␈αat␈α
least
␈↓ ↓H␈↓two and consider the advantages and disadvantages of each.

␈↓ ↓H␈↓α␈↓ ∧UProperty list and Special properties.

␈↓ ↓H␈↓2.␈α∂ The␈α∂association-list␈α∞argument␈α∂of␈α∂␈↓↓eval␈↓␈α∂I.13.1␈α∞provides␈α∂a␈α∂way␈α∞of␈α∂associating␈α∂a␈α∂value␈α∞property
␈↓ ↓H␈↓with␈αany␈α
atom.␈α This␈αsimple␈α
version␈αof␈α
␈↓↓eval␈↓␈αdoes␈αnot␈α
distinguish␈αbetween␈α
values␈αthat␈αare␈α
intended
␈↓ ↓H␈↓to␈α∂be␈α∂applied␈α∂as␈α∂functions␈α∂and␈α∂simple␈α⊂values.␈α∂ Replace␈α∂the␈α∂a-list␈α∂argument␈α∂by␈α∂a␈α⊂more␈α∂general
␈↓ ↓H␈↓structure␈α∞capable␈α∞of␈α∞associating␈α∞arbitrary␈α∂properties␈α∞with␈α∞an␈α∞atom.␈α∞ Write␈α∂appropriate␈α∞accessing
␈↓ ↓H␈↓and␈α
modifying␈α
functions␈α
(␈↓↓get,␈↓␈α
␈↓↓putprop,␈↓␈α
and␈α
␈↓↓remprop␈↓)␈α
and␈α
modify␈α
␈↓↓eval␈↓␈α
where␈αnecessary,␈α
including
␈↓ ↓H␈↓a clause for ␈↓¬DEFUN. ␈↓

␈↓ ↓H␈↓3.␈α∀ How␈α∪might␈α∀you␈α∪extend␈α∀the␈α∪more␈α∀sophisticated␈α∪␈↓↓eval␈↓␈α∀above␈α∪to␈α∀handle␈α∪␈↓¬PROG␈α∀␈↓and␈α∪related
␈↓ ↓H␈↓constructs.  Are their now new ways of treating statement labels?

␈↓ ↓H␈↓4.␈α The␈αmacro␈αdefinition␈αfeature␈αof␈αLISP␈αdescribed␈αearlier␈αis␈αsome␈αwhat␈αclumsy␈αto␈αuse␈αin␈αgeneral.
␈↓ ↓H␈↓Write␈αyour␈αown␈αmacro␈αdefinition␈αmaker␈αthat␈αtakes␈αa␈αmacro␈αname,␈αa␈αparmeter␈αspecification␈αand␈αa
␈↓ ↓H␈↓definition␈αbody␈αand␈αproduces␈α an␈αappropriate␈αdefinition.␈α The␈αparameter␈αspecification␈αcould␈αbe␈αa
␈↓ ↓H␈↓pattern␈α∪containing␈α∩variables␈α∪that␈α∩are␈α∪to␈α∩match␈α∪various␈α∩kinds␈α∪of␈α∩expressions␈α∪such␈α∪as␈α∩atoms,
␈↓ ↓H␈↓segments␈αof␈αlists,␈αarbitrary␈αexpressions,␈αexpressions␈αsatisfying␈αsome␈αpredicate.␈α The␈α
pattern␈αwould
␈↓ ↓H␈↓also␈α
contain␈α
constants,␈α
etc..␈α
 The␈α
macro␈αdefinition␈α
produced␈α
would␈α
then␈α
generate␈α
code␈αby␈α
matching
␈↓ ↓H␈↓the␈αcall␈αto␈αthe␈αpattern␈αto␈αobtain␈αvalues␈αof␈αthe␈αvariables␈αand␈αthen␈αfilling␈αthem␈αin␈αwhere␈αthey␈αoccur
␈↓ ↓H␈↓in the body.  For example given a definition such as

␈↓ ↓H␈↓¬␈↓ βλ(MACRO-MAKER FOR (?I IN ??LIST DO *STMTS)
␈↓ ↓H␈↓¬␈↓ βλ    (PROG (?I L)
␈↓ ↓H␈↓¬␈↓ βλ        (SETQ L ??LIST)
␈↓ ↓H␈↓¬␈↓ βλ       LOOP
␈↓ ↓H␈↓¬␈↓ βλ        (COND ((NULL L) (RETURN NIL)))
␈↓ ↓H␈↓¬␈↓ βλ        (SETQ ?I (CAR L))
␈↓ ↓H␈↓¬␈↓ βλ        (SETQ L (CDR L))
␈↓ ↓H␈↓¬␈↓ βλ        *STMTS
␈↓ ↓H␈↓¬␈↓ βλ        (GO LOOP)) )

␈↓ ↓H␈↓A call to this macro such as

␈↓ ↓H␈↓¬␈↓ βλ(FOR X IN '(A B C) DO
␈↓ ↓H␈↓¬␈↓ βλ  (COND ((GET X 'FN) (APPLY (GET X 'FN) NIL)))
␈↓ ↓H␈↓¬␈↓ βλ  (PRINT X))

␈↓ ↓H␈↓would result in the following code to be evaluated

␈↓ ↓H␈↓¬␈↓ βλ(PROG (X L)
␈↓ ↓H␈↓¬␈↓ βλ    (SETQ L '(A B C))
␈↓ ↓H␈↓¬␈↓ βλ   LOOP
␈↓ ↓H␈↓¬␈↓ βλ    (COND ((NULL L) (RETURN NIL)))
␈↓ ↓H␈↓¬␈↓ βλ    (SETQ X (CAR L))
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ ≠109


␈↓ ↓H␈↓¬␈↓ βλ    (SETQ L (CDR L))
␈↓ ↓H␈↓¬␈↓ βλ    (COND ((GET X 'FN) (APPLY (GET X 'FN) NIL)))
␈↓ ↓H␈↓¬␈↓ βλ    (PRINT X)
␈↓ ↓H␈↓¬␈↓ βλ    (GO LOOP))

␈↓ ↓H␈↓The␈αtask␈αthen␈αis␈αto␈αwrite␈αthe␈αprogram␈α␈↓↓macro-maker␈↓␈αwhich␈αwill␈αput␈αa␈αsuitable␈αmacro␈αdefinition␈αon
␈↓ ↓H␈↓the property list of the indicated atom.

␈↓ ↓H␈↓α␈↓ ∧wStructure modifying functions

␈↓ ↓H␈↓5.␈α∞Write␈α∞a␈α∞function␈α∞that␈α∞reverses␈α∞a␈α∞list␈α∞by␈α∞reversing␈α∞the␈α∞pointers␈α∞and␈α∞thus␈α∞not␈α∞copying␈α∞the␈α∞list.
␈↓ ↓H␈↓How␈α∩could␈α∩you␈α∩prove␈α∩your␈α∩function␈α∩correct?␈α∪ (The␈α∩latter␈α∩is␈α∩a␈α∩non-trivial␈α∩task␈α∩to␈α∪carry␈α∩out
␈↓ ↓H␈↓formally.)

␈↓ ↓H␈↓α␈↓ ¬≤Re-entrant list structures.

␈↓ ↓H␈↓6. Write a program to list the atoms in a possibly re-entrant list structure.

␈↓ ↓H␈↓7.␈α
Write␈α
a␈α
program␈α
to␈αtranslate␈α
a␈α
graph␈α
description␈α
from␈α
the␈αnotation␈α
of␈α
Chapter␈α
I␈α
to␈αa␈α
re-entrant
␈↓ ↓H␈↓list structure.

␈↓ ↓H␈↓8.␈α∂Write␈α∞a␈α∂function␈α∂␈↓↓mk-label␈↓␈α∞which␈α∂takes␈α∞a␈α∂name␈α∂(atom)␈α∞and␈α∂an␈α∞expression␈α∂as␈α∂arguments␈α∞and
␈↓ ↓H␈↓replaces␈α∂all␈α∂occurrences␈α∂of␈α∂the␈α∂name␈α∂by␈α∂a␈α∂pointer␈α∂to␈α∂the␈α∂expression␈α∂itself.␈α∂ When␈α∂applied␈α∂to␈α∞a
␈↓ ↓H␈↓lambda-expression␈α
this␈αcreates␈α
a␈α
self-referential␈αexpression␈α
which␈α
when␈αapplied␈α
to␈αan␈α
appropriate
␈↓ ↓H␈↓list␈α
of␈α
arguments␈α
returns␈α
the␈α
same␈α
value␈α
as␈α
if␈α
␈↓↓label[name,expression]␈↓␈α
had␈α
been␈α
applied␈α
to␈α
the␈α
same
␈↓ ↓H␈↓arguments thus simulating the ␈↓↓label␈↓ construct.

␈↓ ↓H␈↓9.␈αWrite␈αfunctions␈αto␈αtranslate␈αin␈αeach␈αdirection␈αbetween␈αpossibly␈αre-entrant␈αstructures␈αand␈αthe␈αS-
␈↓ ↓H␈↓expressions corresponding to them using the ␈↓¬LABEL, ␈↓␈↓¬POINTER ␈↓notation described above.

␈↓ ↓H␈↓10.␈α
Write␈α
an␈α
axiom␈α
schema␈α
of␈α
induction␈α
for␈α
finite␈α
possibly␈α
re-entrant␈α
list␈α
structures.␈α
 Hint:␈α
The
␈↓ ↓H␈↓schema␈α⊂can␈α∂be␈α⊂based␈α⊂on␈α∂an␈α⊂assertion␈α⊂of␈α∂convergence␈α⊂of␈α∂a␈α⊂program␈α⊂that␈α∂scans␈α⊂a␈α⊂list␈α∂structure
␈↓ ↓H␈↓keeping track of the vertices it has already visited.
␈↓ ↓H␈↓110␈↓ εH␈↓ H


␈↓ ↓H␈↓α␈↓ εαChapter V

␈↓ ↓H␈↓α␈↓ ¬:HOW LISP WORKS


␈↓ ↓H␈↓        The␈αpurpose␈αof␈αthis␈αchapter␈αis␈αto␈αgive␈αyou␈αan␈αidea␈αof␈αwhat␈αit␈αtakes␈αto␈αmake␈αLISP␈αactually
␈↓ ↓H␈↓run␈αon␈αcomputer.␈α  There␈αare␈αtwo␈αaspects␈αof␈αa␈αLISP␈αsystem.␈α One␈αis␈αthe␈αbasic␈αdata␈αstructures␈αand
␈↓ ↓H␈↓corresponding␈αprimitive␈αoperations␈αnecessary␈αfor␈αa␈α
minimal␈αsystem␈αthat␈αwill␈αbe␈αsimply␈αbe␈α
able␈αto
␈↓ ↓H␈↓read␈αexpressions␈αand␈α
return␈αvalues.␈α The␈α
second␈αis␈αthe␈α
ability␈αto␈αinteract␈α
productively␈αwith␈αa␈α
user.
␈↓ ↓H␈↓In␈α
particular,␈α∞we␈α
require␈α∞more␈α
of␈α∞our␈α
system␈α
that␈α∞just␈α
being␈α∞able␈α
to␈α∞run␈α
programs.␈α∞ Most␈α
LISP
␈↓ ↓H␈↓systems␈α∞have␈α∞facilities␈α∞for␈α∞making␈α∞output␈α∞more␈α∞readable,␈α∞for␈α∞detecting␈α∞and␈α∞reporting␈α∞errors,␈α
for
␈↓ ↓H␈↓helping␈α∞debug␈α∞programs,␈α∞and␈α∞for␈α∞editing␈α∂programs␈α∞when␈α∞errors␈α∞are␈α∞found.␈α∞ The␈α∞ability␈α∂to␈α∞run
␈↓ ↓H␈↓programs␈αinterpretively␈αand␈αthe␈αvery␈αsimple␈αsyntax␈α
of␈αLISP␈αmake␈αit␈αpossible␈αto␈αhave␈αvery␈α
elegant
␈↓ ↓H␈↓error-handling,␈α⊂debugging␈α⊂and␈α⊂editing␈α⊂facilities␈α⊂as␈α∂an␈α⊂integral␈α⊂part␈α⊂of␈α⊂the␈α⊂system.␈α⊂  Usually␈α∂a
␈↓ ↓H␈↓LISP system can also run compiled functions.

␈↓ ↓H␈↓        We␈α
will␈α∞describe␈α
the␈α
main␈α∞components␈α
of␈α
a␈α∞LISP␈α
system␈α
and␈α∞a␈α
few␈α∞additional␈α
operations
␈↓ ↓H␈↓usually␈α
made␈αavailable␈α
to␈αthe␈α
user.␈α Typically␈α
the␈αfeatures␈α
which␈αare␈α
in␈αaddition␈α
to␈α
the␈αminimal
␈↓ ↓H␈↓primitives␈α
are␈α∞written␈α
in␈α∞LISP.␈α
 We␈α∞shall␈α
give␈α∞some␈α
examples,␈α∞and␈α
suggest␈α∞additional␈α
 progams
␈↓ ↓H␈↓you to write.  The examples will represent a variety of programming styles.



␈↓ ↓H␈↓1.  ␈↓αBasic data structures and operations of LISP.␈↓


␈↓ ↓H␈↓        In␈α⊂this␈α⊂and␈α⊂the␈α⊂following␈α∂section␈α⊂we␈α⊂will␈α⊂discuss␈α⊂what␈α∂is␈α⊂required␈α⊂for␈α⊂a␈α⊂minimal␈α∂LISP.
␈↓ ↓H␈↓Clearly␈αit␈αmust␈αbe␈α
able␈αto␈αaccept␈αinput␈α(read),␈α
evaluate␈αexpressions␈αand␈αreturn␈αthe␈α
results␈α(print).
␈↓ ↓H␈↓But␈αbefore␈αwe␈αcan␈αeven␈αread␈αand␈αprint␈αit␈αis␈αnecessary␈αto␈αbe␈αable␈αto␈αrecognize␈αand␈αconstruct␈αatoms
␈↓ ↓H␈↓and␈α∩S-expressions.␈α∩ Thus␈α∩we␈α∩begin␈α∩with␈α∪a␈α∩discussion␈α∩of␈α∩the␈α∩basic␈α∩data␈α∩structures␈α∪of␈α∩LISP.
␈↓ ↓H␈↓Typically␈α∞a␈α∞LISP␈α∞system␈α∞divides␈α∞its␈α∞world␈α∞into␈α∞various␈α∞types␈α∞of␈α∞objects.␈α∞ Some␈α
implementations
␈↓ ↓H␈↓may␈αacknowledge␈αmore␈αtypes␈αthan␈αothers,␈α
but␈αthe␈αbasic␈αkinds␈αof␈αobjects␈αusually␈α
include␈αsymbolic
␈↓ ↓H␈↓atoms,␈αprint␈αnames␈α
(e.g.␈αcharacter␈αstrings)␈αhenceforth␈α
known␈αas␈α"pnames",␈αnumbers␈α
(often␈αseveral
␈↓ ↓H␈↓kinds),␈α∂list␈α∂structures,␈α∂stacks␈α∂(for␈α∞storing␈α∂control␈α∂information),␈α∂binary␈α∂programs␈α∂(compiled␈α∞code),
␈↓ ↓H␈↓and arrays.


␈↓ ↓H␈↓αSymbolic Atoms.

␈↓ ↓H␈↓        LISP␈αdistinguishes␈αtwo␈αkinds␈αof␈αatoms,␈αnumeric␈αand␈αsymbolic.␈α Symbolic␈αatoms␈αcan␈αbe␈αused
␈↓ ↓H␈↓as␈α⊂variables␈α∂and␈α⊂function␈α⊂names,␈α∂and␈α⊂may␈α∂in␈α⊂general␈α⊂have␈α∂various␈α⊂properties␈α⊂associated␈α∂with
␈↓ ↓H␈↓them.␈α
 They␈αneed␈α
to␈αbe␈α
represented␈αuniquely␈α
in␈α
the␈αcomputer␈α
as␈αthe␈α
these␈αproperties␈α
can␈α
not␈αbe
␈↓ ↓H␈↓determined␈αfrom␈αthe␈αname␈α(as␈αcan␈αthe␈αvalue␈αof␈αa␈αnumber␈αfor␈αexample).␈α LISP␈αkeeps␈αtrack␈αof␈αthe
␈↓ ↓H␈↓atoms␈α∃that␈α∃it␈α∃knows␈α⊗by␈α∃name␈α∃by␈α∃making␈α∃entries␈α⊗in␈α∃the␈α∃"oblist"␈α∃(called␈α∃obarray␈α⊗in␈α∃some
␈↓ ↓H␈↓implementations).␈α The␈αoblist␈αis␈αLISPs␈α"symbol␈αtable".␈α When␈αthe␈αLISP␈αreader␈αcollects␈αa␈αstring␈αof
␈↓ ↓H␈↓characters␈αthat␈αname␈αan␈αatom␈αit␈αlooks␈αin␈αthe␈αoblist␈αto␈αsee␈αif␈αan␈αatom␈αof␈αthat␈αname␈αalready␈αexists.
␈↓ ↓H␈↓If␈αso␈αthe␈αname␈αrefers␈αto␈α
that␈αatom␈αand␈αa␈αpointer␈αto␈αit␈α
is␈αreturned,␈αotherwise␈αan␈αatom␈αof␈αthat␈α
name
␈↓ ↓H␈↓is created and added to the list.

␈↓ ↓H␈↓        The␈α∩exact␈α⊃representation␈α∩of␈α∩an␈α⊃atom␈α∩varies␈α⊃with␈α∩implementation.␈α∩ It␈α⊃could␈α∩just␈α∩be␈α⊃an
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠111


␈↓ ↓H␈↓ordinary␈α∞list␈α∞cell␈α∞where␈α∞the␈α∞d-part␈α∞is␈α∞a␈α∞pointer␈α
to␈α∞the␈α∞property␈α∞list␈α∞and␈α∞the␈α∞a-part␈α∞is␈α∞some␈α
flag
␈↓ ↓H␈↓saying␈α
"I␈α
am␈αan␈α
atom"␈α
(e.g.␈αa␈α
collection␈α
of␈αbits␈α
that␈α
could␈α
not␈αbe␈α
a␈α
address␈αof␈α
a␈α
list␈αcell)␈α
Alternately
␈↓ ↓H␈↓an␈α
atom␈αcould␈α
be␈αa␈α
block␈αof␈α
cells␈αin␈α
a␈α
particular␈αsegment␈α
of␈αmemory,␈α
where␈αsome␈α
of␈αthe␈α
properties
␈↓ ↓H␈↓such␈αas␈αvalue␈αand␈αpname␈αand␈αa␈αpointer␈αto␈αthe␈αgeneral␈αproperty␈αlist␈αare␈αin␈αfixed␈α
locations␈αwithin
␈↓ ↓H␈↓the block Other representations are possible.

␈↓ ↓H␈↓        The␈αoblist␈αcould␈αbe␈αan␈αordinary␈αlist␈αmanipulated␈αby␈α␈↓↓car,␈↓␈α␈↓↓cdr,␈↓␈α␈↓↓cons␈↓␈αand␈αeven␈α␈↓↓rplac␈↓'s.␈α Some
␈↓ ↓H␈↓implementations␈αuse␈αarrays␈αrather␈αthan␈αlists␈αto␈αstore␈αthe␈αoblist,␈αhence␈αthe␈αname␈α
"obarray".␈α Often
␈↓ ↓H␈↓the␈α
oblist␈α
is␈α
"hashed"␈α
in␈α
order␈α
to␈α
speed␈α
up␈α
the␈α
search␈α
for␈α
a␈α
particular␈α
entry.␈α
 The␈α
idea␈α
of␈αa␈α
hashed
␈↓ ↓H␈↓list␈α(or␈αarray)␈αis␈αthat␈αit␈αis␈αdivided␈αup␈αinto␈αa␈αfixed␈αnumber␈α␈↓↓n␈↓␈αof␈αsublists␈αcalled␈α"hash␈αbuckets",␈αand
␈↓ ↓H␈↓there␈αis␈αa␈αfunction␈α␈↓↓hash␈↓␈αwhich␈αcomputes␈αa␈αnumber␈αbetween␈α1␈αand␈α␈↓↓n␈↓␈αfor␈αeach␈αpossible␈αelement␈αof
␈↓ ↓H␈↓the␈α∂list.␈α∂ If␈α⊂␈↓↓hash[e]␈↓␈α∂is␈α∂␈↓↓k␈↓␈α⊂then␈α∂␈↓↓e␈↓␈α∂is␈α∂in␈α⊂the␈α∂␈↓↓k␈↓th␈α∂bucket␈α⊂and␈α∂the␈α∂search␈α∂for␈α⊂␈↓↓e␈↓␈α∂can␈α∂be␈α⊂restricted␈α∂to
␈↓ ↓H␈↓searching␈αthat␈αbucket␈αrather␈αthan␈αthe␈αwhole␈αlist.␈α In␈αthe␈αcase␈αof␈αoblists␈αthe␈αargument␈αto␈α␈↓↓hash␈↓␈αis␈αa
␈↓ ↓H␈↓pname.␈α  If␈α
the␈αhashed␈α
list␈αis␈α
to␈αbe␈α
an␈αimprovement␈αover␈α
a␈αsimple␈α
linear␈αlist␈α
as␈αfar␈α
as␈αsearching
␈↓ ↓H␈↓efficiency␈αis␈αconcerned,␈αthe␈αhash␈αfunction␈αshould␈αdistribute␈αthe␈αelements␈αof␈αthe␈αlist␈αevenly␈αamong
␈↓ ↓H␈↓the␈α∂buckets.␈α∂ This␈α∂is␈α∂not␈α∂always␈α∂easy␈α∂to␈α∂do.␈α∂ For␈α∂a␈α∂further␈α∂discussion␈α∂of␈α∂hashing␈α∂as␈α∂a␈α∞general
␈↓ ↓H␈↓searching method see Knuth[1968b].

␈↓ ↓H␈↓        The␈α
basic␈α
operations␈α
on␈αsymbolic␈α
atoms,␈α
pnames,␈α
and␈α
oblists␈αcan␈α
be␈α
described␈α
in␈α
terms␈αof
␈↓ ↓H␈↓the␈αfollowing␈αthree␈αprimitive␈αoperations:␈α
␈↓↓makatom,␈↓␈α␈↓↓intern,␈↓␈αand␈α␈↓↓remob.␈↓␈α  ␈↓↓makatom␈↓␈αtakes␈α
a␈αpname
␈↓ ↓H␈↓and␈α⊃creates␈α⊃an␈α⊃atomic␈α⊃object␈α⊃having␈α⊃that␈α⊃pname.␈α⊃ The␈α⊃value␈α⊃of␈α⊃␈↓↓mkatom␈↓␈α⊃is␈α⊃a␈α⊃pointer␈α⊃to␈α⊂the
␈↓ ↓H␈↓resulting␈αatom.␈α This␈α
object␈αis␈αnew.␈α
 ␈↓↓intern␈↓␈αtakes␈αan␈αatomic␈α
object␈α(a␈αpointer␈α
to␈αit)␈αand␈α
returns␈αa
␈↓ ↓H␈↓pointer␈α
to␈α
an␈αatom␈α
with␈α
the␈α
same␈αpname␈α
which␈α
is␈α
a␈αmember␈α
of␈α
the␈α
oblist.␈α If␈α
no␈α
atom␈α
with␈αthe
␈↓ ↓H␈↓same␈α
pname␈αis␈α
on␈α
the␈αoblist␈α
then␈α
␈↓↓intern␈↓␈αputs␈α
the␈α
given␈αobject␈α
there,␈α
and␈αthe␈α
returned␈αpointer␈α
 will
␈↓ ↓H␈↓thus␈αbe␈αequal␈αto␈αthe␈αinput␈α
pointer.␈α  If␈αan␈αatom␈αof␈αthe␈α
same␈αname␈αis␈αalready␈αon␈αthe␈α
 oblist,␈αthen
␈↓ ↓H␈↓the␈α
pointer␈αto␈α
that␈αatom␈α
is␈α
returned␈αand␈α
it␈αmay␈α
or␈α
may␈αnot␈α
be␈αequal␈α
to␈α
the␈αinput␈α
pointer.␈α ␈↓↓remob␈↓␈α
is
␈↓ ↓H␈↓the␈αinverse␈αof␈α␈↓↓intern,␈↓␈αin␈αthat␈αit␈αtakes␈αan␈αatomic␈αobject␈αas␈αan␈αargument␈αand␈αremoves␈αit␈αfrom␈α the
␈↓ ↓H␈↓oblist.␈α∞ ␈↓↓remob␈↓␈α
doesn't␈α∞destroy␈α
the␈α∞atom,␈α
it␈α∞just␈α∞can␈α
no␈α∞longer␈α
be␈α∞referenced␈α
by␈α∞name.␈α∞ ␈↓↓remob␈↓␈α
and
␈↓ ↓H␈↓␈↓↓intern␈↓␈αare␈αusually␈αavailable␈αdirectly␈αto␈αthe␈αuser.␈α ␈↓↓mkatom␈↓␈αis␈αonly␈αindirectly␈αavailable␈αthrough␈αsuch
␈↓ ↓H␈↓operations␈αas␈α␈↓↓gensym␈↓␈α
and␈αfriends.␈α As␈α
with␈αany␈αdestructive␈α
operation␈αthe␈αuse␈α
of␈α␈↓↓remob␈↓␈αcan␈αget␈α
you
␈↓ ↓H␈↓into trouble if you are not careful, and are not quite sure of the state of the world when you do it.

␈↓ ↓H␈↓        We␈αcan␈αnow␈αexplain␈αfurther␈αthe␈αsymbolic␈αatom␈αmanipulating␈αoperations␈α␈↓↓gensym,␈↓␈α␈↓↓implode,␈↓
␈↓ ↓H␈↓and ␈↓↓maknam.␈↓  Consider the following fragment of a session with LISP.

␈↓ ↓H␈↓␈↓ αXuser:␈↓ ¬_␈↓¬(SETQ X (GENSYM))␈↓
␈↓ ↓H␈↓␈↓ αXLISP:␈↓ ¬_␈↓¬G0123  ␈↓

␈↓ ↓H␈↓␈↓ αXuser:␈↓ ¬_␈↓¬(EQ X 'G0123)␈↓
␈↓ ↓H␈↓␈↓ αXLISP:␈↓ ¬_␈↓¬NIL ␈↓

␈↓ ↓H␈↓This␈α
appearently␈α
strange␈α
behavior␈α
is␈α
due␈α
to␈α
the␈α
fact␈α
that␈α
the␈α
symbolic␈α
atom␈α
generated␈α∞by␈α
LISP
␈↓ ↓H␈↓when␈α
evaluating␈αthe␈α
␈↓↓gensym␈↓␈α
is␈αnot␈α
only␈αnot␈α
on␈α
the␈αcurrent␈α
oblist␈α(of␈α
known␈α
atoms),␈αit␈α
is␈αnot␈α
added
␈↓ ↓H␈↓to␈α∂that␈α∞list.␈α∂ Thus␈α∂␈↓↓gensym␈↓␈α∞can␈α∂be␈α∞thought␈α∂of␈α∂as␈α∞looking␈α∂up␈α∞a␈α∂prefix␈α∂and␈α∞count␈α∂in␈α∂some␈α∞gobal
␈↓ ↓H␈↓location,␈αconstructing␈αa␈αpname␈αand␈αcalling␈α␈↓↓mkatom.␈↓␈α If␈αat␈αa␈αlater␈αtime␈αthe␈αreader␈αcomes␈αacross␈αthe
␈↓ ↓H␈↓same␈αpname␈α
it␈αdoes␈αnot␈α
find␈αthe␈α
atom␈αcreated␈αby␈α
␈↓↓gensym␈↓␈αon␈α
the␈αoblist.␈α (It␈α
either␈αfinds␈α
no␈αentry
␈↓ ↓H␈↓and␈αcreates␈αone␈αor␈αfinds␈αa␈αdifferent␈αatom␈αwith␈αthat␈αname.)␈α ␈↓↓maknam␈↓␈αtakes␈αa␈αlist␈αof␈αcharacters␈αand
␈↓ ↓H␈↓constructs␈α∞a␈α
pname␈α∞and␈α
applies␈α∞␈↓↓mkatom,␈↓␈α
while␈α∞␈↓↓implode␈↓␈α
constructs␈α∞the␈α
pname,␈α∞then␈α
looks␈α∞in␈α
the
␈↓ ↓H␈↓oblist␈α
and␈αeither␈α
returns␈αa␈α
pointer␈αto␈α
an␈αalready␈α
existing␈α
atom␈αwith␈α
that␈αname,␈α
or␈αif␈α
none␈αis␈α
found
␈↓ ↓H␈↓it␈αapplies␈α␈↓↓mkatom␈↓␈αfollowed␈αby␈α␈↓↓intern.␈↓␈α Thus␈αthe␈αresulting␈αatom␈αcan␈αbe␈αreferred␈αto␈αby␈αname␈αwhile
␈↓ ↓H␈↓112␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓in␈α
the␈α
␈↓↓gensym␈↓␈α
and␈α
␈↓↓maknam␈↓␈α
cases␈α
the␈α
resulting␈α
atom␈α
can␈α
only␈α
be␈α
referred␈α
to␈α
if␈α
you␈α
save␈αthe␈α
pointer
␈↓ ↓H␈↓that␈α∞is␈α∂returned.␈α∞ Perhaps␈α∞the␈α∂following␈α∞conversation␈α∂with␈α∞LISP␈α∞will␈α∂help␈α∞make␈α∂the␈α∞distinction
␈↓ ↓H␈↓clear.

␈↓ ↓H␈↓␈↓ αXuser:␈↓ ¬_␈↓¬(MAKNAM '(B A Z))␈↓
␈↓ ↓H␈↓␈↓ αXLISP:␈↓ ¬_␈↓¬BAZ  ␈↓

␈↓ ↓H␈↓␈↓ αXuser:␈↓ ¬_␈↓¬(IMPLODE '(B A Z))␈↓
␈↓ ↓H␈↓␈↓ αXLISP:␈↓ ¬_␈↓¬BAZ  ␈↓

␈↓ ↓H␈↓␈↓ αXuser:␈↓ ¬_␈↓¬(EQ (MAKNAM '(B A Z)) (MAKNAM '(B A Z)))␈↓
␈↓ ↓H␈↓␈↓ αXLISP:␈↓ ¬_␈↓¬NIL  ␈↓

␈↓ ↓H␈↓␈↓ αXuser:␈↓ ¬_␈↓¬(EQ (IMPLODE '(B A Z)) (IMPLODE '(B A Z)))␈↓
␈↓ ↓H␈↓␈↓ αXLISP:␈↓ ¬_␈↓¬T  ␈↓

␈↓ ↓H␈↓One␈αreason␈αfor␈αusing␈α␈↓↓gensym␈↓␈αand␈α␈↓↓maknam␈↓␈αhas␈αto␈αdo␈αwith␈αthe␈αfact␈αthat␈αan␈αatomic␈αsymbol␈αthat␈αis
␈↓ ↓H␈↓put␈α
on␈α
the␈αoblist␈α
never␈α
goes␈α
away␈α(unless␈α
you␈α
explicitly␈α
␈↓↓remob␈↓␈αit),␈α
while␈α
atoms␈α
generated␈αby␈α
␈↓↓gensym␈↓
␈↓ ↓H␈↓and␈α
␈↓↓implode␈↓␈α
can␈α
be␈α
garbage␈α
collected␈α
as␈α
soon␈α
as␈αno␈α
one␈α
is␈α
pointing␈α
to␈α
them␈α
any␈α
more.␈α
 Thus␈αif
␈↓ ↓H␈↓you␈αhave␈α
a␈αprogran␈α
that␈αis␈α
going␈αto␈α
generate␈αa␈α
lot␈αof␈α
temporary␈αsymbols␈α
which␈αwill␈α
not␈αbe␈α
wanted
␈↓ ↓H␈↓after some it is efficient spacewise to not put these symbols on the oblist.


␈↓ ↓H␈↓αNumbers.

␈↓ ↓H␈↓        Numeric␈αatoms␈αare␈αgenerally␈αassumed␈αto␈αdenote␈αparticular␈αnumbers␈αand␈αcan␈αbe␈αrecognized,
␈↓ ↓H␈↓represented␈α∂and␈α∂printed␈α∂without␈α∂a␈α∂lot␈α∞of␈α∂additional␈α∂information.␈α∂ Thus,␈α∂although␈α∂numbers␈α∞are
␈↓ ↓H␈↓classed␈αas␈αatoms␈α(e.g.␈α␈↓αat|␈↓1␈αis␈αtrue)␈αthe␈αrepresentation␈αof␈αnumbers␈αin␈αLISP␈αis␈αusually␈αdifferent␈αthan
␈↓ ↓H␈↓that␈α∞of␈α∞symbolic␈α∞atoms.␈α∞ Numbers␈α
are␈α∞not␈α∞entered␈α∞on␈α∞the␈α
oblist␈α∞and␈α∞they␈α∞do␈α∞not␈α∞have␈α
property
␈↓ ↓H␈↓lists.␈α Their␈αpnames␈α
and␈α"values"␈αare␈αcomputed␈α
from␈αthe␈αrepresentation␈α
and␈αcannot␈αbe␈αaccessed␈α
in
␈↓ ↓H␈↓the␈αusual␈αway.␈α
In␈αparticular,␈αyou␈α
cannot␈αset␈αthe␈α"value"␈α
of␈αa␈αnumeric␈α
atom␈α(which␈αis␈αprobably␈α
just
␈↓ ↓H␈↓as␈α
well)␈α
so␈α
they␈α
really␈α
are␈α
constants.␈α
 One␈α
possible␈α
representation␈α
of␈α
numbers␈α
is␈α
by␈α
a␈α
cell␈α
the␈α
a-
␈↓ ↓H␈↓part␈α
of␈α
which␈αsays␈α
this␈α
is␈α
an␈αatom,␈α
and␈α
d-part␈α
which␈αis␈α
a␈α
pair␈α
consisting␈αof␈α
the␈α
type␈α
and␈αa␈α
pointer
␈↓ ↓H␈↓to␈α∪actual␈α∪machine␈α∪representation␈α∩of␈α∪the␈α∪numerical␈α∪value.␈α∩ Sufficiently␈α∪small␈α∪integers␈α∪can␈α∩be
␈↓ ↓H␈↓represented␈α
more␈α
compactly␈α
by␈αa␈α
cell␈α
the␈α
a-part␈αof␈α
which␈α
is␈α
a␈α
special␈αflag␈α
and␈α
the␈α
d-part␈αof␈α
which
␈↓ ↓H␈↓is␈αa␈αmachine␈αrepresentation␈αof␈αthe␈αnumerical␈αvalue.␈α Another␈αpossibility␈αis␈αto␈αstore␈αall␈αnumbers␈αof
␈↓ ↓H␈↓a␈α
particular␈α
kind␈α
in␈α
a␈αparticular␈α
area␈α
and␈α
represent␈α
a␈α
number␈αby␈α
a␈α
pointer␈α
directly␈α
to␈αthe␈α
machine
␈↓ ↓H␈↓representation␈α⊃of␈α⊃its␈α⊃value.␈α⊃ As␈α⊃we␈α∩mentioned␈α⊃in␈α⊃Chapter␈α⊃I,␈α⊃LISP␈α⊃can␈α⊃treat␈α∩arbitrarily␈α⊃large
␈↓ ↓H␈↓integers␈αby␈αrepresenting␈αthem␈αas␈αa␈αlist␈α"digits"␈αwith␈αrespect␈αto␈αsome␈αlarge␈αbase.␈α Typically␈αthe␈αfirst
␈↓ ↓H␈↓element␈αof␈αthe␈αlist␈αis␈αa␈α
flag␈αsaying␈α"I␈αam␈αa␈αbignum"␈αand␈α
a␈αsign.␈α In␈αany␈αcase␈αthe␈αreading,␈α
printing,
␈↓ ↓H␈↓and␈αprimitive␈αfunctions␈αof␈αLISP␈αmust␈αpay␈αattention␈αto␈αwhether␈αor␈αnot␈αthe␈αobject␈αbeing␈αdealt␈αwith
␈↓ ↓H␈↓is a symbolic atom or a numerical atom or some non-atomic object.


␈↓ ↓H␈↓αList structure.

␈↓ ↓H␈↓                Now␈α
that␈α
we␈α
can␈αbuild␈α
and␈α
recognize␈α
atoms,␈αthe␈α
next␈α
step␈α
is␈αto␈α
be␈α
able␈α
to␈αconstruct
␈↓ ↓H␈↓representations␈α⊗of␈α⊗arbitrary␈α⊗S-expressions.␈α⊗ Thus␈α∃we␈α⊗need␈α⊗to␈α⊗represent␈α⊗list␈α⊗structures␈α∃and
␈↓ ↓H␈↓implement␈α
the␈α
operations␈α␈↓↓car,␈↓␈α
␈↓↓cdr,␈↓␈α
and␈α␈↓↓cons.␈↓␈α
 LISP␈α
sets␈αaside␈α
a␈α
portion␈αof␈α
memory␈α
available␈αto␈α
it
␈↓ ↓H␈↓in␈α
an␈α
area␈α
called␈α
list␈α
space.␈α
 This␈α
space␈α
consists␈α
of␈α
"list␈α
cells"␈α
A␈α
list␈α
cell␈α
must␈α
be␈α
big␈α
enough␈αto␈α
hold
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠113


␈↓ ↓H␈↓two␈α
pointers␈α
and␈α
could␈α
be␈αa␈α
single␈α
machine␈α
word␈α
with␈α
the␈αtwo␈α
halves␈α
being␈α
pointers␈α
to␈α
the␈αa-␈α
and
␈↓ ↓H␈↓d-␈αparts␈αor␈αif␈αthe␈αsize␈αof␈αa␈αword␈αis␈αtoo␈αsmall,␈αa␈αpair␈αof␈αwords.␈α In␈αeither␈αcase␈α␈↓↓car␈↓␈αand␈α␈↓↓cdr␈↓␈αcan␈αbe
␈↓ ↓H␈↓fairly␈α∩simply␈α∩operations␈α∩(in␈α∩the␈α∩case␈α∩of␈α∩the␈α∩usual␈α∩PDP-10␈α∩implementation␈α∩a␈α∩single␈α⊃machine
␈↓ ↓H␈↓instruction).

␈↓ ↓H␈↓        At␈αany␈α
particular␈αtime␈αsome␈α
of␈αthe␈α
list␈αspace␈αwill␈α
be␈αin␈α
use␈αand␈αsome␈α
of␈αit␈α
will␈αbe␈αfree.␈α
 The
␈↓ ↓H␈↓free␈αspace␈αis␈αlinked␈αinto␈αa␈αsingle␈αlist␈αcalled␈αthe␈α"free␈αstorage␈αlist"␈αand␈αLISP␈αkeeps␈αa␈αpointer␈αto␈αthe
␈↓ ↓H␈↓beginning␈αof␈αthat␈αlist.␈α When␈αa␈α␈↓↓cons␈↓␈αis␈αexecuted,␈αa␈αword␈αis␈αremoved␈αfrom␈αthe␈αfree␈αstorage␈αlist,␈αthe
␈↓ ↓H␈↓a-part␈α
and␈α
the␈αd-part␈α
are␈α
filled␈α
in␈αand␈α
a␈α
pointer␈α
to␈αthis␈α
word␈α
is␈α
returned.␈α When␈α
there␈α
is␈αno␈α
more
␈↓ ↓H␈↓free␈αstorage␈αleft␈α(more␈αaccurately␈αwhen␈αthe␈α
amount␈αleft␈αreaches␈αsome␈αpreset␈αminimal␈αamount)␈α
then
␈↓ ↓H␈↓LISP␈αattempts␈αto␈αobtain␈αmore␈αfree␈αspace.␈α One␈αway␈αis␈αto␈αrequest␈αit␈αfrom␈αthe␈αhost␈αsystem,␈αbut␈αthis
␈↓ ↓H␈↓is␈α
undesirable␈αin␈α
general,␈αsince␈α
a␈α
lot␈αof␈α
the␈αlist␈α
space␈αis␈α
taken␈α
up␈αby␈α
structures␈αwhich␈α
are␈αno␈α
longer
␈↓ ↓H␈↓accessible␈α
and␈α
may␈α
as␈α∞well␈α
be␈α
"recycled".␈α
 This␈α∞recycling␈α
process␈α
is␈α
done␈α∞by␈α
what␈α
is␈α
known␈α∞as␈α
a
␈↓ ↓H␈↓"garbage␈αcollector".␈α Thus␈αwhen␈α␈↓↓cons␈↓␈αdiscovers␈αthat␈αit␈αis␈αout␈αof␈αspace,␈αit␈αcalls␈αthe␈αgarbage␈αcollecter
␈↓ ↓H␈↓to␈α⊂find␈α⊂some.␈α⊂ The␈α⊂garbage␈α⊃collector␈α⊂must␈α⊂first␈α⊂find␈α⊂out␈α⊂which␈α⊃parts␈α⊂of␈α⊂list␈α⊂space␈α⊂are␈α⊃in␈α⊂use
␈↓ ↓H␈↓(accessible)␈α∞and␈α∞which␈α∞are␈α∞not␈α
(the␈α∞marking␈α∞phase),␈α∞then␈α∞it␈α
collects␈α∞the␈α∞unused␈α∞space,␈α∞making␈α
a
␈↓ ↓H␈↓new␈αfree␈αstorage␈αlist␈α(the␈αsweep␈αphase).␈α  If␈αit␈αsucceeds␈αin␈αfinding␈αunused␈αspace,␈α␈↓↓cons␈↓␈αcan␈αproceed,
␈↓ ↓H␈↓if not the computation usually grinds to a halt, printing some suitable error message.

␈↓ ↓H␈↓        To␈αdetermine␈αwhat␈αparts␈αof␈αlist␈αstructure␈αare␈αcurrently␈αaccessible␈αthe␈αgarbage␈αcollector␈αstarts
␈↓ ↓H␈↓with␈α⊃all␈α⊂of␈α⊃the␈α⊂immediately␈α⊃accessible␈α⊂parts␈α⊃and␈α⊂follows␈α⊃all␈α⊂possible␈α⊃␈↓↓car␈↓␈α⊂and␈α⊃␈↓↓cdr␈↓␈α⊃chains␈α⊂until
␈↓ ↓H␈↓reaching␈αan␈αatom␈αor␈αsomething␈αpreviously␈α
seen␈α(marked).␈α The␈αimmediately␈αaccessible␈αparts␈αof␈α
list
␈↓ ↓H␈↓structure␈αinclude␈αthe␈αproperty␈αlists␈αof␈αall␈αatoms␈α(including␈αthe␈αcontents␈αof␈αvalue␈αcell␈αin␈αcase␈αthis␈αis
␈↓ ↓H␈↓kept␈αseparately),␈αstructures␈αon␈αwhatever␈αstacks␈αLISP␈α
is␈αusing␈αfor␈αcontrol,␈αcontents␈αof␈αany␈α
list␈αtype
␈↓ ↓H␈↓arrays,␈αplus␈αprobably␈α
a␈αfew␈αspecial␈αlocations␈α
or␈αregisters␈αthat␈αLISP␈α
may␈αuse␈αfor␈α
special␈αpurposes.
␈↓ ↓H␈↓There␈α
are␈αa␈α
number␈αof␈α
algorithms␈αof␈α
doing␈αthe␈α
marking␈αand␈α
collection␈αof␈α
unused␈αspace.␈α
 It␈α
is␈αa
␈↓ ↓H␈↓technique␈α⊂generally␈α⊂useful␈α⊂in␈α⊂situations␈α⊂where␈α⊂storage␈α⊂gets␈α⊂allocated␈α⊂and␈α⊂reclaimed.␈α⊂ For␈α∂more
␈↓ ↓H␈↓details the reader is referred to Knuth[1968a] or Baker[1978] and references therein.


␈↓ ↓H␈↓αControl structures.

␈↓ ↓H␈↓        In␈α
addition␈α
to␈α
the␈α
structures␈α
that␈α
make␈α∞up␈α
the␈α
data␈α
manipulated␈α
by␈α
a␈α
LISP␈α∞program,␈α
the
␈↓ ↓H␈↓LISP␈α
system␈αmust␈α
maintain␈αstructures␈α
describing␈αthe␈α
current␈αstate␈α
of␈αa␈α
computation.␈α
  In␈αgeneral
␈↓ ↓H␈↓there␈αwill␈α
be␈αone␈α
or␈αmore␈α
stacks␈α(often␈α
called␈αPush-Down-Lists␈α
or␈αpdls)␈α
where␈αinformation␈αis␈α
kept
␈↓ ↓H␈↓as␈α∀to␈α∀current␈α∀and/or␈α∪old␈α∀variable␈α∀bindings,␈α∀knowlege␈α∪about␈α∀the␈α∀expression␈α∀currently␈α∪being
␈↓ ↓H␈↓evaluated,␈α⊃what␈α⊃to␈α⊂do␈α⊃next,␈α⊃etc..␈α⊂ One␈α⊃reason␈α⊃for␈α⊂using␈α⊃stacks␈α⊃is␈α⊂that␈α⊃they␈α⊃fit␈α⊃naturally␈α⊂with
␈↓ ↓H␈↓evaluation␈α∞of␈α∞recursive␈α∞procedures.␈α∞ When␈α∞we␈α∞discuss␈α∞compiling␈α∞LISP,␈α∞you␈α∞will␈α∞see␈α∞one␈α∂way␈α∞of
␈↓ ↓H␈↓managing␈α
a␈α
stack␈α
that␈α
contains␈α
a␈α
minimal␈α
amount␈α
of␈α
information␈α
concerning␈α
current␈α
bindings␈α
and
␈↓ ↓H␈↓return␈α⊂addresses.␈α⊂Most␈α⊂LISP␈α⊂interpreters␈α⊂also␈α∂keep␈α⊂additional␈α⊂information␈α⊂which␈α⊂is␈α⊂useful␈α∂for
␈↓ ↓H␈↓explaining␈α
to␈α
the␈α
user␈α
what␈αthe␈α
state␈α
of␈α
a␈α
computation␈αwas␈α
when␈α
something␈α
went␈α
wrong.␈α  We␈α
will
␈↓ ↓H␈↓say␈αmore␈αabout␈αwhat␈αinformation␈αmight␈αbe␈αkept␈αon␈αstacks␈αand␈αhow␈αit␈αis␈αused␈αin␈αthe␈αlater␈αsections
␈↓ ↓H␈↓on␈αerror␈αhandling␈αand␈αdebugging.␈α The␈αsubject␈αwill␈αalso␈αarise␈αwhen␈αwe␈αdiscuss␈αcontrol␈αstructures
␈↓ ↓H␈↓in general (Chapter ).


␈↓ ↓H␈↓αOther LISP objects.

␈↓ ↓H␈↓        The␈α∞structures␈α∂and␈α∞operations␈α∞we␈α∂have␈α∞discussed␈α∂above␈α∞are␈α∞sufficient␈α∂for␈α∞a␈α∂LISP␈α∞system
␈↓ ↓H␈↓that␈α∂is␈α∂only␈α∂going␈α∂to␈α∂deal␈α∂with␈α∂interpreted␈α∂code␈α∂and␈α∂will␈α∂only␈α∂allow␈α∂S-expressions␈α∂as␈α∂data␈α∞for
␈↓ ↓H␈↓114␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓programs.␈α⊃ Most␈α⊃systems␈α⊃provide␈α⊃at␈α⊃least␈α⊃two␈α⊂additional␈α⊃features.␈α⊃  One␈α⊃is␈α⊃the␈α⊃ability␈α⊃to␈α⊂run
␈↓ ↓H␈↓compiled␈α
code,␈αand␈α
the␈αother␈α
is␈αthe␈α
addition␈αof␈α
arrays␈α
to␈αthe␈α
allowed␈αtypes␈α
of␈αdata.␈α
  Thus␈αa␈α
space
␈↓ ↓H␈↓known␈αas␈αbinary␈αprogram␈αspace␈αis␈αusually␈αset␈αaside␈αfor␈αstoring␈αcompiled␈αcode.␈α  Sometimes␈αarrays
␈↓ ↓H␈↓are␈αkept␈αin␈αthe␈αsame␈αspace,␈αand␈αsometimes␈αthey␈αare␈αkept␈αin␈αan␈αadditional␈αseparate␈αspace.␈α In␈αany
␈↓ ↓H␈↓event,␈αthis␈αmeans␈αthat␈αthere␈αare␈αtwo␈αadditional␈αobjects␈αthat␈αsome␈αof␈αthe␈αbasic␈αLISP␈αroutines␈αmust
␈↓ ↓H␈↓recognize␈α
and␈α∞deal␈α
with.␈α
 One␈α∞is␈α
the␈α∞"subr"␈α
object␈α
which␈α∞is␈α
the␈α
address␈α∞of␈α
a␈α∞compiled␈α
program
␈↓ ↓H␈↓and␈α∞is␈α∞associated␈α∞with␈α∞the␈α∞atom␈α∞naming␈α∞the␈α∞program␈α∞ via␈α∞the␈α∞␈↓¬SUBR␈α∞␈↓property.␈α∞ The␈α∞other␈α∞is␈α∞an
␈↓ ↓H␈↓array␈α⊂descriptor␈α⊂which␈α⊂will␈α⊂be␈α⊂associated␈α⊃via␈α⊂the␈α⊂␈↓¬ARRAY␈α⊂␈↓property␈α⊂with␈α⊂the␈α⊂atom␈α⊃naming␈α⊂that
␈↓ ↓H␈↓array.



␈↓ ↓H␈↓2.  ␈↓αThe Top level of LISP.␈↓


␈↓ ↓H␈↓        The␈α
top␈α
level␈αof␈α
LISP␈α
can␈αbe␈α
described␈α
in␈αterms␈α
of␈α
three␈αfunctions␈α
␈↓↓read,␈↓␈α
␈↓↓eval␈↓␈α
and␈α␈↓↓print.␈↓
␈↓ ↓H␈↓The␈α∞␈↓↓read␈↓␈α∞function␈α∞gets␈α∂the␈α∞next␈α∞string␈α∞of␈α∞tokens␈α∂that␈α∞corresponds␈α∞to␈α∞an␈α∞S-expression␈α∂from␈α∞the
␈↓ ↓H␈↓input␈α⊂source,␈α⊂constructs␈α⊃the␈α⊂correct␈α⊂piece␈α⊂of␈α⊃list␈α⊂structure␈α⊂and␈α⊂returns␈α⊃the␈α⊂result.␈α⊂ ␈↓↓print␈↓␈α⊃is␈α⊂the
␈↓ ↓H␈↓inverse␈α⊂to␈α⊃read␈α⊂in␈α⊂that␈α⊃it␈α⊂takes␈α⊂an␈α⊃S-expression␈α⊂and␈α⊂sends␈α⊃to␈α⊂the␈α⊂current␈α⊃output␈α⊂a␈α⊃string␈α⊂of
␈↓ ↓H␈↓characters␈αwhich␈α
if␈αread␈αby␈α
␈↓↓read␈↓␈αwould␈α
in␈αfact␈αproduce␈α
and␈α␈↓↓equal␈↓␈α
S-expression.␈α A␈αsimple␈α
version
␈↓ ↓H␈↓of the LISP top level can be described by the program

␈↓ ↓H␈↓␈↓ β\␈↓↓toplevel[] ← ␈↓αprogram␈↓↓[[] top: print eval read[]; ␈↓αgo to␈↓↓ top]␈↓. 

␈↓ ↓H␈↓Here␈α
we␈αare␈α
assuming␈α
(as␈αis␈α
the␈αusual␈α
case)␈α
that␈α␈↓↓eval␈↓␈α
uses␈αthe␈α
global␈α
state␈αof␈α
the␈α
control␈αstack(s)
␈↓ ↓H␈↓and property lists rather than an explicit alist.

␈↓ ↓H␈↓        We␈αhave␈αalready␈αdiscussed␈αhow␈αa␈αsimplified␈αversion␈αof␈α␈↓↓eval␈↓␈αworks␈α(Chapter␈αI␈α␈↓π∞␈↓13).␈α In␈αthe
␈↓ ↓H␈↓remainder␈αof␈αthis␈αsection␈αwe␈αwill␈αdiscuss␈αthe␈αreading␈αand␈αprinting␈αof␈αS-expressions.␈α Imagine␈αthat
␈↓ ↓H␈↓there␈α∞is␈α
a␈α∞scanner␈α
that␈α∞preprocesses␈α
input␈α∞and␈α
returns␈α∞a␈α
list␈α∞of␈α
tokens␈α∞which␈α
are␈α∞represented␈α
as
␈↓ ↓H␈↓atoms.␈α
 We␈α
will␈α
let␈α∞the␈α
special␈α
atoms␈α
␈↓¬LP,␈α∞␈↓␈↓¬RP␈α
␈↓and␈α
␈↓¬DOT␈α
␈↓represent␈α∞the␈α
delimiter␈α
tokens␈α
"(",␈α∞")"␈α
and
␈↓ ↓H␈↓"."␈αrespectively␈αand␈αall␈αother␈αatoms␈αwill␈αrepresent␈αthemselves.␈α Then␈αthe␈αrecursive␈αstructure␈αof␈αthe
␈↓ ↓H␈↓LISP␈αprinting␈αroutine␈αcan␈αbe␈αdescribed␈αby␈αa␈αprogram␈αthat␈αtakes␈αan␈αS-expression␈αand␈αproduces␈αa
␈↓ ↓H␈↓list␈αof␈αtokens␈αcorresponding␈αto␈αthe␈αlinear␈αrepresentation.␈α Similarly␈αthe␈αreader␈αcan␈αbe␈αdescribed␈αby
␈↓ ↓H␈↓a␈α
program␈αthat␈α
takes␈α
a␈αlist␈α
of␈α
tokens␈αcorresponding␈α
to␈α
the␈αlinear␈α
representation␈α
of␈αan␈α
S-expression
␈↓ ↓H␈↓and constructs the internal representation.

␈↓ ↓H␈↓        The␈αprogram␈α␈↓↓prindot␈↓␈α"print"s␈αan␈αS-expression␈αin␈α"dot"␈αnotation.␈α The␈αdefinition␈αof␈α␈↓↓prindot␈↓
␈↓ ↓H␈↓is:

␈↓ ↓H␈↓↓2.1)␈↓ β8prindot[e] ← prina[e,␈↓¬NIL␈↓↓]

␈↓ ↓H␈↓↓␈↓ β8prina[e, l] ←
␈↓ ↓H␈↓↓2.2)␈↓ βx␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ e . l
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ ␈↓¬LP␈↓↓ . prina[␈↓αa|␈↓↓e, ␈↓¬DOT␈↓↓ . prina[␈↓αd|␈↓↓e, ␈↓¬RP␈↓↓ . l]]


␈↓ ↓H␈↓thus
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠115



␈↓ ↓H␈↓␈↓ β8␈↓↓prindot[␈↓¬(PLUS (TIMES A B) C)␈↓↓] = ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓¬(LP PLUS DOT LP LP TIMES DOT LP A DOT LP B ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓¬ DOT NIL RP RP RP DOT LP C DOT NIL RP RP RP),␈↓

␈↓ ↓H␈↓        The␈α∂program␈α∂␈↓↓prinlis␈↓␈α∞"print"s␈α∂an␈α∂S-expression␈α∞in␈α∂"list"␈α∂notation.␈α∞ In␈α∂the␈α∂non-atomic␈α∞case,
␈↓ ↓H␈↓this␈αis␈αdone␈α by␈αstarting␈αwith␈α␈↓¬LP,␈α␈↓␈α␈↓↓cdr␈↓ing␈αthrough␈αthe␈αS-expression␈αprinting␈αthe␈α␈↓↓car␈↓␈αat␈αeach␈αlevel
␈↓ ↓H␈↓until␈αan␈αatom␈αis␈αreached.␈α If␈αthis␈αatom␈αis␈α␈↓¬NIL␈↓␈αthe␈αS-expression␈αis␈αindeed␈αa␈αlist␈αand␈αthe␈αprinting␈αis
␈↓ ↓H␈↓terminated␈α∀with␈α∪␈↓¬RP.␈α∀␈↓Otherwise␈α∀the␈α∪printing␈α∀is␈α∪terminated␈α∀with␈α∀␈↓¬DOT␈α∪␈↓followed␈α∀by␈α∀the␈α∪atom
␈↓ ↓H␈↓followed by ␈↓¬RP. ␈↓ ␈↓↓prinlis␈↓ is defined by:

␈↓ ↓H␈↓↓2.3)␈↓ β8prinlis[e] ← prinb[e,␈↓¬NIL␈↓↓]

␈↓ ↓H␈↓↓␈↓ β8prinb[e, l] ←
␈↓ ↓H␈↓↓␈↓ βx␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ e . l
␈↓ ↓H␈↓↓2.4)␈↓ βx␈↓αelse␈↓↓ ␈↓¬LP␈↓↓ . [␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓e ␈↓αthen␈↓↓ prinb[␈↓αa|␈↓↓e, ␈↓¬RP␈↓↓ . l]
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αd|␈↓↓e ␈↓αthen␈↓↓ prinb[␈↓αa|␈↓↓e, ␈↓¬DOT␈↓↓ . [␈↓αd|␈↓↓e . [␈↓¬RP␈↓↓ . l]]]
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αelse␈↓↓ prinb[␈↓αa|␈↓↓e, ␈↓αd|␈↓↓prinb[␈↓αd|␈↓↓e, l]]]


␈↓ ↓H␈↓thus

␈↓ ↓H␈↓␈↓ αi␈↓↓prinlis[␈↓¬(PLUS (TIMES A B) C)␈↓↓] = ␈↓¬(LP PLUS LP TIMES A B RP C RP).␈↓↓␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ β(␈↓↓     prinlis[␈↓¬(A . ( B . (C . D)))␈↓↓] = ␈↓¬(LP A B C DOT D RP).␈↓↓␈↓


␈↓ ↓H␈↓        Notice␈α∞that␈α∂the␈α∞recursive␈α∂structure␈α∞of␈α∂␈↓↓prindot␈↓␈α∞is␈α∂very␈α∞much␈α∂like␈α∞that␈α∂of␈α∞␈↓↓flatten␈↓␈α∂(I.8.8).␈α∞ It
␈↓ ↓H␈↓simply␈α
inserts␈αthe␈α
delimiter␈α
characters␈αin␈α
the␈α
appropriate␈αplaces.␈α
 ␈↓↓prinlis␈↓␈α
also␈αhas␈α
the␈α
same␈αbasic
␈↓ ↓H␈↓structure,␈α∩but␈α∩makes␈α∩some␈α∩intermediate␈α∩tests␈α∪in␈α∩order␈α∩to␈α∩omit␈α∩all␈α∩but␈α∩the␈α∪essential␈α∩delimiter
␈↓ ↓H␈↓characters.

␈↓ ↓H␈↓        The␈αprogram␈α␈↓↓read␈↓␈αconverts␈α
a␈αlist␈αof␈αsymbols␈α
representing␈αan␈αS-expression␈αin␈α"dot"␈α
notation,
␈↓ ↓H␈↓or␈α∞"list"␈α∂notation␈α∞or␈α∞some␈α∂mixture␈α∞thereof␈α∞into␈α∂the␈α∞corresponding␈α∞S-expression.␈α∂  The␈α∞auxiliary
␈↓ ↓H␈↓function ␈↓↓reada␈↓ uses the auxiliary variable ␈↓↓l␈↓ to stack partial results.  The definitions are as follows:

␈↓ ↓H␈↓↓2.5)␈↓ β8read u ← ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬LP␈↓↓ ␈↓αthen␈↓↓ ␈↓αa|␈↓↓reada[␈↓αd|␈↓↓u, ␈↓¬NIL␈↓↓] ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u


␈↓ ↓H␈↓↓␈↓ β8reada[u, l] ←
␈↓ ↓H␈↓↓␈↓ βx␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ rev1[l, ␈↓¬ERROR␈↓↓] . ␈↓¬NIL␈↓↓
␈↓ ↓H␈↓↓2.6)␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬RP␈↓↓ ␈↓αthen␈↓↓ reverse l . ␈↓αd|␈↓↓u
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬LP␈↓↓ ␈↓αthen␈↓↓ {reada[␈↓αd|␈↓↓u, ␈↓¬NIL␈↓↓]}[λw: reada[␈↓αd|␈↓↓w, ␈↓αa|␈↓↓w . l]]
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬DOT␈↓↓ ␈↓αthen␈↓↓ {reada[␈↓αd|␈↓↓u, ␈↓¬NIL␈↓↓]}[λw: rev1[l, ␈↓αaa|␈↓↓w] . ␈↓αd|␈↓↓w]
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ reada[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . l]

␈↓ ↓H␈↓↓2.7)␈↓ β8rev1[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ rev1[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . v]


␈↓ ↓H␈↓        As␈αwe␈α
mentioned␈αpreviously,␈αthe␈α
actual␈αLISP␈αreading␈α
programs␈αmust␈αdeal␈α
with␈αthe␈αoblist␈α
in
␈↓ ↓H␈↓116␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓order␈α
to␈α
identify␈αthe␈α
atom␈α
having␈αa␈α
given␈α
print␈α
name,␈αpossibly␈α
updating␈α
the␈αoblist␈α
if␈α
the␈αatom␈α
has
␈↓ ↓H␈↓not␈α⊂been␈α⊂seen␈α⊃before␈α⊂and␈α⊂the␈α⊂printing␈α⊃programs␈α⊂must␈α⊂deal␈α⊂with␈α⊃property␈α⊂lists␈α⊂or␈α⊃some␈α⊂other
␈↓ ↓H␈↓mechanism␈αfor␈αobtaining␈αthe␈αprint␈αname␈αof␈αan␈αatom.␈α We␈αhave␈αrelegated␈αthese␈αtasks␈αto␈αthe␈αtoken
␈↓ ↓H␈↓scanner and what ever routine actually prints the string of tokens produced by ␈↓↓prindot␈↓ or ␈↓↓prinlis.␈↓

␈↓ ↓H␈↓        Implicit␈α
in␈α
the␈α
functioning␈α
of␈αthe␈α
LISP␈α
scanner␈α
in␈α
the␈αfact␈α
that␈α
certain␈α
characters␈α
such␈αas
␈↓ ↓H␈↓the␈αdelimiters␈α"(",␈α")",␈α"."␈αand␈α"␈α"␈α(<space>)␈αare␈αtreated␈αspecially␈αand␈αcannot␈αdirectly␈αbe␈αincluded␈αin
␈↓ ↓H␈↓the␈αnames␈αof␈αatoms.␈α LISP␈αsystems␈αusually␈αprovide␈αa␈αway␈αof␈αincluding␈αthese␈αspecial␈αcharacters␈αin
␈↓ ↓H␈↓an␈α⊂atom␈α∂name␈α⊂by␈α⊂designating␈α∂an␈α⊂additional␈α⊂special␈α∂character␈α⊂to␈α⊂serve␈α∂as␈α⊂an␈α⊂escape␈α∂character.
␈↓ ↓H␈↓Thus␈α∞any␈α∞character␈α∞following␈α∞the␈α∞escape␈α∞character␈α∂in␈α∞an␈α∞input␈α∞string␈α∞is␈α∞treated␈α∞as␈α∂an␈α∞ordinary
␈↓ ↓H␈↓letter.␈α∞ This␈α∂know␈α∞as␈α∂"slashifying"␈α∞as␈α∂the␈α∞designated␈α∞escape␈α∂character␈α∞is␈α∂often␈α∞the␈α∂"/"␈α∞character.
␈↓ ↓H␈↓Thus␈α␈↓¬(AB/.D)␈α␈↓is␈αa␈αlist␈αof␈αone␈αelement,␈αthe␈αatom␈αwhose␈αpname␈αis␈α␈↓¬AB.D␈α␈↓␈αwhereas␈α␈↓¬(AB.D)␈α␈↓is␈αa␈αpair,
␈↓ ↓H␈↓the␈αa-part␈αbeing␈α␈↓¬AB␈α␈↓and␈αthe␈αd-part␈αbeing␈α␈↓¬D.␈α␈↓␈α One␈αresult␈αof␈αthis␈α"slashifying"␈αconvention␈αis␈αthat
␈↓ ↓H␈↓many␈α∪of␈α∪the␈α∪pname␈α∩manipulating␈α∪and␈α∪printing␈α∪functions␈α∩have␈α∪several␈α∪flavors␈α∪according␈α∩to
␈↓ ↓H␈↓whether␈α∂the␈α∂name␈α∞or␈α∂the␈α∂slashified␈α∞version␈α∂is␈α∂being␈α∞dealt␈α∂with.␈α∂  We␈α∞mention␈α∂the␈α∂fact␈α∂only␈α∞to
␈↓ ↓H␈↓make␈α
you␈α∞aware␈α
of␈α∞it.␈α
 You␈α
should␈α∞consult␈α
your␈α∞LISP␈α
manual␈α
for␈α∞further␈α
details.␈α∞  You␈α
should
␈↓ ↓H␈↓also␈α
be␈α
aware␈α
of␈α
the␈α∞fact␈α
that␈α
"/"␈α
itself␈α
(or␈α
whatever␈α∞the␈α
designated␈α
escape␈α
character␈α
is)␈α∞being␈α
a
␈↓ ↓H␈↓special␈α
character␈α
is␈α
gobbled␈α
up␈α
by␈α
the␈α
LISP␈αscanner,␈α
thus␈α
if␈α
you␈α
really␈α
want␈α
the␈α
symbol␈α
"/"␈αyou
␈↓ ↓H␈↓must type "//".



␈↓ ↓H␈↓3.  ␈↓αError Handling.␈↓


␈↓ ↓H␈↓        A␈α
robust␈αand␈α
helpful␈α
LISP␈αsystem␈α
will␈α
do␈αall␈α
it␈α
can␈αto␈α
keep␈α
you␈αfrom␈α
destroying␈α
it.␈α This
␈↓ ↓H␈↓includes␈α
noticing␈α
when␈α
you␈α
have␈α∞asked␈α
it␈α
to␈α
evalute␈α
an␈α∞ill␈α
formed␈α
or␈α
illegal␈α
expression.␈α∞ In␈α
this
␈↓ ↓H␈↓class␈αwe␈αinclude␈αattemping␈αto␈αevaluate␈αa␈αvariable␈αthat␈αhasn't␈αbeen␈αbound␈α(␈↓¬SETQ␈↓ed␈αor␈αbound␈αas␈αa
␈↓ ↓H␈↓␈↓¬LAMBDA␈α␈↓or␈α␈↓¬PROG␈α␈↓variable),␈αtaking␈αthe␈α␈↓↓car␈↓␈αor␈α␈↓↓cdr␈↓␈αof␈αan␈αatom␈αor␈αother␈αnon␈αlist␈αstructure␈αobject␈αsuch
␈↓ ↓H␈↓as␈αan␈αarray␈αpointer,␈αattempting␈αto␈αappend␈αa␈αnon-list␈αto␈αsome␈αS-expression,␈αapplying␈αa␈αfunction␈αto
␈↓ ↓H␈↓the␈α
wrong␈α
number␈α
of␈α
arguments,␈α
etc..␈α
 When␈αsuch␈α
an␈α
error␈α
is␈α
noticed␈α
LISP␈α
will␈αcomplain,␈α
printing
␈↓ ↓H␈↓a␈α∞(hopefully␈α∞informative)␈α∞message,␈α
and␈α∞take␈α∞some␈α∞appropriate␈α
action.␈α∞  Other␈α∞errors␈α∞such␈α∞as␈α
an
␈↓ ↓H␈↓infinite␈αrecursion␈α
can't␈αbe␈α
prevented␈αby␈α
inspection␈αof␈α
the␈αexpression␈α
to␈αbe␈α
evaluated,␈αbut␈α
will␈αbe
␈↓ ↓H␈↓noticed␈α∂when␈α⊂some␈α∂abnormal␈α∂state␈α⊂is␈α∂reached␈α∂such␈α⊂as␈α∂when␈α∂the␈α⊂control␈α∂stack␈α∂overflows␈α⊂or␈α∂an
␈↓ ↓H␈↓attempt␈αis␈α
made␈αto␈α
read␈αor␈αwrite␈α
in␈αan␈α
illegal␈αlocation.␈α Again␈α
LISP␈αwill␈α
complain␈αby␈α
printing␈αa
␈↓ ↓H␈↓message␈α∂saying␈α∂what␈α∂abnormal␈α∂condition␈α∂was␈α∂noticed␈α∂and␈α∂take␈α∂appropriate␈α∂action.␈α∂  Of␈α∞course
␈↓ ↓H␈↓there␈αis␈α
always␈αthe␈α
possibility␈αof␈αa␈α
truly␈αdisastrous␈α
error␈αfrom␈αwhich␈α
there␈αis␈α
no␈αrecovery␈αand␈α
here
␈↓ ↓H␈↓there␈α
is␈αnot␈α
much␈αto␈α
do␈αbut␈α
restart␈αand␈α
proceed␈α
with␈αcaution.␈α
 The␈αquestion␈α
remains␈αas␈α
to␈αwhat␈α
to
␈↓ ↓H␈↓do␈α
when␈α
an␈α
error␈α
is␈α∞noticed.␈α
 Typically␈α
LISP␈α
will␈α
enter␈α∞a␈α
state␈α
where␈α
a␈α
debugging␈α∞package␈α
can
␈↓ ↓H␈↓take␈α∩over␈α⊃and␈α∩help␈α∩you␈α⊃find␈α∩out␈α⊃what␈α∩is␈α∩amiss.␈α⊃ In␈α∩this␈α⊃state␈α∩you␈α∩will␈α⊃be␈α∩able␈α∩to␈α⊃evaluate
␈↓ ↓H␈↓expressions,␈α∩possibly␈α∩back␈α∩up␈α∩the␈α∩computation␈α∩some␈α∩number␈α∩of␈α∩steps,␈α∩have␈α∩the␈α⊃computation
␈↓ ↓H␈↓proceed␈α
(after␈α
providing␈α
whatever␈α
information␈α
was␈αlacking␈α
and␈α
caused␈α
the␈α
error)␈α
or␈αabandon␈α
ship
␈↓ ↓H␈↓and return to the top level to start again.

␈↓ ↓H␈↓        In␈α
order␈α
to␈α
aid␈α
in␈α
error␈α
recovery␈α
and␈α
generally␈α
provide␈α
the␈α
user␈α
with␈α
the␈α
ability␈α
to␈αaccess
␈↓ ↓H␈↓information␈α
about␈α
the␈α
state␈α
of␈α
a␈α
computation,␈α
LISP␈α
provides␈α
various␈α
means␈α
of␈α
altering␈αthe␈α
normal
␈↓ ↓H␈↓progress␈α
of␈α∞evaluation␈α
and␈α∞examining␈α
and␈α∞modifying␈α
the␈α∞environment.␈α
 One␈α∞such␈α
means␈α∞is␈α
the
␈↓ ↓H␈↓break␈αloop.␈α Evaluating␈α
␈↓↓break[name,test]␈↓␈αcauses␈αLISP␈α
to␈αevaluate␈α␈↓↓test␈↓␈αand␈α
if␈αit␈αis␈α
not␈α␈↓¬NIL␈↓␈αto␈αgo␈α
in
␈↓ ↓H␈↓to␈αa␈αbreak␈αloop.␈α
 The␈α␈↓↓name␈↓␈αargument␈αis␈α
printed␈αout␈αas␈αa␈α
message␈αand␈αthen␈αa␈α
pseudo␈αread-eval-
␈↓ ↓H␈↓print␈αloop␈α
is␈αentered,␈αwhere␈α
the␈αexpression␈αread␈α
is␈αtested␈αto␈α
see␈αif␈αit␈α
is␈αone␈αof␈α
a␈αfew␈αspecial␈α
"escape
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠117


␈↓ ↓H␈↓from␈α∂the␈α∂loop"␈α∂commands.␈α∂ If␈α∞so␈α∂the␈α∂loop␈α∂is␈α∂exited␈α∞in␈α∂the␈α∂manner␈α∂indicated␈α∂by␈α∂the␈α∞command.
␈↓ ↓H␈↓Otherwise␈αthe␈αform␈αis␈αevaled,␈αprinted␈αand␈αwe␈αare␈αback␈αat␈αthe␈αtop␈αof␈αthe␈αloop.␈α There␈αare␈αat␈αleast
␈↓ ↓H␈↓two␈αmodes␈αof␈αreturn␈αfrom␈αa␈αbreak␈αloop.␈α One␈αis␈αjust␈αto␈αreturn␈αto␈αthe␈αtop␈αlevel,␈αaborting␈αwhatever
␈↓ ↓H␈↓computation␈α
the␈αbreak␈α
occured␈α
in,␈α and␈α
the␈α
other␈αis␈α
to␈α
resume␈αthe␈α
computation␈α
(possibly␈αhaving
␈↓ ↓H␈↓altered the state of the world before doing so).

␈↓ ↓H␈↓        To␈αget␈αa␈αcomplete␈αpicture␈αof␈αthe␈αstate␈αof␈αthe␈αcomputation␈αit␈αis␈αuseful␈αto␈αknow␈αthe␈αsequence
␈↓ ↓H␈↓of␈αevents␈αthat␈αled␈αup␈αto␈αthe␈αcurrent␈αstate.␈α This␈αis␈αmade␈αpossible␈αby␈αstacking␈αsuch␈αinformation␈αas
␈↓ ↓H␈↓evaluation␈α∪proceeds.␈α∪ Just␈α∪how␈α∪this␈α∪is␈α∪done␈α∪and␈α∪what␈α∪information␈α∪is␈α∪kept␈α∪depends␈α∀on␈α∪the
␈↓ ↓H␈↓particular␈α∃implementation.␈α∃ For␈α∃the␈α∃sake␈α∃of␈α∃example␈α∃we␈α∃will␈α∃base␈α∃our␈α∃description␈α∃on␈α∃the
␈↓ ↓H␈↓MACLISP␈αimplementation.␈α
 Imagine␈αthat␈α
there␈αis␈αa␈α
stack␈α(called␈α
the␈αspecpdl)␈α
where␈αeach␈αtime␈α
the
␈↓ ↓H␈↓interpreter␈αbegins␈αto␈αevaluate␈αan␈αS-expression␈αit␈αpushes␈αonto␈αthe␈αstack␈αthe␈αfollowing␈αinformation:
␈↓ ↓H␈↓the␈α
current␈α
specpdl␈α
pointer,␈α
the␈α
expression␈α
to␈α
be␈α
evaluated,␈α
an␈α
a␈α
pointer␈α
to␈α
the␈α
current␈α
variable
␈↓ ↓H␈↓binding␈α∂environment,␈α⊂(MACLISPs␈α∂version␈α∂of␈α⊂the␈α∂a-list␈α∂argument␈α⊂to␈α∂␈↓↓eval).␈↓␈α∂ The␈α⊂current␈α∂stack
␈↓ ↓H␈↓pointer␈α⊂is␈α∂then␈α⊂reset␈α⊂to␈α∂the␈α⊂new␈α∂stack␈α⊂top␈α⊂and␈α∂evaluation␈α⊂continues.␈α∂ When␈α⊂evaluation␈α⊂of␈α∂the
␈↓ ↓H␈↓expression␈α∂is␈α∞complete␈α∂that␈α∂collection␈α∞of␈α∂information␈α∂is␈α∞popped␈α∂from␈α∂the␈α∞stack.␈α∂ There␈α∂are␈α∞two
␈↓ ↓H␈↓primitive␈α∀operations␈α∃associated␈α∀with␈α∀this␈α∃stack.␈α∀ One,␈α∀␈↓↓evalframe[pdl],␈↓␈α∃returns␈α∀a␈α∀list␈α∃of␈α∀the
␈↓ ↓H␈↓information␈αat␈αthe␈αstack␈αposition␈αindicated␈αby␈αpdl.␈α Thus␈αwe␈αcan␈αuse␈αthis␈αinformation␈αto␈αmove␈αup
␈↓ ↓H␈↓and␈α⊃down␈α⊃the␈α⊃stack␈α⊂and␈α⊃evaluate␈α⊃expressions␈α⊃in␈α⊂various␈α⊃environments.␈α⊃ The␈α⊃other␈α⊂operation
␈↓ ↓H␈↓causes␈α⊂the␈α⊂interpreter␈α⊂to␈α⊂change␈α⊂its␈α⊂notion␈α⊂about␈α⊂the␈α⊂current␈α⊂top␈α⊂of␈α⊂the␈α⊂stack.␈α⊂ This␈α⊂is␈α∂called
␈↓ ↓H␈↓␈↓↓freturn␈↓␈α∩(for␈α⊃forced␈α∩return).␈α⊃ If␈α∩␈↓↓freturn[pdl,exp]␈↓␈α⊃is␈α∩evaluated␈α⊃then␈α∩the␈α⊃interpreter␈α∩behaves␈α⊃as
␈↓ ↓H␈↓though␈α∂␈↓↓pdl␈↓␈α⊂were␈α∂the␈α∂top␈α⊂of␈α∂the␈α∂stack␈α⊂and␈α∂the␈α∂value␈α⊂of␈α∂␈↓↓exp␈↓␈α∂were␈α⊂the␈α∂result␈α∂of␈α⊂evaluating␈α∂the
␈↓ ↓H␈↓expression␈α
stored␈αthere.␈α
 It␈α
thus␈α"returns"␈α
from␈α
that␈αstate␈α
and␈α
proceeds.␈α This␈α
is␈α
a␈αhighly␈α
non-local
␈↓ ↓H␈↓form␈α∂of␈α∂␈↓αgo to␈↓␈α∂and␈α∞should␈α∂be␈α∂used␈α∂with␈α∞caution,␈α∂however␈α∂it␈α∂is␈α∞most␈α∂useful␈α∂for␈α∂building␈α∞error-
␈↓ ↓H␈↓recovery and debugging routines.

␈↓ ↓H␈↓        Finally␈α⊃there␈α⊂are␈α⊃primitives␈α⊂for␈α⊃inducing␈α⊃and␈α⊂trapping␈α⊃errors.␈α⊂ The␈α⊃main␈α⊃primitives␈α⊂of
␈↓ ↓H␈↓interest␈αare␈αthe␈αpair␈αof␈αfunctions␈α␈↓↓error␈↓␈αand␈α␈↓↓errset.␈↓␈α  In␈αthe␈αsimplest␈αcase␈α␈↓↓error␈↓␈αtakes␈αone␈αargument
␈↓ ↓H␈↓which␈αis␈α
a␈αmessage␈α
to␈αbe␈α
printed.␈α Evaluation␈α of␈α
␈↓↓error␈αmsg␈↓␈α
 causes␈αthe␈α
value␈αof␈α
␈↓↓msg␈↓␈αto␈αbe␈α
printed
␈↓ ↓H␈↓and␈αa␈α
LISP␈αerror␈α
to␈αbe␈α
signaled.␈α The␈α
result␈αof␈αsignalling␈α
an␈αerror␈α
is␈αpopping␈α
up␈αto␈α
the␈αnearest
␈↓ ↓H␈↓␈↓↓errset␈↓␈α
or␈α
to␈α
the␈α
top␈α
level␈α
of␈α
LISP␈α
if␈α
there␈α
is␈α
no␈α
enclosing␈α
␈↓↓errset.␈↓␈α
 The␈α
value␈α
returned␈α
is␈α
␈↓¬NIL␈↓.␈α
 ␈↓↓errset␈↓
␈↓ ↓H␈↓has␈αtwo␈αarguments,␈α
the␈αfirst␈αis␈α
an␈αexpression␈αto␈α
be␈αevaluated␈αand␈α
the␈αsecond␈αis␈α
a␈αflag.␈α If␈αan␈α
error
␈↓ ↓H␈↓occurs␈αwhile␈αevaluating␈αthe␈αexpression,␈αthe␈α␈↓↓errset␈↓␈α"traps"␈αthe␈αerror␈αand␈αreturns␈αthe␈αvalue␈αreturned
␈↓ ↓H␈↓by␈α
the␈α
error␈αfunction,␈α
otherwise␈α
␈↓↓errset␈↓␈αreturns␈α
a␈α
list␈α
of␈αone␈α
element,␈α
the␈αvalue␈α
of␈α
the␈αexpression.␈α
 If
␈↓ ↓H␈↓the␈α
flag␈α
is␈α
off␈α(eg.␈α
if␈α
it␈α
is␈α
␈↓¬NIL␈↓)␈αthen␈α
no␈α
error␈α
messages␈α
are␈αprinted.␈α
 The␈α
reason␈α
for␈α␈↓↓errset␈↓␈α
returning
␈↓ ↓H␈↓a␈αlist␈αcontaining␈αthe␈αvalue␈αof␈αthe␈αsuccessfully␈αevaluated␈αexpression␈αis␈αto␈αdistinguish␈αthe␈αvalue␈α␈↓¬NIL␈↓
␈↓ ↓H␈↓from␈αthe␈αatom␈α␈↓¬NIL␈↓␈αwhich␈αsays␈αthat␈αan␈αerror␈αhas␈αoccurred.␈α An␈αalternate␈αform␈αof␈αerror␈αgeneration
␈↓ ↓H␈↓is␈α␈↓↓err␈↓␈αwhich␈αtakes␈αa␈αsingle␈αargument.␈α Evaluation␈αof␈α␈↓↓err␈αx␈↓␈αcauses␈αan␈αerror␈αto␈αbe␈αsignaled␈αand␈αthe
␈↓ ↓H␈↓value␈α
of␈α
␈↓↓x␈↓␈α∞is␈α
returned.␈α
 There␈α
are␈α∞many␈α
variations␈α
and␈α
extensions␈α∞of␈α
the␈α
above␈α∞error␈α
functions
␈↓ ↓H␈↓which you should learn about for your particular implementation of LISP.

␈↓ ↓H␈↓        In␈α∞addition␈α
to␈α∞trapping␈α∞errors,␈α
the␈α∞␈↓↓err,␈↓␈α
␈↓↓errset␈↓␈α∞mechanism␈α∞can␈α
be␈α∞used␈α
to␈α∞return␈α∞from␈α
the
␈↓ ↓H␈↓midst␈α∞of␈α
a␈α∞computation␈α
when␈α∞the␈α
answer␈α∞has␈α
been␈α∞found␈α
and␈α∞it␈α
is␈α∞not␈α
necessary␈α∞to␈α∞process␈α
the
␈↓ ↓H␈↓result␈αfurther.␈α
  For␈αexample␈α
if␈αyou␈α
are␈αlooking␈αfor␈α
a␈αparticular␈α
atom␈αin␈α
an␈αS-expression␈αby␈α
doing
␈↓ ↓H␈↓a␈αrecursive␈αsearch,␈αthen␈αif␈αthe␈αatom␈αis␈αfound,␈αexecuting␈α␈↓↓err␈↓␈αpops␈αdirectly␈αback␈αto␈αthe␈αcalling␈α␈↓↓errset␈↓
␈↓ ↓H␈↓without␈αdoing␈α
the␈αintermediate␈αbook␈α
keeping␈αnecessary␈α
to␈αreturn␈αthrough␈α
many␈αlevels␈αof␈α
recursive
␈↓ ↓H␈↓calls.␈α⊃ MACLISP␈α⊃provides␈α⊃a␈α⊃"structured"␈α⊃form␈α⊃of␈α⊃such␈α⊃non-local␈α⊃returns␈α⊃in␈α⊃the␈α⊃form␈α⊃of␈α⊃the
␈↓ ↓H␈↓␈↓↓catch/throw␈↓␈α
construct.␈α
 Each␈α
takes␈α
two␈α
arguments␈α
an␈α
expression␈α
to␈α
evaluate␈α
and␈α
a␈α
tag.␈α
 ␈↓↓catch[e,tag]␈↓
␈↓ ↓H␈↓is␈α∂evaluated␈α∂by␈α∞evaluating␈α∂␈↓↓e.␈↓␈α∂  If␈α∞an␈α∂expression␈α∂of␈α∞the␈α∂form␈α∂␈↓↓throw[x,tag1]␈↓␈α∞is␈α∂evaluated␈α∂in␈α∞the
␈↓ ↓H␈↓course␈αof␈αevauating␈α␈↓↓e␈↓␈α
and␈α␈↓↓tag=tag1␈↓␈αthen␈αthe␈α
evaluation␈αof␈α␈↓↓e␈↓␈αis␈αabandonded␈α
an␈αthe␈αvalue␈αof␈α
␈↓↓x␈↓␈αis
␈↓ ↓H␈↓118␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓returned␈αas␈αthe␈αvalue␈αof␈αthe␈α␈↓↓catch.␈↓␈α If␈αthe␈αevaluation␈αof␈α␈↓↓e␈↓␈αis␈αcompleted␈αthen␈αits␈αvalue␈αis␈αreturned
␈↓ ↓H␈↓as␈α⊃the␈α∩value␈α⊃of␈α⊃the␈α∩␈↓↓catch.␈↓␈α⊃ We␈α⊃will␈α∩have␈α⊃more␈α⊃to␈α∩say␈α⊃about␈α⊃this␈α∩and␈α⊃other␈α∩mechanisms␈α⊃of
␈↓ ↓H␈↓controlling evaluation of expressions in a later chapter.



␈↓ ↓H␈↓4.  ␈↓αDebugging Aids in LISP.␈↓


␈↓ ↓H␈↓        In␈α
some␈α
cases␈αlooking␈α
at␈α
the␈αlist␈α
of␈α
function␈αcalls␈α
leading␈α
up␈αto␈α
an␈α
error␈αand␈α
at␈α
the␈αvalues␈α
of
␈↓ ↓H␈↓some␈α
of␈α
the␈αvariables␈α
is␈α
sufficient␈α
to␈αpin␈α
point␈α
an␈α
error␈αin␈α
a␈α
program,␈α
however␈αit␈α
is␈α
often␈αuseful␈α
to
␈↓ ↓H␈↓have␈αmore␈αpowerful␈αdebugging␈αfacilities␈αavailable.␈α We␈αwill␈αlook␈αat␈αtwo␈αkinds␈αof␈αdebugging␈αaids.
␈↓ ↓H␈↓The␈αfirst␈αis␈αthe␈αsort␈αof␈αdebugging␈αpackage␈αthat␈αmodifies␈αfunction␈αdefinitions␈αto␈αprint␈αinformation
␈↓ ↓H␈↓about␈α∩the␈α∩state␈α⊃of␈α∩the␈α∩computation␈α∩and␈α⊃perhaps␈α∩interact␈α∩with␈α⊃the␈α∩user.␈α∩ This␈α∩includes␈α⊃such
␈↓ ↓H␈↓features␈α
as␈α
tracing␈α
and␈α
breaking.␈α
 The␈α
second␈α
is␈α
the␈α
sort␈α
of␈α
program␈α
that␈α
will␈α
take␈α
over␈α
after␈α
a
␈↓ ↓H␈↓break loop has been entered and help you figure out what has happened.

␈↓ ↓H␈↓        When␈α⊂a␈α⊂function␈α⊂is␈α⊂traced,␈α⊂each␈α⊂time␈α⊂it␈α⊂is␈α⊂called␈α⊂information␈α⊂is␈α⊂printed␈α⊂out␈α⊃giving␈α⊂the
␈↓ ↓H␈↓function␈α∞name␈α
and␈α∞the␈α
values␈α∞of␈α
the␈α∞arguments.␈α
 When␈α∞it␈α
returns␈α∞a␈α
value␈α∞an␈α
additional␈α∞line␈α
is
␈↓ ↓H␈↓printed␈αout␈αgiving␈αthe␈αname␈αof␈αthe␈αfunction␈αand␈αthe␈αvalue␈αbeing␈αreturned.␈α In␈αorder␈αto␈αmake␈αthe
␈↓ ↓H␈↓output␈α∞somewhat␈α∞more␈α∞readable,␈α∂the␈α∞printed␈α∞lines␈α∞could␈α∞be␈α∂indented␈α∞according␈α∞to␈α∞the␈α∂depth␈α∞of
␈↓ ↓H␈↓nesting␈α
of␈α
calls␈α
to␈α
a␈αtraced␈α
function.␈α
  To␈α
trace␈α
a␈αfunction␈α
one␈α
replaces␈α
the␈α
the␈α
original␈αfunction
␈↓ ↓H␈↓definition␈α⊂with␈α∂a␈α⊂program␈α∂that␈α⊂does␈α⊂the␈α∂initial␈α⊂printing,␈α∂applies␈α⊂the␈α∂original␈α⊂definition␈α⊂to␈α∂the
␈↓ ↓H␈↓arguments,␈α∞does␈α∞the␈α∞final␈α
printing␈α∞and␈α∞returns␈α∞the␈α∞value.␈α
 This␈α∞program␈α∞is␈α∞also␈α∞responsible␈α
for
␈↓ ↓H␈↓updating any global variables that are used to keep track of the recursion depth of a call.

␈↓ ↓H␈↓        Thus we could build a small trace package using the following programs.

␈↓ ↓H␈↓␈↓ β8␈↓↓trace fn ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αprogram␈↓↓ [def]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αif␈↓↓ get[fn, ␈↓¬TRACED␈↓↓] ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓¬ALREADY-TRACED␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓def ← get[fn, ␈↓¬EXPR␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓def ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓¬NOT-DEFINED␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αif␈↓↓ ¬[␈↓αa|␈↓↓def = ␈↓¬LAMBDA␈↓↓] ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓¬NOT-LAMBDA␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓putprop[fn, ␈↓¬T␈↓↓, ␈↓¬TRACED␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓putprop[fn, def, ␈↓¬!OLDDEF␈↓↓]␈↓
␈↓ ↓H␈↓4.1)␈↓ ∧8␈↓↓putprop[fn, ␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓subst[fn, ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓↓␈↓¬?FN␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓↓subst[␈↓αad|␈↓↓def, ␈↓
␈↓ ↓H␈↓␈↓ ε_␈↓↓␈↓¬?ARGS␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ ε_␈↓↓subst[␈↓¬LIST␈↓↓ . ␈↓αad|␈↓↓def, ␈↓
␈↓ ↓H␈↓␈↓ εX␈↓↓␈↓¬*ARGS␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ εX␈↓↓get[␈↓¬!TRACE␈↓↓, ␈↓¬!PATTERN␈↓↓]]]], ␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓␈↓¬EXPR␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αreturn␈↓↓ ␈↓¬OK␈↓↓␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓untrace fn ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αprogram␈↓↓ [def]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αif␈↓↓ ¬get[fn, ␈↓¬TRACED␈↓↓] ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓¬NOT-TRACED␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓def ← get[fn, ␈↓¬!OLDDEF␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠119


␈↓ ↓H␈↓4.2)␈↓ ∧8␈↓↓putprop[fn, ␈↓¬NIL␈↓↓, ␈↓¬TRACED␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓remprop[fn, ␈↓¬!OLDDEF␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓putprop[fn, def, ␈↓¬EXPR␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αreturn␈↓↓ ␈↓¬OK␈↓↓␈↓


␈↓ ↓H␈↓In order for the ␈↓↓trace␈↓ routine to work we define the ␈↓¬!PATTERN␈↓ property by evaluating

␈↓ ↓H␈↓¬␈↓↓defprop[␈↓¬!TRACE,
␈↓ ↓H␈↓¬        (LAMBDA ?ARGS
␈↓ ↓H␈↓¬          (PROG (!VAL)
␈↓ ↓H␈↓¬            (TERPRI) (MARKS !ILEVEL)
␈↓ ↓H␈↓¬            (PRINC (QUOTE Entering )) (PRINC (QUOTE ?FN))
␈↓ ↓H␈↓¬            (PROG (ARGL)
␈↓ ↓H␈↓¬              (SETQ ARGL (QUOTE ?ARGS))
␈↓ ↓H␈↓¬              L1
␈↓ ↓H␈↓¬              (COND ((NULL ARGL) (RETURN NIL)))
␈↓ ↓H␈↓¬              (TERPRI) (INDENT (PLUS !ILEVEL 2))
␈↓ ↓H␈↓¬              (PRINC (CAR ARGL)) (PRINC (QUOTE  = )) (PRINC (EVAL (CAR ARGL)))
␈↓ ↓H␈↓¬              (SETQ ARGL (CDR ARGL))
␈↓ ↓H␈↓¬              (GO L1))
␈↓ ↓H␈↓¬            (SETQ !VAL
␈↓ ↓H␈↓¬                  ((LAMBDA (!ILEVEL)
␈↓ ↓H␈↓¬                     (APPLY (GET (QUOTE ?FN) (QUOTE !OLDDEF)) *ARGS))
␈↓ ↓H␈↓¬                   (ADD1 !ILEVEL)))
␈↓ ↓H␈↓¬            (TERPRI) (INDENT !ILEVEL)
␈↓ ↓H␈↓¬            (PRINC (QUOTE Returning from )) (PRINC (QUOTE ?FN))
␈↓ ↓H␈↓¬            (PRINC (QUOTE  with )) (PRINC !VAL)
␈↓ ↓H␈↓¬            (TERPRI)
␈↓ ↓H␈↓¬            (RETURN !VAL))),
␈↓ ↓H␈↓¬        !PATTERN␈↓↓]␈↓¬

␈↓ ↓H␈↓The␈αprograms␈α␈↓↓indent␈↓␈αand␈α␈↓↓marks␈↓␈αare␈αused␈αby␈αa␈αtraced␈αprogram␈αto␈αprint␈α␈↓↓n␈↓␈αblanks␈αor␈α␈↓↓n␈↓␈αmarks␈αsay
␈↓ ↓H␈↓">".  We give the definition of ␈↓↓indent.␈↓  The definition of ␈↓↓marks␈↓ is similiar.

␈↓ ↓H␈↓↓␈↓ β8indent n ←
␈↓ ↓H␈↓↓␈↓ βx␈↓αprogram␈↓↓[i]
␈↓ ↓H␈↓↓␈↓ ∧8i←1
␈↓ ↓H␈↓↓␈↓ βxloop
␈↓ ↓H␈↓↓4.3)␈↓ ∧8␈↓αif␈↓↓ i>n ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓¬NIL␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8princ " "
␈↓ ↓H␈↓↓␈↓ ∧8i←i+1
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αgo to␈↓↓ loop

␈↓ ↓H␈↓Before evaluating a traced function it is necessary to initialize the global variable ␈↓¬!ILEVEL␈↓.

␈↓ ↓H␈↓        The␈αother␈αcase␈αof␈αdebugging␈αby␈αmodification␈αof␈αfunction␈αdefinitions␈αis␈αbreaking.␈α The␈αidea
␈↓ ↓H␈↓is␈αto␈αmodify␈αa␈αfunction␈αby␈αputting␈α"break␈αpoints"␈αin␈αthe␈αcode.␈α The␈αbreaks␈αmay␈αbe␈αconditional␈αor
␈↓ ↓H␈↓unconditional,␈αthey␈αmay␈αbe␈αinserted␈αat␈αthe␈αbeginning␈αor␈αbefore␈αor␈αafter␈αcertain␈αexpressions␈αin␈αthe
␈↓ ↓H␈↓code.␈α
 Thus␈α
we␈α
might␈αhave␈α
a␈α
program␈α
␈↓↓mkbreaks[fn,clauses]␈↓␈αwhich␈α
saves␈α
the␈α
original␈αdefinition␈α
of
␈↓ ↓H␈↓␈↓↓fn,␈↓␈αsets␈αa␈αflag␈αsaying␈αit␈αis␈αbroken,␈αmakes␈αthe␈αmodifications␈αindicated␈αby␈αthe␈αlist␈αof␈αbreak␈αclauses,
␈↓ ↓H␈↓and␈α
makes␈α
the␈α
new␈α
definition.␈α
 Each␈α
break␈α
clause␈α
describes␈α
a␈α
position␈α
to␈α
place␈α
a␈α
break␈α
and␈αthe
␈↓ ↓H␈↓condition under which the break is to occur (the test expression).
␈↓ ↓H␈↓120␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓        Given␈α⊂the␈α⊂ability␈α⊂to␈α⊂"break"␈α⊂the␈α⊂evaluation␈α⊂of␈α⊂an␈α⊂expression␈α⊂we␈α⊂now␈α⊂need␈α⊃some␈α⊂useful
␈↓ ↓H␈↓routines␈αfor␈αexamining␈αthe␈α
state␈αof␈αthe␈αworld.␈α The␈α
functions␈α␈↓↓evalframe␈↓␈αand␈α␈↓↓freturn␈↓␈α(or␈α
analogous
␈↓ ↓H␈↓functions)␈α∞provide␈α∞us␈α∞with␈α∞the␈α∞necessary␈α∞primitives,␈α∞but␈α∞not␈α∞it␈α∞the␈α∞most␈α∞useful␈α∞form.␈α∞ A␈α∞typical
␈↓ ↓H␈↓␈↓↓helper␈↓␈αprogram␈αmight␈αinclude␈αoperations␈αfor␈αmoving␈αup␈αand␈αdown␈αthe␈αstack,␈αprinting␈αthe␈αcurrent
␈↓ ↓H␈↓expression,␈αevaluting␈α
expressions␈αin␈αthe␈α
current␈αenvironment,␈αand␈α
forcing␈αthe␈αbroken␈α
computation
␈↓ ↓H␈↓to␈α⊂continue.␈α⊂ Then␈α⊂in␈α⊂a␈α⊂break␈α⊂loop␈α⊂␈↓↓helper␈↓␈α⊂could␈α⊂be␈α⊂invoked,␈α⊂and␈α⊂would␈α⊂go␈α⊂into␈α⊃a␈α⊂command
␈↓ ↓H␈↓reading␈αloop.␈α Notice␈αthat␈αmoving␈αdown␈αthe␈αstack␈α(backwards␈αin␈αtime)␈αcan␈αbe␈αdone␈αby␈αalternately
␈↓ ↓H␈↓applying␈α
␈↓↓evalframe␈↓␈αand␈α
selecting␈α
the␈αpdl␈α
pointer␈αfrom␈α
the␈α
list.␈α However␈α
moving␈α
back␈αup␈α
is␈αnot␈α
so
␈↓ ↓H␈↓easy.  The ␈↓↓helper␈↓ program will need to maintain its own stack of pdl pointers.

␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.␈α Add␈αsome␈αadditional␈αfeatures␈αto␈αthe␈αtracing␈αprogram.␈α For␈αexample␈αit␈αwould␈αbe␈αuseful␈αto␈α
have
␈↓ ↓H␈↓only␈α⊃selected␈α⊃arguments␈α⊃to␈α⊃a␈α⊃function␈α⊃printed␈α⊃at␈α⊃each␈α⊃call.␈α⊃ Also␈α⊃printing␈α⊃the␈α⊃value␈α⊃of␈α⊂some
␈↓ ↓H␈↓additional␈αexpression␈αbefore␈αor␈αafter,␈α(or␈αboth)␈αthe␈αevaluation␈αthe␈αfunction␈αapplication.␈α You␈αmay
␈↓ ↓H␈↓think of other features you would like to have in a tracing package.

␈↓ ↓H␈↓2.␈α Write␈αa␈αprogram␈αthat␈αadds␈αbreak␈αpoints␈αto␈αfunction␈αdefinitions.␈α You␈αwill␈αneed␈αto␈αdecide␈αhow
␈↓ ↓H␈↓the break points are to be specified.

␈↓ ↓H␈↓3.␈α Write␈α
code␈αto␈α
execute␈αsome␈α␈↓↓helper␈↓␈α
commands.␈α  Use␈α
the␈α␈↓↓evalframe␈α
function␈↓␈αdescribed␈αin␈α
section
␈↓ ↓H␈↓␈↓π∞␈↓3.␈α Assume␈αthat␈α␈↓¬NIL␈↓␈αrepresents␈αthe␈αpointer␈αto␈α
the␈αcurrent␈αtop␈αof␈αthe␈αstack.␈α Use␈αand␈αmaintain␈α
the
␈↓ ↓H␈↓global␈αvariables␈α␈↓¬POINT␈α␈↓and␈α␈↓¬PTLIST␈α␈↓which␈αrepresent␈αthe␈αcurrent␈αposition␈αin␈αthe␈αstack␈αand␈αthe␈αlist
␈↓ ↓H␈↓of␈α
stack␈α
positions␈α
from␈α
here␈α
to␈α
the␈α
top␈α
(to␈α
allow␈α
you␈α
to␈α
go␈α
up␈α
as␈α
well␈α
as␈α
down).␈α∞ Implement␈α
the
␈↓ ↓H␈↓commands␈α⊂␈↓¬UP␈α⊃␈↓<n>␈α⊂and␈α⊂␈↓¬DOWN␈α⊃␈↓<n>␈α⊂for␈α⊃moving␈α⊂up␈α⊂and␈α⊃down␈α⊂the␈α⊂stack␈α⊃<n>␈α⊂levels,␈α⊃␈↓¬NEXT␈α⊂␈↓<fn>
␈↓ ↓H␈↓which␈α⊃moves␈α⊃to␈α⊃the␈α⊂next␈α⊃occurrence␈α⊃of␈α⊃a␈α⊂call␈α⊃to␈α⊃<fn>␈α⊃down␈α⊂the␈α⊃stack,␈α⊃ ␈↓¬CE␈α⊃␈↓which␈α⊃prints␈α⊂the
␈↓ ↓H␈↓expression␈α⊃being␈α⊃evaluated␈α⊃at␈α⊂the␈α⊃current␈α⊃position,␈α⊃␈↓¬VAL␈α⊂␈↓<exp>␈α⊃which␈α⊃evaluates␈α⊃<exp>␈α⊃in␈α⊂the
␈↓ ↓H␈↓binding␈α⊃environment␈α⊃corresponding␈α∩to␈α⊃the␈α⊃current␈α⊃position␈α∩and␈α⊃prints␈α⊃the␈α⊃result,␈α∩and␈α⊃␈↓¬FREES
␈↓ ↓H␈↓¬␈↓which␈αfinds␈αall␈αfree␈αvariables␈αin␈αthe␈αcurrent␈αexpression␈αand␈αprints␈αthe␈αvariable␈αnames␈αand␈αvalues
␈↓ ↓H␈↓in␈αthe␈α
environment␈αcorresponding␈αto␈α
the␈αcurrent␈α
position.␈α ␈↓¬FREES␈α␈↓should␈α
check␈αthat␈α
the␈αvariable
␈↓ ↓H␈↓is␈αactually␈α
bound␈αin␈α
the␈αcurrent␈α
environment␈αand␈α
if␈αindicate␈α
that␈αfact␈α
rather␈αthat␈α
causing␈αan␈α
error
␈↓ ↓H␈↓by trying to evaluate it.



␈↓ ↓H␈↓5.  ␈↓αEditing.␈↓


␈↓ ↓H␈↓        The␈α
editor␈α
is␈α
an␈α
important␈α
feature␈α
of␈α
a␈α
LISP␈α
system.␈α
 The␈α
simple␈α
and␈α
uniform␈α∞syntax␈α
of
␈↓ ↓H␈↓LISP␈αprograms␈αmakes␈αit␈αpossible␈αto␈αwrite␈αan␈α
editor␈αin␈αLISP␈αwhich␈αis␈αquite␈αpowerful␈αyet␈αsimple␈α
to
␈↓ ↓H␈↓understand.␈α⊃ In␈α⊃this␈α⊃section␈α⊃we␈α⊃give␈α⊃a␈α⊂simple␈α⊃interactive␈α⊃editor.␈α⊃ The␈α⊃editor␈α⊃is␈α⊃called␈α⊃with␈α⊂a
␈↓ ↓H␈↓function␈α
name␈α
as␈α
argument.␈α
 It␈α
looks␈αup␈α
the␈α
function␈α
definition,␈α
initializes␈α
some␈α
parameters␈αand
␈↓ ↓H␈↓then␈α⊃accepts␈α∩commands.␈α⊃ The␈α⊃key␈α∩global␈α⊃parameters␈α∩are␈α⊃␈↓↓top,␈↓␈α⊃the␈α∩toplevel␈α⊃expression,␈α∩␈↓↓ce␈↓␈α⊃the
␈↓ ↓H␈↓current␈α
expression,␈α
and␈α
␈↓↓chain␈↓␈α
the␈α
chain␈α
of␈αexpressions␈α
leading␈α
from␈α
␈↓↓top␈↓␈α
to␈α
␈↓↓ce.␈↓␈α
 Each␈α
element␈αof
␈↓ ↓H␈↓␈↓↓chain␈↓␈αis␈αthe␈α
pair␈α␈↓↓n␈α.␈α
e␈↓␈αwhere␈α␈↓↓e␈↓␈αis␈α
the␈αexpression␈αimmediately␈α
containing␈αthe␈αnext␈αexpression␈α
down
␈↓ ↓H␈↓the␈αchain␈αand␈α
␈↓↓n␈↓␈αis␈αthe␈αposition␈α
of␈αthat␈αexpressionin␈α␈↓↓e.␈↓␈α
 We␈αimagine␈αthat␈α␈↓↓ce␈↓␈α
is␈αthe␈αbottom␈α
of␈αthe
␈↓ ↓H␈↓chain.␈α∂ There␈α∂are␈α∂four␈α∂classes␈α∂of␈α∂commands␈α∂recognized␈α∂by␈α∂the␈α∂editor:␈α∂exit␈α∂commands␈α∂which␈α∂it
␈↓ ↓H␈↓handles␈α∩directly,␈α∩numbers␈α⊃meaning␈α∩move␈α∩to␈α∩the␈α⊃nth␈α∩element␈α∩of␈α⊃␈↓↓ce,␈↓␈α∩which␈α∩are␈α∩also␈α⊃handled
␈↓ ↓H␈↓directly,␈α⊂other␈α⊂atomic␈α⊂commands␈α⊂and␈α⊂list␈α⊃commands.␈α⊂ The␈α⊂latter␈α⊂two␈α⊂classes␈α⊂of␈α⊃commands␈α⊂are
␈↓ ↓H␈↓handled␈α
by␈α∞looking␈α
up␈α∞the␈α
command␈α∞code␈α
to␈α
be␈α∞executed␈α
on␈α∞the␈α
property␈α∞list␈α
of␈α∞the␈α
command.
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠121


␈↓ ↓H␈↓Thus␈α⊃you␈α⊃can␈α⊃add␈α⊃commands␈α⊃simply␈α⊃by␈α⊃defining␈α⊃the␈α⊃appropriate␈α⊃property.␈α⊃ The␈α⊃editor␈α⊃and
␈↓ ↓H␈↓command␈α
codes␈α
make␈α
some␈α
effort␈α
to␈α
to␈α
make␈α∞sure␈α
the␈α
command␈α
will␈α
work␈α
in␈α
the␈α
context␈α∞it␈α
has
␈↓ ↓H␈↓been␈αgiven.␈α If␈αnot␈αan␈αerror␈αmessage␈αis␈αprinted␈αand␈αno␈αaction␈αoccurs.␈α There␈αare␈αtwo␈αways␈αto␈αexit
␈↓ ↓H␈↓␈↓↓editor,␈↓␈α
one␈αis␈α
to␈α
␈↓¬Q␈↓uit␈αwithout␈α
affecting␈αthe␈α
the␈α
function␈αdefinition␈α
and␈αthe␈α
other␈α
is␈αto␈α
␈↓¬OK␈↓␈αthe␈α
result
␈↓ ↓H␈↓of␈αediting.␈α
 In␈αthis␈αcase␈α
the␈αcurrent␈α␈↓↓top␈↓␈α
replaces␈αthe␈αfunction␈α
definition.␈α Many␈αof␈α
the␈αcommands
␈↓ ↓H␈↓work␈α
destructively.␈α∞ This␈α
is␈α∞safe␈α
as␈α∞the␈α
expression␈α
to␈α∞edit␈α
is␈α∞a␈α
"copy"␈α∞of␈α
the␈α∞function␈α
definition.
␈↓ ↓H␈↓Below␈α⊂we␈α⊂give␈α⊂the␈α⊂toplevel␈α⊂␈↓↓editor␈↓␈α⊂function␈α⊂definition␈α⊂and␈α⊂an␈α⊂assortment␈α⊂of␈α⊂a␈α⊂atomic␈α⊃and␈α⊂list
␈↓ ↓H␈↓command␈α∂descriptions.␈α∞ We␈α∂will␈α∞suggest␈α∂various␈α∞extensions␈α∂and␈α∞modifications␈α∂as␈α∂exercises.␈α∞ We
␈↓ ↓H␈↓note␈αthat␈αthe␈α␈↓↓editor␈↓␈α
expects␈αits␈αexpressions␈αto␈α
be␈αatomic␈αor␈αlists␈α
as␈αare␈αthe␈αexpression␈α
occuring␈αin
␈↓ ↓H␈↓function␈αdefinitions.␈α
 The␈αbasic␈α
actions␈αof␈α
an␈αeditor␈α
are␈α(i)␈α
moving␈αaround␈α
in␈αthe␈α
expression␈α(ii)
␈↓ ↓H␈↓deleting␈αa␈αsubexpression␈α(iii)␈αinserting␈αa␈αsubexpression␈α(iv)␈αmoving␈αleft␈αor␈αright␈αparentheses␈αin␈αor
␈↓ ↓H␈↓out␈α⊃one␈α⊃expression␈α∩We␈α⊃will␈α⊃give␈α∩examples␈α⊃of␈α⊃each␈α∩kind.␈α⊃ We␈α⊃begin␈α∩with␈α⊃the␈α⊃␈↓↓editor␈↓␈α∩and␈α⊃its
␈↓ ↓H␈↓auxiliary functions.

␈↓ ↓H␈↓␈↓ α8␈↓↓        editor l (FEXPR) ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓            ␈↓αprogram␈↓↓ [fn, top, ce, chain, command, efn]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 fn ← ␈↓αa|␈↓↓l␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 top ← copy get[fn, ␈↓¬EXPR␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αif␈↓↓ ␈↓αn|␈↓↓top ␈↓αthen␈↓↓ [errmsg0[]; ␈↓αreturn␈↓↓ ␈↓¬NO-EDIT␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ce ← top; chain ← ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         ␈↓¬EDLOOP:␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 print ␈↓¬ε␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 command ← read[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αif␈↓↓ command = ␈↓¬Q␈↓↓ ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓¬BYE␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ command = ␈↓¬OK␈↓↓ ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ putprop[fn, top, ␈↓¬EXPR␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ numberp command ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓5.1)␈↓ α8␈↓↓                       [␈↓αif␈↓↓ ␈↓αat|␈↓↓ce ∨ command > length ce ␈↓αthen␈↓↓ [errmsg1[], ␈↓αgo to␈↓↓ ␈↓¬EDLOOP␈↓↓], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                        chain ← [command . ce] . chain, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                        ce ← nth[ce, command], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                        ␈↓αgo to␈↓↓ ␈↓¬EDLOOP␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓command ∧ [efn ← get[command, ␈↓¬ATOMIC-EDIT-FN␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                       ␈↓αthen␈↓↓ [eval efn, ␈↓αgo to␈↓↓ ␈↓¬EDLOOP␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ¬␈↓αat|␈↓↓command ∧ [efn ← get[␈↓αa|␈↓↓command, ␈↓¬LIST-EDIT-FN␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                       ␈↓αthen␈↓↓ [apply[efn, ␈↓αd|␈↓↓command], ␈↓αgo to␈↓↓ ␈↓¬EDLOOP␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 errmsg2[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αgo to␈↓↓ ␈↓¬EDLOOP␈↓↓␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        errmsg0[] ← [print fn, princ ␈↓¬`not an EXPR'␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        errmsg3[] ← [terpri[], princ ␈↓¬`You are at the top'␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        errmsg5[] ← [terpri[], princ ␈↓¬`You are at the right edge'␈↓↓]␈↓

␈↓ ↓H␈↓5.2)␈↓ α8␈↓↓        nth[u, n] ← ␈↓αif␈↓↓ n > 1 ∧ ¬␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ nth[␈↓αd|␈↓↓u, sub1 n] ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u␈↓

␈↓ ↓H␈↓5.3)␈↓ α8␈↓↓        pos[u, n] ← ␈↓αif␈↓↓ n > 1 ∧ ¬␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ pos[␈↓αd|␈↓↓u, sub1 n] ␈↓αelse␈↓↓ u␈↓

␈↓ ↓H␈↓5.4)␈↓ α8␈↓↓        copy x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ copy ␈↓αa|␈↓↓x . copy ␈↓αd|␈↓↓x␈↓


␈↓ ↓H␈↓        Below␈α⊃are␈α⊃programs␈α⊂for␈α⊃executing␈α⊃the␈α⊂following␈α⊃atomic␈α⊃commands:␈α⊂␈↓¬P␈α⊃␈↓(print␈α⊃the␈α⊃␈↓↓ce␈↓),␈α⊂␈↓¬UP
␈↓ ↓H␈↓¬␈↓(move␈αup␈αone␈αlevel␈αin␈αthe␈αchain),␈α␈↓¬RT␈α␈↓(move␈αone␈αto␈αthe␈αright),␈α␈↓¬LI␈α␈↓move␈αthe␈αleft␈αparenthesis␈αin␈αone
␈↓ ↓H␈↓122␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓thus␈αremoving␈αthe␈αfirst␈αelement␈αof␈αthe␈α␈↓↓ce␈↓␈αand␈αinserting␈αit␈αbefore␈αthe␈α␈↓↓ce␈↓),␈α and␈α␈↓¬RO␈α␈↓(move␈αthe␈αright
␈↓ ↓H␈↓parenthesis out one thus making the right sibling of the ␈↓↓ce␈↓ the last element of the ␈↓↓ce␈↓).

␈↓ ↓H␈↓5.5)␈↓ α8␈↓↓        p[] ← print ce␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        up[] ← ␈↓αprogram␈↓↓ []␈↓
␈↓ ↓H␈↓5.6)␈↓ α8␈↓↓                    ␈↓αif␈↓↓ ␈↓αn|␈↓↓chain ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg3[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ce ← ␈↓αda|␈↓↓chain␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    chain ← ␈↓αd|␈↓↓chain␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        rt[] ← ␈↓αprogram␈↓↓ [n]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αif␈↓↓ ␈↓αn|␈↓↓chain ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg3[]␈↓
␈↓ ↓H␈↓5.7)␈↓ α8␈↓↓                    n ← add1 ␈↓αaa|␈↓↓chain␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αif␈↓↓ n > length ␈↓αda|␈↓↓chain ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg5[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ce ← nth[␈↓αda|␈↓↓chain, n]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αaa|␈↓↓chain ← n␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        li[] ← ␈↓αprogram␈↓↓ [cetmp, pos]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αif␈↓↓ ␈↓αn|␈↓↓chain ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg3[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αif␈↓↓ ␈↓αat|␈↓↓ce ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg6[]␈↓
␈↓ ↓H␈↓5.8)␈↓ α8␈↓↓                    pos ← pos[␈↓αda|␈↓↓chain, ␈↓αaa|␈↓↓chain]␈↓␈↓ πx;point to ␈↓↓ce␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αd|␈↓↓pos ← ␈↓αd|␈↓↓ce . ␈↓αd|␈↓↓pos␈↓␈↓ πx;insert ␈↓↓␈↓αd|␈↓↓ce␈↓ after ␈↓↓ce␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αa|␈↓↓pos ← ␈↓αa|␈↓↓ce␈↓␈↓ πx;replace ␈↓↓ce␈↓ by ␈↓↓␈↓αa|␈↓↓ce␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ce ← ␈↓αd|␈↓↓ce␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αaa|␈↓↓chain ← add1 ␈↓αaa|␈↓↓chain␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        ro[] ← ␈↓αprogram␈↓↓ [cetmp, pos, pos1]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αif␈↓↓ ␈↓αn|␈↓↓chain ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg3[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αif␈↓↓ ␈↓αat|␈↓↓ce ∧ ¬␈↓αn|␈↓↓ce ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg6[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    pos ← pos[␈↓αda|␈↓↓chain, ␈↓αaa|␈↓↓chain]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    pos1 ← ␈↓αd|␈↓↓pos␈↓
␈↓ ↓H␈↓5.9)␈↓ α8␈↓↓                    ␈↓αif␈↓↓ ␈↓αn|␈↓↓pos1 ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg5[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    cetmp ← ␈↓¬NIL␈↓↓ . ce␈↓␈↓ πx;in case ␈↓↓ce␈↓ is ␈↓¬NIL␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    nconc[cetmp, pos1]␈↓␈↓ πx;add next element to end of ␈↓↓ce␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αd|␈↓↓pos ← ␈↓αd|␈↓↓pos1␈↓␈↓ πx;delete it from parent list
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αd|␈↓↓pos1 ← ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ce ← ␈↓αd|␈↓↓cetmp␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                    ␈↓αa|␈↓↓pos ← ce ␈↓␈↓ πx;make sure new ␈↓↓ce␈↓ is in parent list

␈↓ ↓H␈↓        The␈α⊂list␈α⊂commands␈α⊂have␈α⊂the␈α⊂form␈α⊂(␈↓¬I␈α⊂␈↓<n>␈α⊂<exp>)␈α⊂or␈α⊂(␈↓¬D␈α⊂␈↓<n>)␈α⊂which␈α⊂mean␈α⊂insert␈α⊂<exp>
␈↓ ↓H␈↓before␈α∞the␈α
<n>th␈α∞position␈α∞of␈α
the␈α∞␈↓↓ce␈↓␈α∞and␈α
delete␈α∞the␈α∞<n>th␈α
element␈α∞of␈α∞the␈α
␈↓↓ce.␈↓␈α∞ The␈α∞programs␈α
for
␈↓ ↓H␈↓executing these commands are given by
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠123


␈↓ ↓H␈↓␈↓ α8␈↓↓        i[n, x] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓            ␈↓αprogram␈↓↓ [pos, tmp]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αif␈↓↓ n < 0 ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg2[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                   ␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = 1 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓5.10)␈↓ α8␈↓↓                        ␈↓αif␈↓↓ ␈↓αn|␈↓↓chain ␈↓αthen␈↓↓ top ← [ce ← x . ce]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                        ␈↓αelse␈↓↓ [ce ← x . ce ; ␈↓αa|␈↓↓pos[␈↓αda|␈↓↓chain, ␈↓αaa|␈↓↓chain] ← ce]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 pos ← pos[ce, sub1 n]␈↓␈↓ πx;point to position for insertion
␈↓ ↓H␈↓␈↓ α8␈↓↓                 tmp ← x . ␈↓¬NIL␈↓↓␈↓␈↓ πx;make list containing ␈↓↓x␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αd|␈↓↓tmp ← ␈↓αd|␈↓↓pos ␈↓␈↓ πx;splice it in
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αd|␈↓↓pos ← tmp ␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        d n ← ␈↓αprogram␈↓↓ [pos, tmp]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                   ␈↓αif␈↓↓ n < 0 ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg2[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                     ␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = 1 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓5.11)␈↓ α8␈↓↓                          ␈↓αif␈↓↓ ␈↓αn|␈↓↓chain ␈↓αthen␈↓↓ top ← [ce ← ␈↓αd|␈↓↓ce]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                          ␈↓αelse␈↓↓ [ce ← ␈↓αd|␈↓↓ce ; ␈↓αa|␈↓↓pos[␈↓αda|␈↓↓chain, ␈↓αaa|␈↓↓chain] ← ce]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                     ␈↓αelse␈↓↓ ␈↓αif␈↓↓ n > length ce ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg2[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                   pos ← pos[ce, sub1 n]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                   ␈↓αd|␈↓↓pos ← ␈↓αdd|␈↓↓pos ␈↓

␈↓ ↓H␈↓α␈↓ ε⊂Exercise

␈↓ ↓H␈↓1.␈α
 Write␈α∞programs␈α
to␈α
execute␈α∞the␈α
atomic␈α∞commands␈α
␈↓¬LO,␈α
␈↓and␈α∞␈↓¬RI␈α
␈↓(see␈α
the␈α∞description␈α
of␈α∞␈↓¬RO␈α
␈↓and
␈↓ ↓H␈↓␈↓¬LI␈↓).

␈↓ ↓H␈↓2.␈α
 Modify␈αthe␈α
list␈αcommands␈α
to␈αtake␈α
a␈αnegative␈α
argment␈αwhich␈α
is␈αinterpreted␈α
as␈αcount␈α
from␈αthe
␈↓ ↓H␈↓end␈α
rather␈α
that␈α
the␈α
beginning␈α
of␈α
the␈α␈↓↓ce.␈α
 3.␈↓␈α
 Write␈α
a␈α
programs␈α
for␈α
executing␈α
list-commands␈αof␈α
the
␈↓ ↓H␈↓form␈α
(␈↓¬R␈α∞␈↓<e1>␈α
<e2>)␈α∞(which␈α
means␈α
to␈α∞replace␈α
occurrences␈α∞of␈α
<e1>␈α
as␈α∞elements␈α
of␈α∞the␈α
␈↓↓ce␈↓␈α∞by␈α
<e2>)
␈↓ ↓H␈↓and␈α(␈↓¬TR␈α
␈↓<e1>␈α<e2>)␈α
(which␈αmeans␈α
to␈αreplace␈αoccurrences␈α
of␈α<e1>␈α
as␈αsubexpression␈α
(at␈αany␈αlevel)␈α
of
␈↓ ↓H␈↓␈↓↓ce␈↓␈αby␈α<e2>).␈α Note␈αthat␈αit␈αis␈αadvisable␈αto␈αmake␈αa␈αnew␈αcopy␈αof␈α<e2>␈αfor␈αeach␈αreplacement␈α
otherwise
␈↓ ↓H␈↓in␈α
later␈α
editing␈α∞a␈α
change␈α
to␈α
one␈α∞occurrence␈α
may␈α
have␈α
the␈α∞undesired␈α
side␈α
effects␈α
of␈α∞changing␈α
all
␈↓ ↓H␈↓occurrences.

␈↓ ↓H␈↓4.␈α∞Write␈α∞a␈α
program␈α∞to␈α∞execute␈α
the␈α∞command␈α∞(␈↓¬MV␈α
␈↓<m>␈α∞<n>)␈α∞which␈α
moves␈α∞the␈α∞element␈α∞in␈α
<m>th
␈↓ ↓H␈↓position to the <n>th position.

␈↓ ↓H␈↓5.␈α Write␈αprograms␈α
to␈αexecute␈αone␈α
or␈αtwo␈αcommands␈α
that␈αyou␈αhave␈α
always␈αwished␈αthe␈α␈↓↓editor␈↓␈α
could
␈↓ ↓H␈↓execute.



␈↓ ↓H␈↓6.  ␈↓αCompiling.␈↓


␈↓ ↓H␈↓        We␈αwill␈αgive␈αexamples␈αof␈αseveral␈αLISP␈αcompilers␈α
in␈αChapter␈αIX.␈α Here␈αwe␈αgive␈αjust␈αa␈α
brief
␈↓ ↓H␈↓introduction␈α
in␈αorder␈α
to␈αdiscuss␈α
the␈αinteraction␈α
of␈α
compiled␈αand␈α
interpreted␈αcode␈α
and␈αcomplete␈α
our
␈↓ ↓H␈↓picture␈α∩of␈α∩a␈α⊃LISP␈α∩system.␈α∩ The␈α⊃target␈α∩language␈α∩of␈α⊃a␈α∩LISP␈α∩compiler␈α⊃is␈α∩a␈α∩symbolic␈α⊃assembly
␈↓ ↓H␈↓language␈α∞called␈α∞␈↓¬LAP.␈α∂␈↓␈α∞Each␈α∞␈↓¬LAP␈α∂␈↓program␈α∞is␈α∞a␈α∞list␈α∂of␈α∞instructions␈α∞and␈α∂each␈α∞instruction␈α∞is␈α∂a␈α∞list
␈↓ ↓H␈↓containing␈α∀a␈α∀symbolic␈α∀operation␈α∀code,␈α∀and␈α∀what␈α∀ever␈α∀address␈α∀information␈α∀is␈α∀needed.␈α∪ The
␈↓ ↓H␈↓important␈α∞point␈α
to␈α∞notice␈α
is␈α∞that␈α∞such␈α
programs␈α∞are␈α
made␈α∞up␈α∞of␈α
ordinary␈α∞lists␈α
and␈α∞thus␈α∞can␈α
be
␈↓ ↓H␈↓124␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓generated␈α⊂by␈α∂a␈α⊂LISP␈α∂program.␈α⊂ All␈α∂that␈α⊂is␈α∂needed␈α⊂then␈α∂is␈α⊂an␈α∂assembler/loader␈α⊂to␈α⊂convert␈α∂the
␈↓ ↓H␈↓assembly␈αlanguage␈αprogram␈αinto␈αactual␈αbinary␈αcode␈αand␈αload␈αit␈αinto␈αmemory␈α(putting␈αappropriate
␈↓ ↓H␈↓␈↓¬SUBR ␈↓properties on the property lists of the atoms naming the compiled programs).

␈↓ ↓H␈↓        There␈α∞are␈α
two␈α∞ways␈α
in␈α∞which␈α
compiled␈α∞code␈α
and␈α∞interpreted␈α
code␈α∞might␈α
interact.␈α∞ One␈α
is
␈↓ ↓H␈↓that␈αif␈αwe␈αhave␈αa␈αcollection␈αof␈αprograms␈αcommunicating␈αthrough␈αsome␈αglobal␈αvariables␈αthen␈αboth
␈↓ ↓H␈↓the␈αinterpreter␈αand␈αthe␈αcompiler␈αmay␈αneed␈αto␈αknow␈αabout␈αthese␈αvariables.␈α Some␈αimplementations
␈↓ ↓H␈↓of␈α⊃LISP␈α⊃have␈α⊃a␈α⊃value␈α⊃cell␈α⊃associated␈α⊃with␈α⊃each␈α⊃atom␈α⊃where␈α⊃the␈α⊃current␈α⊃value␈α⊃is␈α⊃kept.␈α⊂ Old
␈↓ ↓H␈↓bindings␈α
are␈α
kept␈α
on␈α
a␈α
stack␈α
to␈α
use␈α
for␈α
restoring␈α
when␈α
λ's␈α
or␈α
␈↓αprogram␈↓s␈α
are␈α
exited.␈α
 In␈α
this␈αcase␈α
the
␈↓ ↓H␈↓loader␈αcan␈αfill␈α
in␈αthe␈αlocation␈α
of␈αthe␈αvalue␈αcell␈α
for␈αreferences␈αto␈α
global␈αvariables.␈α If␈αcurrent␈α
values
␈↓ ↓H␈↓are␈α
kept␈α
on␈α
a␈α
stack␈α
then␈α
names␈α
will␈α
have␈α
to␈α
be␈α
kept␈α
too␈α
and␈α
reference␈α
to␈α
a␈α
global␈α
variable␈α
will
␈↓ ↓H␈↓generate code to search the stack.

␈↓ ↓H␈↓        The␈αsecond␈α
is␈αthat␈αwe␈α
may␈αwish␈αto␈α
have␈αa␈αcompiled␈α
program␈αcall␈αan␈α
interpreted␈αprogram.
␈↓ ↓H␈↓In␈α∂general␈α∂when␈α∞a␈α∂program␈α∂is␈α∂compiled␈α∞and␈α∂a␈α∂function␈α∂application␈α∞occurs␈α∂in␈α∂the␈α∂program␈α∞the
␈↓ ↓H␈↓compiler␈αwill␈α
not␈αknow␈αwhether␈α
the␈αfunction␈αwill␈α
be␈αcompiled␈α
or␈αinterpreted.␈α And␈α
in␈αfact␈αwe␈α
may
␈↓ ↓H␈↓wish␈αto␈αswitch␈αhorses␈αin␈αmid␈αstream␈αfor␈αvarious␈αreasons␈αand␈αthus␈αnot␈αbe␈αcommitted␈αto␈αone␈αmode
␈↓ ↓H␈↓of␈α⊃application␈α∩or␈α⊃another.␈α⊃ The␈α∩solution␈α⊃to␈α∩the␈α⊃problem␈α⊃is␈α∩as␈α⊃follows.␈α⊃ A␈α∩function␈α⊃call␈α∩in␈α⊃a
␈↓ ↓H␈↓compiled␈αprogram␈αjumps␈αto␈αa␈αroutine␈αthat␈αlooks␈αup␈αthe␈αfunction␈αdefinition␈αjust␈αas␈αthe␈αinterpreter
␈↓ ↓H␈↓would and performs the appropriate action depending on what kind of definition is found.

␈↓ ↓H␈↓        This␈αinteraction␈α
means␈αthat␈α
we␈αcan␈α
replace␈αfunction␈α
definitions␈αand␈α
have␈αthe␈α
new␈αversion
␈↓ ↓H␈↓used␈αby␈αcompiled␈αas␈αwell␈αas␈αinterpreted␈αprograms.␈α In␈αparticular␈αwe␈αcan␈αtrace␈αcompiled␈αprograms
␈↓ ↓H␈↓(using␈αa␈αspecial␈αform␈α
of␈α␈↓↓apply␈↓␈αthat␈αtakes␈α
a␈αsubroutine␈αpointer␈αrather␈α
than␈αa␈αlambda␈αexpression␈α
as
␈↓ ↓H␈↓first argument).  Also traced programs will be noticed by calling compiled programs.

␈↓ ↓H␈↓        You␈α∂may␈α∂complain␈α∞that␈α∂we␈α∂pay␈α∞a␈α∂large␈α∂price␈α∞efficiency␈α∂wise␈α∂for␈α∞all␈α∂this␈α∂flexibility␈α∞when
␈↓ ↓H␈↓afterall␈αthe␈α
usual␈αreason␈α
for␈αcompiling␈αis␈α
to␈αmake␈α
things␈αrun␈αfaster.␈α
 LISP␈αhas␈α
a␈αsolution␈α
to␈αthis
␈↓ ↓H␈↓problem␈αtoo.␈α Namely,␈αit␈αis␈αpossible␈αto␈αarrange␈αfor␈αthe␈αcompiled␈αcode␈αto␈αmodify␈αitself␈αwhen␈αa␈αcall
␈↓ ↓H␈↓turns␈α
out␈α
to␈α
be␈αto␈α
a␈α
compiled␈α
function␈α
so␈αthat␈α
the␈α
next␈α
time␈α
that␈αcall␈α
is␈α
executed,␈α
the␈α
address␈αis
␈↓ ↓H␈↓known␈α
and␈α
no␈α
search␈α
for␈α
the␈α
definition␈α
is␈α
necessary.␈α
 Of␈α
course␈α
if␈α
you␈α
run␈α
in␈α
this␈α
mode␈αa␈α
function
␈↓ ↓H␈↓that␈αis␈αredefined␈αwill␈αnote␈αbe␈αnoticed␈αby␈αa␈αcompiled␈αprogram␈αcalling␈αthis␈αfunction.␈α As␈αyou␈αmight
␈↓ ↓H␈↓imagine␈α
there␈α∞are␈α
various␈α
intermediate␈α∞modes␈α
and␈α∞other␈α
clever␈α
solutions␈α∞to␈α
the␈α∞calling␈α
problem.
␈↓ ↓H␈↓One␈αfairly␈αnice␈αsolution␈αis␈αto␈αuse␈αa␈αtable␈αwhich␈αcontains␈αan␈αentry␈αfor␈αeach␈αfunction.␈α The␈αcompile
␈↓ ↓H␈↓code␈α∃knows␈α∃only␈α∃the␈α∃entry␈α∀location.␈α∃ The␈α∃first␈α∃time␈α∃a␈α∀function␈α∃is␈α∃called␈α∃(by␈α∃anyone)␈α∀the
␈↓ ↓H␈↓corresponding␈α∪entry␈α∪is␈α∪filled␈α∪in␈α∀and␈α∪any␈α∪later␈α∪calls␈α∪go␈α∀directly␈α∪with␈α∪only␈α∪a␈α∪single␈α∀level␈α∪of
␈↓ ↓H␈↓indirection.␈α
 At␈αany␈α
point␈α
one␈αor␈α
all␈α
entries␈αin␈α
the␈α
table␈αcan␈α
be␈α
erased.␈α Then␈α
it␈α
appears␈αas␈α
though
␈↓ ↓H␈↓the function hadn't previously been called and the entry is reset at the next call.

␈↓ ↓H␈↓α␈↓ ε⊂Exercise

␈↓ ↓H␈↓1.␈α
 Add␈α∞a␈α
feature␈α
to␈α∞the␈α
trace␈α∞package␈α
to␈α
trace␈α∞compiled␈α
programs␈α
(␈↓¬SUBR␈↓s).␈α∞  Note␈α
that␈α∞you␈α
will
␈↓ ↓H␈↓have␈αto␈αdo␈αsome␈αadditional␈αwork␈αto␈αtreat␈αthe␈αarguments␈αto␈αa␈α␈↓¬SUBR␈↓␈αas␈αthey␈αcan't␈αbe␈αobtained␈αfrom
␈↓ ↓H␈↓the␈αpointer.␈α You␈αmight␈αrequire␈αto␈αuser␈αto␈αsay␈αhow␈αmany,␈αor␈αassume␈αthat␈αthe␈αfunction␈αalso␈αhas␈αan
␈↓ ↓H␈↓␈↓¬NARGS␈↓ property which is the number of arguments required.
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠125


␈↓ ↓H␈↓7.  ␈↓αExercises.␈↓


␈↓ ↓H␈↓1.␈α Write␈αa␈αprogram␈αto␈α"pretty␈αprint"␈αfunction␈αdefinitions.␈α The␈αidea␈αis␈αto␈αbe␈αable␈αto␈αprint␈αwith␈αa
␈↓ ↓H␈↓suitable␈αdivision␈αinto␈α lines␈αand␈αwith␈αindentation␈αso␈αthat␈αthe␈αstructure␈αof␈αthe␈αprogram␈αis␈αeasier␈αto
␈↓ ↓H␈↓discover.␈α∞ The␈α∞program␈α∞may␈α
be␈α∞similar␈α∞to␈α∞␈↓↓prinlis.␈↓␈α
 You␈α∞will␈α∞need␈α∞additional␈α∞special␈α
characters,
␈↓ ↓H␈↓say␈α␈↓¬SPACE␈α
␈↓and␈α␈↓¬NEWLINE.␈α
␈↓␈αThe␈α
program␈αshould␈α
print␈αsubexpressions␈α
on␈αa␈α
single␈αline␈α
when␈αever
␈↓ ↓H␈↓possible,␈αalign␈αarguments␈αin␈αa␈αfunction␈αcall,␈αand␈αpairs␈α
of␈αa␈αconditional␈αwhen␈αthey␈αwill␈αnot␈αfit␈αon␈α
a
␈↓ ↓H␈↓line.␈α∞ You␈α∞will␈α∞probably␈α∞wish␈α∞to␈α∞experiment␈α
to␈α∞see␈α∞how␈α∞much␈α∞indentation␈α∞is␈α∞desired␈α∞in␈α
various
␈↓ ↓H␈↓cases.␈α⊂ You␈α⊂will␈α⊂probably␈α⊂ be␈α⊂able␈α⊂to␈α⊂think␈α⊂of␈α⊂other␈α⊂conventions␈α⊂that␈α⊂will␈α⊂make␈α⊂the␈α⊂resulting
␈↓ ↓H␈↓output easier to read.

␈↓ ↓H␈↓2.␈α
 A␈α
standard␈α
aid␈αin␈α
the␈α
debugging␈α
machine␈αlanguage␈α
programs␈α
is␈α
the␈αability␈α
to␈α
single␈α
step␈αto␈α
the
␈↓ ↓H␈↓execution␈αof␈αa␈αprogram.␈α In␈αthis␈αmode␈αthe␈αprocessor␈αhalts␈αafter␈αthe␈αevaluation␈αof␈αeach␈αinstruction,
␈↓ ↓H␈↓allows␈α∞the␈α∞programmer␈α∞to␈α∞examine␈α∞and␈α
modify␈α∞the␈α∞contents␈α∞of␈α∞regiseters,␈α∞memory␈α∞locations,␈α
the
␈↓ ↓H␈↓program␈α∞counter␈α∞etc..␈α∞ We␈α∂can␈α∞imagine␈α∞the␈α∞process␈α∂of␈α∞interpreting␈α∞and␈α∞S-expression␈α∂and␈α∞being
␈↓ ↓H␈↓composed␈αof␈α
indivisible␈αsteps␈α
such␈αand␈αapplying␈α
primitive␈αfunctions␈α
building␈αan␈α
environment␈αto
␈↓ ↓H␈↓evaluate␈α
the␈α∞body␈α
of␈α
a␈α∞lambda␈α
expression,␈α
a␈α∞branching␈α
decision,␈α
etc..␈α∞ Write␈α
a␈α∞program␈α
␈↓↓stepper␈↓
␈↓ ↓H␈↓which␈α"single-steps"␈αthrough␈αthe␈α
evaluation␈αof␈αan␈αexpression.␈α
 A␈αsimple␈αversion␈αwould␈α prints␈α
out
␈↓ ↓H␈↓each␈αexpression␈α
as␈αit␈αbegins␈α
to␈αevaluate␈αit,␈α
and␈αprint␈αout␈α
values␈αas␈αthey␈α
are␈αobtained.␈α
 A␈αfancier
␈↓ ↓H␈↓version␈α
might␈α
go␈α
into␈α
a␈α
read-eval-print␈α
loop␈α
(maybe␈α
a␈α
break␈α
loop)␈α
so␈α
that␈α
the␈α
programmer␈α
can
␈↓ ↓H␈↓examine␈α
the␈α
state␈α
of␈α
the␈α
computation,␈α
modify␈αthings,␈α
and␈α
then␈α
continue␈α
the␈α
single␈α
steping.␈α You
␈↓ ↓H␈↓may␈α
want␈α
options␈αto␈α
bail␈α
out␈αnow,␈α
or␈α
evaluate␈αthe␈α
current␈α
expression␈αin␈α
fast␈α
mode␈α
(returning␈αto
␈↓ ↓H␈↓single stepping if there is any computation pending.
␈↓ ↓H␈↓126␈↓ εH␈↓ H


␈↓ ↓H␈↓α␈↓ ¬{Chapter VI

␈↓ ↓H␈↓α␈↓ ∧BLISP PROGRAMS FOR SEARCHING


␈↓ ↓H␈↓        Many␈α∂programs␈α∂use␈α∂the␈α⊂technique␈α∂of␈α∂exploring␈α∂a␈α⊂"search␈α∂space"␈α∂of␈α∂possible␈α⊂solutions␈α∂in
␈↓ ↓H␈↓order␈αto␈α
solve␈αa␈α
problem.␈α It␈α
is␈αoften␈α
convenient␈αto␈α
represent␈αthe␈α
search␈αspace␈α
as␈αa␈α
tree␈αstructure
␈↓ ↓H␈↓with␈αa␈αstarting␈αposition␈αand␈αsome␈αmethod␈αof␈α
getting␈αto␈αsuccessor␈αpositions.␈α In␈αthis␈αcase␈αthe␈α
search
␈↓ ↓H␈↓can be controlled by some standard algorithm for visiting all nodes of a tree.

␈↓ ↓H␈↓        In␈α∞this␈α
chapter␈α∞we␈α
will␈α∞examine␈α
various␈α∞algorithms␈α
for␈α∞searching.␈α
 The␈α∞key␈α
feature␈α∞in␈α
all
␈↓ ↓H␈↓cases␈αis␈α
the␈αablility␈αto␈α
separate␈αthe␈αproblem␈α
into␈αa␈αbasic␈α
recursive␈αcontrol␈αprogram␈α
that␈αdescribes
␈↓ ↓H␈↓the␈α∪search␈α∩and␈α∪a␈α∪collection␈α∩of␈α∪problem␈α∩dependent␈α∪functions.␈α∪ We␈α∩give␈α∪examples␈α∪of␈α∩specific
␈↓ ↓H␈↓problems and the collection of functions that characterize the problem for each case.

␈↓ ↓H␈↓        Searching␈α
is␈αan␈α
important␈αtool␈α
in␈αArtificial␈α
Intelligence␈αresearch␈α
and␈αmuch␈α
work␈α
has␈αbeen
␈↓ ↓H␈↓done␈α
on␈α
various␈α
techniques.␈α
 Most␈α
books␈α
treating␈α
AI␈α
discuss␈α
searching.␈α
 For␈α
an␈α
introduction␈αto␈α
the
␈↓ ↓H␈↓general topic of searching techniques see Nilsson[1971].



␈↓ ↓H␈↓1.  ␈↓αDepth First Tree Search.␈↓


␈↓ ↓H␈↓         One␈α∞algorithm␈α∞for␈α
searching␈α∞a␈α∞tree␈α
like␈α∞structure␈α∞is␈α
the␈α∞depth␈α∞first␈α
search.␈α∞ It␈α∞consists␈α
of
␈↓ ↓H␈↓taking␈α⊃the␈α⊃first␈α∩(in␈α⊃some␈α⊃fixed␈α⊃ordering)␈α∩untried␈α⊃branch␈α⊃at␈α⊃each␈α∩level␈α⊃until␈α⊃a␈α∩leaf␈α⊃(terminal
␈↓ ↓H␈↓position)␈α⊂is␈α∂reached␈α⊂or␈α∂until␈α⊂no␈α⊂more␈α∂branches␈α⊂are␈α∂left.␈α⊂ In␈α⊂either␈α∂case␈α⊂pop␈α∂up␈α⊂one␈α⊂level␈α∂and
␈↓ ↓H␈↓continue the search until all nodes have been visited.

␈↓ ↓H␈↓        Simple␈αS-expression␈αrecursion␈αcan␈αbe␈αregarded␈αas␈αa␈αspecial␈αcase␈αof␈αdepth␈αfirst␈αrecursion.␈α It
␈↓ ↓H␈↓is␈αspecial␈αin␈αthat␈αthere␈α
are␈αexactly␈αtwo␈αbranches,␈αbut␈αeven␈α
more␈αimportant,␈αthe␈αtree␈αis␈αthe␈α
tree␈αof
␈↓ ↓H␈↓parts of the S-expression and is present at the beginning of the calculation.

␈↓ ↓H␈↓        In␈α
the␈α
general␈α
case␈α
of␈α
tree␈α
search␈α∞recursion,␈α
the␈α
tree␈α
is␈α
generated␈α
by␈α
a␈α∞␈↓↓successors␈↓␈α
function.
␈↓ ↓H␈↓We␈α∞can␈α∞describe␈α∞the␈α∞tree␈α∂search␈α∞in␈α∞a␈α∞problem␈α∞independent␈α∂way␈α∞by␈α∞the␈α∞general␈α∞depth␈α∂first␈α∞tree
␈↓ ↓H␈↓search␈α
function␈α
␈↓↓search.␈↓␈α
 We␈α
have␈α
already␈α
seen␈α
a␈α
simple␈α
application␈α
of␈α
␈↓↓search␈↓␈α
in␈α
the␈α
problem␈αof
␈↓ ↓H␈↓finding␈αa␈αpath␈αto␈αa␈αparticular␈αnode␈αin␈α
a␈αgraph␈α(Chapter␈αII␈α␈↓π∞␈↓6).␈α We␈αrecall␈αhere␈αthe␈α
definition␈αof
␈↓ ↓H␈↓␈↓↓search.␈↓

␈↓ ↓H␈↓1.1) ␈↓ αQ␈↓↓search p ← ␈↓αif␈↓↓ lose p ␈↓αthen␈↓↓ ␈↓¬LOSE ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ p ␈↓αelse␈↓↓ searchlis[successors p]␈↓ 

␈↓ ↓H␈↓where

␈↓ ↓H␈↓1.2) ␈↓ ↓o␈↓↓    searchlis u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬LOSE ␈↓↓ ␈↓αelse␈↓↓ {search ␈↓αa|␈↓↓u}[λx: ␈↓αif␈↓↓ x = ␈↓¬LOSE ␈↓↓␈↓αthen␈↓↓ searchlis ␈↓αd|␈↓↓u ␈↓αelse␈↓↓ x]␈↓. 

␈↓ ↓H␈↓In␈α
order␈α
to␈α
solve␈α
a␈α
search␈α
problem␈α
using␈α␈↓↓search␈↓␈α
we␈α
must␈α
be␈α
able␈α
to␈α
characterize␈α
the␈α
problem␈αin
␈↓ ↓H␈↓terms of the auxiliary functions ␈↓↓successors,␈↓ ␈↓↓ter,␈↓ and ␈↓↓lose␈↓ used by ␈↓↓search.␈↓

␈↓ ↓H␈↓        As␈αa␈αnontrivial␈αexample␈αwe␈αshow␈αhow␈αto␈αsolve␈αthe␈αso-called␈α␈↓αInstant␈αInsanity␈↓␈αpuzzle␈αusing
␈↓ ↓H␈↓␈↓↓search.␈↓␈α First␈αwe␈αmust␈αdescribe␈αthe␈αpuzzle.␈α There␈αare␈αfour␈αcubical␈αblocks,␈αand␈αeach␈αface␈α
of␈αeach
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠127


␈↓ ↓H␈↓block␈αis␈αcolored␈αwith␈αone␈αof␈αfour␈αcolors.␈α The␈αobject␈αof␈αthe␈αpuzzle␈αis␈αto␈αbuild␈αa␈αtower␈αof␈α
all␈αfour
␈↓ ↓H␈↓blocks␈αsuch␈α
that␈αeach␈αvertical␈α
face␈αof␈α
the␈αtower␈αinvolves␈α
all␈αfour␈α
colors.␈α In␈αorder␈α
to␈αuse␈αthe␈α
above
␈↓ ↓H␈↓defined␈αfunction␈α
␈↓↓search␈↓␈αfor␈α
this␈αpurpose,␈α
we␈αmust␈α
decide␈αon␈α
a␈αrepresentation␈α
of␈αpositions␈αand␈α
give
␈↓ ↓H␈↓the␈αfunctions␈α␈↓↓lose,␈↓␈α␈↓↓ter,␈↓␈αand␈α␈↓↓successors.␈↓␈α A␈αposition␈αwill␈αbe␈αrepresented␈αby␈αa␈αlist␈αof␈αlists␈α-␈αone␈αfor
␈↓ ↓H␈↓each␈αface␈αof␈αthe␈αtower.␈α Each␈αsublist␈αis␈αthe␈αlist␈αof␈αcolors␈αof␈αthe␈αfaces␈αof␈αthe␈αblocks␈αshowing␈αin␈αthat
␈↓ ↓H␈↓face.␈α Initially␈α
the␈αtower␈α
is␈αempty.␈α
 The␈αsuccessors␈αof␈α
a␈αposition␈α
are␈αobtained␈α
by␈αadding␈α
the␈αnext
␈↓ ↓H␈↓unused␈αblock␈αin␈αall␈αpossible␈αorientations,␈αeg.␈α
by␈αadding␈αthe␈αappropriate␈αcolor␈αto␈αeach␈αof␈α
the␈αfour
␈↓ ↓H␈↓sublists.␈α∃ We␈α∀shall␈α∃assume␈α∃that␈α∀the␈α∃blocks␈α∀are␈α∃described␈α∃in␈α∀the␈α∃following␈α∃longwinded␈α∀but
␈↓ ↓H␈↓convenient␈αway.␈α
 (We'll␈αtake␈α
up␈αprecomputing␈α
this␈αdescription␈α
later.) ␈αFor␈α
each␈αblock␈α
there␈αis␈αa␈α
list
␈↓ ↓H␈↓of␈α⊂the␈α∂24␈α⊂orientations␈α⊂of␈α∂the␈α⊂block␈α⊂where␈α∂each␈α⊂orientation␈α⊂is␈α∂described␈α⊂as␈α⊂a␈α∂list␈α⊂of␈α⊂the␈α∂colors
␈↓ ↓H␈↓around␈αthe␈αvertical␈αfaces␈αof␈αthe␈αblock␈αwhen␈α
it␈αis␈αin␈αthat␈αorientation.␈α Thus␈αthe␈αpuzzle␈αis␈α
described
␈↓ ↓H␈↓by a list of lists of lists which we shall call ␈↓↓puzz.␈↓

␈↓ ↓H␈↓        We now have

␈↓ ↓H␈↓1.3)␈↓ αx␈↓↓lose p ← orlis[λu: ¬␈↓αn|␈↓↓u ∧ ␈↓αa|␈↓↓u ε ␈↓αd|␈↓↓u, p],␈↓
␈↓ ↓H␈↓1.4)␈↓ αx␈↓↓ ter p ← [length ␈↓αa|␈↓↓p = 4],        ␈↓
␈↓ ↓H␈↓1.5)␈↓ αx␈↓↓successors p ← mapcar[λx: mapcar2[λyz: z.y, p, x], nth[puzz, 1 + length ␈↓αa|␈↓↓p]]␈↓

␈↓ ↓H␈↓where the functions ␈↓↓mapcar2␈↓ and ␈↓↓nth␈↓ are given by

␈↓ ↓H␈↓1.6)␈↓ αx␈↓↓mapcar2[f, u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ f[␈↓αa|␈↓↓u, ␈↓αa|␈↓↓v] . mapcar2[f, ␈↓αd|␈↓↓u, ␈↓αd|␈↓↓v]␈↓.
␈↓ ↓H␈↓1.7)␈↓ αx␈↓↓nth[u, n] ← ␈↓αif␈↓↓ n=1 ␈↓αthen␈↓↓ ␈↓αa|␈↓↓u ␈↓αelse␈↓↓ nth[␈↓αd|␈↓↓u, n-1]␈↓.

␈↓ ↓H␈↓␈↓↓nth␈↓␈α⊂picks␈α⊃out␈α⊂the␈α⊂list␈α⊃repesenting␈α⊂the␈α⊂next␈α⊃block␈α⊂in␈α⊂␈↓↓puzz.␈↓␈α⊃ The␈α⊂outer␈α⊂mapping␈α⊃functon␈α⊂runs
␈↓ ↓H␈↓through␈αthe␈αlist␈αof␈αorientations␈αreturned␈αby␈α␈↓↓nth␈↓␈αand␈αmakes␈αa␈αnew␈αposition␈αfor␈αeach␈αone.␈α This␈αis
␈↓ ↓H␈↓done␈αby␈α
the␈αinner␈αmapping␈α
function␈αwhich␈αruns␈α
simultaneously␈αthrough␈αthe␈α
list␈αtower␈α
faces␈αand
␈↓ ↓H␈↓block faces, putting the block face on the tower face.

␈↓ ↓H␈↓A solution to the puzzle is obtained by assigning a value to ␈↓↓puzz␈↓ and evaluating ␈↓↓search[p0]␈↓ with

␈↓ ↓H␈↓␈↓ ∧y␈↓↓    p0 = ␈↓¬(NIL NIL NIL NIL)␈↓↓.  ␈↓ 

␈↓ ↓H␈↓        Obtaining␈α⊂␈↓↓puzz␈↓␈α⊂in␈α⊂the␈α⊂desired␈α⊂form␈α⊂is␈α⊂as␈α⊂complicated␈α⊂a␈α⊂computation␈α⊂as␈α⊂the␈α⊃actual␈α⊂tree
␈↓ ↓H␈↓search.␈α∂ It␈α⊂can␈α∂be␈α∂conveniently␈α⊂done␈α∂by␈α∂a␈α⊂sequence␈α∂of␈α∂assignment␈α⊂statements␈α∂starting␈α⊂with␈α∂the
␈↓ ↓H␈↓following description of the blocks:

␈↓ ↓H␈↓␈↓ αD␈↓↓puzz1 ← ␈↓¬((G B B W R G) (G G B G W R) (G W W R B R) (G G R B W W))␈↓↓␈↓. 

␈↓ ↓H␈↓Here␈αeach␈αblock␈αis␈αrepresented␈αby␈αa␈αlist␈αof␈αthe␈αcolors␈αof␈αthe␈αfaces␈αstarting␈αwith␈αthe␈αtop␈αface,␈αgoing
␈↓ ↓H␈↓around the sides in a clockwise direction and finishing with the bottom face.

␈↓ ↓H␈↓        We␈αneed␈αto␈α
go␈αfrom␈αthis␈α
description␈αof␈αthe␈α
blocks␈αto␈αa␈α
list␈αof␈αthe␈α
possible␈αcycles␈αof␈αcolors␈α
on
␈↓ ↓H␈↓the␈αvertical␈αfaces␈αfor␈αthe␈α24␈αorientations␈αof␈αthe␈αblock.␈α This␈αnot␈αeasy,␈αbecause␈αthe␈αorder␈αin␈αwhich
␈↓ ↓H␈↓we␈α
have␈α
given␈αthe␈α
colors␈α
is␈α
not␈αinvariant␈α
under␈α
rotations␈α
of␈αthe␈α
block.␈α
 An␈α
easy␈αway␈α
out␈α
is␈αto␈α
start
␈↓ ↓H␈↓with␈αa␈αblock␈αwhose␈αfaces␈αare␈αassigned␈αthe␈αnumbers␈α1␈αthru␈α6␈αstarting␈αwith␈αthe␈αtop,␈αgoing␈α
clockwise
␈↓ ↓H␈↓around␈αthe␈αsides␈αand␈αfinishing␈αwith␈αthe␈αbottom.␈α We␈αwrite␈αdown␈αone␈αcycle␈αof␈αside␈αcolors␈αfor␈αeach
␈↓ ↓H␈↓choice␈α∩of␈α⊃the␈α∩face␈α⊃put␈α∩on␈α⊃top␈α∩and␈α⊃get␈α∩the␈α⊃list␈α∩of␈α⊃all␈α∩24␈α⊃cycles␈α∩by␈α⊃appending␈α∩the␈α∩results␈α⊃of
␈↓ ↓H␈↓generating the cyclic permutations of the cycles.  All this is accomplished by the assignment,
␈↓ ↓H␈↓128␈↓ ¬wChapter  VI␈↓ H



␈↓ ↓H␈↓␈↓ β∞␈↓↓puzz2 ← cycles[␈↓¬(2 3 4 5)␈↓↓] * cycles[␈↓¬(2 5 4 3)␈↓↓] * cycles[␈↓¬(1 2 6 4)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ β_␈↓↓      * cycles[␈↓¬(1 4 6 2)␈↓↓] * cycles[␈↓¬(1 3 6 5)␈↓↓] * cycles[␈↓¬(1 5 6 3)␈↓↓]␈↓,

␈↓ ↓H␈↓where the function ␈↓↓cycles␈↓ is defined by

␈↓ ↓H␈↓1.8) ␈↓ βY␈↓↓  cycles u ← maplist[λv: v * upto[u, v], u]                   ␈↓
␈↓ ↓H␈↓1.9) ␈↓ βk␈↓↓upto[u, v] ← ␈↓αif␈↓↓ v = u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . upto[␈↓αd|␈↓↓u, v]␈↓.

␈↓ ↓H␈↓        Next␈αwe␈α
create␈αa␈αlist␈α
of␈αsubstitution␈αlists,␈α
one␈αfor␈αeach␈α
block,␈αexpressing␈αthe␈α
colors␈αof␈αthe␈α
six
␈↓ ↓H␈↓numbered faces.  This is done by the assignment

␈↓ ↓H␈↓␈↓ β%␈↓↓puzz3 ← mapcar[λv: mapcar2[cons, ␈↓¬(1 2 3 4 5 6)␈↓↓, v], puzz1]␈↓. 

␈↓ ↓H␈↓We use these substitutions to get for each block the list of 24 orientations of the block. Thus

␈↓ ↓H␈↓␈↓ α6␈↓↓puzz4 ← mapcar[[λs: mapcar[[λu: mapcar[[λx: ␈↓αd|␈↓↓assoc[x, s]], u]], puzz2]], puzz3]␈↓ 

␈↓ ↓H␈↓␈↓↓puzz4␈↓␈αhas␈αall␈α24␈αorientations␈αof␈αthe␈αfirst␈αblock␈αwhile␈αfor␈αsymmetry␈αreasons␈αwe␈αneed␈αonly␈αconsider
␈↓ ↓H␈↓three as distinct, say the first, ninth, and seventeenth.  So we finally get

␈↓ ↓H␈↓␈↓ αt␈↓↓puzz ← <nth[␈↓αa|␈↓↓puzz4, 1], nth[␈↓αa|␈↓↓puzz4, 9], nth[␈↓αa|␈↓↓puzz4, 17]> . ␈↓αd|␈↓↓puzz4␈↓. 

␈↓ ↓H␈↓The␈α∞program␈α∞when␈α∞compiled␈α∞runs␈α
about␈α∞11␈α∞seconds␈α∞on␈α∞the␈α
KA-10.␈α∞ Interpreted␈α∞it␈α∞runs␈α∞in␈α
2-3
␈↓ ↓H␈↓seconds on the KL-10.  And we have

␈↓ ↓H␈↓␈↓ β,␈↓↓search[p0] = ␈↓¬((G W R B) (R W G B) (B R G W) (W B G R))␈↓↓␈↓ 

␈↓ ↓H␈↓        The␈α
descriptions␈α
for␈α
making␈α
␈↓↓puzz3␈↓␈α
and␈α
␈↓↓puzz4␈↓␈α
make␈α
heavy␈α
use␈α
of␈α
mapping␈αfunctions,␈α
which
␈↓ ↓H␈↓sometimes␈α⊂obscures␈α⊂what␈α⊂is␈α⊂really␈α⊂going␈α⊂on.␈α⊂ Alternate␈α⊂descriptions␈α⊂for␈α⊂these␈α⊂constructions␈α∂are
␈↓ ↓H␈↓given by the following assignments

␈↓ ↓H␈↓␈↓ βx␈↓↓puzz3a ← mapcar[[λv: prup[␈↓¬(1 2 3 4 5 6)␈↓↓, v]], puzz1]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ βx␈↓↓prup[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [␈↓αa|␈↓↓u . ␈↓αa|␈↓↓v] . prup[␈↓αd|␈↓↓u, ␈↓αd|␈↓↓v]␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ βx␈↓↓puzz4a ← mapcar[[λs: sublis[puzz2, s]], puzz3]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ βx␈↓↓sublis[z, a] ← ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ {assoc[z, a]}[λzz: ␈↓αif␈↓↓ ␈↓αn|␈↓↓zz ␈↓αthen␈↓↓ z ␈↓αelse␈↓↓ ␈↓αd|␈↓↓zz]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ sublis[␈↓αa|␈↓↓z, a] . sublis[␈↓αd|␈↓↓z, a]␈↓.

␈↓ ↓H␈↓Of course we have ␈↓↓puzz3 = puzz3a␈↓ and ␈↓↓puzz4 = puzz4a.␈↓

␈↓ ↓H␈↓        A␈αmore␈αsophisticated␈αrepresentation␈αof␈αface␈α
cycles␈αand␈αpartial␈αtowers␈αmakes␈αa␈αfactor␈α
of␈αten
␈↓ ↓H␈↓speedup␈αwithout␈αchanging␈αthe␈αbasic␈αsearch␈αalgorithm.␈α Here␈αa␈αface␈αcycle␈αis␈αrepresented␈αby␈α16␈αbits
␈↓ ↓H␈↓in␈αa␈αword,␈αfour␈αfor␈αeach␈αface,␈αa␈αparticular␈αone␈αof␈αwhich␈αbeing␈αturned␈αon␈αtells␈αus␈αthe␈αcolor␈αof␈αthe
␈↓ ↓H␈↓face.␈α
 If␈α
we␈α
␈↓αbool-or␈↓␈α
these␈α
words␈α
together␈α
for␈α
the␈α
blocks␈α
in␈α
a␈α
partial␈α
tower␈α
we␈α
get␈α
a␈α
word␈α
which␈α
tells
␈↓ ↓H␈↓us␈αfor␈αeach␈αface␈αof␈αthe␈αtower␈αwhat␈α
colors␈αhave␈αbeen␈αused.␈α A␈αparticular␈αface␈αcycle␈αfrom␈α
the␈αnext
␈↓ ↓H␈↓block␈αcan␈αbe␈αadded␈αto␈αthe␈αtower␈αif␈αthe␈α␈↓αbool-and␈↓␈αof␈αits␈αword␈αwith␈αthe␈αword␈αrepresenting␈αthe␈αtower
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠129


␈↓ ↓H␈↓is␈α
zero.␈α
 We␈α
represent␈α
a␈α
position␈α
by␈α
a␈α∞list␈α
of␈α
words␈α
representing␈α
its␈α
partial␈α
towers␈α
with␈α
␈↓¬0␈α∞␈↓as␈α
the
␈↓ ↓H␈↓last␈α
element␈αand␈α
the␈α
highest␈αpartial␈α
tower␈αas␈α
the␈α
first␈αelement.␈α
 The␈α
virtue␈αof␈α
this␈αrepresentation␈α
is
␈↓ ↓H␈↓that␈αit␈αmakes␈α
the␈αdescription␈αof␈α
the␈αalgorithm␈αshort.␈α
 The␈αinitial␈αposition␈α
is␈α␈↓¬(0)␈↓.␈α The␈α
new␈α␈↓↓puzz␈↓
␈↓ ↓H␈↓can be formed from the old one by the assignment

␈↓ ↓H␈↓␈↓ ∧λ␈↓↓puzza ← mapcar[λu: mapcar[bool-cycle, u], puzz]␈↓ 

␈↓ ↓H␈↓where

␈↓ ↓H␈↓1.10)␈↓ β8␈↓↓bool-cycle v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ bool-color ␈↓αa|␈↓↓v + ␈↓¬16 ␈↓↓␈↓π*␈↓↓ bool-cycle ␈↓αd|␈↓↓v␈↓
␈↓ ↓H␈↓1.11)␈↓ β8␈↓↓bool-color x ← ␈↓αif␈↓↓ x = ␈↓¬R ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ if x = ␈↓¬W ␈↓↓␈↓αthen␈↓↓ ␈↓¬2 ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ x = ␈↓¬G ␈↓↓␈↓αthen␈↓↓ ␈↓¬4 ␈↓↓␈↓αelse␈↓↓ ␈↓¬8␈↓↓␈↓.

␈↓ ↓H␈↓Now we need new versions of ␈↓↓lose,␈↓ ␈↓↓ter,␈↓ and ␈↓↓successors.␈↓  These are

␈↓ ↓H␈↓1.12)␈↓ β8␈↓↓lose p ← ␈↓αfalse␈↓↓ , ␈↓
␈↓ ↓H␈↓1.13)␈↓ β8␈↓↓ter p ← [length p = 5]␈↓,
␈↓ ↓H␈↓1.14)␈↓ β8␈↓↓successors p ← mapchoose[[λx: [␈↓αa|␈↓↓p ␈↓αbool-and␈↓↓ x] = ␈↓¬0␈↓↓], ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓                         [λx: [␈↓αa|␈↓↓p ␈↓αbool-or␈↓↓ x] . p], nth[puzza, length p]]␈↓

␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ β8␈↓↓mapchoose[pred, fn, u] ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓1.15)␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ pred ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ fn[␈↓αa|␈↓↓u] . mapchoose[pred, fn, ␈↓αd|␈↓↓u] ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ mapchoose[pred, fn, ␈↓αd|␈↓↓u]␈↓.

␈↓ ↓H␈↓␈↓↓lose␈↓␈αis␈αtrivial,␈αbecause␈αthe␈α␈↓↓mapchoose␈↓␈αis␈αused␈αto␈αmake␈αsure␈αthat␈αonly␈αnon-losing␈αnew␈αpositions␈αare
␈↓ ↓H␈↓generated␈α∂by␈α∂␈↓↓successors.␈↓␈α∂This␈α∂version␈α⊂runs␈α∂in␈α∂a␈α∂little␈α∂less␈α⊂than␈α∂one␈α∂second␈α∂on␈α∂the␈α⊂KA-10.␈α∂ A
␈↓ ↓H␈↓greater␈α⊃speedup␈α⊂can␈α⊃be␈α⊂made␈α⊃by␈α⊃the␈α⊂application␈α⊃of␈α⊂some␈α⊃mathematics.␈α⊂ In␈α⊃fact,␈α⊃with␈α⊂enough
␈↓ ↓H␈↓mathematics, extensive tree search is unnecessary in this problem.



␈↓ ↓H␈↓2.  ␈↓αGame trees.␈↓


␈↓ ↓H␈↓        The␈α⊂positions␈α∂that␈α⊂can␈α∂be␈α⊂reached␈α∂from␈α⊂an␈α⊂initial␈α∂position␈α⊂in␈α∂a␈α⊂game␈α∂form␈α⊂a␈α⊂tree,␈α∂and
␈↓ ↓H␈↓deciding␈α
what␈αmove␈α
to␈α
make␈αoften␈α
involves␈α
searching␈αthis␈α
tree.␈α
 However,␈αgame␈α
trees␈αare␈α
searched
␈↓ ↓H␈↓in␈αa␈αdifferent␈αway␈αthan␈αthe␈αtrees␈αwe␈αhave␈αlooked␈αat␈αpreviously,␈αbecause␈αthe␈αopposing␈αinterests␈αof
␈↓ ↓H␈↓the␈αplayers␈αmakes␈αit␈αnot␈αa␈αsearch␈αfor␈αa␈αjoint␈αline␈αof␈αplay␈αthat␈αwill␈αlead␈αto␈αthe␈αfirst␈αplayer␈α
winning,
␈↓ ↓H␈↓but␈αrather␈αa␈αsearch␈αfor␈αa␈αstrategy␈αthat␈αwill␈αlead␈αto␈αa␈αwin␈αregardless␈αof␈αwhat␈αthe␈αother␈αplayer␈αdoes.
␈↓ ↓H␈↓In␈αthis␈αsection␈αwe␈αdiscuss␈αtwo␈αmethods␈αfor␈αsearching␈αa␈αgame␈αtree.␈α The␈αfirst␈αis␈αa␈αsimple␈αminimax
␈↓ ↓H␈↓search␈α∞which␈α∞examines␈α∞all␈α∞lines␈α∞of␈α∞play␈α∞before␈α
making␈α∞a␈α∞decision.␈α∞ The␈α∞second␈α∞makes␈α∞use␈α∞of␈α
a
␈↓ ↓H␈↓heuristic␈α
known␈αas␈α
the␈ααβ-heuristic␈α
to␈αprune␈α
the␈αsearch␈α
space.␈α In␈α
both␈αcases␈α
the␈α
basic␈αrecursive
␈↓ ↓H␈↓structure␈α
of␈α
the␈αcomputation␈α
is␈α
presented␈αin␈α
a␈α
game␈αindependent␈α
fashion␈α
by␈αletting␈α
the␈α
game␈αbe
␈↓ ↓H␈↓characterized␈αby␈αa␈αcollection␈αof␈αfunctions␈αwhich␈αsatisfy␈αsome␈αgeneral␈αconditions.␈α The␈αgame␈αof␈α2-
␈↓ ↓H␈↓dimensional␈α∂Tic Tac Toe␈α∂will␈α∂be␈α∞used␈α∂as␈α∂an␈α∂example␈α∞application␈α∂of␈α∂these␈α∂game␈α∂tree␈α∞searching
␈↓ ↓H␈↓techniques.

␈↓ ↓H␈↓        In␈α∞the␈α
simplest␈α∞situation␈α
the␈α∞game␈α∞is␈α
characterized␈α∞by␈α
a␈α∞function␈α
␈↓↓successors␈↓␈α∞that␈α∞gives␈α
the
␈↓ ↓H␈↓positions␈α∂that␈α∂can␈α∂be␈α∂reached␈α∂in␈α∂one␈α∂move,␈α∂a␈α∂predicate␈α∂␈↓↓ter␈↓␈α∂that␈α∂tells␈α∂when␈α∂a␈α∂position␈α∂is␈α∂to␈α∂be
␈↓ ↓H␈↓130␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓regarded␈α⊗as␈α⊗terminal␈α⊗for␈α⊗the␈α⊗given␈α⊗analysis,␈α⊗and␈α⊗a␈α⊗function␈α⊗␈↓↓imval␈↓␈α⊗that␈α⊗gives␈α⊗a␈α∃number
␈↓ ↓H␈↓approximating␈α
the␈α
value␈αof␈α
a␈α
terminal␈αposition␈α
to␈α
one␈α
of␈αthe␈α
players.␈α
 We␈αshall␈α
call␈α
this␈αplayer␈α
the
␈↓ ↓H␈↓maximizing␈α
player␈α
and␈α
his␈α
opponent␈α
the␈αminimizing␈α
player.␈α
Usually,␈α
the␈α
numerical␈α
values␈αarise,
␈↓ ↓H␈↓because␈α∂the␈α∂search␈α∂cannot␈α∂be␈α∂carried␈α∂out␈α∂to␈α∂the␈α∂end␈α∂of␈α∂the␈α∂game,␈α∂and␈α∂the␈α∂analysis␈α⊂stops␈α∂with
␈↓ ↓H␈↓reasonably␈α∞static␈α∞positions␈α
that␈α∞can␈α∞be␈α∞evaluated␈α
by␈α∞some␈α∞rule.␈α
 Naturally,␈α∞the␈α∞function␈α∞␈↓↓imval␈↓␈α
is
␈↓ ↓H␈↓chosen␈α
to␈α
be␈α
easy␈α
to␈α
calculate␈α
and␈α
to␈α
correlate␈α
well␈α
with␈α
the␈α
probability␈α
that␈α
the␈αmaximizing␈α
player
␈↓ ↓H␈↓can win the position.

␈↓ ↓H␈↓        Consider␈α
the␈α∞game␈α
of␈α
Tic␈α∞Tac␈α
Toe␈α
played␈α∞in␈α
2-dimensions.␈α
 A␈α∞position␈α
is␈α∞determined␈α
by
␈↓ ↓H␈↓knowing␈α
which␈α
of␈α
the␈α
nine␈α
squares␈α
contain␈α
X's,␈α
O's,␈α
or␈α
blanks.␈α
 The␈α
successors␈α
of␈α
a␈α
position␈α
in
␈↓ ↓H␈↓the␈αcase␈αthat␈αit␈αis␈αthe␈αX-players␈αturn␈αto␈αmove␈αare␈αall␈αthose␈αpositions␈αobtained␈αby␈αputting␈αan␈αX␈αin
␈↓ ↓H␈↓a␈α
blank␈α
square.␈α A␈α
position␈α
is␈αterminal␈α
if␈α
there␈αare␈α
three␈α
X's␈α
or␈αthree␈α
O's␈α
on␈αa␈α
line␈α
or␈αdiagonal,␈α
or
␈↓ ↓H␈↓if␈αthere␈αare␈αno␈αblank␈αsquares.␈αIf␈αwe␈αtake␈α
the␈αX-player␈αto␈αbe␈αthe␈αmaximizing␈αplayer␈αthen␈αwe␈α
could
␈↓ ↓H␈↓assign␈αvalues␈α
to␈αthe␈α
terminal␈αpositions␈α
as␈αfollows.␈α
 In␈αthe␈α
case␈αof␈α
three␈αX's␈α
the␈αvalue␈α
is␈α1,␈α
in␈αthe
␈↓ ↓H␈↓case of three O's the value is -1 and otherwise it is 0.

␈↓ ↓H␈↓        The␈α
simple␈α
minimax␈α
rule␈α
for␈α
finding␈α
the␈αcorrect␈α
move␈α
in␈α
a␈α
position␈α
uses␈α
a␈α
pair␈αof␈α
programs
␈↓ ↓H␈↓␈↓↓(vmin, vmax)␈↓.␈α ␈↓↓vmax␈↓␈αgives␈αthe␈αvalue␈αof␈αa␈αposition␈αto␈αthe␈αmaximizing␈αplayer␈αby␈αusing␈α␈↓↓imval␈↓␈αif␈αthe
␈↓ ↓H␈↓position␈αis␈αterminal␈αand␈αtaking␈αthe␈αmax␈αof␈αthe␈αvalues␈αof␈αthe␈αsuccessor␈αpositions␈αto␈αthe␈α
minimizing
␈↓ ↓H␈↓player otherwise.  ␈↓↓vmin␈↓ similiarly gives the value of a position to the minimizing player.

␈↓ ↓H␈↓        For␈α
this␈α
we␈α
want␈α
programs␈α
for␈α
getting␈α∞the␈α
maximum␈α
or␈α
the␈α
minimum␈α
of␈α
a␈α
function␈α∞on␈α
a
␈↓ ↓H␈↓list, and they are defined as follows:

␈↓ ↓H␈↓2.1) ␈↓ β7␈↓↓maxlis[u, f] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ -∞ ␈↓αelse␈↓↓ max[f[␈↓αa|␈↓↓u], maxlis[␈↓αd|␈↓↓u, f]]␈↓,

␈↓ ↓H␈↓2.2) ␈↓ βF␈↓↓minlis[u, f] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ∞ ␈↓αelse␈↓↓ min[f[␈↓αa|␈↓↓u], minlis[␈↓αd|␈↓↓u, f]]␈↓.

␈↓ ↓H␈↓Here␈αthe␈αsymbols␈α-∞␈αand␈α∞␈αrepresent␈αnumbers␈αthat␈αare␈αsmaller␈αand␈αlarger␈αthan␈αany␈αactual␈αvalues
␈↓ ↓H␈↓that␈αwill␈αoccur␈αin␈αevaluating␈α␈↓↓f.␈↓␈α If␈αthese␈α
numbers␈αare␈αnot␈αavailable,␈αthen␈αit␈αis␈αnecessary␈α
to␈αprime
␈↓ ↓H␈↓the␈α∞program␈α∞with␈α
the␈α∞values␈α∞of␈α
␈↓↓f␈↓␈α∞applied␈α∞to␈α
the␈α∞first␈α∞element␈α
of␈α∞the␈α∞list,␈α
and␈α∞the␈α∞programs␈α
are
␈↓ ↓H␈↓then␈αmeaningless␈αfor␈α
null␈αlists.␈α Iterative␈α
versions␈αof␈αthese␈αprograms␈α
are␈αgenerally␈αbetter;␈α
we␈αgive
␈↓ ↓H␈↓only the iterative version of ␈↓↓maxlis,␈↓ namely

␈↓ ↓H␈↓␈↓ β⊗␈↓↓    maxlis[u, f] ← maxlisa[u, -∞, f],                                      ␈↓
␈↓ ↓H␈↓2.3)
␈↓ ↓H␈↓␈↓ βλ␈↓↓maxlisa[u, x, f] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ maxlisa[␈↓αd|␈↓↓u, max[x, f[␈↓αa|␈↓↓u]], f]␈↓.


␈↓ ↓H␈↓        We now have

␈↓ ↓H␈↓2.4) ␈↓ β.␈↓↓vmax p ← ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ imval p ␈↓αelse␈↓↓ maxlis[successors p, vmin]␈↓,

␈↓ ↓H␈↓2.5) ␈↓ β0␈↓↓vmin p ← ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ imval p ␈↓αelse␈↓↓ minlis[successors p, vmax]␈↓.

␈↓ ↓H␈↓        The␈α
best␈α
move␈α
is␈αdetermined␈α
by␈α
the␈α
pair␈α
of␈αprograms␈α
(␈↓↓movemax,␈↓␈α
␈↓↓movemin).␈↓␈α
 The␈α
key␈αto␈α
the
␈↓ ↓H␈↓computation␈α
is␈α
the␈α
pair␈α
of␈α
programs␈α
(␈↓↓bestmaxa,␈↓␈α
␈↓↓bestmina)␈↓␈α
which␈α
determine␈α
the␈α
optimal␈αmove␈α
from
␈↓ ↓H␈↓a␈α∂list␈α∂of␈α∂moves.␈α∂ They␈α∂call␈α∞each␈α∂other␈α∂recursively␈α∂and␈α∂differ␈α∂only␈α∞in␈α∂that␈α∂one␈α∂is␈α∂for␈α∂when␈α∞the
␈↓ ↓H␈↓maximizing␈αplayer␈αis␈αto␈αmove␈αand␈αthe␈αother␈αis␈αfor␈αwhen␈αthe␈αminimizing␈αplayer␈αis␈αto␈α
move.␈α The
␈↓ ↓H␈↓argument␈α␈↓↓bestmove␈↓␈αis␈αthe␈αbest␈αmove␈αfound␈αso␈αfar␈αand␈α␈↓↓bestval␈↓␈αis␈αvalue␈αof␈αthe␈αresulting␈αposition␈αas
␈↓ ↓H␈↓computed by ␈↓↓f.␈↓  Here are the definitions.
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠131



␈↓ ↓H␈↓2.6) ␈↓ ∧?␈↓↓movemax p ← bestmax[succesors p, vmin]␈↓,

␈↓ ↓H␈↓2.7) ␈↓ ∧A␈↓↓movemin p ← bestmin[succesors p, vmax]␈↓,

␈↓ ↓H␈↓where
␈↓ ↓H␈↓                ␈↓↓bestmax[u, f] ← bestmaxa[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u, f[␈↓αa|␈↓↓u], f]␈↓,
␈↓ ↓H␈↓2.8)
␈↓ ↓H␈↓                ␈↓↓bestmaxa[u, bestmove, bestval, f] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ bestmove␈↓
␈↓ ↓H␈↓                        ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ f[␈↓αa|␈↓↓u] ≤ bestval ␈↓αthen␈↓↓ bestmaxa[␈↓αd|␈↓↓u, bestmove, bestval, f]␈↓
␈↓ ↓H␈↓                        ␈↓↓␈↓αelse␈↓↓ bestmaxa[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u, f[␈↓αa|␈↓↓u], f]␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓                ␈↓↓bestmin[u, f] ← bestmina[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u, f[␈↓αa|␈↓↓u], f]␈↓,
␈↓ ↓H␈↓2.9)
␈↓ ↓H␈↓                ␈↓↓bestmina[u, bestmove, bestval, f] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ bestmove␈↓
␈↓ ↓H␈↓                        ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ f[␈↓αa|␈↓↓u] ≥ bestval ␈↓αthen␈↓↓ bestmina[␈↓αd|␈↓↓u, bestmove, bestval, f]␈↓
␈↓ ↓H␈↓                        ␈↓↓␈↓αelse␈↓↓ bestmina[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u, f[␈↓αa|␈↓↓u], f]␈↓.


␈↓ ↓H␈↓        This␈α⊃straight␈α⊃minimax␈α⊃computation␈α⊃of␈α∩the␈α⊃best␈α⊃move␈α⊃does␈α⊃much␈α⊃more␈α∩computation␈α⊃in
␈↓ ↓H␈↓general than is necessary.  The simplest way to see this is from the move tree of Figure 11.



␈↓"␈↓ ↓H␈↓␈↓ εX8
␈↓"␈↓ ↓H␈↓␈↓ ¬xmax␈↓ ε(≤'
␈↓"␈↓ ↓H␈↓␈↓ ελ≤'␈↓ ε_≥␈↓ ε_ααα 1
␈↓"␈↓ ↓H␈↓␈↓ ¬8min␈↓ ¬h≤'␈↓ ε(`≥
␈↓"␈↓ ↓H␈↓␈↓ ¬H≤'␈↓ εX6
␈↓"␈↓ ↓H␈↓␈↓ ¬8'␈↓ ¬8≥
␈↓"␈↓ ↓H␈↓␈↓ ¬H`≥␈↓ εX9
␈↓"␈↓ ↓H␈↓␈↓ ¬h`≥␈↓ ε(≤'
␈↓"␈↓ ↓H␈↓␈↓ ελ`≥␈↓ ε_'␈↓ ε_ααα x
␈↓"␈↓ ↓H␈↓␈↓ ε(`≥
␈↓"␈↓ ↓H␈↓␈↓ εXx


␈↓ ↓H␈↓␈↓ αl␈↓αFigure 11.␈↓  Move tree showing how simple minimax does too much work.



␈↓ ↓H␈↓We␈α∞see␈α∞from␈α∂this␈α∞figure␈α∞that␈α∞it␈α∂is␈α∞unnecessary␈α∞to␈α∞examine␈α∂the␈α∞moves␈α∞marked␈α∞ ␈↓x␈↓ ␈α∂because␈α∞their
␈↓ ↓H␈↓values␈αhave␈αno␈αeffect␈α
on␈αthe␈αvalue␈αof␈α
the␈αgame␈αor␈αon␈α
the␈αcorrect␈αmove␈αsince␈α
the␈αpresence␈αof␈αthe␈α
␈↓9␈↓
␈↓ ↓H␈↓is␈α∞sufficient␈α∞information␈α
at␈α∞this␈α∞point␈α
to␈α∞show␈α∞that␈α
the␈α∞move␈α∞leading␈α
to␈α∞the␈α∞vertex␈α∞preceding␈α
it
␈↓ ↓H␈↓should not be chosen by the minimizing player.

␈↓ ↓H␈↓        The␈α∞general␈α∞situation␈α∞is␈α∞that␈α∞it␈α∂is␈α∞unnecessary␈α∞to␈α∞examine␈α∞further␈α∞moves␈α∞from␈α∂a␈α∞position
␈↓ ↓H␈↓once␈αa␈α
move␈αis␈α
found␈αthat␈α
refutes␈αmoving␈α
to␈αthat␈αposition␈α
in␈αthe␈α
first␈αplace.␈α
 This␈αidea␈α
is␈αcalled
␈↓ ↓H␈↓the␈α
αβ-heuristic.␈α As␈α
an␈αexample␈α
of␈α
how␈αthis␈α
heuristic␈αmight␈α
be␈α
used,␈αwe␈α
describe␈αthree␈α
collections
␈↓ ↓H␈↓132␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓of␈α~programs:␈α~␈↓↓(valmax, valmin)␈↓,␈α≠␈↓↓(linemax, linemin)␈↓␈α~and␈α~␈↓↓(treemax, treemin)␈↓.␈α≠ The␈α~programs
␈↓ ↓H␈↓␈↓↓(valmax, valmin)␈↓␈α∂return␈α∂the␈α∂value␈α∂of␈α∂the␈α∂game␈α∂to␈α∂the␈α∂corresponding␈α⊂player,␈α∂␈↓↓(linemax, linemin)␈↓
␈↓ ↓H␈↓return␈αthe␈αvalue␈αand␈αa␈αline␈αof␈αplay␈αobtaining␈αthat␈αvalue,␈αand␈α␈↓↓(treemax, treemin)␈↓␈αreturn␈αthe␈αvalue
␈↓ ↓H␈↓and␈αa␈α"proof"␈αthat␈αthis␈αis␈αthe␈αoptimal␈αvalue␈αfor␈αthe␈αcorresponding␈αplayer.␈α The␈α"proof"␈αconsists␈αof
␈↓ ↓H␈↓two␈αtrees␈αof␈αmoves␈αand␈αterminal␈αvalues␈αgiving␈αa␈αstrategy␈αguaranteeing␈αin␈αone␈αcase␈αthat␈αthe␈αvalue
␈↓ ↓H␈↓of␈α⊂the␈α∂game␈α⊂is␈α⊂at␈α∂least␈α⊂the␈α∂value␈α⊂returned␈α⊂and␈α∂in␈α⊂the␈α∂other␈α⊂case␈α⊂that␈α∂it␈α⊂is␈α∂at␈α⊂most␈α⊂the␈α∂value
␈↓ ↓H␈↓returned.␈α Each␈αpair␈αof␈αprograms␈αuses␈αan␈αauxiliary␈αpair␈αof␈αprograms␈αwhich␈αdo␈αthe␈α
processing␈αof
␈↓ ↓H␈↓successor lists.  Thus in each group there are four mutually interacting programs.

␈↓ ↓H␈↓        The␈αuse␈αof␈α
the␈ααβ-heuristic␈αis␈α
the␈αsame␈αin␈α
each␈αgroup,␈αthey␈α
differ␈αin␈αthe␈α
information␈αthat
␈↓ ↓H␈↓they␈α⊃are␈α∩carrying␈α⊃around␈α∩as␈α⊃the␈α∩search␈α⊃progresses.␈α⊃ As␈α∩before␈α⊃we␈α∩assume␈α⊃that␈α∩the␈α⊃functions
␈↓ ↓H␈↓␈↓↓successors,␈↓␈α⊃␈↓↓ter,␈↓␈α⊃and␈α⊃␈↓↓imval␈↓␈α⊃characterize␈α⊃the␈α⊂game.␈α⊃ For␈α⊃purposes␈α⊃of␈α⊃determining␈α⊃properties␈α⊂of
␈↓ ↓H␈↓␈↓↓valmax␈↓␈α∞and␈α
friends,␈α∞the␈α
program␈α∞␈↓↓rectify␈↓␈α
can␈α∞be␈α
assumed␈α∞to␈α
compute␈α∞the␈α
identity␈α∞function.␈α∞ It␈α
is
␈↓ ↓H␈↓included␈α
in␈αorder␈α
to␈αallow␈α
a␈α
more␈αefficient␈α
representation␈αof␈α
the␈α
game␈αand␈α
will␈αbe␈α
discussed␈αin␈α
the
␈↓ ↓H␈↓next section.  ␈↓↓ext␈↓ is used to extract from a position the move leading to that position.

␈↓ ↓H␈↓        The␈α⊗programs␈α∃(␈↓↓valmax,␈↓␈α⊗␈↓↓valmin)␈↓␈α∃each␈α⊗take␈α∃three␈α⊗arguments:␈α∃a␈α⊗position␈α∃␈↓↓p,␈↓␈α⊗and␈α∃the
␈↓ ↓H␈↓parameters␈α␈↓↓alpha␈↓␈αand␈α␈↓↓beta.␈↓␈α ␈↓↓alpha␈↓␈αis␈αthe␈α
maximum␈αvalue␈αthat␈αcan␈αbe␈αguaranteed␈αthe␈α
maximizing
␈↓ ↓H␈↓player␈α∩based␈α∪on␈α∩the␈α∪search␈α∩so␈α∩far,␈α∪and␈α∩␈↓↓beta␈↓␈α∪is␈α∩the␈α∩least␈α∪value␈α∩that␈α∪can␈α∩be␈α∪guaranteed␈α∩the
␈↓ ↓H␈↓minimizing␈α⊂player␈α⊂based␈α⊂on␈α⊂the␈α⊂search␈α⊂so␈α⊂far.␈α⊂ Initially␈α⊂␈↓↓alpha␈↓␈α⊂is␈α⊂essentially␈α⊂-∞␈α⊂and␈α⊂␈↓↓beta␈↓␈α⊂is␈α⊂∞.
␈↓ ↓H␈↓␈↓↓valmax␈↓␈α∞works␈α∞as␈α∂follows.␈α∞ It␈α∞checks␈α∂to␈α∞see␈α∞if␈α∞the␈α∂position␈α∞is␈α∞terminal,␈α∂and␈α∞if␈α∞so␈α∂returns␈α∞␈↓↓imval p␈↓
␈↓ ↓H␈↓directly,␈α
 otherwise␈αit␈α
asks␈α␈↓↓vmaxlis␈↓␈α
for␈αthe␈α
best␈α
value␈α(with␈α
respect␈αto␈α
␈↓↓alpha␈↓␈αand␈α
␈↓↓beta)␈↓␈α
among␈αthe
␈↓ ↓H␈↓successor␈α
positions.␈α
 ␈↓↓vmaxlis␈↓␈αgoes␈α
through␈α
a␈αlist␈α
of␈α
positions,␈αcomputes␈α
the␈α
value␈αof␈α
each␈α
one␈αto␈α
the
␈↓ ↓H␈↓minimizing␈α
player,␈αusing␈α
␈↓↓valmin,␈↓␈αand␈α
does␈αone␈α
of␈αthree␈α
things.␈α If␈α
the␈αvalue␈α
is␈αnot␈α
greater␈αthan
␈↓ ↓H␈↓␈↓↓alpha,␈↓␈αit␈αis␈αignored␈αas␈α
we␈αare␈αguaranteed␈αto␈αbe␈α
able␈αto␈αdo␈αas␈αwell␈α
or␈αbetter␈αby␈αa␈αpreviously␈α
found
␈↓ ↓H␈↓move.␈α
 If␈α
the␈α
value␈α
is␈α
not␈α
less␈α
than␈α
␈↓↓beta␈↓␈α
then␈α
the␈α
computation␈α
is␈α
aborted␈α
as␈α
␈↓↓beta␈↓␈α
is␈α
the␈α
best␈αthat␈α
the
␈↓ ↓H␈↓maximizing␈αplayer␈αcan␈αhope␈αfor␈αand␈αthe␈αminimizing␈αplayer␈αwould␈αnot␈αallow␈αthis␈αline␈αof␈αplay␈αif␈αit
␈↓ ↓H␈↓produced␈α
a␈α
higher␈αvalue.␈α
 If␈α
the␈α
value␈αis␈α
greater␈α
than␈α
␈↓↓alpha␈↓␈αbut␈α
less␈α
than␈α
␈↓↓beta␈↓␈αthen␈α
this␈α
is␈αa␈α
better
␈↓ ↓H␈↓choice␈α∩for␈α∩the␈α∩maximizing␈α∩player␈α∩and␈α∩␈↓↓alpha␈↓␈α⊃is␈α∩updated.␈α∩ When␈α∩the␈α∩list␈α∩is␈α∩exhausted␈α⊃␈↓↓alpha␈↓
␈↓ ↓H␈↓represents␈α
the␈α∞maximum␈α
value␈α
that␈α∞the␈α
maximizing␈α∞player␈α
can␈α
be␈α∞guaranteed␈α
starting␈α∞from␈α
the
␈↓ ↓H␈↓position␈α∪that␈α∩gave␈α∪rise␈α∪to␈α∩the␈α∪initial␈α∪list␈α∩of␈α∪moves.␈α∩ ␈↓↓valmin␈↓␈α∪works␈α∪in␈α∩a␈α∪dual␈α∪fashion.␈α∩ The
␈↓ ↓H␈↓definitions are:

␈↓ ↓H␈↓␈↓ β8␈↓↓valmax[p, alpha, beta] ← ␈↓
␈↓ ↓H␈↓2.10)␈↓ βx␈↓↓␈↓αif␈↓↓ ter[rectify p, alpha, beta] ␈↓αthen␈↓↓ imval p␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ vmaxlis[successors p, alpha, beta]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓vmaxlis[u, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ alpha␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ {valmin[␈↓αa|␈↓↓u, alpha, beta]}[λs: ␈↓
␈↓ ↓H␈↓2.11)␈↓ ∧8␈↓↓␈↓αif␈↓↓ ¬[s > alpha] ␈↓αthen␈↓↓ vmaxlis[␈↓αd|␈↓↓u, alpha, beta]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ s < beta ␈↓αthen␈↓↓ vmaxlis[␈↓αd|␈↓↓u, s, beta]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ beta]␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠133


␈↓ ↓H␈↓␈↓ β8␈↓↓valmin[p, alpha, beta] ← ␈↓
␈↓ ↓H␈↓2.12)␈↓ βx␈↓↓␈↓αif␈↓↓ ter[rectify p, alpha, beta] ␈↓αthen␈↓↓ imval p␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ vminlis[successors p, alpha, beta]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓vminlis[u, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ beta␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ {valmax[␈↓αa|␈↓↓u, alpha, beta]}[λs: ␈↓
␈↓ ↓H␈↓2.13)␈↓ ∧8␈↓↓␈↓αif␈↓↓ ¬[s > alpha] ␈↓αthen␈↓↓ alpha␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ s < beta ␈↓αthen␈↓↓ vminlis[␈↓αd|␈↓↓u, alpha, s]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ vminlis[␈↓αd|␈↓↓u, alpha, beta]]␈↓


␈↓ ↓H␈↓                We see that ␈↓↓(valmax, valmin)␈↓ are related to ␈↓↓(vmax, vmin)␈↓ as follows:

␈↓ ↓H␈↓␈↓ βx␈↓↓valmax[p,alpha,beta]=␈↓
␈↓ ↓H␈↓␈↓ ∧x␈↓↓␈↓αif␈↓↓ vmax[p]≤alpha ␈↓αthen␈↓↓ alpha␈↓
␈↓ ↓H␈↓2.14)␈↓ ∧x␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ alpha<vmax[p]<beta ␈↓αthen␈↓↓ vmax[p]␈↓
␈↓ ↓H␈↓␈↓ ∧x␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ beta≤vmax[p] ␈↓αthen␈↓↓ beta␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓valmin[p,alpha,beta]=␈↓
␈↓ ↓H␈↓␈↓ ∧x␈↓↓␈↓αif␈↓↓ vmin[p]≤alpha ␈↓αthen␈↓↓ alpha␈↓
␈↓ ↓H␈↓2.15)␈↓ ∧x␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ alpha<vmin[p]<beta ␈↓αthen␈↓↓ vmin[p]␈↓
␈↓ ↓H␈↓␈↓ ∧x␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ beta≤vmin[p] ␈↓αthen␈↓↓ beta␈↓


␈↓ ↓H␈↓Thus␈αif␈αwe␈αinitialize␈α␈↓↓valmax␈↓␈αor␈α␈↓↓valmin␈↓␈αwith␈α
a␈αsufficiently␈αsmall␈α␈↓↓alpha␈↓␈αand␈αa␈αsufficiently␈αlarge␈α
␈↓↓beta␈↓
␈↓ ↓H␈↓we␈α
will␈α
get␈α
the␈α
same␈α
answer␈α
as␈α
given␈α
by␈α
the␈αbrute␈α
force␈α
search.␈α
 If␈α
we␈α
miss,␈α
we␈α
can␈α
tell␈α
and␈αtry
␈↓ ↓H␈↓again with a better estimate.

␈↓ ↓H␈↓        The␈αprograms␈α
(␈↓↓linemax,␈↓␈α␈↓↓linemin)␈↓␈αtake␈α
three␈αarguments,␈αthe␈α
same␈αas␈αfor␈α
(␈↓↓valmax,␈↓␈α␈↓↓valmin).␈↓
␈↓ ↓H␈↓The␈α
additional␈α
argument␈α
␈↓↓line␈↓␈α
required␈α
by␈α␈↓↓(lmaxlis, lminlis)␈↓␈α
corresponds␈α
to␈α
the␈α
first␈α
line␈α
of␈αplay
␈↓ ↓H␈↓(list␈α⊃of␈α⊃moves)␈α⊃found␈α⊃which␈α⊂terminates␈α⊃in␈α⊃a␈α⊃position␈α⊃of␈α⊂value␈α⊃␈↓↓alpha␈↓␈α⊃for␈α⊃␈↓↓linemax␈↓␈α⊃or␈α⊃␈↓↓beta␈↓␈α⊂for
␈↓ ↓H␈↓␈↓↓linemin.␈↓␈α⊂ ␈↓↓(lmaxlis, lminlis)␈↓␈α⊃either␈α⊂pass␈α⊃␈↓↓line␈↓␈α⊂along␈α⊂in␈α⊃the␈α⊂case␈α⊃that␈α⊂the␈α⊂value␈α⊃found␈α⊂is␈α⊃not␈α⊂an
␈↓ ↓H␈↓improvement,␈α
or␈αconstruct␈α
a␈α
new␈α␈↓↓line␈↓␈α
from␈α
the␈αone␈α
returned␈α
by␈α␈↓↓(linemin, linemax)␈↓␈α
in␈α
the␈αcase␈α
that
␈↓ ↓H␈↓it␈α∞leads␈α
to␈α∞an␈α
improved␈α∞value.␈α
 The␈α∞result␈α
returned␈α∞is␈α
the␈α∞optimal␈α
value␈α∞␈↓↓cons␈↓ed␈α
onto␈α∞the␈α∞list␈α
of
␈↓ ↓H␈↓moves giving the first line of play found attaining that value.  The definitions are:

␈↓ ↓H␈↓␈↓ β8␈↓↓linemax[p, alpha, beta] ← ␈↓
␈↓ ↓H␈↓2.16)␈↓ βx␈↓↓␈↓αif␈↓↓ ter[rectify p, alpha, beta] ␈↓αthen␈↓↓ <imval p>␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ lmaxlis[successors p, alpha . ␈↓¬ALPHA-CUTOFF␈↓↓, alpha, beta]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓lmaxlis[u, line, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ alpha . line␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ {linemin[␈↓αa|␈↓↓u, alpha, beta]}[λs: ␈↓
␈↓ ↓H␈↓2.17)␈↓ ∧8␈↓↓␈↓αif␈↓↓ ¬[␈↓αa|␈↓↓s > alpha] ␈↓αthen␈↓↓ lmaxlis[␈↓αd|␈↓↓u, line, alpha, beta]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓s < beta ␈↓αthen␈↓↓ lmaxlis[␈↓αd|␈↓↓u, ext ␈↓αa|␈↓↓u . ␈↓αd|␈↓↓s, ␈↓αa|␈↓↓s, beta]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ beta . line]␈↓
␈↓ ↓H␈↓134␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓␈↓ β8␈↓↓linemin[p, alpha, beta] ← ␈↓
␈↓ ↓H␈↓2.18)␈↓ βx␈↓↓␈↓αif␈↓↓ ter[rectify p, alpha, beta] ␈↓αthen␈↓↓ <imval p>␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ lminlis[successors p, beta . ␈↓¬BETA-CUTOFF␈↓↓, alpha, beta]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓lminlis[u, line, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ beta . line␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ {linemax[␈↓αa|␈↓↓u, alpha, beta]}[λs: ␈↓
␈↓ ↓H␈↓2.19)␈↓ ∧8␈↓↓␈↓αif␈↓↓ ¬[␈↓αa|␈↓↓s > alpha] ␈↓αthen␈↓↓ alpha . line␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓s < beta ␈↓αthen␈↓↓ lminlis[␈↓αd|␈↓↓u, ext ␈↓αa|␈↓↓u . ␈↓αd|␈↓↓s, alpha, ␈↓αa|␈↓↓s]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ lminlis[␈↓αd|␈↓↓u, line, alpha, beta]]␈↓


␈↓ ↓H␈↓        The␈α
programs␈α
(␈↓↓treemax,␈↓␈α
␈↓↓treemin)␈↓␈α∞take␈α
the␈α
same␈α
arguments␈α∞as␈α
the␈α
value␈α
and␈α∞line␈α
versions
␈↓ ↓H␈↓do.␈α∩ The␈α∩programs␈α∩␈↓↓(tmaxlis, tminlis)␈↓␈α∩each␈α∪take␈α∩two␈α∩additional␈α∩arguments,␈α∩␈↓↓trmax␈↓␈α∪and␈α∩␈↓↓trmin.␈↓
␈↓ ↓H␈↓These␈α∞are␈α∞move␈α∞trees␈α∞including␈α∞terminal␈α∂values,␈α∞which␈α∞are␈α∞used␈α∞to␈α∞build␈α∞the␈α∂proofs␈α∞previously
␈↓ ↓H␈↓mentioned.␈α If␈αa␈αposition␈α␈↓↓p␈↓␈α
is␈αnon-terminal␈αand␈α␈↓↓u = successors p␈↓␈αthen␈α
␈↓↓tmaxlis[u, ␈↓¬NIL␈↓↓, ␈↓¬NIL␈↓↓, α, β]␈↓
␈↓ ↓H␈↓returns a list ␈↓↓<val, pfmax, pfmin>␈↓ where one of the following is true:

␈↓ ↓H␈↓␈↓ αλ1.) ␈↓↓val␈↓ ≤ α and ␈↓↓pfmin␈↓ is a proof that the value of ␈↓↓p␈↓ to the maximizing player is at most ␈↓↓val.␈↓

␈↓ ↓H␈↓␈↓ αλ2.)␈αα␈α<␈α␈↓↓val␈↓␈α<␈αβ,␈α␈↓↓pfmax␈↓␈αis␈αa␈αproof␈αthat␈αthe␈αvalue␈αof␈α␈↓↓p␈↓␈αto␈αthe␈αmaximizing␈αplayer␈αis␈αat␈αleast␈α␈↓↓val␈↓
␈↓ ↓H␈↓␈↓ αHand ␈↓↓pfmin␈↓ is a proof that it is at most ␈↓↓val.␈↓

␈↓ ↓H␈↓␈↓ αλ3.) β ≤ ␈↓↓val␈↓ and ␈↓↓pfmax␈↓ is a proof that the value of ␈↓↓p␈↓ to the maximizing player is at least ␈↓↓val.␈↓

␈↓ ↓H␈↓Similarly␈α␈↓↓treemin[u, ␈↓¬NIL␈↓↓, ␈↓¬NIL␈↓↓, α, β]␈↓␈αreturns␈αa␈αlist␈α␈↓↓<val, pfmax, pfmin>␈↓␈αwhere␈αthe␈αvalue␈αis␈αnow
␈↓ ↓H␈↓relative to the minimizing player.

␈↓ ↓H␈↓        Note␈α
that␈αin␈α
the␈α
case␈α␈↓↓val ≤ α (≥ β)␈↓␈α
we␈αmake␈α
no␈α
requirement␈αof␈α
␈↓↓pfmax␈↓␈α(␈↓↓pfmin).␈↓␈α
 This␈α
is␈αin
␈↓ ↓H␈↓these␈α
cases␈α
the␈α
searches␈αwere␈α
probably␈α
incomplete␈α
and␈α
the␈αresulting␈α
proof␈α
would␈α
make␈α
no␈αsense.
␈↓ ↓H␈↓The definitions of are:

␈↓ ↓H␈↓␈↓ αx␈↓↓treemax[p, alpha, beta] ← ␈↓
␈↓ ↓H␈↓2.20)␈↓ β8␈↓↓␈↓αif␈↓↓ ter[rectify p, alpha, beta] ␈↓αthen␈↓↓ {imval p}[λv: <v, <v>, <v>>]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ tmaxlis[successors p, alpha . ␈↓¬ALPHA-CUTOFF␈↓↓, ␈↓¬NIL␈↓↓, alpha, beta]␈↓

␈↓ ↓H␈↓␈↓ αx␈↓↓tmaxlis[u, trmax, trmin, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ <alpha, trmax, trmin>␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ {treemin[␈↓αa|␈↓↓u, alpha, beta]}[λs: ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ ¬[␈↓αa|␈↓↓s > alpha] ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓2.21)␈↓ ∧8␈↓↓tmaxlis[␈↓αd|␈↓↓u, trmax, [ext ␈↓αa|␈↓↓u . ␈↓αadd|␈↓↓s] . trmin, alpha, beta]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓s < beta ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓tmaxlis[␈↓αd|␈↓↓u, ext ␈↓αa|␈↓↓u . ␈↓αad|␈↓↓s, [ext ␈↓αa|␈↓↓u . ␈↓αadd|␈↓↓s] . trmin, ␈↓αa|␈↓↓s, beta]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ <beta, ext ␈↓αa|␈↓↓u . ␈↓αad|␈↓↓s, ␈↓¬NIL␈↓↓>]␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠135


␈↓ ↓H␈↓␈↓ αx␈↓↓treemin[p, alpha, beta] ← ␈↓
␈↓ ↓H␈↓2.22)␈↓ β8␈↓↓␈↓αif␈↓↓ ter[rectify p, alpha, beta] ␈↓αthen␈↓↓ {imval p}[λv: <v, <v>, <v>>]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ tminlis[successors p, ␈↓¬NIL␈↓↓, beta . ␈↓¬BETA-CUTOFF␈↓↓, alpha, beta]␈↓

␈↓ ↓H␈↓␈↓ αx␈↓↓tminlis[u, trmax, trmin, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ <beta, trmax, trmin>␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ {treemax[␈↓αa|␈↓↓u, alpha, beta]}[λs: ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ ¬[␈↓αa|␈↓↓s > alpha] ␈↓αthen␈↓↓ <alpha, ␈↓¬NIL␈↓↓, ext ␈↓αa|␈↓↓u . ␈↓αadd|␈↓↓s>␈↓
␈↓ ↓H␈↓2.23)␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓s < beta ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓tminlis[␈↓αd|␈↓↓u, [ext ␈↓αa|␈↓↓u . ␈↓αad|␈↓↓s] . trmax, ext ␈↓αa|␈↓↓u . ␈↓αadd|␈↓↓s, alpha, ␈↓αa|␈↓↓s]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ tminlis[␈↓αd|␈↓↓u, [ext ␈↓αa|␈↓↓u . ␈↓αad|␈↓↓s] . trmax, trmin, alpha, beta]]␈↓


␈↓ ↓H␈↓        We␈α⊂now␈α⊂describe␈α⊂how␈α⊂a␈α⊂move␈α⊂tree,␈α⊃␈↓↓pf,␈↓␈α⊂represents␈α⊂a␈α⊂proof␈α⊂of␈α⊂the␈α⊂sort␈α⊃required␈α⊂above.
␈↓ ↓H␈↓There are four cases.

␈↓ ↓H␈↓1)␈α␈↓↓pf␈↓␈α
is␈αa␈α
proof␈αthat␈αthe␈α
value␈αof␈α
␈↓↓p␈↓␈αto␈αthe␈α
maximizing␈αplayer␈α
is␈αat␈αleast␈α
␈↓↓val␈↓␈αif␈α
one␈αof␈αthe␈α
following
␈↓ ↓H␈↓holds:

␈↓ ↓H␈↓        (i) ␈↓↓p␈↓ is terminal and ␈↓↓pf = <imval p>␈↓ and ␈↓↓imval p ≥ val␈↓

␈↓ ↓H␈↓        (ii)␈α
␈↓↓p␈↓␈α
is␈αnot␈α
terminal␈α
and␈α
␈↓↓pf = mv . pf'␈↓␈αwhere␈α
␈↓↓mv␈↓␈α
is␈α
a␈αmove␈α
leading␈α
to␈α␈↓↓p' ε successors p␈↓␈α
and
␈↓ ↓H␈↓␈↓↓pf'␈↓ is a proof that the value of ␈↓↓p'␈↓ to the minimizing player is at least ␈↓↓val.␈↓

␈↓ ↓H␈↓2)␈α␈↓↓pf␈↓␈αis␈αa␈αproof␈αthat␈αthe␈α
value␈αof␈α␈↓↓p␈↓␈αto␈αthe␈αminimizing␈αplayer␈α
is␈αat␈αleast␈α␈↓↓val␈↓␈αif␈αone␈αof␈αthe␈α
following
␈↓ ↓H␈↓holds:

␈↓ ↓H␈↓        (i) ␈↓↓p␈↓ is terminal and ␈↓↓pf = <imval p>␈↓ and ␈↓↓imval p ≥ val␈↓.

␈↓ ↓H␈↓        (ii)␈α⊃␈↓↓p␈↓␈α⊃is␈α⊃not␈α⊃terminal␈α⊃and␈α⊃␈↓↓pf␈↓␈α⊃is␈α⊃a␈α⊃list␈α⊃of␈α⊃elements␈α⊃of␈α⊃the␈α⊃form␈α⊃␈↓↓mv . pf'␈↓,␈α⊃one␈α⊃for␈α⊃each
␈↓ ↓H␈↓␈↓↓p' ε successors p,␈↓␈αsuch␈αthat␈α␈↓↓mv␈↓␈αis␈αa␈αmove␈αleading␈αfrom␈α␈↓↓p␈↓␈αto␈α␈↓↓p'␈↓␈αand␈α␈↓↓pf'␈↓␈αis␈αa␈αproof␈αthat␈αthe␈αvalue
␈↓ ↓H␈↓of ␈↓↓p'␈↓ to the maximizing player is at least ␈↓↓val.␈↓

␈↓ ↓H␈↓Cases␈α∩3)␈α∩and␈α∪4)␈α∩are␈α∩obtained␈α∩from␈α∪1)␈α∩and␈α∩2)␈α∩by␈α∪interchanging␈α∩the␈α∩words␈α∪maximizing␈α∩and
␈↓ ↓H␈↓minimizing and reversing the sense of the inequalities throughout.

␈↓ ↓H␈↓        The␈α
αβ␈α
algorithm␈α
can␈α
also␈α
be␈αused␈α
to␈α
in␈α
the␈α
case␈α
that␈α
there␈αis␈α
a␈α
way␈α
to␈α
estimate␈α
the␈αvalue␈α
of
␈↓ ↓H␈↓a␈α⊃position␈α⊃␈↓↓p.␈↓␈α⊃ Suppose␈α⊃we␈α⊃estimate␈α⊃that␈α⊃␈↓↓a < vmax p < b␈↓.␈α⊃ Then␈α⊃if␈α⊃␈↓↓valmax[p, a, b] = val␈↓␈α⊃with
␈↓ ↓H␈↓␈↓↓a < val < b␈↓␈α⊂then␈α⊂we␈α∂win␈α⊂as␈α⊂we␈α∂now␈α⊂have␈α⊂the␈α∂correct␈α⊂value␈α⊂and␈α∂hopefully␈α⊂the␈α⊂narrower␈α∂range
␈↓ ↓H␈↓caused␈α
even␈α
more␈α
pruning␈αthan␈α
usual.␈α
 If␈α
␈↓↓val ≤a␈↓␈αthen␈α
we␈α
must␈α
lower␈αthe␈α
estimate␈α
of␈α
the␈αvalue␈α
and
␈↓ ↓H␈↓try again.  Similarly if ␈↓↓val ≥ b␈↓ then we raise the estimate.

␈↓ ↓H␈↓        The␈αreduction␈αin␈αnumber␈αof␈αpositions␈αexamined␈αgiven␈αby␈αthe␈ααβ␈αalgorithm␈αover␈αthe␈αsimple
␈↓ ↓H␈↓minimax␈α
algorithm␈α
depends␈α
on␈α
the␈α
order␈α
in␈α
which␈α
the␈α
moves␈α
are␈α
examined.␈α
 In␈α
the␈α∞worst␈α
case,
␈↓ ↓H␈↓the␈αmoves␈αhappen␈αto␈αbe␈αexamined␈αin␈αinverse␈αorder␈αof␈αmerit␈αin␈αevery␈αposition␈αon␈αthe␈αtree,␈αi.e.␈αthe
␈↓ ↓H␈↓worst␈αmove␈αfirst.␈α In␈αthat␈αcase,␈αthere␈αis␈αno␈αimprovement␈αover␈αminimax.␈α The␈αbest␈αcase␈αis␈αthe␈αone
␈↓ ↓H␈↓in␈α
which␈α
the␈α
best␈α
move␈α
in␈α
every␈α
position␈α
is␈α
examined␈α
first.␈α
 If␈α
we␈α
look␈α
␈↓↓n␈↓␈α
moves␈α
deep␈α
on␈α
a␈αtree
␈↓ ↓H␈↓that␈αhas␈α␈↓↓k␈↓␈αsuccessors␈αto␈αeach␈αposition,␈αthen␈αminimax␈αlooks␈αat␈α␈↓↓k␈↓∧n␈↓␈αpositions␈αwhile␈ααβ␈αlooks␈αat␈αabout
␈↓ ↓H␈↓only␈α∂␈↓↓k␈↓∧n/2␈↓.␈α∂ Thus␈α∂a␈α⊂program␈α∂that␈α∂looks␈α∂at␈α⊂10␈↓∧4␈↓␈α∂moves␈α∂with␈α∂αβ␈α∂might␈α⊂have␈α∂to␈α∂look␈α∂at␈α⊂10␈↓∧8␈↓␈α∂with
␈↓ ↓H␈↓minimax.␈α For␈αthis␈αreason,␈αgame␈αplaying␈αprograms␈αusing␈ααβ␈αmake␈αa␈αbig␈αeffort␈αto␈αinclude␈αas␈αgood
␈↓ ↓H␈↓136␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓as␈α
possible␈αan␈α
ordering␈αof␈α
moves␈αinto␈α
the␈α␈↓↓successors␈↓␈α
function.␈α When␈α
there␈αis␈α
a␈αdeep␈α
tree␈αsearch␈α
to
␈↓ ↓H␈↓be␈αdone,␈αthe␈αway␈αto␈αmake␈αthe␈αordering␈αis␈αwith␈αa␈αshort␈αlook-ahead␈αto␈αa␈αmuch␈αsmaller␈αdepth␈αthan
␈↓ ↓H␈↓the␈α⊂main␈α∂search.␈α⊂ Still␈α⊂shorter␈α∂look-aheads␈α⊂are␈α∂used␈α⊂deeper␈α⊂in␈α∂the␈α⊂tree,␈α∂and␈α⊂beyond␈α⊂a␈α∂certain
␈↓ ↓H␈↓depth, non-look-ahead ordering methods are of decreasing complexity.

␈↓ ↓H␈↓        A␈α
version␈α
of␈α
αβ␈α
incorporating␈α∞optimistic␈α
and␈α
pessimistic␈α
evaluations␈α
of␈α
positions␈α∞was␈α
first
␈↓ ↓H␈↓proposed␈α∂by␈α∂McCarthy␈α∂about␈α∂1958.␈α∂ Edwards␈α∂and␈α∂Hart␈α∂at␈α∂M.I.T.␈α∂about␈α∂1959␈α∂proved␈α∂that␈α∞the
␈↓ ↓H␈↓present␈α∞version␈α∞of␈α∞αβ␈α∂works␈α∞and␈α∞calculated␈α∞the␈α∞improvement␈α∂it␈α∞gives␈α∞over␈α∞minimax.␈α∂ The␈α∞first
␈↓ ↓H␈↓publication,␈α∃however,␈α⊗was␈α∃Brudno␈α∃[1963].␈α⊗ For␈α∃additional␈α∃discussion␈α⊗of␈α∃αβ␈α⊗techinques␈α∃see
␈↓ ↓H␈↓Nilsson[1971], Knuth and Moore[1975] or Winston[1977].

␈↓ ↓H␈↓        It␈αis␈αworth␈αnoting␈αthat␈ααβ␈αwas␈αnot␈αused␈αin␈αthe␈αearly␈αchess␈αplaying␈αprograms␈αin␈αspite␈αof␈αthe
␈↓ ↓H␈↓fact␈αthat␈αit␈αis␈αclearly␈αused␈αin␈αany␈αhuman␈αplay.␈α Its␈αnon-use,␈αtherefore,␈αrepresents␈αa␈αfailure␈αof␈αself-
␈↓ ↓H␈↓observation.␈α Very␈αlikely,␈αthere␈αare␈αa␈αnumber␈αof␈αother␈αalgorithms␈αused␈αin␈αhuman␈αthought␈αthat␈αwe
␈↓ ↓H␈↓have␈α⊃not␈α⊃noticed␈α⊂and␈α⊃incorporated␈α⊃in␈α⊂our␈α⊃programs.␈α⊃ To␈α⊂the␈α⊃extent␈α⊃that␈α⊂this␈α⊃is␈α⊃so,␈α⊂artificial
␈↓ ↓H␈↓intelligence will be ␈↓↓a posteriori␈↓ obvious even though it is ␈↓↓a priori␈↓ very difficult.



␈↓ ↓H␈↓3.  ␈↓αThe hidden board trick.␈↓


␈↓ ↓H␈↓        The␈α⊃program␈α⊃␈↓↓rectify␈↓␈α⊃is␈α⊂used␈α⊃to␈α⊃implement␈α⊃a␈α⊂particular␈α⊃method␈α⊃of␈α⊃representing␈α⊃a␈α⊂game
␈↓ ↓H␈↓known␈α⊂as␈α⊂the␈α⊂"hidden␈α⊂board␈α⊂trick".␈α⊂ It␈α∂basically␈α⊂relies␈α⊂on␈α⊂side␈α⊂effects␈α⊂to␈α⊂produce␈α⊂an␈α∂alternate
␈↓ ↓H␈↓representation␈α
of␈αa␈α
given␈α
position␈αwhen␈α
it␈α
is␈αneeded.␈α
 Here␈α
a␈αposition␈α
is␈α
represented␈αby␈α
a␈α
list␈αof
␈↓ ↓H␈↓moves␈α
leading␈α
to␈α
it␈α
with␈α
the␈αmost␈α
recent␈α
move␈α
at␈α
the␈α
head␈α
of␈αthe␈α
list␈α
and␈α
the␈α
first␈α
move␈α
of␈αthe
␈↓ ↓H␈↓game␈α∞at␈α∞the␈α∂end␈α∞of␈α∞the␈α∂list.␈α∞ This␈α∞is␈α∞an␈α∂efficient␈α∞representation␈α∞with␈α∂respect␈α∞to␈α∞storage␈α∂for␈α∞two
␈↓ ↓H␈↓reasons.␈α One␈αis␈αthat␈αlists␈αof␈αpositions␈αwill␈αfrequently␈αhave␈αonly␈αthe␈αlast␈αfew␈αmoves␈αdiffering␈αand
␈↓ ↓H␈↓can␈αbe␈αrepresented␈αas␈αmerging␈αlist␈αstructures.␈α The␈αsecond␈αis␈αthat␈αother␈αrepresentations␈αfrequently
␈↓ ↓H␈↓take␈α∞the␈α∞form␈α
of␈α∞a␈α∞collection␈α
of␈α∞tables␈α∞and␈α∞the␈α
representation␈α∞of␈α∞each␈α
position␈α∞requires␈α∞a␈α∞lot␈α
of
␈↓ ↓H␈↓space.␈α∞ However␈α∞it␈α
is␈α∞often␈α∞easier␈α
to␈α∞compute␈α∞the␈α
successors,␈α∞determine␈α∞if␈α
a␈α∞position␈α∞is␈α
terminal,
␈↓ ↓H␈↓and␈α
compute␈α
its␈α
value␈α
using␈α
a␈α
more␈α
spacious␈α
representation.␈α
 To␈α
take␈α
advantage␈α
of␈α
this␈α
fact␈αwe
␈↓ ↓H␈↓keep␈α∂around␈α⊂a␈α∂representation␈α∂of␈α⊂the␈α∂position␈α∂of␈α⊂current␈α∂interest,␈α∂the␈α⊂hidden␈α∂board,␈α∂in␈α⊂a␈α∂form
␈↓ ↓H␈↓convenient␈α∞for␈α∞doing␈α∞these␈α∞computations.␈α∞ A␈α∞list␈α∂of␈α∞moves␈α∞leading␈α∞to␈α∞that␈α∞position␈α∞is␈α∂also␈α∞kept.
␈↓ ↓H␈↓The␈α∪function␈α∀␈↓↓rectify␈↓␈α∪is␈α∪used␈α∀to␈α∪make␈α∪a␈α∀new␈α∪position␈α∪current.␈α∀ It␈α∪returns␈α∪the␈α∀new␈α∪position
␈↓ ↓H␈↓unchanged,␈α⊃but␈α⊃has␈α⊃the␈α⊃side␈α⊃effect␈α⊃of␈α⊃making␈α⊃it␈α⊃the␈α⊃current␈α⊃position␈α⊃as␈α⊃far␈α⊃as␈α⊃the␈α⊃alternate
␈↓ ↓H␈↓representation␈α
is␈α
concerned.␈α
 This␈α
is␈α
accomplished␈α
by␈α
using␈α
␈↓↓commontail␈↓␈α
to␈α
find␈α
out␈α
where␈α
in␈αthe
␈↓ ↓H␈↓past␈α
the␈α
current␈α
and␈αnew␈α
positions␈α
meet␈α
,␈αbacking␈α
up␈α
to␈α
this␈αposition␈α
and␈α
then␈α
moving␈αforward
␈↓ ↓H␈↓along␈αthe␈αpath␈α
of␈αthe␈αnew␈αposition.␈α
 This␈αrequires␈αthe␈α
additional␈αgame␈αdependent␈αfunctions␈α
␈↓↓revert␈↓
␈↓ ↓H␈↓for␈α∀taking␈α∀back␈α∀moves,␈α∀and␈α∀␈↓↓update␈↓␈α∀for␈α∪making␈α∀moves.␈α∀ The␈α∀programs␈α∀for␈α∀␈↓↓rectify␈↓␈α∀and␈α∪its
␈↓ ↓H␈↓auxiliaries follow.
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠137


␈↓ ↓H␈↓␈↓ β8␈↓↓        rectify p ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓          ␈↓αprogram␈↓↓ [z, q]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓               q ← commontail[p, p1]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓           ␈↓¬L1:␈↓↓ ␈↓αif␈↓↓ q = p1 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L2␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓               revert[]␈↓
␈↓ ↓H␈↓3.1)␈↓ β8␈↓↓               ␈↓αgo to␈↓↓ ␈↓¬L1␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓           ␈↓¬L2:␈↓↓ z ← listsubt[p, p1]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓           ␈↓¬L3:␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓z ␈↓αthen␈↓↓ return p␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓               update ␈↓αa|␈↓↓z␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓               z ← ␈↓αd|␈↓↓z␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓               ␈↓αgo to␈↓↓ ␈↓¬L3␈↓↓␈↓

␈↓ ↓H␈↓3.2)␈↓ β8␈↓↓        commontail[u, v] ← reverse commonhead[reverse u, reverse v]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓        commonhead[u, v] ← ␈↓
␈↓ ↓H␈↓3.3)␈↓ β8␈↓↓          ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓v ∨ ¬[␈↓αa|␈↓↓u = ␈↓αa|␈↓↓v] ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓          ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . commonhead[␈↓αd|␈↓↓u, ␈↓αd|␈↓↓v]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓        listsubt[u, v] ← listsubta[u, length u - length v, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓3.4)
␈↓ ↓H␈↓␈↓ β8␈↓↓        listsubta[u, n, z] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓          ␈↓αif␈↓↓ n = 0 ␈↓αthen␈↓↓ z ␈↓αelse␈↓↓ listsubta[␈↓αd|␈↓↓u, sub1 n, ␈↓αa|␈↓↓u . z]␈↓




␈↓ ↓H␈↓4.  ␈↓α2-dimensional Tic Tac Toe.␈↓


␈↓ ↓H␈↓        As␈α∞a␈α∞simple␈α
example␈α∞of␈α∞how␈α∞the␈α
programs␈α∞described␈α∞in␈α∞the␈α
previous␈α∞two␈α∞sections␈α∞can␈α
be
␈↓ ↓H␈↓used␈αwe␈αgive␈αa␈αset␈αof␈αprograms␈αcharacterizing␈αthe␈αgame␈αof␈α2-dimensional␈αTic␈αTac␈αToe␈αand␈αshow
␈↓ ↓H␈↓the␈α_results␈α_of␈α_some␈α_computations␈α_using␈α_the␈α_programs␈α_employing␈α_the␈α→αβ-heuristic.␈α_ This
␈↓ ↓H␈↓characterization␈α∂of␈α∂Tic␈α∂Tac␈α∂Toe␈α∂improves␈α∂the␈α∞efficiency␈α∂of␈α∂the␈α∂search␈α∂routines␈α∂by␈α∂doing␈α∞some
␈↓ ↓H␈↓precomputing␈αof␈αposition␈αvalues␈αand␈αthus␈αpruning␈αand␈αordering␈αthe␈αlist␈αof␈αsuccessors␈αfor␈αa␈αgiven
␈↓ ↓H␈↓position.

␈↓ ↓H␈↓        The␈αnine␈αsquares␈αof␈αthe␈αTic␈αTac␈αToe␈αboard␈αare␈αnumbered␈αfrom␈αleft␈αto␈αright␈αstarting␈αfrom
␈↓ ↓H␈↓the␈αtop␈αrow.␈α The␈αeight␈αlines␈αare␈αnumbered␈αsimilarly␈αwith␈αthe␈αhorizontal␈αlines␈αfirst,␈αthen␈αvertical,
␈↓ ↓H␈↓then␈α
the␈α
two␈α
diagonals␈α
There␈α
are␈α
several␈αglobal␈α
arrays␈α
and␈α
variables.␈α
 These␈α
are␈α
initialized␈αby␈α
the
␈↓ ↓H␈↓programs␈α
␈↓↓commence␈↓␈α∞and␈α
␈↓↓newgame.␈↓␈α∞ The␈α
ith␈α∞element␈α
of␈α
␈↓↓lines␈↓␈α∞is␈α
a␈α∞list␈α
of␈α∞lines␈α
containing␈α∞the␈α
ith
␈↓ ↓H␈↓square.␈α ␈↓↓xcount␈↓␈αtells␈αhow␈αmany␈α␈↓¬X␈↓'s␈αare␈αon␈αa␈αgiven␈αline,␈α␈↓↓ocount␈↓␈αdoes␈αthe␈αsame␈αfor␈α␈↓¬O␈↓'s.␈α ␈↓↓p1␈↓␈αis␈αthe␈αlist
␈↓ ↓H␈↓of␈α∂moves␈α∂leading␈α∂to␈α∂the␈α∂position␈α∂represented␈α∂by␈α∂the␈α∂state␈α∂of␈α∂the␈α∂global␈α∂variables.␈α∂ (A␈α∂move␈α∂is
␈↓ ↓H␈↓represented␈α
by␈αthe␈α
number␈αof␈α
the␈α
square␈αfilled.) ␈α
␈↓↓xs,␈↓␈α␈↓↓os,␈↓␈α
and␈α
␈↓↓bs␈↓␈αare␈α
lists␈αof␈α
the␈αsquares␈α
containing
␈↓ ↓H␈↓␈↓¬X␈↓'s,␈α
␈↓¬O␈↓'s,␈α
and␈α
blanks␈α
respectively.␈α
 ␈↓↓w␈↓␈α
tells␈α
whose␈α
turn␈αit␈α
is␈α
to␈α
move.␈α
 If␈α
␈↓¬T␈↓␈α
then␈α
it␈α
is␈α
the␈α␈↓¬O␈↓-players␈α
turn,
␈↓ ↓H␈↓if␈α␈↓¬NIL␈↓␈α
then␈αit␈αis␈α
the␈α␈↓¬X␈↓-players␈αturn.␈α
 ␈↓↓level␈↓␈αis␈αthe␈α
number␈αof␈αmoves␈α
so␈αfar␈α(the␈α
length␈αof␈α␈↓↓p1).␈↓␈α
 ␈↓↓count␈↓
␈↓ ↓H␈↓is the total number of moves examined.

␈↓ ↓H␈↓        For␈α∞the␈α∞␈↓¬X␈↓-player␈α∞a␈α∂winning␈α∞position␈α∞has␈α∞value␈α∂␈↓¬10␈↓-␈↓↓level,␈↓␈α∞any␈α∞other␈α∞terminal␈α∂position␈α∞has
␈↓ ↓H␈↓value␈α∩␈↓¬0.␈α∩␈↓␈α∩For␈α∪the␈α∩␈↓¬O␈↓-player␈α∩a␈α∩winning␈α∩position␈α∪has␈α∩value␈α∩␈↓↓level-␈↓␈↓¬10␈α∩␈↓and␈α∩any␈α∪other␈α∩terminal
␈↓ ↓H␈↓position has value ␈↓¬0. ␈↓
␈↓ ↓H␈↓138␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓        A␈αposition␈αis␈αconsidered␈αterminal␈αif␈αthere␈αare␈αno␈αblank␈αspaces␈αleft,␈αor␈αif␈α␈↓↓␈↓¬10␈↓↓ - level ≤ α␈↓,␈αor␈αif
␈↓ ↓H␈↓␈↓↓-␈↓¬10␈↓↓ + level ≥β␈↓,␈αor␈αif␈αit␈αis␈αthe␈α␈↓¬X␈↓-players␈αturn␈αto␈αmove␈αand␈α␈↓↓xcount␈↓␈αis␈α␈↓¬3␈α␈↓for␈αsome␈αline,␈αor␈αif␈αit␈αis␈αthe
␈↓ ↓H␈↓␈↓¬O␈↓-players turn and ␈↓↓ocount␈↓ is ␈↓¬3 ␈↓for some line.

␈↓ ↓H␈↓        ␈↓↓successors␈↓␈α
uses␈α
␈↓↓sort␈↓␈α
and␈α
its␈α
auxiliaries␈α
to␈α
prune␈α
and␈α
sort␈α
the␈α
list␈α
of␈α
possible␈α
moves␈α∞from␈α
a
␈↓ ↓H␈↓non-terminal␈αposition␈α
according␈αto␈α
the␈αfollowing␈α
rules.␈α If␈αsome␈α
move␈αis␈α
a␈αwin␈α
then␈αreturn␈α
a␈αlist
␈↓ ↓H␈↓containing␈α
only␈α
that␈α
move,␈α
otherwise␈α
if␈α
some␈αmove␈α
is␈α
required␈α
to␈α
keep␈α
the␈α
opponent␈αfrom␈α
winning
␈↓ ↓H␈↓then␈α
that␈αis␈α
returned␈α
as␈αthe␈α
single␈αpossibility,␈α
otherwise␈α
if␈αsome␈α
move␈αis␈α
a␈α
double-threat␈α(creates
␈↓ ↓H␈↓two␈αwinning␈αmoves␈αfor␈α
the␈αplayer)␈αthen␈αit␈αis␈α
returned␈αas␈αthe␈αonly␈α
possibility,␈αotherwise␈αa␈αlist␈αof␈α
all
␈↓ ↓H␈↓possible␈αmoves␈αis␈αreturned␈αwith␈αthe␈αthreats␈α(moves␈αcreating␈αa␈αwinning␈αmove␈αfor␈αthe␈αplayer)␈αat␈α
the
␈↓ ↓H␈↓front of the list.

␈↓ ↓H␈↓        The programs ␈↓↓revert␈↓ and ␈↓↓update␈↓ take back and make moves in the obvious way.
␈↓ ↓H␈↓        Here are the definitions: (note that the numbers are octal)

␈↓ ↓H␈↓␈↓ α8␈↓↓        commence[] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αprogram␈↓↓ []␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               array[lines, ␈↓¬T␈↓↓, 12]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               array[xcount, fixnum, 11]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               array[ocount, fixnum, 11]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               store[lines 1, ␈↓¬(1 4 7)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               store[lines 2, ␈↓¬(1 5)␈↓↓]␈↓
␈↓ ↓H␈↓4.1)␈↓ α8␈↓↓               store[lines 3, ␈↓¬(1 6 10)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               store[lines 4, ␈↓¬(2 4)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               store[lines 5, ␈↓¬(2 5 7 10)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               store[lines 6, ␈↓¬(2 6)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               store[lines 7, ␈↓¬(3 4 10)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               store[lines 10, ␈↓¬(3 5)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               store[lines 11, ␈↓¬(3 6 7)␈↓↓]␈↓

␈↓ ↓H␈↓4.2)␈↓ α8␈↓↓        ext p ← ␈↓αa|␈↓↓p␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        newgame[] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αprogram␈↓↓ [n]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               n ← 0␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓            ␈↓¬L:␈↓↓ n ← add1 n␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               store[xcount n, 0]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               store[ocount n, 0]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               ␈↓αif␈↓↓ n < 10 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L␈↓↓␈↓
␈↓ ↓H␈↓4.3)␈↓ α8␈↓↓               p1 ← ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               xs ← ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               os ← ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               bs ← ␈↓¬(1 2 3 4 5 6 7 10 11)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               w ← ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               level ← 0␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               count ← 0␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               return ␈↓¬(NEW GAME)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠139


␈↓ ↓H␈↓␈↓ α8␈↓↓        ter[p, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ¬␈↓αn|␈↓↓p␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ∧ [level = 11␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓             ∨ 11 - level < alpha␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓             ∨ -11 + level > beta␈↓
␈↓ ↓H␈↓4.4)␈↓ α8␈↓↓             ∨ [␈↓αprogram␈↓↓ [n]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                     n ← 0␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓¬L2:␈↓↓ n ← add1 n␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                     ␈↓αif␈↓↓ 3 = [␈↓αif␈↓↓ w ␈↓αthen␈↓↓ xcount n ␈↓αelse␈↓↓ ocount n] ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                       return ␈↓¬T␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                     ␈↓αif␈↓↓ n < 10 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L2␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                     return ␈↓¬NIL␈↓↓]]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        imval p ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓            [␈↓αprogram␈↓↓ [n]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                  n ← 0␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓              ␈↓¬L3:␈↓↓ n ← add1 n␈↓
␈↓ ↓H␈↓4.5)␈↓ α8␈↓↓                  ␈↓αif␈↓↓ 3 = xcount n ␈↓αthen␈↓↓ return 12 - level␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                  ␈↓αif␈↓↓ n < 10 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L3␈↓↓ ␈↓αelse␈↓↓ return 0]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αelse␈↓↓ ␈↓αprogram␈↓↓ [n]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                     n ← 0␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓¬L4:␈↓↓ n ← add1 n␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                     ␈↓αif␈↓↓ 3 = ocount n ␈↓αthen␈↓↓ return -12 + level␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                     ␈↓αif␈↓↓ n < 10 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L4␈↓↓ ␈↓αelse␈↓↓ return 0␈↓

␈↓ ↓H␈↓4.6)␈↓ α8␈↓↓        successors p ← sort mapcar[[λx: x . p], bs]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        revert[] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αprogram␈↓↓ [a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               level ← sub1 level␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               bs ← ␈↓αa|␈↓↓[␈↓αif␈↓↓ w ␈↓αthen␈↓↓ xs ␈↓αelse␈↓↓ os] . bs␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ xs ← ␈↓αd|␈↓↓xs ␈↓αelse␈↓↓ os ← ␈↓αd|␈↓↓os␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               a ← lines ␈↓αa|␈↓↓p1␈↓
␈↓ ↓H␈↓4.7)␈↓ α8␈↓↓           ␈↓¬L5:␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓a ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L6␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ store[xcount ␈↓αa|␈↓↓a, sub1 xcount ␈↓αa|␈↓↓a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ␈↓αelse␈↓↓ store[ocount ␈↓αa|␈↓↓a, sub1 ocount ␈↓αa|␈↓↓a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               a ← ␈↓αd|␈↓↓a␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               ␈↓αgo to␈↓↓ ␈↓¬L5␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓           ␈↓¬L6:␈↓↓ w ← ¬w␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               p1 ← ␈↓αd|␈↓↓p1␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               return ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓140␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓␈↓ α8␈↓↓        update m ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αprogram␈↓↓ [a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               level ← add1 level␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ os ← m . os ␈↓αelse␈↓↓ xs ← m . xs␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               bs ← delete[m, bs]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               p1 ← m . p1␈↓
␈↓ ↓H␈↓4.8)␈↓ α8␈↓↓               count ← add1 count␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               a ← lines m␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓           ␈↓¬L7:␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓a ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L8␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ store[ocount ␈↓αa|␈↓↓a, add1 ocount ␈↓αa|␈↓↓a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ␈↓αelse␈↓↓ store[xcount ␈↓αa|␈↓↓a, add1 xcount ␈↓αa|␈↓↓a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               a ← ␈↓αd|␈↓↓a␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               ␈↓αgo to␈↓↓ ␈↓¬L7␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓           ␈↓¬L8:␈↓↓ w ← ¬w␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               return[]␈↓

␈↓ ↓H␈↓4.9)␈↓ α8␈↓↓        sort u ← sorta[u, ␈↓¬NIL␈↓↓, ␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        sorta[u, th, ord] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ [th * ord]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αelse␈↓↓ ␈↓αif␈↓↓ win ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ <␈↓αa|␈↓↓u>␈↓
␈↓ ↓H␈↓4.10)␈↓ α8␈↓↓          ␈↓αelse␈↓↓ ␈↓αif␈↓↓ answer ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ sortb[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αelse␈↓↓ ␈↓αif␈↓↓ doubleth ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ sortc[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αelse␈↓↓ ␈↓αif␈↓↓ threat ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ sorta[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . th, ord]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αelse␈↓↓ sorta[␈↓αd|␈↓↓u, th, ␈↓αa|␈↓↓u . ord]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        sortb[u, m] ← ␈↓
␈↓ ↓H␈↓4.11)␈↓ α8␈↓↓          ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ <m> ␈↓αelse␈↓↓ ␈↓αif␈↓↓ win ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ <␈↓αa|␈↓↓u> ␈↓αelse␈↓↓ sortb[␈↓αd|␈↓↓u, m]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        sortc[u, m] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ <m>␈↓
␈↓ ↓H␈↓4.12)␈↓ α8␈↓↓          ␈↓αelse␈↓↓ ␈↓αif␈↓↓ win ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ <␈↓αa|␈↓↓u>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αelse␈↓↓ ␈↓αif␈↓↓ answer ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ sortb[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αelse␈↓↓ sortc[␈↓αd|␈↓↓u, m]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        win p ← ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ orlis[[λx: 2 = ocount x], lines ␈↓αa|␈↓↓p]␈↓
␈↓ ↓H␈↓4.13)␈↓ α8␈↓↓                ␈↓αelse␈↓↓ orlis[[λx: 2 = xcount x], lines ␈↓αa|␈↓↓p]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        answer p ← ␈↓
␈↓ ↓H␈↓4.14)␈↓ α8␈↓↓          ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ orlis[[λx: 2 = xcount x], lines ␈↓αa|␈↓↓p]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αelse␈↓↓ orlis[[λx: 2 = ocount x], lines ␈↓αa|␈↓↓p]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        doubleth p ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          twolis[[λx: ␈↓
␈↓ ↓H␈↓4.15)␈↓ α8␈↓↓                  1 = [␈↓αif␈↓↓ w ␈↓αthen␈↓↓ ocount x ␈↓αelse␈↓↓ xcount x]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                  ∧ orlis[[λw: x ε lines w], delete[␈↓αa|␈↓↓p, bs]]], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 lines ␈↓αa|␈↓↓p]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        twolis[pred, u] ← ␈↓
␈↓ ↓H␈↓4.16)␈↓ α8␈↓↓          ¬␈↓αn|␈↓↓u ∧ [[pred ␈↓αa|␈↓↓u ∧ orlis[pred, ␈↓αd|␈↓↓u]] ∨ twolis[pred, ␈↓αd|␈↓↓u]]␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠141


␈↓ ↓H␈↓␈↓ α8␈↓↓        threat p ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          orlis[[λx: ␈↓
␈↓ ↓H␈↓4.17)␈↓ α8␈↓↓                 1 = [␈↓αif␈↓↓ w ␈↓αthen␈↓↓ ocount x ␈↓αelse␈↓↓ xcount x]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ∧ orlis[[λw: x ε lines w], delete[␈↓αa|␈↓↓p, bs]]], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                lines ␈↓αa|␈↓↓p]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓        delete[x, u] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓4.18)␈↓ α8␈↓↓          ␈↓αelse␈↓↓ ␈↓αif␈↓↓ x = ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ ␈↓αd|␈↓↓u␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . delete[x, ␈↓αd|␈↓↓u]␈↓
␈↓ ↓H␈↓142␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓        Below␈αwe␈αgive␈αsome␈αexamples␈αof␈αthe␈αresults␈αreturned␈αby␈α␈↓↓(lmin,lmax)␈↓␈αand␈α␈↓↓(tmin,tmax).␈↓␈α In
␈↓ ↓H␈↓each␈αcase␈αthe␈αsituation␈αis␈α2␈αmoves␈αinto␈αa␈αgame␈αwhere␈αthe␈α␈↓¬X␈α␈↓player␈αhas␈αmoved␈αfirst␈αand␈αhence␈αit␈α
is
␈↓ ↓H␈↓the ␈↓¬X ␈↓players turn to move.

␈↓ ↓H␈↓¬␈↓ αH    Position numbering:
␈↓ ↓H␈↓¬␈↓ αH            (1  2  3)
␈↓ ↓H␈↓¬␈↓ αH            (4  5  6)
␈↓ ↓H␈↓¬␈↓ αH            (7 10 11)

␈↓ ↓H␈↓¬␈↓ αHExample 1.

␈↓ ↓H␈↓¬␈↓ αH    Board:  (O X _)
␈↓ ↓H␈↓¬␈↓ αH            (_ _ _)
␈↓ ↓H␈↓¬␈↓ αH            (_ _ _)

␈↓ ↓H␈↓¬␈↓ αH␈↓↓lmax[p1, ␈↓¬-1000, ␈↓↓␈↓¬1000␈↓↓] = ␈↓¬(0 5 10 6 4 7 3 11)

␈↓ ↓H␈↓¬␈↓ αH␈↓↓tmax[p1,␈↓¬-1000, ␈↓↓␈↓¬1000␈↓↓] =␈↓¬
␈↓ ↓H␈↓¬␈↓ αH       (0
␈↓ ↓H␈↓¬␈↓ αH        (5 (10 6 (4 7 (3 11 0))))
␈↓ ↓H␈↓¬␈↓ αH        ((3 7 (4 11 (5 10 -2)))
␈↓ ↓H␈↓¬␈↓ αH         (4 5 (11 7 (3 6 (10 0))))
␈↓ ↓H␈↓¬␈↓ αH         (6 5 (11 3 (7 10 (4 0))))
␈↓ ↓H␈↓¬␈↓ αH         (7 5 (11 10 (4 6 (3 0)) (3 6 (4 0)) (6 3 (4 0))))
␈↓ ↓H␈↓¬␈↓ αH         (11 7 (4 5 (3 6 (10 0))))
␈↓ ↓H␈↓¬␈↓ αH         (5 10 (11 4 (7 3 (6 0)))
␈↓ ↓H␈↓¬␈↓ αH               (7 3 (11 6 (4 0)) (4 6 (11 0)) (6 4 (11 0)))
␈↓ ↓H␈↓¬␈↓ αH               (3 7 (4 11 -2))
␈↓ ↓H␈↓¬␈↓ αH               (4 6 (11 3 (7 0)) (7 3 (11 0)) (3 7 (11 0)))
␈↓ ↓H␈↓¬␈↓ αH               (6 4 (7 3 (11 0))) )
␈↓ ↓H␈↓¬␈↓ αH         (10 5 (11 7 (3 4 -2))) ) )


␈↓ ↓H␈↓¬␈↓ αHExample 2.

␈↓ ↓H␈↓¬␈↓ αH    Board:  (O _ X)
␈↓ ↓H␈↓¬␈↓ αH            (_ _ _)
␈↓ ↓H␈↓¬␈↓ αH            (_ _ _)

␈↓ ↓H␈↓¬␈↓ αH␈↓↓lmax[p1, ␈↓¬-1000, ␈↓↓␈↓¬1000␈↓↓] = ␈↓¬(3 11 6 7 5 10)

␈↓ ↓H␈↓¬␈↓ αH␈↓↓tmax[p1,␈↓¬-1000, ␈↓↓␈↓¬1000␈↓↓] =␈↓¬
␈↓ ↓H␈↓¬␈↓ αH       (3
␈↓ ↓H␈↓¬␈↓ αH        (11 (6 7 (5 10 3)))
␈↓ ↓H␈↓¬␈↓ αH        ((2 7 (4 11 (5 0)))
␈↓ ↓H␈↓¬␈↓ αH         (4 5 (11 6 (2 0) (10 0) (7 0)))
␈↓ ↓H␈↓¬␈↓ αH         (10 11 (5 7 (2 3)))
␈↓ ↓H␈↓¬␈↓ αH         (5 7 (4 6 (11 0) (10 0) (2 0)))
␈↓ ↓H␈↓¬␈↓ αH         (6 11 (5 7 (4 3)))
␈↓ ↓H␈↓¬␈↓ αH         (7 5 (11 6 (10 3)))
␈↓ ↓H␈↓¬␈↓ αH         (11 6 (7 5 (10 3))) ) )
␈↓ ↓H␈↓␈↓ εH␈↓ ≠143


␈↓ ↓H␈↓α␈↓ ¬uChapter VII

␈↓ ↓H␈↓α␈↓ ∧CTRANSFORMING LISP PROGRAMS


␈↓ ↓H␈↓        The␈α∞techniques␈α
developed␈α∞in␈α
Chapter␈α∞III␈α∞were␈α
limited␈α∞to␈α
stating␈α∞and␈α∞proving␈α
extensional
␈↓ ↓H␈↓properties␈α⊂of␈α⊂functional␈α⊂LISP␈α⊂programs.␈α⊂ We␈α⊂have␈α⊂seen␈α⊂in␈α⊂Chapter␈α⊂IV␈α⊂that␈α⊂in␈α⊂general␈α⊂LISP
␈↓ ↓H␈↓programs␈α
may␈α
not␈α
be␈α
functional.␈α
Also␈α
many␈α
interesting␈α
properties␈α
of␈α
programs␈α
such␈α
as␈α∞space␈α
or
␈↓ ↓H␈↓time␈αrequirements,␈α
depth␈αof␈αrecursion,␈α
number␈αof␈α
calls␈αto␈αa␈α
particular␈αfunction␈α
during␈αexecution,
␈↓ ↓H␈↓etc.,␈αare␈αnot␈α
extensional.␈α  In␈αthis␈α
chapter␈αwe␈αshow␈α
how␈αthe␈αfirst␈α
order␈αtechniques␈αof␈α
Chapter␈αIII
␈↓ ↓H␈↓can␈α
be␈α
applied␈α∞to␈α
a␈α
larger␈α∞class␈α
of␈α
problems.␈α∞ In␈α
particular␈α
we␈α∞discuss␈α
how␈α
to␈α∞transform␈α
certain
␈↓ ↓H␈↓sequential␈α∪LISP␈α∪programs␈α∪into␈α∪functional␈α∪programs␈α∪that␈α∪compute␈α∪the␈α∪same␈α∪function.␈α∩ Thus
␈↓ ↓H␈↓extensional␈αproperties␈α
can␈αbe␈αdiscussed␈α
in␈αterms␈α
of␈αthe␈αequivalent␈α
functional␈αprogram.␈α
  We␈αalso
␈↓ ↓H␈↓show␈α⊃how␈α⊃some␈α⊃intensional␈α⊃properties␈α⊃can␈α⊃be␈α⊃handled␈α⊃by␈α⊃forming␈α⊃a␈α⊃"derived"␈α⊃program␈α⊃that
␈↓ ↓H␈↓computed␈αthe␈αdesired␈αproperty␈αof␈αthe␈αoriginal␈αprogram.␈α Finally␈αwe␈αdiscuss␈αbriefly␈αhow␈αto␈αconnect
␈↓ ↓H␈↓the␈αformal␈α
properties␈αof␈αtransforming␈α
programs␈αwith␈α
those␈αof␈αthe␈α
transformed␈αprograms␈αusing␈α
the
␈↓ ↓H␈↓␈↓↓meta␈↓␈α
theory␈αof␈α
LISP.␈α This␈α
connection␈αwill␈α
allow␈αus␈α
for␈αexample␈α
to␈αdefine␈α
intensional␈αproperties
␈↓ ↓H␈↓formally and show that the derived programs actually do compute the property correctly.



␈↓ ↓H␈↓1.  ␈↓αPurifying LISP programs.␈↓


␈↓ ↓H␈↓        As␈αwe␈α
noted␈αin␈α
Chapter␈αIV␈α
a␈αsequential␈α
term␈αoccuring␈α
as␈αa␈α
term␈αin␈α
a␈αLISP␈α
program␈αcan␈α
be
␈↓ ↓H␈↓replaced␈αby␈αan␈αextensionally␈αequivalent␈αfunctional␈αterm␈αunder␈αcertain␈αconditions.␈α A␈αsufficient␈αset
␈↓ ↓H␈↓of conditions are the following:

␈↓ ↓H␈↓␈↓ αλ1.␈αAll␈αvariables␈αlocal␈α
to␈αthe␈αprogram␈αare␈α
initialized␈αbefore␈αbeing␈αused.␈α
 Of␈αcourse,␈αit␈αcould␈α
be
␈↓ ↓H␈↓␈↓ αHtaken␈α∞as␈α
a␈α∞convention␈α
that␈α∞these␈α
variables␈α∞are␈α
initialized␈α∞to␈α
␈↓¬NIL␈↓␈α∞when␈α
the␈α∞program␈α
is
␈↓ ↓H␈↓␈↓ αHentered.␈α∪ Then␈α∩it␈α∪wouldn't␈α∩affect␈α∪the␈α∪correctness␈α∩of␈α∪the␈α∩translation␈α∪to␈α∪a␈α∩functional
␈↓ ↓H␈↓␈↓ αHprogram if some of them weren't further initialized.

␈↓ ↓H␈↓␈↓ αλ2. All assignments are to variables local to the program.

␈↓ ↓H␈↓␈↓ αλ3. Each label referred to in a ␈↓αgo to␈↓ statement occurs exactly once in the program.

␈↓ ↓H␈↓        One␈α
method␈α
of␈αfinding␈α
a␈α
functional␈α
term␈αequivalent␈α
to␈α
a␈αsequential␈α
term␈α
is␈α
the␈αfollowing.
␈↓ ↓H␈↓Suppose␈α∀the␈α∃␈↓αprogram␈↓␈α∀has␈α∃local␈α∀variables␈α∀␈↓↓x1,...,xn.␈↓␈α∃ For␈α∀each␈α∃conditional␈α∀branch␈α∃(i.e.␈α∀a
␈↓ ↓H␈↓conditional␈α
statement␈αcontaining␈α
a␈α␈↓αgo to␈↓␈α
or␈αa␈α
␈↓αreturn␈↓)␈αwe␈α
define␈αa␈α
new␈αfunction.␈α
 If␈αthe␈α
conditional
␈↓ ↓H␈↓has the form

␈↓ ↓H␈↓␈↓ ∧1␈↓↓␈↓αif␈↓↓ p1 ␈↓αthen␈↓↓ s1 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ... ␈↓αelse␈↓↓ ␈↓αif␈↓↓ pn ␈↓αthen␈↓↓ sn␈↓ 

␈↓ ↓H␈↓then  the body of the function definition has the form

␈↓ ↓H␈↓␈↓ ∧¬␈↓↓␈↓αif␈↓↓ p1 ␈↓αthen␈↓↓ t1 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ... ␈↓αelse␈↓↓ ␈↓αif␈↓↓ pn ␈↓αthen␈↓↓ tn ␈↓αelse␈↓↓ t0␈↓ 

␈↓ ↓H␈↓where␈α␈↓↓s0␈↓␈α
is␈αthe␈αsequence␈α
of␈αstatements␈α
following␈αthe␈αconditional␈α
and␈α␈↓↓ti␈↓␈αis␈α
the␈αresult␈α
of␈αexecuting
␈↓ ↓H␈↓the␈αsequence␈αof␈α
statements␈α␈↓↓si;s0␈↓␈α(or␈αjust␈α
␈↓↓s0␈↓␈α for␈α␈↓↓t0␈↓␈α)␈α
taking␈αthe␈αcurrent␈αvalue␈α
of␈α␈↓↓xi␈↓␈αto␈αbe␈α
␈↓↓xi.␈↓␈α We
␈↓ ↓H␈↓144␈↓ ¬qChapter  VII␈↓ H


␈↓ ↓H␈↓obtain␈α
the␈α
term␈α
corresponding␈α∞to␈α
the␈α
result␈α
of␈α
executing␈α∞a␈α
sequence␈α
of␈α
statements␈α
as␈α∞follows.␈α
 If
␈↓ ↓H␈↓the␈αstatement␈αis␈αan␈αassigment␈αthen␈αthe␈αcurrent␈αvalue␈αof␈αthe␈αvariable␈αbecomes␈αthe␈αcurrent␈αvalue␈αof
␈↓ ↓H␈↓the␈α∂term␈α∂on␈α∂the␈α∞right␈α∂hand␈α∂side.␈α∂(The␈α∂current␈α∞value␈α∂of␈α∂the␈α∂term␈α∞is␈α∂just␈α∂the␈α∂term␈α∂obtained␈α∞by
␈↓ ↓H␈↓replacing␈α
all␈α
free␈α
occurrences␈α
of␈α
local␈α
variables␈α
by␈α
their␈α
current␈α
values.␈α
If␈α
some␈α
local␈α
variable␈α
is
␈↓ ↓H␈↓needed␈α
that␈α
has␈α
no␈α
current␈α
value␈α
then␈α
the␈α
current␈α
value␈α
of␈α
the␈α
term␈α
is␈α
undefined.)␈α
If␈α
the␈α
statement
␈↓ ↓H␈↓is␈αa␈αconditional␈αwithout␈αany␈α␈↓αgo to␈↓s␈αor␈α␈↓αreturn␈↓s␈αthen␈αit␈αgets␈αconverted␈αto␈αa␈αsequence␈αof␈αassignments
␈↓ ↓H␈↓where␈α∞the␈α
conditional␈α∞has␈α
been␈α∞distributed␈α∞through␈α
the␈α∞assignment.␈α
 If␈α∞the␈α
statement␈α∞is␈α∞a␈α
␈↓αgo to␈↓
␈↓ ↓H␈↓then␈α∂the␈α∂next␈α∂statement␈α∂to␈α∂execute␈α∂becomes␈α∂the␈α∂one␈α∂with␈α∂the␈α∂corresponding␈α∂label.␈α∂ If␈α∂the␈α∂next
␈↓ ↓H␈↓statement␈α⊂is␈α⊂a␈α⊂␈↓αreturn␈↓␈α⊂then␈α⊂the␈α⊂result␈α⊂is␈α⊂the␈α⊂current␈α⊂value␈α⊂of␈α⊂the␈α⊂term␈α⊂to␈α⊂be␈α⊂returned.␈α⊂ If␈α∂the
␈↓ ↓H␈↓statement␈αis␈α
one␈αof␈α
the␈αconditionals␈αfor␈α
which␈αwe␈α
have␈αcreated␈αa␈α
new␈αfunction␈α
then␈αthe␈α
result␈αis
␈↓ ↓H␈↓that␈αfunction␈αapplied␈αto␈αthe␈αcurrent␈αvalues␈αof␈αthe␈αlocal␈αvariables.␈α If␈αwe␈αare␈αat␈αthe␈αend␈αof␈αthe␈αlist
␈↓ ↓H␈↓of␈αstatements␈αthen␈αthe␈αresult␈αis␈αundefined.␈α Finally␈αthe␈α␈↓αprogram␈↓␈αterm␈αis␈αreplaced␈αby␈αthe␈αresult␈αof
␈↓ ↓H␈↓executing␈α
the␈α∞sequence␈α
of␈α∞statements␈α
starting␈α∞with␈α
the␈α∞first,␈α
with␈α∞the␈α
current␈α∞values␈α
of␈α∞the␈α
local
␈↓ ↓H␈↓variables undefined.

␈↓ ↓H␈↓        [In␈α⊃order␈α⊃to␈α∩insure␈α⊃that␈α⊃this␈α⊃process␈α∩will␈α⊃terminate␈α⊃we␈α⊃must␈α∩keep␈α⊃track␈α⊃of␈α∩the␈α⊃labeled
␈↓ ↓H␈↓statements␈αvisited␈αin␈αan␈αexecution␈αsequence␈αso␈αthat␈αif␈αa␈αlabel␈αgets␈αvisited␈αtwice␈αunconditionally␈αwe
␈↓ ↓H␈↓know that this will be an infinite loop and the result of executing this sequence will be defined.]

␈↓ ↓H␈↓        As␈αan␈αexample,␈αconsider␈αthe␈αfollowing␈αexpression␈αthat␈αgives␈αthe␈αresult␈αof␈αappending␈αthe␈αlist
␈↓ ↓H␈↓␈↓↓a␈↓ with the reversal of ␈↓↓a*b:␈↓

␈↓ ↓H␈↓␈↓ α	␈↓↓a*␈↓αprogram␈↓↓[[u,v] u←a*b; v←␈↓¬NIL␈↓↓; loop: ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ v; v←␈↓αa|␈↓↓u . v; u←␈↓αd|␈↓↓u;  ␈↓αgo to␈↓↓ loop].␈↓ 

␈↓ ↓H␈↓This expression can be replaced by

␈↓ ↓H␈↓␈↓ ∧C␈↓↓a * loop[a*b, ␈↓¬NIL␈↓↓]                            ␈↓ 

␈↓ ↓H␈↓and the recursive definition

␈↓ ↓H␈↓␈↓ ∧∩␈↓↓loop[u,v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ loop[d u,␈↓αa|␈↓↓u . n].␈↓ 

␈↓ ↓H␈↓        A␈α
second␈α
method␈α
for␈α
transforming␈α∞sequential␈α
terms␈α
into␈α
functional␈α
terms␈α
is␈α∞the␈α
following.
␈↓ ↓H␈↓There␈α∂will␈α∂be␈α∂a␈α∂new␈α∂function␈α∂associated␈α∂with␈α∞each␈α∂label.␈α∂ We␈α∂process␈α∂the␈α∂list␈α∂of␈α∂statements␈α∞in
␈↓ ↓H␈↓reverse␈αorder␈α
starting␈αwith␈α
the␈αlast␈α
one␈αand␈α
carry␈αalong␈α
a␈αcurrent␈α
result.␈α We␈α
begin␈αwith␈αthe␈α
result
␈↓ ↓H␈↓undefined.␈α If␈αthe␈αstatement␈αjust␈αprocessed␈αis␈α
labeled␈αthen␈αwe␈αmake␈αthe␈αdefining␈αexpression␈αof␈α
the
␈↓ ↓H␈↓function␈α
corresponding␈αto␈α
that␈αlabel␈α
equal␈αto␈α
the␈αcurrent␈α
result,␈αand␈α
the␈αcurrent␈α
result␈αbecomes␈α
the
␈↓ ↓H␈↓corresponding␈αfunction␈αapplied␈αto␈αthe␈αlist␈αof␈αlocal␈αvariables.␈α If␈αwe␈αare␈αat␈αthe␈αtop␈αof␈αthe␈αlist␈αthen
␈↓ ↓H␈↓we␈α∂are␈α∞done␈α∂and␈α∞the␈α∂current␈α∞result␈α∂is␈α∞the␈α∂desired␈α∞functional␈α∂term.␈α∞ If␈α∂the␈α∞next␈α∂statement␈α∂is␈α∞an
␈↓ ↓H␈↓assignment␈αwe␈αreplace␈αall␈αfree␈αoccurrences␈αof␈αthe␈αassigned␈αvariable␈αin␈αthe␈αresult␈αby␈αthe␈αexpression
␈↓ ↓H␈↓part␈αof␈αthe␈αassignment.␈α If␈αthe␈αstatement␈αis␈αa␈α␈↓αreturn␈↓␈αwe␈αreplace␈αthe␈αresult␈αby␈αthe␈αexpression␈αto␈αbe
␈↓ ↓H␈↓returned.  If the statement is a conditional of the form

␈↓ ↓H␈↓␈↓ ∧1␈↓↓␈↓αif␈↓↓ p1 ␈↓αthen␈↓↓ s1 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ... ␈↓αelse␈↓↓ ␈↓αif␈↓↓ pn ␈↓αthen␈↓↓ sn␈↓ 

␈↓ ↓H␈↓then the result is replaced by a conditional of the form

␈↓ ↓H␈↓␈↓ ∧¬␈↓↓␈↓αif␈↓↓ p1 ␈↓αthen␈↓↓ t1 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ... ␈↓αelse␈↓↓ ␈↓αif␈↓↓ pn ␈↓αthen␈↓↓ tn ␈↓αelse␈↓↓ t0␈↓ 

␈↓ ↓H␈↓where␈α
␈↓↓ti␈↓␈α
is␈α
the␈α
result␈αof␈α
processing␈α
the␈α
statements␈α
␈↓↓si␈↓␈α
(bottom␈αup)␈α
starting␈α
with␈α
the␈α
current␈αresult␈α
(␈↓↓t0␈↓
␈↓ ↓H␈↓␈↓ ¬qChapter  VII␈↓ ≠145


␈↓ ↓H␈↓is␈αjust␈αthe␈α
current␈αresult).␈α If␈α
the␈αstatement␈αis␈α
a␈α␈↓αgo to␈↓␈αthen␈α
the␈αresult␈αis␈α
replaced␈αby␈αthe␈α
application
␈↓ ↓H␈↓of the function corresponding to the label to the list of local variables.

␈↓ ↓H␈↓        The␈α∂first␈α∂method␈α∂is␈α∞based␈α∂on␈α∂the␈α∂technique␈α∞for␈α∂transforming␈α∂Flow␈α∂Charts␈α∂or␈α∞ALGOLic
␈↓ ↓H␈↓programs␈α
into␈αrecursive␈α
functional␈αprograms␈α
given␈αin␈α
McCarthy[1962b]␈αThe␈α
second␈αtechnique␈α
for
␈↓ ↓H␈↓converting␈α
sequential␈α
programs␈α∞into␈α
functional␈α
programs␈α∞was␈α
suggested␈α
by␈α∞Richard␈α
Weyhrauch.
␈↓ ↓H␈↓It␈αis␈αbased␈αon␈αthe␈αgeneral␈αmethod␈αused␈αby␈αmathematical␈αlogicians␈αto␈αcode␈αinformal␈αdescriptions␈αof
␈↓ ↓H␈↓sequential␈αcomputations␈αinto␈αfunctional␈αform␈α(typically␈αinto␈αλ-calculus).␈α Both␈αprocedures␈αgive␈αthe
␈↓ ↓H␈↓same␈α∞result␈α
on␈α∞the␈α∞simple␈α
example␈α∞given␈α∞above␈α
as␈α∞well␈α
as␈α∞for␈α∞the␈α
program␈α∞for␈α∞␈↓↓append␈↓␈α
(IV.1.4)
␈↓ ↓H␈↓and␈αa␈α
cleaned␈αup␈α
version␈α(adding␈αan␈α
additional␈αlocal␈α
variable␈αto␈αeliminate␈α
assignments␈αto␈α
a␈αnon
␈↓ ↓H␈↓local variable) of the ␈↓↓reverse␈↓ program (IV.1.1).

␈↓ ↓H␈↓        The␈α⊂above␈α⊃conditions␈α⊂permit␈α⊃a␈α⊂purely␈α⊂local␈α⊃rewrite,␈α⊂ and␈α⊃they␈α⊂are␈α⊃indeed␈α⊂unnecessarily
␈↓ ↓H␈↓restrictive.␈α
 The␈αquestion␈α
is␈αthen␈α
how␈α
to␈αrelax␈α
the␈αrestricitions␈α
in␈α
some␈αuseful␈α
way.␈α In␈α
any␈αcase␈α
we
␈↓ ↓H␈↓must␈α
maintain␈α
the␈αproperty␈α
that␈α
replacing␈α
an␈αoccurence␈α
of␈α
the␈α
sequential␈αterm␈α
in␈α
an␈αexpression␈α
by
␈↓ ↓H␈↓its␈αvalue␈α will␈α
not␈αchange␈αthe␈α
value␈αof␈αthe␈αexpression.␈α
 It␈αseems␈αevident␈α
that␈αif␈αan␈αouter␈α
sequential
␈↓ ↓H␈↓program␈α∂containing␈α∂an␈α∞inner␈α∂one␈α∂satisfies␈α∞the␈α∂initialization␈α∂and␈α∞local␈α∂assignment␈α∂(i.e.␈α∂ no␈α∞side-
␈↓ ↓H␈↓effect)␈α⊃conditions,␈α⊃then␈α⊃the␈α⊃program␈α⊃can␈α∩be␈α⊃converted␈α⊃to␈α⊃functional␈α⊃form␈α⊃even␈α⊃if␈α∩it␈α⊃contains
␈↓ ↓H␈↓sequential␈α∞subprograms␈α∞that␈α∞violate␈α∞these␈α∞conditions␈α∞provided␈α∞that␈α∞all␈α∞assignments␈α∞in␈α∞the␈α∞inner
␈↓ ↓H␈↓program␈α
are␈α
at␈α
least␈α
local␈α
to␈α
the␈α
outer␈α
program,␈α
 and␈α
that␈α
these␈α
inner␈α
programs␈α
can␈α
be␈αreplaced␈α
by
␈↓ ↓H␈↓a␈αfunctional␈αterm␈αin␈αsuch␈αa␈αway␈αthat␈αthe␈αvalue␈αof␈αthe␈αouter␈αsequential␈αterm␈αis␈αnot␈αchanged.␈α The
␈↓ ↓H␈↓rules for doing this have not been formulated as yet.

␈↓ ↓H␈↓        The␈αreason␈αfor␈αbeing␈αparticular␈αabout␈αthe␈αkind␈αof␈αassignments␈αallowed␈αis␈αthat␈αif␈αa␈αprogram
␈↓ ↓H␈↓should␈α
make␈α
assignments␈α
to␈α
external␈α∞variables,␈α
ambiguities␈α
may␈α
arise.␈α
 Suppose␈α
we␈α∞evaluate␈α
the
␈↓ ↓H␈↓expression

␈↓ ↓H␈↓␈↓ ∧	␈↓↓<x, ␈↓αprogram␈↓↓[[] x ← ␈↓αd|␈↓↓x; ␈↓αreturn␈↓↓ x], x>.             ␈↓ 

␈↓ ↓H␈↓when␈α
␈↓↓x␈↓␈α
has␈αthe␈α
value␈α
␈↓¬(A B).␈α␈↓␈α
If␈α
the␈αarguments␈α
of␈α
the␈α
list␈αare␈α
"executed"␈α
in␈αleft␈α
to␈α
right␈αorder,␈α
the
␈↓ ↓H␈↓expression␈αwill␈αhave␈αvalue␈α␈↓¬((A B) (B) (B)),␈α␈↓␈αwhereas␈αif␈αthe␈αexecution␈αis␈αfrom␈αright␈αto␈αleft,␈αthe
␈↓ ↓H␈↓value␈α⊂will␈α⊂be␈α⊂␈↓¬((B) (B) (A B)).␈α⊂␈↓␈α⊃ However,␈α⊂the␈α⊂mathematical␈α⊂semantics␈α⊂of␈α⊃expressions␈α⊂don't
␈↓ ↓H␈↓prescribe␈α
any␈α
order␈αof␈α
evaluation,␈α
and␈α
experience␈αwith␈α
ALGOL 60,␈α
which␈αprescribed␈α
left-to-right
␈↓ ↓H␈↓evaluation␈α
in␈α
order␈αto␈α
give␈α
a␈αdefinite␈α
meaning␈α
to␈α
function␈αprocedures␈α
that␈α
alter␈αexternal␈α
variables,
␈↓ ↓H␈↓has shown that the quality of compiled code is greatly reduced by prescribing a fixed order.

␈↓ ↓H␈↓        This␈α
ambiguity␈αcan␈α
be␈α
resolved␈αby␈α
rewriting␈α
expressions␈αcontaining␈α
programs␈α
that␈αchange
␈↓ ↓H␈↓variables␈α
in␈α
the␈α
expression␈α
using␈α
lambda␈αexpressions␈α
to␈α
force␈α
a␈α
desired␈α
order␈α
of␈αevaluation.␈α
 Thus
␈↓ ↓H␈↓the above expression could be written

␈↓ ↓H␈↓␈↓ ∧∧␈↓↓[λy:[λz:<z,y,y>][␈↓αprogram␈↓↓[[] x ← ␈↓αd|␈↓↓x; ␈↓αreturn␈↓↓ x]]][x]␈↓ 

␈↓ ↓H␈↓which␈α∞would␈α∂evaluate␈α∞to␈α∞␈↓¬((B) (A B) (A B))␈↓␈α∂in␈α∞the␈α∂above␈α∞case.␈α∞ Seven␈α∂other␈α∞results␈α∂could␈α∞be
␈↓ ↓H␈↓obtained␈α
by␈α
putting␈α
different␈α
combinations␈α
of␈α∞␈↓↓z␈↓␈α
and␈α
␈↓↓y␈↓␈α
between␈α
the␈α
pointy␈α
brackets.␈α∞ Once␈α
these
␈↓ ↓H␈↓ambiguities␈α
have␈α
been␈α
resolved,␈α
it␈α
would␈α
seem␈α
that␈α
we␈α
could␈α
translate␈α
an␈α
outer␈α
sequential␈α
program
␈↓ ↓H␈↓containing␈αinner␈αsequential␈αprograms␈αinto␈αa␈αfunction␈αprogram␈αin␈αa␈αunique␈αway␈αeven␈αallowing␈αthe
␈↓ ↓H␈↓inner␈αprograms␈αto␈αmake␈αassignments␈αto␈αvariables␈αin␈αthe␈αouter␈αprograms.␈α The␈αrules␈αfor␈αdoing␈α
this
␈↓ ↓H␈↓haven't been worked out, however.
␈↓ ↓H␈↓146␈↓ ¬qChapter  VII␈↓ H


␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓        1.␈αWrite␈αa␈α
LISP␈αpredicate␈αthat␈α
will␈αdetermine␈αwhether␈α
a␈α␈↓¬PROG␈α␈↓␈α
in␈αinternal␈αform␈αsatisfies␈α
the
␈↓ ↓H␈↓above conditions for replacement by a functional term.

␈↓ ↓H␈↓        2.␈α∞Given␈α∞that␈α
a␈α∞␈↓¬PROG␈α∞␈↓satisfies␈α
the␈α∞conditions,␈α∞write␈α
a␈α∞LISP␈α∞functions␈α
to␈α∞convert␈α∞the␈α
␈↓¬PROG
␈↓ ↓H␈↓¬␈↓expression and give the a list of the required auxiliary function definitions.



␈↓ ↓H␈↓2.  ␈↓αDerived programs.␈↓


␈↓ ↓H␈↓        So␈α∪far␈α∀we␈α∪have␈α∪discussed␈α∀mainly␈α∪extensional␈α∪properties␈α∀of␈α∪programs.␈α∪ Recall␈α∀that␈α∪an
␈↓ ↓H␈↓extensional␈α
property␈α
is␈α
a␈α
property␈α
that␈α
depends␈α
only␈α
on␈α
the␈α
function␈α
computed␈α
by␈α∞the␈α
program.
␈↓ ↓H␈↓In␈αmany␈αcases␈αwe␈αwould␈αlike␈αto␈α
be␈αable␈αto␈αexpress␈αand␈αprove␈αthings␈αabout␈α
intensional␈αproperties
␈↓ ↓H␈↓such␈α_as␈α_space␈α_or␈α_time␈α_requirements,␈α_depth␈α_of␈α_recursion,␈α_etc..␈α_ Thus␈α_the␈α→statement␈α_that
␈↓ ↓H␈↓␈↓↓∀u.flatten x=fringe x␈↓␈α⊂ expresses␈α⊃the␈α⊂extensional␈α⊃property␈α⊂that␈α⊂␈↓↓flatten␈↓␈α⊃(I.8.8)␈α⊂and␈α⊃␈↓↓fringe␈↓␈α⊂(I.8.10)
␈↓ ↓H␈↓compute␈α
the␈αsame␈α
function.␈α However␈α
the␈α
statement␈αmade␈α
in␈αChapter␈α
I␈αthat␈α
␈↓↓flatten␈↓␈α
requires␈αless
␈↓ ↓H␈↓computation␈α∞than␈α∞␈↓↓fringe␈↓␈α∞refers␈α
to␈α∞an␈α∞intensional␈α∞property.␈α
 We␈α∞could␈α∞make␈α∞this␈α∞statement␈α
more
␈↓ ↓H␈↓precise␈α
by␈αsaying␈α
that␈α
more␈α␈↓↓cons␈↓es␈α
are␈α
executed␈αin␈α
computing␈α
␈↓↓fringe x␈↓␈αthan␈α
in␈αcomputing␈α
␈↓↓flatten x␈↓
␈↓ ↓H␈↓but the methods of Chapter III do not allow us to prove it.

␈↓ ↓H␈↓        Consider␈α∀the␈α∀problem␈α∀of␈α∀computing␈α∀the␈α∀number␈α∀of␈α∀␈↓↓cons␈↓es␈α∀executed␈α∀when␈α∀computing
␈↓ ↓H␈↓␈↓↓flatten x.␈↓␈α
 Since␈α
␈↓↓flatten␈↓␈α
simply␈α
calls␈α
the␈α
auxiliary␈αfunction␈α
␈↓↓flat␈↓␈α
we␈α
will␈α
do␈α
the␈α
problem␈α
for␈α␈↓↓flat.␈↓
␈↓ ↓H␈↓There␈α⊂are␈α⊂two␈α⊃cases.␈α⊂ If␈α⊂␈↓↓x␈↓␈α⊃is␈α⊂an␈α⊂atom␈α⊃then␈α⊂␈↓↓flat[x,u]=x . u␈↓␈α⊂so␈α⊃1␈α⊂␈↓↓cons␈↓␈α⊂is␈α⊃executed.␈α⊂ Otherwise
␈↓ ↓H␈↓␈↓↓flat[x,u]=flat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,u]␈↓␈αso␈αthe␈αnumber␈αof␈α␈↓↓cons␈↓es␈αexecuted␈αmust␈αbe␈αthe␈αsum␈αof␈αthose␈α
executed
␈↓ ↓H␈↓in␈α∂computing␈α∂␈↓↓flat[␈↓αd|␈↓↓x,u]␈↓␈α∂and␈α⊂those␈α∂executed␈α∂␈↓↓flat[␈↓αa|␈↓↓x,v]␈↓␈α∂where␈α∂␈↓↓v␈↓␈α⊂is␈α∂␈↓↓flat[␈↓αd|␈↓↓x,u]␈↓.␈α∂ The␈α∂idea␈α⊂is␈α∂to
␈↓ ↓H␈↓count␈αthe␈α␈↓↓cons␈↓es␈αexecuted␈αin␈αcomputing␈αthe␈αvalues␈αof␈αthe␈αarguments␈αand␈αadd␈αto␈αthat␈αthe␈αnumber
␈↓ ↓H␈↓of␈α␈↓↓cons␈↓␈αexecuted␈αin␈αcomputing␈αthe␈αprogram␈αwith␈αthose␈αvalues␈αas␈αarguments.␈α Thus␈αwe␈αcan␈αdefine
␈↓ ↓H␈↓the function ␈↓↓cflat␈↓ that computes the number of ␈↓↓cons␈↓es executed in computing ␈↓↓flat␈↓ by

␈↓ ↓H␈↓↓␈↓ βxcflat[x, u] ←
␈↓ ↓H␈↓↓2.1)␈↓ ∧8␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬1␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ cflat[␈↓αa|␈↓↓x, flat[␈↓αd|␈↓↓x, u]] + cflat[␈↓αd|␈↓↓x, u]

␈↓ ↓H␈↓for example

␈↓ ↓H␈↓␈↓ ∧J␈↓↓cflat[␈↓¬(A . (B . (C . D)))␈↓↓, ␈↓¬NIL␈↓↓] = ␈↓¬4␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧J␈↓↓cflat[␈↓¬(((A . B) . C) . D)␈↓↓, ␈↓¬NIL␈↓↓] = ␈↓¬4␈↓↓␈↓

␈↓ ↓H␈↓We␈α⊃can␈α⊂express␈α⊃the␈α⊂function␈α⊃computed␈α⊃by␈α⊂␈↓↓cflat␈↓␈α⊃in␈α⊂terms␈α⊃of␈α⊃the␈α⊂size␈α⊃of␈α⊂the␈α⊃input␈α⊃using␈α⊂␈↓↓size␈↓
␈↓ ↓H␈↓(III.11.6) by

␈↓ ↓H␈↓2.2)␈↓ ¬&␈↓↓∀x u: cflat[x, u] = size x␈↓. 

␈↓ ↓H␈↓To␈α∃prove␈α⊗this␈α∃statement␈α⊗we␈α∃us␈α⊗S-expresion␈α∃induction␈α∃on␈α⊗␈↓↓x.␈↓␈α∃ In␈α⊗the␈α∃case␈α⊗␈↓↓x␈↓␈α∃is␈α⊗an␈α∃atom
␈↓ ↓H␈↓␈↓↓∀u.cflat[x,u]=␈↓¬1␈↓↓=sixe x␈↓␈α'by␈α&simplifying␈α'the␈α'definitions.␈α& Otherwise␈α'we␈α'may␈α&assume
␈↓ ↓H␈↓␈↓↓∀u.cflat[␈↓αa|␈↓↓x,u]=size ␈↓αa|␈↓↓x␈↓ and ␈↓↓∀u.cflat[␈↓αd|␈↓↓x,u]=size ␈↓αd|␈↓↓x␈↓ .  Now
␈↓ ↓H␈↓␈↓ ¬qChapter  VII␈↓ ≠147



␈↓ ↓H␈↓␈↓ αx␈↓↓cflat[x,u]␈↓ βx=cflat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,u]]+cflat[␈↓αd|␈↓↓x,u]␈↓␈↓ λ8definition of ␈↓↓flat␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓=size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x␈↓␈↓ λ8induction hypothesis
␈↓ ↓H␈↓␈↓ βx␈↓↓=size x␈↓␈↓ λ8definition of ␈↓↓size␈↓

␈↓ ↓H␈↓Notice that in order to make the second step we need to know that ␈↓↓flat␈↓ is total.

␈↓ ↓H␈↓        Now␈α
we␈α
will␈α
do␈α
the␈α
same␈α
construction␈α
for␈α
␈↓↓fringe.␈↓␈α
 When␈α
␈↓↓x␈↓␈α
is␈α
an␈α
atom␈α
then␈α
␈↓↓fringe x=x . ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓so␈α∩the␈α∩number␈α∩of␈α∩␈↓↓cons␈↓es␈α∩is␈α∩␈↓¬1.␈α∩␈↓␈α∩Otherwise␈α∩␈↓↓fringe x=fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x␈↓.␈α∩ So␈α∩we␈α∪count␈α∩the
␈↓ ↓H␈↓number␈α∞of␈α∞␈↓↓cons␈↓es␈α∞executed␈α
in␈α∞computing␈α∞␈↓↓fringe ␈↓αa|␈↓↓x␈↓␈α∞and␈α∞ ␈↓↓fringe ␈↓αd|␈↓↓x␈↓␈α
and␈α∞add␈α∞to␈α∞this␈α∞the␈α
number
␈↓ ↓H␈↓executed␈αin␈αcomputing␈α
␈↓↓u * v␈↓␈αwhere␈α␈↓↓u␈↓␈αis␈α
␈↓↓fringe ␈↓αa|␈↓↓x␈↓␈αand␈α␈↓↓v␈↓␈αis␈α
␈↓↓fringe ␈↓αd|␈↓↓x␈↓.␈α A␈αsimilar␈αanalysis␈α
can␈αbe
␈↓ ↓H␈↓made for ␈↓↓append.␈↓  Thus we can define the functions ␈↓↓cfringe␈↓ and ␈↓↓cappend␈↓ by

␈↓ ↓H␈↓↓␈↓ βxcfringe x ←
␈↓ ↓H␈↓↓2.3)␈↓ ∧8␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬1␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ cappend[fringe ␈↓αa|␈↓↓x, fringe ␈↓αd|␈↓↓x] + cfringe ␈↓αa|␈↓↓x + cfringe ␈↓αd|␈↓↓x
␈↓ ↓H␈↓↓␈↓and␈↓↓
␈↓ ↓H␈↓↓␈↓ βxcappend[u,v] ←
␈↓ ↓H␈↓↓2.4)␈↓ ∧8␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓¬1 ␈↓↓+ cappend[␈↓αd|␈↓↓u, v]

␈↓ ↓H␈↓For example

␈↓ ↓H␈↓␈↓ ∧←␈↓↓cfringe[␈↓¬(A . (B . (C . D)))␈↓↓] = ␈↓¬7␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧W␈↓↓cfringe[␈↓¬(((A . B) . C) . D)␈↓↓] = ␈↓¬10␈↓↓␈↓

␈↓ ↓H␈↓We can express ␈↓↓cfringe␈↓ in terms of the size of the input  by

␈↓ ↓H␈↓␈↓ ¬
␈↓↓∀x: cfringe x ≥ ␈↓¬2 ␈↓↓␈↓π*␈↓↓ size x - ␈↓¬1␈↓↓␈↓
␈↓ ↓H␈↓2.5)
␈↓ ↓H␈↓␈↓ ∧Q␈↓↓∀x: [size x + ␈↓¬1␈↓↓]␈↓π*␈↓↓[size x] ≥ ␈↓¬2 ␈↓↓␈↓π*␈↓↓ cfringe x␈↓

␈↓ ↓H␈↓This can be proved by S-expression using the fact that

␈↓ ↓H␈↓2.6)␈↓ ∧r␈↓↓∀x u.cappend[fringe x,u]=size x␈↓. 

␈↓ ↓H␈↓In␈α⊂the␈α⊂case␈α∂␈↓↓x␈↓␈α⊂is␈α⊂atomic␈α∂the␈α⊂inequalities␈α⊂reduce␈α∂to␈α⊂1≥1␈α⊂and␈α∂2≥2␈α⊂using␈α⊂the␈α⊂function␈α∂definitions.
␈↓ ↓H␈↓Otherwise we have

␈↓ ↓H␈↓␈↓ αx␈↓↓cfringe x␈↓ βx= cappend[fringe ␈↓αa|␈↓↓x,fringe ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓  + cfringe ␈↓αa|␈↓↓x + cfringe ␈↓αd|␈↓↓x ␈↓␈↓ λ8by definition of ␈↓↓cfringe␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓≥ cappend[fringe ␈↓αa|␈↓↓x,fringe ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓  + ␈↓¬2␈↓↓␈↓π*␈↓↓[size ␈↓αa|␈↓↓x-␈↓¬1␈↓↓] + ␈↓¬2␈↓↓␈↓π*␈↓↓[size ␈↓αd|␈↓↓x-␈↓¬1␈↓↓]␈↓␈↓ λ8by induction
␈↓ ↓H␈↓␈↓ βx␈↓↓≥ size ␈↓αa|␈↓↓x + ␈↓¬2␈↓↓␈↓π*␈↓↓[size x-␈↓¬2␈↓↓]␈↓␈↓ λ8by definition of ␈↓↓size␈↓ and 2.6
␈↓ ↓H␈↓␈↓ βx␈↓↓≥ ␈↓¬2␈↓↓␈↓π*␈↓↓[size x-␈↓¬1␈↓↓]␈↓␈↓ λ8since ␈↓↓size ␈↓αa|␈↓↓x␈↓≥␈↓¬1␈↓

␈↓ ↓H␈↓and
␈↓ ↓H␈↓148␈↓ ¬qChapter  VII␈↓ H



␈↓ ↓H␈↓␈↓ αX␈↓↓␈↓¬2␈↓↓␈↓π*␈↓↓cfringe x␈↓ βx= ␈↓¬2␈↓↓␈↓π*␈↓↓[cappend[fringe ␈↓αa|␈↓↓x,fringe ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓  + cfringe ␈↓αa|␈↓↓x + cfringe ␈↓αd|␈↓↓x]␈↓␈↓ λ_by definition of ␈↓↓cfringe␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓≤ ␈↓¬2␈↓↓␈↓π*␈↓↓size ␈↓αa|␈↓↓x ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓  + [size ␈↓αa|␈↓↓x+␈↓¬1␈↓↓]␈↓π*␈↓↓[size ␈↓αa|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓  + [size ␈↓αd|␈↓↓x+␈↓¬1␈↓↓]␈↓π*␈↓↓[size ␈↓αd|␈↓↓x]␈↓␈↓ λ_by induction and 2.6
␈↓ ↓H␈↓␈↓ βx␈↓↓≤ [sixe ␈↓αa|␈↓↓x+size ␈↓αd|␈↓↓x+␈↓¬1␈↓↓]␈↓π*␈↓↓[size qa+size ␈↓αd|␈↓↓x]␈↓␈↓ λ_by computation
␈↓ ↓H␈↓␈↓ βx␈↓↓≤ [sixe x+␈↓¬1␈↓↓]␈↓π*␈↓↓[size x]␈↓␈↓ λ_by definition of ␈↓↓size␈↓

␈↓ ↓H␈↓Thus␈αwith␈αthe␈αaid␈αof␈αthe␈αderived␈αprograms␈α␈↓↓cflat␈↓␈αand␈α␈↓↓cfringe␈↓␈αwe␈αare␈αable␈αto␈αmake␈αa␈αquantitative
␈↓ ↓H␈↓comparison of the amount of computation done by ␈↓↓flat␈↓ and ␈↓↓fringe.␈↓

␈↓ ↓H␈↓        The␈α
derived␈α
program␈α
that␈α
computes␈α
the␈α
number␈α
of␈α
␈↓↓cons␈↓es␈α
executed␈α
in␈α
computing␈α
a␈α
program
␈↓ ↓H␈↓is␈α∞an␈α∞example␈α∞of␈α∂a␈α∞general␈α∞class␈α∞of␈α∞␈↓↓cost␈↓␈α∂computing␈α∞derived␈α∞programs.␈α∞ The␈α∞rules␈α∂for␈α∞obtaining
␈↓ ↓H␈↓such␈αa␈α
derived␈αprogram␈αfrom␈α
the␈αoriginal␈α
program␈αdefinition␈αare␈α
as␈αfollows.␈α
 First␈αassociate␈αa␈α
cost
␈↓ ↓H␈↓program␈α
with␈α∞each␈α
of␈α
the␈α∞function␈α
applications␈α
occuring␈α∞in␈α
defining␈α
expression.␈α∞ (This␈α
includes
␈↓ ↓H␈↓associating␈α
the␈αprogram␈α
to␈αbe␈α
derived␈αwith␈α
the␈αoriginal␈α
program.)␈α Then␈α
transform␈α
the␈αdefining
␈↓ ↓H␈↓expression.␈α  If␈αthe␈αexpression␈αis␈αa␈αconstant␈αor␈αvariable␈αthen␈αthe␈αcost␈αis␈α␈↓¬0.␈α␈↓␈αIf␈αthe␈αexpression␈αis␈αa
␈↓ ↓H␈↓conditional of the form

␈↓ ↓H␈↓␈↓ ¬N␈↓↓␈↓αif␈↓↓ p ␈↓αthen␈↓↓ t␈↓β1␈↓↓ ␈↓αelse␈↓↓ t␈↓β2␈↓↓␈↓ 

␈↓ ↓H␈↓then it is transformed into

␈↓ ↓H␈↓␈↓ ∧+␈↓↓<cost(p)>+␈↓αif␈↓↓ p ␈↓αthen␈↓↓ <cost(t␈↓β1␈↓↓)> ␈↓αelse␈↓↓ <cost(t␈↓β2␈↓↓)>␈↓. 

␈↓ ↓H␈↓where␈α␈↓↓<cost(e)>␈↓␈α
is␈αthe␈α
cost␈αterm␈α
derived␈αby␈α
applying␈αthe␈α
rules␈αto␈α
to␈α␈↓↓e.␈↓␈α
 If␈αthe␈α
expression␈αis␈α
a␈αlist
␈↓ ↓H␈↓forming␈α⊂operation␈α⊂we␈α⊂replace␈α∂it␈α⊂by␈α⊂the␈α⊂equivalent␈α⊂composition␈α∂of␈α⊂␈↓↓cons␈↓es␈α⊂and␈α⊂then␈α⊂proceed␈α∂as
␈↓ ↓H␈↓usual.␈α⊂ If␈α⊂the␈α∂expression␈α⊂is␈α⊂the␈α⊂application␈α∂of␈α⊂a␈α⊂function,␈α∂␈↓↓f,␈↓␈α⊂to␈α⊂a␈α⊂list␈α∂of␈α⊂arguments␈α⊂then␈α⊂it␈α∂is
␈↓ ↓H␈↓transformed␈αinto␈αthe␈αsum␈αof␈αthe␈αcosts␈α
of␈αthe␈αarguments␈αplus␈αthe␈αcost␈αfunction␈αcorresponding␈α
to␈α␈↓↓f␈↓
␈↓ ↓H␈↓applied␈α∂to␈α∂the␈α∂argument␈α∂values.␈α∂ If␈α∂the␈α∂expression␈α∂is␈α∂a␈α∂lambda␈α∂expression␈α∂applied␈α∂to␈α∂a␈α∂list␈α∂of
␈↓ ↓H␈↓arguments␈α
it␈α
is␈α
transformed␈α
into␈α
the␈αsum␈α
of␈α
the␈α
costs␈α
of␈α
the␈αarguments␈α
plus␈α
the␈α
cost␈α
of␈α
the␈αbody␈α
of
␈↓ ↓H␈↓the lambda expression.

␈↓ ↓H␈↓        Thus␈α⊂if␈α⊂we␈α∂put␈α⊂␈↓↓ccons[x,y]←␈↓¬1␈↓↓␈↓␈α⊂and␈α∂all␈α⊂other␈α⊂cost␈α∂functions␈α⊂except␈α⊂␈↓↓cflat␈↓␈α∂equal␈α⊂to␈α⊂the␈α∂zero
␈↓ ↓H␈↓function␈αand␈αapply␈αthe␈αabove␈αalgorithm␈αwe␈αobtain␈αthe␈αdefinition␈αof␈α␈↓↓cflat␈↓␈αgiven␈αabove.␈α Similarly
␈↓ ↓H␈↓for ␈↓↓cfringe.␈↓

␈↓ ↓H␈↓        There␈α⊂are␈α∂other␈α⊂intensional␈α⊂properties␈α∂of␈α⊂interest.␈α∂For␈α⊂example␈α⊂the␈α∂number␈α⊂of␈α⊂calls␈α∂␈↓↓flat␈↓
␈↓ ↓H␈↓makes to itself is given by

␈↓ ↓H␈↓↓␈↓ βxrflat[x, u] ←
␈↓ ↓H␈↓↓2.7)␈↓ ∧8␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ ␈↓¬1 ␈↓↓+ rflat[␈↓αa|␈↓↓x, flat[␈↓αd|␈↓↓x, u]] + rflat[␈↓αd|␈↓↓x, u].

␈↓ ↓H␈↓Thus

␈↓ ↓H␈↓␈↓ ∧A␈↓↓rflat[␈↓¬(A . (B . (C . D)))␈↓↓, ␈↓¬NIL␈↓↓] = ␈↓¬7 ␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧>␈↓↓rflat[␈↓¬(((A . B) . C) . D)␈↓↓, ␈↓¬NIL␈↓↓] = ␈↓¬7 ␈↓↓␈↓.

␈↓ ↓H␈↓We can express ␈↓↓rflat␈↓ in terms of the size of the input by
␈↓ ↓H␈↓␈↓ ¬qChapter  VII␈↓ ≠149


␈↓ ↓H␈↓2.8)␈↓ ∧⎇␈↓↓∀x u: rflat[x, u] = ␈↓¬2␈↓↓␈↓π*␈↓↓size x - ␈↓¬1␈↓↓␈↓. 

␈↓ ↓H␈↓The maximum depth of recursion occuring in a computation of ␈↓↓flat␈↓ is given by

␈↓ ↓H␈↓↓␈↓ βxdflat[x, u] ←
␈↓ ↓H␈↓↓2.9)␈↓ ∧8if ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬0␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ ␈↓¬1 ␈↓↓+ max[dflat[␈↓αa|␈↓↓x, flat[␈↓αd|␈↓↓x, u]], dflat[␈↓αd|␈↓↓x, u]],

␈↓ ↓H␈↓so that

␈↓ ↓H␈↓␈↓ ∧?␈↓↓dflat[␈↓¬(A . (B . (C . D)))␈↓↓, ␈↓¬NIL␈↓↓] = ␈↓¬3 ␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧?␈↓↓dflat[␈↓¬((A . B) . (C . D))␈↓↓, ␈↓¬NIL␈↓↓] = ␈↓¬2 ␈↓↓␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓2.10)␈↓ βG␈↓↓∀x u: dflat[x, u] = ␈↓¬length of longest ␈↓αa|␈↓¬- ␈↓αd|␈↓¬chain in␈↓↓ x␈↓.

␈↓ ↓H␈↓        A␈α∞somewhat␈α∞more␈α∞complex␈α∞property␈α∞is␈α∞the␈α∂trace␈α∞of␈α∞the␈α∞computation.␈α∞ A␈α∞trace␈α∞is␈α∞a␈α∂list␈α∞of
␈↓ ↓H␈↓n+1-tuples␈α(<arg1>...<argn>␈α<result>)␈αcontaining␈αone␈αsuch␈α
tuple␈αfor␈αeach␈αcall␈αto␈αthe␈αfunction␈α
being
␈↓ ↓H␈↓traced.  Thus if we wish to compute a trace of ␈↓↓flat␈↓ we can use ␈↓↓tflat.␈↓

␈↓ ↓H␈↓↓␈↓ βxtflat[x, u] ←
␈↓ ↓H␈↓↓2.11)␈↓ ∧8␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <<x, u, flat[x, u]>>
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ <x, u, flat[x, u]> . [tflat[␈↓αd|␈↓↓x, u] * tflat[␈↓αa|␈↓↓x, flat[␈↓αd|␈↓↓x, u]]]


␈↓ ↓H␈↓For example the traces of ␈↓↓flat[␈↓¬(A B C . D),NIL␈↓↓]␈↓ and ␈↓↓flat[␈↓¬(((A . B) . C) . D),NIL␈↓↓]␈↓ are

␈↓ ↓H␈↓¬␈↓ βx␈↓↓tflat␈↓¬[(A . (B . (C . D))), NIL] =
␈↓ ↓H␈↓¬␈↓ ¬((((A B C . D) NIL (A B C D))
␈↓ ↓H␈↓¬␈↓ ¬8((B C . D) NIL (B C D))
␈↓ ↓H␈↓¬␈↓ ¬8((C . D) NIL (C D))
␈↓ ↓H␈↓¬␈↓ ¬8(D NIL (D))
␈↓ ↓H␈↓¬␈↓ ¬8(C (D) (C D))
␈↓ ↓H␈↓¬␈↓ ¬8(B (C D) (B C D))
␈↓ ↓H␈↓¬␈↓ ¬8(A (B C D) (A B C D)))

␈↓ ↓H␈↓¬␈↓ βx␈↓↓tflat␈↓¬[(((A . B) . C) . D), NIL] =
␈↓ ↓H␈↓¬␈↓ ¬((((((A . B) . C) . D) NIL (A B C D))
␈↓ ↓H␈↓¬␈↓ ¬8(D NIL (D))
␈↓ ↓H␈↓¬␈↓ ¬8(((A . B) . C) (D) (A B C D))
␈↓ ↓H␈↓¬␈↓ ¬8(C (D) (C D))
␈↓ ↓H␈↓¬␈↓ ¬8((A . B) (C D) (A B C D))
␈↓ ↓H␈↓¬␈↓ ¬8(B (C D) (B C D))
␈↓ ↓H␈↓¬␈↓ ¬8(A (B C D) (A B C D))))

␈↓ ↓H␈↓The␈αidea␈αfor␈αgeneral␈αcost␈αcomputing␈αprograms␈αis␈αdue␈αto␈αBen␈αMoszkoswki.␈α This␈αis␈αdescribed␈αand
␈↓ ↓H␈↓many sample applications are given in Moszkowski[1978].
␈↓ ↓H␈↓150␈↓ ¬qChapter  VII␈↓ H


␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.␈αDefine␈αfunctions␈αto␈αcompute␈αthe␈αnumber␈αof␈α␈↓↓cons␈↓es␈αexecuted␈αby␈αthe␈αprograms␈α␈↓↓reverse␈↓␈α(I.8.7)␈αand
␈↓ ↓H␈↓␈↓↓reversea␈↓ (I.8.9) and work out the relation between these costs.



␈↓ ↓H␈↓3.  ␈↓αSubstantial exercises␈↓


␈↓ ↓H␈↓1.␈αWrite␈αa␈α
program␈αto␈αconvert␈α
functional␈αLISP␈αprograms␈α
into␈αiterative␈α(sequential)␈α
programs␈αfor
␈↓ ↓H␈↓some class of programs.

␈↓ ↓H␈↓2. Implement the algorithm for producing cost computing derived functions described in ␈↓π∞␈↓2.

␈↓ ↓H␈↓3.␈α
Write␈α
a␈α
program␈α
that␈α∞transforms␈α
a␈α
program␈α
into␈α
one␈α∞that␈α
computes␈α
the␈α
trace␈α
of␈α∞a␈α
functional
␈↓ ↓H␈↓LISP program.
␈↓ ↓H␈↓␈↓ εH␈↓ ≠151


␈↓ ↓H␈↓α␈↓ ¬nChapter VIII

␈↓ ↓H␈↓α␈↓ ¬.ABSTRACT SYNTAX




␈↓ ↓H␈↓1.  ␈↓αWhat is Abstract Syntax?␈↓


␈↓ ↓H␈↓        When␈α
we␈α
compute␈α
with␈α
symbolic␈α
expressions,␈α
whether␈α
they␈α
be␈α
computer␈αprograms,␈α
algebraic
␈↓ ↓H␈↓expressions,␈α
or␈α
formulas␈αof␈α
logic,␈α
many␈αaspects␈α
of␈α
their␈αrepresentation␈α
are␈α
irrelevant␈αfor␈α
theoretical
␈↓ ↓H␈↓purposes.␈α
 For␈αexample,␈α
the␈αsum␈α
of␈α␈↓↓a␈↓␈α
and␈α␈↓↓b␈↓␈α
may␈α
be␈αwritten␈α
␈↓↓a + b␈↓␈αas␈α
in␈αthe␈α
most␈αcommon␈α
algebraic
␈↓ ↓H␈↓notation,␈α
␈↓¬(PLUS␈α
␈↓↓a␈↓¬␈α
␈↓↓b␈↓¬)␈↓␈α∞as␈α
in␈α
LISP,␈α
␈↓↓+ab␈↓␈α∞in␈α
"Polish␈α
notation"␈α
or␈α∞␈↓↓ab+␈↓␈α
in␈α
the␈α
reverse␈α∞Polish␈α
notation
␈↓ ↓H␈↓made␈α↔popular␈α↔by␈α↔the␈α↔Hewlett-Packard␈α↔calculators.␈α↔ Mathematical␈α↔logicians␈α↔even␈α⊗represent
␈↓ ↓H␈↓expressions␈αby␈α"G␈↓π:␈↓odel␈αnumbers"␈αwhen␈αthey␈αwant␈αto␈αrepresent␈αsymbolic␈αcomputations␈αas␈αnumerical
␈↓ ↓H␈↓computations.␈α⊂  One␈α∂such␈α⊂G␈↓π:␈↓odel␈α∂numbering␈α⊂would␈α∂represent␈α⊂the␈α∂sum␈α⊂of␈α∂␈↓↓a␈↓␈α⊂and␈α∂␈↓↓b␈↓␈α⊂as␈α⊂2␈↓∧a␈↓3␈↓∧b␈↓.␈α∂ Of
␈↓ ↓H␈↓course,␈α
"the␈αsum␈α
of␈α␈↓↓a␈↓␈α
and␈α
␈↓↓b␈↓"␈αis␈α
just␈αanother␈α
such␈αnotation␈α
which␈α
we␈αhave␈α
been␈αusing␈α
to␈αtalk␈α
about
␈↓ ↓H␈↓the others.

␈↓ ↓H␈↓        If,␈α
for␈α
example,␈α
we␈α
are␈αinterested␈α
in␈α
evaluating␈α
sums␈α
or␈αin␈α
compiling␈α
them,␈α
the␈α
details␈αof␈α
the
␈↓ ↓H␈↓representation␈αmay␈α
be␈αirrelevant.␈α We␈α
need␈αto␈α
be␈αable␈αto␈α
tell␈αwhether␈α
an␈αexpression␈αis␈α
a␈αsum,␈αif␈α
so
␈↓ ↓H␈↓what␈αthe␈αsummands␈αare,␈αand␈αwe␈αmay␈αneed␈αto␈αbe␈αable␈αto␈αmake␈αsums␈αfrom␈αconstituent␈αexpressions.
␈↓ ↓H␈↓The␈α∩predicate␈α∩that␈α⊃tells␈α∩whether␈α∩an␈α∩expression␈α⊃is␈α∩a␈α∩sum␈α∩and␈α⊃the␈α∩functions␈α∩that␈α∩extract␈α⊃the
␈↓ ↓H␈↓summands␈αcomprise␈αthe␈α␈↓↓analytic␈α
syntax␈↓␈αof␈αsums.␈α The␈αfunction␈α
that␈αmakes␈αa␈αsum␈α
expression␈αout
␈↓ ↓H␈↓of␈α∩the␈α∩summand␈α∪expression␈α∩gives␈α∩the␈α∪␈↓↓synthetic␈α∩syntax␈↓␈α∩of␈α∪sums.␈α∩ The␈α∩analytic␈α∪and␈α∩synthetic
␈↓ ↓H␈↓syntaxes are, of course, related to each other.

␈↓ ↓H␈↓        ␈↓↓Abstract␈α∩syntax␈↓,␈α∪first␈α∩described␈α∩in␈α∪[McCarthy␈α∩1962b],␈α∪is␈α∩a␈α∩way␈α∪of␈α∩presenting␈α∪only␈α∩the
␈↓ ↓H␈↓information␈α∪about␈α∩a␈α∪class␈α∪of␈α∩expressions␈α∪that␈α∩is␈α∪relevant␈α∪to␈α∩computing␈α∪with␈α∪them,␈α∩omitting
␈↓ ↓H␈↓irrelevant␈α∩detail.␈α⊃ Later␈α∩we␈α⊃shall␈α∩prove␈α∩a␈α⊃small␈α∩compiler␈α⊃correct␈α∩without␈α⊃ever␈α∩deciding␈α∩on␈α⊃a
␈↓ ↓H␈↓definite␈α∂notation.␈α∂ If␈α∂we␈α∂decide␈α∂on␈α∂a␈α∞notation␈α∂for␈α∂some␈α∂expressions␈α∂and␈α∂write␈α∂programs␈α∂to␈α∞test
␈↓ ↓H␈↓whether␈α∀an␈α∀expression␈α∀is␈α∀of␈α∀a␈α∀given␈α∪type,␈α∀to␈α∀extract␈α∀subexpressions␈α∀and␈α∀to␈α∀construct␈α∪new
␈↓ ↓H␈↓expressions␈α~from␈α→old␈α~ones␈α→then␈α~we␈α~have␈α→give␈α~a␈α→␈↓↓concrete syntax␈↓.␈α~ Any␈α~concrete␈α→syntax
␈↓ ↓H␈↓corresponding to our abstract syntax will do.


␈↓ ↓H␈↓αExample 1. Simplification of arithmetic expressions.

␈↓ ↓H␈↓        Consider␈α
arithmetic␈αexpressions␈α
built␈α
up␈αfrom␈α
constants␈α
and␈αvariables␈α
by␈α
forming␈αsums␈α
and
␈↓ ↓H␈↓products.   The abstract syntax is given in table 4.

␈↓ ↓H␈↓↓␈↓α␈↓ αxpredicate␈↓ ∧xanalytic operation␈↓ πxsynthetic operation␈↓↓

␈↓ ↓H␈↓↓␈↓ αxisconst(e)
␈↓ ↓H␈↓↓␈↓ αxisvar(e)

␈↓ ↓H␈↓↓␈↓ αxissum(e)␈↓ ¬_s1(e),   s2(e)␈↓ λ8mksum(e1,e2)
␈↓ ↓H␈↓↓␈↓ αxisprod(e)␈↓ ¬_p1(e),   p2(e)␈↓ λ8mkprod(e1,e2)

␈↓ ↓H␈↓␈↓ βn␈↓αTable 4.␈↓  Abstract Syntax of Arithmetic Expressions.
␈↓ ↓H␈↓152␈↓ ¬lChapter  VIII␈↓ H


␈↓ ↓H␈↓        The␈α␈↓αpredicate␈↓␈αcolumn␈αasserts␈αthat␈αthe␈αexpressions␈αcomprise␈αconstants,␈αvariables␈αand␈αbinary
␈↓ ↓H␈↓sums␈α
and␈α
procucts,␈α
and␈α
that␈α
the␈α
predicates␈α
␈↓↓isconst,␈↓␈α
␈↓↓isvar,␈↓␈α
␈↓↓issum,␈↓␈α
and␈α
␈↓↓isprod␈↓␈α
enable␈α
one␈α
to␈α
classify
␈↓ ↓H␈↓each␈α∪expression.␈α∪ The␈α∪␈↓αanalytic␈α∪operations␈↓␈α∪column␈α∩tells␈α∪us␈α∪that␈α∪variables␈α∪and␈α∪constants␈α∩are
␈↓ ↓H␈↓"atomic"␈αexpressions,␈αwhile␈αsums␈αand␈α
products␈αhave␈αsubexpressions␈αobtained␈αby␈αapplying␈α
␈↓↓s1␈↓␈αand
␈↓ ↓H␈↓␈↓↓s2␈↓ in the case of sums, or ␈↓↓p1␈↓ and ␈↓↓p2␈↓ in the case of products.

␈↓ ↓H␈↓        The␈α∀predicates␈α∪and␈α∀analytic␈α∀operations␈α∪comprise␈α∀the␈α∀␈↓↓abstract␈α∪analytic␈α∀syntax␈↓␈α∀of␈α∪these
␈↓ ↓H␈↓expressions,␈αe.g.␈αthey␈αallow␈αus␈αto␈αanalyse␈αthe␈αexpressions.␈α The␈αanalytic␈αsyntax␈αprovides␈αsufficient
␈↓ ↓H␈↓information␈α⊂to␈α∂allow␈α⊂us␈α⊂to␈α∂write␈α⊂an␈α⊂interpreter␈α∂(evaluator)␈α⊂thus␈α⊂defining␈α∂the␈α⊂semantics␈α⊂of␈α∂our
␈↓ ↓H␈↓expression language. Thus we have

␈↓ ↓H␈↓␈↓ βx␈↓↓value(e,␈↓πx␈↓↓) ← ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αif␈↓↓ isconst(e) ␈↓αthen␈↓↓ val(e) ␈↓
␈↓ ↓H␈↓1.1)␈↓ ∧8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ isvar(e) ␈↓αthen␈↓↓ c(e,␈↓πx␈↓↓)␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ issum(e) ␈↓αthen␈↓↓ value(s1(e),␈↓πx␈↓↓) + value(s2(e),␈↓πx␈↓↓)␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ isprod(e) ␈↓αthen␈↓↓ value(p1(e),␈↓πx␈↓↓) ␈↓π*␈↓↓ value(p2(e),␈↓πx␈↓↓)␈↓

␈↓ ↓H␈↓where␈α␈↓↓val(e)␈↓␈αgives␈αthe␈αnumerical␈αvalue␈αof␈αan␈αexpression␈α␈↓↓e␈↓␈αrepresenting␈αa␈αconstant,␈α␈↓↓c(e,␈↓πx␈↓↓)␈↓␈αgives␈αthe
␈↓ ↓H␈↓value␈α
of␈α∞the␈α
variable␈α∞␈↓↓e␈↓␈α
in␈α∞the␈α
state␈α∞vector␈α
␈↓πx␈↓␈α∞and␈α
"+"␈α∞and␈α
"␈↓π*␈↓"␈α∞are␈α
some␈α∞binary␈α
operations.␈α∞ (It␈α
is
␈↓ ↓H␈↓natural␈αto␈α
regard␈α"+"␈αand␈α
"␈↓π*␈↓"␈αas␈αan␈α
operations␈αthat␈αresembles␈α
addition␈αand␈αmultiplication␈α
of␈αreal
␈↓ ↓H␈↓numbers, but our we shall need only a few properties of these operations.)

␈↓ ↓H␈↓        The␈α␈↓αsynthetic␈αoperation␈↓s␈αcolumn␈αof␈αtable 4␈αsays␈αthat␈αsums␈αcan␈αbe␈αconstructed␈αfrom␈αa␈αpair
␈↓ ↓H␈↓of␈αexpressions␈αusing␈αthe␈α
operation␈α␈↓↓mksum␈↓␈αand␈αsimilarly␈αproducts␈α
can␈αbe␈αconstructed␈αfrom␈α
a␈αpair
␈↓ ↓H␈↓of␈α
expression␈αusing␈α
␈↓↓mkprod.␈↓␈α These␈α
operations␈α
form␈αthe␈α
␈↓↓abstract␈αsynthetic␈α
syntax␈↓␈α
of␈αexpressions,
␈↓ ↓H␈↓e.g. they allow us to construct new expressions from old ones.

␈↓ ↓H␈↓        Using␈α
the␈α
abstract␈α
syntax␈α
functions␈α
and␈α
predicates␈α
we␈α
can␈α
write␈α
a␈α
function␈α
that␈α
simplifies
␈↓ ↓H␈↓an expression by

␈↓ ↓H␈↓␈↓ βλ1.1. removing 0's from sums,
␈↓ ↓H␈↓␈↓ βλ1.2. removing 1's from products,
␈↓ ↓H␈↓␈↓ βλ1.3. replacing a product containing 0 as a factor by 0.

␈↓ ↓H␈↓We␈α⊃will␈α⊃assume␈α⊃the␈α⊃constants␈α⊃0␈α⊃and␈α⊃1␈α⊂are␈α⊃represented␈α⊃by␈α⊃"0"␈α⊃and␈α⊃"1",␈α⊃for␈α⊃simplicity.␈α⊂ Then
␈↓ ↓H␈↓␈↓↓simplify␈↓ is given by

␈↓ ↓H␈↓␈↓ β8␈↓↓simplify(e) ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓    ␈↓αif␈↓↓ isvar(e) ∨ isconst(e) ␈↓αthen␈↓↓ e␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓    ␈↓αelse␈↓↓ ␈↓αif␈↓↓ issum(e) ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓          {simplify(s1(e)),simplify(s2(e))} ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓          [λe1 e2. ␈↓αif␈↓↓ e1=0 ␈↓αthen␈↓↓ e2 ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓                   ␈↓αelse␈↓↓ ␈↓αif␈↓↓ e2=0 ␈↓αthen␈↓↓ e1 ␈↓
␈↓ ↓H␈↓1.2)␈↓ β8␈↓↓                   ␈↓αelse␈↓↓ mksum(e1,e2)] ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓    ␈↓αelse␈↓↓ ␈↓αif␈↓↓ isprod(e) ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓          {simplify(p1(e)),simplify(p2(e))} ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓          [λe1 e2. ␈↓αif␈↓↓ e1=0 ∨ e2=0 ␈↓αthen␈↓↓ 0 ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓                   ␈↓αelse␈↓↓ ␈↓αif␈↓↓ e1=1 ␈↓αthen␈↓↓ e2 ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓                   ␈↓αelse␈↓↓ ␈↓αif␈↓↓ e2=1 ␈↓αthen␈↓↓ e1 ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓                   ␈↓αelse␈↓↓ mkprod(e1,e2)] ␈↓
␈↓ ↓H␈↓␈↓ ¬lChapter  VIII␈↓ ≠153


␈↓ ↓H␈↓        Suppose␈α∪we␈α∀would␈α∪like␈α∪to␈α∀prove␈α∪something␈α∪about␈α∀our␈α∪functions␈α∪on␈α∀expressions.␈α∪ For
␈↓ ↓H␈↓example␈αwe␈αwould␈αlike␈αto␈αknow␈αthat␈αthe␈αsimplifier␈αwill␈αindeed␈αterminate␈α(e.g.␈αthere␈αare␈αno␈α
infinite
␈↓ ↓H␈↓expressions).␈α Then␈αwe␈α
need␈α and␈αa␈αway␈α
of␈αexpressing␈αthe␈αfact␈α
that␈αthe␈αonly␈α
expressions␈αallowed
␈↓ ↓H␈↓are␈αthose␈αthat␈αcan␈αbe␈αconstructed␈αfrom␈αvariables␈αand␈αconstants␈αby␈αa␈αfinite␈αnumber␈αof␈αapplications
␈↓ ↓H␈↓of␈α∩the␈α∪sum␈α∩and␈α∪product␈α∩constructions.␈α∪ The␈α∩following␈α∪schema␈α∩is␈α∪an␈α∩induction␈α∪principle␈α∩for
␈↓ ↓H␈↓expressions essentially expressing this fact.

␈↓ ↓H␈↓␈↓ αλ    Suppose␈α
␈↓πF␈↓␈α
is␈α
a␈αpredicate␈α
applicable␈α
to␈α
expressions,␈α
and␈αsuppose␈α
that␈α
for␈α
all␈αexpressions␈α
␈↓↓e,␈↓
␈↓ ↓H␈↓␈↓ αλwe have

␈↓ ↓H␈↓↓␈↓ ∧8isconst(e) ⊃ ␈↓πF␈↓↓(e)  ␈↓and␈↓↓
␈↓ ↓H␈↓↓1.3)␈↓ ∧8isvar(e) ⊃ ␈↓πF␈↓↓(e)  ␈↓and␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8issum(e) ∧ ␈↓πF␈↓↓(s1(e)) ∧ ␈↓πF␈↓↓(s2(e)) ⊃ ␈↓πF␈↓↓(e)   ␈↓and␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8isprod(e) ∧ ␈↓πF␈↓↓(p1(e)) ∧ ␈↓πF␈↓↓(p2(e)) ⊃ ␈↓πF␈↓↓(e).

␈↓ ↓H␈↓    Then we may conclude that ␈↓πF␈↓(e) is true for all expressions ␈↓↓e.␈↓

␈↓ ↓H␈↓Using this principle we can show that the simplifier terminates by the techniques of Chapter III.

␈↓ ↓H␈↓        Now␈αsuppose␈αwe␈αwould␈α
like␈αto␈αshow␈αthat␈αthe␈α
simplifier␈αpreserves␈αthe␈αvalue␈α
of␈αexpressions.
␈↓ ↓H␈↓Then␈α∃we␈α∃will␈α∀need␈α∃some␈α∃facts␈α∃about␈α∀the␈α∃relations␈α∃satisfied␈α∀by␈α∃the␈α∃analytic␈α∃and␈α∀synthetic
␈↓ ↓H␈↓components of the syntax.  Thus for expressions ␈↓↓e,␈↓ ␈↓↓e1,␈↓ ␈↓↓e2␈↓ we have

␈↓ ↓H␈↓␈↓ ∧8␈↓↓issum(mksum(e1,e2)) ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓s1(mksum(e1,e2))=e1 ␈↓
␈↓ ↓H␈↓1.4)␈↓ ∧8␈↓↓s2(mksum(e1,e2))=e2 ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓issum(e) ⊃ e=mksum(s1(e),s2(e)) ␈↓

␈↓ ↓H␈↓␈↓ ∧8␈↓↓isprod(mkprod(e1,e2)) ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓p1(mkprod(e1,e2))=e1 ␈↓
␈↓ ↓H␈↓1.5)␈↓ ∧8␈↓↓p2(mkprod(e1,e2))=e2 ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓isprod(e) ⊃ e=mkprod(p1(e),p2(e)) ␈↓

␈↓ ↓H␈↓Using␈α⊃these␈α⊃relations,␈α⊃the␈α⊃principle␈α⊂of␈α⊃induction,␈α⊃and␈α⊃some␈α⊃simple␈α⊂properties␈α⊃of␈α⊃the␈α⊃+␈α⊃and␈α⊂␈↓π*␈↓
␈↓ ↓H␈↓operations involving 0 and 1 we can show that

␈↓ ↓H␈↓1.6)␈↓ ¬β␈↓↓value(e,␈↓πx␈↓↓)=value(simplify(e),␈↓πx␈↓↓)␈↓ 

␈↓ ↓H␈↓far all expressions ␈↓↓e␈↓ and state vectors ␈↓πx␈↓.


␈↓ ↓H␈↓αExample 2. Abstract and Concrete Syntax of LISP terms.

␈↓ ↓H␈↓        Now␈αwe␈αwill␈αgive␈αthe␈αabstract␈αand␈αconcrete␈αanalytic␈αsyntax␈αof␈αpure␈α(functional)␈αLISP␈αterms.
␈↓ ↓H␈↓We␈αwill␈α
see␈αin␈α
the␈αcase␈α
of␈αLISP␈α
the␈αconcrete␈α
syntax␈αis␈α
very␈αclose␈α
to␈αthe␈α
abstract␈αsyntax.␈α
 This␈αis
␈↓ ↓H␈↓one␈αreason␈αthat␈αit␈αis␈αso␈αeasy␈αto␈αwrite␈αLISP␈αprograms␈αto␈αmanipulate␈αLISP␈αprograms.␈α We␈αwill␈αalso
␈↓ ↓H␈↓introduce in this example additional techniques which are useful for defining abstract syntax.

␈↓ ↓H␈↓        A␈α
pure␈α
LISP␈α
term␈α
is␈αessentially␈α
any␈α
term␈α
that␈α
can␈α
occur␈αat␈α
the␈α
right␈α
hand␈α
side␈α
of␈αa␈α
defining
␈↓ ↓H␈↓equation␈α⊂as␈α⊂described␈α∂in␈α⊂Chapter␈α⊂I␈α∂(excluding␈α⊂labels).␈α⊂ Thus␈α∂LISP␈α⊂terms␈α⊂are␈α⊂either␈α∂variables,
␈↓ ↓H␈↓154␈↓ ¬lChapter  VIII␈↓ H


␈↓ ↓H␈↓constants,␈α
conditional␈α
terms,␈αlist␈α
terms,␈α
or␈αapplication␈α
terms,␈α
denoted␈αby␈α
␈↓↓lvar,␈↓␈α
␈↓↓lconst,␈↓␈α
␈↓↓lcond,␈↓␈α␈↓↓llist␈↓
␈↓ ↓H␈↓and ␈↓↓lappl␈↓ respectively.

␈↓ ↓H␈↓        We␈αwill␈αuse␈αsome␈αfurther␈αclasses␈αof␈αexpressions␈αwhich␈αare␈αnot␈αLISP␈αterms␈αbut␈αare␈αuseful␈αin
␈↓ ↓H␈↓analyzing␈α⊃the␈α⊃composite␈α⊃terms.␈α⊂ These␈α⊃are␈α⊃the␈α⊃classes␈α⊂of␈α⊃function␈α⊃expressions:␈α⊃basic␈α⊂functions
␈↓ ↓H␈↓(␈↓↓lbfun),␈↓␈α⊃defined␈α⊂functions␈α⊃(␈↓↓ldfun),␈↓␈α⊃and␈α⊂lambda␈α⊃expressions␈α⊃(␈↓↓llamb)␈↓␈α⊂ that␈α⊃occur␈α⊃in␈α⊂application
␈↓ ↓H␈↓terms.

␈↓ ↓H␈↓        Finally␈αsome␈αof␈αthe␈αcomponents␈αof␈αthe␈αexpressions␈αwill␈αbe␈αlists␈αof␈αexpressions.␈α We␈αwill␈αuse
␈↓ ↓H␈↓␈↓↓car,␈↓␈α␈↓↓cdr␈↓␈αand␈α
␈↓↓null␈↓␈αto␈αanalyse␈αthe␈α
lists␈αof␈αexpressions.␈α
 It␈αshould␈αbe␈αemphasized␈α
that␈αthis␈αuse␈α
is␈αin
␈↓ ↓H␈↓an␈α∞abstract␈α∞sense␈α∞and␈α∞does␈α∞not␈α∞depend␈α
on␈α∞any␈α∞properties␈α∞of␈α∞a␈α∞particular␈α∞representation␈α∞of␈α
lists.
␈↓ ↓H␈↓There␈α
will␈α
be␈α
three␈α
special␈αkinds␈α
of␈α
lists:␈α
the␈α
list␈α
of␈αpairs␈α
of␈α
terms␈α
in␈α
a␈α
conditional␈αterm␈α
(␈↓↓lpairlist),␈↓
␈↓ ↓H␈↓the␈α∂list␈α∂of␈α∂arguments␈α∂in␈α∂an␈α∂application␈α∂term␈α∂(␈↓↓ltermlist),␈↓␈α∂and␈α∂the␈α∂list␈α∂of␈α∂variables␈α∂in␈α∂a␈α∞lambda
␈↓ ↓H␈↓expression (␈↓↓lvarlist).␈↓

␈↓ ↓H␈↓        The abstract analytic syntax of LISP terms is given in table 5.

␈↓ ↓H␈↓↓␈↓ βx␈↓αpredicate␈↓ ε8associated functions␈↓↓

␈↓ ↓H␈↓↓␈↓ βxlconst(e)
␈↓ ↓H␈↓↓␈↓ βxlvar(e)
␈↓ ↓H␈↓↓␈↓ βxlbfun(e)
␈↓ ↓H␈↓↓␈↓ βxldfun(e)
␈↓ ↓H␈↓↓␈↓ βxnull(e)

␈↓ ↓H␈↓↓␈↓ βxlcond(e)␈↓ ε8pairl(e)
␈↓ ↓H␈↓↓␈↓ βxllist(e)␈↓ ε8terml(e)
␈↓ ↓H␈↓↓␈↓ βxlappl(e)␈↓ ε8funx(e),  argl(e)

␈↓ ↓H␈↓↓␈↓ βxllamb(e)␈↓ ε8varl(e),  body(e)
␈↓ ↓H␈↓↓␈↓ βxlpairlist(e)␈↓ ε8ifpart(e), thenpart(e), elsepart(e)
␈↓ ↓H␈↓↓␈↓ βxltermlist␈↓ ε8car,   cdr
␈↓ ↓H␈↓↓␈↓ βxlvarlist␈↓ ε8car,   cdr

␈↓ ↓H␈↓␈↓ ∧v␈↓αTable 5.␈↓  LISP Absrtact Syntax.


␈↓ ↓H␈↓        In␈αthe␈αusual␈α
S-expression␈αrepresentation␈αof␈α
LISP␈αterms␈α(internal␈α
form)␈αwe␈αwould␈α
have␈αfor
␈↓ ↓H␈↓the basic predicates the following definitions

␈↓ ↓H␈↓␈↓ αx␈↓↓lconst x ← [␈↓αat|␈↓↓x ∧ [numberp x ∨ x=␈↓¬T␈↓↓ ∨ x=␈↓¬NIL␈↓↓]] ∨ ␈↓αa|␈↓↓x=␈↓¬QUOTE ␈↓↓␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓lvar x ← ␈↓αat|␈↓↓x ∧ ¬[numberp x ∨ x=␈↓¬T␈↓↓ ∨ x=␈↓¬NIL␈↓↓] ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓lbfun(e) ← [e=␈↓¬CAR ␈↓↓∨ e=␈↓¬CDR ␈↓↓∨ e=␈↓¬CONS ␈↓↓∨ e=␈↓¬ATOM ␈↓↓∨ e=␈↓¬EQ␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓ldfun(e) ← ␈↓αat|␈↓↓e ∧ ¬[numberp(e) ∨ e=␈↓¬T␈↓↓ ∨ e=␈↓¬NIL␈↓↓ ∨ lbfun(e)] ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓null(e) ← ␈↓αn|␈↓↓e ␈↓

␈↓ ↓H␈↓The concrete syntax for conditional terms and the related auxiliary expressions is given by

␈↓ ↓H␈↓␈↓ αx␈↓↓lcond(e) ← ␈↓αa|␈↓↓e=␈↓¬COND ␈↓↓∧ lpairlist(pairl(e))␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓pairl(e) ← ␈↓αd|␈↓↓e ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓lpairlist(e) ← ␈↓αn|␈↓↓e ∨ [lterm(ifpart(e)) ∧ lterm(thenpart(e) ∧ lpairlist(elsepart(e))]␈↓
␈↓ ↓H␈↓␈↓ ¬lChapter  VIII␈↓ ≠155


␈↓ ↓H␈↓␈↓ αx␈↓↓ifpart(e) ← ␈↓αaa|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓thenpart(e) ← ␈↓αada|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓elsepart(e) ← ␈↓αd|␈↓↓e␈↓

␈↓ ↓H␈↓For list terms we have

␈↓ ↓H␈↓␈↓ αx␈↓↓llist(e) ← ␈↓αa|␈↓↓e=␈↓¬LIST ␈↓↓∧ ltermlist(terml(e))␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓terml(e) ← ␈↓αd|␈↓↓e ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓ltermlist(e) ← ␈↓αn|␈↓↓e ∨ [lterm(car(e)) ∧ ltermlist(cdr(e))]␈↓

␈↓ ↓H␈↓The concrete syntax for application terms and lambda expression is the following

␈↓ ↓H␈↓␈↓ αx␈↓↓lappl(e) ← [lbfun(funx(e)) ∨ ldfun(funx(e)) ∨ llamb(funx(e))] ∧ ltermlist(argl(e))␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓funx(e) ← ␈↓αa|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓argl(e) ← ␈↓αd|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓llamb(e) ← ␈↓αa|␈↓↓e=␈↓¬LAMBDA ␈↓↓∧ lvarlist(varl(e)) ∧ lterm(body(e))␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓varl(e) ← ␈↓αad|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓body(e) ← ␈↓αadd|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓lvarlist(e)← ␈↓αn|␈↓↓e ∨ [lvar(car(e)) ∧ lvarlist(cdr(e))]␈↓



␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.␈α∂Prove␈α∂the␈α∂statements␈α∂made␈α∂about␈α∂the␈α∂expression␈α∂simplifier␈α∂in␈α∂example␈α∂1.␈α∂ That␈α∂is,␈α∂prove␈α∞it
␈↓ ↓H␈↓terminates by defining a predicate ␈↓↓isexp␈↓ such that

␈↓ ↓H␈↓␈↓ β␈␈↓↓isexp(e) ≡ isvar(e) ∨ isconst(e) ∨ issum(e) ∨ isprod(e)␈↓ 

␈↓ ↓H␈↓and␈αshowing␈α
that␈α␈↓↓isexp(simplify(e))␈↓␈α
for␈αall␈α
expressions␈α␈↓↓e.␈↓␈α
 Then␈αshow␈α
that␈αthe␈α
statement␈α(1.6)␈α
hold
␈↓ ↓H␈↓for all expressions.

␈↓ ↓H␈↓2.␈α Work␈αout␈αthe␈αsynthetic␈αsyntax␈αfor␈αLISP␈αterms␈αand␈αstate␈αrelations␈αfor␈αeach␈αconstruct␈αanalagous
␈↓ ↓H␈↓to those given for the expressions in example 1.  State an induction principle for LISP terms.



␈↓ ↓H␈↓2.  ␈↓αCorrectness of a Compiler for Arithmetic Expressions.␈↓


␈↓ ↓H␈↓        In␈α⊃the␈α⊃following␈α⊃sections␈α⊃we␈α⊃prove␈α⊂the␈α⊃correctness␈α⊃of␈α⊃a␈α⊃simple␈α⊃compiling␈α⊃algorithm␈α⊂for
␈↓ ↓H␈↓compiling␈α
arithmetic␈α
expressions␈α
into␈α
machine␈α
language.␈α
 The␈α
presentation␈α
is␈α
adapted␈α
from␈αthat
␈↓ ↓H␈↓of McCarthy and Painter[1967].

␈↓ ↓H␈↓        We␈αbegin␈αwith␈αa␈αbrief␈αoutline␈αof␈αthe␈αmethods␈αused␈αto␈αformalize␈αthe␈αproblem.␈α The␈αanalytic
␈↓ ↓H␈↓abstract␈α∪syntax␈α∩is␈α∪given␈α∩for␈α∪both␈α∪the␈α∩arithmetic␈α∪expressions␈α∩(source␈α∪language)␈α∪and␈α∩machine
␈↓ ↓H␈↓language␈α∞programs␈α∞(object␈α∞language).␈α∞ An␈α∞interpreter␈α∞defined␈α∞in␈α∞terms␈α∞of␈α∞the␈α∞analytic␈α∞syntax␈α
is
␈↓ ↓H␈↓given␈αfor␈αeach␈αlanguage.␈α The␈αtwo␈αinterpreters␈αare␈αwritten␈αin␈αthe␈αsame␈αlanguage,␈αand␈αthus␈αdefine
␈↓ ↓H␈↓the␈αsemantics␈αof␈α
the␈αtwo␈αlanguages␈αin␈α
common␈αterms.␈α The␈αinterpreters␈α
each␈αmake␈αuse␈αof␈α
a␈α␈↓↓state␈↓
␈↓ ↓H␈↓␈↓↓vector␈↓␈αto␈α
determine␈αthe␈α
values␈αof␈α
variables␈α(in␈αthe␈α
expression␈αlanguage␈α
case)␈αor␈α
to␈αstore␈αcontents␈α
of
␈↓ ↓H␈↓156␈↓ ¬lChapter  VIII␈↓ H


␈↓ ↓H␈↓registers␈α∂(in␈α⊂the␈α∂machine␈α⊂language␈α∂case).␈α⊂  The␈α∂synthetic␈α∂syntax␈α⊂is␈α∂also␈α⊂given␈α∂for␈α⊂the␈α∂machine
␈↓ ↓H␈↓language␈αand␈αthe␈α
compiler␈αis␈αdefined␈αin␈α
terms␈αof␈αthe␈αanalytic␈α
syntax␈αof␈αthe␈αsource␈α
language␈αand
␈↓ ↓H␈↓the␈αsynthetic␈αsyntax␈αof␈α
the␈αobject␈αlanguage.␈α Thus␈αthe␈α
compiler␈αbreaks␈αdown␈αterms␈αin␈α
the␈αsource
␈↓ ↓H␈↓language␈αand␈α
rebuilds␈αthem␈αin␈α
the␈αobject␈αlanguage.␈α
 To␈αshow␈α
that␈αthe␈αcompiler␈α
is␈αcorrect␈αwe␈α
must
␈↓ ↓H␈↓show␈αthat␈αfor␈αan␈αexpression␈α
␈↓↓e␈↓␈αthe␈αinterpretation␈αof␈α␈↓↓e␈↓␈α
 by␈αthe␈αexpression␈αinterpreter␈αgives␈αthe␈α
same
␈↓ ↓H␈↓answer␈αas␈αthe␈α
execution␈αof␈αthe␈α
compiled␈αcode␈αfor␈α
␈↓↓e␈↓␈αby␈αthe␈α
machine␈αlanguage␈αinterpreter,␈α
assuming
␈↓ ↓H␈↓that they start out with the same values for all of the variables in ␈↓↓e.␈↓

␈↓ ↓H␈↓        The␈α⊃expressions␈α∩of␈α⊃our␈α⊃source␈α∩language␈α⊃are␈α⊃a␈α∩subclass␈α⊃of␈α⊃those␈α∩described␈α⊃in␈α∩␈↓π∞␈↓1.␈α⊃ The
␈↓ ↓H␈↓computer␈α∂language␈α∂into␈α∂which␈α∞these␈α∂expressions␈α∂are␈α∂compiled␈α∞is␈α∂for␈α∂a␈α∂single␈α∂address␈α∞computer
␈↓ ↓H␈↓with␈α∞an␈α∞accumulator,␈α∞called␈α∞␈↓↓ac,␈↓␈α∞and␈α∞four␈α∞instructions:␈α∞␈↓↓li␈↓␈α∞(load␈α∞immediate),␈α∞␈↓↓load,␈↓␈α∞␈↓↓sto␈↓␈α∞(store)␈α
and
␈↓ ↓H␈↓␈↓↓add.␈↓␈α
 (Our␈α
simple␈α
language␈α
does␈α
not␈αrequire␈α
the␈α
use␈α
of␈α
jump␈α
instructions.)␈α
The␈αcompiler␈α
produces
␈↓ ↓H␈↓code␈α⊃that␈α⊂computes␈α⊃the␈α⊂value␈α⊃of␈α⊂the␈α⊃expression␈α⊂being␈α⊃compiled␈α⊂and␈α⊃leaves␈α⊂this␈α⊃value␈α⊃in␈α⊂the
␈↓ ↓H␈↓accumulator.␈α The␈αabove␈α
expression␈αis␈αcompiled␈α
into␈αcode␈αwhich␈α
in␈αassembly␈αlanguage␈αmight␈α
look
␈↓ ↓H␈↓as follows:


␈↓ ↓H␈↓↓␈↓ ¬hload␈↓ εHx
␈↓ ↓H␈↓↓␈↓ ¬hsto␈↓ εHt
␈↓ ↓H␈↓↓␈↓ ¬hli␈↓ εH3
␈↓ ↓H␈↓↓␈↓ ¬hadd␈↓ εHt
␈↓ ↓H␈↓↓␈↓ ¬hsto␈↓ εHt
␈↓ ↓H␈↓↓␈↓ ¬hload␈↓ εHx
␈↓ ↓H␈↓↓␈↓ ¬hsto␈↓ εHt + 1
␈↓ ↓H␈↓↓␈↓ ¬hload␈↓ εHy
␈↓ ↓H␈↓↓␈↓ ¬hsto␈↓ εHt + 2
␈↓ ↓H␈↓↓␈↓ ¬hli␈↓ εH2
␈↓ ↓H␈↓↓␈↓ ¬hadd␈↓ εHt + 2
␈↓ ↓H␈↓↓␈↓ ¬hadd␈↓ εHt + 1
␈↓ ↓H␈↓↓␈↓ ¬hadd␈↓ εHt


␈↓ ↓H␈↓Again␈α∂because␈α∂we␈α∂are␈α∂using␈α∞abstract␈α∂syntax␈α∂there␈α∂is␈α∂no␈α∞commitment␈α∂to␈α∂a␈α∂precise␈α∂form␈α∂for␈α∞the
␈↓ ↓H␈↓object code.



␈↓ ↓H␈↓3.  ␈↓αThe source language. ␈↓


␈↓ ↓H␈↓        As␈α
mentioned␈α
above␈α
the␈αsource␈α
language␈α
is␈α
a␈α
subset␈αof␈α
the␈α
expression␈α
language␈αdescribed␈α
in
␈↓ ↓H␈↓␈↓π∞␈↓1.␈α In␈αparticular,␈αwe␈αwill␈αconsider␈αexpressions␈αthat␈αare␈αvariables,␈αconstants␈αor␈αsums.␈α Products␈αare
␈↓ ↓H␈↓omitted␈α
as␈α
they␈α
introduce␈α
nothing␈α
new␈α
into␈αthe␈α
problem␈α
and␈α
serve␈α
only␈α
to␈α
make␈αeverything␈α
longer.
␈↓ ↓H␈↓Thus␈α∀we␈α∪will␈α∀use␈α∪the␈α∀relevant␈α∪parts␈α∀of␈α∀table␈α∪4␈α∀for␈α∪the␈α∀abstract␈α∪analytic␈α∀syntax␈α∀of␈α∪source
␈↓ ↓H␈↓expressions.␈α
 The␈α
semantics␈α
is␈α
given␈α
by␈α
the␈α
interpreter␈α
␈↓↓value␈↓␈α
(1.1)␈α
by␈α
simply␈α
ignoring␈α
the␈α
␈↓↓isprod␈↓
␈↓ ↓H␈↓clause.

␈↓ ↓H␈↓        For␈α∩proving␈α∩the␈α∩correctness␈α∪of␈α∩compilers,␈α∩we␈α∩don't␈α∪need␈α∩a␈α∩synthetic␈α∩syntax␈α∪for␈α∩source
␈↓ ↓H␈↓language␈α∀expressions␈α∪since␈α∀the␈α∪interpreter␈α∀and␈α∀compiler␈α∪use␈α∀only␈α∪the␈α∀analytic␈α∀syntax.␈α∪ The
␈↓ ↓H␈↓induction␈αprinciple␈αfor␈αthis␈αsimpler␈αclass␈αof␈αexpressions␈α is␈αobtained␈αby␈αdeleting␈αthe␈α␈↓↓isprod␈↓␈αclause
␈↓ ↓H␈↓from the induction hypothesis (1.3).  Thus
␈↓ ↓H␈↓␈↓ ¬lChapter  VIII␈↓ ≠157


␈↓ ↓H␈↓␈↓ αλ    If ␈↓πF␈↓ is a predicate applicable to expressions, and if for all expressions ␈↓↓e,␈↓ we have

␈↓ ↓H␈↓↓␈↓ αλ␈↓ ∧8isconst(e) ⊃ ␈↓πF␈↓↓(e)  ␈↓and␈↓↓
␈↓ ↓H␈↓↓␈↓ αλ3.1)␈↓ ∧8isvar(e) ⊃ ␈↓πF␈↓↓(e)  ␈↓and␈↓↓
␈↓ ↓H␈↓↓␈↓ αλ␈↓ ∧8issum(e) ∧ ␈↓πF␈↓↓(s1(e)) ∧ ␈↓πF␈↓↓(s2(e)) ⊃ ␈↓πF␈↓↓(e).

␈↓ ↓H␈↓␈↓ αλThen we may conclude that ␈↓πF␈↓(e) is true for all expressions ␈↓↓e.␈↓



␈↓ ↓H␈↓4.  ␈↓αThe object language.␈↓


␈↓ ↓H␈↓        We␈αmust␈αgive␈αboth␈αthe␈αanalytic␈αand␈αsynthetic␈αsyntaxes␈αfor␈αthe␈αobject␈αlanguage␈αbecause␈αthe
␈↓ ↓H␈↓interpreter␈α⊂defining␈α⊂its␈α⊂semantics␈α⊂uses␈α⊂the␈α⊂analytic␈α⊂syntax␈α⊂and␈α⊂the␈α⊂compiler␈α⊂uses␈α⊃the␈α⊂synthetic
␈↓ ↓H␈↓syntax.  The analytic and synthetic syntaxes for instructions are given in the table 6.

␈↓ ↓H␈↓↓␈↓ α8␈↓αoperation␈↓ ∧_predicate␈↓ ¬xanalytic operation␈↓ λXsynthetic operation

␈↓ ↓H␈↓α␈↓ αHli␈↓ β_␈↓πa␈↓α␈↓ ∧_isli(s)␈↓ ε8arg(s)␈↓ 	_mkli(␈↓πa␈↓α)
␈↓ ↓H␈↓α␈↓ αHload␈↓ β_x␈↓ ∧_isload(s)␈↓ ε8adr(s)␈↓ 	_mkload(x)
␈↓ ↓H␈↓α␈↓ αHsto␈↓ β_x␈↓ ∧_issto(s)␈↓ ε8adr(s)␈↓ 	_mksto(x)
␈↓ ↓H␈↓α␈↓ αHadd␈↓ β_x␈↓ ∧_isadd(s)␈↓ ε8adr(s)␈↓ 	_mkadd(x)

␈↓ ↓H␈↓␈↓ β}␈↓αTable 6.␈↓  Abstract Syntax of Machine Instructions.


␈↓ ↓H␈↓        A␈αprogram␈αis␈αa␈αlist␈αof␈αinstructions␈αand␈α␈↓↓null(p)␈↓␈αasserts␈αthat␈α␈↓↓p␈↓␈αis␈αthe␈αnull␈αlist.␈α If␈αthe␈α
program
␈↓ ↓H␈↓␈↓↓p␈↓␈α⊃is␈α∩not␈α⊃null␈α∩then␈α⊃␈↓↓first(p)␈↓␈α∩gives␈α⊃the␈α⊃first␈α∩instruction␈α⊃and␈α∩␈↓↓rest(p)␈↓␈α⊃gives␈α∩the␈α⊃list␈α∩of␈α⊃remaining
␈↓ ↓H␈↓instructions.␈α We␈αshall␈αuse␈αthe␈αoperation␈α␈↓↓p1*p2␈↓␈αto␈αdenote␈αthe␈αprogram␈αobtained␈αby␈αappending␈α␈↓↓p2␈↓
␈↓ ↓H␈↓onto␈αthe␈αend␈αof␈α␈↓↓p1.␈↓␈α Since␈αwe␈αhave␈αonly␈αone␈αlevel␈αof␈αlist␈αwe␈αcan␈αidentify␈αa␈αsingle␈αinstruction␈αwith
␈↓ ↓H␈↓a program that has just one instruction.

␈↓ ↓H␈↓        The synthetic and analytic syntaxes of instructions are related by the following.

␈↓ ↓H␈↓↓␈↓ βxisli(mkli(␈↓πa␈↓↓))
␈↓ ↓H␈↓↓␈↓ βxarg(mkli(␈↓πa␈↓↓)) = ␈↓πa␈↓↓
␈↓ ↓H␈↓↓4.1)␈↓ βxisli(s) ⊃ s = mkli(arg(s))
␈↓ ↓H␈↓↓␈↓ βxnull(rest(mkli(␈↓πa␈↓↓)))
␈↓ ↓H␈↓↓␈↓ βxisli(s) ⊃ first(s) = s

␈↓ ↓H␈↓↓␈↓ βxisload(mkload(x))
␈↓ ↓H␈↓↓␈↓ βxx = adr(mkload(x))
␈↓ ↓H␈↓↓4.2)␈↓ βxisload(x) ⊃ x = mkload(adr(x))
␈↓ ↓H␈↓↓␈↓ βxnull(rest(mkload(x)))
␈↓ ↓H␈↓↓␈↓ βxisload(s) ⊃ first(s) = s
␈↓ ↓H␈↓158␈↓ ¬lChapter  VIII␈↓ H



␈↓ ↓H␈↓↓␈↓ βxissto(mksto(x))
␈↓ ↓H␈↓↓␈↓ βxx = adr(mksto(x))
␈↓ ↓H␈↓↓4.3)␈↓ βxissto(s) ⊃ s = mksto(adr(s))
␈↓ ↓H␈↓↓␈↓ βxnull(rest(mksto(x)))
␈↓ ↓H␈↓↓␈↓ βxissto(s) ⊃ first(s) = s

␈↓ ↓H␈↓↓␈↓ βxisadd(mkadd(x))
␈↓ ↓H␈↓↓␈↓ βxx = adr(mkadd(x))
␈↓ ↓H␈↓↓4.4)␈↓ βxisadd(s) ⊃ s = mkadd(adr(s))
␈↓ ↓H␈↓↓␈↓ βxnull(rest(mkadd(x)))
␈↓ ↓H␈↓↓␈↓ βxisadd(s) ⊃ first(s) = s

␈↓ ↓H␈↓↓␈↓ βx¬null(p) ⊃ p = first(p) * rest(p),
␈↓ ↓H␈↓↓4.5)␈↓ βx¬null(p1) ∧ null(rest(p1)) ⊃ p1 = first(p1*p2)
␈↓ ↓H␈↓↓␈↓ βxnull(p1*p2) ≡ null(p1) ∧ null(p2).

␈↓ ↓H␈↓The␈α⊃*␈α⊃operation␈α⊃is␈α⊃associative.␈α⊃(The␈α⊃somewhat␈α⊃awkward␈α⊃form␈α⊃of␈α⊃these␈α⊃relations␈α⊃comes␈α⊃from
␈↓ ↓H␈↓having␈α⊂a␈α∂general␈α⊂concatenation␈α∂operation␈α⊂rather␈α∂than␈α⊂just␈α∂an␈α⊂operation␈α∂that␈α⊂prefixes␈α⊂a␈α∂single
␈↓ ↓H␈↓instruction onto a program.)

␈↓ ↓H␈↓        A␈α⊂state␈α⊃vector␈α⊂for␈α⊃a␈α⊂machine␈α⊃gives,␈α⊂for␈α⊃each␈α⊂register␈α⊃in␈α⊂the␈α⊃machine,␈α⊂its␈α⊃contents.␈α⊂ We
␈↓ ↓H␈↓include␈αthe␈αaccumulator␈αdenoted␈αby␈α␈↓↓ac␈↓␈αas␈αa␈αregister.␈αAssociatedwith␈αstate␈αvectors␈αare␈αthe␈αfunctions
␈↓ ↓H␈↓␈↓↓a␈↓ (for assign) and ␈↓↓c␈↓ (for contents).   Thus

␈↓ ↓H␈↓        1.  ␈↓↓c(x,␈↓πh␈↓↓)␈↓ denotes the value of the contents of register ␈↓↓x␈↓ in machine state ␈↓πh␈↓.

␈↓ ↓H␈↓        2.␈α
 ␈↓↓a(x,␈↓πa␈↓↓,␈↓πh␈↓↓)␈↓␈α
denotes␈α∞the␈α
state␈α
vector␈α
that␈α∞is␈α
obtained␈α
from␈α
the␈α∞state␈α
vector␈α
␈↓πh␈↓␈α∞by␈α
changing
␈↓ ↓H␈↓the contents of register ␈↓↓x␈↓ to ␈↓πa␈↓ leaving the other registers unaffected.

␈↓ ↓H␈↓The relations satisfied by these functions are:

␈↓ ↓H␈↓␈↓ βx␈↓↓     c(x,a(y,␈↓πa␈↓↓,␈↓πh␈↓↓)) = ␈↓αif␈↓↓ x = y ␈↓αthen␈↓↓ ␈↓πa␈↓↓ ␈↓αelse␈↓↓ c(x,␈↓πh␈↓↓),␈↓
␈↓ ↓H␈↓4.6)␈↓ βx␈↓↓a(x,␈↓πa␈↓↓,a(y,␈↓πa␈↓↓,␈↓πh␈↓↓)) = ␈↓αif␈↓↓ x = y ␈↓αthen␈↓↓ a(x,␈↓πa␈↓↓,␈↓πh␈↓↓) ␈↓αelse␈↓↓ a(y,␈↓πa␈↓↓,a(x,␈↓πa␈↓↓,␈↓πh␈↓↓)),␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓      a(x,c(x,␈↓πh␈↓↓),␈↓πh␈↓↓) = ␈↓πh␈↓↓.␈↓

␈↓ ↓H␈↓[We␈α⊃note␈α⊃that␈α⊃the␈α⊃first␈α⊃argument␈α∩to␈α⊃the␈α⊃␈↓↓c␈↓␈α⊃and␈α⊃␈↓↓a␈↓␈α⊃functions␈α∩is␈α⊃a␈α⊃variable␈α⊃in␈α⊃the␈α⊃case␈α∩of␈α⊃the
␈↓ ↓H␈↓expression␈α
state␈α
vector␈α
while␈α
in␈α
the␈α
case␈α
of␈α
the␈α
machine␈α
state␈α
vector␈α
it␈α
is␈α
a␈α
register.␈α
 This␈α
causes␈α
no
␈↓ ↓H␈↓difficulty,␈α⊃the␈α∩functions␈α⊃satisfy␈α∩the␈α⊃same␈α∩relations␈α⊃in␈α∩both␈α⊃cases␈α∩and␈α⊃can␈α∩just␈α⊃be␈α∩thought␈α⊃as
␈↓ ↓H␈↓polymorphic.]

␈↓ ↓H␈↓        Now we can define the semantics of the object language by

␈↓ ↓H␈↓↓␈↓ βxstep(x,␈↓πh␈↓↓) ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αif␈↓↓ isli(s) ␈↓αthen␈↓↓ a(ac,arg(s),␈↓πh␈↓↓)
␈↓ ↓H␈↓↓4.7)␈↓ ∧8␈↓αelse␈↓↓ ␈↓αif␈↓↓ isload(s) ␈↓αthen␈↓↓ a(ac,c(adr(s),␈↓πh␈↓↓),␈↓πh␈↓↓)
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ ␈↓αif␈↓↓ issto(s) ␈↓αthen␈↓↓ a(adr(s),c(ac,␈↓πh␈↓↓),␈↓πh␈↓↓)
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ ␈↓αif␈↓↓ isadd(s) ␈↓αthen␈↓↓ a(ac,c(adr(s),␈↓πh␈↓↓) + c(ac,␈↓πh␈↓↓),␈↓πh␈↓↓)

␈↓ ↓H␈↓which gives the state vector that results from executing an instruction and
␈↓ ↓H␈↓␈↓ ¬lChapter  VIII␈↓ ≠159


␈↓ ↓H␈↓4.8) ␈↓ β
␈↓↓outcome(p,␈↓πh␈↓↓) ← ␈↓αif␈↓↓ null(p) ␈↓αthen␈↓↓ ␈↓πh␈↓↓ ␈↓αelse␈↓↓ outcome(rest(p),step(first(p),␈↓πh␈↓↓))␈↓ 

␈↓ ↓H␈↓which gives the state vector that results from executing the program ␈↓↓p␈↓ with state vector ␈↓πh␈↓.

␈↓ ↓H␈↓        The following lemma is left as an exercise for the reader.

␈↓ ↓H␈↓4.9)␈↓ ∧_␈↓↓outcome(p1*p2,␈↓πh␈↓↓) = outcome(p2,outcome(p1,␈↓πh␈↓↓))␈↓ 



␈↓ ↓H␈↓5.  ␈↓αThe compiler.␈↓


␈↓ ↓H␈↓        In␈α⊂order␈α⊃to␈α⊂compile␈α⊃an␈α⊂expression␈α⊂we␈α⊃need␈α⊂to␈α⊃know␈α⊂where␈α⊂the␈α⊃values␈α⊂of␈α⊃the␈α⊂variables
␈↓ ↓H␈↓occuring␈α⊃in␈α⊃the␈α⊂expression␈α⊃will␈α⊃be␈α⊃stored.␈α⊂ We␈α⊃shall␈α⊃assume␈α⊃that␈α⊂the␈α⊃function␈α⊃␈↓↓loc␈↓␈α⊃maps␈α⊂each
␈↓ ↓H␈↓variable␈α∩to␈α∩the␈α⊃register␈α∩where␈α∩it␈α⊃will␈α∩initially␈α∩be␈α⊃stored␈α∩in␈α∩the␈α⊃machine␈α∩state␈α∩vector.␈α∩ If␈α⊃the
␈↓ ↓H␈↓comparison␈αof␈αthe␈αinterpretation␈αof␈αan␈αexpression␈αand␈αthe␈αexecution␈αof␈αits␈αcompiled␈αcode␈αis␈αto␈αbe
␈↓ ↓H␈↓meaningful,␈αthe␈αinitial␈αstate␈αvectors␈αmust␈αagree␈αon␈αthe␈αvariables␈αoccuring␈αin␈αthe␈αexpression.␈α
 Thus
␈↓ ↓H␈↓we will assume that the relation

␈↓ ↓H␈↓5.1)␈↓ ¬R␈↓↓c(loc(v),␈↓πh␈↓↓) = c(v,␈↓πx␈↓↓)␈↓ 

␈↓ ↓H␈↓between␈αthe␈αstate␈αvector␈α␈↓πh␈↓␈αbefore␈αthe␈αcompiled␈αprogram␈αstarts␈αto␈αact␈αand␈αthe␈αstate␈αvector␈α␈↓πx␈↓␈αof␈αthe
␈↓ ↓H␈↓source program holds.

␈↓ ↓H␈↓        Now we can write the compiler.  It is

␈↓ ↓H␈↓↓␈↓ α8compile(e,t) ←
␈↓ ↓H␈↓↓␈↓ αx␈↓αif␈↓↓ isconst(e) ␈↓αthen␈↓↓ mkli(val(e))
␈↓ ↓H␈↓↓5.2)␈↓ αx␈↓αelse␈↓↓ ␈↓αif␈↓↓ isvar(e) ␈↓αthen␈↓↓ mkload(loc(e))
␈↓ ↓H␈↓↓␈↓ αx␈↓αelse␈↓↓ ␈↓αif␈↓↓ issum(e) ␈↓αthen␈↓↓ compile(s1(e),t) * mksto(t) * compile(s2(e),t + 1) * mkadd(t)


␈↓ ↓H␈↓Here␈α∞␈↓↓t␈↓␈α∞is␈α∞the␈α
number␈α∞of␈α∞a␈α∞register␈α
such␈α∞that␈α∞all␈α∞variables␈α
are␈α∞stored␈α∞in␈α∞registers␈α∞numbered␈α
less
␈↓ ↓H␈↓than ␈↓↓t,␈↓ so that registers ␈↓↓t␈↓ and above are available for temporary storage.

␈↓ ↓H␈↓        Before␈α
we␈α
can␈α
state␈α
our␈α
definition␈α
of␈αcorrectness␈α
of␈α
the␈α
compiler,␈α
we␈α
need␈α
a␈α
notion␈αof␈α
partial
␈↓ ↓H␈↓equality for state vectors

␈↓ ↓H␈↓␈↓ ε∪␈↓↓␈↓πz␈↓↓␈↓β1␈↓↓ =␈↓βA␈↓↓ ␈↓πz␈↓↓␈↓β2␈↓↓␈↓ 

␈↓ ↓H␈↓where␈α∀␈↓πz␈↓␈↓β1␈↓␈α∀and␈α∀␈↓πz␈↓␈↓β2␈↓␈α∀are␈α∃state␈α∀vectors␈α∀and␈α∀␈↓¬A␈α∀␈↓is␈α∃a␈α∀set␈α∀of␈α∀variables␈α∀means␈α∃that␈α∀corresponding
␈↓ ↓H␈↓components␈α
of␈α
␈↓πz␈↓␈↓β1␈↓␈α∞and␈α
␈↓πz␈↓␈↓β2␈↓␈α
are␈α∞equal␈α
except␈α
possibly␈α∞for␈α
values,␈α
of␈α∞variables␈α
in␈α
␈↓¬A.␈α∞␈↓␈α
Symbolically,
␈↓ ↓H␈↓␈↓↓x ␈↓ππ␈↓↓ ␈↓¬A␈↓↓ ⊃ c(x,␈↓πz␈↓↓␈↓β1␈↓↓)=c(x,␈↓πz␈↓↓␈↓β2␈↓↓)␈↓.  Partial equality satisfies the following relations:
␈↓ ↓H␈↓160␈↓ ¬lChapter  VIII␈↓ H



␈↓ ↓H␈↓5.3)␈↓ β8␈↓πz␈↓␈↓β1␈↓ = ␈↓πz␈↓␈↓β2␈↓ is equivalent to ␈↓πz␈↓␈↓β1␈↓ =␈↓β{ }␈↓ ␈↓πz␈↓␈↓β2␈↓ where { } denotes the empty set,
␈↓ ↓H␈↓5.4)␈↓ β8if ␈↓¬A ␈↓⊂ ␈↓¬B ␈↓and ␈↓πz␈↓␈↓β1␈↓ =␈↓βA␈↓ ␈↓πz␈↓␈↓β2␈↓ then ␈↓πz␈↓␈↓β1␈↓ =␈↓βB␈↓ ␈↓πz␈↓␈↓β2␈↓,
␈↓ ↓H␈↓5.5)␈↓ β8if ␈↓πz␈↓␈↓β1␈↓ =␈↓βA␈↓ ␈↓πz␈↓␈↓β2␈↓, then ␈↓↓a(x,␈↓πa␈↓↓,␈↓πz␈↓↓␈↓β1␈↓↓) =␈↓βA-{x}␈↓↓ a(x,␈↓πa␈↓↓,␈↓πz␈↓↓␈↓β2␈↓↓)␈↓,
␈↓ ↓H␈↓5.6)␈↓ β8if ␈↓↓x␈↓ ε ␈↓¬A ␈↓then ␈↓↓a(x,␈↓πa␈↓↓,␈↓πz␈↓↓) =␈↓βA␈↓↓ ␈↓πz␈↓↓␈↓,
␈↓ ↓H␈↓5.7)␈↓ β8if ␈↓πz␈↓␈↓β1␈↓ =␈↓βA␈↓ ␈↓πz␈↓␈↓β2␈↓ and ␈↓πz␈↓␈↓β2␈↓ =␈↓βB␈↓ ␈↓πz␈↓␈↓β3␈↓, then ␈↓πz␈↓␈↓β1␈↓ =␈↓βA∪B␈↓ ␈↓πz␈↓␈↓β3␈↓.


␈↓ ↓H␈↓In our case we need a specialization of this notation and will use

␈↓ ↓H␈↓␈↓ β8␈↓πz␈↓␈↓β1␈↓ =␈↓βt␈↓ ␈↓πz␈↓␈↓β2␈↓ to denote ␈↓πz␈↓␈↓β1␈↓ =␈↓β{x|x≥t}␈↓ ␈↓πz␈↓␈↓β2␈↓
␈↓ ↓H␈↓␈↓ β8␈↓πz␈↓␈↓β1␈↓ =␈↓βac␈↓ ␈↓πz␈↓␈↓β2␈↓ to denote ␈↓πz␈↓␈↓β1␈↓ =␈↓β{ac}␈↓ ␈↓πz␈↓␈↓β2␈↓   and
␈↓ ↓H␈↓␈↓ β8␈↓πz␈↓␈↓β1␈↓ =␈↓βt,ac␈↓ ␈↓πz␈↓␈↓β2␈↓ to denote a␈↓πz␈↓␈↓β1␈↓ =␈↓β{x|x = ac ∨ x ≥ t}␈↓ ␈↓πz␈↓␈↓β2␈↓.

␈↓ ↓H␈↓        The correctness of the compiler is stated in

␈↓ ↓H␈↓THEOREM␈α
1.␈α If␈α
␈↓πh␈↓␈α
and␈α␈↓πx␈↓␈α
are␈αmachine␈α
and␈α
source␈αlanguage␈α
state␈αvectors␈α
respectively␈α
such␈αthat
␈↓ ↓H␈↓(5.1) holds then

␈↓ ↓H␈↓␈↓ ∧(␈↓↓outcome(compile(e,t),␈↓πh␈↓↓) =␈↓βt␈↓↓ a(ac,value(e,␈↓πx␈↓↓),␈↓πh␈↓↓)␈↓ 

␈↓ ↓H␈↓It␈α∞states␈α∞that␈α∞the␈α∞result␈α∞of␈α∞running␈α∞the␈α∞compiled␈α∞program␈α∞is␈α∞to␈α∞put␈α∞the␈α∞value␈α∞of␈α∞the␈α∞expression
␈↓ ↓H␈↓compiled␈αinto␈αthe␈αaccumulator.␈α No␈αregisters␈αexcept␈αthe␈αaccumulator␈αand␈αthose␈αwith␈αaddresses␈α≥␈α␈↓↓t␈↓
␈↓ ↓H␈↓are affected.



␈↓ ↓H␈↓6.  ␈↓αProof of Correctness Theorem.␈↓


␈↓ ↓H␈↓        The␈α
proof␈α
is␈α∞accomplished␈α
by␈α
an␈α∞induction␈α
on␈α
the␈α
expression␈α∞␈↓↓e␈↓␈α
being␈α
compiled␈α∞using␈α
the
␈↓ ↓H␈↓principle␈α
given␈α
in␈α
␈↓π∞␈↓3.␈α
 We␈α
prove␈α
it␈α
first␈αfor␈α
constants,␈α
then␈α
for␈α
variables,␈α
and␈α
then␈α
for␈α
sums␈αon␈α
the
␈↓ ↓H␈↓induction hypothesis that it is true for the summands.  Thus there are three cases.

␈↓ ↓H␈↓↓␈↓ βX␈↓αStatement␈↓ λxJustification␈↓↓

␈↓ ↓H␈↓↓␈↓I.␈↓↓  isconst(e).

␈↓ ↓H␈↓↓␈↓ ↓xoutcome(compile(e,t),␈↓πh␈↓↓)␈↓ ∧H= outcome(mkli(val(e)),␈↓πh␈↓↓)␈↓ 	_5.2
␈↓ ↓H␈↓↓␈↓ ∧H= step(mkli(val(e)),␈↓πh␈↓↓)␈↓ 	_4.8, 4.1
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,arg(mkli(val(e))),␈↓πh␈↓↓)␈↓ 	_4.1, 4.7
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,val(e),␈↓πh␈↓↓)␈↓ 	_4.1
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,value(e,␈↓πx␈↓↓),␈↓πh␈↓↓)␈↓ 	_1.1
␈↓ ↓H␈↓↓␈↓ ∧H=␈↓βt␈↓↓ a(ac,value(e,␈↓πx␈↓↓),␈↓πh␈↓↓).␈↓ 	_5.3, 5.4


␈↓ ↓H␈↓↓␈↓II.␈↓↓  isvar(e).
␈↓ ↓H␈↓␈↓ ¬lChapter  VIII␈↓ ≠161


␈↓ ↓H␈↓↓␈↓ ↓xoutcome(compile(e,t),␈↓πh␈↓↓)␈↓ ∧H= outcome(mkload(loc(e)),␈↓πh␈↓↓)␈↓ 	_5.2
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,c(adr(mkload(loc(e))),␈↓πh␈↓↓,␈↓πh␈↓↓)␈↓ 	_4.8, 4.2, 4.7
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,c(loc(e),␈↓πh␈↓↓),␈↓πh␈↓↓)␈↓ 	_4.2
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,c(e,␈↓πx␈↓↓),␈↓πh␈↓↓)␈↓ 	_5.1
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,value(e,␈↓πx␈↓↓),␈↓πh␈↓↓)␈↓ 	_1.1
␈↓ ↓H␈↓↓␈↓ ∧H=␈↓βt␈↓↓ a(ac,value(e,␈↓πx␈↓↓),␈↓πh␈↓↓).␈↓ 	_5.3, 5.4


␈↓ ↓H␈↓↓␈↓III.␈↓↓  issum(e).

␈↓ ↓H␈↓↓␈↓ ↓xoutcome(compile(e,t),␈↓πh␈↓↓)␈↓ ∧H= outcome(compile(s1(e),t) * mksto(t)␈↓ 	_5.2
␈↓ ↓H␈↓↓␈↓ ¬λ* compile(s2(e),t + 1) * mkadd(t),␈↓πh␈↓↓)
␈↓ ↓H␈↓↓6.1)␈↓ ∧H= outcome(mkadd(t),␈↓ 	_4.9
␈↓ ↓H␈↓↓␈↓ ¬λoutcome(compile(s2(e),t + 1),
␈↓ ↓H␈↓↓␈↓ ¬Houtcome(mksto(t),
␈↓ ↓H␈↓↓␈↓ ελoutcome(compile(s1(e),t),␈↓πh␈↓↓)))

␈↓ ↓H␈↓using␈α∞the␈α∞relation␈α∞between␈α∞concatenating␈α∞programs␈α∞and␈α∞composing␈α∞the␈α∞functions␈α∂they␈α∞represent.
␈↓ ↓H␈↓Now we introduce some notation.  Let

␈↓ ↓H␈↓↓␈↓ ∧(v␈↓ ∧H= value(e,␈↓πx␈↓↓),
␈↓ ↓H␈↓↓6.2)␈↓ ∧(v␈↓β1␈↓↓␈↓ ∧H= value(s1(e),␈↓πx␈↓↓),
␈↓ ↓H␈↓↓␈↓ ∧(v␈↓β2␈↓↓␈↓ ∧H= value(s2(e),␈↓πx␈↓↓),

␈↓ ↓H␈↓so that ␈↓↓v = v␈↓β1␈↓↓ + v␈↓β2␈↓↓␈↓.  Further let

␈↓ ↓H␈↓↓␈↓ ∧(␈↓πz␈↓↓␈↓β1␈↓↓␈↓ ∧H= outcome(compile(s1(e),t),␈↓πh␈↓↓),
␈↓ ↓H␈↓↓6.3)␈↓ ∧(␈↓πz␈↓↓␈↓β2␈↓↓␈↓ ∧H= outcome(mksto(t),␈↓πz␈↓↓␈↓β1␈↓↓),
␈↓ ↓H␈↓↓␈↓ ∧(␈↓πz␈↓↓␈↓β3␈↓↓␈↓ ∧H= outcome(compile(s2(e),t + 1),␈↓πz␈↓↓␈↓β2␈↓↓),
␈↓ ↓H␈↓↓␈↓ ∧(␈↓πz␈↓↓␈↓β4␈↓↓␈↓ ∧H= outcome(mkadd(t),␈↓πz␈↓↓␈↓β3␈↓↓)

␈↓ ↓H␈↓so that by (6.1) ␈↓↓␈↓πz␈↓↓␈↓β4␈↓↓ = outcome(compile(e,t),␈↓πh␈↓↓)␈↓, and the statement to be proved becomes

␈↓ ↓H␈↓↓␈↓ ∧(␈↓πz␈↓↓␈↓β4␈↓↓␈↓ ∧H=␈↓βt␈↓↓ a(ac,v,␈↓πh␈↓↓).

␈↓ ↓H␈↓In␈αorder␈αto␈αapply␈αthe␈αinduction␈αhypothesis␈αto␈αshow␈αthat␈α␈↓↓s2(e)␈↓␈αcompiles␈αcorrectly␈αwe␈αneed␈αto␈αknow
␈↓ ↓H␈↓that for each variable ␈↓↓v␈↓ the following equation holds:

␈↓ ↓H␈↓↓6.4)␈↓ β(c(loc(v),␈↓πz␈↓↓␈↓β2␈↓↓)␈↓ ∧H= c(v,␈↓πx␈↓↓)

␈↓ ↓H␈↓This is proved as follows:

␈↓ ↓H␈↓↓␈↓ β(c(loc(v),␈↓πz␈↓↓␈↓β2␈↓↓)␈↓ ∧H= c(loc(v),a(t,v␈↓β1␈↓↓,␈↓πh␈↓↓))␈↓ 	_␈↓since␈↓↓ loc(v) < t
␈↓ ↓H␈↓↓␈↓ ∧H= c(loc(v),␈↓πh␈↓↓)␈↓ 	_␈↓for the same reason␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧H= c(v,␈↓πx␈↓↓)␈↓ 	_5.1

␈↓ ↓H␈↓thus the induction hypothesis together with (5.1) and (6.4) and the definitions (6.3) give
␈↓ ↓H␈↓162␈↓ ¬lChapter  VIII␈↓ H



␈↓ ↓H␈↓↓6.5)␈↓ ∧(␈↓πz␈↓↓␈↓β1␈↓↓␈↓ ∧H= ␈↓βt␈↓↓ a(ac,v␈↓β1␈↓↓,␈↓πh␈↓↓)
␈↓ ↓H␈↓↓6.6)␈↓ ∧(␈↓πz␈↓↓␈↓β3␈↓↓␈↓ ∧H= ␈↓βt+1␈↓↓ a(ac,v␈↓β2␈↓↓,␈↓πz␈↓↓␈↓β2␈↓↓)

␈↓ ↓H␈↓Now we resume the main proof

␈↓ ↓H␈↓↓6.7)␈↓ βXc(ac,␈↓πz␈↓↓␈↓β1␈↓↓)␈↓ ∧H= v␈↓β1␈↓↓␈↓ 	_4.6, 6.5

␈↓ ↓H␈↓↓␈↓ ∧(␈↓πz␈↓↓␈↓β2␈↓↓␈↓ ∧H= outcome(mksto(t),␈↓πz␈↓↓␈↓β1␈↓↓)␈↓ 	_6.3
␈↓ ↓H␈↓↓␈↓ ∧H= a(t,c(ac,␈↓πz␈↓↓␈↓β1␈↓↓),␈↓πz␈↓↓␈↓β1␈↓↓)␈↓ 	_4.8, 4.3, 4.7
␈↓ ↓H␈↓↓␈↓ ∧H= a(t,v␈↓β1␈↓↓,␈↓πz␈↓↓␈↓β1␈↓↓)␈↓ 	_6.7
␈↓ ↓H␈↓↓␈↓ ∧H=␈↓βt+1␈↓↓ a(t,v␈↓β1␈↓↓,a(ac,v␈↓β1␈↓↓,␈↓πh␈↓↓))␈↓ 	_5.5, 6.5
␈↓ ↓H␈↓↓6.8)␈↓ ∧H=␈↓βt+1,ac␈↓↓ a(t,v␈↓β1␈↓↓,␈↓πh␈↓↓)␈↓ 	_5.6, 4.6

␈↓ ↓H␈↓↓6.9)␈↓ ∧(␈↓πz␈↓↓␈↓β3␈↓↓␈↓ ∧H= ␈↓βt+1␈↓↓ a(ac,v␈↓β2␈↓↓,a(t,v␈↓β1␈↓↓,␈↓πh␈↓↓))␈↓ 	_6.6, 4.6, 5.5, 6.8

␈↓ ↓H␈↓↓6.10)␈↓ βhc(ac,␈↓πz␈↓↓␈↓β3␈↓↓)␈↓ ∧H= v␈↓β2␈↓↓  ␈↓and␈↓↓ c(t,␈↓πz␈↓↓␈↓β3␈↓↓) = v␈↓β1␈↓↓␈↓ 	_4.6, 6.9

␈↓ ↓H␈↓↓␈↓ ∧(␈↓πz␈↓↓␈↓β4␈↓↓␈↓ ∧H= outcome(mkadd(t),␈↓πz␈↓↓␈↓β3␈↓↓)␈↓ 	_6.3
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,c(t,␈↓πz␈↓↓␈↓β3␈↓↓) + c(ac,␈↓πz␈↓↓␈↓β3␈↓↓),␈↓πz␈↓↓␈↓β3␈↓↓)␈↓ 	_4.8, 4.4, 4.7
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,v,␈↓πz␈↓↓␈↓β3␈↓↓)␈↓ 	_6.2, 6.10, 1.1
␈↓ ↓H␈↓↓␈↓ ∧H=␈↓βt+1␈↓↓ a(ac,v,a(ac,v␈↓β2␈↓↓,a(t,v␈↓β1␈↓↓,␈↓πh␈↓↓)))␈↓ 	_5.5, 6.9
␈↓ ↓H␈↓↓␈↓ ∧H=␈↓βt+1␈↓↓ a(ac,v,a(t,v␈↓β1␈↓↓,␈↓πh␈↓↓))␈↓ 	_4.6
␈↓ ↓H␈↓↓␈↓ ∧H=␈↓βt␈↓↓ a(ac,v,␈↓πh␈↓↓).␈↓ 	_4.6, 5.6, 5.7

␈↓ ↓H␈↓This concludes the proof.



␈↓ ↓H␈↓7.  ␈↓αRemarks.␈↓


␈↓ ↓H␈↓        The␈α∩definition␈α∩of␈α∪correctness,␈α∩the␈α∩formalism␈α∩used␈α∪to␈α∩express␈α∩the␈α∩description␈α∪of␈α∩source
␈↓ ↓H␈↓language,␈α∞object␈α∞language␈α∞and␈α∞compiler,␈α∞and␈α∞the␈α∞methods␈α∞of␈α∞proof␈α∞are␈α∞all␈α∞intended␈α∞to␈α∂serve␈α∞as
␈↓ ↓H␈↓prototypes␈α∂for␈α⊂the␈α∂more␈α⊂complicated␈α∂task␈α⊂of␈α∂proving␈α⊂the␈α∂correctness␈α⊂of␈α∂usable␈α⊂compilers.␈α∂ The
␈↓ ↓H␈↓ultimate␈α∂goal,␈α∂as␈α∂outlined␈α∂in␈α∂McCarthy␈α∂[1962a,1962b,1963,1964]␈α∞is␈α∂to␈α∂make␈α∂it␈α∂possible␈α∂to␈α∂use␈α∞a
␈↓ ↓H␈↓computer␈αto␈αcheck␈αproofs␈αthat␈αcompilers␈αare␈αcorrect.␈α The␈αconcepts␈αof␈αabstract␈αsyntax,␈αstate␈αvector,
␈↓ ↓H␈↓the␈α∪use␈α∀of␈α∪an␈α∀interpreter␈α∪for␈α∀defining␈α∪the␈α∪semantics␈α∀of␈α∪a␈α∀programming␈α∪language,␈α∀and␈α∪the
␈↓ ↓H␈↓definition␈α∂of␈α∂correctness␈α⊂of␈α∂a␈α∂compiler␈α∂were␈α⊂introduced␈α∂in␈α∂[McCarthy␈α∂1962b].␈α⊂ [McCarthy␈α∂and
␈↓ ↓H␈↓Painter 1967], however, was the first in which the correctness of a compiler was proved.

␈↓ ↓H␈↓        The␈α∂problem␈α∂of␈α∞the␈α∂relations␈α∂between␈α∞source␈α∂language␈α∂and␈α∞object␈α∂language␈α∂arithmetic␈α∞is
␈↓ ↓H␈↓dealt␈αwith␈αhere␈αby␈αassuming␈αthat␈αthe␈α+␈αsigns␈αin␈αthe␈αdefinitions␈αof␈α␈↓↓value␈↓␈α(1.1)␈αand␈α␈↓↓step␈↓␈α(4.7)␈αwhich
␈↓ ↓H␈↓define␈αthe␈αsemantics␈α
of␈αthe␈αsource␈αand␈α
object␈αlanguages␈αrepresent␈α
the␈αsame␈αoperation.␈α Theorem␈α
1
␈↓ ↓H␈↓does not depend on any properties of this operation, not even commutativity or associativity.
␈↓ ↓H␈↓␈↓ ¬lChapter  VIII␈↓ ≠163


␈↓ ↓H␈↓        The␈α∪proof␈α∪is␈α∪entirely␈α∪straightforward␈α∪once␈α∪the␈α∪necessary␈α∪machinery␈α∪has␈α∀been␈α∪created.
␈↓ ↓H␈↓Additional␈α∂operations␈α∂such␈α∂as␈α∂subtraction,␈α⊂multiplication␈α∂and␈α∂division␈α∂could␈α∂be␈α⊂added␈α∂without
␈↓ ↓H␈↓essential␈α∞change␈α∞in␈α∞the␈α∞proof.␈α∞ For␈α∂example,␈α∞to␈α∞put␈α∞multiplication␈α∞into␈α∞the␈α∞system␈α∂the␈α∞following
␈↓ ↓H␈↓changes would be required.

␈↓ ↓H␈↓1.␈α∩ Use␈α⊃the␈α∩analytic␈α⊃syntax,␈α∩semantics,␈α⊃and␈α∩induction␈α⊃principle␈α∩given␈α⊃in␈α∩␈↓π∞␈↓1␈α⊃for␈α∩the␈α⊃language
␈↓ ↓H␈↓including products.

␈↓ ↓H␈↓2.␈α Add␈αan␈αinstruction␈α␈↓↓mul␈↓␈α␈↓↓x␈↓␈αand␈αthe␈αthree␈αsyntactical␈αfunctions␈α␈↓↓ismul(s),␈↓␈α␈↓↓adr(s),␈↓␈α␈↓↓mkmul(x)␈↓␈αto␈αthe
␈↓ ↓H␈↓abstract syntax of the object language together with the necessary relations among them.

␈↓ ↓H␈↓3.  Add to the definition (4.7) of ␈↓↓step␈↓ the clause

␈↓ ↓H␈↓␈↓ ∧ε␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ismul(s) ␈↓αthen␈↓↓ a(ac,c(adr(s),␈↓πh␈↓↓) ␈↓π#␈↓↓ c(ac,␈↓πh␈↓↓),␈↓πh␈↓↓)␈↓. 

␈↓ ↓H␈↓4.  Add to the compiler (5.2) the clause

␈↓ ↓H␈↓␈↓ α4␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ isprod(e) ␈↓αthen␈↓↓ compile(p1(e),t) * mksto(t) * compile(p2(e),t + 1) * mkmul(t)␈↓. 

␈↓ ↓H␈↓5.  Add to the proof a case ␈↓↓isprod(e)␈↓ which parallels the case ␈↓↓issum(e)␈↓ exactly.

␈↓ ↓H␈↓Many extensions of this compiling problem are treated in Painter [1967].



␈↓ ↓H␈↓α␈↓ ε⊂Exercise

␈↓ ↓H␈↓1.␈α∂Modify␈α⊂the␈α∂arithmetic␈α∂expression␈α⊂language␈α∂to␈α⊂allow␈α∂variable␈α∂length␈α⊂sums.␈α∂ Describe␈α⊂how␈α∂to
␈↓ ↓H␈↓modify␈α
the␈αabstract␈α
syntax␈α
and␈αhow␈α
to␈α
fix␈αup␈α
the␈α
interpreter␈αand␈α
compiler,␈α
in␈αorder␈α
to␈α
give␈αthe
␈↓ ↓H␈↓semantics␈α
of␈α
the␈αnew␈α
language␈α
an␈α
compile␈αit.␈α
 Are␈α
any␈α
modifications␈αof␈α
the␈α
object␈αlanguage␈α
syntax
␈↓ ↓H␈↓or␈αsemantics␈αneeded.␈α How␈αcan␈αthe␈αproof␈αof␈αcorrectness␈αof␈αthe␈αoriginal␈αcompiler␈αbe␈αturned␈αinto␈αa
␈↓ ↓H␈↓proof of correctness of this new compiler?

␈↓ ↓H␈↓2.␈α∃What␈α∃problems␈α∃are␈α∃encountered␈α∃when␈α∃conditional␈α∃expressions␈α∃are␈α∃added␈α∃to␈α⊗the␈α∃source
␈↓ ↓H␈↓language?␈α In␈αwhat␈αway␈αis␈αthe␈αcurrent␈αobject␈αlanguage␈αinadequate␈αfor␈αthis␈αcase?␈α How␈αwould␈αyou
␈↓ ↓H␈↓solve this problem?



␈↓ ↓H␈↓8.  ␈↓αSome substantial exercises.␈↓


␈↓ ↓H␈↓        The␈α
following␈α
are␈α
some␈α
problems␈α
in␈α
programming␈α
and␈α
proving␈α
that␈α
can␈α
be␈α
solved␈α
using␈α
the
␈↓ ↓H␈↓techniques described in this chapter.

␈↓ ↓H␈↓1.␈α
  Consider␈αthe␈α
class␈α
of␈αboolean␈α
conditional␈α
expressions␈α(bces).␈α
 A␈αbce␈α
is␈α
either␈αa␈α
literal␈α
(␈↓↓lit)␈↓␈αor
␈↓ ↓H␈↓conditional␈α∞(␈↓↓if)␈↓␈α∞ composed␈α∞from␈α∞a␈α∞triple␈α∂of␈α∞bces␈α∞with␈α∞component␈α∞parts␈α∞␈↓↓premiss,␈↓␈α∂␈↓↓consequent␈↓␈α∞and
␈↓ ↓H␈↓␈↓↓alternate.␈↓␈α∞ Make␈α∂a␈α∞table␈α∞giving␈α∂the␈α∞abstract␈α∞syntax␈α∂of␈α∞bces.␈α∞ Define␈α∂an␈α∞evaluator␈α∞␈↓↓bval␈↓␈α∂for␈α∞bces
␈↓ ↓H␈↓given␈α∂that␈α∂␈↓↓lval␈↓␈α∂determines␈α∂the␈α∂truth␈α∂value␈α∂of␈α∂literals.␈α∂ Your␈α∂evaluator␈α∂should␈α∂satisfy␈α∂the␈α∂usual
␈↓ ↓H␈↓rules for conditional expressions as given in Chapter III (III.9.1) - (III.9.6).
␈↓ ↓H␈↓164␈↓ ¬lChapter  VIII␈↓ H


␈↓ ↓H␈↓        Work␈α
out␈αthe␈α
relations␈αbetween␈α
the␈αanalytic␈α
and␈αsynthetic␈α
syntaxes,␈αand␈α
give␈α
an␈αinduction
␈↓ ↓H␈↓principle for bces.

␈↓ ↓H␈↓        We␈α∂say␈α⊂that␈α∂a␈α⊂bce␈α∂is␈α∂in␈α⊂normal␈α∂form␈α⊂if␈α∂the␈α∂premiss␈α⊂of␈α∂every␈α⊂subexpression␈α∂is␈α⊂a␈α∂literal.
␈↓ ↓H␈↓Define a function that converts a bce to normal form.

␈↓ ↓H␈↓[Hint: Observe that in our external language the equivalence
␈↓ ↓H␈↓␈↓ αε␈↓↓␈↓αif␈↓↓ p ␈↓αthen␈↓↓ [␈↓αif␈↓↓ q ␈↓αthen␈↓↓ s ␈↓αelse␈↓↓ t] ␈↓αelse␈↓↓ [␈↓αif␈↓↓ r ␈↓αthen␈↓↓ s ␈↓αelse␈↓↓ t]  ≡ ␈↓αif␈↓↓ [␈↓αif␈↓↓ p ␈↓αthen␈↓↓ q ␈↓αelse␈↓↓ r] ␈↓αthen␈↓↓ s ␈↓αelse␈↓↓ t␈↓
␈↓ ↓H␈↓holds.]

␈↓ ↓H␈↓Prove␈αthat␈αyour␈αfunction␈αis␈αtotal␈αand␈αthat␈αthe␈αresult␈αis␈αin␈αnormal␈αform.␈α Proving␈αtotality␈αwill␈αtake
␈↓ ↓H␈↓some␈α∂thought␈α∂to␈α∞get␈α∂the␈α∂correct␈α∂induction␈α∞predicate.␈α∂ Proving␈α∂correctness,␈α∞e.g.␈α∂that␈α∂the␈α∂result␈α∞is
␈↓ ↓H␈↓normal,␈αwill␈αrequire␈αmaking␈αa␈αformal␈αstatement␈αof␈αthe␈αnotion␈αof␈αnormality.␈α This␈αcan␈αbe␈αdone␈αby
␈↓ ↓H␈↓defining␈α∃a␈α∃predicate␈α⊗␈↓↓normal␈↓␈α∃on␈α∃bces.␈α∃ Show␈α⊗that␈α∃the␈α∃normalizing␈α∃transformation␈α⊗is␈α∃value
␈↓ ↓H␈↓preserving.

␈↓ ↓H␈↓2.␈α⊃Write␈α⊃the␈α⊃syntax␈α⊃for␈α⊃a␈α⊃language␈α⊃in␈α⊃which␈α⊃programs␈α⊃are␈α⊃sequences␈α⊃of␈α⊃statements␈α⊂(possibly
␈↓ ↓H␈↓labeled),␈αwhere␈αa␈αstatement␈αis␈αeither␈αan␈αassignment␈αof␈αa␈αvariable␈αto␈αthe␈αvalue␈αof␈αan␈αexpression,␈αa
␈↓ ↓H␈↓conditional␈αbranch␈αwhere␈αthe␈αonly␈αtests␈αare␈α␈↓↓e=0␈↓␈αand␈α␈↓↓e>0,␈↓␈αor␈αa␈αreturn␈αstatement␈αwhich␈αreturns␈αthe
␈↓ ↓H␈↓value␈α∩of␈α∩an␈α∩expression.␈α∩ For␈α∩simplicity␈α∩assume␈α⊃expressions␈α∩are␈α∩of␈α∩the␈α∩form␈α∩described␈α∩in␈α⊃␈↓π∞␈↓3.
␈↓ ↓H␈↓Extend␈αthe␈αmachine␈α
language␈αof␈α␈↓π∞␈↓4␈α
to␈αallow␈αconditional␈αbranches.␈α
 Now␈αwrite␈αinterpreters␈α
and␈αa
␈↓ ↓H␈↓compiler␈α∪for␈α∪your␈α∪new␈α∪language␈α∪and␈α∪machine.␈α∪ Outline␈α∪a␈α∪proof␈α∪of␈α∪correctness.␈α∀ What␈α∪new
␈↓ ↓H␈↓difficulties are encountered when branches are allowed in the source language?
␈↓ ↓H␈↓␈↓ εH␈↓ ≠165


␈↓ ↓H␈↓α␈↓ ¬{Chapter IX

␈↓ ↓H␈↓α␈↓ ¬/COMPILING IN LISP


␈↓ ↓H␈↓        Compiling␈αis␈αan␈αimportant␈αexample␈αof␈αsymbolic␈αcomputation␈αand␈αhas␈αreceived␈αmuch␈αstudy.
␈↓ ↓H␈↓Much␈α
of␈α
the␈α
study␈α
has␈α
been␈α
devoted␈α
to␈α
parsing␈α
which␈α
is␈α
essentially␈α
the␈α
transformation␈α
of␈αan␈α
input
␈↓ ↓H␈↓string␈α⊂in␈α⊂the␈α∂source␈α⊂language␈α⊂into␈α∂an␈α⊂internal␈α⊂form.␈α∂ The␈α⊂internal␈α⊂form␈α∂used␈α⊂depends␈α⊂on␈α∂the
␈↓ ↓H␈↓compiler.␈α⊂ Sometimes␈α⊂it␈α⊂is␈α⊂Polish␈α⊂prefix␈α⊂or␈α∂postfix␈α⊂notation,␈α⊂sometimes␈α⊂it␈α⊂is␈α⊂list␈α⊂structure,␈α∂and
␈↓ ↓H␈↓sometimes it consists of entries in a collection of tables.

␈↓ ↓H␈↓        When␈αinternal␈αnotation␈αLISP␈αis␈αbeing␈αcompiled,␈αthe␈αparsing␈αis␈αtrivial,␈αbecause␈αthe␈αinput␈αis
␈↓ ↓H␈↓S-expressions␈α
and␈αthe␈α
internal␈α
form␈αwanted␈α
is␈α
list␈αstructure,␈α
so␈αwhat␈α
parsing␈α
there␈αis␈α
is␈α
done␈αby
␈↓ ↓H␈↓the␈α∞ordinary␈α∞LISP␈α∞␈↓↓read␈↓␈α∂routine.␈α∞ Therefore,␈α∞compilers␈α∞can␈α∂be␈α∞very␈α∞compact␈α∞and␈α∂transparent␈α∞in
␈↓ ↓H␈↓structure,␈αand␈α
we␈αcan␈α
concentrate␈αour␈α
attention␈αon␈αthe␈α
code␈αgeneration␈α
phase.␈α This␈α
is␈αas␈αit␈α
should
␈↓ ↓H␈↓be,␈α∞because,␈α∞parsing␈α∞is␈α∞basically␈α∞a␈α∞side␈α∞issue␈α
in␈α∞compiling,␈α∞and␈α∞code␈α∞generation␈α∞is␈α∞the␈α∞matter␈α
of
␈↓ ↓H␈↓main scientific interest.

␈↓ ↓H␈↓        We␈α
shall␈αdescribe␈α
two␈α
compilers␈αin␈α
this␈αchapter␈α
called␈α
LCOM0␈αand␈α
LCOM4␈αwhich␈α
compile
␈↓ ↓H␈↓S-expression␈αLISP␈αinto␈αmachine␈α
language␈αfor␈αthe␈αPDP-10␈α
computer␈αaccording␈αto␈αthe␈α
conventions
␈↓ ↓H␈↓of␈α⊃MACLISP.␈α⊃ For␈α⊃now␈α∩we␈α⊃shall␈α⊃take␈α⊃these␈α∩conventions␈α⊃for␈α⊃granted.␈α⊃ Before␈α∩describing␈α⊃the
␈↓ ↓H␈↓compilers, we must describe these conventions.



␈↓ ↓H␈↓1.  ␈↓αSome facts about the PDP-10.␈↓


␈↓ ↓H␈↓        The␈αtarget␈αlanguage␈αis␈αcalled␈αLAP␈αfor␈αLISP␈αassembly␈αprogram.␈α Each␈αfunction␈αis␈αcompiled
␈↓ ↓H␈↓separately␈αinto␈αa␈αseparate␈αLAP␈αprogram,␈αand␈αthese␈αprograms␈αare␈αwritten␈αonto␈αa␈αdisk␈αfile.␈α These
␈↓ ↓H␈↓files␈α∂can␈α∂later␈α∂be␈α∞read␈α∂into␈α∂a␈α∂LISP␈α∞core␈α∂image␈α∂and␈α∂assembled␈α∞in␈α∂place␈α∂by␈α∂the␈α∂LAP␈α∞assembler
␈↓ ↓H␈↓which␈αis␈αpart␈αof␈αthe␈αLISP␈αruntime␈αroutines.␈α
 The␈αcompiler,␈αon␈αthe␈αother␈αhand,␈αis␈αa␈αseparate␈α
LISP
␈↓ ↓H␈↓core␈αimage␈αthat␈αcan␈αbe␈αinstructed␈αto␈αcompile␈αseveral␈αinput␈αfiles.␈α For␈αan␈αinput␈αfile␈αcalled␈α<name>,
␈↓ ↓H␈↓it␈α∞produces␈α∞an␈α∞output␈α
file␈α∞called␈α∞<name>.LAP.␈α∞ All␈α∞this␈α
is␈α∞specific␈α∞to␈α∞the␈α∞PDP-10␈α
time-sharing
␈↓ ↓H␈↓system␈αand␈αfortunately␈αworks␈αthe␈αsame␈αwhether␈αthe␈αtime-sharing␈αsystem␈αis␈αthe␈αD.E.C.␈αsystem,␈αthe
␈↓ ↓H␈↓Stanford system, or TENEX.

␈↓ ↓H␈↓        The␈αLAP␈αprogram␈αproduced␈αis␈αa␈αlist␈αof␈αwords;␈αthe␈αlast␈αword␈αis␈α␈↓¬NIL␈↓,␈αand␈αthe␈αfirst␈αword␈αis␈αa
␈↓ ↓H␈↓header␈αof␈αthe␈αform␈α␈↓¬(LAP␈α␈↓↓fname␈↓¬␈αSUBR)␈↓␈αwhere␈α␈↓↓fname␈↓␈αis␈αthe␈αname␈αof␈αthe␈αfunction␈α
compiled.␈α This
␈↓ ↓H␈↓header␈αtells␈α
the␈αDSKIN␈α
program␈αthat␈α
it␈αshould␈αread␈α
S-expressions␈αtill␈α
it␈αcomes␈α
to␈α␈↓¬NIL␈↓␈α
and␈αthen
␈↓ ↓H␈↓submit␈αwhat␈α
it␈αhas␈α
collected␈αto␈α
the␈αLAP␈α
assembler␈αwhich␈α
will␈αassemble␈α
it␈αinto␈α
core.␈α The␈α
rest␈αof
␈↓ ↓H␈↓the words are either atoms representing labels or lists representing single PDP-10 instructions.

␈↓ ↓H␈↓        The following facts about the PDP-10 may be of use:

␈↓ ↓H␈↓        The␈αPDP-10␈αhas␈αa␈α36␈αbit␈αword␈αand␈α
an␈α18␈αbit␈αaddress.␈α In␈αinstructions␈αand␈αin␈α
accumulators
␈↓ ↓H␈↓used␈α∩as␈α⊃index␈α∩registers␈α⊃the␈α∩address␈α∩is␈α⊃found␈α∩in␈α⊃the␈α∩right␈α∩part␈α⊃of␈α∩the␈α⊃word␈α∩where␈α∩the␈α⊃least
␈↓ ↓H␈↓significant bits in arithmetic reside.

␈↓ ↓H␈↓        There␈α
are␈α
16␈α
general␈α
registers␈α
which␈α
serve␈α
simultaneously␈α
 as␈α
accumulators␈α
 (receiving␈αthe
␈↓ ↓H␈↓166␈↓ ¬wChapter  IX␈↓ H


␈↓ ↓H␈↓results␈αof␈αarithmetic␈αoperations),␈αindex␈αregisters␈α(modifying␈αthe␈αnominal␈αaddresses␈αof␈α instructions
␈↓ ↓H␈↓to␈α
 form␈α
effective␈α
addresses),␈α
and␈α
as␈α
the␈α
first␈α
16␈α
registers␈α
of␈α
memory␈α
(if␈α
the␈α
effective␈α
address␈α
of␈α
 an
␈↓ ↓H␈↓instruction␈α
 is␈α less␈α
 than␈α 16,␈α
 then␈α the␈α
instruction␈αuses␈α
the␈αcorresponding␈α
general␈αregister␈α
as␈αits
␈↓ ↓H␈↓operand.)

␈↓ ↓H␈↓        All␈α
instructions␈α
have␈α
the␈α
same␈α
format␈α
and␈α
are␈α
written␈α
for␈α
the␈α
LAP␈α
assembly␈α
program␈α
in␈α
the
␈↓ ↓H␈↓form

␈↓ ↓H␈↓        (<op name> <accumulator> <address> <index register>).

␈↓ ↓H␈↓Thus␈α ␈↓¬(MOVE␈α1␈α3␈αP)␈↓␈α causes␈αaccumulator  1␈α to␈αreceive␈αthe␈αcontents␈αof␈αa␈α memory␈α register␈αwhose
␈↓ ↓H␈↓address␈α
is  3+c(P),␈α
i.e.␈α
3+<the␈α
contents␈α
of␈α
general␈αregister␈α
P>.␈α
 <address>␈α
may␈α
be␈α
a␈α
number,␈αa␈α
label,
␈↓ ↓H␈↓or␈αan␈αS-expression␈αconstant␈αin␈αthe␈αform␈α␈↓¬(QUOTE␈α␈↓↓e␈↓¬)␈↓␈αwhere␈α␈↓↓e␈↓␈αis␈αan␈αS-expression.␈α The␈αlatter␈αallows
␈↓ ↓H␈↓a␈α*quoted␈α*S-expression␈α+to␈α*be␈α*loaded␈α+into␈α*accumulator  1␈α*by␈α+the␈α*instruction
␈↓ ↓H␈↓␈↓¬(MOVEI 1 (QUOTE ␈↓↓e␈↓¬))␈↓.␈α↔  An␈α_addtional␈α↔form␈α↔that␈α_appears␈α↔in␈α↔the␈α_address␈α↔field␈α_for␈α↔some
␈↓ ↓H␈↓instructions␈α
is␈α
␈↓¬(% 0 0 ␈↓↓m␈↓¬␈α␈↓↓m␈↓¬)␈↓␈α
.␈α
 This␈αis␈α
a␈α
literal␈α
with␈αvalue␈α
a␈α
word␈αcontaining␈α
␈↓↓m␈↓␈α
in␈α
the␈αlefthand
␈↓ ↓H␈↓side␈α⊃and␈α⊃␈↓↓n␈↓␈α⊃in␈α⊃the␈α⊃righthand␈α⊃side.␈α⊃ Which␈α⊃form␈α⊃of␈α⊃<address>␈α⊃is␈α⊃meaningful␈α⊃depends␈α⊃on␈α⊃the
␈↓ ↓H␈↓instruction.

␈↓ ↓H␈↓        To␈α
determine␈α
the␈αeffective␈α
address␈α
of␈αan␈α
instruction␈α
the␈αfollowing␈α
rules␈α
apply.␈α
 The␈αvalue
␈↓ ↓H␈↓of␈α<address>␈α
is␈αcombined␈αwith␈α
the␈αcontents␈αof␈α
the␈αindex␈αregister␈α
to␈αform␈αa␈α
direct␈αaddress.␈α
 If␈αno
␈↓ ↓H␈↓index␈αregister␈αis␈αspecified,␈αthen␈αjust␈αthe␈αvalue␈αof␈α<address>␈αis␈αused.␈α If␈αan␈α@␈αoccurs␈αin␈αthe␈αlist␈αas␈α
a
␈↓ ↓H␈↓separate␈αsymbol,␈α then␈αthe␈αmemory␈αreference␈αis␈αindirect,␈αi.e.␈αthe␈αeffective␈αaddress␈αis␈αthe␈αcontents␈α
of
␈↓ ↓H␈↓the␈α⊂right␈α⊂half␈α⊃of␈α⊂the␈α⊂word␈α⊃ directly␈α⊂ addressed␈α⊂ by␈α⊂ the␈α⊃ instruction␈α⊂(modified␈α⊂ by␈α⊃ the␈α⊂index
␈↓ ↓H␈↓register and indirect bit of that word).

␈↓ ↓H␈↓        In␈α∞the␈α∞following␈α∞description␈α
of␈α∞some␈α∞instructions␈α∞useful␈α
in␈α∞constructing␈α∞the␈α∞compiler,␈α
<ef>
␈↓ ↓H␈↓denotes the effective address of an instruction and ac denotes the accumulator.

␈↓ ↓H␈↓␈↓ αHMOVE ␈↓ ¬(c(ac) ← c(<ef>)
␈↓ ↓H␈↓␈↓ αHMOVEI ␈↓ ¬(c(ac) ← <ef>
␈↓ ↓H␈↓␈↓ αHMOVEM ␈↓ ¬(c(<ef>) ← c(ac)
␈↓ ↓H␈↓␈↓ αHHLRZ ␈↓ ¬(right half of c(ac) ← left half of c(<ef>)
␈↓ ↓H␈↓␈↓ αHHRRZ ␈↓ ¬(right half of c(ac) ← right half of c(<ef>)
␈↓ ↓H␈↓␈↓ αH␈↓ β8(These two are used indirectly for  car  and  cdr  respectively.)
␈↓ ↓H␈↓␈↓ αHADD ␈↓ ¬(c(ac) ← c(ac) + c(<ef>)
␈↓ ↓H␈↓␈↓ αHSUB ␈↓ ¬(c(ac) ← c(ac) - c(<ef>)
␈↓ ↓H␈↓␈↓ αHJRST ␈↓ ¬(go to <ef>
␈↓ ↓H␈↓␈↓ αHJUMPE ␈↓ ¬(if c(ac) = 0 then go to <ef>
␈↓ ↓H␈↓␈↓ αHJUMPN ␈↓ ¬(if c(ac) ≠ 0 then go to <ef>
␈↓ ↓H␈↓␈↓ αHCAME ␈↓ ¬(if c(ac) = c(<ef>) then <skip next instruction>
␈↓ ↓H␈↓␈↓ αHCAMN ␈↓ ¬(if c(ac) ≠ c(<ef>) then <skip next instruction>
␈↓ ↓H␈↓␈↓ αHPUSH ␈↓ ¬(c(c(right␈αhalf␈αof␈αac))␈α←␈αc(<ef>);␈αthe␈αcontents␈αof␈αeach
␈↓ ↓H␈↓␈↓ ¬(half␈α
of␈αac␈α
is␈α
increased␈αby␈α
one␈α
and␈αif␈α
the␈αcontents␈α
of
␈↓ ↓H␈↓␈↓ ¬(the␈α∞left␈α∞half␈α∞is␈α∞then␈α∞ 0,␈α∞a␈α∞stack␈α∞overflow␈α∞interrupt
␈↓ ↓H␈↓␈↓ ¬(occurs.␈α
 (PUSH␈α
P␈α
ac)␈α
is␈α
used␈α
in␈α
LISP␈α
to␈α
put␈α
c(ac)
␈↓ ↓H␈↓␈↓ ¬(on the stack.
␈↓ ↓H␈↓␈↓ αHPOP ␈↓ ¬(c(<ef>)␈α
←c(c(right␈α∞half␈α
of␈α
ac));␈α∞the␈α
contents␈α∞of␈α
each
␈↓ ↓H␈↓␈↓ ¬(half␈α∞of␈α∞ac␈α∞are␈α∞then␈α∞decreased␈α∞by␈α∞1.␈α∞ This␈α∂may␈α∞be
␈↓ ↓H␈↓␈↓ ¬(used␈α∩for␈α⊃removing␈α∩the␈α∩top␈α⊃element␈α∩of␈α∩the␈α⊃stack.
␈↓ ↓H␈↓␈↓ ¬wChapter  IX␈↓ ≠167


␈↓ ↓H␈↓␈↓ ¬(Thus␈α(POP␈αP␈α
1)␈αputs␈αthe␈α
top␈αelement␈αof␈α
the␈αstack
␈↓ ↓H␈↓␈↓ ¬(in␈αaccumulator␈α1.␈α The␈αi-th␈αelement␈αof␈αthe␈α
stack␈αis
␈↓ ↓H␈↓␈↓ ¬(obtained by (MOVE 1 @ i P).
␈↓ ↓H␈↓␈↓ αHPOPJ ␈↓ ¬((POPJ P) is used for returning from a subroutine

␈↓ ↓H␈↓        These␈α∞instructions␈α∞are␈α∞adequate␈α∞for␈α∞compiling␈α∂basic␈α∞LISP␈α∞code␈α∞with␈α∞ the␈α∞addition␈α∂of␈α∞the
␈↓ ↓H␈↓subroutine␈αcalling␈αpseudo-instrucion.␈α The␈αform␈αof␈αthis␈αinstruction␈αis␈α␈↓¬(CALL ␈↓↓n␈↓¬␈α(QUOTE ␈↓<subr>␈↓¬))␈↓
␈↓ ↓H␈↓.␈αIt␈αis␈αused␈αfor␈αcalling␈αthe␈αLISP␈αsubroutine␈α<subr>␈αwith␈α ␈↓↓n␈↓␈αarguments.␈α  The␈α convention␈α is␈αthat
␈↓ ↓H␈↓the␈α
arguments␈α
will␈α∞be␈α
stored␈α
in␈α∞successive␈α
accumulators␈α
beginning␈α∞with␈α
accumulator␈α
 1,␈α∞and␈α
the
␈↓ ↓H␈↓result␈α
will␈α
 be␈α
 returned␈α
 in␈α accumulator  1.␈α
  In␈α
particular␈α
the␈α
functions␈α ␈↓¬ATOM␈α
␈↓␈α
and␈α
 ␈↓¬CONS␈α
␈↓␈αare
␈↓ ↓H␈↓called␈α∂with␈α∂ ␈↓¬(CALL 1 (QUOTE ATOM))␈↓␈α∂ and␈α∂ ␈↓¬(CALL 2 (QUOTE CONS))␈↓␈α⊂  respectively.␈α∂ Programs
␈↓ ↓H␈↓produced␈α
by␈α
you␈α
 will␈α
be␈αcalled␈α
similarly␈α
when␈α
their␈α
names␈αare␈α
referred␈α
to.␈α
 The␈α
details␈α
of␈αhow
␈↓ ↓H␈↓␈↓¬CALL␈α∂␈↓works␈α∂are␈α∂unimportant␈α∂here;␈α∂it␈α∂actually␈α∂traps␈α∂to␈α∂a␈α∂machine␈α∂language␈α∂routine␈α⊂that␈α∂checks
␈↓ ↓H␈↓whether␈αthe␈αfunction␈αis␈αbeing␈αtraced␈αand␈αwhich␈αcan␈αreplace␈αthe␈α␈↓¬CALL␈α␈↓by␈α␈↓¬PUSHJ␈α␈↓for␈αgreater␈αspeed
␈↓ ↓H␈↓when tracing is definitely not wanted.

␈↓ ↓H␈↓        There␈α
are␈α
minor␈α
modifications␈αin␈α
the␈α
form␈α
of␈α
some␈αof␈α
the␈α
instructions␈α
for␈α
compiling␈αcode
␈↓ ↓H␈↓for␈α∂the␈α∞LISP1.6␈α∂assembler.␈α∞ These␈α∂include␈α∞the␈α∂calling␈α∞instruction␈α∂which␈α∞has␈α∂the␈α∂form␈α∞␈↓¬(CALL ␈↓↓n␈↓¬
␈↓ ↓H␈↓¬(QUOTE ␈↓<subr>␈↓¬) S)␈↓␈α∞,␈α∞the␈α∞form␈α
of␈α∞the␈α∞special␈α∞literal␈α
which␈α∞is␈α∞␈↓¬(C ␈↓↓m␈↓¬␈α∞0 ␈↓↓n␈↓¬␈α
0)␈↓␈α∞and␈α∞the␈α∞manner␈α
of
␈↓ ↓H␈↓indicating␈α∞indirect␈α∞addressing,␈α∞which␈α∞is␈α∞to␈α∞attach␈α∞the␈α∞@␈α∞sign␈α∞to␈α∞the␈α∞operation␈α∞name␈α∞rather␈α
than
␈↓ ↓H␈↓putting in as a separate element of the list.



␈↓ ↓H␈↓2.  ␈↓αCode produced by LISP compilers.␈↓


␈↓ ↓H␈↓        We␈αwill␈αdiscuss␈α
two␈αcompilers,␈αa␈αsimple␈α
one␈αcalled␈αLCOM0␈α
and␈αa␈αmore␈αoptimising␈α
compiler
␈↓ ↓H␈↓called␈αLCOM4.␈α LCOM4␈αproduces␈α
about␈αhalf␈αas␈αmany␈αinstructions␈α
for␈αa␈αgiven␈αfunction␈α
as␈αdoes
␈↓ ↓H␈↓LCOM0.␈α⊃Besides␈α⊃these,␈α⊂there␈α⊃are␈α⊃the␈α⊂standard␈α⊃PDP-10␈α⊃LISP␈α⊂compiler␈α⊃written␈α⊃at␈α⊃M.I.T.␈α⊂ by
␈↓ ↓H␈↓Richard␈α⊂Greenblatt␈α⊂and␈α⊂Stuart␈α⊂Nelson␈α⊃and␈α⊂modified␈α⊂by␈α⊂Whitfield␈α⊂Diffie␈α⊂and␈α⊃the␈α⊂MACLISP
␈↓ ↓H␈↓compiler NCOMPLR.

␈↓ ↓H␈↓        At␈α
the␈α
end␈α
of␈α
this␈α
section␈α
are␈α
examples␈α
of␈α
the␈α
output␈α
of␈α
each␈α
of␈α
the␈α
compilers␈α
mentioned
␈↓ ↓H␈↓above for the file containing:


␈↓ ↓H␈↓                ␈↓¬(DEFPROP DROP␈↓
␈↓ ↓H␈↓                 ␈↓¬(LAMBDA(U)␈↓
␈↓ ↓H␈↓                  ␈↓¬(COND ((NULL U) NIL) (T (CONS (LIST (CAR U)) (DROP (CDR U))))))␈↓
␈↓ ↓H␈↓                ␈↓¬EXPR)␈↓

␈↓ ↓H␈↓        The following comments are with regard to these examples.

␈↓ ↓H␈↓        1.␈α
Note␈α
 that␈α
all␈α
four␈α
compilers␈α
produce␈αthe␈α
same␈α
first␈α
line␈α
of␈α
heading.␈α
 This␈α
is␈αnecessary␈α
for
␈↓ ↓H␈↓the␈α⊂LAP␈α⊂assembly␈α⊂program␈α⊂ which␈α⊂ also␈α⊂requires␈α⊂ the␈α⊂ ␈↓¬NIL␈↓ ␈α⊂at␈α⊂the␈α⊂end␈α⊂as␈α⊂punctuation.␈α∂ The
␈↓ ↓H␈↓standard␈αcompiler␈αand␈αNCOMPLR␈αuse␈αa␈α
function␈αcalled␈α ␈↓¬XCONS ␈α␈↓that␈αhas␈α the␈α same␈α
 effect␈α as
␈↓ ↓H␈↓␈↓¬CONS␈α
␈↓␈α
except␈α
 that␈α
 it␈αreceives␈α
its␈α
arguments␈α
in␈α
the␈αreverse␈α
order␈α
which␈α
is␈α
sometimes␈αconvenient.
␈↓ ↓H␈↓This␈αrequires,␈αof␈αcourse,␈αthat␈αthe␈αcompiler␈αbe␈αsmart␈αenough␈αto␈αdecide␈αwhere␈αit␈αis␈αmore␈αconvenient
␈↓ ↓H␈↓to␈αput␈αthe␈αarguments␈αof␈αthe␈α␈↓↓cons␈↓␈αfunction.␈α They␈αalso␈αuse␈α␈↓¬NCONS␈α␈↓rather␈αthan␈α␈↓¬LIST␈α␈↓␈αto␈αform␈αa␈αlist
␈↓ ↓H␈↓of one element.
␈↓ ↓H␈↓168␈↓ ¬wChapter  IX␈↓ H


␈↓ ↓H␈↓        2.␈α∂The␈α⊂two␈α∂compilers,␈α⊂LCOM0␈α∂and␈α⊂LCOM4,␈α∂are␈α⊂similar␈α∂in␈α⊂structure,␈α∂but␈α⊂LCOM4,␈α∂the
␈↓ ↓H␈↓better one, which is twice as long, uses the following optimisations.

␈↓ ↓H␈↓        2.1.␈α
When␈α∞the␈α
argument␈α∞of␈α
␈↓¬CAR␈α∞␈↓or␈α
␈↓¬CDR␈α
␈↓is␈α∞a␈α
variable,␈α∞it␈α
compiles␈α∞a␈α
 ␈↓¬(HLRZ 1 @  ␈↓↓i␈↓¬␈α∞P)␈↓␈α
 or
␈↓ ↓H␈↓␈↓¬(HRRZ 1 @  ␈↓↓i␈↓¬␈α∞P)␈↓␈α∞ which␈α
gets␈α∞the␈α∞result␈α
through␈α∞the␈α∞stack␈α
without␈α∞first␈α∞compiling␈α∞the␈α
argument
␈↓ ↓H␈↓into an accumulator.

␈↓ ↓H␈↓        2.2.␈α
When␈α
it␈α
has␈α
to␈α
set␈α
up␈α
the␈α
arguments␈α
of␈α
a␈α
function␈α
 in␈α
 the␈α
accumulators,␈α
 in␈αgeneral,␈α
the
␈↓ ↓H␈↓program␈α
must␈α
compute␈α
the␈α
arguments␈α
one␈α
at␈α
a␈αtime␈α
and␈α
save␈α
them␈α
on␈α
the␈α
stack,␈α
and␈α
then␈αload
␈↓ ↓H␈↓the␈α∞ accumulators␈α∞from␈α∞the␈α∞stack.␈α∞ However,␈α∞if␈α∂one␈α∞of␈α∞the␈α∞arguments␈α∞is␈α∞a␈α∞variable,␈α∞is␈α∂a␈α∞quoted
␈↓ ↓H␈↓expression,␈αor␈αcan␈αbe␈αobtained␈αfrom␈αa␈αvariable␈αby␈αa␈α chain␈α of␈α ␈↓¬CAR␈↓s ␈α and␈α  ␈↓¬CDR␈↓s,␈α then␈α it␈α need
␈↓ ↓H␈↓not␈αbe␈αcomputed␈α
until␈αthe␈αtime␈α
of␈αloading␈α accumulators␈α
 since␈α it␈α can␈α
 be␈α computed␈α using␈α
 only
␈↓ ↓H␈↓the accumulator in which it is wanted.

␈↓ ↓H␈↓        3.␈α⊂ The␈α⊂ Diffie␈α⊂ compiler␈α⊂ produces␈α⊂about␈α⊂10␈α⊂percent␈α⊂less␈α⊂code␈α⊂than␈α⊂LCOM4;␈α⊂the␈α∂main
␈↓ ↓H␈↓difference␈α∞seems␈α∞to␈α∂be␈α∞that␈α∞it␈α∂sometimes␈α∞notices␈α∞when␈α∂ it␈α∞ has␈α∞ an␈α∂ expression␈α∞that␈α∞it␈α∂will␈α∞need
␈↓ ↓H␈↓later.␈αSometimes␈α
this␈αfeature␈αleads␈α
it␈αastray.␈α
For␈αexample,␈αit␈α
may␈αsave␈α
 ␈↓αa|␈↓␈↓↓u␈↓␈α ␈αfor␈α
later␈αuse␈αat␈α
greater
␈↓ ↓H␈↓cost than re-computing it.

␈↓ ↓H␈↓        4.␈α∩ NCOMPLR␈α∩is␈α∩noted␈α∩particularly␈α∩for␈α∩its␈α∩efficient␈α∩code␈α∩for␈α∩numerical␈α∩computations.
␈↓ ↓H␈↓Hence␈α_the␈α_"N".␈α_   Except␈α_for␈α→some␈α_bookkeeping␈α_code␈α_initially,␈α_the␈α_Diffie␈α→compiler␈α_and
␈↓ ↓H␈↓NCOMPLR produce similar code for this example.
␈↓ ↓H␈↓␈↓ ¬wChapter  IX␈↓ ≠169


␈↓ ↓H␈↓******************************************************************



␈↓ ↓H␈↓LCOM0  produces the following code for the MACLISP LAP assembler.


␈↓ ↓H␈↓¬(LAP DROP SUBR) 
␈↓ ↓H␈↓¬(PUSH P 1) 
␈↓ ↓H␈↓¬(MOVE 1 0 P) 
␈↓ ↓H␈↓¬(PUSH P 1) 
␈↓ ↓H␈↓¬(MOVE 1 0 P) 
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1)) 
␈↓ ↓H␈↓¬(CALL 1 (QUOTE NULL)) 
␈↓ ↓H␈↓¬(JUMPE 1 G0003) 
␈↓ ↓H␈↓¬(MOVEI 1 0) 
␈↓ ↓H␈↓¬(JRST 0 G0002) 
␈↓ ↓H␈↓¬G0003 
␈↓ ↓H␈↓¬(MOVEI 1 (QUOTE T)) 
␈↓ ↓H␈↓¬(JUMPE 1 G0004) 
␈↓ ↓H␈↓¬(MOVE 1 0 P) 
␈↓ ↓H␈↓¬(PUSH P 1) 
␈↓ ↓H␈↓¬(MOVE 1 0 P) 
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1)) 
␈↓ ↓H␈↓¬(CALL 1 (QUOTE CAR)) 
␈↓ ↓H␈↓¬(PUSH P 1) 
␈↓ ↓H␈↓¬(MOVE 1 0 P) 
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1)) 
␈↓ ↓H␈↓¬(CALL 1 (QUOTE LIST)) 
␈↓ ↓H␈↓¬(PUSH P 1) 
␈↓ ↓H␈↓¬(MOVE 1 -1 P) 
␈↓ ↓H␈↓¬(PUSH P 1) 
␈↓ ↓H␈↓¬(MOVE 1 0 P) 
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1)) 
␈↓ ↓H␈↓¬(CALL 1 (QUOTE CDR)) 
␈↓ ↓H␈↓¬(PUSH P 1) 
␈↓ ↓H␈↓¬(MOVE 1 0 P) 
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1)) 
␈↓ ↓H␈↓¬(CALL 1 (QUOTE DROP)) 
␈↓ ↓H␈↓¬(PUSH P 1) 
␈↓ ↓H␈↓¬(MOVE 1 -1 P) 
␈↓ ↓H␈↓¬(MOVE 2 0 P) 
␈↓ ↓H␈↓¬(SUB P (% 0 0 2 2)) 
␈↓ ↓H␈↓¬(CALL 2 (QUOTE CONS)) 
␈↓ ↓H␈↓¬(JRST 0 G0002) 
␈↓ ↓H␈↓¬G0004 
␈↓ ↓H␈↓¬G0002 
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1)) 
␈↓ ↓H␈↓¬(POPJ P) 
␈↓ ↓H␈↓¬NIL 
␈↓ ↓H␈↓170␈↓ ¬wChapter  IX␈↓ H


␈↓ ↓H␈↓******************************************************************



␈↓ ↓H␈↓LCOM4  produces the following code for the MACLISP LAP assembler.



␈↓ ↓H␈↓¬(LAP DROP SUBR)                             
␈↓ ↓H␈↓¬(PUSH P 1)                                 ~save argument, U, on stack
␈↓ ↓H␈↓¬(MOVE 1 0 P)                               ~get U off stack
␈↓ ↓H␈↓¬(JUMPE 1 G0003)                            ~if U=NIL then exit
␈↓ ↓H␈↓¬(HLRZ 1 @ 0 P)                             ~car U
␈↓ ↓H␈↓¬(CALL 1 (QUOTE LIST))                      ~<car U>
␈↓ ↓H␈↓¬(PUSH P 1)                                 ~save <car U>
␈↓ ↓H␈↓¬(HRRZ @ 1 -1 P)                            ~cdr U (U now 1 below top of stack
␈↓ ↓H␈↓¬(CALL 1 (QUOTE DROP))                      ~drop cdr U
␈↓ ↓H␈↓¬(MOVE 2 1)                                 ~set up arguments for cons
␈↓ ↓H␈↓¬(MOVE 1 0 P) 
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1))                        ~reset stack to arglist
␈↓ ↓H␈↓¬(CALL 2 (QUOTE CONS))                      ~cons[<car u>,cdr U]
␈↓ ↓H␈↓¬G0003 
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1))                        ~reset stack to position at entry
␈↓ ↓H␈↓¬(POPJ P)                                   ~return
␈↓ ↓H␈↓¬NIL 



␈↓ ↓H␈↓******************************************************************



␈↓ ↓H␈↓The Diffie compiler produces the following code for the LISP1.6 assembler.


␈↓ ↓H␈↓¬(LAP DROP SUBR) 
␈↓ ↓H␈↓¬       (PUSH P 1) 
␈↓ ↓H␈↓¬       (JUMPE 1 TAG1) 
␈↓ ↓H␈↓¬       (HLRZ@ 1 0 P) 
␈↓ ↓H␈↓¬       (CALL 1 (E NCONS) S) 
␈↓ ↓H␈↓¬       (PUSH P 1) 
␈↓ ↓H␈↓¬       (HRRZ@ 1 -1 P) 
␈↓ ↓H␈↓¬       (CALL 1 (E DROP) S) 
␈↓ ↓H␈↓¬       (POP P 2) 
␈↓ ↓H␈↓¬       (CALL 2 (E XCONS) S) 
␈↓ ↓H␈↓¬ TAG1  (SUB P (C O 0 1 1)) 
␈↓ ↓H␈↓¬       (POPJ P) 
␈↓ ↓H␈↓¬       NIL 
␈↓ ↓H␈↓␈↓ ¬wChapter  IX␈↓ ≠171


␈↓ ↓H␈↓******************************************************************



␈↓ ↓H␈↓The MACLISP compiler, NCOMPLR,  produces the following code:

␈↓ ↓H␈↓        (The␈α∞pseudo-instruction␈α∞ARGS␈α∞tells␈α∞the␈α∞assembler␈α∞the␈α∞number␈α∞of␈α∞arguments␈α∂expected␈α∞by
␈↓ ↓H␈↓the␈α∞function.␈α∂ The␈α∞instruction␈α∂JSP␈α∞is␈α∂a␈α∞special␈α∂fast␈α∞function␈α∂call␈α∞done␈α∂by␈α∞jumping␈α∂rather␈α∞than
␈↓ ↓H␈↓going␈α⊃through␈α⊃the␈α⊃regular␈α⊃calling␈α∩mechanism.␈α⊃ T␈α⊃denotes␈α⊃an␈α⊃accumulator␈α⊃designated␈α∩to␈α⊃hold
␈↓ ↓H␈↓temporary values.)


␈↓ ↓H␈↓¬(LAP DROP SUBR) 
␈↓ ↓H␈↓¬(ARGS DROP (NIL . 1)) 
␈↓ ↓H␈↓¬(PUSH P 1) 
␈↓ ↓H␈↓¬(JSP T PDLNMK) 
␈↓ ↓H␈↓¬(JUMPE 1 G0001) 
␈↓ ↓H␈↓¬(HLRZ 1 @ 0 P) 
␈↓ ↓H␈↓¬(JSP T %NCONS) 
␈↓ ↓H␈↓¬(PUSH P 1) 
␈↓ ↓H␈↓¬(HRRZ 1 @ -1 P) 
␈↓ ↓H␈↓¬(CALL 1 'DROP) 
␈↓ ↓H␈↓¬(POP P 2) 
␈↓ ↓H␈↓¬(JSP T %XCONS) 
␈↓ ↓H␈↓¬G0001 
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1)) 
␈↓ ↓H␈↓¬(POPJ P) 
␈↓ ↓H␈↓¬NIL 
␈↓ ↓H␈↓172␈↓ ¬wChapter  IX␈↓ H


␈↓ ↓H␈↓3.  ␈↓αLCOM0.␈↓


␈↓ ↓H␈↓        The␈α∂following␈α∞is␈α∂an␈α∞annotated␈α∂listing␈α∞of␈α∂the␈α∞MACLISP␈α∂version␈α∞of␈α∂LCOM0␈α∂ in␈α∞external
␈↓ ↓H␈↓notation.  A listing in internal notation can be found in Appendix A.

␈↓ ↓H␈↓        ␈↓↓compl␈↓␈α∂is␈α∂the␈α∂user-callable␈α∂driver.␈α∂ It␈α∂is␈α∂a␈α∂FEXPR.␈α∂ It␈α∂takes␈α∂as␈α∂an␈α∂argument␈α∂a␈α∂single␈α∂file
␈↓ ↓H␈↓name.␈α⊂ EXPRs␈α⊂on␈α⊂a␈α⊂file␈α∂called␈α⊂FILNAM␈α⊂will␈α⊂be␈α⊂compiled␈α∂into␈α⊂LAP␈α⊂and␈α⊂written␈α⊂on␈α⊂the␈α∂file
␈↓ ↓H␈↓FILNAM.LAP.␈α⊃Other␈α⊃types␈α⊃of␈α⊃function␈α⊃definitions␈α⊃and␈α⊃non-definitions␈α⊃are␈α⊃simply␈α⊃copied␈α⊃to
␈↓ ↓H␈↓output.␈α It␈αis,␈αalas,␈αdependent␈αon␈αthe␈αI/O␈αstructure␈αof␈αthe␈αimplementation␈αand␈αneed␈αnot␈αbe␈αstudied
␈↓ ↓H␈↓carefully.  All the other functions are substantially independent of implementation.

␈↓ ↓H␈↓␈↓ α8FEXPER:
␈↓ ↓H␈↓␈↓ α8␈↓↓compl file ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  [uwrite[], ␈↓␈↓ λ(~Open a file for output
␈↓ ↓H␈↓␈↓ α8␈↓↓   apply[␈↓¬EREAD␈↓↓, file], ␈↓␈↓ λ(~Open input file
␈↓ ↓H␈↓␈↓ α8␈↓↓   select-disk-input[␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     read-until-eof[with  z  do␈↓␈↓ λ(~Read each expression in file
␈↓ ↓H␈↓␈↓ α8␈↓↓       ␈↓αif␈↓↓ ␈↓αa|␈↓↓z = ␈↓¬DEFUN␈↓↓ ∨ [␈↓αa|␈↓↓z = ␈↓¬DEFPROP␈↓↓ ∧ ␈↓αaddd|␈↓↓z = ␈↓¬EXPR␈↓↓] ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         [␈↓αprogram␈↓↓ [prog]␈↓
␈↓ ↓H␈↓3.1)␈↓ α8␈↓↓               prog␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ← [␈↓αif␈↓↓ ␈↓αa|␈↓↓z = ␈↓¬DEFUN␈↓↓ ␈↓αthen␈↓↓ comp[␈↓αad|␈↓↓z, ␈↓αadd|␈↓↓z, ␈↓αaddd|␈↓↓z]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                   ␈↓αelse␈↓↓ comp[␈↓αad|␈↓↓z, ␈↓αad|␈↓↓␈↓αadd|␈↓↓z, ␈↓αadd|␈↓↓␈↓αadd|␈↓↓z]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               unselect-tty ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 select-disk-output mapc[print, prog]␈↓␈↓ λ(~ Print code in file
␈↓ ↓H␈↓␈↓ α8␈↓↓               print <␈↓αad|␈↓↓z, length prog>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        ␈↓αelse␈↓↓ unselect-tty select-disk-output print z], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     apply[␈↓¬UFILE␈↓↓, <␈↓αa|␈↓↓file, ␈↓¬LAP␈↓↓>], ␈↓␈↓ λ(~Close and name file
␈↓ ↓H␈↓␈↓ α8␈↓↓     ␈↓¬ENDCOMP␈↓↓]]␈↓

␈↓ ↓H␈↓        ␈↓↓comp␈↓␈αcompiles␈αa␈αsingle␈α
function␈αdefinition,␈αreturning␈αa␈α
list␈αof␈αthe␈αLAP␈α
code␈αcorresponding
␈↓ ↓H␈↓to␈α⊂the␈α⊂definition.␈α⊂ ␈↓↓fn␈↓␈α⊂is␈α⊃the␈α⊂atomic␈α⊂name␈α⊂of␈α⊂the␈α⊃function␈α⊂being␈α⊂compiled.␈α⊂ ␈↓↓vars␈↓␈α⊂is␈α⊃the␈α⊂formal
␈↓ ↓H␈↓parameter list for the function.  ␈↓↓exp␈↓ is the function body.

␈↓ ↓H␈↓␈↓ α8␈↓↓comp[fn, vars, exp] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  {length vars}[λn: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <<␈↓¬LAP␈↓↓, fn, ␈↓¬SUBR␈↓↓>>␈↓
␈↓ ↓H␈↓3.2)␈↓ α8␈↓↓    * mkpush[n, 1]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    * compexp[exp, -n, prup[vars, 1]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    * <<␈↓¬SUB␈↓↓, ␈↓¬P␈↓↓, <␈↓¬%␈↓↓, 0, 0, n, n>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    * ␈↓¬((POPJ P) NIL)␈↓↓]␈↓

␈↓ ↓H␈↓        ␈↓↓prup␈↓␈α∩returns␈α∩an␈α∩a-list␈α∩formed␈α∩by␈α⊃pairing␈α∩successive␈α∩elements␈α∩of␈α∩␈↓↓vars␈↓␈α∩with␈α⊃consecutive
␈↓ ↓H␈↓integers beginning with ␈↓↓n.␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓prup[vars, n] ← ␈↓
␈↓ ↓H␈↓3.3)␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓vars ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [␈↓αa|␈↓↓vars . n] . prup[␈↓αd|␈↓↓vars, add1 n]␈↓

␈↓ ↓H␈↓        ␈↓↓mkpush␈↓␈α
returns␈α∞a␈α
list␈α
of␈α∞␈↓↓n␈↓␈α
␈↓¬(PUSH␈α
P␈α∞␈↓↓i␈↓¬)␈↓␈α
instructions,␈α
where␈α∞␈↓↓i␈↓␈α
runs␈α
from␈α∞␈↓↓n␈↓␈α
to␈α
␈↓↓m+n-1.␈↓␈α∞ It␈α
is
␈↓ ↓H␈↓used to push arguments onto the stack.
␈↓ ↓H␈↓␈↓ ¬wChapter  IX␈↓ ≠173


␈↓ ↓H␈↓␈↓ α8␈↓↓mkpush[n, m] ← ␈↓
␈↓ ↓H␈↓3.4)␈↓ α8␈↓↓  ␈↓αif␈↓↓ n < m ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ <␈↓¬PUSH␈↓↓, ␈↓¬P␈↓↓, m> . mkpush[n, add1 m]␈↓

␈↓ ↓H␈↓        ␈↓↓compexp␈↓␈α∩is␈α∪the␈α∩heart␈α∩of␈α∪LCOM0.␈α∩ It␈α∪determines␈α∩precisely␈α∩what␈α∪an␈α∩expression␈α∪is,␈α∩and
␈↓ ↓H␈↓compiles␈α⊃appropriate␈α⊃code␈α⊂for␈α⊃it.␈α⊃ It␈α⊂returns␈α⊃a␈α⊃list␈α⊃of␈α⊂that␈α⊃code.␈α⊃ ␈↓↓exp␈↓␈α⊂is␈α⊃the␈α⊃expression␈α⊃to␈α⊂be
␈↓ ↓H␈↓compiled.␈α ␈↓↓m␈↓␈αis␈αminus␈αthe␈αnumber␈αof␈αentries␈αon␈αthe␈αstack.␈αWhen␈αadded␈αto␈αa␈αvalue␈αretrieved␈αfrom
␈↓ ↓H␈↓the␈αA-LIST␈α␈↓↓vpr,␈↓␈αit␈αcan␈αbe␈αused␈αto␈αlocate␈αa␈αvariable␈αon␈αthe␈αstack.␈α ␈↓↓vpr␈↓␈αis␈αan␈αA-LIST,␈αassociating
␈↓ ↓H␈↓variable␈α⊂names␈α⊂with␈α⊂numbers␈α⊂which,␈α⊂when␈α⊂added␈α∂to␈α⊂␈↓↓m,␈↓␈α⊂give␈α⊂stack␈α⊂offsets.␈α⊂ Both␈α⊂␈↓↓m␈↓␈α⊂and␈α∂␈↓↓vpr␈↓
␈↓ ↓H␈↓maintain␈α∂these␈α∂definitions␈α∞throughout.␈α∂ ␈↓↓gensym[]␈↓␈α∂is␈α∂a␈α∞pseudo-function␈α∂of␈α∂no␈α∂arguments.␈α∞ Every
␈↓ ↓H␈↓time␈αit␈αis␈αcalled,␈αit␈αreturns␈αa␈αdifferent␈αsymbol.␈α The␈αpresence␈αof␈α␈↓↓gensym[]␈↓␈αmeans␈αthat␈αLCOM0␈α
and
␈↓ ↓H␈↓LCOM4␈α
are␈α
not␈α
pure␈αLISP␈α
so␈α
that␈α
the␈αtechniques␈α
of␈α
Chapter␈α
3␈α
cannot␈αbe␈α
used␈α
to␈α
prove␈αthat␈α
they
␈↓ ↓H␈↓meet␈α⊂their␈α∂specifications.␈α⊂ It␈α∂is␈α⊂possible␈α∂to␈α⊂eliminate␈α⊂the␈α∂use␈α⊂of␈α∂␈↓↓gensym,␈↓␈α⊂but␈α∂as␈α⊂of␈α⊂the␈α∂present
␈↓ ↓H␈↓writing,␈α
such␈α
version␈α
hasn't␈α
been␈α
written.␈α In␈α
any␈α
case,␈α
other␈α
unsolved␈α
problems␈α
remain␈αbefore␈α
one
␈↓ ↓H␈↓can␈α
give␈α
a␈α
first␈α
order␈αlogic␈α
proof␈α
of␈α
the␈α
correspondence␈αbetween␈α
␈↓↓eval␈↓␈α
and␈α
the␈α
code␈α
produced␈αby
␈↓ ↓H␈↓the compilers.


␈↓ ↓H␈↓␈↓ α8␈↓↓compexp[exp, m, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓exp ␈↓αthen␈↓↓ ␈↓¬((MOVEI 1 0))␈↓↓␈↓␈↓ 	_~␈↓¬NIL␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ exp = ␈↓¬T␈↓↓ ␈↓αthen␈↓↓ ␈↓¬((MOVEI 1 (QUOTE T)))␈↓↓␈↓␈↓ 	_~␈↓¬T␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ numberp exp ␈↓αthen␈↓↓ <<␈↓¬MOVEI␈↓↓, 1, <␈↓¬QUOTE␈↓↓, exp>>>␈↓␈↓ 	_~number
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓exp ␈↓αthen␈↓↓ <<␈↓¬MOVE␈↓↓, 1, m + ␈↓αd|␈↓↓assoc[exp, vpr], ␈↓¬P␈↓↓>>␈↓␈↓ 	_~variable
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬AND␈↓↓ ∨ ␈↓αa|␈↓↓exp = ␈↓¬OR␈↓↓ ∨ ␈↓αa|␈↓↓exp = ␈↓¬NOT␈↓↓ ␈↓αthen␈↓↓ ␈↓␈↓ 	_~boolean expression
␈↓ ↓H␈↓␈↓ α8␈↓↓    {gensym[], gensym[]}[λl1, l2: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      combool[exp, m, l1, ␈↓¬NIL␈↓↓, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      * <␈↓¬(MOVEI 1 (QUOTE T))␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         <␈↓¬JRST␈↓↓, 0, l2>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         l1, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         ␈↓¬(MOVEI 1 0)␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         l2>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬COND␈↓↓ ␈↓αthen␈↓↓ comcond[␈↓αd|␈↓↓exp, m, gensym[], vpr]␈↓␈↓ 	_~␈↓¬COND ␈↓
␈↓ ↓H␈↓3.5)␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬QUOTE␈↓↓ ␈↓αthen␈↓↓ <<␈↓¬MOVEI␈↓↓, 1, exp>>␈↓␈↓ 	_~␈↓¬QUOTE ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓exp ␈↓αthen␈↓↓ ␈↓␈↓ 	_~function call
␈↓ ↓H␈↓␈↓ α8␈↓↓    {length ␈↓αd|␈↓↓exp}[λn: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      complis[␈↓αd|␈↓↓exp, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      * loadac[1 - n, 1]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      * <<␈↓¬SUB␈↓↓, ␈↓¬P␈↓↓, <␈↓¬%␈↓↓, 0, 0, n, n>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      * <<␈↓¬CALL␈↓↓, n, <␈↓¬QUOTE␈↓↓, ␈↓αa|␈↓↓exp>>>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓exp = ␈↓¬LAMBDA␈↓↓ ␈↓αthen␈↓↓ ␈↓␈↓ 	_~λ-expression
␈↓ ↓H␈↓␈↓ α8␈↓↓    {length ␈↓αd|␈↓↓exp}[λn: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      complis[␈↓αd|␈↓↓exp, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      * compexp[␈↓αadda|␈↓↓exp, m - n, prup[␈↓αada|␈↓↓exp, 1 - m] * vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      * <<␈↓¬SUB␈↓↓, ␈↓¬P␈↓↓, <␈↓¬%␈↓↓, 0, 0, n, n>>>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓␈↓␈↓ 	_~oops!

␈↓ ↓H␈↓        ␈↓↓complis␈↓␈αcompiles␈αcode␈αto␈αevaluate␈αeach␈αexpression␈αin␈αa␈αlist␈αof␈αexpressions␈αand␈αto␈αpush␈αthose
␈↓ ↓H␈↓values␈α⊃onto␈α⊃the␈α⊃stack.␈α⊃ It␈α⊃returns␈α⊂a␈α⊃list␈α⊃of␈α⊃that␈α⊃code.␈α⊃ It␈α⊂is␈α⊃used␈α⊃to␈α⊃compile␈α⊃code␈α⊃to␈α⊂evaluate
␈↓ ↓H␈↓arguments to called functions or λ-expressions.  ␈↓↓u␈↓ is a list of expressions.

␈↓ ↓H␈↓␈↓ α8␈↓↓complis[u, m, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓174␈↓ ¬wChapter  IX␈↓ H


␈↓ ↓H␈↓3.6)␈↓ α8␈↓↓  ␈↓αelse␈↓↓ compexp[␈↓αa|␈↓↓u, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        * ␈↓¬((PUSH P 1))␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        * complis[␈↓αd|␈↓↓u, sub1 m, vpr]␈↓

␈↓ ↓H␈↓␈↓↓loadac␈↓␈α
returns␈α
a␈α∞list␈α
of␈α
␈↓¬(MOVE␈α
␈↓↓i␈↓¬␈α∞␈↓↓j␈↓¬␈α
P)␈↓␈α
instructions,␈α
loading␈α∞consecutive␈α
accumulators␈α
from␈α∞the␈α
top
␈↓ ↓H␈↓of the stack.  ␈↓↓k␈↓ indexes the accumulator loaded.  ␈↓↓n␈↓ indexes the stack offset.

␈↓ ↓H␈↓␈↓ α8␈↓↓loadac[n, k] ← ␈↓
␈↓ ↓H␈↓3.7)␈↓ α8␈↓↓  ␈↓αif␈↓↓ n > 0 ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ <␈↓¬MOVE␈↓↓, k, n, ␈↓¬P␈↓↓> . loadac[add1 n, add1 k]␈↓

␈↓ ↓H␈↓        ␈↓↓comcond␈↓␈αcompiles␈αa␈α
␈↓¬COND.␈α␈↓␈α␈↓↓u␈↓␈αis␈α
a␈αlist␈αof␈α
clauses␈αin␈αthe␈α␈↓¬COND.␈α
␈↓␈α␈↓↓l␈↓␈αis␈αa␈α
label␈αto␈αbe␈α
emitted␈αat
␈↓ ↓H␈↓the end of all code for the ␈↓¬COND. ␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓comcond[u, m, l, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ <l>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ {gensym[]}[λl1: ␈↓
␈↓ ↓H␈↓3.8)␈↓ α8␈↓↓    combool[␈↓αaa|␈↓↓u, m, l1, ␈↓¬NIL␈↓↓, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    * compexp[␈↓αada|␈↓↓u, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    * <<␈↓¬JRST␈↓↓, 0, l>, l1>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    * comcond[␈↓αd|␈↓↓u, m, l, vpr]]␈↓

␈↓ ↓H␈↓        ␈↓↓combool␈↓␈α⊂compiles␈α∂code␈α⊂for␈α∂a␈α⊂single␈α∂propositional␈α⊂expression.␈α∂ That␈α⊂is,␈α∂the␈α⊂code␈α∂generated
␈↓ ↓H␈↓evaluates␈αthe␈αexpression␈αand␈αbranches␈αsomewhere,␈αaccording␈αto␈αwhether␈αits␈αvalue␈αis␈αtrue␈αor␈αfalse.
␈↓ ↓H␈↓␈↓↓p␈↓␈αis␈αthe␈αpropositional␈αexpression.␈α ␈↓↓l␈↓␈αis␈αa␈αlabel␈αwhich␈αrepresents␈αthe␈αbranch␈αpoint.␈α ␈↓↓flg␈↓␈αis␈αa␈αflag.␈α If
␈↓ ↓H␈↓␈↓↓flg␈↓␈αis␈α
␈↓¬NIL␈↓,␈αcode␈α
is␈αto␈αfall␈α
thru␈αon␈α
non-␈↓¬NIL␈↓␈αresult␈α
and␈αbranch␈αto␈α
␈↓↓l␈↓␈αon␈α
␈↓¬NIL␈↓␈αresult.␈α
 If␈α␈↓↓flg␈↓␈αis␈α
non-␈↓¬NIL␈↓,
␈↓ ↓H␈↓code is to fall thru on ␈↓¬NIL␈↓ result and branch to ␈↓↓l␈↓ on non-␈↓¬NIL␈↓ result.

␈↓ ↓H␈↓␈↓ α8␈↓↓combool[p, m, l, flg, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αat|␈↓↓p ␈↓αthen␈↓↓ ␈↓␈↓ 	_~simple variable
␈↓ ↓H␈↓␈↓ α8␈↓↓    [compexp[p, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     * <<␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ ␈↓¬JUMPN␈↓↓ ␈↓αelse␈↓↓ ␈↓¬JUMPE␈↓↓, 1, l>>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬AND␈↓↓ ␈↓αthen␈↓↓ ␈↓␈↓ 	_~conjunction
␈↓ ↓H␈↓␈↓ α8␈↓↓    [␈↓αif␈↓↓ ¬flg ␈↓αthen␈↓↓ compandor[␈↓αd|␈↓↓p, m, l, ␈↓¬NIL␈↓↓, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     ␈↓αelse␈↓↓ {gensym[]}[λl1: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓       compandor[␈↓αd|␈↓↓p, m, l1, ␈↓¬NIL␈↓↓, vpr]␈↓
␈↓ ↓H␈↓3.9)␈↓ α8␈↓↓       * <<␈↓¬JRST␈↓↓, 0, l>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓       * <l1>]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬OR␈↓↓ ␈↓αthen␈↓↓ ␈↓␈↓ 	_~disjunction
␈↓ ↓H␈↓␈↓ α8␈↓↓    [␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ compandor[␈↓αd|␈↓↓p, m, l, ␈↓¬T␈↓↓, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     ␈↓αelse␈↓↓ {gensym[]}[λl1: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓       compandor[␈↓αd|␈↓↓p, m, l1, ␈↓¬T␈↓↓, vpr] * <<␈↓¬JRST␈↓↓, 0, l>> * <l1>]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬NOT␈↓↓ ␈↓αthen␈↓↓ combool[␈↓αad|␈↓↓p, m, l, ¬flg, vpr]␈↓␈↓ 	_~negation
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ compexp[p, m, vpr]␈↓␈↓ 	_~other expression
␈↓ ↓H␈↓␈↓ α8␈↓↓        * <<␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ ␈↓¬JUMPN␈↓↓ ␈↓αelse␈↓↓ ␈↓¬JUMPE␈↓↓, 1, l>>␈↓

␈↓ ↓H␈↓        ␈↓↓compandor␈↓␈α
compiles␈αcode␈α
for␈α
lists␈αof␈α
predicates␈α
connected␈αconjunctively␈α
or␈α
disjunctively.␈α ␈↓↓u␈↓
␈↓ ↓H␈↓is␈α
a␈α∞list␈α
of␈α
predicates.␈α∞ ␈↓↓l␈↓␈α
is␈α∞a␈α
label.␈α
 ␈↓↓flg␈↓␈α∞is␈α
a␈α∞flag.␈α
 If␈α
␈↓↓flg␈↓␈α∞is␈α
␈↓¬NIL␈↓,␈α∞we␈α
are␈α
to␈α∞fall␈α
thru␈α∞on␈α
non-␈↓¬NIL␈↓
␈↓ ↓H␈↓results␈α
and␈α
branch␈α
to␈α
␈↓↓l␈↓␈α
on␈α
␈↓¬NIL␈↓␈α
results␈α
(␈↓¬AND␈α
␈↓case).␈α
 If␈α
␈↓↓flg␈↓␈α
is␈α
non-␈↓¬NIL␈↓,␈α
we␈α
are␈α
to␈α
fall␈α
thru␈α
on␈α
␈↓¬NIL␈↓
␈↓ ↓H␈↓results and branch to ␈↓↓l␈↓ on non-␈↓¬NIL␈↓ results (␈↓¬OR ␈↓case).
␈↓ ↓H␈↓␈↓ ¬wChapter  IX␈↓ ≠175


␈↓ ↓H␈↓␈↓ α8␈↓↓compandor[u, m, l, flg, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓3.10)␈↓ α8␈↓↓  ␈↓αelse␈↓↓ combool[␈↓αa|␈↓↓u, m, l, flg, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        * compandor[␈↓αd|␈↓↓u, m, l, flg, vpr]␈↓
␈↓ ↓H␈↓176␈↓ ¬wChapter  IX␈↓ H


␈↓ ↓H␈↓4.  ␈↓αLCOM4.␈↓



␈↓ ↓H␈↓        The␈α∞following␈α∞is␈α∞a␈α∂listing␈α∞in␈α∞external␈α∞notation␈α∞of␈α∂the␈α∞MACLISP␈α∞version␈α∞of␈α∂the␈α∞compiler
␈↓ ↓H␈↓LCOM4.␈α∂  A␈α∂listing␈α∂in␈α∂internal␈α∂notation␈α∂can␈α∂be␈α∂found␈α∂in␈α∂Appendix A.␈α∂ Comments␈α⊂are␈α∂added
␈↓ ↓H␈↓where␈αthis␈αcompiler␈αdiffers␈αfrom␈αLCOM0.␈α The␈αmain␈αdifference␈αhas␈αto␈αdo␈αwith␈αthe␈αclassification
␈↓ ↓H␈↓of␈α
expressions␈α
into␈α
five␈α∞classes␈α
that␈α
can␈α
be␈α
compiled␈α∞differently␈α
for␈α
greater␈α
efficiency.␈α∞ The␈α
five
␈↓ ↓H␈↓classes are defined as follows:


␈↓ ↓H␈↓        0.      constants ␈↓¬NIL␈↓, ␈↓¬T␈↓, and numbers,
␈↓ ↓H␈↓        1.      variables -- any atom not considered a constant,
␈↓ ↓H␈↓        2.      quoted S-expressions -- non-atomic constants,
␈↓ ↓H␈↓        3.      ␈↓↓car␈↓ - ␈↓↓cdr␈↓ chains applied to a variable,
␈↓ ↓H␈↓        4.      all others, except
␈↓ ↓H␈↓        5.      the last class 4 element in the argument list.


␈↓ ↓H␈↓        FEXPR:
␈↓ ↓H␈↓␈↓ α8␈↓↓compl file ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  [uwrite[], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓   apply[␈↓¬EREAD␈↓↓, file], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓   select-disk-input[␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     read-until-eof[with  z  do␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓       ␈↓αif␈↓↓ ␈↓αa|␈↓↓z = ␈↓¬DEFUN␈↓↓ ∨ [␈↓αa|␈↓↓z = ␈↓¬DEFPROP␈↓↓ ∧ ␈↓αaddd|␈↓↓z = ␈↓¬EXPR␈↓↓] ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         [␈↓αprogram␈↓↓ [prog]␈↓
␈↓ ↓H␈↓4.1)␈↓ α8␈↓↓               prog␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ← [␈↓αif␈↓↓ ␈↓αa|␈↓↓z = ␈↓¬DEFUN␈↓↓ ␈↓αthen␈↓↓ comp[␈↓αad|␈↓↓z, ␈↓αadd|␈↓↓z, ␈↓αaddd|␈↓↓z]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                   ␈↓αelse␈↓↓ comp[␈↓αad|␈↓↓z, ␈↓αad|␈↓↓␈↓αadd|␈↓↓z, ␈↓αadd|␈↓↓␈↓αadd|␈↓↓z]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               unselect-tty ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 select-disk-output mapc[print, prog]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓               print <␈↓αad|␈↓↓z, length prog>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        ␈↓αelse␈↓↓ unselect-tty select-disk-output print z], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     apply[␈↓¬UFILE␈↓↓, <␈↓αa|␈↓↓file, ␈↓¬LAP␈↓↓>], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     ␈↓¬ENDCOMP␈↓↓]]␈↓

␈↓ ↓H␈↓        Instead␈α∞of␈α∂␈↓↓append␈↓ing␈α∞the␈α∂instructions␈α∞into␈α∂a␈α∞list␈α∂like␈α∞␈↓↓compexp␈↓␈α∂of␈α∞LCOM0␈α∂does,␈α∞LCOM4
␈↓ ↓H␈↓␈↓↓cons␈↓es␈αthem␈αinto␈αa␈α
tree␈αform␈αand␈αflattens␈αthe␈α
final␈αversion␈αinto␈αa␈αlist.␈α
 In␈αorder␈αthat␈αthe␈α
items␈αof
␈↓ ↓H␈↓the␈αtree␈αform␈αall␈αhave␈αthe␈αstructure␈αof␈αa␈αlist␈α
of␈αatoms,␈αlabels␈αand␈αthe␈αend␈αmark␈α␈↓¬NIL␈↓␈αare␈αput␈α
in␈αa
␈↓ ↓H␈↓list␈α⊃flagged␈α⊂by␈α⊃␈↓¬LABEL␈α⊂␈↓and␈α⊃extracted␈α⊂by␈α⊃␈↓↓flat.␈↓␈α⊂ The␈α⊃hope␈α⊂was␈α⊃to␈α⊂make␈α⊃the␈α⊂compiler␈α⊃faster␈α⊂by
␈↓ ↓H␈↓omitting all the calls to ␈↓↓append␈↓ but it does not seem to have made a lot of difference.

␈↓ ↓H␈↓␈↓ α8␈↓↓comp[fn, vars, exp] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  {prup[vars, 1], length vars}[λvpr, n: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    flat[<<<␈↓¬LAP␈↓↓, fn, ␈↓¬SUBR␈↓↓>>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          mkpush[n, 1], ␈↓
␈↓ ↓H␈↓4.2)␈↓ α8␈↓↓          compexp[exp, -n, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          substack n, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓          ␈↓¬((POPJ P) (LABEL NIL))␈↓↓>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         ␈↓¬NIL␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter  IX␈↓ ≠177


␈↓ ↓H␈↓␈↓ α8␈↓↓flat[u, s] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ s␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αa|␈↓↓u ␈↓αthen␈↓↓ flat[␈↓αd|␈↓↓u, s]␈↓
␈↓ ↓H␈↓4.3)␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬LABEL␈↓↓ ␈↓αthen␈↓↓ ␈↓αad|␈↓↓u . s␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓u ␈↓αthen␈↓↓ u . s␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓u, flat[␈↓αd|␈↓↓u, s]]␈↓

␈↓ ↓H␈↓        ␈↓↓substack␈↓␈αcreates␈αcode␈αto␈αmove␈αdown␈αthe␈αstack,␈αbut␈αcreates␈αno␈αcode␈αfor␈αthe␈αcase␈α␈↓↓n␈↓␈α=␈α0␈αwhich
␈↓ ↓H␈↓is a no-op.

␈↓ ↓H␈↓␈↓ α8␈↓↓substack n ← ␈↓
␈↓ ↓H␈↓4.4)␈↓ α8␈↓↓  ␈↓αif␈↓↓ n = 0 ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ <<␈↓¬SUB␈↓↓, ␈↓¬P␈↓↓, <␈↓¬%␈↓↓, 0, 0, n, n>>>␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓prup[vars, n] ← ␈↓
␈↓ ↓H␈↓4.5)␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓vars ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [␈↓αa|␈↓↓vars . n] . prup[␈↓αd|␈↓↓vars, add1 n]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓mkpush[n, m] ← ␈↓
␈↓ ↓H␈↓4.6)␈↓ α8␈↓↓  ␈↓αif␈↓↓ n < m ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ <␈↓¬PUSH␈↓↓, ␈↓¬P␈↓↓, m> . mkpush[n, add1 m]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓compexp[exp, m, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓exp ␈↓αthen␈↓↓ ␈↓¬((MOVEI 1 0))␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ exp = ␈↓¬T␈↓↓ ∨ numberp exp ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <<␈↓¬MOVEI␈↓↓, 1, <␈↓¬QUOTE␈↓↓, exp>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓exp ␈↓αthen␈↓↓ <<␈↓¬MOVE␈↓↓, 1, m + ␈↓αd|␈↓↓assoc[exp, vpr], ␈↓¬P␈↓↓>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬CAR␈↓↓ ␈↓αthen␈↓↓ ␈↓␈↓ λx~compute ␈↓↓car␈↓ directly
␈↓ ↓H␈↓␈↓ α8␈↓↓    [␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αad|␈↓↓exp ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      <<␈↓¬HLRZ␈↓↓, 1, ␈↓¬@␈↓↓, m + ␈↓αd|␈↓↓assoc[␈↓αad|␈↓↓exp, vpr], ␈↓¬P␈↓↓>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     ␈↓αelse␈↓↓ <compexp[␈↓αad|␈↓↓exp, m, vpr], ␈↓¬((HLRZ 1 @ 1))␈↓↓>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬CDR␈↓↓ ␈↓αthen␈↓↓ ␈↓␈↓ λx~compute ␈↓↓cdr␈↓ directly
␈↓ ↓H␈↓␈↓ α8␈↓↓    [␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αad|␈↓↓exp ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      <<␈↓¬HRRZ␈↓↓, 1, ␈↓¬@␈↓↓, m + ␈↓αd|␈↓↓assoc[␈↓αad|␈↓↓exp, vpr], ␈↓¬P␈↓↓>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     ␈↓αelse␈↓↓ <compexp[␈↓αad|␈↓↓exp, m, vpr], ␈↓¬((HRRZ 1 @ 1))␈↓↓>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬AND␈↓↓ ∨ ␈↓αa|␈↓↓exp = ␈↓¬OR␈↓↓ ∨ ␈↓αa|␈↓↓exp = ␈↓¬NOT␈↓↓ ∨ ␈↓αa|␈↓↓exp = ␈↓¬EQ␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    ␈↓αthen␈↓↓ {gensym[], gensym[]}[λl1, l2: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      <combool[exp, m, l1, ␈↓¬NIL␈↓↓, vpr], ␈↓
␈↓ ↓H␈↓4.7)␈↓ α8␈↓↓       <␈↓¬(MOVEI 1 (QUOTE T))␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        <␈↓¬JRST␈↓↓, 0, l2>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        <␈↓¬LABEL␈↓↓, l1>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        ␈↓¬(MOVEI 1 0)␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        <␈↓¬LABEL␈↓↓, l2>>>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬COND␈↓↓ ␈↓αthen␈↓↓ comcond[␈↓αd|␈↓↓exp, m, gensym[], vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬QUOTE␈↓↓ ␈↓αthen␈↓↓ <<␈↓¬MOVEI␈↓↓, 1, exp>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓exp ␈↓αthen␈↓↓ ␈↓␈↓ λx~uses improved
␈↓ ↓H␈↓␈↓ α8␈↓↓    <complisa[␈↓αd|␈↓↓exp, m, vpr], ␈↓␈↓ λx~argument evaluation
␈↓ ↓H␈↓␈↓ α8␈↓↓     <<␈↓¬CALL␈↓↓, length ␈↓αd|␈↓↓exp, <␈↓¬QUOTE␈↓↓, ␈↓αa|␈↓↓exp>>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓exp = ␈↓¬LAMBDA␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    {length ␈↓αd|␈↓↓exp}[λn: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓      <stackup[␈↓αd|␈↓↓exp, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓       compexp[␈↓αadda|␈↓↓exp, m - n, prup[␈↓αada|␈↓↓exp, 1 - m] * vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓       substack n>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓178␈↓ ¬wChapter  IX␈↓ H


␈↓ ↓H␈↓␈↓ α8␈↓↓stackup[u, m, vpr] ← ␈↓␈↓ λx␈↓↓~␈↓ ≡ ␈↓↓complis␈↓ of LCOM0.
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓4.8)␈↓ α8␈↓↓  ␈↓αelse␈↓↓ <compexp[␈↓αa|␈↓↓u, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         ␈↓¬((PUSH P 1))␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         stackup[␈↓αd|␈↓↓u, sub1 m, vpr]>␈↓

␈↓ ↓H␈↓        ␈↓↓ccchain␈↓ test whether an expression is a ␈↓↓car␈↓ - ␈↓↓cdr␈↓ chain applied to a variable.

␈↓ ↓H␈↓␈↓ α8␈↓↓ccchain exp ← ␈↓
␈↓ ↓H␈↓4.9)␈↓ α8␈↓↓  [␈↓αa|␈↓↓exp = ␈↓¬CAR␈↓↓ ∨ ␈↓αa|␈↓↓exp = ␈↓¬CDR␈↓↓] ∧ [␈↓αat|␈↓↓␈↓αad|␈↓↓exp ∨ ccchain ␈↓αad|␈↓↓exp]␈↓

␈↓ ↓H␈↓        ␈↓↓compc␈↓␈α⊂generates␈α∂code␈α⊂(in␈α∂reverse␈α⊂order)␈α⊂to␈α∂load␈α⊂the␈α∂value␈α⊂of␈α⊂a␈α∂␈↓↓car␈↓␈α⊂-␈α∂␈↓↓cdr␈↓␈α⊂chain␈α⊂into␈α∂the
␈↓ ↓H␈↓accumulator designated by ␈↓↓n2.␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓compc[exp, n2, m, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αat|␈↓↓exp ␈↓αthen␈↓↓ error ␈↓¬COMPC␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬CAR␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    [␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αad|␈↓↓exp ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓4.10)␈↓ α8␈↓↓      <<␈↓¬HLRZ␈↓↓, n2, ␈↓¬@␈↓↓, m + ␈↓αd|␈↓↓assoc[␈↓αad|␈↓↓exp, vpr], ␈↓¬P␈↓↓>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     ␈↓αelse␈↓↓ <␈↓¬HLRZ␈↓↓, n2, ␈↓¬@␈↓↓, n2> . compc[␈↓αad|␈↓↓exp, n2, m, vpr]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αad|␈↓↓exp ␈↓αthen␈↓↓ ␈↓
∨A ↓H␈↓␈↓ α8␈↓↓    <<␈↓¬HRRZ␈↓↓, ␈↓¬@␈↓↓, n2, m + ␈↓αd|␈↓↓assoc[␈↓αad|␈↓↓exp, vpr], ␈↓¬P␈↓↓>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ <␈↓¬HRRZ␈↓↓, n2, ␈↓¬@␈↓↓, n2> . compc[␈↓αad|␈↓↓exp, n2, m, vpr]␈↓

␈↓ ↓H␈↓        ␈↓↓comcond␈↓␈αis␈αessentially␈αthe␈αsame␈αas␈αin␈αLCOM0.␈α It␈αoptimizes␈αby␈αconsidering␈αthe␈αspecial␈αcases
␈↓ ↓H␈↓where␈αthe␈αnext␈αclause␈αin␈αthe␈αlist␈αof␈αarguments␈αto␈α␈↓¬COND␈α␈↓␈αis␈αof␈αthe␈αform␈α␈↓¬((NULL ␈↓↓e␈↓¬) NIL)␈↓␈α or␈α␈↓¬(T ␈↓↓e␈↓¬)␈↓
␈↓ ↓H␈↓for some expression ␈↓↓e.␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓comcond[u, m, l, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ <<␈↓¬LABEL␈↓↓, l>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ¬␈↓αat|␈↓↓␈↓αaa|␈↓↓u ∧ ␈↓αaaa|␈↓↓u = ␈↓¬NULL␈↓↓ ∧ ␈↓αn|␈↓↓␈↓αada|␈↓↓u ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <compexp[␈↓αadaa|␈↓↓u, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     <<␈↓¬JUMPE␈↓↓, 1, l>>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     comcond[␈↓αd|␈↓↓u, m, l, vpr]>␈↓
␈↓ ↓H␈↓4.11)␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓u = ␈↓¬T␈↓↓ ␈↓αthen␈↓↓ <compexp[␈↓αada|␈↓↓u, m, vpr], <<␈↓¬LABEL␈↓↓, l>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ {gensym[]}[λl1: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <combool[␈↓αaa|␈↓↓u, m, l1, ␈↓¬NIL␈↓↓, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     compexp[␈↓αada|␈↓↓u, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     <<␈↓¬JRST␈↓↓, 0, l>, <␈↓¬LABEL␈↓↓, l1>>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     comcond[␈↓αd|␈↓↓u, m, l, vpr]>]␈↓

␈↓ ↓H␈↓        ␈↓↓complisa␈↓␈αclassifies␈αthe␈αexpressions␈αon␈αthe␈αargument␈αlist␈α␈↓↓u,␈↓␈αcalls␈α␈↓↓complis␈↓␈αto␈αgenerate␈αcode␈αfor
␈↓ ↓H␈↓those␈α∞arguments␈α∂which␈α∞must␈α∂be␈α∞computed␈α∂and␈α∞stored␈α∂on␈α∞the␈α∂stack,␈α∞␈↓↓loadac␈↓␈α∂to␈α∞generate␈α∂code␈α∞for
␈↓ ↓H␈↓loading the accumulators and ␈↓↓substack␈↓ to generate code to restore the stack.

␈↓ ↓H␈↓␈↓ α8␈↓↓complisa[u, m, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  {classify u}[λz: ␈↓
␈↓ ↓H␈↓4.12)␈↓ α8␈↓↓    <complis[z, m, 1, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     loadac[z, 1 - ccount z, 1, m - ccount z, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     substack ccount z>]␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter  IX␈↓ ≠179


␈↓ ↓H␈↓        ␈↓↓ccount␈↓␈αof␈α
a␈αlist␈α
of␈αclassified␈αexpressions␈α
is␈αthe␈α
number␈αof␈αexpressions␈α
on␈αthe␈α
list␈αof␈α
class␈α4.
␈↓ ↓H␈↓That is the number of values that will have to be stacked when evaluating ␈↓↓z␈↓ as an argument list.

␈↓ ↓H␈↓␈↓ α8␈↓↓ccount z ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓z ␈↓αthen␈↓↓ 0␈↓
␈↓ ↓H␈↓4.13)␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 4 ␈↓αthen␈↓↓ add1 ccount ␈↓αd|␈↓↓z␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ccount ␈↓αd|␈↓↓z␈↓

␈↓ ↓H␈↓        ␈↓↓loadac␈↓␈αcompiles␈αcode␈αto␈αload␈αaccumulators␈α␈↓↓n2␈↓␈αand␈αup␈αwith␈αthe␈αvalues␈αof␈αthe␈αexpressions␈αon
␈↓ ↓H␈↓the␈α⊃list␈α∩␈↓↓z␈↓␈α⊃of␈α∩classified␈α⊃expressions.␈α∩ ␈↓↓m2␈↓␈α⊃is␈α∩the␈α⊃stack␈α⊃offset␈α∩for␈α⊃the␈α∩value␈α⊃of␈α∩the␈α⊃next␈α∩class␈α⊃4
␈↓ ↓H␈↓expression on the list.

␈↓ ↓H␈↓␈↓ α8␈↓↓loadac[z, m2, n2, m, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓z ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 1 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <␈↓¬MOVE␈↓↓, n2, m + ␈↓αd|␈↓↓assoc[␈↓αda|␈↓↓z, vpr], ␈↓¬P␈↓↓>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    . loadac[␈↓αd|␈↓↓z, m2, add1 n2, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 0 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <␈↓¬MOVEI␈↓↓, n2, <␈↓¬QUOTE␈↓↓, ␈↓αda|␈↓↓z>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    . loadac[␈↓αd|␈↓↓z, m2, add1 n2, m, vpr]␈↓
␈↓ ↓H␈↓4.14)␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 2 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <␈↓¬MOVEI␈↓↓, n2, ␈↓αda|␈↓↓z> . loadac[␈↓αd|␈↓↓z, m2, add1 n2, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 3 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <reverse compc[␈↓αda|␈↓↓z, n2, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     loadac[␈↓αd|␈↓↓z, m2, add1 n2, m, vpr]>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 5 ␈↓αthen␈↓↓ loadac[␈↓αd|␈↓↓z, 1, add1 n2, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ <␈↓¬MOVE␈↓↓, n2, m2, ␈↓¬P␈↓↓>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        . loadac[␈↓αd|␈↓↓z, add1 m2, add1 n2, m, vpr]␈↓

␈↓ ↓H␈↓        ␈↓↓complis␈↓␈αgenerates␈αcode␈αto␈αstack␈αthe␈αvalues␈αof␈αthe␈αclass␈α4␈αexpressions␈αon␈αthe␈αlist␈αof␈αclassified
␈↓ ↓H␈↓expressions␈α␈↓↓z.␈↓␈α ␈↓↓k␈↓␈αis␈αthe␈αaccumulator␈αwhere␈αthe␈αnext␈αvalue␈αshould␈αgo.␈α The␈αlast␈αclass 4␈αexpression
␈↓ ↓H␈↓is␈α∞marked␈α∞class 5.␈α∞ When␈α∞this␈α∞is␈α∞evaluated␈α∞it␈α∞is␈α∞loaded␈α∞directly␈α∞rather␈α∞than␈α∞being␈α∞stacked.␈α∞ If␈α
a
␈↓ ↓H␈↓class 5 expression is encountered, ␈↓↓complis␈↓ can quit as it has no more work.

␈↓ ↓H␈↓␈↓ α8␈↓↓complis[z, m, k, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓z ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 4 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <compexp[␈↓αda|␈↓↓z, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     ␈↓¬((PUSH P 1))␈↓↓, ␈↓
␈↓ ↓H␈↓4.15)␈↓ α8␈↓↓     complis[␈↓αd|␈↓↓z, sub1 m, add1 k, vpr]>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 5 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <compexp[␈↓αda|␈↓↓z, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     ␈↓αif␈↓↓ k = 1 ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ <<␈↓¬MOVE␈↓↓, k, 1>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ complis[␈↓αd|␈↓↓z, m, add1 k, vpr]␈↓

␈↓ ↓H␈↓        ␈↓↓classify␈↓␈αtakes␈αa␈αlist␈αof␈αexpressions␈αand␈αreturns␈αa␈αcorresponding␈αlist␈αof␈α
classified␈αexpressions
␈↓ ↓H␈↓of␈αthe␈αform␈α[␈↓↓n␈↓␈α
.␈α␈↓↓e]␈↓␈αwhere␈α␈↓↓n␈↓␈α
is␈αthe␈αclass␈αof␈α␈↓↓e␈↓␈α
according␈αto␈αthe␈αrules␈α
given␈αat␈αthe␈αbeginning␈α
of␈αthe
␈↓ ↓H␈↓listing.  The last class 4 expression on the list is numbered 5.

␈↓ ↓H␈↓4.16)␈↓ α8␈↓↓classify u ← class2[class1[u, ␈↓¬NIL␈↓↓], ␈↓¬NIL␈↓↓, ␈↓¬T␈↓↓]␈↓
␈↓ ↓H␈↓180␈↓ ¬wChapter  IX␈↓ H


␈↓ ↓H␈↓␈↓ α8␈↓↓class1[u, v] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓u ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    [␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬NIL␈↓↓ ∨ ␈↓αa|␈↓↓u = ␈↓¬T␈↓↓ ∨ numberp ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓4.17)␈↓ α8␈↓↓      class1[␈↓αd|␈↓↓u, [0 . ␈↓αa|␈↓↓u] . v]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     ␈↓αelse␈↓↓ class1[␈↓αd|␈↓↓u, [1 . ␈↓αa|␈↓↓u] . v]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓u = ␈↓¬QUOTE␈↓↓ ␈↓αthen␈↓↓ class1[␈↓αd|␈↓↓u, [2 . ␈↓αa|␈↓↓u] . v]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ccchain ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ class1[␈↓αd|␈↓↓u, [3 . ␈↓αa|␈↓↓u] . v]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ class1[␈↓αd|␈↓↓u, [4 . ␈↓αa|␈↓↓u] . v]␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓class2[u, v, flg] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v␈↓
␈↓ ↓H␈↓4.18)␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ flg ∧ ␈↓αaa|␈↓↓u = 4 ␈↓αthen␈↓↓ class2[␈↓αd|␈↓↓u, [5 . ␈↓αda|␈↓↓u] . v, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ class2[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . v, flg]␈↓

␈↓ ↓H␈↓4.19)␈↓ α8␈↓↓mkjrst l ← <<␈↓¬JRST␈↓↓, 0, l>>␈↓

␈↓ ↓H␈↓        ␈↓↓combool␈↓␈α
is␈α
essentially␈αthe␈α
same␈α
as␈α
in␈αLCOM0␈α
except␈α
it␈α
treats␈αadditional␈α
special␈α
cases␈αwhere␈α
␈↓↓p␈↓
␈↓ ↓H␈↓is␈α∂of␈α∂the␈α∂form␈α∂␈↓¬T␈↓,␈α∂␈↓¬(EQ␈α∂␈↓↓e1␈↓¬␈α∂␈↓↓e2␈↓¬)␈↓,␈α⊂or␈α∂␈↓¬(NULL␈α∂␈↓↓e␈↓¬)␈↓.␈α∂ It␈α∂also␈α∂handles␈α∂the␈α∂jumping␈α⊂somewhat␈α∂differently
␈↓ ↓H␈↓using two versions of ␈↓↓compandor.␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓combool[p, m, l, flg, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ p = ␈↓¬T␈↓↓ ␈↓αthen␈↓↓ [␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ mkjrst l ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓p ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <compexp[p, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     <<␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ ␈↓¬JUMPN␈↓↓ ␈↓αelse␈↓↓ ␈↓¬JUMPE␈↓↓, 1, l>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬EQ␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <complisa[␈↓αd|␈↓↓p, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     ␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ ␈↓¬((CAMN 1 2))␈↓↓ ␈↓αelse␈↓↓ ␈↓¬((CAME 1 2))␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     mkjrst l>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬AND␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    [␈↓αif␈↓↓ ¬flg ␈↓αthen␈↓↓ compandor[␈↓αd|␈↓↓p, m, l, ␈↓¬NIL␈↓↓, vpr]␈↓
␈↓ ↓H␈↓4.20)␈↓ α8␈↓↓     ␈↓αelse␈↓↓ {gensym[]}[λl1: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓       <compandor1[␈↓αd|␈↓↓p, m, l1, l, ␈↓¬NIL␈↓↓, vpr], <<␈↓¬LABEL␈↓↓, l1>>>]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬OR␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    [␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ compandor[␈↓αd|␈↓↓p, m, l, ␈↓¬T␈↓↓, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     ␈↓αelse␈↓↓ {gensym[]}[λl1: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓       <compandor1[␈↓αd|␈↓↓p, m, l1, l, ␈↓¬T␈↓↓, vpr], <<␈↓¬LABEL␈↓↓, l1>>>]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬NOT␈↓↓ ␈↓αthen␈↓↓ combool[␈↓αad|␈↓↓p, m, l, ¬flg, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬NULL␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓    <compexp[␈↓αad|␈↓↓p, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓     <<␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ ␈↓¬JUMPE␈↓↓ ␈↓αelse␈↓↓ ␈↓¬JUMPN␈↓↓, 1, l>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ <compexp[p, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         <<␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ ␈↓¬JUMPN␈↓↓ ␈↓αelse␈↓↓ ␈↓¬JUMPE␈↓↓, 1, l>>>␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓compandor[u, m, l, flg, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓4.21)␈↓ α8␈↓↓  ␈↓αelse␈↓↓ <combool[␈↓αa|␈↓↓u, m, l, flg, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         compandor[␈↓αd|␈↓↓u, m, l, flg, vpr]>␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓compandor1[u, m, l, l2, flg, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter  IX␈↓ ≠181


␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ mkjrst l2␈↓
␈↓ ↓H␈↓4.22)␈↓ α8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ combool[␈↓αa|␈↓↓u, m, l2, ¬flg, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓  ␈↓αelse␈↓↓ <combool[␈↓αa|␈↓↓u, m, l, flg, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         compandor1[␈↓αd|␈↓↓u, m, l, l2, flg, vpr]>␈↓
␈↓ ↓H␈↓182␈↓ εH␈↓ H


␈↓ ↓H␈↓α␈↓ εαChapter X

␈↓ ↓H␈↓α␈↓ ¬BCOMPUTABILITY




␈↓ ↓H␈↓1.  ␈↓αA Call-by-name LISP interpreter.␈↓


␈↓ ↓H␈↓        Except␈αfor␈αspeed␈αand␈αmemory␈αsize␈αall␈αpresent␈αday␈αstored␈αprogram␈αcomputers␈αare␈αequivalent
␈↓ ↓H␈↓in␈αwhat␈αcomputations␈αthey␈αcan␈αdo.␈α A␈αprogram␈αwritten␈αfor␈αone␈αcomputer␈αcan␈αbe␈αtranslated␈αto␈αrun
␈↓ ↓H␈↓on␈αanother.␈α Indeed,␈αone␈αcan␈αwrite␈αa␈αsimulator␈αfor␈αone␈αcomputer␈αto␈αrun␈αon␈αanother.␈α To␈αput␈αit␈αin
␈↓ ↓H␈↓commercial␈α
terms,␈α
no␈α
computer␈α
manufacturer␈α
can␈α
advertise␈α
that␈α
his␈α
machine␈α
can␈α
do␈αcalculations
␈↓ ↓H␈↓impossible on the machine made by his competitors.

␈↓ ↓H␈↓        This␈αis␈αwell␈αknown␈αintuitively,␈αand␈αthe␈αfirst␈αmathematical␈αtheorem␈αof␈αthis␈αkind␈αwas␈αproved
␈↓ ↓H␈↓by␈αA.M.␈αTuring␈α(1936),␈αwho␈αdefined␈αa␈αprimitive␈αkind␈αof␈αcomputer␈αnow␈αcalled␈αa␈αTuring␈αmachine,
␈↓ ↓H␈↓and␈α⊃showed␈α⊃how␈α⊃to␈α⊃make␈α⊂a␈α⊃universal␈α⊃machine␈α⊃that␈α⊃could␈α⊂do␈α⊃any␈α⊃computation␈α⊃done␈α⊃by␈α⊂any
␈↓ ↓H␈↓Turing␈αmachine␈αwhen␈αgiven␈αa␈αdescription␈αof␈αthe␈αmachine␈αto␈αbe␈αsimulated␈αand␈αthe␈αinitial␈αtape␈αof
␈↓ ↓H␈↓the computation to be imitated.

␈↓ ↓H␈↓        In␈αLISP␈α
the␈αfunction␈α␈↓↓eval,␈↓␈α
defined␈αin␈α
Chapter I␈α(I.13.1)␈αis␈α
a␈αuniversal␈α
LISP␈αfunction␈αin␈α
the
␈↓ ↓H␈↓sense␈α
that␈α
any␈α
computation␈α
done␈αby␈α
any␈α
LISP␈α
function␈α
can␈α
be␈αdone␈α
by␈α
␈↓↓eval␈↓␈α
when␈α
␈↓↓eval␈↓␈α
is␈αgiven
␈↓ ↓H␈↓suitable arguments.

␈↓ ↓H␈↓        The␈α⊂way␈α⊂␈↓↓eval␈↓␈α⊂handles␈α⊂arguments␈α∂corresponds␈α⊂to␈α⊂the␈α⊂call-by-value␈α⊂method␈α⊂of␈α∂parameter
␈↓ ↓H␈↓passing␈αin␈αALGOL␈αand␈αsimilar␈αlanguages.␈α There␈αis␈αalso␈αa␈αform␈αof␈α␈↓↓eval␈↓␈αthat␈αcorresponds␈αto␈αcall-
␈↓ ↓H␈↓by-name.  Here it is:

␈↓ ↓H␈↓␈↓ α8␈↓↓        neval[e, a] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        ␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                [␈↓αif␈↓↓ e = ␈↓¬T␈↓↓  ␈↓αthen␈↓↓ ␈↓¬T␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ␈↓αelse␈↓↓ ␈↓αif␈↓↓ e = ␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ␈↓αelse␈↓↓ ␈↓αif␈↓↓ numberp e ␈↓αthen␈↓↓ e␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ␈↓αelse␈↓↓ neval[␈↓αad|␈↓↓assoc[e, a], ␈↓αdd|␈↓↓assoc[e, a]]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓e ␈↓αthen␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                [␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CAR ␈↓↓␈↓αthen␈↓↓ ␈↓αa|␈↓↓neval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CDR ␈↓↓␈↓αthen␈↓↓ ␈↓αd|␈↓↓neval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓1.1)␈↓ α8␈↓↓             ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CONS ␈↓↓␈↓αthen␈↓↓ neval[␈↓αad|␈↓↓e, a] . neval[␈↓αadd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬ATOM ␈↓↓␈↓αthen␈↓↓ ␈↓αat|␈↓↓neval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬EQ ␈↓↓␈↓αthen␈↓↓ neval[␈↓αad|␈↓↓e, a] = neval[␈↓αadd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬QUOTE ␈↓↓␈↓αthen␈↓↓ ␈↓αad|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬COND ␈↓↓␈↓αthen␈↓↓ nevcon[␈↓αd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬LIST ␈↓↓␈↓αthen␈↓↓ mapcar[␈↓αd|␈↓↓e, λx: neval[x, a]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                ␈↓αelse␈↓↓ neval[␈↓αd|␈↓↓assoc[␈↓αa|␈↓↓e, a] . ␈↓αd|␈↓↓e, a]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓e = ␈↓¬LAMBDA ␈↓↓␈↓αthen␈↓↓ neval[␈↓αadda|␈↓↓e, nprup[␈↓αada|␈↓↓e, ␈↓αd|␈↓↓e,  a]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓e = ␈↓¬LABEL ␈↓↓␈↓αthen␈↓↓ neval[␈↓αadda|␈↓↓e . ␈↓αd|␈↓↓e, [␈↓αada|␈↓↓e . ␈↓αa|␈↓↓e] . a]␈↓,

␈↓ ↓H␈↓where the auxiliary function ␈↓↓nevcon␈↓ is given by
␈↓ ↓H␈↓␈↓ ¬|Chapter  X␈↓ ≠183


␈↓ ↓H␈↓1.2)␈↓ αp␈↓↓nevcon[u, a] ← ␈↓αif␈↓↓ neval[␈↓αaa|␈↓↓u, a] ␈↓αthen␈↓↓ neval[␈↓αada|␈↓↓u, a] ␈↓αelse␈↓↓ nevcon[␈↓αd|␈↓↓u, a]␈↓. 

␈↓ ↓H␈↓and ␈↓↓nprup␈↓ is

␈↓ ↓H␈↓1.3)␈↓ αy␈↓↓nprup[u, v,a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ [␈↓αa|␈↓↓u . [␈↓αa|␈↓↓v . a]] . nprup[␈↓αd|␈↓↓u, ␈↓αd|␈↓↓v,a]␈↓. 

␈↓ ↓H␈↓        The␈α
difference␈α
between␈α∞␈↓↓eval␈↓␈α
and␈α
␈↓↓neval␈↓␈α
is␈α∞only␈α
in␈α
two␈α
terms.␈α∞ ␈↓↓eval␈↓␈α
evaluates␈α
a␈α∞variable␈α
by
␈↓ ↓H␈↓looking␈αit␈αup␈αon␈αthe␈αassociation␈αlist␈αwhereas␈α
␈↓↓neval␈↓␈αlooks␈αit␈αup␈αon␈αthe␈αassociation␈αlist␈αand␈α
evaluates
␈↓ ↓H␈↓the␈αresult␈αin␈αthe␈αcontext␈αin␈αwhich␈αit␈αwas␈αput␈αon␈αthe␈αassociation␈αlist.␈α Correspondingly,␈αwhen␈αa␈αλ-
␈↓ ↓H␈↓expression␈α
is␈αencountered,␈α
␈↓↓eval␈↓␈α
forms␈αa␈α
new␈α
association␈αlist␈α
by␈α
pairing␈αthe␈α
values␈α
of␈αthe␈α
arguments
␈↓ ↓H␈↓with␈αthe␈α
variables␈αbound␈αby␈α
the␈αλ␈αand␈α
putting␈αthe␈αnew␈α
pairs␈αin␈αfront␈α
of␈αthe␈αold␈α
association␈αlist,
␈↓ ↓H␈↓whereas␈α
␈↓↓neval␈↓␈α
pairs␈α
the␈α
arguments␈α
themselves␈α
with␈αthe␈α
variables␈α
and␈α
puts␈α
them␈α
on␈α
the␈α
front␈αof
␈↓ ↓H␈↓the␈α
association␈αlist.␈α
 The␈α
function␈α␈↓↓neval␈↓␈α
also␈α
saves␈αthe␈α
current␈α
association␈αlist␈α
with␈α
each␈αvariable
␈↓ ↓H␈↓on␈αthe␈αassociation␈αlist,␈αso␈αthat␈αthe␈αvariables␈αcan␈αbe␈αevaluated␈αin␈αthe␈αcorrect␈αcontext.␈α In␈αmost␈αcases
␈↓ ↓H␈↓they␈α⊂give␈α∂the␈α⊂same␈α∂result␈α⊂with␈α∂the␈α⊂same␈α∂work,␈α⊂but␈α∂␈↓↓neval␈↓␈α⊂gives␈α∂a␈α⊂result␈α∂for␈α⊂some␈α⊂functions␈α∂of
␈↓ ↓H␈↓several␈αarguments␈αfor␈αwhich␈α␈↓↓eval␈↓␈αloops.␈α An␈αexample␈αis␈αobtained␈αby␈αevaluating␈α␈↓↓F[2,␈α1]␈↓␈αwhere␈α␈↓↓F␈↓
␈↓ ↓H␈↓is defined by

␈↓ ↓H␈↓␈↓ ∧
␈↓↓F[x, y] ← ␈↓αif␈↓↓ x=0 ␈↓αthen␈↓↓ 0 ␈↓αelse␈↓↓ F[x-1, F[y-2, x]]␈↓. 

␈↓ ↓H␈↓        Vuillemin␈α
[1973]␈α
showed␈α
that␈α
if␈α
a␈α
function␈α
is␈α
␈↓↓strict,␈↓␈α
never␈α
has␈α
a␈α
defined␈α
value␈α
unless␈α
all
␈↓ ↓H␈↓arguments have a defined value, then call-by-name and call-by-value always give the same result.


␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.␈α⊂Write␈α⊃␈↓↓neval␈↓␈α⊂and␈α⊂the␈α⊃necessary␈α⊂auxiliary␈α⊃functions␈α⊂in␈α⊂list␈α⊃form,␈α⊂and␈α⊂try␈α⊃them␈α⊂out␈α⊃on␈α⊂some
␈↓ ↓H␈↓examples.



␈↓ ↓H␈↓2.  ␈↓αNon-computability.␈↓


␈↓ ↓H␈↓        Some␈α∞LISP␈α
calculations␈α∞run␈α∞on␈α
indefinitely.␈α∞ The␈α∞most␈α
trivial␈α∞case␈α∞occurs␈α
if␈α∞we␈α∞make␈α
the
␈↓ ↓H␈↓recursive definition

␈↓ ↓H␈↓2.1)␈↓ ¬(␈↓↓loop x ← loop x             ␈↓ 

␈↓ ↓H␈↓and␈α
attempt␈α
to␈α
compute␈α
␈↓↓loop[x]␈↓␈α
for␈α
any␈α
␈↓↓x␈↓␈α
whatsoever.␈α
 Don't␈α
dismiss␈α
this␈α
example␈α
just␈αbecause␈α
no-
␈↓ ↓H␈↓one␈αwould␈αwrite␈αsuch␈αan␈αobviously␈αuseless␈αfunction␈αdefinition.␈α There␈αis␈αa␈αsense␈αin␈αwhich␈αit␈αis␈α
the
␈↓ ↓H␈↓"zero"␈α∞of␈α∞a␈α
large␈α∞class␈α∞of␈α
non-terminating␈α∞function␈α∞definitions,␈α
and,␈α∞as␈α∞the␈α∞Romans␈α
experienced
␈↓ ↓H␈↓but never learned, leaving zero out of the number system is a mistake.

␈↓ ↓H␈↓        Nevertheless,␈αin␈αmost␈α
programming,␈αnon-terminating␈αcalculations␈αare␈α
the␈αresults␈αof␈αerrors␈α
in
␈↓ ↓H␈↓defining␈αfunctions.␈α Therefore,␈αit␈αwould␈αbe␈αuseful␈αto␈αbe␈αable␈αto␈αtell␈αwhether␈αa␈αfunction␈αdefinition
␈↓ ↓H␈↓gives␈αa␈αresult␈α
for␈αall␈αarguments.␈α In␈α
fact,␈αit␈αwould␈αbe␈α
useful␈αto␈αbe␈αable␈α
to␈αtell␈αwhether␈α
a␈αfunction
␈↓ ↓H␈↓will terminate for a single argument.  Let us make this goal more precise.

␈↓ ↓H␈↓        Suppose␈α∞that␈α
␈↓↓f␈↓␈α∞is␈α∞a␈α
LISP␈α∞function␈α∞and␈α
␈↓↓a␈↓␈α∞is␈α∞an␈α
S-expression,␈α∞and␈α∞we␈α
would␈α∞like␈α∞to␈α
know
␈↓ ↓H␈↓184␈↓ ¬|Chapter  X␈↓ H


␈↓ ↓H␈↓whether␈α
the␈α
computation␈αof␈α
␈↓↓f[a]␈↓␈α
terminates.␈α
 Suppose␈α␈↓↓f␈↓␈α
is␈α
represented␈αby␈α
the␈α
S-expression␈α
␈↓↓f*␈↓␈αin
␈↓ ↓H␈↓internal␈α∞notation.␈α
 Then␈α∞the␈α
S-expression␈α∞␈↓¬(␈↓↓f*␈↓¬␈α
(QUOTE␈α∞␈↓↓a␈↓¬))␈↓␈α
represents␈α∞␈↓↓f[a].␈↓␈α
 Define␈α∞the␈α
function
␈↓ ↓H␈↓␈↓↓term␈↓␈α∞by␈α∞giving␈α∞␈↓↓term[e]␈↓␈α∂the␈α∞value␈α∞␈↓αtrue␈↓␈α∞if␈α∂␈↓↓e␈↓␈α∞is␈α∞an␈α∞S-expression␈α∞of␈α∂the␈α∞form␈α∞␈↓¬(␈↓↓f*␈↓¬␈α∞(QUOTE␈α∂␈↓↓a␈↓¬))␈↓␈α∞for
␈↓ ↓H␈↓which␈α␈↓↓f[a]␈↓␈αterminates␈αand␈α␈↓αfalse␈↓␈αotherwise.␈α We␈αnow␈αask␈αwhether␈α␈↓↓term␈↓␈αis␈αa␈αLISP␈αfunction,␈αi.e.␈αcan
␈↓ ↓H␈↓it␈αbe␈αconstructed␈α
from␈α␈↓↓car,␈↓␈α␈↓↓cdr,␈↓␈α␈↓↓cons,␈↓␈α
␈↓↓atom,␈↓␈α and␈α␈↓↓eq␈↓␈αusing␈α
␈↓↓λ,␈↓␈α␈↓↓label,␈↓␈αand␈αconditional␈α
expressions?
␈↓ ↓H␈↓Well,␈αit␈αcan't,␈αas␈αwe␈αshall␈αshortly␈αprove,␈αand␈αthis␈αmeans␈αthat␈αit␈αis␈αnot␈α␈↓↓computable␈↓␈αwhether␈αa␈αLISP
␈↓ ↓H␈↓calculation␈αterminates,␈αsince␈αif␈α␈↓↓term␈↓␈αwere␈αcomputable␈αby␈αany␈αcomputer␈αor␈αin␈αany␈αrecognized␈αsense,
␈↓ ↓H␈↓it could be represented as a LISP function.  Here is the proof:

␈↓ ↓H␈↓        Consider the function ␈↓↓terma␈↓ defined from ␈↓↓term␈↓ by

␈↓ ↓H␈↓2.2)␈↓ β2␈↓↓terma u ← ␈↓αif␈↓↓ term list[u, list[␈↓¬QUOTE, ␈↓↓u]] ␈↓αthen␈↓↓ loop u ␈↓αelse␈↓↓ ␈↓αtrue␈↓↓␈↓, 

␈↓ ↓H␈↓and␈α∞suppose␈α∞that␈α∞␈↓↓f␈↓␈α∞is␈α∞a␈α∞LISP␈α∞function␈α
and␈α∞that␈α∞␈↓↓f*␈↓␈α∞is␈α∞its␈α∞S-expression␈α∞representation.␈α∞ What␈α
is
␈↓ ↓H␈↓␈↓↓terma␈↓␈α␈↓↓f*␈↓␈α?␈αWell␈α␈↓↓terma␈↓␈α␈↓↓f*␈↓␈αtells␈αus␈αwhether␈αthe␈αcomputation␈αof␈α␈↓↓f[f*]␈↓␈αterminates,␈αand␈αit␈αtells␈αus␈αthis
␈↓ ↓H␈↓by␈α∞going␈α∂into␈α∞a␈α∂loop␈α∞if␈α∂␈↓↓f[f*]␈↓␈α∞terminates␈α∂and␈α∞giving␈α∂␈↓αtrue␈↓␈α∞otherwise.␈α∂ Now␈α∞if␈α∂␈↓↓term␈↓␈α∞were␈α∂a␈α∞LISP
␈↓ ↓H␈↓function,␈α
then␈α
␈↓↓terma␈↓␈α
would␈αalso␈α
be␈α
a␈α
LISP␈αfunction.␈α
 Indeed␈α
if␈α
␈↓↓term␈↓␈αwere␈α
represented␈α
by␈α
the␈αS-
␈↓ ↓H␈↓expression ␈↓↓term*,␈↓ then ␈↓↓terma␈↓ would be represented by the S-expression

␈↓ ↓H␈↓2.3)␈↓ ↓m␈↓↓terma*␈↓ = ␈↓¬(LAMBDA (U) (COND ((␈↓↓term*␈↓¬ (LIST U (LIST (QUOTE QUOTE) U))) (LOOP U)) 
␈↓ ↓H␈↓ ε
␈↓¬(T T)))␈↓. 

␈↓ ↓H␈↓Now␈α⊂consider␈α∂␈↓↓terma[terma*].␈↓␈α⊂  According␈α∂to␈α⊂the␈α∂definition␈α⊂of␈α∂␈↓↓terma,␈↓␈α⊂this␈α∂will␈α⊂tell␈α⊂us␈α∂whether
␈↓ ↓H␈↓␈↓↓terma[terma*]␈↓␈αis␈αdefined,␈αi.e.␈αit␈αtells␈αabout␈αitself.␈α However,␈αit␈αgives␈αthis␈αanswer␈αin␈αa␈αcontradictory
␈↓ ↓H␈↓way;␈α
namely␈α␈↓↓terma[terma*]␈↓␈α
looping␈α
tells␈αus␈α
that␈α
␈↓↓terma[terma*]␈↓␈αterminates,␈α
and␈α␈↓↓terma[terma*]␈↓␈α
being
␈↓ ↓H␈↓␈↓αtrue␈↓␈α∞tells␈α∞us␈α
that␈α∞␈↓↓terma[terma*]␈↓␈α∞doesn't␈α∞terminate.␈α
 This␈α∞contradiction␈α∞tells␈α∞us␈α
that␈α∞␈↓↓term␈↓␈α∞is␈α∞not␈α
a
␈↓ ↓H␈↓LISP␈α∃function,␈α∃and␈α∃there␈α∃is␈α∃no␈α∀general␈α∃procedure␈α∃for␈α∃telling␈α∃whether␈α∃a␈α∃LISP␈α∀calculation
␈↓ ↓H␈↓terminates.

␈↓ ↓H␈↓        The␈α∩above␈α⊃result␈α∩does␈α⊃not␈α∩exclude␈α⊃LISP␈α∩functions␈α⊃that␈α∩tell␈α⊃whether␈α∩LISP␈α⊃calculations
␈↓ ↓H␈↓terminate.␈α⊃ It␈α⊃just␈α⊃excludes␈α∩perfect␈α⊃ones.␈α⊃ Suppose␈α⊃we␈α⊃have␈α∩a␈α⊃function␈α⊃␈↓↓t␈↓␈α⊃that␈α∩sometimes␈α⊃says
␈↓ ↓H␈↓calculations␈α∂terminate,␈α∞sometimes␈α∂says␈α∂they␈α∞don't␈α∂terminate,␈α∂and␈α∞sometimes␈α∂runs␈α∂on␈α∞indefinitely.
␈↓ ↓H␈↓We␈α
shall␈α
further␈α
assume␈α
that␈α
when␈α
␈↓↓t␈↓␈α
gives␈αan␈α
answer␈α
it␈α
is␈α
always␈α
right.␈α
 Given␈α
such␈α
a␈αfunction␈α
we
␈↓ ↓H␈↓can␈αimprove␈αit␈α
a␈αbit␈αso␈α
that␈αit␈αwill␈α
always␈αgive␈αthe␈α
right␈αanswer␈αwhen␈α
the␈αcalculation␈αit␈α
is␈αasked
␈↓ ↓H␈↓about␈α∪terminates.␈α∪ This␈α∀is␈α∪done␈α∪by␈α∀mixing␈α∪the␈α∪computation␈α∪of␈α∀␈↓↓t[e]␈↓␈α∪with␈α∪a␈α∀computation␈α∪of
␈↓ ↓H␈↓␈↓↓eval[e, ␈↓¬NIL␈↓↓]␈↓␈αdoing␈αthe␈αcomputations␈αalternately.␈α If␈αthe␈α␈↓↓eval[e, ␈↓¬NIL␈↓↓]␈↓␈αcomputation␈αever␈αterminates,
␈↓ ↓H␈↓then the new function asserts termination.

␈↓ ↓H␈↓        Given␈αsuch␈αa␈α␈↓↓t,␈↓␈αwe␈αcan␈αalways␈αfind␈αa␈αcalculation␈αthat␈αdoes␈αnot␈αterminate␈αbut␈α␈↓↓t␈↓␈αdoesn't␈αsay
␈↓ ↓H␈↓so.  The construction is just like that used in the previous proof.  Given ␈↓↓t,␈↓ we construct

␈↓ ↓H␈↓2.4) ␈↓ β`␈↓↓ta u ← ␈↓αif␈↓↓ t list[u, list[␈↓¬QUOTE, ␈↓↓u]] ␈↓αthen␈↓↓ loop u ␈↓αelse␈↓↓ ␈↓αtrue␈↓↓␈↓, 

␈↓ ↓H␈↓and␈α
then␈α
we␈α
consider␈α
␈↓↓ta[ta*].␈↓␈α
 If␈α
this␈α
had␈α
the␈α
value␈α
␈↓αtrue␈↓,␈α
then␈α
it␈α
wouldn't␈α
terminate␈α
so␈α
therefore␈α
it
␈↓ ↓H␈↓doesn't␈α⊃terminate␈α⊃but␈α⊂is␈α⊃not␈α⊃one␈α⊂of␈α⊃those␈α⊃expressions␈α⊂which␈α⊃␈↓↓t␈↓␈α⊃decides.␈α⊂ Thus␈α⊃for␈α⊃any␈α⊂partial
␈↓ ↓H␈↓decider␈α∂we␈α∂can␈α∞find␈α∂a␈α∂LISP␈α∂calculation␈α∞which␈α∂doesn't␈α∂terminate␈α∞but␈α∂which␈α∂the␈α∂decider␈α∞doesn't
␈↓ ↓H␈↓decide.  This can in turn be used to get a slightly better decider, namely

␈↓ ↓H␈↓2.5)␈↓ βj␈↓↓t␈↓β1␈↓↓[e] ← ␈↓αif␈↓↓ e = ta* ␈↓αthen␈↓↓ ␈↓¬DOESN'T-TERMINATE ␈↓↓ ␈↓αelse␈↓↓ t[e]␈↓. 
␈↓ ↓H␈↓␈↓ ¬|Chapter  X␈↓ ≠185


␈↓ ↓H␈↓Of␈αcourse,␈α␈↓↓t␈↓␈↓β1␈↓␈αisn't␈αmuch␈αbetter␈αthan␈α␈↓↓t,␈↓␈αsince␈αit␈αcan␈αdecide␈αonly␈αone␈αmore␈αcomputation,␈αbut␈αwe␈αcan
␈↓ ↓H␈↓form␈α␈↓↓t␈↓␈↓β2␈↓␈αby␈αapplying␈αthe␈αsame␈αprocess,␈αand␈αso␈αforth.␈α In␈αfact,␈αwe␈αcan␈αeven␈αform␈α␈↓↓t␈↓␈↓π␈↓#vw␈↓#␈↓␈αwhich␈αdecides
␈↓ ↓H␈↓all␈αthe␈αcases␈αdecided␈αby␈αany␈α␈↓↓t␈↓␈↓βn␈↓.␈α This␈αcan␈αbe␈αfurther␈αimproved␈αby␈αthe␈αsame␈αprocess,␈αetc.␈α How␈αfar
␈↓ ↓H␈↓can␈α∂we␈α∂go?␈α∞ The␈α∂answer␈α∂is␈α∞technical;␈α∂namely,␈α∂the␈α∞improvement␈α∂process␈α∂can␈α∞be␈α∂carried␈α∂out␈α∞any
␈↓ ↓H␈↓recursive ordinal number of times.

␈↓ ↓H␈↓        Unfortunately,␈α⊂this␈α⊂kind␈α∂of␈α⊂improvement␈α⊂seems␈α∂to␈α⊂be␈α⊂superficial,␈α∂since␈α⊂none␈α⊂of␈α⊂the␈α∂new
␈↓ ↓H␈↓computations proved not to terminate are likely to be of practical interest.


␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1. Write a function that gives ␈↓↓t␈↓␈↓βn+1␈↓ in terms of ␈↓↓t␈↓␈↓βn␈↓.

␈↓ ↓H␈↓2. Write a function that gives ␈↓↓t␈↓␈↓π␈↓#vw␈↓#␈↓ in terms of ␈↓↓t.␈↓

␈↓ ↓H␈↓3.␈α∞If␈α∞you␈α∞know␈α∞about␈α
Turing␈α∞machines,␈α∞write␈α∞a␈α∞LISP␈α
function␈α∞to␈α∞simulate␈α∞an␈α∞arbitrary␈α
Turing
␈↓ ↓H␈↓machine given a description of the machine in some convenient notation.

␈↓ ↓H␈↓4.␈α
Write␈α
a␈α
LISP␈α
function␈αthat␈α
will␈α
translate␈α
a␈α
Turing␈αmachine␈α
description␈α
into␈α
a␈α
LISP␈αfunction
␈↓ ↓H␈↓that will do the same computation.

␈↓ ↓H␈↓5.␈αIf␈αyou␈αreally␈αlike␈αTuring␈αmachines,␈αwrite␈αa␈αdescription␈αof␈αa␈αTuring␈αmachine␈αthat␈αwill␈αinterpret
␈↓ ↓H␈↓LISP internal notation.
␈↓ ↓H␈↓␈↓ εH␈↓ ?i


␈↓ ↓H␈↓α␈↓ ¬PBIBLIOGRAPHY





␈↓ ↓H␈↓␈↓αBaker,␈αHenry␈α
G.␈α[1978]␈↓:␈α "List␈α
Processing␈αin␈αReal␈α
TIme␈αon␈αa␈α
Serial␈αComputer",␈α␈↓↓Communications␈α
of
␈↓ ↓H␈↓↓␈↓ αλthe ACM␈↓ ␈↓α21␈↓, pp. 280-294.

␈↓ ↓H␈↓        Description␈α
and␈αanalysis␈α
of␈αa␈α
list␈α
processing␈αsystem␈α
that␈αcontinuously␈α
reclaims␈αgarbage␈α
while
␈↓ ↓H␈↓␈↓ αλlinearizing and compacting space in use.

␈↓ ↓H␈↓␈↓αBoyer, Robert S. and J. Strather Moore [1978]␈↓: ␈↓↓A Computational Logic␈↓  manuscript.

␈↓ ↓H␈↓␈↓αBrudno,␈α∞A.␈α
L.␈α∞[1963]␈↓:␈α∞ "Bounds␈α
and␈α∞Valuations␈α
for␈α∞Shortening␈α∞the␈α
Scanning␈α∞of␈α∞Variations",␈α
[in
␈↓ ↓H␈↓␈↓ αλRussian], ␈↓↓Problemy Kibernetiki␈↓ ␈↓α10␈↓ pp.141-150.

␈↓ ↓H␈↓        First published account of the αβ algorithm.

␈↓ ↓H␈↓␈↓αCartwright,␈α∪Robert␈α∪[1977]␈↓:␈α∪ "Practical␈α∪Formal␈α∪Semantic␈α∪Defintion␈α∪and␈α∪Verification␈α∩Systems,"
␈↓ ↓H␈↓␈↓ αλPh.D. thesis, Computer Science Department, Stanford University, Stanford, Ca..

␈↓ ↓H␈↓␈↓αFilman,␈α⊃R.␈α⊃E.␈α⊃and␈α⊃R.␈α⊃W.␈α⊃Weyhrauch␈α⊃[1976]␈↓:␈α⊃␈↓↓A␈α⊃FOL␈α⊃Primer␈↓,␈α⊃Stanford␈α⊃Artificial␈α⊂Intelligence
␈↓ ↓H␈↓␈↓ αλLaboratory Memo AIM-288.

␈↓ ↓H␈↓        An excellent introduction to using the automatic first order logic proof checker FOL.

␈↓ ↓H␈↓␈↓αGordon,␈α∩M.␈α∩[1975]␈↓:␈α∩ "Operational␈α∩Reasoning␈α∩and␈α∩Denotational␈α∩Semantics",␈α∩Stanford␈α⊃Artificial
␈↓ ↓H␈↓␈↓ αλIntelligence Laboratory Memo, AIM-264.

␈↓ ↓H␈↓␈↓αKnuth,␈α∩D.␈α⊃E.␈α∩[1968a]␈↓:␈α⊃ ␈↓↓The␈α∩Art␈α⊃of␈α∩Computer␈α⊃Programming,␈α∩Vol.␈α⊃I:␈α∩Fundamental␈α⊃Algorithms␈↓.
␈↓ ↓H␈↓␈↓ αλAddison-Wesley.

␈↓ ↓H␈↓        Section ␈↓π∞␈↓2.3.5 contains a discussion of list structures and garbage collection algorithms.

␈↓ ↓H␈↓␈↓αKnuth,␈α⊃D.␈α∩E.␈α⊃[1968b]␈↓:␈α⊃ ␈↓↓The␈α∩Art␈α⊃of␈α∩Computer␈α⊃Programming,␈α⊃Vol.␈α∩III:␈α⊃Sorting␈α∩and␈α⊃Searching␈↓.
␈↓ ↓H␈↓␈↓ αλAddison-Wesley.

␈↓ ↓H␈↓        Section ␈↓π∞␈↓6.4 contains a discussion of hashing.

␈↓ ↓H␈↓␈↓αKnuth,␈αD.␈αE.␈αand␈αR.␈αMoore␈α[1975]␈↓:␈α "An␈αAnalysis␈αof␈αAlpha-Beta␈αPruning",␈α␈↓↓Artificial␈αIntelligence␈↓,
␈↓ ↓H␈↓␈↓ αλ␈↓α6␈↓.

␈↓ ↓H␈↓        A history, description, and analysis of the αβ algorithm.

␈↓ ↓H␈↓␈↓αManna, Zohar [1974]␈↓: ␈↓↓Mathematical Theory of Computation␈↓, McGraw-Hill.

␈↓ ↓H␈↓        Chapter␈α2␈αcontains␈αa␈αdiscussion␈αof␈αfirst␈α
order␈αlogic␈αand␈αof␈αthe␈α␈↓↓Natural deduction␈↓␈αmethod␈α
of
␈↓ ↓H␈↓␈↓ αλproof.

␈↓ ↓H␈↓        Chapter 5 contains a discussion of functionals and fixedpoints.
␈↓ ↓H␈↓ii␈↓ ¬KBIBLIOGRAPHY␈↓ H


␈↓ ↓H␈↓        The␈α
book␈αalso␈α
contains␈α
a␈αdiscussion␈α
of␈αstructural␈α
induction␈α
and␈αother␈α
methods␈α
of␈αproving
␈↓ ↓H␈↓␈↓ αλproperties of programs.

␈↓ ↓H␈↓␈↓αMcCarthy,␈α
John␈α[1962a]␈↓:␈α
"Computer␈α
Programs␈αfor␈α
Checking␈αMathematical␈α
Proofs",␈α
␈↓↓Proc.␈αSymp.
␈↓ ↓H␈↓↓␈↓ αλPure Math.␈↓ Vol.5 , Amer. Amth. Soc., Providence, R. I., pp219-227.

␈↓ ↓H␈↓␈↓αMcCarthy,␈α_John␈α↔[1962b]␈↓:␈α_ "Towards␈α↔a␈α_Mathematical␈α↔Science␈α_of␈α↔Computation,"␈α_in␈α_C.␈α↔M.
␈↓ ↓H␈↓␈↓ αλPopplewell␈α
(ed.),␈α
␈↓↓Information␈α
Processing,␈α
Proceedings␈α
of␈α
IFIP␈α
Congress␈α
62␈↓,␈α∞pp21-28,␈α
North
␈↓ ↓H␈↓␈↓ αλHolland Publishing Company, Amsterdam.

␈↓ ↓H␈↓        Contains␈αdiscussions␈αof␈αtransforming␈αFlow␈αChart␈αprograms␈αinto␈αrecursive␈αprograms␈αand␈αof
␈↓ ↓H␈↓␈↓ αλthe Abstract Syntax of programming languages.

␈↓ ↓H␈↓␈↓αMcCarthy,␈αJohn␈α
[1963]␈↓:␈α"A␈αBasis␈α
for␈αa␈αMathematical␈α
Theory␈αof␈αComputation",␈α
in␈αP.␈αBraffort␈α
and
␈↓ ↓H␈↓␈↓ αλD.␈αHirschberg␈α(eds.),␈α␈↓↓Computer␈αProgramming␈αand␈αFormal␈αSystems␈↓,␈αpp.␈α33-70.␈αNorth-Holland
␈↓ ↓H␈↓␈↓ αλPublishing Company, Amsterdam.

␈↓ ↓H␈↓        Contains␈α⊃a␈α⊃complete␈α⊃discussion␈α⊃of␈α⊃properties␈α⊃of␈α⊃conditional␈α⊃forms,␈α⊃including␈α⊃cannonical
␈↓ ↓H␈↓␈↓ αλforms.  Also discusses computable functionals and recursion induction.

␈↓ ↓H␈↓␈↓αMcCarthy,␈α∂John␈α∂[1964]␈↓:␈α∂ "A␈α∞Formal␈α∂Description␈α∂of␈α∂a␈α∂Subset␈α∞of␈α∂Algol",␈α∂␈↓↓Proc.␈α∂Conf.␈α∂on␈α∞Formal
␈↓ ↓H␈↓↓␈↓ αλLanguage Description Languages␈↓, Vienna.

␈↓ ↓H␈↓␈↓αMcCarthy,␈α∂John␈α∂[1978a]␈↓:␈α∂ "History␈α∂of␈α∂LISP",␈α∂␈↓↓Proceedings␈α∂of␈α∂the␈α∂ACM␈α∂conference␈α∂on␈α∂History␈α∂of
␈↓ ↓H␈↓↓␈↓ αλProgramming Languages␈↓, 1978.

␈↓ ↓H␈↓␈↓αMcCarthy,␈α_John␈α↔[1978b]␈↓:␈α_ "Representation␈α_of␈α↔Recursive␈α_Programs␈α↔in␈α_First␈α_Order␈α↔Logic",
␈↓ ↓H␈↓␈↓ αλ␈↓↓Proceedings␈αthe␈α
International␈αConference␈α
on␈αMathematical␈α
Studies␈αof␈α
Information␈αProcessing␈↓,
␈↓ ↓H␈↓␈↓ αλKyoto Japan, 1978.

␈↓ ↓H␈↓␈↓αMcCarthy,␈α→John␈α_and␈α→James␈α→Painter␈α_[1967]␈↓:␈α→"Correctness␈α→of␈α_a␈α→Compiler␈α→for␈α_Arithmetic
␈↓ ↓H␈↓␈↓ αλExpressions",␈α␈↓↓Proceeding␈αof␈αSymposia␈αin␈αApplied␈α Mathematics␈↓,␈αVol.␈α19,␈αJ.␈αT.␈αSchwartz␈α(ed.),
␈↓ ↓H␈↓␈↓ αλAmerican Mathematical Society, pp 33-41.

␈↓ ↓H␈↓␈↓αMoszkowski, B. [1978]␈↓: Stanford Artificial Intellinence Laboratory Memo, (to appear).

␈↓ ↓H␈↓␈↓αNilsson, N. J. [1971]␈↓:  ␈↓↓Problem Solving Methods in Artificial Intelligence␈↓, McGraw-Hill.

␈↓ ↓H␈↓␈↓αPainter,␈α
James␈α
[1967]␈↓:␈α "Semantic␈α
Correctness␈α
of␈αa␈α
Compiler␈α
for␈αan␈α
Algol-like␈α
Language",␈αPh.D.
␈↓ ↓H␈↓␈↓ αλthesis, Computer Science Department, Stanford University, Stanford, Ca..

␈↓ ↓H␈↓␈↓αVuillemin,␈αJ.␈α
[1973]␈↓:␈α"Proof␈α
Techniques␈αfor␈α
Recursive␈αPrograms,"␈α
Ph.D.␈α thesis,␈αComputer␈α
Science
␈↓ ↓H␈↓␈↓ αλDepartment, Stanford University, Stanford, Ca..

␈↓ ↓H␈↓␈↓αWeyhrauch,␈αR.␈αW.␈α[1977]␈↓:␈α "A␈αusers␈αmanual␈αfor␈αFOL",␈αStanford␈αArtificial␈αIntelligence␈α
Laboratory
␈↓ ↓H␈↓␈↓ αλMemo, AIM-235.1.

␈↓ ↓H␈↓        A manual for the automatic proof checker FOL.

␈↓ ↓H␈↓␈↓αWinston, P. H. [1977]␈↓:  ␈↓↓Artificial Intelligence␈↓,  Addison-Wesley.
␈↓ ↓H␈↓␈↓ εH␈↓ ?i


␈↓ ↓H␈↓α␈↓ ¬uAppendix A

␈↓ ↓H␈↓α␈↓ ¬\LISP Compilers




␈↓ ↓H␈↓1.  ␈↓αLCOM0: listing of MACLISP version.␈↓


␈↓ ↓H␈↓ε(DECLARE (SETQ NO-DISK-HACKS T))
␈↓ ↓H␈↓ε(DECLARE (READ))

␈↓ ↓H␈↓ε(DEFPROP LC0FNS
␈↓ ↓H␈↓ε (LC0FNS COMPL COMP PRUP MKPUSH COMPEXP COMPLIS LOADAC COMCOND COMBOOL COMPANDOR)
␈↓ ↓H␈↓εVALUE)

␈↓ ↓H␈↓ε(DEFPROP COMPL
␈↓ ↓H␈↓ε (LAMBDA(FILE)
␈↓ ↓H␈↓ε  (UWRITE)
␈↓ ↓H␈↓ε  (APPLY (QUOTE EREAD) FILE)
␈↓ ↓H␈↓ε  (SELECT-DISK-INPUT
␈↓ ↓H␈↓ε   (READ-UNTIL-EOF
␈↓ ↓H␈↓ε    WITH
␈↓ ↓H␈↓ε    Z
␈↓ ↓H␈↓ε    DO
␈↓ ↓H␈↓ε    (COND ((OR (EQ (CAR Z) (QUOTE DEFUN)) (AND (EQ (CAR Z) (QUOTE DEFPROP)) (EQ (CADDDR Z) (QUOTE EXPR))))
␈↓ ↓H␈↓ε           (PROG (PROG)
␈↓ ↓H␈↓ε                 (SETQ PROG
␈↓ ↓H␈↓ε                       (COND ((EQ (CAR Z) (QUOTE DEFUN)) (COMP (CADR Z) (CADDR Z) (CADDDR Z)))
␈↓ ↓H␈↓ε                             (T (COMP (CADR Z) (CADR (CADDR Z)) (CADDR (CADDR Z))))))
␈↓ ↓H␈↓ε                 (UNSELECT-TTY (SELECT-DISK-OUTPUT (MAPC (FUNCTION PRINT) PROG)))
␈↓ ↓H␈↓ε                 (PRINT (LIST (CADR Z) (LENGTH PROG)))))
␈↓ ↓H␈↓ε          (T (UNSELECT-TTY (SELECT-DISK-OUTPUT (PRINT Z))))))
␈↓ ↓H␈↓ε   (APPLY (QUOTE UFILE) (LIST (CAR FILE) (QUOTE LAP)))
␈↓ ↓H␈↓ε   (QUOTE ENDCOMP)))
␈↓ ↓H␈↓εFEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMP
␈↓ ↓H␈↓ε (LAMBDA(FN VARS EXP)
␈↓ ↓H␈↓ε  ((LAMBDA(N)
␈↓ ↓H␈↓ε    (APPEND (LIST (LIST (QUOTE LAP) FN (QUOTE SUBR)))
␈↓ ↓H␈↓ε            (MKPUSH N 1)
␈↓ ↓H␈↓ε            (COMPEXP EXP (MINUS N) (PRUP VARS 1))
␈↓ ↓H␈↓ε            (LIST (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE %) 0 0 N N)))
␈↓ ↓H␈↓ε            (QUOTE ((POPJ P) NIL))))
␈↓ ↓H␈↓ε   (LENGTH VARS)))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP PRUP
␈↓ ↓H␈↓ε (LAMBDA (VARS N) (COND ((NULL VARS) NIL) (T (CONS (CONS (CAR VARS) N) (PRUP (CDR VARS) (ADD1 N))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP MKPUSH
␈↓ ↓H␈↓ε (LAMBDA (N M) (COND ((LESSP N M) NIL) (T (CONS (LIST (QUOTE PUSH) (QUOTE P) M) (MKPUSH N (ADD1 M))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPEXP
␈↓ ↓H␈↓ε (LAMBDA(EXP M VPR)
␈↓ ↓H␈↓ε  (COND ((NULL EXP) (QUOTE ((MOVEI 1 0))))
␈↓ ↓H␈↓ε        ((EQ EXP T) (QUOTE ((MOVEI 1 (QUOTE T)))))
␈↓ ↓H␈↓ε        ((NUMBERP EXP) (LIST (LIST (QUOTE MOVEI) 1 (LIST (QUOTE QUOTE) EXP))))
␈↓ ↓H␈↓ε        ((ATOM EXP) (LIST (LIST (QUOTE MOVE) 1 (PLUS M (CDR (ASSOC EXP VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε        ((OR (EQ (CAR EXP) (QUOTE AND)) (EQ (CAR EXP) (QUOTE OR)) (EQ (CAR EXP) (QUOTE NOT)))
␈↓ ↓H␈↓ε         ((LAMBDA(L1 L2)
␈↓ ↓H␈↓ε           (APPEND (COMBOOL EXP M L1 NIL VPR)
␈↓ ↓H␈↓ii␈↓ ¬pAppendix  A␈↓ H


␈↓ ↓H␈↓ε                   (LIST (QUOTE (MOVEI 1 (QUOTE T))) (LIST (QUOTE JRST) 0 L2) L1 (QUOTE (MOVEI 1 0)) L2)))
␈↓ ↓H␈↓ε          (GENSYM)
␈↓ ↓H␈↓ε          (GENSYM)))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE COND)) (COMCOND (CDR EXP) M (GENSYM) VPR))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE QUOTE)) (LIST (LIST (QUOTE MOVEI) 1 EXP)))
␈↓ ↓H␈↓ε        ((ATOM (CAR EXP))
␈↓ ↓H␈↓ε         ((LAMBDA(N)
␈↓ ↓H␈↓ε           (APPEND (COMPLIS (CDR EXP) M VPR)
␈↓ ↓H␈↓ε                   (LOADAC (DIFFERENCE 1 N) 1)
␈↓ ↓H␈↓ε                   (LIST (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE %) 0 0 N N)))
␈↓ ↓H␈↓ε                   (LIST (LIST (QUOTE CALL) N (LIST (QUOTE QUOTE) (CAR EXP))))))
␈↓ ↓H␈↓ε          (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ε        ((EQ (CAAR EXP) (QUOTE LAMBDA))
␈↓ ↓H␈↓ε         ((LAMBDA(N)
␈↓ ↓H␈↓ε           (APPEND (COMPLIS (CDR EXP) M VPR)
␈↓ ↓H␈↓ε                   (COMPEXP (CADDAR EXP) 
␈↓ ↓H␈↓ε                            (DIFFERENCE M N) 
␈↓ ↓H␈↓ε                            (APPEND (PRUP (CADAR EXP) (DIFFERENCE 1 M)) VPR))
␈↓ ↓H␈↓ε                   (LIST (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE %) 0 0 N N)))))
␈↓ ↓H␈↓ε          (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ε        (T NIL)))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPLIS
␈↓ ↓H␈↓ε (LAMBDA(U M VPR)
␈↓ ↓H␈↓ε  (COND ((NULL U) NIL)
␈↓ ↓H␈↓ε        (T (APPEND (COMPEXP (CAR U) M VPR) (QUOTE ((PUSH P 1))) (COMPLIS (CDR U) (SUB1 M) VPR)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP LOADAC
␈↓ ↓H␈↓ε (LAMBDA(N K)
␈↓ ↓H␈↓ε  (COND ((GREATERP N 0) NIL) (T (CONS (LIST (QUOTE MOVE) K N (QUOTE P)) (LOADAC (ADD1 N) (ADD1 K))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMCOND
␈↓ ↓H␈↓ε (LAMBDA(U M L VPR)
␈↓ ↓H␈↓ε  (COND ((NULL U) (LIST L))
␈↓ ↓H␈↓ε        (T
␈↓ ↓H␈↓ε         ((LAMBDA(L1)
␈↓ ↓H␈↓ε           (APPEND (COMBOOL (CAAR U) M L1 NIL VPR)
␈↓ ↓H␈↓ε                   (COMPEXP (CADAR U) M VPR)
␈↓ ↓H␈↓ε                   (LIST (LIST (QUOTE JRST) 0 L) L1)
␈↓ ↓H␈↓ε                   (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ε          (GENSYM)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMBOOL
␈↓ ↓H␈↓ε (LAMBDA(P M L FLG VPR)
␈↓ ↓H␈↓ε  (COND ((ATOM P) 
␈↓ ↓H␈↓ε         (APPEND (COMPEXP P M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE AND))
␈↓ ↓H␈↓ε         (COND ((NOT FLG) (COMPANDOR (CDR P) M L NIL VPR))
␈↓ ↓H␈↓ε               (T
␈↓ ↓H␈↓ε                ((LAMBDA(L1)
␈↓ ↓H␈↓ε                  (APPEND (COMPANDOR (CDR P) M L1 NIL VPR) (LIST (LIST (QUOTE JRST) 0 L)) (LIST L1)))
␈↓ ↓H␈↓ε                 (GENSYM)))))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE OR))
␈↓ ↓H␈↓ε         (COND (FLG (COMPANDOR (CDR P) M L T VPR))
␈↓ ↓H␈↓ε               (T
␈↓ ↓H␈↓ε                ((LAMBDA (L1) 
␈↓ ↓H␈↓ε                  (APPEND (COMPANDOR (CDR P) M L1 T VPR) (LIST (LIST (QUOTE JRST) 0 L)) (LIST L1)))
␈↓ ↓H␈↓ε                 (GENSYM)))))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE NOT)) (COMBOOL (CADR P) M L (NOT FLG) VPR))
␈↓ ↓H␈↓ε        (T (APPEND (COMPEXP P M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPANDOR
␈↓ ↓H␈↓ε (LAMBDA(U M L FLG VPR)
␈↓ ↓H␈↓ε  (COND ((NULL U) NIL) (T (APPEND (COMBOOL (CAR U) M L FLG VPR) (COMPANDOR (CDR U) M L FLG VPR)))))
␈↓ ↓H␈↓␈↓ ¬pAppendix  A␈↓ -iii


␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓iv␈↓ ¬pAppendix  A␈↓ H


␈↓ ↓H␈↓2.  ␈↓αLCOM4: listing of MACLISP version.␈↓


␈↓ ↓H␈↓ε(DECLARE (SETQ NO-DISK-HACKS T))
␈↓ ↓H␈↓ε(DECLARE (READ))

␈↓ ↓H␈↓ε(DEFPROP COMPFCNS
␈↓ ↓H␈↓ε (COMPFCNS COMPL
␈↓ ↓H␈↓ε           COMP
␈↓ ↓H␈↓ε           SUBSTACK
␈↓ ↓H␈↓ε           PRUP
␈↓ ↓H␈↓ε           MKPUSH
␈↓ ↓H␈↓ε           COMPEXP
␈↓ ↓H␈↓ε           STACKUP
␈↓ ↓H␈↓ε           CCCHAIN
␈↓ ↓H␈↓ε           COMPC
␈↓ ↓H␈↓ε           COMCOND
␈↓ ↓H␈↓ε           COMPLISA
␈↓ ↓H␈↓ε           CCOUNT
␈↓ ↓H␈↓ε           LOADAC
␈↓ ↓H␈↓ε           COMPLIS
␈↓ ↓H␈↓ε           CLASSIFY
␈↓ ↓H␈↓ε           CLASS1
␈↓ ↓H␈↓ε           CLASS2
␈↓ ↓H␈↓ε           MKJRST
␈↓ ↓H␈↓ε           COMBOOL
␈↓ ↓H␈↓ε           COMPANDOR
␈↓ ↓H␈↓ε           COMPANDOR1
␈↓ ↓H␈↓ε           FLAT)
␈↓ ↓H␈↓εVALUE)

␈↓ ↓H␈↓ε(DEFPROP COMPL
␈↓ ↓H␈↓ε (LAMBDA(FILE)
␈↓ ↓H␈↓ε  (UWRITE)
␈↓ ↓H␈↓ε  (APPLY (QUOTE EREAD) FILE)
␈↓ ↓H␈↓ε  (SELECT-DISK-INPUT
␈↓ ↓H␈↓ε   (READ-UNTIL-EOF
␈↓ ↓H␈↓ε    WITH
␈↓ ↓H␈↓ε    Z
␈↓ ↓H␈↓ε    DO
␈↓ ↓H␈↓ε    (COND ((OR (EQ (CAR Z) (QUOTE DEFUN)) (AND (EQ (CAR Z) (QUOTE DEFPROP)) (EQ (CADDDR Z) (QUOTE EXPR))))
␈↓ ↓H␈↓ε           (PROG (PROG)
␈↓ ↓H␈↓ε                 (SETQ PROG
␈↓ ↓H␈↓ε                       (COND ((EQ (CAR Z) (QUOTE DEFUN)) (COMP (CADR Z) (CADDR Z) (CADDDR Z)))
␈↓ ↓H␈↓ε                             (T (COMP (CADR Z) (CADR (CADDR Z)) (CADDR (CADDR Z))))))
␈↓ ↓H␈↓ε                 (UNSELECT-TTY (SELECT-DISK-OUTPUT (MAPC (FUNCTION PRINT) PROG)))
␈↓ ↓H␈↓ε                 (PRINT (LIST (CADR Z) (LENGTH PROG)))))
␈↓ ↓H␈↓ε          (T (UNSELECT-TTY (SELECT-DISK-OUTPUT (PRINT Z))))))
␈↓ ↓H␈↓ε   (APPLY (QUOTE UFILE) (LIST (CAR FILE) (QUOTE LAP)))
␈↓ ↓H␈↓ε   (QUOTE ENDCOMP)))
␈↓ ↓H␈↓εFEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMP
␈↓ ↓H␈↓ε (LAMBDA(FN VARS EXP)
␈↓ ↓H␈↓ε  ((LAMBDA(VPR N)
␈↓ ↓H␈↓ε    (FLAT (LIST (LIST (LIST (QUOTE LAP) FN (QUOTE SUBR)))
␈↓ ↓H␈↓ε                (MKPUSH N 1)
␈↓ ↓H␈↓ε                (COMPEXP EXP (MINUS N) VPR)
␈↓ ↓H␈↓ε                (SUBSTACK N)
␈↓ ↓H␈↓ε                (QUOTE ((POPJ P) (LABEL NIL))))
␈↓ ↓H␈↓ε          NIL))
␈↓ ↓H␈↓ε   (PRUP VARS 1)
␈↓ ↓H␈↓ε   (LENGTH VARS)))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP SUBSTACK
␈↓ ↓H␈↓ε (LAMBDA (N) (COND ((= N 0) NIL) (T (LIST (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE %) 0 0 N N))))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓␈↓ ¬pAppendix  A␈↓ 7v


␈↓ ↓H␈↓ε(DEFPROP PRUP
␈↓ ↓H␈↓ε (LAMBDA (VARS N) (COND ((NULL VARS) NIL) (T (CONS (CONS (CAR VARS) N) (PRUP (CDR VARS) (ADD1 N))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP MKPUSH
␈↓ ↓H␈↓ε (LAMBDA (N M) (COND ((LESSP N M) NIL) (T (CONS (LIST (QUOTE PUSH) (QUOTE P) M) (MKPUSH N (ADD1 M))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPEXP
␈↓ ↓H␈↓ε (LAMBDA(EXP M VPR)
␈↓ ↓H␈↓ε  (COND ((NULL EXP) (QUOTE ((MOVEI 1 0))))
␈↓ ↓H␈↓ε        ((OR (EQ EXP T) (NUMBERP EXP)) (LIST (LIST (QUOTE MOVEI) 1 (LIST (QUOTE QUOTE) EXP))))
␈↓ ↓H␈↓ε        ((ATOM EXP) (LIST (LIST (QUOTE MOVE) 1 (PLUS M (CDR (ASSOC EXP VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE CAR))
␈↓ ↓H␈↓ε         (COND ((ATOM (CADR EXP)) 
␈↓ ↓H␈↓ε                (LIST (LIST (QUOTE HLRZ) 1 (QUOTE @)(PLUS M (CDR (ASSOC (CADR EXP) VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε               (T (LIST (COMPEXP (CADR EXP) M VPR) (QUOTE ((HLRZ 1 @ 1)))))))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE CDR))
␈↓ ↓H␈↓ε         (COND ((ATOM (CADR EXP)) 
␈↓ ↓H␈↓ε                (LIST (LIST (QUOTE HRRZ) 1 (QUOTE @)(PLUS M (CDR (ASSOC (CADR EXP) VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε               (T (LIST (COMPEXP (CADR EXP) M VPR) (QUOTE ((HRRZ 1 @ 1)))))))
␈↓ ↓H␈↓ε        ((OR (EQ (CAR EXP) (QUOTE AND))
␈↓ ↓H␈↓ε             (EQ (CAR EXP) (QUOTE OR))
␈↓ ↓H␈↓ε             (EQ (CAR EXP) (QUOTE NOT))
␈↓ ↓H␈↓ε             (EQ (CAR EXP) (QUOTE EQ)))
␈↓ ↓H␈↓ε         ((LAMBDA(L1 L2)
␈↓ ↓H␈↓ε           (LIST (COMBOOL EXP M L1 NIL VPR)
␈↓ ↓H␈↓ε                 (LIST (QUOTE (MOVEI 1 (QUOTE T)))
␈↓ ↓H␈↓ε                       (LIST (QUOTE JRST) 0 L2)
␈↓ ↓H␈↓ε                       (LIST (QUOTE LABEL) L1)
␈↓ ↓H␈↓ε                       (QUOTE (MOVEI 1 0))
␈↓ ↓H␈↓ε                       (LIST (QUOTE LABEL) L2))))
␈↓ ↓H␈↓ε          (GENSYM)
␈↓ ↓H␈↓ε          (GENSYM)))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE COND)) (COMCOND (CDR EXP) M (GENSYM) VPR))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE QUOTE)) (LIST (LIST (QUOTE MOVEI) 1 EXP)))
␈↓ ↓H␈↓ε        ((ATOM (CAR EXP))
␈↓ ↓H␈↓ε         (LIST (COMPLISA (CDR EXP) M VPR)
␈↓ ↓H␈↓ε               (LIST (LIST (QUOTE CALL) (LENGTH (CDR EXP)) (LIST (QUOTE QUOTE) (CAR EXP))))))
␈↓ ↓H␈↓ε        ((EQ (CAAR EXP) (QUOTE LAMBDA))
␈↓ ↓H␈↓ε         ((LAMBDA(N)
␈↓ ↓H␈↓ε           (LIST (STACKUP (CDR EXP) M VPR)
␈↓ ↓H␈↓ε                 (COMPEXP (CADDAR EXP) (DIFFERENCE M N) (APPEND (PRUP (CADAR EXP) (DIFFERENCE 1 M)) VPR))
␈↓ ↓H␈↓ε                 (SUBSTACK N)))
␈↓ ↓H␈↓ε          (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ε        (T NIL)))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP STACKUP
␈↓ ↓H␈↓ε (LAMBDA(U M VPR)
␈↓ ↓H␈↓ε  (COND ((NULL U) NIL) 
␈↓ ↓H␈↓ε        (T (LIST (COMPEXP (CAR U) M VPR) (QUOTE ((PUSH P 1))) (STACKUP (CDR U) (SUB1 M) VPR)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP CCCHAIN
␈↓ ↓H␈↓ε (LAMBDA(EXP)
␈↓ ↓H␈↓ε  (AND (OR (EQ (CAR EXP) (QUOTE CAR)) (EQ (CAR EXP) (QUOTE CDR))) 
␈↓ ↓H␈↓ε       (OR (ATOM (CADR EXP)) (CCCHAIN (CADR EXP)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPC
␈↓ ↓H␈↓ε (LAMBDA(EXP N2 M VPR)
␈↓ ↓H␈↓ε  (COND ((ATOM EXP) (ERROR (QUOTE COMPC)))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE CAR))
␈↓ ↓H␈↓ε         (COND ((ATOM (CADR EXP))
␈↓ ↓H␈↓ε                (LIST (LIST (QUOTE HLRZ) N2 (QUOTE @)(PLUS M (CDR (ASSOC (CADR EXP) VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε               (T (CONS (LIST (QUOTE HLRZ) N2 (QUOTE @) N2) (COMPC (CADR EXP) N2 M VPR)))))
␈↓ ↓H␈↓ε        ((ATOM (CADR EXP)) 
␈↓ ↓H␈↓ε         (LIST (LIST (QUOTE HRRZ) (QUOTE @) N2 (PLUS M (CDR (ASSOC (CADR EXP) VPR))) (QUOTE P))))
␈↓ ↓H␈↓vi␈↓ ¬pAppendix  A␈↓ H


␈↓ ↓H␈↓ε        (T (CONS (LIST (QUOTE HRRZ) N2 (QUOTE @) N2) (COMPC (CADR EXP) N2 M VPR)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMCOND
␈↓ ↓H␈↓ε (LAMBDA(U M L VPR)
␈↓ ↓H␈↓ε  (COND ((NULL U) (LIST (LIST (QUOTE LABEL) L)))
␈↓ ↓H␈↓ε        ((AND (NOT (ATOM (CAAR U))) (EQ (CAAAR U) (QUOTE NULL)) (NULL (CADAR U)))
␈↓ ↓H␈↓ε         (LIST (COMPEXP (CADAAR U) M VPR) (LIST (LIST (QUOTE JUMPE) 1 L)) (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ε        ((EQ (CAAR U) T) (LIST (COMPEXP (CADAR U) M VPR) (LIST (LIST (QUOTE LABEL) L))))
␈↓ ↓H␈↓ε        (T
␈↓ ↓H␈↓ε         ((LAMBDA(L1)
␈↓ ↓H␈↓ε           (LIST (COMBOOL (CAAR U) M L1 NIL VPR)
␈↓ ↓H␈↓ε                 (COMPEXP (CADAR U) M VPR)
␈↓ ↓H␈↓ε                 (LIST (LIST (QUOTE JRST) 0 L) (LIST (QUOTE LABEL) L1))
␈↓ ↓H␈↓ε                 (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ε          (GENSYM)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPLISA
␈↓ ↓H␈↓ε (LAMBDA(U M VPR)
␈↓ ↓H␈↓ε  ((LAMBDA(Z)
␈↓ ↓H␈↓ε    (LIST (COMPLIS Z M 1 VPR)
␈↓ ↓H␈↓ε          (LOADAC Z (DIFFERENCE 1 (CCOUNT Z)) 1 (DIFFERENCE M (CCOUNT Z)) VPR)
␈↓ ↓H␈↓ε          (SUBSTACK (CCOUNT Z))))
␈↓ ↓H␈↓ε   (CLASSIFY U)))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP CCOUNT
␈↓ ↓H␈↓ε (LAMBDA (Z) (COND ((NULL Z) 0) ((= (CAAR Z) 4) (ADD1 (CCOUNT (CDR Z)))) (T (CCOUNT (CDR Z)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP LOADAC
␈↓ ↓H␈↓ε (LAMBDA(Z M2 N2 M VPR)
␈↓ ↓H␈↓ε  (COND ((NULL Z) NIL)
␈↓ ↓H␈↓ε        ((= (CAAR Z) 1)
␈↓ ↓H␈↓ε         (CONS (LIST (QUOTE MOVE) N2 (PLUS M (CDR (ASSOC (CDAR Z) VPR))) (QUOTE P))
␈↓ ↓H␈↓ε               (LOADAC (CDR Z) M2 (ADD1 N2) M VPR)))
␈↓ ↓H␈↓ε        ((= (CAAR Z) 0)
␈↓ ↓H␈↓ε         (CONS (LIST (QUOTE MOVEI) N2 (LIST (QUOTE QUOTE) (CDAR Z))) (LOADAC (CDR Z) M2 (ADD1 N2) M VPR)))
␈↓ ↓H␈↓ε        ((= (CAAR Z) 2) (CONS (LIST (QUOTE MOVEI) N2 (CDAR Z)) (LOADAC (CDR Z) M2 (ADD1 N2) M VPR)))
␈↓ ↓H␈↓ε        ((= (CAAR Z) 3) (LIST (REVERSE (COMPC (CDAR Z) N2 M VPR)) (LOADAC (CDR Z) M2 (ADD1 N2) M VPR)))
␈↓ ↓H␈↓ε        ((= (CAAR Z) 5) (LOADAC (CDR Z) 1 (ADD1 N2) M VPR))
␈↓ ↓H␈↓ε        (T (CONS (LIST (QUOTE MOVE) N2 M2 (QUOTE P)) (LOADAC (CDR Z) (ADD1 M2) (ADD1 N2) M VPR)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPLIS
␈↓ ↓H␈↓ε (LAMBDA(Z M K VPR)
␈↓ ↓H␈↓ε  (COND ((NULL Z) NIL)
␈↓ ↓H␈↓ε        ((= (CAAR Z) 4)
␈↓ ↓H␈↓ε         (LIST (COMPEXP (CDAR Z) M VPR) (QUOTE ((PUSH P 1))) (COMPLIS (CDR Z) (SUB1 M) (ADD1 K) VPR)))
␈↓ ↓H␈↓ε        ((= (CAAR Z) 5) 
␈↓ ↓H␈↓ε         (LIST (COMPEXP (CDAR Z) M VPR) (COND ((= K 1) NIL) (T (LIST (LIST (QUOTE MOVE) K 1))))))
␈↓ ↓H␈↓ε        (T (COMPLIS (CDR Z) M (ADD1 K) VPR))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP CLASSIFY
␈↓ ↓H␈↓ε (LAMBDA (U) (CLASS2 (CLASS1 U NIL) NIL T))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP CLASS1
␈↓ ↓H␈↓ε (LAMBDA(U V)
␈↓ ↓H␈↓ε  (COND ((NULL U) V)
␈↓ ↓H␈↓ε        ((ATOM (CAR U))
␈↓ ↓H␈↓ε         (COND ((OR (EQUAL (CAR U) NIL) (EQUAL (CAR U) T) (NUMBERP (CAR U)))
␈↓ ↓H␈↓ε                (CLASS1 (CDR U) (CONS (CONS 0 (CAR U)) V)))
␈↓ ↓H␈↓ε               (T (CLASS1 (CDR U) (CONS (CONS 1 (CAR U)) V)))))
␈↓ ↓H␈↓ε        ((EQUAL (CAAR U) (QUOTE QUOTE)) (CLASS1 (CDR U) (CONS (CONS 2 (CAR U)) V)))
␈↓ ↓H␈↓ε        ((CCCHAIN (CAR U)) (CLASS1 (CDR U) (CONS (CONS 3 (CAR U)) V)))
␈↓ ↓H␈↓ε        (T (CLASS1 (CDR U) (CONS (CONS 4 (CAR U)) V)))))
␈↓ ↓H␈↓␈↓ ¬pAppendix  A␈↓ %vii


␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP CLASS2
␈↓ ↓H␈↓ε (LAMBDA(U V FLG)
␈↓ ↓H␈↓ε  (COND ((NULL U) V)
␈↓ ↓H␈↓ε        ((AND FLG (= (CAAR U) 4)) (CLASS2 (CDR U) (CONS (CONS 5 (CDAR U)) V) NIL))
␈↓ ↓H␈↓ε        (T (CLASS2 (CDR U) (CONS (CAR U) V) FLG))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP MKJRST
␈↓ ↓H␈↓ε (LAMBDA (L) (LIST (LIST (QUOTE JRST) 0 L)))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMBOOL
␈↓ ↓H␈↓ε (LAMBDA(P M L FLG VPR)
␈↓ ↓H␈↓ε  (COND ((EQ P T) (COND (FLG (MKJRST L)) (T NIL)))
␈↓ ↓H␈↓ε        ((ATOM P) (LIST (COMPEXP P M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE EQ))
␈↓ ↓H␈↓ε         (LIST (COMPLISA (CDR P) M VPR) 
␈↓ ↓H␈↓ε               (COND (FLG (QUOTE ((CAMN 1 2)))) (T (QUOTE ((CAME 1 2))))) 
␈↓ ↓H␈↓ε               (MKJRST L)))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE AND))
␈↓ ↓H␈↓ε         (COND ((NOT FLG) (COMPANDOR (CDR P) M L NIL VPR))
␈↓ ↓H␈↓ε               (T
␈↓ ↓H␈↓ε                ((LAMBDA (L1) (LIST (COMPANDOR1 (CDR P) M L1 L NIL VPR) (LIST (LIST (QUOTE LABEL) L1))))
␈↓ ↓H␈↓ε                 (GENSYM)))))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE OR))
␈↓ ↓H␈↓ε         (COND (FLG (COMPANDOR (CDR P) M L T VPR))
␈↓ ↓H␈↓ε               (T
␈↓ ↓H␈↓ε                ((LAMBDA (L1) (LIST (COMPANDOR1 (CDR P) M L1 L T VPR) (LIST (LIST (QUOTE LABEL) L1))))
␈↓ ↓H␈↓ε                 (GENSYM)))))
␈↓ ↓H␈↓ε