Syntax With Binders

44
Syntax With Binders COS 441 Princeton University Fall 2004

description

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). - PowerPoint PPT Presentation

Transcript of Syntax With Binders

Page 1: Syntax With Binders

Syntax With Binders

COS 441

Princeton University

Fall 2004

Page 2: Syntax With Binders

Quick Review About Binding

• Difference between bound and free variables x. (x + y)

• -equivalencex. x = y. y

• Capture avoiding substitution

(( x.(x + y)) x) ! (( z. (z + y)) x) ! z. (x + y)

Page 3: Syntax With Binders

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)

Page 4: Syntax With Binders

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

Page 5: Syntax With Binders

ASTs

Variables x 2 …

Numbers n 2 N

Expressions e ::= num[n] | plus(e1,e2) | times(e1,e2)

| let(e1,x.e2) | x

Operator Arity

num[n] 0

times 2

plus 2

Page 6: Syntax With Binders

Encoding Binders with ASTs

Names x 2 …Numbers n 2 N

Expressions e ::= num[n] | plus(e1,e2) | times(e1,e2)

| let(x,e1,e2) | var[x]

Operator Arity

num[n] 0

times 2

plus 2

let 3

var[x] 0

Page 7: Syntax With Binders

Encoding Binders with ASTs

Names x 2 …Numbers n 2 N

Expressions e ::= num[n] | plus(e1,e2) | times(e1,e2)

| let(x,e1,e2) | var[x]

let(x1,num[1],

let(x2,num[2],

plus(var[x1],var[x2])))

let val x = 1 val y = 2in x + yend

Concrete Syntax Abstract Syntax Tree

Page 8: Syntax With Binders

Equality of ASTs

The AST below are not equal

let(x1,num[1],

let(x2,num[2],

plus(var[x1],var[x2])))

let(x2,num[1],

let(x1,num[2],

plus(var[x2],var[x1])))

Page 9: Syntax With Binders

Encoding Binders with ABTs

Names x 2 …

Numbers n 2 N

Expressions e ::= num[n] | plus(e1,e2) | times(e1,e2)

| let(e1,x.e2) | x

name abstractor

Page 10: Syntax With Binders

Encoding Binders with ABTs

Names x 2 …

Numbers n 2 N

Expressions e ::= num[n] | plus(e1,e2) | times(e1,e2)

| let(e1,x.e2) | x

let(num[1],x1.

let(num[2],x2.

plus(x1,x2)))

let val x = 1 val y = 2in x + yend

Concrete Syntax Abstract Binding Tree

Page 11: Syntax With Binders

Equality of ABTs

The ABTs below are -equal

let(num[1],x1.

let(num[2],x2.

plus(x1,x2)))

let(num[1],x2.

let(num[2],x1.

plus(x2,x1)))

Page 12: Syntax With Binders

Encoding Binders with ABTs

Names x 2 *

Numbers n 2 N

Expressions e ::= num[n] | plus(e1,e2) | times(e1,e2)

| let(e1,x.e2) | x

Operator Arity

num[n] ()

times

plus

let

Page 13: Syntax With Binders

Encoding Binders with ABTs

Names x 2 *

Numbers n 2 N

Expressions e ::= num[n] | plus(e1,e2) | times(e1,e2)

| let(e1,x.e2) | x

Operator Arity

num[n] ()

times (0,0)

plus (0,0)

let

Page 14: Syntax With Binders

Encoding Binders with ABTs

Names x 2 *

Numbers n 2 N

Expressions e ::= num[n] | plus(e1,e2) | times(e1,e2)

| let(e1,x.e2) | x

Operator Arity

num[n] ()

times (0,0)

plus (0,0)

let (0,1)

Page 15: Syntax With Binders

Pure -calculus as an ABT

Names x 2 …Expressions e ::= lam(x.e) | apply(e1,e2)| x

Operator Arity

lam ??

apply ??

Page 16: Syntax With Binders

Pure -calculus as an ABT

Names x 2 …Expressions e ::= lam(x.e) | apply(e1,e2)| x

Operator Arity

lam (1)

apply (0,0)

Page 17: Syntax With Binders

Pure -calculus as an ABT

Names x 2 …Expressions e ::= lam(x.e) | apply(e1,e2)| x

X expX name

V

apply(E1,E2)exp

E1 exp E2 expA

lam(X.E)exp

X name E expL

Page 18: Syntax With Binders

Free Names in -calculus

FN(X) {X}

