lisp gal oz based on: john mitchell’s presentation and book
TRANSCRIPT
![Page 1: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/1.jpg)
Lisp
Gal Oz
Based on: John Mitchell’s
Presentation and book.
![Page 2: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/2.jpg)
Agenda
Characteristics (good language ideas):• Motivation application• Abstract machine• Theoretical foundation
Properties:• Functional programming• Dynamic typing, strongly typed
Implantation:• Basic functions, basic usage• Types and Data model• Garbage collection and other good concepts
![Page 3: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/3.jpg)
Lisp usage today:
AI programming First language teaching – because its
elegant and minimalist language A lot of open source programming (open
source libraries and application)• Emacs editor • gtk graphical toolkit (linux)
Scripting language (Emacs/ AutoCad and more)
![Page 4: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/4.jpg)
Lisp summary
Many different dialects• Lisp 1.5, Maclisp, …, Scheme, ...• CommonLisp has many additional features• This presentation: a fragment of Lisp 1.5,
approximatelyBut ignore static/dynamic scope until later in course
Simple syntax(+ 1 2 3)(+ (* 2 3) (* 4 5))(f x y)
Easy to parse (Looking ahead: programs as data)
![Page 5: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/5.jpg)
Lisp, 1960
Look at Historical Lisp• Perspective
– Some old ideas seem old– Some old ideas seem new
• Example of elegant, minimalist language• Not C, C++, Java: a chance to think differently• Illustrate general themes in language design
Supplementary reading (optional)• McCarthy, Recursive functions of symbolic
expressions and their computation by machine, CACM, Vol 3, No 4, 1960.
![Page 6: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/6.jpg)
John McCarthy
Pioneer in AI• Formalize common-
sense reasoning
Also• Proposed timesharing• Mathematical theory• ….
Lisp stems from interest in
symbolic computation (math, logic)
![Page 7: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/7.jpg)
McCarthy’s 1960 Paper
Interesting paper with• Good language ideas, succinct presentation• Some feel for historical context• Insight into language design process
Important concepts• Interest in symbolic computation influenced design• Use of simple machine model• Attention to theoretical considerations
Recursive function theory, Lambda calculus
• Various good ideas: Programs as data, garbage collection
![Page 8: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/8.jpg)
Motivation for Lisp
Advice Taker• Process sentences as input, perform logical
reasoning Symbolic integration, differentiation
• expression for function --> expression for integral (integral ‘(lambda (x) (times 3 (square x))))
Motivating application part of good lang design• Keep focus on most important goals• Eliminate appealing but inessential ideas
Lisp symbolic computation, logic, experimental prog.
C Unix operating systemSimula simulationPL/1 “kitchen sink”, not successful in long run
![Page 9: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/9.jpg)
Abstract Machine
Concept of abstract machine:• Idealized computer, executes programs directly• Capture programmer’s mental image of execution• Not too concrete, not too abstract
Examples• Fortran
– Flat register machine; memory arranged as linear array – No stacks, no recursion.
• Algol family– Stack machine, contour model of scope, heap storage
• Smalltalk– Objects, communicating by messages.
![Page 10: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/10.jpg)
Execution Model (Abstract Machine)
Language semantics must be defined• Too concrete
– Programs not portable, tied to specific architecture– Prohibit optimization (e.g., C eval order undefined in expn)
• Too abstract– Cannot easily estimate running time, space (difficult to
write efficient code)
Lisp: IBM 704, but only certain ideas …• Address, decrement registers -> cells with two parts• Garbage collection provides abstract view of
memory
![Page 11: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/11.jpg)
Theoretical Considerations
McCarthy’s description• “ … scheme for representing the partial recursive
functions of a certain class of symbolic expressions”
Lisp uses• Concept of computable (partial recursive)
functions– Want to express all computable functions
• Function expressions– known from lambda calculus (developed A. Church)– lambda calculus equivalent to Turing Machines, but
provide useful syntax and computation rules
![Page 12: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/12.jpg)
Functional programming
Pure functions• Function are variable like strings and numbers.• There aren’t any free variables that influence on the
machine state. Therefore no side-effects• Every new proceeding of data generate a new data.
this simplify proofs
First-class and higher-order functions• Function can be delivered and import trough other
functions. That mean that you can get function as a variable and execute her
Recursion• high usage In recursion
![Page 13: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/13.jpg)
Dynamic typing
Type checking is performed at run-time as opposed to at compile-time• slow operation vs. more flexible
In dynamic typing, values have types but variables do not• that is, a variable can refer to a value of any
type
Scheme is statically scoped variant of lisp
![Page 14: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/14.jpg)
Strongly typed
preventing the compiling or running of source code which uses data in what is considered to be an invalid way• an integer division operation may not be used upon
strings• The Lisp system detects these errors, terminates
evaluation and print an error massage to the screen
Strongly typed may result in runtime type errors.• The Lisp system detects these errors, terminates
evaluation and print an error massage to the screen.
![Page 15: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/15.jpg)
Example
C like:Int a = 2;Char b = ‘2‘; #concatenate(a, b) # Type Error #add(a, b) # Type Error concatenate(str(a), b) # Returns '22' add(a, int(b)) # Returns 4
Lisp:(+ ‘(2) 2) # Type Error
![Page 16: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/16.jpg)
Innovations in the Design of Lisp
Expression-oriented• function expressions • conditional expressions
– Unique if-then-else expression structure.
• recursive functions
Abstract view of memory• Cells instead of array of numbered locations• Garbage collection
Programs as data (first homoiconic language) Higher-order functions Recursive function
![Page 17: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/17.jpg)
Parts of Speech
Statement load 4094 r1• Imperative command• Alters the contents of previously-accessible
memory Expression (x+5)/2
• Syntactic entity that is evaluated• Has a value, need not change accessible memory• If it does, has a side effect
Declaration integer x• Introduces new identifier• May bind value to identifier, specify type, etc.
![Page 18: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/18.jpg)
Types: Atoms and Pairs
Atoms include numbers, indivisible “strings” <atom> ::= <smbl> | <num>
<smbl> ::= <char> | <smbl><char> | <smbl><digit>
<num> ::= <digit> | <num><digit>
Dotted pairs (the basic data structure)• Write (A . B) for pair• Symbolic expressions, called S-expressions: <sexp> ::= <atom> | (<sexp> . <sexp>)
![Page 19: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/19.jpg)
Lisp Memory Model
Cons cells Atoms and lists represented by cells
Address Decrement
Atom A
Atom B
Atom C
0
![Page 20: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/20.jpg)
Sharing
(a) (b)
Both structures could be printed as ((A.B) . (A.B))
Which is result of evaluating (cons (cons 'A 'B) (cons 'A 'B)) ?
A B A B A B
Note: Scheme actually prints using combination of list and dotted pairs
![Page 21: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/21.jpg)
Basic Functions
Functions on atoms and pairs:• cons – combine two atoms or lists.
(cons (quote A) (quote B)) => pair of atoms “A” and “B”(cons a b) => pair containing the value of a and b.
• car – content of the address register.(car(list 1 2 3)) => 1
• cdr - content of the decrement register.(cdr(list 1 2 3)) => (2 3)
• eq – an equality test.(eq ‘1 (car(list (‘1 ‘2)))) => T
• atom – test if whether this argument is an atom.(atom (list 1 2)) => nil
![Page 22: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/22.jpg)
Basic Functions
Declarations and control:• cond – conditional expression (more to come).
• lambda - defining function declaration.
(lambda (x) (cond ((atom x) x) (T (cons ‘A x))))
function f(x) = if atom(x) then x else cons(‘A’,x)
• define/defun – defined new variable (string number or function).
(defun factorial lambda (n)
(cond ((<= n 1) 1) (T (* n (factorial (- n 1)))))
• eval – evaluate an expression.
(eval (‘3)) => 3
![Page 23: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/23.jpg)
Basic Functions
More declarations and control function:• quote or ‘ – take a single argument and return
it verbatim.‘(+ a b)=> (+ a b)
Functions with side-effects• rplaca – replace the address register value.
(rplacea (list 1 2) 3) => (3 2)
• rplacd - replace the decrement register value.(rplaced (list 1 2) 3) => (1 3)
![Page 24: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/24.jpg)
Examples
(+ 4 5) expression with value 9
(+ (+ 1 2) (+ 4 5)) evaluate 1+2, then 4+5, then 3+9 to get value
(cons (quote A) (quote B))pair of atoms A and B
(quote (+ 1 2))evaluates to list (+ 1 2)
'(+ 1 2)same as (quote (+ 1 2))
![Page 25: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/25.jpg)
Cond – conditional Functions
(cond (p1 e1) ... (pn en) )
Is the same as:
if p1 then e1
else if p2 then e2
…
else if pn then en
proceed from left to right. stop at the first pi thats pi =true.
When find the first pi with value true, eval this ei
![Page 26: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/26.jpg)
Conditional Expressions in Lisp
Generalized if-then-else (cond (p1 e1) (p2 e2) … (pn en) )
– Evaluate conditions p1 … pn from left to right
– If pi is true, then evaluate ei
– Value of ei is value of expression
No value for the expression if no pi true, or
p1 … pi false and pi+1 has no value, or
relevant pi true and ei has no value
Conditional statements in assembler
Conditional expressions apparently new in Lisp
![Page 27: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/27.jpg)
Examples
(cond ((< 2 1) 2) ((< 1 2) 1))
has value 1
(cond ((< 2 1 ) 2) ((< 3 2) 3))
has no value
(cond (diverge 1) (true 0))
no value, if expression diverge loops forever
(cond (true 0) (diverge 1))
has value 0
![Page 28: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/28.jpg)
Evaluation of Expressions
Read-eval-print loop Function call (function arg1 ... argn)
• evaluate each of the arguments• pass list of argument values to function
Special forms do not eval all arguments • Example (cond (p1 e1) ... (pn en) )
– proceed from left to right
– find the first pi with value true, eval this ei
• Example (quote A) does not evaluate A
![Page 29: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/29.jpg)
Function Expressions
Form (lambda ( parameters ) ( function_body ) )
Syntax comes from lambda calculus: f. x. f (f x) (lambda (f) (lambda (x) (f (f x))))
Defines a function but does not give it a name t ( (lambda (f) (lambda (x) (f (f x)))) (lambda (x) (+ 1 x))) )
![Page 30: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/30.jpg)
Example
(define twice (lambda (f) (lambda (x) (f (f x)))))
(define inc (lambda (x) (+ 1 x)))
((twice inc) 2) 4
![Page 31: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/31.jpg)
Strictness
An operator or expression form is strict if it can have a value only if all operands or subexpressions have a value.
Lisp cond is not strict, but addition is strict • (cond (true 1) (diverge 0))
• (+ e1 e2)
Details: (define loop (lambda (x) (loop x))) diverge (loop ())
![Page 32: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/32.jpg)
Garbage Collection
Garbage: At a given point in the execution of a program P, a
memory location m is garbage if no continued execution of P from this point can access location m.
Garbage Collection: • Detect garbage during program execution• GC invoked when more memory is needed• Decision made by run-time system, not program
This can be very convenient. For example: in building text-formatting program, ~40% of programmer time wasted on memory management.
![Page 33: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/33.jpg)
Examples
(car (cons ( e1) ( e2 ) )) Cells created in evaluation of e2 may be
garbage, unless shared by e1 or other parts of program
((lambda (x) (car (cons (… x…) (... x ...))) '(Big Mess))
The car and cdr of this cons cell may point to overlapping structures.
![Page 34: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/34.jpg)
Mark-and-Sweep Algorithm
Assume tag bits associated with data Need list of heap locations named by
program Algorithm:
• Set all tag bits to 0.
• Start from each location used directly in the program. Follow all links, changing tag bit to 1
• Place all cells with tag = 0 on free list
![Page 35: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/35.jpg)
Why Garbage Collection in Lisp?
McCarthy's paper says this is • “… more convenient for the programmer than
a system in which he has to keep track of and erase unwanted lists."
Does this reasoning apply equally well to C?
Is garbage collection "more appropriate" for Lisp than C? Why?
![Page 36: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/36.jpg)
Programs As Data
Programs and data have same representation Eval function used to evaluate contents of list Example: substitute x for y in z and evaluate
(define substitute (lambda (x y z) (cond ((null z) z)
((atom z) (cond ((eq z y) x ) (true z))) (true (cons (substitute x y (car z))
(substitute x y (cdr z)))))))(define substitute-and-eval (lambda (x y z) (eval (substitute x y z))))(substitute-and-eval '3 'x '(+ x 1))
![Page 37: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/37.jpg)
Examples
Suppose you want to write a function that takes a number n and returns a function that adds n to its argument. (defun addn (n) ‘(lambda (x) (+ x n) ) )
To run it: ((addn(5)) 2) => 7
How do you do it in c?
![Page 38: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/38.jpg)
Recursive Functions
Want expression for function f such that(f x) = (cond ((eq x 0) 0) (true (+ x (f (- x 1)))))
Try(lambda (x) (cond ((eq x 0) 0) (true (+ x (f (- x
1))))))
but f in function body is not defined.
McCarthy's 1960 solution was operator “label”(label f
(lambda (x) (cond ((eq x 0) 0) (true (+ x (f (- x 1 )))))))
![Page 39: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/39.jpg)
Recursive vs. non-recursive declarations
Recursive definition• (define f (lambda (x) (cond ((eq x 0) 0) (true (+ x (f (- x
1 )))))))• Legal Scheme: treats inner f as function being
defined
Non-recursive definition• (define x (+ x 2))• Error if x not previously defined
While evaluating arguments to + in expression (+ x 2):Unbound variable: xABORT: (misc-error)
![Page 40: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/40.jpg)
Higher-Order Functions
Function that either • takes a function as an argument• returns a function as a result
Example: function composition(define compose (lambda (f g) (lambda (x) (f (g x)))))
Example: maplist(define maplist (lambda (f x) (cond ((null x) ()) (true (cons (f (car x)) (maplist f (cdr x ) ))))))
![Page 41: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/41.jpg)
Example
(define inc (lambda (x) (+ x 1)))(maplist inc '(1 2 3)) (2 3 4)
Scheme preamble:(define true #t)(define false #f)(define eq eq?)(define null null?)
![Page 42: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/42.jpg)
Lisp inheritors today:
Two major dialects for general-purpose programming today:• Common Lisp, Scheme,
LISP, , Logo, Clojure,ISLISP, Newlisp, Arc, SKILL, Racket
Scripting language dialects:• Emacs lisp• AutoCad (AutoLISP)
Many influenced languge• ML, Perl, Python, Smalltalk, Ruby, Dylan, Haskell,
Mathematica, Rebol, Qi, Lua, JavaScript, LPC, Forth, Nu, OPS5, CLU, Falcon, Io, Ioke
![Page 43: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/43.jpg)
Summary: Contributions of Lisp
Successful language • symbolic computation, experimental
programming
Specific language ideas Expression-oriented: functions and recursion Lists as basic data structures Programs as data, with universal function eval Stack implementation of recursion via "public
pushdown list" Idea of garbage collection.
![Page 44: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/44.jpg)
Language speeds
www.bagley.org/~doug/shoutout: Completely Random and Arbitrary Point System
![Page 45: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/45.jpg)
Lisp
Lots of Irritating Superfluous Parentheses
Who wrote the language?
![Page 46: Lisp Gal Oz Based on: John Mitchell’s Presentation and book](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649cd85503460f949a102a/html5/thumbnails/46.jpg)
The END
Presentation by: Gal Oz
Based on: John Mitchell’s
Presentation and book.
Thanks to:
Wikipedia, Puder Avi, xkcd, Gidon Eitan, Itay Abraham and
http://www.apl.jhu.edu/~hall/lisp.html