CS计算机代考程序代写 Lambda Calculus ER cse130 https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html

cse130 https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
Describing a Programming Language
Syntax: what do programs look like? Semantics: what do programs mean?
Operational semantics: how do programs execute step-by-step?
Syntax: What Programs Look Like
7 of 69 1/7/21, 8:59 AM

cse130
https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
8 of 69
e ::= x
| (\x -> e)
ns ‘e’
| (e1 e2)
— variable ‘x’
— function that takes a parameter ‘x’ and retur
— call (function) ‘e1’ with argument ‘e2’
Programs are expressions e (also called ¦Ë-terms) of one of three kinds:
Variable
x, y, z
Abstraction (aka nameless function definition)
(\x -> e)
x is the formal parameter, e is the body ¡°for any x compute e ¡±
Application (aka function call)
(e1 e2)
e1 is the function, e2 is the argument in your favorite language: e1(e2)
(Here each of e , e1 , e2 can itself be a variable, abstraction, or application)
OO lambda CSBowizi
Ieng6 looks like
rm rf N1 stack work 1/7/21, 8:59 AM
e
2C y Z
1 Lg e
l
Ce e

cse130
https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
Examples
(\x -> x)
ts input
(\x -> (\y -> y))
(\f -> (f (\x -> x))) — A function that applies its argument to i
d
— The identity function (id) that returns i
— A function that returns (id)
QUIZ
Which of the following terms are syntactically incorrect?
arable
Fair
yA. (\(\x->x)->y) B. (\x -> (x x))
41003J
C. (\x -> (x (y x))) D. A and C
9 of 69
1/7/21, 8:59 AM

cse130
https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
E. all of the above
Examples
(\x -> x) — The identity function (id) that returns i ts input
(\x -> (\y -> y)) — A function that returns (id)
(\f -> (f (\x -> x))) — A function that applies its argument to i
a
gum targe
How do I define a function with two arguments?
e.g. a function that takes x and y and returns y ?
fine Cx
d
e ez
T fine
E Arg
y return y
10 of 69
Ix
ly
y
1/7/21, 8:59 AM

cse130 https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
(\x -> (\y -> y)) — A function that returns the identity functi on
— OR: a function that takes two arguments
— and returns the second one!
How do I apply a function to two arguments?
e.g. apply (\x -> (\y -> y)) to apple and banana ?
11 of 69 1/7/21, 8:59 AM

cse130 https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
Cx
¦Ì
ly y X apple
e
banana
X
lx
1
3
(((\x -> (\y -> y)) apple) banana) — first apply to apple,
— then apply the result to ban
ana
Syntactic Sugar
instead of we write
12 of 69 1/7/21, 8:59 AM

cse130 https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
instead of
\x->(\y->(\z->e))
\x->\y->\z->e
(((e1 e2) e3) e4)
we write
\x->\y->\z->e
\x y z -> e
e1 e2 e3 e4
\x y -> y — A function that that takes two arguments — and returns the second one…
(\x y -> y) apple banana — … applied to two arguments
Look like
mean
Semantics : What Programs Mean
How do I ¡°run¡± / ¡°execute¡± a ¦Ë-term?
Syntax Semantic
13 of 69 1/7/21, 8:59 AM

cse130 https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
Think of middle-school algebra:
D
e
(1+2)*((3*8)-2)
==
3 *(24 -2) ==
3 *22 ==
D66
Execute = rewrite step-by-step
Following simple rules until no more rules apply
3 *((3*8)-2) Ie ==
d
ez
t
i
66
result
Rewrite Rules of Lambda Calculus
14 of 69 1/7/21, 8:59 AM

cse130
https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
15 of 69
1/7/21, 8:59 AM
1. ¦Â-step (aka function call)
2. ¦Á-step (aka renaming formals)
Programing
Scope
But first we have to talk about scope
var x cat
vain
3
is
fo
x
KK
cat
IT
Semantics: Scope of a Variable
The part of a program where a variable is visible
In the expression (\x -> e)
x is the newly introduced variable e is the scope of x
D
return e
any occurrence of x in (\x -> e) is bound (by the binder \x ) u
For example, x is bound in: a
(\x -> x)
(\x -> (\y -> x))
funccx

