Names from slide 9 onward Andrew Pitts Lecture Notes on Semantics - - PowerPoint PPT Presentation

names
SMART_READER_LITE
LIVE PREVIEW

Names from slide 9 onward Andrew Pitts Lecture Notes on Semantics - - PowerPoint PPT Presentation

References CIS 352 !!!! Slides for: Language Semantics and Implementation Lectures 11 & 12 , by Richard Mayr and Colin Stirling, School of Informatics, University of Edinburgh, 2013. http://www.inf.ed.ac.uk/teaching/courses/lsi/13Lsi11-12.pdf


slide-1
SLIDE 1

Great Seal

  • f the

Knights of the λ-Calculus CIS 352

Names & Functions

A Second Attempt

Jim Royer February 26, 2019

CIS 352 ❖ Names & Functions 1

References

!!!! Slides for: Language Semantics and Implementation Lectures 11 & 12, by Richard Mayr and Colin Stirling, School of Informatics, University of Edinburgh, 2013. http://www.inf.ed.ac.uk/teaching/courses/lsi/13Lsi11-12.pdf from slide 9 onward ◮ Andrew Pitts’ Lecture Notes on Semantics of Programming Languages: http://www.inf.ed.ac.uk/teaching/courses/lsi/sempl.pdf. ◮ Semantics of programming languages Course Notes 2014-2015: Chapter 4, A simple functional language, by Matthew Hennessy, Trinity College Dublin, University

  • f Dublin, 2014. https://www.scss.tcd.ie/Matthew.Hennessy/

splexternal2015/LectureNotes/Notes14%20copy.pdf ◮ William Cook, Anatomy of Programming Languages, Chapter 3, http://www.cs.utexas.edu/~wcook/anatomy/anatomy.htm ✚ The Y in the equation (Y F) = (F (Y F)) is a Y-combinator (discussed later) is a program that builds programs. It was the inspiration of the well-known Y-Combinator start-up incubator (http://www.ycombinator.com) which is a business that builds businesses.

CIS 352 ❖ Names & Functions 2

LFP = LC + λ + function application + variables

LFP Expressions

E ::= n | b | ℓ | E iop E | E cop E | if E then E else E | !E | E : = E | skip | E; E | while E do E | x | λx.E | E E

  • the λ-calculus

| let X = E in E

  • where

◮ x ∈ V, an unlimited set of variables ◮ n ∈ Z (integers), b ∈ B (booleans), ℓ ∈ L (locations) ◮ iop ∈ (integer-valued binary operations: +, −, ∗, etc.) ◮ cop ∈ (comparison operations: ==, <, =, etc.)

CIS 352 ❖ Names & Functions 3

fv(E) = the set of free variables of LFP expression E

Definition: fv(n), fv(b), fv(ℓ), fv(skip)

  • = ∅.

fv(!E) = fv(E). fv(E1 iop E2), fv(E1 cop E2), fv(E1 : = E2), fv(E1; E2), fv(while E1 do E2), fv(E1 E2)    = fv(E1) ∪ fv(E2). fv(if E0 then E1 else E2) = fv(E0) ∪ fv(E1) ∪ fv(E2). fv(x) = { x }. fv(λx.E) = fv(E) − { x }. fv(let x = E1 in E2) = fv(E1) ∪ (fv(E2) − { x })

CIS 352 ❖ Names & Functions 4

slide-2
SLIDE 2

Class Exercise: Labeling Variables Free or Bound

  • 0. let a = b in (let c = a in (a + (b + c)))

Sample Answer: let a1 = bfree in (let c2 = a1 in (a1 + (bfree + c2)))

  • 1. let x = 3 + x in x + y
  • 2. λx.λy.(y((xx)y))
  • 3. let x = 14 in (let p = (λy.x + y) in (let x = 3 + x in (p x)))
  • 4. let x = 14 in (let p = (λx.x + y) in (let x = 3 + x in (p x)))
  • 5. ((λx.(let x = x + 7 in x + y)) (x + y))

CIS 352 ❖ Names & Functions 5

Class Exercise: Labeling Variables Free or Bound

  • 0. let a = b in (let c = a in (a + (b + c)))

Sample Answer: let a1 = bfree in (let c2 = a1 in (a1 + (bfree + c2)))

  • 1. let x = 3 + x in x + y
  • 2. λx.λy.(y((xx)y))
  • 3. let x = 14 in (let p = (λy.x + y) in (let x = 3 + x in (p x)))
  • 4. let x = 14 in (let p = (λx.x + y) in (let x = 3 + x in (p x)))
  • 5. ((λx.(let x = x + 7 in x + y)) (x + y))

2019-02-26

