overview lists recursive functions in lambda calculus equational - - PowerPoint PPT Presentation

overview
SMART_READER_LITE
LIVE PREVIEW

overview lists recursive functions in lambda calculus equational - - PowerPoint PPT Presentation

overview lists recursive functions in lambda calculus equational programming 2020 11 09 confluence lecture 5 about practical assignment 2 lists: idea pairs: definition definition of pairing operator: := lrz . z l r then: P Q =


slide-1
SLIDE 1

equational programming 2020 11 09 lecture 5

  • verview

lists recursive functions in lambda calculus confluence about practical assignment 2

lists: idea

a list is obtained by repeatedly forming a pair for example: [1, 2, 3] is (1, (2, (3, nil)))

pairs: definition

definition of pairing operator: π := λlrz. z l r then: π P Q =β λz. z P Q projections:: π1 := λu. u (λlr. l) = λu. u true π2 := λu. u (λlr. r) = λu. u false then: π1 (π P Q) =β P π2 (π P Q) =β Q

slide-2
SLIDE 2

lists: definition

constructors: nil := λxy. y cons := λht. λz. z h t = π definition: head := λl. l (λht. h) = π1 tail := λl. l (λht. t) = π2 then: head (cons H T) =β H tail (cons H T) =β T

empty

how do we define a predicate isempty on lists? cons H T =β λz. z H T nil := λxy. y isempty := λl. l (λx.λy.λz. false) true

additional work on encodings

paper about numerical systems in lambda-calculus by Ian Mackie paper about data types in lambda-calculus by Herman Geuvers and the references in those papers

  • verview

lists recursive functions in lambda calculus confluence about practical assignment 2

slide-3
SLIDE 3

recursive functions: examples in Haskell

factorial n = if (n==0) then 1 else (n * factorial (n-1)) som [] = 0 som (h:t) = h + (som t) length [] = 0 length (h:t) = (length t) + 1

how do we define length in lambda-calculus?

first idea: length = λl. if l is empty then zero, else length of tail of l plus 1 lists represented as nil := λxy. y and cons := λht. λz. z h t conditional represented as ite = λb. λx.λy. b x y check on empty represented as isempty := λl. l (λxyz. false) true Church numerals with 0 represented as c0 = λs. λz. z tail represented as tail = λl. l (λh. λt. t) plus one represented as S = λx. λsz. s (x s z)

use fixed point combinator to define recursive functions

So far we have: length := λl. ite (isempty l) c0 (S (length (tail l))) which still contains length. Now using M := λa. λl. ite (isempty l) c0 (S (a (tail l))) we have length =β M length So we actually look for a fixed point of M! So we take: length := Y M with M defined as above, and Y Curry’s fixed point combinator

recursive functions: method

we try to define: G with G = . . . G . . . hence we look for: G with G = (λg. . . . g . . .) G hence we take: a fixed point of λg. . . . g . . . that is, using a fixed point combinator Y we define: G = Y (λg. . . . g . . .)

slide-4
SLIDE 4

from Haskell to lambda

Haskell is translated to core Haskell which can be seen as λ+ length [] = 0 length (h:t) = (length t) + 1 becomes length l = case l of [] -> 0 (h:t) -> 1 + length t becomes (...) becomes roughly Y (λa. λl. if (l == []) then 0 else (λ(h : t). (1 + (a t))) l)

remark: lambda calculi with patterns

computation by reduction and pattern matching: first projection: (λx, y. x) 3, 5 → x[x, y := 3, 5] = 3 length of a non-empty list: (λ(h : t). 1 + (length t)) (1 : nil) → (1 + (length t))[t := nil] = 1 + length nil

  • verview

lists recursive functions in lambda calculus confluence about practical assignment 2

a next question

we know what is a result (normal form or weak head normal form) we know how to get there (leftmost-outermost reduction) this is a way of dealing with non-termination but we may also use a different evaluation, do we then get the same result? that is: what can we say about non-determinism?

slide-5
SLIDE 5

side effects and unique results

if side-effects are present we may obtain different results n + (n:=1) = { use (initial) value 0 for n } 0 + (n:= 1) = { perform assignment for n} 0 + 1 = { apply + } 1 whereas the following evaluation yields another result: n + (n:=1) = { perform assignment for n } n + 1 = { use (new) value 1 for n } 1 + 1 = { apply + } 2

lazy evaluation and unique results

every two terminating evaluations of the same Haskell expression yield the same result we will discuss this issue in the pure λ-calculus

Koblenz confluence: what and why

what is confluence? intuition: two coinitial diverging computations can be joined why is it useful? because it gives unicity of normal forms is λ-calculus with β-reduction confluent? yes how do we prove confluence?

slide-6
SLIDE 6

confluence of beta-reduction: definition

intuition: two coinitial diverging computations can be joined definition: if M →∗

β N and M →∗ β N′

then there exists P with N →∗

β P and N′ →∗ β P

the definition can be given in general for a reduction relation →

example of a non-confluent system

a b c d

why is confluence useful?

Assume → is confluent. Let N be a normal form of P, that is: P reduces to N and N cannot do a step. Suppose also M is a normal form of P, that is: P reduces to M and M cannot do a step. then N and M are equal (modulo α) (why?) confluence implies that the normal form of a term is unique

confluence for lambda-calculus with beta-reduction

theorem: λ-calculus with β-reduction is confluent consequence: a λ-term has at most one β-normal form (why?) consequence: if M =β N then there exists P with M →∗

β P and N →∗ β P (why?)

consequence of consequence: lambda equality is not trivial, example: λx. λy. x =β λx. λy. y

slide-7
SLIDE 7

informal equational reasoning: example divergence

we use the definition double x = x + x double (double 2) = { unfold definition INNER double } double (2 + 2) = {unfold definition double } (2 + 2) + (2 + 2) = {apply first +} 4 + (2+2) = {apply last +} 4 + 4 = {apply +} 8

informal equational reasoning: example divergence

we use the definition double x = x + x. double (double 2) = { unfold definition OUTER double } (double 2) + (double 2) = {unfold definition left double } (2 + 2) + (double 2) = {apply first +} 4 + (double 2) = {unfold definition double} 4 + (2+2) = {apply second +} 4+ 4 = {apply +} 8

lambda-calculus: example divergence

  • (λx. (λy. f y y) x) (I z) →β (λx. f x x) (I z) →β f (I z) (I z)
  • (λx. (λy. f y y) x) (I z) →β (λx. (λy. f y y) x) z

pure and impure functional programming languages

pure: Haskell, Miranda programs are pure mathematical functions pure equational reasoning no side effects impure: ML, Scheme possible side effects, for example assigment and exceptions more difficult to reason about

slide-8
SLIDE 8
  • verview

lists recursive functions in lambda calculus confluence about practical assignment 2

programming assignment 2

infinite lists using lazy evaluation Church numerals algebraic data types monads

monads

simulate in a pure setting side effects now called actions encounter with monads in the practical work no monads in the written exam paper by Philip Wadler about monads

lambda and Haskell: data types

we can write for example Church numerals in Haskell (prac 2)) churchnumeral n = if (n==0) then \s -> \z -> z else \s -> \z -> (churchnumeral (n-1) s (s z)) also: algebraic data types in Haskell example: booleans data Bool = False | True deriving (Eq, Show) more general example: natural numbers data Nat = Zero | Suc Nat deriving (Eq, Show) example: polymorphically labelled binary tree: data Binarytree a = Leaf | Node (Binarytree a) a (Binarytree a) deriving Show