Lambda Lambda Jam
Lambda Lambda Jam
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)]
!
Evaluation
Beta-reduction:
App (Lam x b) a
Look for Var x in b and substitute a
Evaluation
free
Name capture:
Evaluation
Alpha conversion:
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
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)
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]
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