Names & Functions Class Exercise: Labeling Variables Free or Bound

  • 1. let x1 = 3 + xfree in x1 + yfree
  • 2. λx1.λy2.(y2((x1x1)y2))
  • 3. let x1 = 14 in (let p2 = (λy3.x1 + y3) in (let x4 = 3 + x1 in

(p2 x4)))

  • 4. let x1 = 14 in (let p2 = (λx3.x3 + yfree) in (let x4 = 3 + x1 in

(p2 x4)))

  • 5. ((λx1.(let x2 = x1 + 7 in x2 + yfree)) (xfree + yfree))

Defining LFP substitution (the easy/boring cases)

V[P/x] = V (∗) (E1 op E2)[P/x] = (E1[P/x]) op (E2[P/x]) (‡) (ℓ : = E)[P/x] = (ℓ : =(E[P/x]) (C1; C2)[P/x] = (C1[P/x]); (C2[P/x]) (while B do C)[P/x] = while (B[P/x]) do (C[P/x]) (if B then C1 else C2)[P/x] = if (B[P/x]) then (C1[P/x]) else (C2[P/x]) (E1 E2)[P/x] = (E1[P/x]) E2[P/x]) (∗) V is a number, boolean value, location, or a skip. (‡) op = +, −, ∗, ≤, . . .

CIS 352 ❖ Names & Functions 6

Defining LFP substitution (the harder cases)

y[P/x] =

  • P,

if x = y y, if x = y (λy.P′)[P/x] =

  • (λy.P′),

if x = y; (λz.P′′′),

  • /w, where (∗)

(let y = P1 in P2)[P/x] =

  • let y = (P1[P/x]) in P2,

if x = y; let z = (P1[P/x]) in P′′

2,

  • /w, where (†)

(∗) z / ∈ (freeVars(P) ∪ freeVars(P′) ∪ { x }) P′′ = P′[z/y] P′′′ = P′′[P/x] (†) z / ∈ (freeVars(P) ∪ freeVars(P2) ∪ { x }) P′

2 = P2[z/y]

P′′

2 = P′ 2[P/x]

(Why all the fuss?)

CIS 352 ❖ Names & Functions 7

slide-3
SLIDE 3

Recall: Capturing a variable (in C)

#define INCI(i) { int a=0; ++i; } int main(void) { int a = 0, b = 0; INCI(a); INCI(b); printf("a is now %d, b is now %d", a, b); return 0; }

Running the above through the C preprocessor produces:

int main(void) { int a = 0, b = 0; { int a=0; ++a; }; { int a=0; ++b; }; printf("a is now %d, b is now %d", a, b); return 0; }

CIS 352 ❖ Names & Functions 8

Class Exercise: Substitutions

(a) (z y)[(t v)/y] (b) (z y)[(t v)/w] (c) ((z y) z)[(y z)/y] (d) (λy.(z y))[(t v)/y] (e) (λt.(z y))[(tv)/y] (f) (λz.(x y))[(λx.x)/y] (g) ((λt.(u t))(λw.(t w)))[(t u)/u] (h) ((λy.(λz.(w z)))(λx.(y (w x))))[(x (y z))/w]

CIS 352 ❖ Names & Functions 9

Class Exercise: Substitutions

(a) (z y)[(t v)/y] (b) (z y)[(t v)/w] (c) ((z y) z)[(y z)/y] (d) (λy.(z y))[(t v)/y] (e) (λt.(z y))[(tv)/y] (f) (λz.(x y))[(λx.x)/y] (g) ((λt.(u t))(λw.(t w)))[(t u)/u] (h) ((λy.(λz.(w z)))(λx.(y (w x))))[(x (y z))/w]

2019-02-26

Names & Functions Class Exercise: Substitutions

(a) (z y)[(t v)/y] answer: (z (t v)) (b) (z y)[(t v)/w] answer: (z y) (c) ((z y) z)[(y z)/y] answer: ((z (y z)) z) (d) (λy.(z y))[(t v)/y] answer: (λy.(z y)) (e) (λt.(z y))[(tv)/y] answer: (λa.(z (t v))) (f) (λz.(x y))[(λx.x)/y] answer: (λz.(x(λx.x))) (g) ((λt.(u t))(λw.(t w)))[(t u)/u] answer: ((λa.((t u) a))(λw.(t w))) (h) ((λy.(λz.(w z)))(λx.(y (w x))))[(x (y z))/w] answer: ((λa.(λb.((x (y z)) b)))(λc.(y ((x (y z)) c))))

A big-step semantics for LFP, 1

The hold-overs from LC have the same rules as before:

⇓-Values:

V, s ⇓ V, s (V is a value)

⇓-⊛:

E1, s ⇓ n1, s′ E2, s′ ⇓ n2, s′′ E1 ⊛ E2, s ⇓ c, s′′ (c = n1 ⊛ n2) . . . Values consist of numbers, tt, ff, locations, skip, and λ-expressions.

