SLIDE 1
equational programming 2020 10 29 lecture 2
SLIDE 2
lambda terms and reduction relation with Haskell
SLIDE 3
lambda terms and reduction relation with Haskell
SLIDE 4
lambda-calculus in a nutshell
λ-terms: variable x constant c abstraction (λx. M) application (F M) we consider λ-terms modulo α-conversion β-reduction rule: (λx. M) N →β M[x := N]
SLIDE 5
intuitive approach to alpha
we identify α-equivalent λ-terms so terms are equivalence classes modulo α this is similar to identifying f : x → x2 and f : y → y2 and identifying ∀x. P(x) is ∀y. P(y) we adopt the variable convention: we convert the bound variable such that they differ from free variables
SLIDE 6
intuitive approach to substitution
assuming that we adopt the variable convention we define substitution recursively: x[x := N] = N a[x := N] = a with a = x a variable or a constant (P Q)[x := N] = (P[x := N]) (Q[x := N]) (λx. P)[x := N] = λx. P (λy. P)[x := N] = λy. (P[x := N]) (so: we assume that λy does not capture free occurrences of y in N)
SLIDE 7
substitution: example
(λx.y)[y := x] =α (λx′.y)[y := x] = λx′. x
SLIDE 8
substitution: more formal approach
first substitution is defined recursively: substitution in a variable or a constant: x[x := N] = N a[x := N] = a with a = x a variable or a constant substitution in an application: (P Q)[x := N] = (P[x := N]) (Q[x := N]) substitution in an abstraction: (λx. P)[x := N] = λx. P (λy. P)[x := N] = λy. (P[x := N]) if x = y and y ∈ FV(N) (λy. P)[x := N] = λz. (P[y := z][x := N]) if x = y and z ∈ FV(N) ∪ FV(P) and y ∈ FV(N))
SLIDE 9
alpha: more formal approach
using the definition of substitution, α-conversion is defined definition α-conversion axiom: λx. M =α λy. M[x := y] with y ∈ FV (M) definition α-equivalence relation =α: on terms P =α Q if Q can be obtained from P by finitely many ‘uses’ of the α-conversion axiom that is: by finitely many renamings of bound variables in context
SLIDE 10
substitution: examples
(λx. x)[x := c] =
SLIDE 11
substitution: examples
(λx. x)[x := c] = λx. x
SLIDE 12
substitution: examples
(λx. x)[x := c] = λx. x (λx. y)[y := c] =
SLIDE 13
substitution: examples
(λx. x)[x := c] = λx. x (λx. y)[y := c] = λx. c
SLIDE 14
substitution: examples
(λx. x)[x := c] = λx. x (λx. y)[y := c] = λx. c (λx. y)[y := x] =
SLIDE 15
substitution: examples
(λx. x)[x := c] = λx. x (λx. y)[y := c] = λx. c (λx. y)[y := x] = λz. x
SLIDE 16
substitution: examples
(λx. x)[x := c] = λx. x (λx. y)[y := c] = λx. c (λx. y)[y := x] = λz. x (λy. x (λw. v w x))[x := u v] =
SLIDE 17
substitution: examples
(λx. x)[x := c] = λx. x (λx. y)[y := c] = λx. c (λx. y)[y := x] = λz. x (λy. x (λw. v w x))[x := u v] = λy. u v(λw. v w (u v))
SLIDE 18
substitution: examples
(λx. x)[x := c] = λx. x (λx. y)[y := c] = λx. c (λx. y)[y := x] = λz. x (λy. x (λw. v w x))[x := u v] = λy. u v(λw. v w (u v)) (λy. x (λx. x))[x := λy. x y] =
SLIDE 19
substitution: examples
(λx. x)[x := c] = λx. x (λx. y)[y := c] = λx. c (λx. y)[y := x] = λz. x (λy. x (λw. v w x))[x := u v] = λy. u v(λw. v w (u v)) (λy. x (λx. x))[x := λy. x y] = λy.(λy. x y) (λx. x)
SLIDE 20
now we know the statics of the lambda-calculus
we consider λ-terms modulo α-conversion application and abstraction bound and free variables currying substitution we continue with the dynamics: β-reduction
SLIDE 21 currying
reduce a function with several arguments to functions with single arguments example: f : x → x + x becomes λx. x + x g : (x, y) → x + y becomes λx. λy. x + y, not λ(x, y). plus x y (λx. λy. x + y) 3 is an example of partial application history: due to Frege, Sch¨
related to the isomorphism between A × B → C and A → (B → C)
SLIDE 22
theory and dynamics: beta
the β-axiom: (λx. M) N =β M[x := N] gives a theory λβ in which we consider statements P =β Q the β-reduction rule: (λx. M) N →β M[x := N]] gives a theory of reduction →β in which we consider statements P →β Q
SLIDE 23
beta reduction: examples
(λx. x) y →β x[x := y] = y (λx. x x) y →β (x x)[x := y] = y y (λx. x z) y →β (x z)[x := y] = y z (λx. z) y →β z[x := y] = z Ω = (λx. x x) (λx. x x) →β Ω K I Ω →β K I Ω and also K I Ω →β (λy.I) Ω →β I
SLIDE 24
we really need renaming
α is a source of problems but we cannot do without: (λx. x x) (λs. λz. s z) →β (λs. λz. s z) (λs. λz. s z) →β λz. (λs. λz. s z) z →β λz. λz′. zz′
SLIDE 25 reduction or computation
→β
- ne β-reduction step
- btained by applying the β-reduction rule somewhere in a term
→∗
β
a β-reduction (sequence) consisting of 0, 1 or more β-reduction steps the reflexive and transitive closure of →β =β a β-conversion consisting of 0, 1 or more β-reduction steps in either direction the reflexive, transitive and symmetric closure of →β
SLIDE 26
beta-redexes and normal forms
a β-redex is an instance of the left-hand side of the β-reduction rule that is: sub-term of the form (λx. M) N a term can contain zero, one, or more redexes x ((λy. y) u) contains one β-redex x (λy. y) u contains no β-redexes a β-normal form is a term without redexes if M is a normal form we also say: M is in normal form
SLIDE 27
beta-normal form: definition and lemma
intuition: a normal form (NF) is a result of a computation definition: a λ-term is a β-normal form if it does not contain a β-redex so it cannot do a β-reduction step lemma: M is a β-normal form if and only if M = λx. M0 with M0 a normal form, or M = x M1 . . . Mn with n ≥ 0 and M1, . . . , Mn normal forms
SLIDE 28
lambda terms and reduction relation with Haskell
SLIDE 29
compare with Haskell: functions are first-class citizens
just like in λ-calculus however: we cannot check equality of functions (otherwise Haskell would be able to solve the Collatz conjecture)
SLIDE 30
compare with Haskell: application of functions
application by juxtaposition F M N and head [1,2] also: partial application sum = foldr (+) 0 also: anonymous function λx. x and \x -> x in Haskell: only arguments of the right type so far: untyped λ-calculus, later simply typed λ-calculus
SLIDE 31
lambda calculus as model of computation
M term program →β reduction evaluation normal form result
SLIDE 32 additional material
H.P. Barendregt The Lambda Calculus Its Syntax and Semantics
J.R. Hindley and J.P. Seldin Introduction to Combinators and (lambda) Calculus
Why functional programming matters by John Hughes
History of Lambda-calculus and Combinatory Logic by Felice Cardone en J.Roger Hindley