1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.

Slides:



Advertisements
Similar presentations
Substitution & Evaluation Order cos 441 David Walker.
Advertisements

The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
A Type System for Well-Founded Recursion Derek Dreyer Carnegie Mellon University POPL 2004 Venice, Italy.
Catriel Beeri Pls/Winter 2004/5 functional-language 1 Substitution Semantics of FL – a simple functional language FL is EL + (non-recursive) function creation.
Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from G. Klein, NICTA.
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
School of Computing and Mathematics, University of Huddersfield CHA2545 Last Lecture LECTURE: LAMBDA CALCULUS SEMANTICS TUTORIAL: Revision.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
Comp 205: Comparative Programming Languages Semantics of Functional Languages Term- and Graph-Rewriting The λ-calculus Lecture notes, exercises, etc.,
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
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.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Principles of programming languages 9: Lambda calculus Isao Sasano Department of Information Science and Engineering.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.
Lesson2 Lambda Calculus Basics 1/10/02 Chapter 5.1, 5.2.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
CMSC 330: Organization of Programming Languages Operational Semantics.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L04Ext-1 September 21, 2006http:// Some more thoughts.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 8 LECTURE: LAMBDA CALCULUS SEMANTICS TUTORIAL: exercises.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
CS5205: Foundations in Programming Languages
CS510 Compiler Lecture 4.
More on Lambda Calculus
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
Lesson2 Lambda Calculus Basics
Programming Languages and Compilers (CS 421)
Announcements Quiz 6 HW7 due Tuesday, October 30
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Chapter 3 Describing Syntax and Semantics.
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Presentation transcript:

1 Formal Semantics

2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some things are often overlooked for any language evaluation order? side-effects? errors? Therefore, want to formalize what a language's definition really is Ideally, a clear & unambiguous way to define a language Programmers & compiler writers can agree on what's supposed to happen, for all programs Can try to prove rigorously that the language designer got all the corner cases right

3 Aspects to formalize Syntax: what's a syntactically well-formed program? EBNF notation for a context-free grammar Static semantics: which syntactically well-formed programs are semantically well-formed? which programs type-check? typing rules, well-formedness judgments Dynamic semantics: what does a program evaluate to or do when it runs? operational, denotational, or axiomatic semantics Metatheory: properties of the formalization itself E.g. do the static and dynamic semantics match? i.e., is the static semantics sound w.r.t. the dynamic semantics?

4 Approach Formalizing full-sized languages is very hard, tedious many cases to consider lots of interacting features Better: boil full-sized language down into essential core, then formalize and study the core cut out as much complication as possible, without losing the key parts that need formal study hope that insights gained about core will carry back to full-sized language

5 The lambda calculus The essential core of a (functional) programming language The tiniest Turing-complete programming language Outline: Untyped: syntax, dynamic semantics, cool properties Simply typed: static semantics, soundness, more cool properties Polymorphic: fancier static semantics

6 Untyped -calculus: syntax (Abstract) syntax: e ::= xvariable | x. efunction/abstraction (  fn x => e ) | e 1 e 2 call/application Freely parenthesize in concrete syntax to imply the right abstract syntax The trees described by this grammar are called term trees

7 Free and bound variables x. e binds x in e An occurrence of a variable x is free in e if it's not bound by some enclosing lambda freeVars(x)  x freeVars( x. e)  freeVars(e) – {x} freeVars(e 1 e 2 )  freeVars(e 1 )  freeVars(e 2 ) e is closed iff freeVars(e) = {}

8  -renaming First semantic property of lambda calculus: bound variables in a term tree can be renamed (properly) without affecting the semantics of the term tree  -equivalent term trees ( x 1. x 2 x 1 )   ( x 3. x 2 x 3 ) cannot rename free variables term e: e and all  -equivalent term trees Can freely rename bound vars whenever helpful

9 Evaluation:  -reduction Define what it means to "run" a lambda- calculus program by giving simple reduction/rewriting/simplification rules "e 1   e 2 " means "e 1 evaluates to e 2 in one step" One case: ( x. e 1 ) e 2   [x  e 2 ]e 1 "if you see a lambda applied to an argument expression, rewrite it into the lambda body where all free occurrences of the formal in the body have been replaced by the argument expression"

10 Examples

11 Substitution When doing substitution, must avoid changing the meaning of a variable occurrence [x  e]x  e [x  e]y  y, if x  y [x  e]( x. e 2 )  ( x. e 2 ) [x  e]( y. e 2 )  ( y. [x  e]e 2 ), if x  y andy not free in e [x  e](e 1 e 2 )  ([x  e]e 1 ) ([x  e]e 2 ) use  -renaming to ensure "y not free in e"

12 Result of reduction To fully evaluate a lambda calculus term, i.e., to determine the meaning of a term, simply perform  -reduction until you can't any more   *  reflexive, transitive closure of   When you can't any more, you have a value, which is a normal form of the input term Does every lambda-calculus term have a normal form?

13 Reduction order Can have several lambdas applied to an argument in one expression Each called a redex Therefore, several possible choices in reduction Which to choose? Does it matter? To the final result? To how long it takes to compute? To whether the result is computed at all?

14 Two reduction orders Normal-order reduction (a.k.a. call-by-name, lazy evaluation) reduce leftmost, outermost redex Applicative-order reduction (a.k.a. call-by-value, eager evaluation) reduce leftmost, outermost redex whose argument is in normal form

15 Amazing fact #1: Church-Rosser Theorem, Part 1 Thm. If e 1   * e 2 and e 1   * e 3, then  e 4 such that e 2   * e 4 and e 3   * e 4 Corollary. Every term has a unique normal form, if it has one No matter what reduction order is used! Wow!

16 Existence of normal forms? Does every term have a normal form? Consider: ( x. x x) ( x. x x)

17 Amazing fact #2: Church-Rosser Theorem, Part 2 If a term has a normal form, then normal-order reduction will find it! Applicative-order reduction might not! Example: ( x 1. ( x 2. x 2 )) (( x. x x) ( x. x x))