Syntax With Binders COS 441 Princeton University Fall 2004.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Substitution & Evaluation Order cos 441 David Walker.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
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.
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
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.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
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.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
CS 355 – Programming Languages
Foundations of Programming Languages: Introduction to Lambda Calculus
Denotational vs Operational Approaches COS 441 Princeton University Fall 2004.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Data Abstraction COS 441 Princeton University Fall 2004.
Parametric Polymorphism COS 441 Princeton University Fall 2004.
Twelf: The Quintessential Proof Assistant for Language Metatheory Karl Crary Carnegie Mellon University Joint work with Robert Harper and Michael Ashley-Rollman.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
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.
Dynamic Semantics COS 441 Princeton University Fall 2004.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Semantics for MinML COS 441 Princeton University Fall 2004.
Describing Syntax and Semantics
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.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Systems Architecture I1 Propositional Calculus Objective: To provide students with the concepts and techniques from propositional calculus so that they.
The Relational Model: Relational Calculus
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.
© 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.
A Theory of Hygienic Macros David Herman, Mitchell Wand Northeastern University.
CS 363 Comparative Programming Languages 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.
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.
Semantics In Text: Chapter 3.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Just Enough Type Theory or, Featherweight Java A Simple Formal Model of Objects Jonathan Aldrich
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
Higher-Order Encodings in Intensional Type Theory Edwin Westbrook Rice University.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
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. L05-1 September 21, 2006http:// Types and Simple Type.
Operational Semantics of Scheme
Type Checking and Type Inference
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
CSE341: Programming Languages Lecture 7 First-Class Functions
Announcements Quiz 6 HW7 due Tuesday, October 30
6.001 SICP Variations on a Scheme
Spring 2016 Program Analysis and Verification Operational Semantics
Recursive Procedures and Scopes
CSE341: Programming Languages Lecture 7 First-Class Functions
Presentation transcript:

Syntax With Binders COS 441 Princeton University Fall 2004

Quick Review About Binding Difference between bound and free variables x. (x + y)  -equivalence x. x =  y. y Capture avoiding substitution (( x.(x + y)) x) ! (( z. (z + y)) x) ! z. (x + y)

Binding in General Generalize the rules about binding and substitutions for the -calculus to cover other programming languages that uses binding Harper introduces the general notion of an abstract binding trees (ABTs) ABTs generalize what we know about abstract syntax trees (ASTs)

ASTs vs ABTs ASTs do not describe binding or scope of variables Both defined by signature that maps operators to arities AST arity is just a natural number ABT arity sequence of valences Every AST is an ABT

ASTs Variables x2… Numbers n2N Expressions e::= num[ n ] | plus( e 1, e 2 ) | times( e 1, e 2 ) | let( e 1, x.e 2 ) | x OperatorArity num[ n ] 0 times 2 plus 2

Encoding Binders with ASTs Names x2… Numbers n2N Expressions e::= num[ n ] | plus( e 1, e 2 ) | times( e 1, e 2 ) | let( x, e 1, e 2 ) | var[ x ] OperatorArity num[ n ] 0 times 2 plus 2 let 3 var[ x ] 0

Encoding Binders with ASTs Names x2… Numbers n2N Expressions e::= num[ n ] | plus( e 1, e 2 ) | times( e 1, e 2 ) | let( x, e 1, e 2 ) | var[ x ] let( x 1,num[ 1 ], let( x 2,num[ 2 ], plus(var[ x 1 ],var[ x 2 ]))) let val x = 1 val y = 2 in x + y end Concrete Syntax Abstract Syntax Tree

Equality of ASTs The AST below are not equal let( x 1,num[ 1 ], let( x 2,num[ 2 ], plus(var[ x 1 ],var[ x 2 ]))) let( x 2,num[ 1 ], let( x 1,num[ 2 ], plus(var[ x 2 ],var[ x 1 ])))

Encoding Binders with ABTs Names x2… Numbers n2N Expressions e::= num[ n ] | plus( e 1, e 2 ) | times( e 1, e 2 ) | let( e 1, x.e 2 ) | x name abstractor

Encoding Binders with ABTs Names x2… Numbers n2N Expressions e::= num[ n ] | plus( e 1, e 2 ) | times( e 1, e 2 ) | let( e 1, x.e 2 ) | x let(num[ 1 ], x 1. let(num[ 2 ], x 2. plus( x 1, x 2 ))) let val x = 1 val y = 2 in x + y end Concrete Syntax Abstract Binding Tree

 Equality of ABTs The ABTs below are  -equal let(num[ 1 ], x 1. let(num[ 2 ], x 2. plus( x 1, x 2 ))) let(num[ 1 ], x 2. let(num[ 2 ], x 1. plus( x 2, x 1 )))

