Upload
raymond-charles
View
216
Download
0
Embed Size (px)
Citation preview
Shayan EhsaniHessameddin Akhlaghpour
•Remembrance •History•Eval funciton•Applications
(defun sqr (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)
3
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.
4
The LISP universal function is conventionally called eval since it evaluates a LISP expression.
Eval function shows how strong LISP is !
5
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.
6
The first step is to classify LISP expressions:Numeric atom 2Nonnumeric atom valQuotation (quote (B C D
))Conditional (if (null x) nil
1)Primitive (cons x y)User defined (mtable text
nil)
7
(defun eval (e a)(if
(atom e) Handle atoms Handle lists ))
There are two kinds of atoms-numeric and nonnumeric .
8
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.
9
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 )) )
10
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) ) …. ))
11
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))
12
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!
13
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.
14
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) )
15
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)
) )…. )
16
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.
17
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.
18
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 )
19
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 ) ) ))
20
(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))
21
(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)) ) ) ) ))
22
(function (lambda (x) (times val x))We use closures for such casesClosures 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)
23
(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) ) ))
24
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)) ) ))
25
(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)) ) ) ) ))
26
Adopted by Pascal (Dispose function)
Still used in CReleasing each cell by
Programmer
28
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)
29
C
Dangling Reference
Using Reference Count
Include a reference count field in each cell
30
Using Reference Count
31
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.
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
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:1. The mark phase in which all accessible cells
are identified and marked2. The sweep phase in which all inaccessible
cells are disposed
33
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
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
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
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
38
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.
39
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
40
“Do What I Mean”; Intelligent Error Correction
MacrosUndo and Redo
41
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.
42
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.
43
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.
44