Racket-Expr-Decl.Pptx

Racket-Expr-Decl.Pptx

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 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 – • Led to Structure and InterpretaNon Careful study of PL foundaNons (“PL mindset”) of Computer Programs (SICP) and – FuncNonal programming paradigm MIT 6.001 (haps://mitpress.mit.edu/sicp/) • Emphasis on funcNons and their composiNon • Immutable data (lists) – Beauty of minimalism – Observe design constraints/historical context Expr/decl 5 Expr/decl 6 Expressions, Values, and DeclaraNons Values • Values are expressions that cannot be evaluated • EnNre language: these three things further. • 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 • For each structure we add to the language: (strings, symbols, lists, funcNons, …) – 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 7 Expr/decl 8 AddiNon expression: syntax AddiNon expression: evaluaNon Adds two numbers together. Syntax: (+ E1 E2) Note recursive Syntax: (+ E1 E2) Every parenthesis required; none may be omiaed. EvaluaNon rule: structure! 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 Examples: structure! 3. Return the arithmeNc sum of V1 + V2. (+ 251 240) (+ (+ 251 240) 301) (+ #t 251) Not quite! Expr/decl 9 Expr/decl 10 AddiNon: dynamic type checking EvaluaNon AsserNons Formalize EvaluaNon Syntax: (+ E1 E2) The evalua)on asser)on notaNon E ↓ V means S)ll not quite! ``E evaluates to V ’’. 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 return 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 EvaluaNon DerivaNon in English More Compact DerivaNon NotaNon An evalua)on deriva)on is a ``proof ’’ that an expression V ↓ V [value rule] E1 ↓ V1 evaluates to a value using the evaluaNon rules. E2 ↓ V2 whereVis a value [addiNon rule] ↓ (+ 3 (+ 5 4)) 12 by the addiNon rule because: (number, boolean, etc.) (+ E1 E2) ↓ V • 3 ↓ 3 by the value rule side condiNons of rules Where V1 and V2 are numbers and • (+ 5 4) ↓ 9 by the addiNon rule because: V is the sum of V1 and V2. – 5 ↓ 5 by the value rule ↓ [value] – ↓ by the value rule 3 3 4 4 – 5 and 4 are both numbers 5 ↓ 5 [value] – 9 is the sum of 5 and 4 4 ↓ 4 [value] [addiNon] • 3 and 9 are both numbers (+ 5 4) ↓ 9 [addiNon] • 12 is the sum of 3 and 9 (+ 3 (+ 5 4)) ↓ 12 Expr/decl 13 Expr/decl 14 Errors Are Modeled by “Stuck” DerivaNons SyntacNc Sugar for AddiNon 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) (+ #t (+ 5 4))? (+ (+ 1 2) (+ 5 #f))? 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] • (+ 5 4) ↓ 9 5 ↓ 5 [value] This approach is known as syntac)c sugar: introduce new 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 #t is not a number in Stuck here. Can’t apply (addiNon) rule because more complex evaluaNon rules when + has a number of (+ #t 9) #f is not a number in arguments different from 2. (+ 5 #f) Expr/decl 15 Expr/decl 16 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 integers. (It is a floaNng point number if at least one value ↓ is a float.) E1 V1 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) • (/ E) is treated as (/ 1 E) Where V1 and V2 are numbers and 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 [if nonfalse] evaluated! 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 Eelse ↓ Velse Ethen is not return the result of evaluaNng Eelse [if false] (if Etest Ethen Eelse) ↓ Velse evaluated! Expr/decl 19 Expr/decl 20 Your turn Expressions vs. statements Use evaluaNon derivaNons to evaluate the CondiNonal expressions can go anywhere an following expressions expression is expected: (if (< 8 2) (+ #f 5) (+ 3 4)) (+ 4 (* (if (< 9 (- 251 240)) 2 3) 5)) (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! Unlike earlier expressions, not all Etest ↓ Vtest Ethen ↓ Vthen subexpressions of if expressions are evaluated! [if nonfalse] (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 Expr/decl 23 “falsiness” that you will explore in PS2. Expr/decl 24 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 (define test (< x diff)) expressions that contain idenNfiers. • Second argument to evaluaNon, which takes both an (if test (+ (* x y) diff) 17) expression and an environment. Expr/decl 25 Expr/decl 26 AddiNon: evaluaNon with environment Variable references Syntax: Id Syntax: (+ E1 E2) Id: any iden6fier 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. return 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 define DeclaraNons Environments: Example env0 = ∅ (can write as . in text) Syntax: (define Id E) (define x (+ 1 2)) define: keyword env1 = x ⟼ 3, ∅ (abbreviated x ⟼ 3; can write as x -> 3 in text) Id: any iden6fier (define y (* 4 x)) E: any expression env2 = y ⟼ 12, x ⟼ 3 (most recent binding 2irst) This is a declara)on, not an expression! (define diff (- y x)) We will say a declara)ons are processed, not evaluated env3 = diff ⟼ 9, y ⟼ 12, x ⟼ 3 (define test (< x diff)) Processing rule: env4 = test ⟼ #t, diff ⟼ 9, y ⟼ 12, x ⟼ 3 1.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us