Encoding Binders with ABTs Names x2 ** Numbers n2N Expressions e::= num[ n ] | plus( e 1, e 2 ) | times( e 1, e 2 ) | let( e 1, x.e 2 ) | x OperatorArity num[ n ] () times plus let

Encoding Binders with ABTs Names x2 ** Numbers n2N Expressions e::= num[ n ] | plus( e 1, e 2 ) | times( e 1, e 2 ) | let( e 1, x.e 2 ) | x OperatorArity num[ n ] () times (0,0) plus (0,0) let

Encoding Binders with ABTs Names x2 ** Numbers n2N Expressions e::= num[ n ] | plus( e 1, e 2 ) | times( e 1, e 2 ) | let( e 1, x.e 2 ) | x OperatorArity num[ n ] () times (0,0) plus (0,0) let (0,1)

Pure -calculus as an ABT Names x2… Expressions e::= lam( x.e ) | apply( e 1, e 2 ) | x OperatorArity lam ?? apply ??

Pure -calculus as an ABT Names x2… Expressions e::= lam( x.e ) | apply( e 1, e 2 ) | x OperatorArity lam (1) apply (0,0)

Pure -calculus as an ABT Names x2… Expressions e::= lam( x.e ) | apply( e 1, e 2 ) | x X exp X name V apply( E 1, E 2 ) exp E 1 exp E 2 exp A lam( X.E ) exp X name E exp L

Free Names in -calculus FN(X)  {X}{X} FN( apply( E 1, E 2 ) )  FN(E 1 ) [ FN(E 2 ) FN( lam( X.E ) )  FN(E) n { X }

Free Names in -calculus FN(X)  {X}{X} FN( apply( E 1, E 2 ) )  FN(E 1 ) [ FN(E 2 ) FN( lam( X.E ) )  FN(E) n { X } FN( apply( F, lam( X.X )) )

Free Names in -calculus FN(X)  {X}{X} FN( apply( E 1, E 2 ) )  FN(E 1 ) [ FN(E 2 ) FN( lam( X.E ) )  FN(E) n { X } FN( apply( F, lam( X.X )) )  FN(F) [ FN( lam( X.X ) )

Free Names in -calculus FN(X)  {X}{X} FN( apply( E 1, E 2 ) )  FN(E 1 ) [ FN(E 2 ) FN( lam( X.E ) )  FN(E) n { X } FN( apply( F, lam( X.X )) )  FN(F) [ FN( lam( X.X ) )  {F} [ (FN(X) n FN(X))

Free Names in -calculus FN(X)  {X}{X} FN( apply( E 1, E 2 ) )  FN(E 1 ) [ FN(E 2 ) FN( lam( X.E ) )  FN(E) n { X } FN( apply( F, lam( X.X )) )  FN(F) [ FN( lam( X.X ) )  {F} [ (FN(X) n FN(X))  {F} [ ({X} n {X})

Free Names in -calculus FN(X)  {X}{X} FN( apply( E 1, E 2 ) )  FN(E 1 ) [ FN(E 2 ) FN( lam( X.E ) )  FN(E) n { X } FN( apply( F, lam( X.X )) )  FN(F) [ FN( lam( X.X ) )  {F} [ (FN(X) n FN(X))  {F} [ ({X} n {X})  {F} [ {}  {F}

Capture Avoiding Substitution Capture avoid substitution defined using two other relations The first is apartness E 1 # E 2 when FN(E 1 ) Å FN(E 2 ) = {} The unbound variables of terms are distinct The swapping of one name for another [X $ Y] E

Swapping Names in -calculus [X $ Y] X  Y [X $ Y] Y  X [X $ Y] Z  Z (if Z # X, Z # Y) [X $ Y] apply( E 1, E 2 )  apply( [X $ Y] E 1, [X $ Y] E 2 ) ) [X $ Y] lam( Z.E )  lam( [X $ Y] Z. [X $ Y] E )

Swapping Names in -calculus [X $ Y] X  Y [X $ Y] Y  X [X $ Y] Z  Z (if Z # X, Z # Y) [X $ Y] apply( E 1, E 2 )  apply( [X $ Y] E 1, [X $ Y] E 2 ) ) [X $ Y] lam( Z.E )  lam( [X $ Y] Z. [X $ Y] E ) [X $ Y] lam( X. lam( Y. apply( X,Z ))  lam( Y. lam( X. apply( Y,Z ))

Swapping Names in -calculus [X $ Y] X  Y [X $ Y] Y  X [X $ Y] Z  Z (if Z # X, Z # Y) [X $ Y] apply( E 1, E 2 )  apply( [X $ Y] E 1, [X $ Y] E 2 ) ) [X $ Y] lam( Z.E )  lam( [X $ Y] Z. [X $ Y] E ) [X $ Y] lam( X. lam( Y. apply( X,Z ))  ??

Swapping Names in -calculus [X $ Y] X  Y [X $ Y] Y  X [X $ Y] Z  Z (if Z # X, Z # Y) [X $ Y] apply( E 1, E 2 )  apply( [X $ Y] E 1, [X $ Y] E 2 ) ) [X $ Y] lam( Z.E )  lam( [X $ Y] Z. [X $ Y] E ) [X $ Z] lam( X. lam( Y. apply( X,Z ))  ??

Swapping Names in -calculus [X $ Y] X  Y [X $ Y] Y  X [X $ Y] Z  Z (if Z # X, Z # Y) [X $ Y] apply( E 1, E 2 )  apply( [X $ Y] E 1, [X $ Y] E 2 ) ) [X $ Y] lam( Z.E )  lam( [X $ Y] Z. [X $ Y] E ) [X $ Z] lam( X. lam( Y. apply( X,Z ))  lam( Z. lam( Y. apply( Z,X ))

Capture Avoiding Substitution [X Ã E] X  E [X Ã E] Y  Y (if Y # X) [X Ã E] apply( E 1, E 2 )  apply( [X Ã E] E 1, [X Ã E] E 2 ) ) [X Ã E] lam( Y.E’ )  lam( Y.[X Ã E] E’ ) (if Y # E)

Induction Principle for ABTs Induction principle for ABT slightly more complex Two different induction hypotheses – P(E) for raw ABT – PA(n,E) for ABT abstractor of valence n Induction principle lets us rename bound variable names at will –Technicalities are not so important for this course

ABTs in SML ASTs can easily be encoded directly in SML with datatype Unfortunately there is no good direct encoding of ABTs into SML –Must first encode ABT as an AST –Globally rename all names to avoid conflicts Active area of research to provided better programming language support –See

Why Learn About ABTs Lack of direct support of ABTs in SML is annoying ABTs are a useful abstraction when specifying programming languages –All scoping and renaming rules come built in with the semantics of ABT –Rigorous theory of induction for ABTs

Pure -calculus as an ABT Names x2… Expressions e::= lam( x.e ) | apply( e 1, e 2 ) | x X exp X name V apply( E 1, E 2 ) exp E 1 exp E 2 exp A lam( X.E ) exp X name E exp L

Pure -calculus as an AST Names x2… Expressions e::= lam( x, e ) | apply( e 1, e 2 ) | var[ x ] var[ X ] exp X name V apply( E 1, E 2 ) exp E 1 exp E 2 exp A lam( X, E ) exp X name E exp L

Pure -calculus in SML Names x2… Expressions e::= lam( x, e ) | apply( e 1, e 2 ) | var[ x ]

Pure -calculus in SML Names x2 ** Expressions e::= lam( x, e ) | apply( e 1, e 2 ) | var[ x ] type name (* abstract type *)

Pure -calculus in SML type name (* abstract type *) datatype exp = Lam of (name * exp) | Apply of (exp * exp) | Var of (name) val eq_alpha : (exp * exp) -> bool val subst : (var * exp * exp) -> exp val free_vars : exp -> var list

Pure -calculus in SML type name (* abstract type *) datatype exp = Lam of (name * exp) | Apply of (exp * exp) | Var of (name) val fresh : unit -> name val name_eq : (name * name) -> bool val alpha_cvt : exp -> exp val subst : (name * exp * exp) -> exp

Static Semantics Before we can define the precise meaning of a program we must rule non-sense programs The meaning of a program is only defined for well-formed programs Example: A program is well-formed if it contains no free names

Static Semantics for -calculus Names x2… Expressions e::= lam( x.e ) | apply( e 1, e 2 ) | x  ` X ok X 2 X 2  ok-V  ` apply( E 1, E 2 ) ok  ` E 1 ok  ` E 2 ok ok-A  ` lam( X.E ) ok  [ {X} ` E ok X  ok-L

Syntax Directed Rules The ok relation is syntax directed –Exactly one rule for each case of the abt Syntax directed rules guarantee goal- directed search always succeeds or fails –Won’t get into infinite loop –Either find a derivation or find out something is not derivable Syntax directed rules are easier to implement in SML

Syntax Directed Rules (cont.) A set of rules is syntax directed with respect to a certain set of syntax trees –If for every derivable judgment there is a unique derivation tree that mirrors the original syntax tree Definition of syntax directed is a bit vague but basically means goal directed search is easy to carry out

Static Semantics/Type Checking The relation  ` E ok is very simple and only to illustrate a general pattern we will see later We will define a more interesting relation  ` E : T where T is the type of the expression Well typed programs will be free from certain runtime errors –We must define a semantics of program execution to understand what that means precisely –Next lecture will be about program execution