1 / 42

IMPLEMENTATION OF RECURSIVE LIST-PROCESSOR

Shayan Ehsani Hessameddin Akhlaghpour. IMPLEMENTATION OF RECURSIVE LIST-PROCESSOR. Remembrance History Eval funciton Applications. Recursive Interpreter of LISP. Remembrance. ( defun sqr (x) (times x x ) ) List processors : car,cdr,cons,mapcar . Functions : bu,rev,assoc .

peugene
Download Presentation

IMPLEMENTATION OF RECURSIVE LIST-PROCESSOR

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shayan Ehsani Hessameddin Akhlaghpour IMPLEMENTATION OF RECURSIVE LIST-PROCESSOR

  2. Remembrance History Eval funciton Applications Recursive Interpreter of LISP

  3. 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 )

  4. 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.

  5. History • The LISP universal function is conventionally called eval since it evaluates a LISP expression. • Eval function shows how strong LISP is !

  6. 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.

  7. 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)

  8. Implementation • (defun eval (e a) (if (atom e) Handle atoms Handle lists )) • There are two kinds of atoms-numeric and nonnumeric .

  9. 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.

  10. 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 )) )

  11. 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) ) …. ))

  12. 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))

  13. 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!

  14. 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.

  15. 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) )

  16. 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) ) ) …. )

  17. 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.

  18. 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.

  19. 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 )

  20. 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 ) ) ))

  21. 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))

  22. 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)) ) ) ) ) )

  23. 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)

  24. 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) ) ) )

  25. 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)) ) ) )

  26. 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)) ) ) ) ) )

  27. Explicit Erasure • Adopted by Pascal (Dispose function) • Still used in C • Releasing each cell by Programmer

  28. 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

  29. Automatic Erasure • Using Reference Count • Include a reference count field in each cell

  30. 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.

  31. 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.

  32. 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

  33. 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.

  34. 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).

  35. 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

  36. 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

  37. 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.

  38. 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

  39. InterlispProgammer’s Assistant • “Do What I Mean”; Intelligent Error Correction • Macros • Undo and Redo

  40. 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.

  41. 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.

  42. 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.

More Related