Presentation on theme: "Gal Oz Based on: John Mitchell’s Presentation and book."— Presentation transcript:
1Gal Oz Based on: John Mitchell’s Presentation and book. LispGal OzBased on: John Mitchell’sPresentation and book.
2Agenda Characteristics (good language ideas): Properties: Motivation applicationAbstract machineTheoretical foundationProperties:Functional programmingDynamic typing, strongly typedImplantation:Basic functions, basic usageTypes and Data modelGarbage collection and other good concepts
3Lisp usage today: AI programming First language teaching – because its elegant and minimalist languageA lot of open source programming (open source libraries and application)Emacs editorgtk graphical toolkit (linux)Scripting language (Emacs/ AutoCad and more)
4Lisp summary Many different dialects Simple syntax Lisp 1.5, Maclisp, …, Scheme, ...CommonLisp has many additional featuresThis presentation: a fragment of Lisp 1.5, approximatelyBut ignore static/dynamic scope until later in courseSimple syntax( )(+ (* 2 3) (* 4 5))(f x y)Easy to parse (Looking ahead: programs as data)
5Lisp, 1960 Look at Historical Lisp Supplementary reading (optional) PerspectiveSome old ideas seem oldSome old ideas seem newExample of elegant, minimalist languageNot C, C++, Java: a chance to think differentlyIllustrate general themes in language designSupplementary reading (optional)McCarthy, Recursive functions of symbolic expressions and their computation by machine, CACM, Vol 3, No 4, 1960.
6John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning Proposed timesharingMathematical theory….Lispstems from interest in symbolic computation (math, logic)
7McCarthy’s 1960 Paper Interesting paper with Important concepts Good language ideas, succinct presentationSome feel for historical contextInsight into language design processImportant conceptsInterest in symbolic computation influenced designUse of simple machine modelAttention to theoretical considerationsRecursive function theory, Lambda calculusVarious good ideas: Programs as data, garbage collection
8Motivation for Lisp Advice Taker Symbolic integration, differentiation Process sentences as input, perform logical reasoningSymbolic integration, differentiationexpression for function --> expression for integral(integral ‘(lambda (x) (times 3 (square x))))Motivating application part of good lang designKeep focus on most important goalsEliminate appealing but inessential ideasLisp symbolic computation, logic, experimental prog.C Unix operating systemSimula simulationPL/ “kitchen sink”, not successful in long run
9Abstract Machine Concept of abstract machine: Examples Idealized computer, executes programs directlyCapture programmer’s mental image of executionNot too concrete, not too abstractExamplesFortranFlat register machine; memory arranged as linear arrayNo stacks, no recursion.Algol familyStack machine, contour model of scope, heap storageSmalltalkObjects, communicating by messages.
10Execution Model (Abstract Machine) Language semantics must be definedToo concretePrograms not portable, tied to specific architectureProhibit optimization (e.g., C eval order undefined in expn)Too abstractCannot easily estimate running time, space (difficult to write efficient code)Lisp: IBM 704, but only certain ideas …Address, decrement registers -> cells with two partsGarbage collection provides abstract view of memory
11Theoretical Considerations McCarthy’s description“ … scheme for representing the partial recursive functions of a certain class of symbolic expressions”Lisp usesConcept of computable (partial recursive) functionsWant to express all computable functionsFunction expressionsknown from lambda calculus (developed A. Church)lambda calculus equivalent to Turing Machines, but provide useful syntax and computation rules
12Functional programming Pure functionsFunction are variable like strings and numbers.There aren’t any free variables that influence on the machine state. Therefore no side-effectsEvery new proceeding of data generate a new data. this simplify proofsFirst-class and higher-order functionsFunction can be delivered and import trough other functions. That mean that you can get function as a variable and execute herRecursionhigh usage In recursion
13Dynamic typingType checking is performed at run-time as opposed to at compile-timeslow operation vs. more flexibleIn dynamic typing, values have types but variables do notthat is, a variable can refer to a value of any typeScheme is statically scoped variant of lisp
14Strongly typedpreventing the compiling or running of source code which uses data in what is considered to be an invalid wayan integer division operation may not be used upon stringsThe Lisp system detects these errors, terminates evaluation and print an error massage to the screenStrongly typed may result in runtime type errors.The Lisp system detects these errors, terminates evaluation and print an error massage to the screen.
15Example C like: Lisp: Int a = 2; Char b = ‘2‘; #concatenate(a, b) # Type Error#add(a, b) # Type Errorconcatenate(str(a), b) # Returns '22'add(a, int(b)) # Returns 4Lisp:(+ ‘(2) 2) # Type Error
16Innovations in the Design of Lisp Expression-orientedfunction expressionsconditional expressionsUnique if-then-else expression structure.recursive functionsAbstract view of memoryCells instead of array of numbered locationsGarbage collectionPrograms as data (first homoiconic language)Higher-order functionsRecursive function
17Parts of Speech Statement load 4094 r1 Expression (x+5)/2 Imperative commandAlters the contents of previously-accessible memoryExpression (x+5)/2Syntactic entity that is evaluatedHas a value, need not change accessible memoryIf it does, has a side effectDeclaration integer xIntroduces new identifierMay bind value to identifier, specify type, etc.
18Types: 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 pairSymbolic expressions, called S-expressions:<sexp> ::= <atom> | (<sexp> . <sexp>)
19Lisp Memory Model Cons cells Atoms and lists represented by cells AddressDecrementCons cellsAtoms and lists represented by cellsAtomAAtomBAtomC
20Sharing (a) (b) Both structures could be printed as ((A.B) . (A.B)) Which is result of evaluating(cons (cons 'A 'B) (cons 'A 'B)) ?ABABABNote: Scheme actually prints using combination of list and dotted pairs
21Basic 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)) => 1cdr - content of the decrement register.(cdr(list 1 2 3)) => (2 3)eq – an equality test.(eq ‘1 (car(list (‘1 ‘2)))) => Tatom – test if whether this argument is an atom.(atom (list 1 2)) => nil
22Basic 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
23Basic Functions More declarations and control function: quote or ‘ – take a single argument and return it verbatim.‘(+ a b)=> (+ a b)Functions with side-effectsrplaca – 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)
24Examples (+ 4 5) (+ (+ 1 2) (+ 4 5)) (cons (quote A) (quote B)) 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))
25Cond – conditional Functions (cond (p1 e1) (pn en) )Is the same as:if p1 then e1else if p2 then e2…else if pn then enproceed from left to right. stop at the first pi thats pi =true.When find the first pi with value true, eval this ei
26Conditional Expressions in Lisp Generalized if-then-else(cond (p1 e1) (p2 e2) … (pn en) )Evaluate conditions p1 … pn from left to rightIf pi is true, then evaluate eiValue of ei is value of expressionNo value for the expression if no pi true, orp1 … pi false and pi+1 has no value, orrelevant pi true and ei has no valueConditional statements in assemblerConditional expressions apparently new in Lisp
27Examples (cond ((< 2 1) 2) ((< 1 2) 1)) has value 1 has no value(cond (diverge 1) (true 0))no value, if expression diverge loops forever(cond (true 0) (diverge 1))has value 0
28Evaluation of Expressions Read-eval-print loopFunction call (function arg argn)evaluate each of the argumentspass list of argument values to functionSpecial forms do not eval all argumentsExample (cond (p1 e1) (pn en) )proceed from left to rightfind the first pi with value true, eval this eiExample (quote A) does not evaluate A
29Function Expressions Form Syntax comes from lambda calculus: (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))))
31StrictnessAn 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 ())
32Garbage Collection Garbage: Garbage Collection: 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 executionGC invoked when more memory is neededDecision made by run-time system, not programThis can be very convenient. For example: in building text-formatting program, ~40% of programmer time wasted on memory management.
33Examples (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.
34Mark-and-Sweep Algorithm Assume tag bits associated with dataNeed list of heap locations named by programAlgorithm:Set all tag bits to 0.Start from each location used directly in the program. Follow all links, changing tag bit to 1Place all cells with tag = 0 on free list
35Why 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?
36Programs As Data Programs and data have same representation Eval function used to evaluate contents of listExample: 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))
37ExamplesSuppose 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) => 7How do you do it in c?
38Recursive Functions Want expression for function f such that Try (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 )))))))
39Recursive 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 definedNon-recursive definition(define x (+ x 2))Error if x not previously definedWhile evaluating arguments to + in expression (+ x 2):Unbound variable: xABORT: (misc-error)
40Higher-Order Functions Function that eithertakes a function as an argumentreturns a function as a resultExample: 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 )))))))
42Efficiency and Side-Effects Pure Lisp: no side effectsAdditional operations added for “efficiency”(rplaca x y) replace car of cell x with y(rplacd x y) replace cdr of cell x with yWhat does “efficiency” mean here?Is (rplaca x y) faster than (cons y (cdr x)) ?Is faster always better?
43Example (define p '(A B)) (rplaca p 'C) (rplacd p 'D) p (C . D) Scheme preamble(define rplaca set-car!)(define rplacd set-cdr!)
45Summary: Contributions of Lisp Successful languagesymbolic computation, experimental programmingSpecific language ideasExpression-oriented: functions and recursionLists as basic data structuresPrograms as data, with universal function evalStack implementation of recursion via "public pushdown list"Idea of garbage collection.
46Language speedsCompletely Random and Arbitrary Point System
47Lots of Irritating Superfluous Parentheses LispLots of Irritating Superfluous ParenthesesWho wrote the language?
48The END Presentation by: Gal Oz Based on: John Mitchell’s Presentation and book.Thanks to:Wikipedia, Puder Avi, xkcd, Gidon Eitan, Itay Abraham and