





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















Slide 1/
More Lambda-Calculus & Scheme
School of Information and Computer Science
University of California, Irvine
Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
Slide 2/
To typeset
(^) λ -term equations
either use the
(^) environment with the syntax as
defined in the second lecture
or use the
(^) environment (math mode)
\begin{align*} &
(\lambda
f
. \lambda
x.
f (f
x))
(\lambda
y.
y+1)
\
=&
\lambda
x.
(\lambda
y.
y+1)
((\lambda
y.
y+1)
x)
\
=&
\lambda
x.
(\lambda
y.
y+1)
(x+1)
\
=&
\lambda
x.
(x+1)+
\end{align*}
(λf.λx.f
(^) ( f x ))( λy.y
(^) + 1)
= λx.
(λy.y
(^) + 1)((
λy.y
(^) + 1)
x )
= λx.
(λy.y
(^) + 1)(
x (^) + 1)
= λx.
(x (^) + 1) + 1
Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
Slide 4/
Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
More Lambda-Calculus & Scheme
Slide 5/
λ
λ -terms
M
::=
(^) x |M M
(^) | λx.M
The
(^) binding operator
(^) λ (^) binds the
(^) variable
(^) x (^) in the
(^) λ -term
(^) λx.M
M (^) is called the
(^) scope
(^) of (^) x .
Free variables of
(^) M
(^) : FV ( x ) =
(^) { x }
( M N
(^) ) =
( M (^) ) (^) ∪ (^) FV
( N (^) )
( λx.M
(^) ) =
( M (^) ) (^) − (^) x
Free variables can’t be renamed!
Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
More Lambda-Calculus & Scheme
Slide 7/
Interesting paper
(^) with
insight into language design process
Important concepts
- Attention to theoretical considerations Recursive function theory, Lambda calculus
Various good ideas: Programs as data, garbage collection
Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
More Lambda-Calculus & Scheme
Slide 8/
Lots of programming languages allow
(^) local declarations
(^) e.g. in
Simple form for declarations: “C++ we can declare constants in a block and use them
let (^) x (^) = (^) M
(^) in (^) N (^) ”
Inside
(^) N
each occurrence of
(^) x (^) stands for
(^) M
let (^) x (^) = 10
(^) in (^) x (^) + 1
⇒
10 + 1
We can
(^) encode
(^) the
(^) let
(^) construct in
(^) λ -calculus
let (^) x (^) = (^) M
in (^) N
⇔
( λx.N
(^) ) M
Compare:
(^) ( λx.
( x (^) + 1))
⇒
10 + 1
Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
More Lambda-Calculus & Scheme
Slide 10/
Scheme’s
(letrec
((fac
(lambda
(n)
(if
(<=
n 1)
(* 1
n (fac
(-
n 1)))))))
(fac
6))
720 (define
fac
(lambda
(n)
(if
(<=
n
(* 1
n (fac
(-
n 1))))))
(fac
720 (define
(fac
n)
(if
(<=
n
(* 1
n (fac
(-
n 1)))))
(fac
720
Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
More Lambda-Calculus & Scheme
Slide 11/
Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
More Lambda-Calculus & Scheme
Slide 13/
A single
(^) λ -abstraction defines a function with one “argument.”
Idea: What do we do if we need a function with two or more “arguments”?
(^) We can view a function of two arguments as
A (^) basic
(^) construct:
f : A (^) × (^) B (^) →
(^) C
f (^) ( a, b
) =
(^) c
A (^) combination
(^) of two
(^) simpler
(^) functions:
f : A (^) →
(^) ( B (^) →
(^) C )
f (^) ( a ) =
(^) g , g ( b ) =
(^) c
The “spicier” approach is called
(^) currying
(Haskell Curry)
Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
More Lambda-Calculus & Scheme
Slide 14/
This allows
(^) two
(^) interpretations of
(^) λx.λy.
( x (^) + (^) y ) :
function with
(^) two
(^) arguments
(^) a (^) and
(^) b (^) returning
(^) a (^) + (^) b
function with
(^) one
(^) argument
(^) a (^) returning
a function with
(^) one
(^) argument
(^) b (^) returning
(^) a (^) + (^) b
( λx.
( λy.
( x (^) + (^) y )))
ab (^) = (
λy.
( a (^) + (^) y )) b (^) = (
a (^) + (^) b ) Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
More Lambda-Calculus & Scheme
Slide 16/
What are booleans?
constants:
(^) true
false
(carrier set
(^) B )
operations:
(^) not
B (^) →
(^) B , and
: B (^) × (^) B (^) →
(^) B ,...
if-then-else:
(^) if (^) c (^) then
(^) t (^) else
(^) e
(c (^) ∈ (^) B ; t, e
(^) any expression)
Modeling
(^) if (^) as a function:
depending on value of
(^) c , return either
(^) t (^) or (^) e a
λc.λt.λe.ctefunction of three arguments, if(c, t, e): Read^ a
(^) c (^) as “condition,”
(^) t (^) as “then value,” and
(^) e (^) as “else value.”
Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
More Lambda-Calculus & Scheme
Slide 17/
What is our
(^) goal
? How should
(^) if (^) work?
if true then
(^) t (^) else
(^) e
⇒
t
if false then
(^) t (^) else
(^) e
⇒
e
How can we do this in our
(^) encoding
(^) for
(^) λc.λt.λe.cte
Eventually,
(^) c (^) is (^) applied
(^) to (^) t (^) and
(^) e
If (^) c (^) represents
(^) true
, it should return
(^) t
(the
(^) first
(^) argument)
If (^) c (^) represents
(^) false
, it should return
(^) e
(the
(^) second
(^) argument) Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
Slide 19/
Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork
Abstract Machines and Garbage Collection
Slide 20/
- Natural Language “If the condition of a
(^) is
, its body is executed
once, etc.”
- Operational Semantics Explain constructs in terms of
(^) simpler
(^) ones, e.g. abstract
machines,
(^) λ -calculus reduction...
- Axiomatic Semantics Explain constructs through
(^) predicates
, e.g. pre- and
post-conditions
- Denotational Semantics Explain constructs through
(^) functions
, that’s what we look at
later...
Copyright Revision 1.8 (August 31, 2004)
c© (^) Christian H. Stork