PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.

Slides:



Advertisements
Similar presentations
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
Advertisements

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Evaluators for Functional Programming Chapter 4 1 Chapter 4 - Evaluators for Functional Programming.
Foundations of Programming Languages: Introduction to Lambda Calculus
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
The environment model evaluator and compiler 1 The env model evaluator Motivation In one word: Efficiency Saves repeated renaming and substitution: Using.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
PPL Static Verification: Type Inference Lecture Notes: Chapter 2.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CPS 506 Comparative Programming Languages Syntax Specification.
PPL Applicative and Normal Form Verification, Type Checking and Inference.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Leftovers: LET Syntax & Formal Semantics Static Verification: Type Inference Lecture Notes: Chapter 2.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Chapter 3 – Describing Syntax
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lambda Calculus CSE 340 – Principles of Programming Languages
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
The interpreter.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Programming Language Concepts
Closures and Streams cs784(Prasad) L11Clos
Env. Model Implementation
Is everyone signed up on piazza?
Mini Language Interpreter Programming Languages (CS 550)
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Lesson2 Lambda Calculus Basics
Programming Languages and Compilers (CS 421)
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming
Announcements Quiz 6 HW7 due Tuesday, October 30
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
Lecture 2 מבוא מורחב.
Functional Programming: Lisp
Programming Languages
Lecture 2 מבוא מורחב.
Presentation transcript:

PPL Syntax & Formal Semantics Lecture Notes: Chapter 2

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

Today Concrete and Abstract Syntax Operational Semantics: applicative order and normal order

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

Concrete Syntax Non-terminals are denoted as Terminals (tokens) are surrounded with ’ Optional items are denoted as [ ] 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

Concrete Syntax -> | ’(’ ’)’ -> | ’(’ ’)’ -> | -> | -> Numbers -> ’#t’ | ’#f’

-> ( ) -> ( if )-> ( if #f ) -> ( if #f number) -> ( if #f 2) -> ( if #f ( ) 2) -> ( if #f ( / ) 2) -> ( if #f ( / 1 ) 2) -> ( if #f ( / 1 0 ) 2) -> | ’(’ ’)’ -> | | -> | -> Numbers -> ’#t’ | ’#f’ -> sequences of letters, digits, punctuation marks -> | | | | -> + -> ’lambda’ ’(’ * ’)’ + -> ’quote’ -> ’cond’ * -> ’(’ + ’)’ -> ’(’ ’else’ + ’)’ -> ’if’ -> ’let’ ’(’ * ’)’ + -> ’(’ ’)’

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

Abstract Syntax : Kinds:, : Kinds:, : Kinds:,, … : Components: Variable: Expression:

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

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

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

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: 1.Scope of lambda parameters is the entire lambda expression (or let body) 2.Scope of define variables is the entire program, from the define expression and on: Universal / global scope

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.

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

Renaming Bound variables can be renamed without changing the semantics (kind like renaming in Java Eclipse) (lambda (x) x) (lambda (y) y)

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

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

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)))

21 Applicative order evaluation rules Combination... ( …… ) Evaluate to get the procedure evaluate to get the arguments If is primitive: do whatever magic it does If is compound: evaluate body with formal parameters replaced by arguments Eager!

22 Normal order evaluation Combination … ( …… ) Evaluate to get the procedure evaluate to get the arguments If is primitive: do whatever it does If is compound: evaluate body with formal parameters replaced by arguments Lazy…

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

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?

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)))))

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

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!”

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

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

Haskell Curry

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

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)))))