Maclennan-chap11-Lisp.ppt

advertisement
Cons operation calls for the allocation of a new
cons-cell, like new in Pascal.
How to make it free?
2
1. Explicit Erasure
3

Adopted by Pascal (Dispose function)

Releasing each cell by Programmer

Linking the cell on to a Free-List
Problems with Explicit Erasure

Programmers must work harder
–
Who is the “Good” Programmer?

–

Computer Take care of bookkeeping Details
Violating the Security Principle
–
4
Remember the words against Fortran
Dangling Pointers
( pointers that do not point to an allocated cell)
Dangling Pointer



A cell still referenced by
several other lists
Storage allocator
reuses this cell
Dangling
Ref.
Corrupt the storage
allocator’s Data
structures
Free-List
5
C
Garbage Collection with no explicit
allocation/de-allocation: Responsible
Design Principle
Reference count

Explicit Erasure is Low level And Error-prone
–

Keep track of the accessibility of each cell
–
–
7
Return a cell to free storage when it is no longer
needed (no longer accessible from the program)
A cell is accessible only if it is referenced by the
other accessible cells.
The cells that are used by the interpreter are
Directly Accessible
Reference count

Maintain correctly
–
–
Increment with additional reference
Decrement when a reference destroyed



Free-list become exhausted no more freecell available the program aborted
–
8
Overwrite the pointer( rplaca , rplacd)
The cell containing the pointer become inaccessible
(reference count becomes zero)
95% of reference counts are 1
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.
9
Cyclic structures


Are not reclaimed.
One solution: Disallow cyclic structures
–

But some say:
–

10
Error prone & difficult to understand
Cyclic structures are necessary for some problems.
Another solution 
3. Garbage Collection



Garbage : inaccessible cells which are not available
for reuse
Inaccessible cells are abandoned
After the exhaustion of the free space the system
enters a Garbage Collection Phase in which it
identifies all of the unused cells and return them to
free storage
–

Mark-Sweep garbage collector
–
–
11
Ignores the storage reclamation until a crisis develops then
deals with that
Mark phase : marks all of the accessible cells
Sweep phase : places all the inaccessible cells on the free
list
Mark phase
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.
12
Mark phase

There is a problem:
–
Mark phase is recursive

–

Garbage Collector is called only in crisis (no free storage
available)
So how does this work???
–
–
13
Requires space for it’s activation records.
Invoke G.C before the last cell is allocated and there is
enough space for G.C ‘s stack
Encode stack in a clever way (reversing link in the marked
nodes)
sweep phase

Unmarked cell:
–

Inaccessible & can be linked on to the Free-list
Marked cell:
–
Accessible & we reset its mark bit for the next
garbage collection.
Sweep phase:
for each cell c:
if c is marked then reset c’s mark bit,
else link c onto the free-list.
14
Problems with Garbage Collection

Expensive in a large address space
–
–

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.
This is apparent in an interactive system
A serious problem in real time situations (the program most
be guaranteed to respond at a certain amount of time )
solutions
–
15
It should trace down all of the lists
Visit every cell in the memory
Parallel garbage collection: G.C takes place in parallel with
normal program execution
LISP Evaluation

Successful in Artificial Intelligence
–

Ability to represent and manipulate complex interrelationships
among symbolic data.
Suited to iII-Specified Problems
–
–
(In artificial intelligence) the problem will not well understood
Specification of Abstract Data Types:



–
On ill-defined problems this methodology does not work well:



16
1st decide what data structures and data types
2nd necessary operation in terms of their input and output
Finally the representation of the data structures and operators fixed
What operations on a data type will be required is unknown.
It is difficult to fix the input-output when ultimate requirements that the
data structure must satisfy are not clear.
Lisp with few restriction on invocation of procedures and passing
parameters is well suited.
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?

17
In conventional languages they are complicated because Pascal and
… have character-oriented syntax
Lisp programming environments
developed

Programming Environment
–


Lisp programs can manipulate other Lisp programs has led to
the development of a wide variety of Lisp programming tools.
The Interlisp programming environment
–
–
–
–
18
A system that supports all phases of programming including
design, program entry, debugging, documentation, maintenance
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
Why doesn’t every one use it?
 Because it is interpreted and often runs two orders of
magnitude slower than the code produced by the
compiler
( 2*o(n) n-> compiler)
 recursion was inefficient on most machines
(we don’t have loop in Lisp)
 Dynamic storage allocation (and reclamation) is one
of the more expensive aspects but it is also one of
the most valuable (it was a slow process)

19
Languages

Imperative languages :
–

Applicative languages:
–
20
Dependent heavily on assignment statements and
a changeable memory for accomplishing a
programming task.
The central idea is function application that is
applying a function to it’s arguments.
Lisp shows what can be done with an
applicative language


21
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.
Function-oriented languages


There is an emphasis on the use of pure functions
Syntactic structure:
–

Data structure:
–

List is the principle data structure
Control structure:
–
22
Prefix notation
Conditional expression and recursion are basic control
structures.
Function-oriented language properties


High level of abstraction which removes details and
committing many classes of errors. (abstraction
principle)
Independent of assignment statements, therefore
allowing functions to be evaluated in many different
orders.
–

23
Suitable for parallel programming.
It is easier to prove the correctness mathematically
and more suitable for analysis.
Download