lisp list processing
play

LISP: LISt Processing early 1960s McCarthy, MIT ar)ficial - PowerPoint PPT Presentation

LISP: designed by John McCarthy, 1958 published 1960 Introduc)on to Racket, a dialect of LISP: Expressions and Declara)ons CS251 Programming Languages Spring 2017, Lyn Turbak Department of Computer Science Wellesley College Expr/decl 2


  1. LISP: designed by John McCarthy, 1958 published 1960 Introduc)on to Racket, a dialect of LISP: Expressions and Declara)ons CS251 Programming Languages Spring 2017, Lyn Turbak Department of Computer Science Wellesley College Expr/decl 2 LISP: implemented by Steve Russell, LISP: LISt Processing early 1960s • McCarthy, MIT ar)ficial intelligence, 1950s-60s – Advice Taker: represent logic as data, not just program Emacs: M-x doctor • Needed a language for: – Symbolic computa)on i.e., not just number crunching – Programming with logic – Ar)ficial intelligence – Experimental programming • So make one! Expr/decl 3 Expr/decl 4

  2. Scheme • Gerald Jay Sussman and Guy Lewis Steele (mid 1970s) • Grandchild of LISP (variant of Scheme) • Lexically-scoped dialect of LISP – Some changes/improvements, quite similar that arose from trying to make • Developed by the PLT group an “actor” language. (haps://racket-lang.org/people.html), the same folks who • Described in amazing “Lambda the Ul)mate” created DrJava. papers (hap://library.readscheme.org/page1.html) • Why study Racket in CS251? – Lambda the Ul)mate PL blog inspired by these: – Clean slate, unfamiliar hap://lambda-the-ul)mate.org – Careful study of PL founda)ons (“PL mindset”) • Led to Structure and Interpreta)on – Func)onal programming paradigm of Computer Programs (SICP) and • Emphasis on func)ons and their composi)on MIT 6.001 (haps://mitpress.mit.edu/sicp/) • Immutable data (lists) – Beauty of minimalism – Observe design constraints/historical context Expr/decl 5 Expr/decl 6 Values Expressions, Values, and Declara)ons • Values are expressions that cannot be evaluated further. • En)re language: these three things • Syntax: • Expressions have evalua&on rules: – Numbers: 251, 240, 301 – How to determine the value denoted by an expression. – Booleans: #t, #f – There are more values we will meet soon (strings, symbols, lists, func)ons, …) • For each structure we add to the language: – What is its syntax ? How is it wriaen? • Evalua)on rule: – What is its evalua?on rule ? How is it evaluated to a – Values evaluate to themselves. value (expression that cannot be evaluated further)? Expr/decl 8 Expr/decl 7

  3. Addi)on expression: syntax Addi)on expression: evalua)on Adds two numbers together. Syntax: (+ E1 E2 ) Note recursive Syntax: (+ E1 E2 ) structure! Evalua)on rule: Every parenthesis required; none may be omiaed. E1 and E2 stand in for any expression. 1. Evaluate E1 to a value V1 Note prefix nota)on. Note recursive 2. Evaluate E2 to a value V2 structure! 3. Return the arithme)c sum of V1 + V2 . Examples: (+ 251 240) (+ (+ 251 240) 301) Not quite! (+ #t 251) Expr/decl 9 Expr/decl 10 Evalua)on Asser)ons Formalize Evalua)on Addi)on: dynamic type checking The evalua?on asser?on nota)on E ↓ V means Syntax: (+ E1 E2 ) `` E evaluates to V ’’. S?ll not quite! Evalua)on rule: More later … Our evalua)on rules so far: 1. evaluate E1 to a value V1 • value rule : V ↓ V (where V is a number or boolean) 2. Evaluate E2 to a value V2 • addi&on rule : 3. If V1 and V2 are both numbers then if E1 ↓ V1 and E2 ↓ V2 r eturn the arithme)c sum of V1 + V2 . and V1 and V2 are both numbers 4. Otherwise, a type error occurs. and V is the sum of V1 and V2 then (+ E1 E2 ) ↓ V Dynamic type-checking Expr/decl 11 Expr/decl 12

  4. Evalua)on Deriva)on in English More Compact Deriva)on Nota)on An evalua?on deriva?on is a ``proof ’’ that an expression E1 ↓ V1 V ↓ V [value rule] evaluates to a value using the evalua)on rules. E2 ↓ V2 whereVis a value [addi)on rule] (+ 3 (+ 5 4)) ↓ 12 by the addi)on rule because: (+ E1 E2 ) ↓ V (number, boolean, etc.) 3 ↓ 3 by the value rule • Where V1 and V2 are numbers and side condi)ons of rules • (+ 5 4) ↓ 9 by the addi)on rule because: V is the sum of V1 and V2 . – 5 ↓ 5 by the value rule 3 ↓ 3 [value] – 4 ↓ 4 by the value rule 5 ↓ 5 [value] – 5 and 4 are both numbers 4 ↓ 4 [value] – 9 is the sum of 5 and 4 [addi)on] (+ 5 4) ↓ 9 3 and 9 are both numbers • [addi)on] • 12 is the sum of 3 and 9 (+ 3 (+ 5 4)) ↓ 12 Expr/decl 13 Expr/decl 14 Syntac)c Sugar for Addi)on Errors Are Modeled by “Stuck” Deriva)ons The addi)on operator + can take any number of operands. How to evaluate How to evaluate • For now, treat (+ E1 E2 … En ) as (+ (+ E1 E2 ) … En ) (+ 3 (+ 5 #f)) ? (+ #t (+ 5 4)) ? E.g., treat (+ 7 2 -5 8) as (+ (+ (+ 7 2) -5) 8) #t ↓ #t [value] 1 ↓ 1 [value] Treat (+ E ) as E (or say if E ↓ V then (+ E ) ↓ V ) • 5 ↓ 5 [value] 2 ↓ 2 [value] • Treat (+) as 0 (or say (+) ↓ 0 ) 4 ↓ 4 [value] (+ 1 2) ↓ 3 [addi)on] [addi)on] • This approach is known as syntac?c sugar : introduce new (+ 5 4) ↓ 9 5 ↓ 5 [value] syntac)c forms that “ desugar ” into exis)ng ones. Stuck here. Can’t apply #f ↓ #f [value] (addi)on) rule because • In this case, an alterna)ve approach would be to introduce Stuck here. Can’t apply #t is not a number in more complex evalua)on rules when + has a number of (addi)on) rule because (+ #t 9) arguments different from 2. #f is not a number in (+ 5 #f) Expr/decl 15 Expr/decl 16

  5. Other Arithme)c Operators Rela)on Operators Similar syntax and evalua)on for The following rela)onal operators on numbers return - * / quotient remainder min max booleans: < <= = >= > except: • Second argument of / , quotient , remainder must be nonzero For example: • Result of / is a ra)onal number (frac)on) when both values are E1 ↓ V1 integers. (It is a floa)ng point number if at least one value is a float.) E2 ↓ V2 • quotient and remainder take exactly two arguments; [less than] anything else is an error. (< E1 E2 ) ↓ V • (- E ) is treated as (- 0 E ) Where V1 and V2 are numbers and • (/ E ) is treated as (/ 1 E ) V is #t if V1 is less than V2 • (min E ) and (max E ) treated as E or #f if V1 is not less than V2 • (*) evaluates to 1. • (/) , (-) , (min) , (max) are errors (i.e., stuck) Expr/decl 17 Expr/decl 18 Condi)onal (if) expressions Deriva)on-style rules for Condi)onals Syntax: (if Etest Ethen Eelse ) Etest ↓ Vtest Eelse is not Ethen ↓ Vthen evaluated! [if nonfalse] Evalua)on rule: (if Etest Ethen Eelse ) ↓ Vthen 1. Evaluate Etest to a value Vtest . Where Vtest is not #f 2. If Vtest is not the value #f then return the result of evalua)ng Ethen Etest ↓ #f otherwise Ethen is not Eelse ↓ Velse [if false] return the result of evalua)ng Eelse evaluated! (if Etest Ethen Eelse ) ↓ Velse Expr/decl 19 Expr/decl 20

  6. Your turn Expressions vs. statements Use evalua)on deriva)ons to evaluate the Condi)onal expressions can go anywhere an expression is expected: following expressions (+ 4 (* (if (< 9 (- 251 240)) 2 3) 5)) (if (< 8 2) (+ #f 5) (+ 3 4)) (if (if (< 1 2) (> 4 3) (> 5 6)) (if (+ 1 2) (- 3 7) (/ 9 0)) (+ 7 8) (* 9 10) (+ (if (< 1 2) (* 3 4) (/ 5 6)) 7) Note: if is an expression , not a statement. Do (+ (if 1 2 3) #t) other languages you know have condi)onal expressions in addi)on to condi)onal statements? (Many do! Java, JavaScript, Python, …) Expr/decl 21 Expr/decl 22 Design choice in condi)onal seman)cs Condi)onal expressions: careful! In the [if nonfalse] rule, Vtest is not required to be a boolean! Etest ↓ Vtest Unlike earlier expressions, not all Ethen ↓ Vthen [if nonfalse] subexpressions of if expressions are evaluated! (if Etest Ethen Eelse ) ↓ Vthen Where Vtest is not #f (if (> 251 240) 251 (/ 251 0)) This is a design choice for the language designer. What would happen if we replace the above rule by Etest ↓ #t (if #f (+ #t 240) 251) Ethen ↓ Vthen [if true] (if Etest Ethen Eelse ) ↓ Vthen This design choice is related to no)ons of “truthiness” and “falsiness” that you will explore in PS2. Expr/decl 23 Expr/decl 24

  7. Environments: Defini)on Environments: Mo)va)on Want to be able to name values so can refer to • An environment is a sequence of bindings that them later by name. E.g.; associate iden)fiers (variable names) with values. – Concrete example: (define x (+ 1 2)) num ⟼ 17 , absoluteZero ⟼ -273 , true ⟼ #t – Abstract Example (use Id to range over iden)fiers = names): (define y (* 4 x)) Id1 ⟼ V1 , Id2 ⟼ V2 , … , Idn ⟼ Vn – Empty environment: ∅ (define diff (- y x)) • An environment serves as a context for evalua)ng expressions that contain iden)fiers. (define test (< x diff)) • Second argument to evalua)on, which takes both an (if test (+ (* x y) diff) 17) expression and an environment. Expr/decl 25 Expr/decl 26 Variable references Addi)on: evalua)on with environment Syntax: Id Id : any iden&fier Syntax: (+ E1 E2 ) Evalua)on rule: Look up and return the value to which Id is bound in the current Evalua)on rule: environment. • Look-up proceeds by searching from the most-recently added 1. evaluate E1 in the current environment to a value V1 bindings to the least-recently added bindings (front to back in our 2. Evaluate E2 in the current environment to a value V2 representa)on) • If Id is not bound in the current environment, evalua)ng it is “stuck” 3. If V1 and V2 are both numbers then at an unbound variable error . r eturn the arithme)c sum of V1 + V2 . Examples: 4. Otherwise, a type error occurs. • Suppose env is num ⟼ 17 , absZero ⟼ -273 , true ⟼ #t , num ⟼ 5 • In env , num evaluates to 17 (more recent than 5), absZero evaluates to -273 , and true evaluates to #t . Any other name is stuck. Expr/decl 27 Expr/decl 28

Recommend


More recommend