1 / 33

PPL

PPL. Lecture Notes: Chapter 2. Syntax & Formal Semantics. Midterm 2007. ( let ((x 4) (y 5)) ( + x y ( let ((x y) (y x) (+ -)) (+ x y)))) מה הערך של הביטוי?. Today. Concrete and Abstract Syntax

kisha
Download Presentation

PPL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PPL Lecture Notes: Chapter 2 Syntax & Formal Semantics

  2. Midterm 2007 (let ((x 4) (y 5)) ( + x y (let ((x y) (y x) (+ -)) (+ x y)))) מה הערך של הביטוי?

  3. Today • Concrete and Abstract Syntax • Operational Semantics: applicative order and normal order

  4. Concrete Syntax • Defines the actual syntax • In Scheme: • Small and simple • Context free grammar • We use BNF notation (next slide)

  5. Concrete Syntax • Non-terminals are denoted as <...> • Terminals (tokens) are surrounded with ’ • Optional items are denoted as [<item-x>] • Items repeating 0 or more times end with * • Items repeating 1 or more times end with + • Alternative choices are separated by the | symbol • Grouped items are in parentheses

  6. Concrete Syntax <scheme-exp> -> <exp> | ’(’ <define> ’)’ <exp> -> <atomic> | ’(’ <composite> ’)’ <atomic> -> <number> | <boolean> | <variable> <composite> -> <special> | <form> <number> -> Numbers <boolean> -> ’#t’ | ’#f’

  7. <scheme-exp> -> <exp> | ’(’ <define> ’)’ <exp> -> <atomic> | ’(’ <composite> ’)’ <atomic> -> <number> | <boolean> | <variable> <composite> -> <special> | <form> <number> -> Numbers <boolean> -> ’#t’ | ’#f’ <variable> -> sequences of letters, digits, punctuation marks <special> -> <lambda> | <quote> | <cond> | <if> | <let> <form> -> <exp>+ <lambda> -> ’lambda’ ’(’ <variable>* ’)’ <exp>+ <quote> -> ’quote’ <variable> <cond> -> ’cond’ <condition-clause>* <else-clause> <condition-clause> -> ’(’ <exp> <exp>+ ’)’ <else-clause> -> ’(’ ’else’ <exp>+ ’)’ <if> -> ’if’ <exp> <exp> <exp> <let> -> ’let’ ’(’ <var-initialization>* ’)’ <exp>+ <var-initialization> -> ’(’ <variable> <exp> ’)’ <scheme-exp> -> <exp> -> ( <composite> ) -> ( <special> ) -> ( <if> ) -> ( if <exp> <exp> <exp> )-> ( if <atomic> <exp> <exp> )-> ( if <boolean> <exp> <exp> ) -> ( if #f <exp> <exp> ) -> ( if #f <exp> <atomic> ) -> ( if #f <exp> number) -> ( if #f <exp> 2) -> ( if #f ( <composite> ) 2) -> ( if #f ( <form> ) 2) -> ( if #f ( <exp> <exp> <exp> ) 2) -> ( if #f ( <atomic> <exp> <exp> ) 2) -> ( if #f ( <variable> <exp> <exp> ) 2) -> ( if #f ( / <exp> <exp> ) 2) -> ( if #f ( / <atomic> <exp> ) 2) -> ( if #f ( / <number> <exp> ) 2) -> ( if #f ( / 1 <exp> ) 2) -> ( if #f ( / 1 <atomic> ) 2) -> ( if #f ( / 1 <number> ) 2) -> ( if #f ( / 1 0 ) 2)

  8. Abstract Syntax • Emphasizes the components • Ignores syntactic parts irrelevant to semantics (like order of components, for example) • ASP • A parser for abstract syntax • an interface to the syntax • Uses a tree for that

  9. Abstract Syntax <scheme-exp>: Kinds: <exp>, <define> <exp>: Kinds: <atomic>, <composite> <atomic>: Kinds: <number>, <boolean>, <variable> … <define>: Components: Variable: <variable> Expression: <exp>

  10. Possible ASP Tree for the expression: (lambda (x y) (+ x y))

  11. Operational Semantics • Actually an eval(exp) algorithm for Scheme expressions • Defined as a set of evaluation rules

  12. Formalism: Definitions Binding instance (or declaration): a variable occurrence to which other occurrences refer. In Scheme: Variables occurring as lambda parameters are binding instances (also let!) Top level (non-nested) defined variables (in a define form) are binding instances(declarations). No internal “defines” are allowed!

  13. Scope Scope of a binding instance is the region of the program text in which variable occurrences refer to the value that is bound by the variable declaration (where the variable declaration is recognized) In Scheme: Scope of lambda parameters is the entire lambda expression (or let body) Scope of define variables is the entire program, from the define expression and on: Universal / global scope

  14. Bound Occurrence Occurrence of variable x that is not a binding instance and is contained within the scope of a binding instance x. The binding instance (declaration) that binds an occurrence of x is the most nested declaration of x that includes the x occurrence in its scope.

  15. Free Occurrence Free occurrence: An occurrence of variable x that is not a binding instance, and is not bound.

  16. Renaming • Bound variables can be renamed without changing the semantics (kind like renaming in Java Eclipse) (lambda (x) x) <=> (lambda (y) y)

  17. Substitution New definition! A substitution s is a mapping from a finite set of variables to a finite set of values. A pair <x,s(x)> is called binding, and written x=x(x) Substitutions are written using set notions: {x=3, y=a, w=<closure>}

  18. Application of Substitution Given substitution s and expression E: E◦s: • Renaming of E and values in s. • Replacing all free occurrence of variables of s in E by their corresponding values.

  19. Application of Substitution No renaming; no replacement 10◦{x = 5} = 10 No renaming; just replacement (+ x y)◦{x = 5} = (+ 5 y) Renaming and replacement ((+ x ((lambda (x) (+ x 3)) 4)))◦{x = 5} = ((+ 5 ((lambda (x1) (+ x1 3)) 4)))

  20. Applicative order evaluation rules Combination... (<operator> <operand1> …… <operand n>) • Evaluate <operator> to get the procedure • evaluate <operands> to get the arguments • If <operator> is primitive: do whatever magic it does • If <operator> is compound: evaluate body with formal parameters replaced by arguments Eager!

  21. Normal order evaluation Combination … (<operator> <operand1> …… <operand n>) • Evaluate <operator> to get the procedure • evaluate <operands> to get the arguments • If <operator> is primitive: do whatever it does • If <operator> is compound: evaluate body with formal parameters replaced by arguments Lazy…

  22. The Difference Normal ((lambda (x) (+ x x)) (* 3 4)) (+ (* 3 4) (* 3 4)) (+ 12 12) 24 Applicative ((lambda (x) (+ x x)) (* 3 4)) (+ 12 12) 24 This may matter in some cases: ((lambda (x y) (+ x 2)) 3 (/ 1 0)) Your interperter works in Applicative Order!

  23. Another Example (define f (lambda (x) (f x))) (define g (lambda (x) 5)) (g (f 0)) What will happen with applicative evaluation? What about Normal?

  24. LET Revisited (define f (λ (x y) (let ((f-helper (lambda (a b) (+ (* x (* a a)) (* y b) (* a b))) ) ) (f-helper (+ 1 (* x y)) (- 1 y)))))

  25. LET and Recursive Procedures (define fact (λ (n) (let ((iter (lambda (prod c) (if (> c n) prod (iter (* c prod) (+ c 1)))) )) (iter 1 1))))

  26. LETREC “For local recursive functions there is a special operator letrec, similar to let, and used only for local procedure (function) definitions. It’s syntax is the same as that of let. But, the scope of the local procedure declarations includes all of the initialization procedures!”

  27. LETREC (define fact (λ (n) (letrec ((iter (lambda (prod c) (if (> c n) prod (iter (* c prod) (+ c 1)))) )) (iter 1 1))))

  28. Usage Agreement • let: non-procedure locals • letrec: procedural locals • The substitution model does not account for local recursive procedures

  29. Haskell Curry

  30. Currying ;Type: [Number*Number -> Number] (define add (λ (x y) (+ x y))) ;Type: [Number -> [Number -> Number]] (define c-add (λ (x) (λ (y) (add x y)))) (define add3 (c-add 3)) (add3 4) 7

  31. Why Currying? (define add-fib (lambda (x y) (+ (fib x) y))) (define c-add-fib (lambda (x) (lambda (y) (+ (fib x) y)))) (define c-add-fib (lambda (x) (let ((fib-x (fib x))) (lambda (y) (+ fib-x y)))))

More Related