1
play

1 Basic Functions Evaluation of Expressions Functions on atoms and - PDF document

CS 242 Announcements Lisp Exam dates Midterm: Wednesday Oct 27, 7-9 PM Final: Wednesday Dec 8, 8:30-11:30 AM Conflicts send email to cs242@cs now! Homework graders - email to cs242@cs John Mitchell Submit


  1. CS 242 Announcements Lisp � Exam dates • Midterm: Wednesday Oct 27, 7-9 PM • Final: Wednesday Dec 8, 8:30-11:30 AM • Conflicts – send email to cs242@cs now! � Homework graders - email to cs242@cs John Mitchell � Submit homework from far away (SCPD) • Fax (650) 736-1266 by 5PM the day it is due • We will return graded HW by courier � Reading • Will add reading assignment to slides, hw My office hours: will set next week after a trip Reading: Chapter 3 Homework 1: due Oct 6 Lisp, 1960 John McCarthy � Look at Historical Lisp � Pioneer in AI • Perspective • Formalize common- – Some old ideas seem old sense reasoning – Some old ideas seem new � Also • Example of elegant, minimalist language • Proposed timesharing • Not C, C++, Java: a chance to think differently • Mathematical theory • Illustrate general themes in language design • …. � Supplementary reading (optional) • McCarthy, Recursive functions of symbolic � Lisp expressions and their computation by machine, stems from interest in Communications of the ACM, Vol 3, No 4, 1960. symbolic computation (math, logic) Lisp summary Atoms and Pairs � Many different dialects � Atoms include numbers, indivisible “strings” <atom> ::= <smbl> | <number> • Lisp 1.5, Maclisp, …, Scheme, ... <smbl> ::= <char> | <smbl><char> | <smbl><digit> • CommonLisp has many additional features <num> ::= <digit> | <num><digit> • This course: a fragment of Lisp 1.5, approximately � Dotted pairs But ignore static/dynamic scope until later in course • Write (A . B) for pair � Simple syntax • Symbolic expressions, called S-expressions : (+ 1 2 3) <sexp> ::= <atom> | (<sexp> . <sexp>) (+ (* 2 3) (* 4 5)) (f x y) Easy to parse (Looking ahead: programs as data) 1

  2. Basic Functions Evaluation of Expressions � Functions on atoms and pairs: � Read-eval-print loop cons car cdr eq atom � Function call (function arg 1 ... arg n ) � Declarations and control: • evaluate each of the arguments • pass list of argument values to function cond lambda define eval quote � Special forms do not eval all arguments � Example • Example (cond (p 1 e 1 ) ... (p n e n ) ) (lambda (x) (cond ((atom x) x) (T (cons ‘A x)))) – proceed from left to right function f(x) = if atom(x) then x else cons(“A”,x) – find the first p i with value true, eval this e i � Functions with side-effects • Example (quote A) does not evaluate A rplaca rplacd set setq Examples McCarthy’s 1960 Paper (+ 4 5) � Interesting paper with expression with value 9 • Good language ideas, succinct presentation (+ (+ 1 2) (+ 4 5)) • Some feel for historical context • Insight into language design process evaluate 1+2, then 4+5, then 3+9 to get value � Important concepts (cons (quote A) (quote B)) • Interest in symbolic computation influenced design pair of atoms A and B • Use of simple machine model (quote (+ 1 2)) • Attention to theoretical considerations evaluates to list (+ 1 2) Recursive function theory, Lambda calculus '(+ 1 2) • Various good ideas: Programs as data, garbage collection same as (quote (+ 1 2)) Motivation for Lisp Execution Model (Abstract Machine) � Advice Taker � Language semantics must be defined • Process sentences as input, perform logical reasoning • Too concrete � Symbolic integration, differentiation – Programs not portable, tied to specific architecture • expression for function --> expression for integral – Prohibit optimization (e.g., C eval order undefined in expn) (integral ‘(lambda (x) (times 3 (square x)))) • Too abstract � Motivating application part of good lang design – Cannot easily estimate running time, space � Lisp: IBM 704, but only certain ideas … • Keep focus on most important goals • Eliminate appealing but inessential ideas • Address, decrement registers -> cells with two parts Lisp symbolic computation, logic, experimental prog. • Garbage collection provides abstract view of memory C Unix operating system Simula simulation PL/1 “kitchen sink”, not successful in long run 2

  3. Abstract Machine Theoretical Considerations � Concept of abstract machine: � “ … scheme for representing the partial • Idealized computer, executes programs directly recursive functions of a certain class of symbolic • Capture programmer’s mental image of execution expressions.” • Not too concrete, not too abstract � Lisp uses � Examples • Concept of computable (partial recursive) functions • Fortran – Want to express all computable functions – Flat register machine; memory arranged as linear array • Function expressions – No stacks, no recursion. – known from lambda calculus (developed A. Church) • Algol family – lambda calculus equivalent to Turing Machines, but provide – Stack machine, contour model of scope, heap storage useful syntax and computation rules • Smalltalk – Objects, communicating by messages. Innovations in the Design of Lisp Parts of Speech � Expression-oriented � Statement load 4094 r1 • function expressions • Imperative command • conditional expressions • Alters the contents of previously-accessible memory • recursive functions � Expression (x+5)/2 � Abstract view of memory • Syntactic entity that is evaluated • Cells instead of array of numbered locations • Has a value, need not change accessible memory • Garbage collection • If it does, has a side effect � Programs as data � Declaration integer x � Higher-order functions • Introduces new identifier • May bind value to identifier, specify type, etc. Function Expressions Conditional Expressions in Lisp � Generalized if-then-else � Example: (cond (p 1 e 1 ) (p 2 e 2 ) … (p n e n ) ) (lambda ( parameters ) ( function_body ) ) – Evaluate conditions p 1 … p n left to right � Syntax comes from lambda calculus: – If p i is first condition true, then evaluate e i λ f. λ x. f (f x) – Value of e i is value of expression (lambda (f) (lambda (x) (f (f x)))) Undefined if no p i true, or Function expression defines a function but does not give a name to it p 1 … p i false and p i+1 undefined, or ( (lambda (f) (lambda (x) (f (f x)))) relevant p i true and e i undefined (lambda (y) (+ 2 y))) Conditional statements in assembler ) Conditional expressions apparently new in Lisp 3

  4. Examples Strictness (cond ((<2 1) 2) ((<1 2) 1)) � An operator or expression form is strict if it can have a value only if all operands or has value 1 subexpressions have a value. (cond ((<2 1 ) 2) ((<3 2) 3)) � Lisp cond is not strict, but addition is strict is undefined • (cond (true 1) (diverge 0)) • (+ e 1 e 2 ) (cond (diverge 1) (true 0)) is undefined, where diverge is undefined (cond (true 0) (diverge 1)) has value 0 Lisp Memory Model Sharing (a) (b) � Cons cells Address Decrement � Atoms and lists represented by cells A B A B A B Atom A � Both structures could be printed as (A.B).(A.B) 0 � Which is result of evaluating Atom B (cons (cons ‘A ‘B) (cons ‘A ‘B)) ? Atom C Garbage Collection Examples � Garbage: (car (cons ( e 1 ) ( e 2 ) )) At a given point in the execution of a program P , a Cells created in evaluation of e 2 may be garbage, memory location m is garbage if no continued execution unless shared by e 1 or other parts of program of P from this point can access location m . � Garbage Collection: ((lambda (x) (car (cons (… x…) (... x ...))) • Detect garbage during program execution '(Big Mess)) • GC invoked when more memory is needed The car and cdr of this cons cell may point to • Decision made by run-time system, not program overlapping structures. This is can be very convenient. Example: in building text-formatting program, ~40% of programmer time on memory management . 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend