Presentation is loading. Please wait.

Presentation is loading. Please wait.

Syntax With Binders COS 441 Princeton University Fall 2004.

Similar presentations


Presentation on theme: "Syntax With Binders COS 441 Princeton University Fall 2004."— Presentation transcript:

1 Syntax With Binders COS 441 Princeton University Fall 2004

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

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

4 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

5 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

6 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

7 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

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

9 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

10 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

11  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 )))

12 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

13 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

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

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

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

17 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

18 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 }

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

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

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

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

23 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}

24 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

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

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

27 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 ))  ??

28 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 ))  ??

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

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

31 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

32 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 http://www.freshml.orghttp://www.freshml.org

33 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

34 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

35 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

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

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

38 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

39 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

40 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

41 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

42 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

43 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

44 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


Download ppt "Syntax With Binders COS 441 Princeton University Fall 2004."

Similar presentations


Ads by Google