Lisp John Mitchell CS 242. Lisp, 1960 uLook at Historical Lisp Perspective –Some old ideas seem old –Some old ideas seem new Example of elegant, minimalist.

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Gal Oz Based on: John Mitchell’s Presentation and book.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Lisp John Mitchell CS 242 Reading: Chapter 3 Homework 1: due Oct 3.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Programming Languages –14 David Watt (Glasgow) Steven Wong (Singapore) Moodle : Computing Science → Level 3 → Programming Languages 3 © 2012 David.
LISP – Not just a Speech Impediment Jim Lowe. Brief History of LISP Initial development begins at the Dartmouth Summer Research Project in 1956 by Dr.
Functional Programming in Scheme
ISBN Chapter 15 Functional Programming Languages.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Midterm Review John Mitchell CS 242. Midterm uThurs Nov 7, 7-9PM Terman Aud Closed book uClass schedule Thurs Oct 31, Tues Nov 5 – topics not on midterm.
Chapter Fifteen: Functional Programming Languages Lesson 12.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Programming Languages: Design, Specification, and Implementation G Rob Strom September 21, 2006.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
1 Scheme (Section 11.2) CSCI 431 Programming Languages Fall 2003.
Lisp John Mitchell CS 242 Reading: Chapter 3 Homework 1: due Oct 6.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Functional Programming
Functional Programming Languages
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
Introduction to Scheme
CS 326 Programming Languages, Concepts and Implementation
Scheme : variant of LISP
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
6.001 SICP Variations on a Scheme
CSE S. Tanimoto Lambda Calculus
Modern Programming Languages Lecture 18 Fakhar Lodhi
topics interpreters meta-linguistic abstraction eval and apply
Lisp.
Presentation transcript:

Lisp John Mitchell CS 242

Lisp, 1960 uLook at Historical Lisp Perspective –Some old ideas seem old –Some old ideas seem new Example of elegant, minimalist language Not C: a chance to think differently Many general themes in language design uSupplementary reading McCarthy, Recursive functions of symbolic expressions and their computation by machine, Communications of the ACM, Vol 3, No 4, 1960.

John McCarthy uPioneer in AI Formalize common- sense reasoning uAlso Proposed timesharing Mathematical theory …. uLisp stems from interest in symbolic computation (math, logic)

Lisp summary uMany different dialects Lisp 1.5, Maclisp, …, Scheme,... CommonLisp has many additional features This course: a fragment of Lisp 1.5, approximately But ignore static/dynamic scope until later in course uSimple syntax ( ) (+ (* 2 3) (* 4 5)) (f x y) Easy to parse. (Looking ahead: programs as data)

Atoms and Pairs uAtoms include numbers, indivisible “strings” ::= | ::= | | ::= | uDotted pairs Write (A. B) for pair Symbolic expressions, called S-expressions: ::= | (. )

Basic Functions uFunctions on atoms and pairs: cons car cdr eq atom uDeclarations and control: cond lambda define eval quote uExample (lambda (x) (cond ((atom x) x) (T (cons ‘A x)))) function f(x) = if atom(x) then x else cons(“A”,x) uFunctions with side-effects rplaca rplacd set setq

Evaluation of Expressions uRead-eval-print loop uFunction call (function arg 1... arg n ) evaluate each of the arguments pass list of argument values to function uSpecial forms do not eval all arguments Example (cond (p 1 e 1 )... (p n e n ) ) –proceed from left to right –find the first p i with value true, eval this e i Example (quote A) does not evaluate A

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

McCarthy’s 1960 Paper uInteresting paper with Good language ideas, succinct presentation Some feel for historical context Insight into language design process uImportant 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

Motivation for Lisp uAdvice Taker Process sentences as input, perform logical reasoning uSymbolic integration, differentiation expression for function --> expression for integral (integral ‘(lambda (x) (times 3 (square x)))) uMotivating 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 system Simula simulation PL/1 “kitchen sink”, not successful in long run

Execution Model (Abstract Machine) uLanguage 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 uLisp: IBM 704, but only certain ideas … Address, decrement registers -> cells with two parts Garbage collection provides abstract view of memory

Abstract Machine uConcept of abstract machine: Idealized computer, executes programs directly Capture programmer’s mental image of execution Not too concrete, not too abstract uExamples 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.

Theoretical Considerations u“ … scheme for representing the partial recursive functions of a certain class of symbolic expressions.” uLisp 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

Innovations in the Design of Lisp uExpression-oriented function expressions conditional expressions recursive functions uAbstract view of memory Cells instead of array of numbered locations Garbage collection uPrograms as data uHigher-order functions

