Docsity

Study Materials for Software Foundations | CIS 500, Study notes of Computer Science

Partial preview of the text

Download Study Materials for Software Foundations | CIS 500 and more Study notes Computer Science in PDF only on Docsity!

CIS 500

Software Foundations

Fall 2006

October 2

Preliminaries

Homework

Conclusion: I (^) Basically hold course I (^) Make homeworks a little shorter and tighter I (^) Change grading scheme for “PhD problems” I (^) Non-PhD students in “PhD groups” will be graded the same as those in non-PhD groups I (^) Slow down a little more on harder bits of material during lectures I (^) I need your help for this!

Midterm

I (^) Wednesday, October 11th I (^) Topics: I (^) Basic OCaml I (^) TAPL Chapters 3– I (^) Inductive definitions and proofs I (^) Operational semantics I (^) Untyped lambda-calculus I (^) Simple types

Substitution

Our definition of evaluation is based on the “substitution” of values for free variables within terms.

(λx.t 12 ) v 2 −→ [x 7 → v 2 ]t 12 (E-AppAbs)

But what is substitution, exactly? How do we define it?

Substitution

For example, what does

(λx. x (λy. x y)) (λx. x y x)

reduce to?

Note that this example is not a “complete program” — the whole term is not closed. We are mostly interested in the reduction behavior of closed terms, but reduction of open terms is also important in some contexts: I (^) program optimization I (^) alternative reduction strategies such as “full beta-reduction”

Formalizing Substitution

Consider the following definition of substitution: [x 7 → s]x = s [x 7 → s]y = y if x 6 = y [x 7 → s](λy.t 1 ) = λy. ([x 7 → s]t 1 ) [x 7 → s](t 1 t 2 ) = ([x 7 → s]t 1 )([x 7 → s]t 2 ) What is wrong with this definition?

It substitutes for free and bound variables!

[x 7 → y](λx. x) = λx.y

This is not what we want!

Substitution, take two

[x 7 → s]x = s [x 7 → s]y = y if x 6 = y [x 7 → s](λy.t 1 ) = λy. ([x 7 → s]t 1 ) if x 6 = y [x 7 → s](λx.t 1 ) = λx. t 1 [x 7 → s](t 1 t 2 ) = ([x 7 → s]t 1 )([x 7 → s]t 2 )

What is wrong with this definition?

Substitution, take three

[x 7 → s]x = s [x 7 → s]y = y if x 6 = y [x 7 → s](λy.t 1 ) = λy. ([x 7 → s]t 1 ) if x 6 = y, y 6 ∈ FV (s) [x 7 → s](λx.t 1 ) = λx. t 1 [x 7 → s](t 1 t 2 ) = ([x 7 → s]t 1 )([x 7 → s]t 2 ) What is wrong with this definition?

Substitution, take three

[x 7 → s]x = s [x 7 → s]y = y if x 6 = y [x 7 → s](λy.t 1 ) = λy. ([x 7 → s]t 1 ) if x 6 = y, y 6 ∈ FV (s) [x 7 → s](λx.t 1 ) = λx. t 1 [x 7 → s](t 1 t 2 ) = ([x 7 → s]t 1 )([x 7 → s]t 2 ) What is wrong with this definition?

Now substition is a partial function!

E.g., x 7 → y is undefined.

But we want an result for every substitution.

Alpha-equivalence classes

In fact, we can create equivalence classes of terms that differ only in the names of bound variables.

When working with the lambda calculus, it is convenient to think about these equivalence classes, instead of raw terms.

For example, when we write λx.x we mean not just this term, but the class of terms that includes λy.y and λz.z.

We can now freely choose a different representative from a term’s alpha-equivalence class, whenever we need to, to avoid getting stuck.

Substitution, for alpha-equivalence classes

Now consider substitution as an operation over alpha-equivalence classes of terms. [x 7 → s]x = s [x 7 → s]y = y if x 6 = y [x 7 → s](λy.t 1 ) = λy. ([x 7 → s]t 1 ) if x 6 = y, y 6 ∈ FV (s) [x 7 → s](λx.t 1 ) = λx. t 1 [x 7 → s](t 1 t 2 ) = ([x 7 → s]t 1 )([x 7 → s]t 2 ) Examples: I (^) x 7 → y must give the same result as x 7 → y. We know the latter is λz.y, so that is what we will use for the former. I (^) x 7 → y must give the same result as x 7 → y. We know the latter is λw.z so that is what we use for the former.

Types

Plan

I (^) For today, we’ll go back to the simple language of arithmetic and boolean expressions and show how to equip it with a (very simple) type system I (^) The key property of this type system will be soundness: Well-typed programs do not get stuck I (^) Next time, we’ll develop a simple type system for the lambda-calculus I (^) We’ll spend a good part of the rest of the semester adding features to this type system