CS 611: Lecture 10

14
CS 611: Lecture 10 More Lambda Calculus September 20, 1999 Cornell University Computer Science Department Andrew Myers

description

CS 611: Lecture 10. More Lambda Calculus September 20, 1999 Cornell University Computer Science Department Andrew Myers. Why is substitution hard?. Substitution on lambda expressions: ( l y e 0 ) [ e 1 / x ]  ( l y’ e 0 [ y’/y ][ e 1 / x ]) where y ’  FV  e 0  - PowerPoint PPT Presentation

Transcript of CS 611: Lecture 10

Page 1: CS 611: Lecture 10

CS 611: Lecture 10

More Lambda CalculusSeptember 20, 1999

Cornell University Computer Science DepartmentAndrew Myers

Page 2: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

2

Why is substitution hard?• Substitution on lambda expressions:

(y e0) [e1 / x ] (y’ e0[y’/y][e1/x ])

where

y’ FV e0

y’ FV e1

• Abstract syntax DAGs without names:

(e0 e1)apply

e0 e1

( x e)

var

e

Page 3: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

3

Example(y e0) [e1 / x ] (y’ e0[y’/y][e1/x ])

(a (b (b a))) [ ( a a) / b ] = (a’ (b (b a’)) [( a a)/ b])= (a’ (b (b a’)) [( a a) / b])

= (a’ (( a a) (b a’)))

var

apply

var var

...

var

apply

var

var

...

var

var

On graphs:

On expressions:

Page 4: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

4

Holes in scope• Reuse of an identifier creates a hole in

scope(a ((b (a (b a))) a))

scope of outer “a”

• Variable is inaccessible within the region in which it is shadowed

• Common source of programming errors

Page 5: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

5

Reductions• Lambda calculus reductions

– alpha (change of variable)(x e) (x’ e[x’/x]) if (x’ FV e)

– beta (application)(( x e1) e2) e1[e2 / x]

– eta (removal of extra abstraction)(x (e x)) e if (x FV e)

• Expressions are equivalent if they– have the same Stoy diagram/abstract DAG– can be converted to each other using alpha renaming

on sub-expressions

• Evaluation is a sequence of reductions

Page 6: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

6

Normal order• Currently defined operational semantics: lazy

evaluation (call-by-name)

e0 ( x e2)

(e0 e1) e2 [ x / e1 ]

• Normal order evaluation: apply (or ) reductions to leftmost redex till no reductions can be applied (normal form)

• Always finds a normal form if there is one

• Substitutes unevaluated form of actual parameters

• Hard to understand, implement with imperative lang.

Page 7: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

7

Applicative order• Only -substitute when the argument is fully

reduced: argument evaluated before call

e1 1 e’1

(e0 e1) 1 (e0 e’1)

e0 1 e’0

(e0 v) 1 (e’0 v)

(( x e) v) 1 e[v/x]

• Almost call-by-value

Page 8: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

8

Applicative order

• Applicative order may diverge even when a normal form exists

• Example:((b c) ((a (a a)) (a (a a))))

• Need special non-strict if form:(IF TRUE 0 Y)

• What if we allow any arbitrary order of evaluation?

Page 9: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

9

Non-deterministic evaluation

e1 1 e’1

(e0 e1) 1 (e0 e’1)

e0 1 e’0

(e0 e1) 1 (e’0 e1)

e 1 e’

( x e) 1 ( x e’)

(( x e1) e2) 1 e1[e2 / x]

x FV e( x (e x)) 1 e

Page 10: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

10

Church-Rosser theorem• Non-determinism in evaluation order does

not result in non-determinism of result• Formally:

(e0 * e1 e0 * e2 )

e3 . e1 * e3 e2 * e’3 e3 = e’3

• Implies: only one normal form for an expression• Transition relation has the Church-Rosser

property or diamond property if this theorem is true

e0

e1 e2

e3

Page 11: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

11

Concurrency• Transition rules for application permit parallel

evaluation ofoperator and operand

• Church-Rosser: anyinterleaving of computationyields same result

• Many commonly-usedlanguages do not haveChurch-Rosser property C: int x=1, y = (x = 2)+x

• Intuition: lambda calculus is functional; value of expression determined locally (no store)

e1 1 e’1

(e0 e1) 1 (e0 e’1)

e0 1 e’0

(e0 e1) 1 (e’0 e1)

Page 12: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

12

Simplifying lambda calculus• Can we capture essential properties of

lambda calculus in an even simpler language?

• Can we get rid of (or restrict) variables?– S & K combinators: closed expressions are

trees of applications of only S and K (no variables or abstractions!)

– de-Bruijn indices: all variable names are integers

Page 13: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

13

DeBruijn indices• Idea: name of formal argument of

abstraction is not needede ::= e0 | e0 e1 | n

• Variable name n tells how many lambdas to walk up in AST

IDENTITY (a a) = (0)TRUE (x (y x)) = (1))FALSE = 0 (x (y y)) = (0))2 (f (a (f (f a))) = (

Page 14: CS 611: Lecture 10

CS 611—Semantics of Programming Languages—Andrew Myers

14

Translating to DeBruijn indices

• A function DB e that compiles a closed lambda expression e into DeBruijn index representation (closed = no free identifiers)

• Need extra argument N : symbol integer to keep track of indices of each identifier

• DB e = T e, Ø• T (e0 e1), N = (T e0 , N T e1 , N)

• T x , N = N(x)• T (x e) , N =

(T e, ( y . if x = y then 0 else 1+N(y)))