View
220
Download
0
Embed Size (px)
Citation preview
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)
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 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
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
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
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])))
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
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
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)))
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
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
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)
Pure -calculus as an ABT
Names x 2 …Expressions e ::= lam(x.e) | apply(e1,e2)| x
Operator Arity
lam ??
apply ??
Pure -calculus as an ABT
Names x 2 …Expressions e ::= lam(x.e) | apply(e1,e2)| x
Operator Arity
lam (1)
apply (0,0)
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
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)))
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))
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))
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})
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}
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
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)
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))
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))
??
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))
??
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))
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)
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 http://www.freshml.org
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 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
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
Pure -calculus in SML
Names x 2 *
Expressions e ::= lam(x,e) | apply(e1,e2)| var[x]
type name (* abstract type *)
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
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
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 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
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