Docsity

Lecture 3: Lambda-Calculus & Scheme in ICS/CSE 141 at UC Irvine, Study notes of Computer Science

Partial preview of the text

Download Lecture 3: Lambda-Calculus & Scheme in ICS/CSE 141 at UC Irvine and more Study notes Computer Science in PDF only on Docsity!

Slide 1/

ICS/CSE 141: Programming Languages

More Lambda-Calculus & Scheme

School of Information and Computer Science

University of California, Irvine

Chris Stork

[email protected]

Copyright Revision 1.8 (August 31, 2004)

c© (^) Christian H. Stork

Slide 2/

Notes on L

TA

E

Xing

To typeset

(^) λ -term equations

either use the

verbatim

(^) environment with the syntax as

defined in the second lecture

or use the

align*

(^) 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/

More Lambda-Calculus & Scheme

Copyright Revision 1.8 (August 31, 2004)

c© (^) Christian H. Stork

More Lambda-Calculus & Scheme

Slide 5/

λ

-Calculus Review

λ -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 }

FV

( M N

(^) ) =

FV

( M (^) ) (^) ∪ (^) FV

( N (^) )

FV

( λx.M

(^) ) =

FV

( 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/

McCarthy’s 1960 Paper

Interesting paper

(^) with

good language ideas, succinct presentation

some feel for historical context

insight into language design process

Important concepts

Interest in symbolic computation influenced design

Use of simple machine model

- 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/

Declarations

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/

Recursive Functions

Scheme’s

letrec, define

(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/

Recursion over Lists

> (define (len l)

(if (null? l)

(len (cdr l)))))

> (len ’(1 2

Copyright Revision 1.8 (August 31, 2004)

c© (^) Christian H. Stork

More Lambda-Calculus & Scheme

Slide 13/

Currying

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/

Currying II

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/

Booleans

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/

Booleans II

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/

Abstract Machines and Garbage

Collection

Copyright Revision 1.8 (August 31, 2004)

c© (^) Christian H. Stork

Abstract Machines and Garbage Collection

Slide 20/

Semantics

- Natural Language “If the condition of a

while

(^) is

true

, 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