0% found this document useful (0 votes)
14 views20 pages

Lambda Lambda Jam

The document introduces lambda calculus by defining its syntax using variables, function application, and lambda abstraction. It then outlines building a lambda calculator by writing an evaluator that performs beta reduction and handles capture avoidance, as well as a type system. Finally, it discusses evaluation strategies and type systems in more depth and suggests areas to expand the calculator like polymorphic types and recursion.

Uploaded by

yoyoyo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views20 pages

Lambda Lambda Jam

The document introduces lambda calculus by defining its syntax using variables, function application, and lambda abstraction. It then outlines building a lambda calculator by writing an evaluator that performs beta reduction and handles capture avoidance, as well as a type system. Finally, it discusses evaluation strategies and type systems in more depth and suggests areas to expand the calculator like polymorphic types and recursion.

Uploaded by

yoyoyo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Lets make a lambda

calculator
Rnar Bjarnason
@runarorama
Lambda Jam Chicago, July 2014

Lambda Calculus

Variables

f, g, x, y, z, etc.

Function application

f x

Lambda abstraction

x.y

Lambda Calculus
data Term
= Var String
| App Term Term
| Lam String Term

Your quest:
1. Write an evaluator
!

2. Write a typer

Evaluation
type Env = [(String, Term)]
!

eval :: Env -> Term -> Term

Evaluation
Beta-reduction:

App (Lam x b) a
Look for Var x in b and substitute a

Evaluation
free

Name capture:

App (Lam x (Lam y (Var x))) (Var y)


These two y should be kept distinct.

Evaluation
Alpha conversion:

App (Lam a (Lam b (Var a))) (Var y)


Now, a and b are fresh.

Alpha conversion
(Lam v e)
Come up with a new name x for v
such that v is not free in e
substitute x wherever v occurs in e.

Capture-avoidance
strategies

Always alpha convert


Barendregt convention
HOAS
de Bruijn indexing
Scope monads

Barendregt convention
All bound variables have globally unique names.

Higher-order abstract
syntax (HOAS)
Use the host languages lambda!
Makes evaluation trivial but static analysis harder.

data Term
= Var String
| App Term Term
| Lam (Term -> Term)

de Bruijn indexing
Count the number of binders
data Term
= Free String
| Bound Int
| App Term Term
| Lam Term

id = Lam (Bound 0)
const = Lam (Lam (Bound 1))

Compromise
Names for variables, and HOAS at runtime!
data Term
= Var String
| Lit Int
| App Term Term
| Lam String Term

data Value
= Val Int
| Fun (Value -> Value)

eval :: Env -> Term -> Value

Built-ins
data Term
= Var String
| App Term Term
| Lam String Term
!

|
|
|
|

Lit
Add
Mul
Ifz

Int
Term Term
Term Term
Int Term Term

Simply typed
data Term
= Var String
| App Term Term
| Lam Type String Term

Simple types
data Type
= Int
| Fun Type Type

Evaluation strategies:
f(x)
Call by value:
1. Evaluate x to v
2. Evaluate f to y.e
3. Evaluate e[y/v]

Call by name:
1. Evaluate f to y.e
2. Evaluate e[y/x]

Neither one evaluates under a lambda.

Author

Type system

Author

Today

Today

x: 2
`x: (1)
x: 2
(1)
x: 2
c
is
a
constant
of type T
`x:
(1)
(2)
`x:
`c:T
x: 2
c is a constant of type T
(1)
(2)
c is a constant
of type T
,x:
`e:
`x:
`c:T
(2)
(3)
`c:T
`( x: . e):( ! )
,x: `e:
c is a constant of type T
(3)
,x: `e:
(2)
`e
:
!
`e
1 . e):( ! )2 :
`( x:
`c:T (3)
(4)
`( x: . e):( ! )
`e1 e2 :
`e1 : !
`e2 :
,x:
`e:
(4)
`e1 : !
`e2 :
`e1 e2 :
(3)
(4)
`( `e
x: .e e):(
!
)
:
1
2

`e1 : !
`e2 :
`e1 e2 :

(4)

Where to go from
here?

Polymorphic types
Recursion
Definitions (lets)
Modules system
Surface syntax (parser)
Data types

Pattern matching
Runtime
Foreign function interface
I/O and effects
Errors
Compiler

You might also like