Docsity

CS 421: Programming Languages and Compilers - Lambda Calculus - Prof. Elsa Gunter, Study notes of Computer Science

Partial preview of the text

Download CS 421: Programming Languages and Compilers - Lambda Calculus - Prof. Elsa Gunter and more Study notes Computer Science in PDF only on Docsity!

Programming Languages and

Compilers (CS 421)

Elsa L Gunter 2112 SC, UIUC http://www.cs.uiuc.edu/class/fa06/cs421/ Presented by Baris Aktemur and Chris Osborn Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha, and on slides by Grigore Rosu

Where Are We?

  • First four weeks
    • Functional Programming
      • Intro to OCAML
      • Recursion
      • Higher-Order Functions
      • User defined datatypes

After Midterm

  • Language Syntax and Parsing
    • DFAs and NDFAS
    • Grammars
    • First and Follow Sets
    • LL Grammars
    • LR Grammars
  • Interpreters - Implementing Programming Language Semantics
  • And some more …

Lambda Calculus - Motivation

  • Aim is to capture the essence of functions and function applications
  • (^) λ−calculus is a theory of computation
  • “The Lambda Calculus: Its Syntax and Semantics”. H. P. Barendregt. North Holland, 1984

Untyped λ-Calculus

  • Only three kinds of expressions:
    • Variables: x, y, z, w, …
    • Abstraction: λ x. e

(Function creation, think fun x -> e)

  • Application: e 1

e

2

Untyped λ-Calculus Grammar

  • Formal BNF Grammar:
    • (^) → | | | ()
    • (^) → λ .
    • →

Example

  • Label occurrences and scope: (λ x. λ y. y (λ x. x y)) x

Untyped λ-Calculus

  • How do you compute with the λ-calculus?
  • Roughly speaking, by substitution:
  • (λ^ x. e 1 ) e 2 ⇒* e 1 [e 2 / x]
    • Modulo all kinds of subtleties to avoid free variable capture

Typed vs Untyped λ-Calculus

  • (^) The pure λ-calculus has no notion of type: (f f) is a legal expression
  • Types restrict which applications are valid
  • Types are not syntactic sugar! They disallow some terms
  • (^) Simply typed λ-calculus is less powerful than the untyped λ-Calculus: NOT Turing Complete (no recursion)

Elsa L. Gunter

Uses of λ-Calculus

  • (^) Typed and untyped λ-calculus used for theoretical study of sequential programming languages
  • Sequential programming languages are essentially the λ-calculus, extended with predefined constructs, constants, types, and syntactic sugar
  • (^) Ocaml is close to the λ-Calculus: fun x -> exp --> λ x. exp let x = e 1 in e 2 --> (λ x. e 2 )e 1

α Conversion Non-Examples

  1. y is not free in exp λ x. x y --α--> λ y. y y
  2. No free occurrence of x becomes bound when replaced by x λ x. λ y. x y --α--> λ y. λ y. y y exp exp[y/x] But λ x. (λ y. y) x --α--> λ y. (λ y. y) y And λ y. (λ y. y) y --α--> λ x. (λ y. y) x

Congruence

  • Let ~ be a relation on lambda terms. ~ is a congruence if
  • it is an equivalence relation
  • If e 1 ~ e 2 then
  • (e e 1 ) ~ (e e 2 ) and (e 1 e) ~ (e 2 e)
  • λ^ x. e 1 ~ λ x. e 2

Example

Show: λ x. (λ y. y x) x ~α~ λ y. (λ x. x y) y

  • (^) λ x. (λ y. y x) x --α--> λ z. (λ y. y z) z so λ x. (λ y. y x) x ~α~ λ z. (λ y. y z) z
  • (^) (λ y. y z) --α--> (λ x. x z) so λ z. (λ y. y z) z ~α~ λ z. (λ x. x z) z
  • (^) λ z. (λ x. x z) z --α--> λ y. (λ x. x y) y so λ z. (λ x. x z) z ~α~ λ y. (λ x. x y) y
  • (^) λ x. (λ y. y x) x ~α~ λ y. (λ x. x y) y

η (Eta) Reduction

  • (^) η Rule: λ x. f x --η--> f if x not free in f
    • Can be useful in each direction
    • Not valid in Ocaml
      • recall lambda-lifting and side effects
    • (^) Not equivalent to (λ x. f) x --> f (inst of β)
  • (^) Example: λ x. (λ y. y) x --η--> λ y. y