Parts of Speech uStatement load 4094 r1 Imperative command Alters the contents of previously-accessible memory uExpression (x+5)/2 Syntactic entity that is evaluated Has a value, need not change accessible memory If it does, has a side effect uDeclaration integer x Introduces new identifier May bind value to identifier, specify type, etc.

Function Expressions uExample: (lambda ( parameters ) ( function_body ) ) u Syntax comes from lambda calculus: f. x. f (f x) (lambda (f) (lambda (x) (f (f x)))) Function expression defines a function but does not give a name to it. ( (lambda (f) (lambda (x) (f (f x)))) (lambda (y) (+ 2 y))) )

Conditional Expressions in Lisp uGeneralized if-then-else (cond (p 1 e 1 ) (p 2 e 2 ) … (p n e n ) ) –Evaluate conditions p 1 … p n left to right –If p i is first condition true, then evaluate e i –Value of e i is value of expression Undefined if no p i true, or p 1 … p i false and p i+1 undefined, or relevant p i true and e i undefined Conditional statements in assembler Conditional expressions apparently new in Lisp

Examples (cond ((<2 1) 2) ((<1 2) 1)) has value 1 (cond ((<2 1 ) 2) ((<3 2) 3)) is undefined (cond (diverge 1) (true 0)) is undefined, where diverge is undefined (cond (true 0) (diverge 1)) has value 0

Strictness uAn operator or expression form is strict if it can have a value only if all operands or subexpressions have a value. uLisp cond is not strict, but addition is strict (cond (true 1) (diverge 0)) (+ e 1 e 2 )

Lisp Memory Model uCons cells uAtoms and lists represented by cells AddressDecrementAtomA B C0

Sharing (a) (b) uBoth structures could be printed as (A.B).(A.B) uWhich is result of evaluating (cons (cons ‘A ‘B) (cons ‘A ‘B)) ? ABABAB

Garbage Collection uGarbage: 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. uGarbage Collection: Detect garbage during program execution GC invoked when more memory is needed Decision made by run-time system, not program This is can be very convenient. Example: in building text-formatting program, ~40% of programmer time on memory management.

Examples (car (cons ( e 1 ) ( e 2 ) )) Cells created in evaluation of e 2 may be garbage, unless shared by e 1 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.

Mark-and-Sweep Algorithm uAssume tag bits associated with data uNeed list of heap locations named by program uAlgorithm: 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

Why Garbage Collection in Lisp? uMcCarthy's paper says that this is "more convenient for the programmer than a system in which he has to keep track of and erase unwanted lists." uDoes this reasoning apply equally well to C? uIs garbage collection "more appropriate" for Lisp than C? Why?

Programs As Data uPrograms and data have same representation uEval function used to evaluate contents of list uExample: substitute x for y in z and evaluate (define substitute (lambda (x y z) (cond ((atom z) (cond ((eq z y) x ) (T z))) (T (cons (substitute x y (car z)) (substitute x y (cdr z)))))) (define substitute-and-eval (lambda (x y z) (eval (substitute x y z))))

Recursive Functions uWant expression for function f such that (f x) = (cond ((eq x 0) 0) (true (+ x (f (- x 1))))) uTry (lambda (x) (cond ((eq x 0) 0) (true (+ x (f (- x 1)))))) but f in function body is not defined. uMcCarthy's 1960 solution was operator “label” (label f (lambda (x) (cond ((eq x 0) 0) (true (+ x (f (- x 1)))))))

Higher-Order Functions uFunction that either takes a function as an argument returns a function as a result uExample: function composition (define compose (lambda (f g) (lambda (x) (f (g x))))) uExample: maplist (define maplist (f x) (cond ((null x) nil) (true (cons (f (car x)) (maplist f (cdr x))))))

Efficiency and Side-Effects uPure Lisp: no side effects uAdditional operations added for “efficiency” (rplaca x y) replace car of cell x with y (rplacd x y) replace cdr of cell x with y uWhat does “efficiency” mean here? Is (rplaca x y) faster than (cons y (cdr x)) ? Is faster always better?

Summary: Contributions of Lisp uSuccessful language symbolic computation, experimental programming uSpecific 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.

Announcements uExam conflicts – send to If you are taking cs244a, we’ll work something out uHomework graders - to uTopic suggestions received Closures and recursion, actors Perl Generic programming – types in module interfaces –Boost Concept Checking Library Cross-language interoperability (Corba vs COM) OCaml, C#,.Net