420 likes | 442 Views
Learn the history, eval function, and applications of a recursive interpreter for LISP, handling key functions like car, cdr, and cons. Explore user-defined functions and implementation details in this comprehensive guide.
E N D
Shayan Ehsani Hessameddin Akhlaghpour IMPLEMENTATION OF RECURSIVE LIST-PROCESSOR
Remembrance History Eval funciton Applications Recursive Interpreter of LISP
Remembrance • (defunsqr (x) (times x x) ) • List processors : car,cdr,cons,mapcar. • Functions : bu,rev,assoc. • (lambda (x y) (sqrt (plus (sqr x) (sqr y) (sqr z) ) ) ) • e.g. (sqr 3) = ( (lambda (x) (times x x)) 3 )
History • LISP was first implemented by Steve Russell on an IBM 704 computer. • It was a working LISP interpreter which could be used to run LISP programs, or more properly, 'evaluate LISP expressions.‘ • Since it is written in LISP, it makes use of the facilities of LISP such as car,cdr,etc.
History • The LISP universal function is conventionally called eval since it evaluates a LISP expression. • Eval function shows how strong LISP is !
Eval function • General form : (eval ‘E A) • Example : (eval ‘(cons (quote A ) (quote ( B C ) ) ) nil ) (eval ‘(sqr x) ( (x 3) (y 4) ) • A is a data structure representing the context in which the evaluation to be done.
Eval function The first step is to classify LISP expressions: • Numeric atom 2 • Nonnumeric atom val • Quotation (quote (B C D )) • Conditional (if (null x) nil 1) • Primitive (cons x y) • User defined (mtable text nil)
Implementation • (defun eval (e a) (if (atom e) Handle atoms Handle lists )) • There are two kinds of atoms-numeric and nonnumeric .
Implementation • The value of a numeric atoms is that atom ! • (eval 2 a) = 2 • (( and (atome e) (numberp e)) e ) • Nonnumeric atoms (val,text,etc) must be looked up in the enviroment. • The result of evaluating them is the value to which thay are bound.
Implementation • The value which an atom is bound is determined by the environment. • This is the purpose of the second parameter. • One of the simplest way to represent an environment is an association list. • ( (val 2) (text (to be or not to be )) )
Implementation • We have to look up nonnumeric atoms in the association list representing current environment. • (eval e a) = (assoc e a) • (defun eval (e a) (cond (( and (atom e) (numberp e)) e) (( atom e) (assoc e a) ) …. ))
Implementation • Quote • Quote : The whole purpose of quote is to prevent the evaluation of its argument. • (eval ‘(quote x ) a) = x • ((eq (car e ) ‘quote) (cadr e))
Implementation • Conditional • The conditional expression is different from other built-in functions : it’s argument is not evaluate unitl its value is needed. • (if P T F) : First evaluate P if it is t, then evaluate T; it is nil evaluate F. • (if (eval (cadr e) a) (eval (caddr e) a) (eval (cadddr e) a)) • We are using if to interpret if! • We are calling eval recursively!
Implementation • Primitives and user-defined • General form : (f x1 x2 … xn) • First we should evaluate arguments. • (evargs (cdr e) a) : will be the list of argument values. • We need to construct a list, the ith element of which is (eval xi a) • We need to use mapcar.
Implementation • Primitives and user-defined • ((bu (rev ‘eval) a) xi) = (eval xi a) • (defun evargs (x a) (mapcar (bu (rev ‘eval) a) x )) • We define (apply f x a) in out interpreter. • (apply (car e) (evargs (cdr e) a) a) )
Implementation • Primitives : • The natural structure for the apply function is a cond that handles primitives . • Again we use plus to interpret plus and car to interpret car ... • (defun apply (f x a) (cond ( (eq f ‘plus) (plus (car x) (cadr x)) ) ( (eq f ‘car) (car (car x)) ) ( ( eq f ‘eq) (eq (car x) (cadr x) ) ) …. )
Implementation • User-defined • The steps required to handle user-defined functions is very similar to the steps to invoke a procedure in other languages. • The environment must bind all of the names used in the expression. • The environment is composed of locals and non-locals.
Implementation • User-defined : 1.Evaluate the actual parameters. 2.Bind the formal parameters to the actual parameters. 3.Add these new bindings to the environment of evaluation. 4.Evaluate the body of the function in this envirnment.
Implementation • User-defined : Constructing the environment • (consval text) (lambda (x) (cons val x ) ) • (apply f (x1, x2, x3,…, xn) a) (apply ‘consval (ab dad) ((val baba) (consval (lambda (x) (cons val x) ) ) ) • L = ( lambda (v1, v2, …,vn) B ) • LE = ((v1,x1)…(vn,xn)) • LE = (mapcar ‘list (cadr L) x ) • EE = (append LE a) • (eval (caddr L) EE )
Implementation • We use the let function • e.g. (let ((yek 1) (do 2)) (times yek do) ) • (let ((L (eval f a) )) (let (( LE (mapcar ‘list (cadr L) x) )) (eval (caddr L) (append LE a ) ) ))
Implementation (defun eval (e a) (cond ((and (atom e) (numberp e)) e) ((atom e) (assoc e a)) ((eq (car e) ‘quote) (cadr e)) ((eq (car e) ‘if (if (eval (cadr e) a) (eval (caddr e) a) (eval (cadddr e) a)) ) (t (apply (car e) (evargs (cdr e) a) a) ) ) ) (defun evargs (x a) (mapcar (bu (rev ‘eval) a) x))
Implementation (defun apply (f x a) (cond ((eq f ‘car) (car (car x)) ) ((eq f ‘cdr) (cdr (car x)) ) ((eq f ‘atom) (atom (car x)) ) ((eq f ‘null) (null (car x)) ) ((eq f ‘cons) (cons(car x)) ) ((eq f ‘eq) (eq(car x)) ) . . . (t (let ((L (eval f a) )) (let ((LE (mapcar ‘list (cadr L) x) )) (eval (caddr L) (append LE a)) ) ) ) ) )
Implementation • (function (lambda (x) (times val x)) • We use closures for such cases • Closures consists of two parts: • An ip (instruction part) which points to the piece of program • An ep (environment part) which points to the environment in which the piece of program must be evaluated. • (closure ip ep)
Implementation (defun eval (e a) (cond ((and (atom e) (numberp e)) e) . . ((eq (car e) ‘function) (list ‘closure (cadr e) a)) . . (t (apply (car e) (evargs (cdr e) a) a) ) ) )
Implementation • L = (closure (lambda (x1 … xn) B) ED) • LE = (mapcar ‘list (cadadr L) x) • EE = (append LE (caddr L)) (t (let ((L (eval f a) )) (let ((LE (mapcar ‘list (cadr L) x) )) (eval (caddr L) (append LE a)) ) ) )
Implementation (defun apply (f x a) (cond ((eq f ‘car) (car (car x)) ) . . . (t (let ((L (eval f a) )) (if (eq (car f) ‘closure) (let ((LE (mapcar ‘list (cadadr L) x) )) (eval (caddadr L) (append LE (caddr L))) ) (let ((LE (mapcar ‘list (cadr L) x) )) (eval (caddr L) (append LE a)) ) ) ) ) )
Explicit Erasure • Adopted by Pascal (Dispose function) • Still used in C • Releasing each cell by Programmer
Problems with Explicit Erasure • Programmers must work harder • Who is the “Good” Programmer? • Remember the words against Fortran. • Computers must take care of bookkeeping details. • Violating the Security Principle • Dangling Pointers (pointers that do not point to an allocated cell) Dangling Reference C
Automatic Erasure • Using Reference Count • Include a reference count field in each cell
Automatic Erasure • Using Reference Count decrement (C) : reference count (C) := reference count (C) -1; if reference count (C) = 0 then decrement (C.left); decrement (C.right); return C to free-list; end if.
Handling Cyclic Structures • One Solution is to disallow cyclic structures by eliminating rplaca and rplacd pseudo-functions. • These pseudo-functions have side-effects and do not belong in an applicative programming language. Cycles are prone and difficult to understand.
Garbage Collection • After the exhaustion of the free space, the system enters a “Garbage Collection Phase” in which it identifies all of the inaccessible cells and returns them to free storage. • The “mark-sweep” garbage collector operates in two phases: • The mark phase in which all accessible cells are identified and marked • The sweep phase in which all inaccessible cells are disposed
Garbage Collection Mark phase: for each root R, mark(R) mark (R) : if R is not marked then: set mark bit of R; mark (R^. left); mark (R^. right); end if.
Garbage Collection • Problem: • Mark phase is recursive and requires space for it’s activation records. • Garbage Collector is called only in crisis (no free storage available) • Solution: • Start garbage collection before the last cell is allocated and there is enough space for the stack. • Encode stack in a clever way (reversing link in the marked nodes).
Problems with Garbage Collection • Expensive in a large address space • It should trace down all of the lists • Visit every cell in the memory • There is a high-speed execution of the program until a garbage collection take place. • The system will stop for minutes while a garbage collection is in the progress. • Solutions: • Parallel garbage collection
Easy to extend, preprocess, generate • Lisp has simple structure syntax. • The representation of the Lisp programs as Lisp lists. • It has simplified writing programs that process other Lisp programs such as compilers and optimizer. • It is very easy to manage Lisp programs using other Lisp programs. ( As we saw in the eval interpreter ) • Lisp programmers write many programming tools in Lisp • It has encouraged special–purpose extensions to Lisp for pattern matching, text processing, editing, type checking … • This tools are provided by conventional languages • So why? • In conventional languages they are complicated because Pascal and … have character-oriented syntax
Lisp programming environments developed • Programming Environment • A system that supports all phases of programming including design, program entry, debugging, documentation, maintenance • Lisp programs can manipulate other Lisp programs has led to the development of a wide variety of Lisp programming tools. • The Interlisp programming environment • 1966: BBN( Bolt Beraneck Newman) • 1972: a joint project with Xerox PARC (Palo Alto Research Center) that was renamed Interlisp. • Grew during 1970s in response to the needs and ideas of implementers and users. • The combination of Lisp language and an experimental approach to tool developmentproduced a highly integrated but loosely coupled and flexible programming environment.
LISP’s inefficiency discouraged it’s use • LISP problems: • It is interpreted and often runs two orders of magnitude slower than the code produced by the compiler. • Recursion was inefficient on most machines and in LISP everything (even loops) are implemented recursively. • Dynamic Storage Allocation and Reclamation is slow. • Solutions: • LISP Compilers • LISP Machines
InterlispProgammer’s Assistant • “Do What I Mean”; Intelligent Error Correction • Macros • Undo and Redo
Languages • Imperative languages : • Dependent heavily on assignment statements and a changeable memory for accomplishing a programming task. • Applicative languages: • The central idea is function application that is applying a function to it’s arguments.
LISP, an Applicative Language • Lisp is the closest thing to an applicative language in widespread use. • The Lisp experience is evidence in favor of the practicality of functional programming languages.
LISP, a Function-oriented languages • There is an emphasis on the use of pure functions • Syntactic structure: • Prefix notation • Data structure: • List is the principle data structure • Control structure: • Conditional expression and recursion are basic control structures.