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 Names from slide 9 onward ◮ Andrew Pitts’ Lecture Notes on Semantics of Programming Languages: & http://www.inf.ed.ac.uk/teaching/courses/lsi/sempl.pdf . Functions ◮ Semantics of programming languages Course Notes 2014-2015: Chapter 4, A simple functional language , by Matthew Hennessy, Trinity College Dublin, University of Dublin, 2014. https://www.scss.tcd.ie/Matthew.Hennessy/ A Second Attempt splexternal2015/LectureNotes/Notes14%20copy.pdf ◮ William Cook, Anatomy of Programming Languages, Chapter 3, Great Seal http://www.cs.utexas.edu/~wcook/anatomy/anatomy.htm Jim Royer of the ✚ The Y in the equation ( Y F ) = ( F ( Y F )) is a Y-combinator (discussed later) is a Knights of the λ -Calculus February 26, 2019 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 1 CIS 352 ❖ Names & Functions 2 fv ( E ) = the set of free variables of LFP expression E LFP = LC + λ + function application + variables Definition: LFP Expressions � fv ( n ) , fv ( b ) , = ∅ . fv ( ℓ ) , fv ( skip ) fv ( ! E ) = fv ( E ) . 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 fv ( E 1 iop E 2 ) , fv ( E 1 cop E 2 ) , � | let X = E in E � | x | λ x . E | E E fv ( E 1 : = E 2 ) , fv ( E 1 ; E 2 ) , = fv ( E 1 ) ∪ fv ( E 2 ) . � �� � fv ( while E 1 do E 2 ) , fv ( E 1 E 2 ) the λ -calculus where fv ( if E 0 then E 1 else E 2 ) = fv ( E 0 ) ∪ fv ( E 1 ) ∪ fv ( E 2 ) . ◮ x ∈ V , an unlimited set of variables ◮ n ∈ Z (integers), b ∈ B (booleans), ℓ ∈ L (locations) fv ( x ) = { x } . ◮ iop ∈ (integer-valued binary operations: + , − , ∗ , etc.) fv ( λ x . E ) = fv ( E ) − { x } . ◮ cop ∈ (comparison operations: == , < , � = , etc.) fv ( let x = E 1 in E 2 ) = fv ( E 1 ) ∪ ( fv ( E 2 ) − { x } ) CIS 352 ❖ Names & Functions 3 CIS 352 ❖ Names & Functions 4
Class Exercise: Labeling Variables Free or Bound Names & Functions Class Exercise: Labeling Variables Free or Bound 2019-02-26 0. let a = b in ( let c = a in ( a + ( b + c ))) Sample Answer: let a 1 = b free in ( let c 2 = a 1 in ( a 1 + ( b free + c 2 ))) 1. let x = 3 + x in x + y 2. λ x . λ y . ( y (( xx ) y )) Class Exercise: Labeling Variables Free or 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 )) Bound 0. let a = b in ( let c = a in ( a + ( b + c ))) 1. let x 1 = 3 + x free in x 1 + y free Sample Answer: 2. λ x 1 . λ y 2 . ( y 2 (( x 1 x 1 ) y 2 )) let a 1 = b free in ( let c 2 = a 1 in ( a 1 + ( b free + c 2 ))) 3. let x 1 = 14 in ( let p 2 = ( λ y 3 . x 1 + y 3 ) in ( let x 4 = 3 + x 1 in ( p 2 x 4 ))) 1. let x = 3 + x in x + y 4. let x 1 = 14 in ( let p 2 = ( λ x 3 . x 3 + y free ) in ( let x 4 = 3 + x 1 in ( p 2 x 4 ))) 2. λ x . λ y . ( y (( xx ) y )) 5. (( λ x 1 . ( let x 2 = x 1 + 7 in x 2 + y free )) ( x free + y free )) 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 Defining LFP substitution (the easy/boring cases) Defining LFP substitution (the harder cases) � if x = y V [ P / x ] = V ( ∗ ) P , y [ P / x ] = y , if x � = y ( E 1 op E 2 )[ P / x ] = ( E 1 [ P / x ]) op ( E 2 [ P / x ]) ( ‡ ) � ( λ y . P ′ ) , if x = y ; ( λ y . P ′ )[ P / x ] = ( ℓ : = E )[ P / x ] = ( ℓ : =( E [ P / x ]) ( λ z . P ′′′ ) , o/w, where ( ∗ ) � ( C 1 ; C 2 )[ P / x ] = ( C 1 [ P / x ]) ; ( C 2 [ P / x ]) let y = ( P 1 [ P / x ]) in P 2 , if x = y ; ( let y = P 1 in P 2 )[ P / x ] = let z = ( P 1 [ P / x ]) in P ′′ o/w, where ( † ) 2 , ( while B do C )[ P / x ] = while ( B [ P / x ]) do ( C [ P / x ]) ∈ ( freeVars ( P ) ∪ freeVars ( P ′ ) ∪ { x } ) ( ∗ ) z / ( if B then C 1 else C 2 )[ P / x ] = if ( B [ P / x ]) then ( C 1 [ P / x ]) else ( C 2 [ P / x ]) P ′′ = P ′ [ z / y ] P ′′′ = P ′′ [ P / x ] ∈ ( freeVars ( P ) ∪ freeVars ( P 2 ) ∪ { x } ) (†) z / ( E 1 E 2 )[ P / x ] = ( E 1 [ P / x ]) E 2 [ P / x ]) P ′ P ′′ 2 = P ′ 2 = P 2 [ z / y ] 2 [ P / x ] ( ∗ ) V is a number, boolean value, location, or a skip . (Why all the fuss?) ( ‡ ) op = + , − , ∗ , ≤ , . . . CIS 352 ❖ Names & Functions 6 CIS 352 ❖ Names & Functions 7
Recall: Capturing a variable (in C) Class Exercise: Substitutions #define INCI(i) { int a=0; ++i; } int main(void) { (a) ( z y )[( t v ) / y ] int a = 0, b = 0; INCI(a); (b) ( z y )[( t v ) / w ] INCI(b); printf("a is now %d, b is now %d", a, b); (c) (( z y ) z )[( y z ) / y ] return 0; (d) ( λ y . ( z y ))[( t v ) / y ] } (e) ( λ t . ( z y ))[( tv ) / y ] Running the above through the C preprocessor produces: (f) ( λ z . ( x y ))[( λ x . x ) / y ] int main(void) (g) (( λ t . ( u t ))( λ w . ( t w )))[( t u ) / u ] { int a = 0, b = 0; (h) (( λ y . ( λ z . ( w z )))( λ x . ( y ( w x ))))[( x ( y z )) / w ] { 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 CIS 352 ❖ Names & Functions 9 Class Exercise: Substitutions Names & Functions A big-step semantics for LFP, 1 2019-02-26 (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 ] Class Exercise: Substitutions (g) (( λ t . ( u t ))( λ w . ( t w )))[( t u ) / u ] (h) (( λ y . ( λ z . ( w z )))( λ x . ( y ( w x ))))[( x ( y z )) / w ] The hold-overs from LC have the same rules as before: (a) ( z y )[( t v ) / y ] ( V is a value ) ⇓ -Values: answer: ( z ( t v )) � V , s � ⇓ � V , s � (b) ( z y )[( t v ) / w ] answer: ( z y ) � E 1 , s � ⇓ � n 1 , s ′ � � E 2 , s ′ � ⇓ � n 2 , s ′′ � (c) (( z y ) z )[( y z ) / y ] ( c = n 1 ⊛ n 2 ) ⇓ - ⊛ : � E 1 ⊛ E 2 , s � ⇓ � c , s ′′ � 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 ] Values consist of numbers, tt , ff , locations, skip , and λ -expressions. 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 )))) CIS 352 ❖ Names & Functions 10
A big-step semantics for LFP, 2 Call-by-name and call-by-value are incompatable For function application we have two choices: Let: Call by name Boom = def while true do skip 1 , s ′ � 1 [ E 2 / x ] , s ′ � ⇓ � V , s ′′ � ⇓ -cbn: � E 1 , s � ⇓ � λ x . E ′ � E ′ C 1 = def ( λ x . skip ) Boom � ( E 1 E 2 ) , s � ⇓ � V , s ′′ � C 2 = def ( λ x . if ! ℓ = 0 then skip else Boom )( ℓ : = 0 ) Call by value Then: 1 , s ′ � � E 2 , s ′ � ⇓ � V 2 , s ′′ � 1 [ V 2 / x ] , s ′′ � ⇓ � V , s ′′′ � ⇓ -cbv: � E 1 , s � ⇓ � λ x . E ′ � E ′ � C 1 , s � ⇓ N � skip , s � (for any s ) � ( E 1 E 2 ) , s � ⇓ � V , s ′′′ � � C 1 , s � �⇓ V ◮ ⇓ N , the call-by-name evaluation relation � C 2 , { ℓ �→ 1 } � �⇓ N ◮ ⇓ V , the call-by-value evaluation relation � C 2 , { ℓ �→ 1 } � ⇓ V � skip , { ℓ �→ 0 } � not ( ∃� V , s ′ � )[ � E , s � ⇓ N � V . s ′ � ] ◮ � E , s � �⇓ N means not ( ∃� V , s ′ � )[ � E , s � ⇓ V � V . s ′ � ] ◮ � E , s � �⇓ V means (We’ll do a closer comparison when we look at environment models for (We handle let later.) evaluation.) CIS 352 ❖ Names & Functions 11 CIS 352 ❖ Names & Functions 12 Recursion, 1 Names & Functions Recursion, 1 2019-02-26 What goes wrong? let f = λ n . if n ≤ 0 then 1 else n ∗ ( f ( n − 1 )) in ( f 4 ) Recursion, 1 What goes wrong? let f 1 = λ n 2 . if n 2 ≤ 0 then 1 else n 2 ∗ ( f free ( n 2 − 1 )) in ( f 1 4 ) let f = λ n . if n ≤ 0 then 1 else n ∗ ( f ( n − 1 )) in ( f 4 ) CIS 352 ❖ Names & Functions 13
Recommend
More recommend