CLISP - Conversational LISP
Total Page:16
File Type:pdf, Size:1020Kb
Session 25 Hardware and Software for Artificial Intelligence CLISP - Conversational LISP Warren Teitelman Xerox Palo Alto Research center Palo Alto, California 94304 Abstract CLISP is an attempt to make LISP programs The syntax for a conditional expression is easier to read and write by extending the correspondingly simple:* syntax of LISP to include infix operators, IF-THEN statements, FOR-DO-WHILE statements, Figure 2 and similar ALGOL-like constructs, without Syntax of a conditional Expression changing the structure or representation of the language. CLISP is implemented through <conditional expression> ::x LISP's error handling machinery, rather than (COND <clause> ... <clause>) by modifying the interpreter: when an <clause> :;= (<form> <form>) expression is encountered whose evaluation causes an error, the expression is scanned Note that there are no IF's, THEN's, for possible CLISP constructs, which are BEGIN's, END'S, or semi-colons. LISP avoids then converted to the equivalent LISP the problem of parsing the conditional expressions. Thus, users can freely expression, i.e., delimiting the individual intermix LISP and CLISP without having to clauses, and delimiting the predicates and distinguish which is which. Emphasis in the consequents within the clauses, by requiring design and development of CLISP has been on that each clause be a separate element in the system aspects of such a facility, with the conditional expression, namely a the goal in mind of producing a useful tool, sublist, of which the predicate is always not just another language. To this end, the first element, and the consequent the CLISP includes interactive error correction second element- As an example, let us and many 'Do-what-I-Mean* features. consider the following conditional expression which embodies a recursive * * * definition for FACTORIALS** Figure 3 Recursive Definition of Factorial The syntax of the programming language LISP»»»»» is very simple, in the sense that (COND it can be described concisely, but not in ( (EQ N 0) the sense that LISP programs are easy to read or write! This simplicity of syntax is (T (TIMES N (FACTORIAL (SOB1 N))))) achieved by, and at the expense of, extensive use of explicit structuring, namely grouping through parenthesesization. For the benefit of readers unfamiliar with LISP syntax, the basic element of LISP programs is called a fgr.m. A form is either * Actually, a clause can have more than (1) atomic, or (2) a list, the latter being two consequents, in which case each form denoted by enclosing the elements of the is evaluated and the value of the last list in matching parentheses. In the first form returned as the value of the case, the form is either a variable or a conditional. However, for the purposes constant, and its value is computed of this discussion, we can confine accordingly. In the second case, the first ourselves to the case where a clause has element of the list is the name of a only one consequent. function, and the remaining elements are again forms whose values will be the arguments to that function. In Backus ** The expression in Figure 3 is shown as notation: it would be printed by a special formatting program called PRETTYPRINT. PRETTYPRINT attempts to make the Figure 1 structure of LISP expressions more Syntax of a LISP Form manageable by judicious use of identation and breaking the output into <form> ::« <variable>|<constant>| separate lines. Its existence is a (<function-name> <form> ... <form>) tacit acknowledgment of the fact that LISP programs require more information For example, assign x the value of the sum than that contained solely in the of A and the product of B and C is written parenthesization in order to make them in LISP as (SETQ X (PLUS A (TIMES B C))) . easily readable by people. 686 The first clause in this conditional is the embedding, because embedded sentences are list of two elements ((EQ NO) 1), which more difficult to grasp and understand than says if (EQ N 0) is true, i.e., if N is equivalent non-embedded ones (even when the equal to 0, then return 1 as the value of latter sentences are somewhat longer). the conditional expression, otherwise go on Similarly, most high level programming to the second clause. The second clause is languages offer syntactic devices for (T (TIMES N (FACTORIAL (SUB1 N) ) ) ) , which reducing apparent depth and complexity of a says if T is true (a tautology-the ELSE of program: the reserved words and infix ALGOL conditionals), return the product of N operators used in ALGOL-like languages and (FACTORIAL (SUB1 N)). The latter is simultaneously delimit operands and evaluated by first evaluating (SUB1 N), and operations, and also convey meaning to the then calling FACTORIAL (recursively) on this programmer. They are far more intuitive value. than parentheses. In fact, since LISP uses parentheses (i.e., lists) for almost all As a result of the structuring of syntactic forms, there is very little conditional expressions, LISP does not have information contained in the parentheses for to search for words such as IF, THEN, ELSE, the person reading a LISP program, and so ELSEIF, etc., when interpreting or compiling the parentheses tend mostly to be ignored: conditional expressions in order to delimit the meaning of a particular LISP expression clauses and their constituents: this for people is found almost entirely in the grouping is specified by the parentheses, words, not in the structure. For example, and is performed at input time by the READ the expression in Figure 4 program which creates the list structure used to represent the expression. Figure 4 Similarly, LISP does not have to worry about careless Definition of Factorial how to parse expressions such as A+B+C, since (A+B)*C must be written unambiguously (COND (EQ N 0) 1) as (TIMES (PLUS A B) c), and A+(B*C) as (T TIMES N FACTORIAL ( (SUB1 N) ) ) (PLUS A (TIMES B C)). In fact, there are no reserved words in LISP such as IF, THEN, is recognizable as FACTORIAL even though AND, OR, FOR, DO, BEGIN, END, etc., nor there are five misplaced or missing reserved characters like + , -, *, /, =, «-, parentheses. Grouping words together in etc.* This eliminates entirely the need for parentheses is done more for LISP's benefit, parsers and precedence rules in the LISP than for the programmer's. interpreter and compiler, and thereby makes program manipulation of LISP programs CLISP is designed to make LISP programs straightforward. In other words, a program easier to read and write by permitting the that "looks at" other LISP programs does not user to employ various infix operators, IF- need to incorporate a lot of syntactic THEN- ELSE statements, FOR-DO-WHILE-UNLESS- information. For example, a LISP FROM-TO-etc. expressions, which are interpreter can be written in one or two automatically converted to equivalent LISP pages of LISP code.' It is for this reason expressions when they are first interpreted. that LISP is by far the most suitable, and For example, FACTORIAL could be written in frequently used, programming language for CLISP as shown in Figure 5. writing programs that deal with other programs as data, e.g., programs that Figure 5 analyze, modify, or construct other CLISP Definition Of FACTORIAL programs. (IF N=0 THEN 1 ELSE N*(FACTORIAL N-1)) However, it is precisely this same simplicity of syntax that makes LISP Note that this expression would be programs difficult to read and write represented internally (after it had been (especially for beginners), 'pushing down' interpreted once) as shown in Figure 3, so is something programs do very well, and that programs that might have to analyse or people do poorly. As an example, consider otherwise process this expression could take the following two "equivalent" sentences: advantage of the simple syntax. "The rat that the cat that the dog CLISP also contains facilities for making that I owned chased caught ate the sense out of expressions such as the cheese." careless conditional shown in Figure 4. Furthermore, CLISP will detect those cases versus which would not generate LISP errors, but are nevertheless obviously not what the "I own the dog that chased the cat programmer intended. For example, the that caught the rat that ate the expression (QUOTE <expression> <form>) will cheese." not cause a LISP error, but <form> would never be seen by the interpreter. This is Natural language contains many linguistic clearly a parentheses error. CLISP uses devices Buch as that illustrated in the both local and global information to detect, second sentence above for minimizing and where possible, repair such errors. However, this paper will concentrate * except for parentheses (and period), primarily on the syntax extension aspects of which are used for indicating structure, CLISP, and leave a discussion of the and space and end-of-line, which are semantic issues for a later time. used for delimiting identifiers. 687 There have been similar efforts in other 1) (LIST X*FACT N), where FACT is the name LISP systems, most notably the MLISP of a variable, means (LIST (X*FACT) N). language at Stanford.* CLISP differs from these in that it does not attempt to replace 2) (LIST X*FACT N), where FACT is not the the LISP language so much as to augment it. name of a variable but instead is the In fact, one of the principal criteria in name of a function, means the design of CLISP was that users be able (LIST X*(FACT N)>, i.e., N is FACT'S to freely intermix LISP and CLISP without argument. having to identify which is which. Users can write programs, or type in expressions 3) (LIST X*FACT(N)), FACT the name of a for evaluation, in LISP, CLISP, or a mixture function (and not the name of a of both.