FN(apply(E1,E2)) FN(E1) [ FN(E2)

FN(lam(X.E)) FN(E) n {X}

Page 19: Syntax With Binders

Free Names in -calculus

FN(X) {X}

FN(apply(E1,E2)) FN(E1) [ FN(E2)

FN(lam(X.E)) FN(E) n {X}

FN(apply(F,lam(X.X)))

Page 20: Syntax With Binders

Free Names in -calculus

FN(X) {X}

FN(apply(E1,E2)) FN(E1) [ FN(E2)

FN(lam(X.E)) FN(E) n {X}

FN(apply(F,lam(X.X)))

FN(F) [ FN(lam(X.X))

Page 21: Syntax With Binders

Free Names in -calculus

FN(X) {X}

FN(apply(E1,E2)) FN(E1) [ FN(E2)

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

Page 22: Syntax With Binders

Free Names in -calculus

FN(X) {X}

FN(apply(E1,E2)) FN(E1) [ FN(E2)

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

Page 23: Syntax With Binders

Free Names in -calculus

FN(X) {X}

FN(apply(E1,E2)) FN(E1) [ FN(E2)

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}

Page 24: Syntax With Binders

Capture Avoiding Substitution

• Capture avoid substitution defined using two other relations

• The first is apartness E1 # E2 when FN(E1) Å FN(E2) = {}

The unbound variables of terms are distinct

• The swapping of one name for another[X $ Y] E

Page 25: Syntax With Binders

Swapping Names in -calculus [X $ Y] X Y

[X $ Y] Y X

[X $ Y] Z Z (if Z # X, Z # Y)

[X $ Y] apply(E1,E2) apply([X $ Y] E1,

[X $ Y] E2))

[X $ Y] lam(Z.E) lam([X $ Y] Z.

[X $ Y] E)

Page 26: Syntax With Binders

Swapping Names in -calculus [X $ Y] X Y

[X $ Y] Y X

[X $ Y] Z Z (if Z # X, Z # Y)

[X $ Y] apply(E1,E2) apply([X $ Y] E1,

[X $ Y] E2))

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

Page 27: Syntax With Binders

Swapping Names in -calculus [X $ Y] X Y

[X $ Y] Y X

[X $ Y] Z Z (if Z # X, Z # Y)

[X $ Y] apply(E1,E2) apply([X $ Y] E1,

[X $ Y] E2))

[X $ Y] lam(Z.E) lam([X $ Y] Z.

[X $ Y] E)

[X $ Y] lam(X.lam(Y.apply(X,Z))

??

Page 28: Syntax With Binders

Swapping Names in -calculus [X $ Y] X Y

[X $ Y] Y X

[X $ Y] Z Z (if Z # X, Z # Y)

[X $ Y] apply(E1,E2) apply([X $ Y] E1,

[X $ Y] E2))

[X $ Y] lam(Z.E) lam([X $ Y] Z.

[X $ Y] E)

[X $ Z] lam(X.lam(Y.apply(X,Z))

??

Page 29: Syntax With Binders

Swapping Names in -calculus [X $ Y] X Y

[X $ Y] Y X

[X $ Y] Z Z (if Z # X, Z # Y)

[X $ Y] apply(E1,E2) apply([X $ Y] E1,

[X $ Y] E2))

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

Page 30: Syntax With Binders

Capture Avoiding Substitution[X Ã E] X E

[X Ã E] Y Y (if Y # X)

[X Ã E] apply(E1,E2) apply([X Ã E] E1,

[X Ã E] E2))

[X Ã E] lam(Y.E’) lam(Y.[X Ã E] E’)

(if Y # E)

Page 31: Syntax With Binders

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

Page 32: Syntax With Binders

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.org

Page 33: Syntax With Binders

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

Page 34: Syntax With Binders

Pure -calculus as an ABT

Names x 2 …Expressions e ::= lam(x.e) | apply(e1,e2)| x

X expX name

V

apply(E1,E2)exp

E1 exp E2 expA

lam(X.E)exp

X name E expL

Page 35: Syntax With Binders

Pure -calculus as an AST

Names x 2 …Expressions e ::= lam(x,e) | apply(e1,e2)| var[x]

var[X] expX name

V

apply(E1,E2)exp

E1 exp E2 expA

lam(X,E)exp

X name E expL

Page 36: Syntax With Binders

Pure -calculus in SML

Names x 2 …Expressions e ::= lam(x,e) | apply(e1,e2)| var[x]

Page 37: Syntax With Binders

Pure -calculus in SML

Names x 2 *

Expressions e ::= lam(x,e) | apply(e1,e2)| var[x]

type name (* abstract type *)

Page 38: Syntax With Binders

Pure -calculus in SMLtype name (* abstract type *)datatype exp = Lam of (name * exp) | Apply of (exp * exp) | Var of (name)

val eq_alpha : (exp * exp) -> boolval subst : (var * exp * exp)

-> exp val free_vars : exp -> var list

Page 39: Syntax With Binders

Pure -calculus in SMLtype name (* abstract type *)datatype exp = Lam of (name * exp) | Apply of (exp * exp) | Var of (name)

val fresh : unit -> nameval name_eq : (name * name) -> boolval alpha_cvt : exp -> expval subst : (name * exp * exp)

-> exp

Page 40: Syntax With Binders

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

Page 41: Syntax With Binders

Static Semantics for -calculus

Names x 2 …Expressions e ::= lam(x.e) | apply(e1,e2)| x

` X okX 2

ok-V

` apply(E1,E2)ok

` E1 ok ` E2 okok-A

` lam(X.E)ok

[ {X} ` E ok X ok-L

Page 42: Syntax With Binders

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

Page 43: Syntax With Binders

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

Page 44: Syntax With Binders

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