PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
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.
Evaluators for Functional Programming Chapter 4 1 Chapter 4 - Evaluators for Functional Programming.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
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.
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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Winter 2003/4Pls – syntax – Catriel Beeri1 SYNTAX Syntax: form, structure The syntax of a pl: The set of its well-formed programs The rules that define.
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.
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.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
PPL Applicative and Normal Form Verification, Type Checking and Inference.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CMSC 330: Organization of Programming Languages Operational Semantics.
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.
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,
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Chapter 3 – Describing Syntax
CS 550 Programming Languages Jeremy Johnson
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
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.
Corky Cartwright January 18, 2017
Programming Language Concepts
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
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming
The Metacircular Evaluator
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Procedures App B: SLLGEN 1.
Announcements Quiz 6 HW7 due Tuesday, October 30
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
Announcements Exam 2 on Friday, November 2nd Topics
Lecture 2 מבוא מורחב.
Introduction to the Lab
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

Syntax Concrete Abstract

Concrete Syntax Defines the actual syntax In Scheme: – Small and simple – Context free grammar We use BNF notation – Formal way to describe formal languages – 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’ -> valid variable name -> | | | … -> + -> ’define’ -> ’lambda’ ’(’ * ’)’ + … See full syntax in lecture notes! No, really. See lecture notes.

-> ( ) -> ( if )-> ( if #f ) -> ( if #f number) -> ( if #f 2) -> ( if #f ( ) 2) -> ( if #f ( / ) 2) -> ( if #f ( / 1 ) 2) -> ( if #f ( / 1 0 ) 2) Partial syntax: -> | ’(’ ’)’ -> | | -> | -> Numbers -> ’#t’ | ’#f’ -> valid variable name -> | | … -> + -> ’if’

Abstract Syntax Emphasizes the components Ignores syntactic parts irrelevant to semantics (like order of components, for example) Kinds – Kind of category Components – Elements that compose a component

Abstract Syntax : Kinds:, : Kinds:, : Kinds:,, … : Components: Variable: Expression: See notes for full syntax

Abstract Syntax Feature Diagram Can be represented by an Abstract-Syntax- Feature-Diagram (AS-FD) – Actually an and/or tree: kinds are or-branch and components are and-branch

Build AS-FD from Concrete Syntax -> | ‘(‘ ‘)’

: Kinds:, : Kinds:,, : Components: Variable: Expression:

Abstract Syntax Parser (ASP) and Abstract Syntax Tree (AST) ASP is a piece of software used by compilers and interpreters that defines an interface to the syntax For an expression e, AST(e) is a tree representing e. The AS-FD defines the AST representation of expressions The ASP builts AST(e) for us.

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

Recall Java… class MyClass { public int doSomething(int in) { System.out.println(in); }... MyClass myInstance = new MyClass(); myInstance.doSomething(1+5);

Operational Semantics Defined as a set of evaluation rules Actually an eval(exp) algorithm for Scheme expressions In order to formally define eval(exp) we recall some concepts and introduce new ones

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.

Examples (lambda (x) (+ x 5)) ;x is bound by its declaration in the parameter list. (define y 3) ;A binding y instance. (+ x y) ;x is free, y is bound (considering the above evaluations). (+ x ( (lambda (x) (+ x 3)) 4)) ;the 1st x occurrence is free, ;the 2nd is a binding instance, ;the 3rd is bound by the declaration in the lambda parameters. (lambda (y) ;a binding instance (+ x y ;the x occurrence is free ;the y occurrence is bound by the outer declaration ((lambda (x y) (+ x y)) ;the x and y occurrences are bound by the internal declarations. 3 4) ))

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

Substitution New definition!

Composition of Substitution

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 ((λ (x) (+ x 3)) 4)))◦{x = 5} = ((+ 5 ((λ (x1) (+ x1 3)) 4))) Another renaming and replacement (λ (y) (((λ (x) x) y) x))◦{x = } (rename ) => (λ (y 2 ) (((λ (x 3 ) x 3 ) y 2 ) x))◦{x = } (replace ) => (λ (y 2 ) (((λ (x 3 ) x 3 ) y 2 ) ))

Reduce Get the body part of a closure. x

The Substitution Model: Applicative Order Evaluation Now we have all the definitions we need to define eval(e).

31 Applicative order evaluation rules Combination... ( …… ) 1.Evaluate to get the procedure 2.Evaluate to get the arguments 3.If is primitive: do whatever magic it does 4.If is user-define (e.i. lambda): 1.Substitute 2.Reduce Eager!

Before We See the Pseudo-code… Predicates atom?, composite?, number?, boolean? and variable? Are used to classify expressions. Predicates primitive-procedure? And procedure? are used to classify procedures. Predicate value? Identifies Scheme values.

Another Approach: Normal Order Evaluation Also called ‘lazy’ Main idea: argument evaluation is deferred until needed: – Branch – Primitive procedure

35 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…

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

(define fact (λ (n) (let ((iter (λ (prod c) (if (> c n) prod (iter (* c prod) (+ c 1)))))) (iter 1 1)))) (define fact (λ (n) ( (λ (iter) (iter 1 1)) (λ (prod c) (if (> c n) prod (iter (* c prod) (+ c 1)))))))

LET and Recursive Procedures (define fact (λ (n 1 ) (let ((iter 2 (lambda (prod 3 c 3 ) (if (> c 3 n 1 ) prod 3 (iter (* c 3 prod 3 ) (+ c 3 1)))) )) (iter 2 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)))))