cse130 https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
An occurrence of x in e is free if it¡¯s not bound by an enclosing abstraction as e
For example, x is free in: (x y)
r;
— no binders at all!
— no \x binder
((\x -> (\y -> y)) x) — x is outside the scope of the \x binde
a
(\y -> (x y))
a
se
QUIZ
— intuition: it’s not “the same” x
f
iz
Is x bound or free in the expression ((\x -> x) x) ?
A. first occurrence is bound, second is bound 4 Epee B. first occurrence is bound, second is free bound
16 of 69 1/7/21, 8:59 AM

cse130
https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
17 of 69
1/7/21, 8:59 AM
C. first occurrence is free, second is bound D. first occurrence is free, second is free
EXERCISE: Free Variables
An variable x is free in e if there exists a free occurrence of x in e
We can formally define the set of all free variables in a term like so:
FV(x) = ??? Ee FV(\x -> e) = ???
fv apple
pv apple banane
tanana
Ea
FV(e1 e2)
= ???
esse
apple
apple.ba
lapple aapple
FV
pv dapple
banana

cse130
https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
18 of 69
1/7/21, 8:59 AM
Fv Ex FV Chae
Gene
If e has no free variables it is said to be closed Closed expressions are also called combinators
fVce 9
G
F Vce ClOosed Expressions
t
What is the shortest closed expression?
Ix
sac
a
FV
e
a
Fuced
Rewrite Rules of Lambda Calculus
D

cse130
https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
19 of 69
1/7/21, 8:59 AM
1. ¦Â-step (aka function call) D
2. ¦Á-step (aka renaming formals)
Semantics: Redex
A redex is a term of the form
((\x -> e1) e2)
Tnc Irs A function (\x -> e1)
2tE ly
54
H
I
t 5
x is the parameter
e1 is the returned expression
Applied to an argument e2 O
e2 is the argument
5
5
q

cse130 https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
Semantics: ¦Â-Reduction A redex b-steps to another term …
(\x -> e1) e2 =b> e1[x := e2]
where e1[x := e2] means
¡° e1 with all free occurrences of x replaced with e2 ¡±
so
dx s.ae apple apple
Computation by search-aDnd-replace:
If you see an abstraction applied to an argument, t
In the body of the abstraction
Replace all free occurrences of the formal by that argument
20 of 69 1/7/21, 8:59 AM

cse130 https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
We say that (\x -> e1) e2 ¦Â-steps to e1[x := e2] DD
e ez b et x ed
Csx
Redex Examples
((\x D-> x) apple)
=b> apple
Is this right? Ask Elsa (https://goto.ucsd.edu/elsa/index.html)
21 of 69 1/7/21, 8:59 AM

cse130
https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
QUIZ
((\x -> (\y -> y)) apple)
=b> ???
Ixia
b e xi
x
ez
A. apple
B. \y -> apple C. \x -> apple D. \y->y
E. \x->y
yay
w
eD
apple
22 of 69
1/7/21, 8:59 AM

cse130 https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
Ixe ez bex
=b> ???
A. (((apple apple) apple) apple)
QUIZ
papplepaple
Eez
(\x -> (((y x) y) x)) apple
e
B. (((y apple) y) apple) 00
C. (((y y) y) y) D. apple
QUIZ
23 of 69 1/7/21, 8:59 AM
I

cse130
QUIZ ajb a
https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
((\x -> (x (\x -> x))) apple)
ix s.ec =b> ???
er
e x ed
A. (apple (\x -> x)) t
fix funcGE
B. (apple (\apple -> apple)) C. (apple (\x -> apple))
D. apple
E. (\x -> x)
x Ix
def
123
33 6
EXERCISE
What is a ¦Ë-term fill_this_in such that
ee
fill_this_in apple
=b> banana
apple
24 of 69 D banana
1/7/21, 8:59 AM
xE1 3
return
x
binder useloccurrences

cse130
https://ucsd-cse130.github.io/wi21/lectures/01-lambda.html
25 of 69
1/7/21, 8:59 AM
ELSA: https://goto.ucsd.edu/elsa/index.html
Click here to try this exercise (https://goto.ucsd.edu /elsa/index.html#?demo=permalink%2F1585434473_24432.lc)
Ix s apple banana
tanana x
A Tricky One
7C Y Z Ff
((\x -> (\y -> x)) y)
y
vx Eve
=b>\y->y
Is this right?
Fv late
E
e
Cute
x
apple
banga
b
e
YE
ez