Recursion Lecture 6: More Lambda Calculus Programming CSC 131! Fall, 2014!! Kim Bruce Recursive definitions are handy! - fact = λn. cond (iszero n) 1 (Mult n (fact (Pred n)))! - Not a legal definition in lambda calculus because can t name functions!! Compute by expanding:! - fact 2! = cond (iszero 2) 1 (Mult 2 (fact (Pred 2)))! = Mult 2 (fact 1)! = Mult 2 (cond (iszero 1) 1 (Mult 1 (fact (Pred 1)))! = Mult 2 (Mult 1 (fact 0)) =... = Mult 2 (Mult 1 1) = 2 Recursion Fixed Points A different perspective: Start with! - fact = λn. cond (iszero n) 1 (Mult n (fact (Pred n)))! Let F stand for the closed term:! - λf. λn. cond (iszero n) 1 (Mult n (f (Pred n)))! - Notice F(fact) = fact.! - fact is a fixed point of F! - To find fact, need only find fixed point of F!! Easy w/ g(x) = x * x, but F???? Several fixed point operators:! - Ex: Y = λf. (λx. f (xx))(λx. f (xx))! Claim for all g, Y g = g (Y g)! Y g = (λf. (λx. f (xx))(λx. f (xx))) g! = (λx. g(xx))(λx. g(xx))! = g((λx. g(xx)) (λx. g(xx)))! = g (Y g)! If let x 0 = Y g, then g (x 0 ) = x 0. Invented by Haskell Curry
Factorial Computing Factorials Recursive definition:! - let F = λf. λn. cond (iszero n) 1 (Mult n (f (Pred n)))! - let fact = Y F! - then F(fact) = fact because Y always gives fixed points! Compute:! fact 0 = (F (fact)) 0 because fact is a fixed point of F! = cond (iszero 0) 1 (Mult 0 (fact (Pred 0)))! fact 1 = (F (fact)) 1 because fact is a fixed point of F! = (λn. cond (iszero n) 1 (Mult n (fact (Pred n)))) 1 expanding F! = cond (iszero 1) 1 (Mult 1 (fact (Pred 1))) applying it" = Mult 1 (fact (Pred 1)) by the definition of cond! = fact 0 by the definition of Mult and Pred! = 1 by the above calculation! = 1 by the definition of cond Lambda Calculus λ-calculus invented in 1928 by Church in Princeton & first published in 1932.! Goal to provide a foundation for logic! First to state explicit conversion rules.! Original version inconsistent, but corrected! - If this sentence is true then 1 = 2 problematic!!! 1933, definition of natural numbers Collaborators 1931-1934: Grad students:! - J. Barkley Rosser and Stephen Kleene! - Church-Rosser confluence theorem ensured consistency (earlier version inconsistent)! - Kleene showed λ-definable functions very rich! Equivalent to Herbrand-Gödel recursive functions! Equivalent to Turing-computable functions.! Founder of recursion theory, invented regular expressions! Church s thesis:! - λ-definability effectively computable O
Undecidability Alan Turing Turing! Convertibility problem for λ-calculus undecidable.! Validity in first-order predicate logic undecidable.! Proved independently year later by Turing.! - First showed halting problem undecidable - 1936, in Cambridge, England, definition of Turing machine! - 1936-38, in Princeton to get Ph.D. under Church.! - 1937, first published fixed point combinator! (λx. λy. (y (x x y))) (λx. λy. (y (x x y)))! - Kleene did not use fixed-point operator in defining functions on natural numbers!" - Broke German enigma code in WW2, Turing test AI! - Persecuted as homosexual, committed suicide in 1954 Combinatory Logic Schönfinkel - get rid of bound variables! Combinators K, S sufficient to write all computable functions:! - K x y = x! - S x y z = (x y)(x z)! Typed Lambda Calculus CL terms include K, S, variables and all terms created by function application.! All computable fcns, equivalent to λ-calculus
Types Definitions Start with base type e and build up types and terms:! - Type ::= e Type Type! - M ::= v (M M) λv : Type. M! Examples:! - Types: e, e e, e e e, (e e) e,...! - Terms: λx: e. x, λf: e e. λz: e. f(f(z)) Earlier definitions generalize over types t:! - true t = λx:t. λy:u. x! - n t = λs: t t. λz: t. s (n) (z)! Some untyped terms can t be typed:! - Ω = (λx. (x x))(λx. (x x))! - Y = λf. (λx. f (x x))(λx. f (x x)) Totally Awesome!! Theorem: If M is a term of the typed lambda calculus, then M has a unique normal form. I.e., every term of the typed lambda calculus is total.! Functional Languages Corollary: The typed lambda calculus does not include all computable functions.
Commands/Statements Imperative Languages Characteristics! - Support for variables! represent memory locations for storing updatable values.! - Assignment operation! progress in computation depends on changes in values stored in variables.! - Repetition! flow of control guided by conditional and looping statements controlling order in which assignment statements are executed. Organized around notion of commands! Meaning of a statement is operation which, based on current contents of memory, and explicit values supplied to it, modifies the current contents of memory.! How are results of one command communicated to next? Problems Expressions... return a value, depending on the state of the computation.! Too low level.! Architecture dependent.! Does not generalize well to concurrent computation. Examples:! - Literals: 3, true, hello, 42.56! - Aggregates: arrays, records, sets, lists, etc. E.g. [1,3,5]! - Function calls: f(a,b), a + b * (c - d), (if x > 0 then sin else cos)(... )! - Conditional expressions: if x <> 0 then a/x else 1, case (historically only in functional languages)! - Named constants and variables: pi, x
Declarative Language Test Problems Within the scope of specific declarations of x 1,, x n, all occurrences of an expression e containing only variables x 1,, x n have the same value.! - Pure expressions are well behaved.! Fails w/imperative languages:! - x := x + y; y := 2 * x;! - Two occurrences of x have different values.! If DLT holds, then allows for optimizations Order of evaluation! - if (i >= 0 && a[i]!= 100)...! - What happens if i = -1?" - Is && commutative?" Side effects! - Destroy nice properties! - a[++x] = (y = x+1) + y + (++x)! - Does 2*(x++) = (x++) + (x++)? History of Functional Languages Functional Languages LISP 1957! Backus Turing award lecture (1977) - FP / FL! ML (1977) - Turing award for Robin Milner! - Meta language for theorem prover! SASL, KRC, and Miranda (Turner), Haskell (1990).! - Lazy languages! - Implemented using combinatory logic Pure LISP/Scheme (and Rex) are essentially sugared versions of untyped lambda calculus with constants.! ML & Haskell are typed lambda calculus w/ type inference and recursion definitions at each type.