CIS 352 ❖ Names & Functions 10

slide-4
SLIDE 4

A big-step semantics for LFP, 2

For function application we have two choices: Call by name

⇓-cbn: E1, s ⇓ λx.E′

1, s′

E′

1[E2/x], s′ ⇓ V, s′′

(E1 E2), s ⇓ V, s′′ Call by value

⇓-cbv: E1, s ⇓ λx.E′

1, s′

E2, s′ ⇓ V2, s′′ E′

1[V2/x], s′′ ⇓ V, s′′′

(E1 E2), s ⇓ V, s′′′ ◮ ⇓N, the call-by-name evaluation relation ◮ ⇓V, the call-by-value evaluation relation ◮ E, s ⇓N means not (∃ V, s′ )[ E, s ⇓N V.s′ ] ◮ E, s ⇓V means not (∃ V, s′ )[ E, s ⇓V V.s′ ] (We handle let later.)

CIS 352 ❖ Names & Functions 11

Call-by-name and call-by-value are incompatable

Let: Boom =def while true do skip C1 =def (λx. skip) Boom C2 =def (λx. if !ℓ = 0 then skip else Boom)(ℓ : = 0) Then: C1, s ⇓N skip, s (for any s) C1, s ⇓V C2, { ℓ → 1 } ⇓N C2, { ℓ → 1 } ⇓V skip, { ℓ → 0 } (We’ll do a closer comparison when we look at environment models for evaluation.)

CIS 352 ❖ Names & Functions 12

Recursion, 1

What goes wrong?

let f = λn. if n ≤ 0 then 1 else n ∗ (f (n − 1)) in (f 4)

CIS 352 ❖ Names & Functions 13

Recursion, 1

What goes wrong? let f = λn. if n ≤ 0 then 1 else n ∗ (f (n − 1)) in (f 4)

2019-02-26

Names & Functions Recursion, 1 let f 1 = λn2. if n2 ≤ 0 then 1 else n2 ∗ (f free (n2 − 1)) in (f 1 4)

slide-5
SLIDE 5

Recursion, 2

LFP+ = LFP + a recursion operator E ::= . . . | rec x.E Informally: “rec x.E” reads recursively define x to be E. The big-step operational semantics is given by:

unfolding: E[(rec x.E)/x], s ⇓ V, s′

rec x.E, s ⇓ V, s′

CIS 352 ❖ Names & Functions 14

Recursion, 3

Examples:

◮ rec x.x Try: rec x.x ◮ rec f.(λx. if x = 0 then 1 else x ∗ f(x − 1)) Try: (rec f.(λx. if x = 0 then 1 else x ∗ f(x − 1))) 2

  • ◮ rec z.(if E then (E′; z) else skip)

Try: rec z.(if !ℓ > 0 then (ℓ : =!ℓ − 1; z) else skip), { ℓ → 2 } rec is an example of a fixed point combinator. Haskell Curry’s Y: λf.(λx.f(xx))(λx.f(xx)). Alan Turing’s (cbn): (λx.λy.(y(xxy)))(λx.λy.(y(xxy))) Alan Turing’s (cbv): (λx.λy.(y(λz.xxyz)))(λx.λy.(y(λz.xxyz)))

CIS 352 ❖ Names & Functions 15

Digression on Y

Y = λf.(λx.f(x x))(λx.f(x x)) (requires call-by-name) Y g = (λf.(λx.f(x x))(λx.f(x x))) g = (λx.g(x x))(λx.g(x x)) = (λy.g(y y))(λx.g(x x)) = g((λx.g(x x))(λx.g(x x)))) = g(Y g) ◮ For other fixed point combinators, see:

http://en.wikipedia.org/wiki/Fixed-point_combinator#Other_ fixed-point_combinators

◮ The key point: There are all sorts of recursions hiding in the (untyped) λ-calculus: E ::= x | λx.E | (E E′).

CIS 352 ❖ Names & Functions 16

LFP+’s properties and problems

◮ Under call-by-value, LFP+ expressions can have side-effects. (λx.0) (ℓ : = 1), { ℓ → 0 } ⇓V 0, { ℓ → 1 } . [Define side-effect] ◮ However, under call-by-name (λx.0) (ℓ : = 1), { ℓ → 0 } ⇓N 0, { ℓ → 0 } . ◮ Under call-by-name, there are no side-effecting integer

  • expression. (We need to define what the integer expressions are to

nail this down.) ◮ LFP+ is determinate under both call-by-name and call-by-value. [Spell this out] ◮ Subject reduction (i.e., a type-τ expression evaluates to a type-τ value) holds, but requires a typed versions of LFP+.

CIS 352 ❖ Names & Functions 17