lisp list processing
play

LISP: LISt Processing early 1960s McCarthy, MIT arNficial - 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 2019, Lyn Turbak Department of Computer Science Wellesley College These slides


  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 2019, Lyn Turbak Department of Computer Science Wellesley College These slides build on Ben Wood’s Fall ‘15 slides Expr/decl 2 LISP: implemented by Steve Russell, LISP: LISt Processing early 1960s • McCarthy, MIT arNficial intelligence, 1950s-60s – Advice Taker: represent logic as data, not just program Emacs: M-x doctor • Needed a language for: – Symbolic computaNon i.e., not just number crunching – Programming with logic – ArNficial 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 UlNmate” created DrJava. papers (hap://library.readscheme.org/page1.html) • Why study Racket in CS251? – Lambda the UlNmate PL blog inspired by these: – Clean slate, unfamiliar hap://lambda-the-ulNmate.org – Careful study of PL foundaNons (“PL mindset”) • Led to Structure and InterpretaNon – FuncNonal programming paradigm of Computer Programs (SICP) and • Emphasis on funcNons and their composiNon 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 DeclaraNons • Values are expressions that cannot be evaluated further. • EnNre language: these three things • Syntax: • Expressions have evalua6on 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, funcNons, …) • For each structure we add to the language: – What is its syntax ? How is it wriaen? • EvaluaNon 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. AddiNon expression: syntax AddiNon expression: evaluaNon Adds two numbers together. Syntax: (+ E1 E2 ) Note recursive Syntax: (+ E1 E2 ) structure! EvaluaNon 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 notaNon. Note recursive 2. Evaluate E2 to a value V2 structure! 3. Return the arithmeNc sum of V1 + V2 . Examples: (+ 251 240) (+ (+ 251 240) 301) Not quite! (+ #t 251) Expr/decl 9 Expr/decl 10 EvaluaNon AsserNons Formalize EvaluaNon AddiNon: dynamic type checking The evalua)on asser)on notaNon E ↓ V means Syntax: (+ E1 E2 ) `` E evaluates to V ’’. S)ll not quite! EvaluaNon rule: More later … Our evaluaNon 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 • addi6on rule : 3. If V1 and V2 are both numbers then if E1 ↓ V1 and E2 ↓ V2 r eturn the arithmeNc 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. EvaluaNon DerivaNon in English More Compact DerivaNon NotaNon An evalua)on deriva)on is a ``proof ’’ that an expression E1 ↓ V1 V ↓ V [value rule] evaluates to a value using the evaluaNon rules. E2 ↓ V2 whereVis a value [addiNon rule] (+ 3 (+ 5 4)) ↓ 12 by the addiNon rule because: (+ E1 E2 ) ↓ V (number, boolean, etc.) 3 ↓ 3 by the value rule • Where V1 and V2 are numbers and side condiNons of rules • (+ 5 4) ↓ 9 by the addiNon 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 [addiNon] (+ 5 4) ↓ 9 3 and 9 are both numbers • [addiNon] • 12 is the sum of 3 and 9 (+ 3 (+ 5 4)) ↓ 12 Expr/decl 13 Expr/decl 14 SyntacNc Sugar for AddiNon Errors Are Modeled by “Stuck” DerivaNons The addiNon operator + can take any number of operands. How to evaluate How to evaluate • For now, treat (+ E1 E2 … En ) as (+ (+ E1 E2 ) … En ) (+ (+ 1 2) (+ 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 [addiNon] [addiNon] • This approach is known as syntac)c sugar : introduce new (+ 5 4) ↓ 9 5 ↓ 5 [value] syntacNc forms that “ desugar ” into exisNng ones. Stuck here. Can’t apply #f ↓ #f [value] (addiNon) rule because • In this case, an alternaNve approach would be to introduce Stuck here. Can’t apply #t is not a number in more complex evaluaNon rules when + has a number of (addiNon) rule because (+ #t 9) arguments different from 2. #f is not a number in (+ 5 #f) Expr/decl 15 Expr/decl 16

  5. Other ArithmeNc Operators RelaNon Operators Similar syntax and evaluaNon for The following relaNonal operators on numbers return - * / quotient remainder min max booleans: < <= = >= > except: • Second argument of / , quotient , remainder must be nonzero For example: • Result of / is a raNonal number (fracNon) when both values are E1 ↓ V1 integers. (It is a floaNng 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 CondiNonal (if) expressions DerivaNon-style rules for CondiNonals Syntax: (if Etest Ethen Eelse ) Etest ↓ Vtest Eelse is not Ethen ↓ Vthen evaluated! [if nonfalse] EvaluaNon 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 evaluaNng Ethen Etest ↓ #f otherwise Ethen is not Eelse ↓ Velse [if false] return the result of evaluaNng Eelse evaluated! (if Etest Ethen Eelse ) ↓ Velse Expr/decl 19 Expr/decl 20

  6. Your turn Expressions vs. statements Use evaluaNon derivaNons to evaluate the CondiNonal 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 condiNonal expressions in addiNon to condiNonal statements? (Many do! Java, JavaScript, Python, …) Expr/decl 21 Expr/decl 22 Design choice in condiNonal semanNcs CondiNonal 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 noNons of “truthiness” and “falsiness” that you will explore in PS2. Expr/decl 23 Expr/decl 24

  7. Environments: MoNvaNon Environments: DefiniNon 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 idenNfiers (variable names) with values. – Concrete example: (define x (+ 1 2)) num ⟼ 17 , absoluteZero ⟼ -273 , true ⟼ #t – Abstract Example (use Id to range over idenNfiers = names): (define y (* 4 x)) Id1 ⟼ V1 , Id2 ⟼ V2 , … , Idn ⟼ Vn – Empty environment: ∅ (define diff (- y x)) • An environment serves as a context for evaluaNng expressions that contain idenNfiers. (define test (< x diff)) • Second argument to evaluaNon, which takes both an (if test (+ (* x y) diff) 17) expression and an environment. Expr/decl 25 Expr/decl 26 Variable references AddiNon: evaluaNon with environment Syntax: Id Id : any iden6fier Syntax: (+ E1 E2 ) EvaluaNon rule: Look up and return the value to which Id is bound in the current EvaluaNon 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 representaNon) • If Id is not bound in the current environment, evaluaNng it is “stuck” 3. If V1 and V2 are both numbers then at an unbound variable error . r eturn the arithmeNc 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

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