You are on page 1of 19
Nemo & BY MACHINE by J . MeCarthy An Brror in Nene 8 The definition of eval-given on page 15 has two errors, one of which ts typographical and the other conceptual. The typographical error is in the definition of evcon where ">" and “T-" shovld be interchanged. The second error is in evlam. The program as it stands will not work if a quoted expression contains a symbol which also acts as a variable bound by the lambda. This can be corrected by using instead of subst in evlam a function subsq defined by : subsqeA (x;y3z] ;[null[z}2/| satom[2 [ye2—x33 2] ;tirst[z] -QquoTE42; 1—> combine [subsq]x;ysfiret(2) ]ssubaq fxsysrest[z]}]}) 'oject---RLE and MIT Computation Center OF SYMBOLIC EXPRESSIONS AND THEIR COMPUTA'TION alta March’, 1959 artlel Meno 8 RECURSIVE FUNCTIONS OF SYMBOLIC EXPRESSIONS. AND THEIR COMPUTATION BY MACHINE by J. MoCarthy The attached paper is a description of the LISP systeu starting with the machine-independent system of reoursive functions of symbolic expresaions. This seema to be a better point of view for looking at the system ‘than. the original | programming approach. after revision, the’ paper will be sub- mitted for publication in a logic or computing journal. ‘This memorandum contains only the wackigne independent tion of S-expressions 3h the computer and the system for representing ‘S-funetions by parts of the system. The representa: computer subroutines will be added. Intelligence Project---RLE and MJT Computation Center “1. O# SYMBOLIC EXPRESSIONS AND THEIR COMPUTATION BY MACHINE ‘arthy, MIT Computation Center a. Introduction ‘A programming system called LISP (for LIS Processor) has been developed for the IM 704 computer by the Artificial Intelligence Group at MIT. The system was designed to facili- tate experiments with a proposed system called the Advice Taker whereby a machine could be instructeé in declarative as well as imperative sentences and could exhibit "common sense" in carrying out ite instructions. ‘the original proposal for the Advice Taker 1s ccntaited in reference 1, The main require- ment wae a programming system for manipulating © expression’ representing formalized declarative and imperative sentences eo that the Advice Taker system could make deductions. ‘he development of the LISP system went through several stages of simplification in the course of its development and was eventually seen to be based on a scheme for representing the partial recursive functions of a certain class of symbolic expressions. This representation 1s independent of the IBM 704 or any other electronic computer and it now seems expedient to expound the system starting with the class of expressions called S-expressions and the functions called S-functions. In this paper, we first describe the class of S-expressions and S-functions. Then we describe the representation of 8-functions by S-expresaions which enables us to prove that ail computable partial functions have been obtained, to obtain a universal S-function, and to exhibit a eet of questions about S-expressions which cannot be decided by an S-function. We describe the representation of the system in the IBM 704, including the representation of S-expressions by list structures similar to those used by Newell, Simon, and Shaw (see refer- ence 2), and the representation of S-functions by subroutines, Finally, we give some applications to symbolic calculations ineluding analytic differentiation, proof checking, and compiling including a description of the present status of the LISP compiler itself which 1s being written in the eystem. Although we have not carried out the development of recursive functien theory in terus of S-functions and their reprezentation by S-expressions beyond the siuplest theorems, it seems that: formilation of this theory in terms of 3-func- tions has important advantages. Devices such as Godel number- ing are unnecessary and so is the construction of particular Turing machines. (These constructions are all artificial in terus of what is intended to be accomplished by them). The advantage stems from the fact that functions of symbolic expressicns are easily and briefly described as S-expressions and the representation of S-functions by S-expressions is trivial. Moreover, in a large class of cases the S-expression representations of S-functicns translate directly inte effi- cient machine programs for the computation of the functions. Although, the functions described in the manner of this paper include ‘all computable functions of S-expressions, describe many important processes in a very convenient way, and compile into fast running programs for carrying out the processes; there are other kinds of processes whose description by S-func- tions 4s inconvenient and for which the S-functions once found 6o not naturally compile into efficient programs. For this reason, the LISP system includes the possibility of combining S-funetions into Fortran or IAL-like programs. Even this will not provide the flexibility of description of processes hoped for from the Advice Taker system which is beyond the scope of this paper. . 2. Recursive Functions of Symbolic Expressions In this section we define the S-expressions and the S-functions. (Actually they art partial functions. The distinction between @ partial fiction and a function that the former need not be defined for all arguments because, for example, the computation process. defining it may not terminate.) 2.1, S-expressions The expression with which ve shall deal are formed using the special characters "," and "(" and ")" and-an infinite set of distinguishable atomic symbols pj ,Pz+P3y+--- The S-expressions are formed according te the following rules. 1, The atomic symbols p, Py ete are S-expressions , 2. A nul. expression /its also admitted. 3. If-aie an S-expression so is (e). 4, If e, and (ep) are S-expressions #0 1s (e5¢2)- In what follows we shall use sequences of capital Latin letters as atomic symbols. Since we never juxtapose them with- out intervening cowmas they cannot cause confusion by running together. Some examples of S-expreasions are: AB (AB,A) (AB, As sts) ((AB,C),A, (BC. (ByB))) 2.2 Elementary functions and predicates. The functions we shall need are built up from certain elementary ones according to certain recursive rules. There are three elementary predicates: 1. pull te ¢ rullt 4s srue if end only if $-expression e is the null expression/\, (We shall use square brackets and semi-colons for writing functions of S-expressions since parentheses and commas have been pre-empted. Wher writing about functions in géneral we may continue to use parentheses and commas.) 2. atom(e) zcomfe] is‘true if and obly if the S-expression is an atomic symbol. 3. PyPa reoures Py-Pp 18 defined only warn pj and py are both atomic symbols in which case it 1s true 1f and.only if they are the same symbol. ‘his predicate exprenses the distinguishability of the symbols. There are three basic functions of S-expressions whose values are S-expressions. - 4, first @ first ( 1s defined for S-expressions which are aeither null nor atomic. If e has the form (e,,¢) where is an expression, then first (J) =e,. If e has the form (e,) ere sy te sa poteston again re have frst fe] e;- Some examples are: first [{A,B)) =a first [A] 1s undefined first [(A}] =a first (((A,B),c,DJ =(a,B) 5» rest {a} vest [e] 1s also defined for S-expressions which are neither null nor atone. If e has the form (e,,e,) where e, ig an S-expression, then rest (e)=(e,). If e has the form (e,) where e, is an S-expreasion we have rest [6] AN Some examples are: rest faa) « ~ rest [(a)] =/\ meet flaspiolle =(B,C) 6. combine fay 3,3) combine Pregl is defined when e, is not atomic. When e, has the form (e. ), then combine fe, 3¢,]= (91,03) When e2 18 /we have combine [e,s_Ap(0,). Some examples are: conmine iAP) combine [(4,B);(B,C)}=((A,B),B,C) The functions first, rest and combine are related by the relations first eons rest[combine[. eyseay conbine[firat(4] en whenever all the quantities involved are defined. 2.3 Functional Expressions and Functions formed from the elementary functions by composition. Additional functions may be obtained by composing the elementary functions of the preceding section. These functions are described by expressions in the meta-language which should not be confused with the S-expressions being manipulated. For example, the expression first (rest (e]] denotes the second sub- expression of the S-expression e, e.g. first [rest [(a,B,C)] ]=B. In general compositions of first and rest give sub-expressions of an S-expression in a given position within the expression and compositions of combine form S-expreesicns from their sub- expressions. Fer example, combine (xscoubine{y;eombine(z,A } } ) forms « sequence of three terms from the terms, e.g. combine [A combine[(3,C);combinefa, A] ] Ua, (B,C),A)- in order tc be able to name compositions of functions and not merely functional expressions (forms) we use the Church Acnotation. If(1s a functional expression end x,,-++,%, are variables which may oceur ing, tnen A[[a,,+++s% >] denotes the function of n veriables that maps X),-++s%, intof. For example, A{{x],firet[rest[xJ]]1s tae function which selects the second element of a list and we have MCEx}firgt[rest [x]]J[(a, [pc]. a)]-[B.c]- MALxs{A.B ]is the constant function that maps every S-expreseion into [ A,B). (The variables occurring in the list of a h-expression are yound and replacing such @ variable throughcut a A-expression by a vew variable does not change the function represented. Thus >(Cm¥]s combine{ 4,combine[y, i ]]]is the same function as ALLuev) combine [u,nombine[v, 4} ] yout different from A[[y.*}s combine[x,corbine[y,A]]J- If some of the variables in a functional expression or form are bound vy X's and others ere not, we get a function dependent on paraneters or from another point of view a form whowe value is a function when values have been assigned to the variables. 2.4 Conditional Expressions Tet PyyPgy--+sPy be expressions representing propositions and let e;,.-+/%, be arbitrary expressions. The expression [Pyreye + Pei] ia called a conditional expression and its Value is determined from the values assigned to the variables occurring in it as follows: If the value of p, is not defined neither is that of the conditiona expreseion. If p, ie defined and true the value of the conditional expression is that of ¢, if the latter 1s defined and otherwise is undefined. If p, 1s defined and false, then the value of [pprepee ore) te that of [pgregs + Pg ey): Finally 12 p, is felee the value of {pg7e,] is undefined. ‘An example of a conditional expression 1s (nuli[<|—/); atom[+5e,, are allowed expressions so are (PIUS,€;, v1y@q) and (TIMES,e,,....¢,) and represent the sum and produs respectively of Opi Fee This is essentially the Polish notation for functions excert that the inclusion of parentheses and commas allows functions cf variable numbers of arguments. An example of an allowed expression is (DIMES, x, (PLUS, 4A) ,¥) the conventions algearaic notesiea for which 1s X(X+A)¥ our differentiation formula is dirt -n[Ly,x)sLatomLy}—[y~x—r0ne; nd ‘ firex[y] «PLUs—scombine [PLussmaplistCrest(y] sl [a] satee{ first{s] ;x)]}) ;firet(y}=TIMES-scombane[PLUSsmaplist[ rest [¥] s{[8) scombine [TIMES smaplist[rest(y]sA{w] sf mf tre fut rareelesmedfa) x VY TIITI -9- whe derivacive of the above expression somputed by this formuls is (PLUS, (TIMES, ONE, (PLUS,X,A),¥), (TIMES, X, (PLUS, ONE, ZERO) ,¥), {TIMES , X, (PLUS, X,A) , ZERO) } 2.7 Labelled Expression: The A-notation used for naming functions is inadequate for naming recursive functions. For example, if the function named as the second argument of a maplist 1s to be allowed to be recursive an additional notetion is required. We define labelf[a;e] where « is a symbol and e is an expression to be the same as the expression e except that if a occurs ao @ sub-expression of e it 1s understood to refer to the expression e. The symbol a 1s bound in label {a;e] and has nc significance outside this expression. Label acts as a quantifier with respect to its first argument but a quantifier of a different sort frum >. As an example label [subst ;A[ [x3¥38);[nvli[e}9/ jatom[2]—9 [y-8-9x;1-98) ;}—rcombine[ subst [x;y ;firat[s] ]s subst[x:ysrest{s]]}]]] te a name suitable for inclusion in a mapliet of the substitu tion function mentioned earlier. 2.8 Computable Functions In this section we shall show that all functions compu- table by Turing machine are expressable as S-functions. If, as we contend, S-functions are a nore suitable device for developing a theory of computability than Turing machines, the proof in this section 1s out cf place and should be re- placed by a plausibility argument similar to what is called *furing's thesis" to the effect that S-functions satisfy our intuitive notion of effectively computable function. The reader unfamiliar with Turing machines should skip this section. Nevertheless, Turing machines are well entrenched at present 80 we shall content ourselves witt showing that any function computable by Turing machine is ar S-function. This is done as follows: 1. We give a way of describing the instantaneous con- figurations of 2 Turing wachine caleulation by an S-expression. This ipression tust describe the Turing machine, its -10- internal state, the tage, and the square on the tape being read. 2. We give an S-function guee whose argument is an instantaneous configuration and whose value is the immediately succeeding configuration if there 1s one and otherwise ts 0. 3. We construct from suce snother S-funetion, turing whose argunente are 2 Turing wachine, with a canonical initial state and an initial tape in a standard position and whose value is defined exactly when the corresponding Turing machine celevlation terminates and in that; case is the final tape. We shall consider Turing machines as given by sets of quintuples. Each quintuple consists of a state, a symbol read, a symbol to be printed, a direction of motion and a new state. The states are represented by a finite set of symbols, the symbols which may occur by another finite set of symbols (it doesn't matter whether these sets over2ap) and the two directions by the symbols "L" and "R". A quintuple is then represented by an S-expression (st,sy,nsy,dir,nst). The Turing machine is represented by an S-expression. (ist, blank,quinl,...,quink) where ist represents the canonical initial state, bi 1s the symbol used for a blank square (squares beyond the region explicitly represented in the S-expression for a tape are assumed to be blank and are read that vay when reached). As an example we give the representa- tion of a Turing machine which moves to the right on a tape and cowputes the parity of the number of I's on the tape ignoring 0a and stopping when it comes to a blank square: (0,8, (0,0,B,R,0),(0,1,B,R,1),(0,B,0,R,2),(1,0,B,R,1),(141,B.2,0), (1,B,1,R,2)) ‘The machine is assumed to stop if there 1s no quintuple with a given symbol state pair so that the above machine stops as ccon as it enters state 2. A Turing machine tape is represented by an S-expression as follows: The symbels on the squares to the right of the scanned square are given in a list v, the symbols to the left of the seanned square in aliat vand the scanned syabo! as a quantity 4. These are combined in a list (w,u,v). Thus the tape ...bb110201109....« 48 represented by the expression (0,(1,0,2,1,b,b),¢1,2,0,b)) i SER a ae We adjoin tos state to this triplet to make a quadruplet vant (g,w,u,v) which jesuribes the instar taneous configuration of a ~ machine he function suec[m;c] whose arguments are a Turing machine m and a configuration ¢ has as value the immediately suc- ceeding configuration of « provided the state-symbol pair is Listed among the quintupleta of m and otherwise has value zero. sace 4g defined with the aid of auxiliary functions. The first of these find[st;sy;qa] given the triplet (nsy;dirjnst) which consist of the last 3 terms of the quintuplet of u «hich contains (st,sy) as its first two elements. The recursive definition is simplified by defining find [stseysqs) where qe=rest[rest[m)]since qs then represents the list of quintuplets of m. We have find(st;sy;qs) (nul: [qe]—0;f1ret [first [as]) =stffirst[rest(firet [4s) ] ]=sysrest(rest/ first [qs)] }s1—f1na btzey,rest [aa)]] The new auxiliary function is nove [msnay;tape;dir]which gives a new tape triplet obtained by writing nsy on the scanned _~ square of tape, and moving in the d:.rection dir. acve[w,noy;tape;dir) =[dir«L—combine[ [ . nuliftirst [rest [rap2)])— riot [reo%[a] 1st iret{ tiret[rest[tape|]] A:comine[ [muta [rirst [rest ftape]]}*/| 11 rest[tirst[rest{tape]]]j;combine[coubine[nsy; first [rest[rest[tape]]] 1} )}idir-r— combine[[nuli(tirst[rest [rest [tape] ]]}—? first (rest [m) })1—2iret[rirst[rest[rest[tape]]]])s combine [combine/nsy;firet[rest[tape| ]]3. combine{[nuli[riret[rest[rest [tape] ]] 2 11-7 rest(tirst[rest[rest[tape]]]}}}1J,)] ‘The reader should not be alarmed at the monstrous size of the last forma. It rises mainly ‘rom the compositions of first and rest required to select the proper elements of the structure representing the tape. Later we shall describe ways of writing such expressions more concisely. we now have x suce[mze]=[rina[riret[c) ;tiret[rest [cl] rest[rest[m)]] =0-20;1—combine(first[rest [rest[fina[ first[c] ;firet [rest [c}] :rest[rest [m]]]]}} move[m;firat[find{tirst(c] first [rest (6]]; vest rect fin] ]) rect OF tirat Gent [tinal Bi Finally we define "oye t spurding (ia; tape) uth peonn to Eee a a where ‘ufesc)=[ouce[ajo]-o-vrest{e] sta eufarsvoel We reiterate that these definittons by some devices that will be discussed 4 | mechines computation of S-funetsone< -23- gp Self-sppliea vue S-functiona have been described vy 2 class of expres- sions which has been informally introduced. Let us cal] these expresaicns F-expressions. If we. provide a way of translating FP-expressions into S-expressions, we can use S-functions to represent certain functions and predicates of S-expressions. First we shall describe this translation. 3.1 Representation of nctions as S-expressions. {The representation is determined by the following rules. 1. Constant S-expressions can occur as parts of the P-exprespions representing S-functions. An S-expression Eis represented by the S-expression. (QUOTE,€ ) 2. Variables and function names which were represented by strings of lower case letters are represented by the ccr- responding strings of the corresponding upper case letters. ‘Thus we have FIRST, REST and COMBINE, and we shall use x,Y ete. for variables. a 3. A form 1s represented by an S-expression whose first term is the name of the main function and whose remaining terms are the arguments of the function. Thus combine[first[x]; rect[x]} 1s represented by (COMBINE, (FIRST,X),(REST,X)) : 3: MBS RN Scererseetordd® B°ERe dnstea by 7 and 7. The conditiénal expression writel preys Peteps «++» Pye] is represented by (COND, (Py 03) »(Par2a)y +--+ (Pes®y)) 6. AL fxs-0 +£) is represented by (LAMBDA,(X,...,3);£ ) 7. label iE] is represented by (LABEL,G.<) 8. xey is represented by (8Q,X,¥) With these conventions the substitution function mentioned earlier whose F-expression is label [subst ;A{[xs¥38]; [nu11[2]-4/saton[ s]—y [y-3—sx1—a] 51—scombine[ subst [x;y;first[s] J; subst [x;ysrest(s]]]]]] nA is represented by the S-expression. (LABEL, SUBST, (LAMBDA, (X,¥,Z) » (COND, ( (NULL, 3) NIL), ((ATOM,2), (COND, ({EQ,¥,8),X)5(1,2)))+ (1, (COMBINE, (SUBST,X,¥,(PIRST,Z)), fermen, ¥.¥, (REST.7)))Y))) =e This notation is ner foruidable for a human to read, and when we come to the computer form of the system we will see how it can be wade easier by adding some features to the read and print routines without changing the internal compu~ tation processes. 3.2. A Function of S-expreesions which is not an S-function. It was mentioned in section 2.5 that an S-function is not defined for values of its arguments for which the process of evaluation does not terminate. It is easy to give examples of $-functions which are defined for all arguments, or examples which are defined for no arguments, or examples which are defined for some arguments. It would be nice to be able to determine whether a given S-function 1s defined for given arguments. Consider, then, the function def [js] whose value is 1 if the S-function whose corresponding S-expression is £ ie defined for the list of arguments s and is zero otherwise. We assert that def[f 3] 4s not an S-function. (If we assume Turing machine theory thin is an obvious consequence of the results of section 2.8, but in support of the contentions that S-functions are a good vehicle for expounding the theory of recursive functions we give a separate proof). Theorem: def[f3s]1s not an S-function. Proo! Suppose the contrary. Consider the function ex (e]}sf eaet [t5t}-41,1-4F1r8t[ JJ) If def were an S-function g would also be ab S-function. yor any S-funetion u with S-exoression u' gfx] is 1 if ufu'} undefined and is undefined otherwise. consider now g[e']where g' is an S-expression for g. Assume first that g[e')were defined. Tals is precisely the condi- tion that g' be the kind of S-expression for which g is undefined. Contrawise, were g[g']undefined g' would be the kind of S-expression for which g is defined. ‘Thus our assumption that def[f;s}]ie an S-function leads to @ contradiction. The proof is the same as the corresponding proof in Toring machine theory. The simplicity of the rules by which S8-functions are represented as S-expressions makes the develop- nent from scratch simpler, however. ~15- 3-3 The Universal S-function, Apply There is an S-function apply such that if f 4s an S-expression for an S-function(vand args is a list of the fora fargl,...,arg n) where argl,---,arg n are arbitrary S-expressions then epply[f,args}. and Pfarg];...;arg «) are defined for the same values of argi,...,arg n and are equal when defined. apply is defined by apply[f ;args] ~eval [combine [f ;args]]} eval is defined by eval [e}=[ first (e] -NULL—null [eval [first [rest [oJ] ]JAT; > F) first [e) -ATOM-»faton[eval[rirst [rest [e}]] }ots17F] first [e) -EQ—reval/first [rest[e)]) rirst[rest[e]]; first [e] -PIRST—firet [eval [rirst[rest{e})} Js first [e) ~REST—rrest [eval/first [rest[e})}}s first [e] =cOMBINE—ycomb ine [eval [tirst[rest{e]] Jseval[tirst [rest /rest ()3)0): firat [e)=COND—~eveon[rest[e]]; first[first[e) )=LAMBDA—vevian[f iret [rest[tirst[e]]);first [rest [rest [first [e]])}srest fel] 5 aval fetvat bl] paste eeea faonstne [oat fFtret[o]sttrat [rest [first [e}] ]:first [rest [rest [tarst (o])})Jirest[e})}]) where; evcon (¢) = [eval [first [first [o)]]=1-veval [first ead feswstfo9)} p 7 T-reveon[rest[e}]] . and evlam[vare;exp;erge) = [nuli[vars}—revai [exp] ;1—evlam rest [vara] ;eubat [first (args] ;firet[vare) ;exp];rest fares))) The proof of the above assertion is by induction on the subexpressions of e. The process described by the above functions 1s exactly the process used in the hand-worked examples of section 2.5. Y -16- 4, Vartants of Lisp There are a nusber of ways of defining functions of syuboile expressions which are quite similar to the syateo we have adopted. Each of them involves three basic functions, conditional expressions and recursive function definitions, but the class of expressions corr2sponding to S-expressions differs and so do the precise definitions of the functions. We sali describe two of these variants. 4.1 Linear Lisp ‘The L-expressions are defined as follows: 1. A finite list of characters is admitted. 2. Any string of admitted characters is an L-expres- sion. This inoludes the null string denoted by /L. There are three functions of strings 1. first(x] te the first character of the string x. first [A] 1s undefined. Por example, first [ABC]=A. 2. vest[x] is the string cf characters remaining when the first character of the string is deleted. rest[A}is undefined. For example, reat [ABC] =BC 3. combine[x;y] is the string formed by prefixing the character x to the string y. Por example, combine [A;BC]=ABC ‘There are three predicates cn strings 1. charfx), x is a single character 2. ulifz], x is the null string . 3. oy, defined for x and y characters The advantage of linear Lisp is that no characters are given special roles as are parentheses and couma in Lisp. ‘This permits computations with any notation which can be written linearly, The disadvantage of linear Lisp is that the extraction of sub-expressionn is a fairly involved rather than an elementary operation. It is not hard to write in linear Lisp functions corresponding to the basic functions of Lisp so that mathematically linear Lisp includes Lisp. This turns out to be the most convenient way of programming more complicated manipulations. However, it turns out that if the fuactions are to be represented by computer routines Lisp is “tdeliy faster 4.2 Binary Lisp ‘The unsymmetrical status cf first and est “may be a 4 source of uneasiness. If we-admit only two element liste then we can define : first [(e,,e))|=e, rest Teqreall-es combine Tey seg}~(ey 29) We need only two predicates; equality for ‘symbols and atom. The null list can be dispensed © This systen io easier until we try to represent, functs is, after all, the principal sppiigation; | to apply the system to itself we need to be able to write functions.

You might also like