Today 1. What is the Lambda Calculus? 2. Its Syntax and Semantics Type Systems 3. Church Booleans and Church Numerals 4. Lazy vs. Eager Evaluation (call-by-name vs. call-by-value) 5. Recursion Lecture 2 Oct. 27th, 2004 Sebastian Maneth 6. Nameless Implementation: deBruijn Indices http://lampwww.epfl.ch/teaching/typeSystems/2004 1. What is the Lambda Calculus 1. What is the Lambda Calculus introduced in late 1930’s by Alonzo Church and Stephen Kleene introduced in late 1930’s by Alonzo Church and Stephen Kleene can compute the same as Turing Machines, which is everything we can (intuitively) compute (Church-Turing Thesis). used in 1936 by Church to prove the undecidability of the Entscheidungsproblem is a formal system designed to investigate is a formal system designed to investigate • function definition • function definition • function application • function application • recursion • recursion 1. What is the Lambda Calculus 2. Syntax of the Lambda Calculus Let V be a countable set of variable names. what do we want? The set of lambda terms (over V) is the smallest set T such that � a small core language, into which other language constructs can be translated. 1. if x ∈ V, then x ∈ T variable 2. if x ∈ V and t 1 ∈ T, then λ x. t 1 ∈ T abstraction There are many such languages: Turing Machines 3. if t 1 , t 2 ∈ T, then t 1 t 2 ∈ T application µ− Recursive Functions Chomsky’s Type-0 Grammars Cellular Automata etc. Function abstraction: instead of f(x) = x + 5 � why do we pick out the Lambda Calulus? write f = λ x.x + 5 because types are about values of program variables. a lambda term (i.e., ∈ T) representing a nameless function , which adds 5 to its parameter 1
2. Syntax of the Lambda Calculus 2. Syntax of the Lambda Calculus λ x λ y Example: Function application: instead of f(x) λ x. λ y. λ z. x z (y z) write f x λ z = λ x.( λ y. λ z. x z (y z)) Example: ( λ x. x + 5) a apply apply = λ x.( λ y.( λ z. (x z (y z)) Abstract Syntax Tree λ x a apply apply = λ x. ( λ y. ( λ z. ((x z) (y z)))) (AST) x z y z x + 5 “surface syntax” “surface syntax” “abstract syntax” “abstract syntax” Conventions (to save parenthesis) Conventions (to save parenthesis) application is left associative: x y z = (x y) z ≠ x (y z) application is left associative: x y z = (x y) z ≠ x (y z) scope of abstraction extends as far to the right as possible : scope of abstraction extends as far to the right as possible : λ x. x y = λ x. (x y) ≠ ( λ x. x) y λ x. x y = λ x. (x y) ≠ ( λ x. x) y 2. Semantics of the Lambda Calculus 2. Semantics of the Lambda Calculus Example: ( λ x.x + 5) a SUBSTITUTE a for x in x + 5 ( λ x. λ y. f (y x) ) 5 ( λ x. x ) redex (REDucible EXpression): ( λ x. t ) t 1 apply λ x a can be reduced to (evaluates to): x + 5 β− reduction [ x � a ] x + 5 = a + 5 To compute in Lambda Calculus, ALL you do is SUBSTITUTE!! 2. Semantics of the Lambda Calculus 2. Semantics of the Lambda Calculus Example: Example: ( λ x. λ y. f (y x) ) 5 ( λ x. x ) ( λ x. λ y. f (y x) ) 5 ( λ x. x ) = ( ( λ x. λ y. f (y x) ) 5 ) ( λ x. x ) = ( ( λ x. λ y. f (y x) ) 5 ) ( λ x. x ) because App binds to the left! because App binds to the left! β− red. [ x � 5 ]( λ y. f (y x) ) ( λ x. x ) = ( λ y. f (y 5) ) ( λ x. x ) 2
2. Semantics of the Lambda Calculus 2. Semantics of the Lambda Calculus Example: Example: ( λ x. λ y. f (y x) ) 5 ( λ x. x ) ( λ x. λ y. f (y x) ) 5 ( λ x. x ) = ( ( λ x. λ y. f (y x) ) 5 ) ( λ x. x ) = ( ( λ x. λ y. f (y x) ) 5 ) ( λ x. x ) because App binds to the left! because App binds to the left! β− red. β− red. [ x � 5 ]( λ y. f (y x) ) ( λ x. x ) [ x � 5 ]( λ y. f (y x) ) ( λ x. x ) = ( λ y. f (y 5) ) ( λ x. x ) = ( λ y. f (y 5) ) ( λ x. x ) β− red. β− red. [ y � λ x. x ]( f (y 5) ) [ y � λ x. x ]( f (y 5) ) = f ( λ x. x 5) = f ( λ x. x 5) β− red. f 5 (normal form = cannot be reduced further) 2. Semantics of the Lambda Calculus 2. Semantics of the Lambda Calculus Example: Does every λ -term have a normal form? Example: Does every λ -term have a normal form? � NO!!! � NO!!! ( λ x. x x ) ( λ x. x x ) ( λ x. x x ) ( λ x. x x ) β− red. β− red. [ x � ( λ x. x x ) ] ( x x ) [ x � ( λ x. x x ) ] ( x x ) = ( λ x. x x ) ( λ x. x x ) 2. Semantics of the Lambda Calculus 2. Semantics of the Lambda Calculus Example: Does every λ -term have a normal form? Example: Does every λ -term have a normal form? � NO!!! � NO!!! ( λ x. x x ) ( λ x. x x ) ( λ x. x x ) ( λ x. x x ) is called the omega combinator β− red. [ x � ( λ x. x x ) ] ( x x ) =: omega = ( λ x. x x ) ( λ x. x x ) combinator = closed lambda term β− red. ( λ x. x x ) ( λ x. x x ) = lambda term with no free variables β− red. ( λ x. x x ) ( λ x. x x ) The simplest combinator, identity: id := λ x. x … 3
2. Semantics of the Lambda Calculus 3. Church Booleans and Numerals Free vs. Bound Variables: How to encode BOOLEANS into the lambda calculus? bound free tru � takes two arguments, selects the FIRST λ x. x y = λ x. (x y) fls � takes two arguments, selects the SECOND scope of x x is bound in its scope THEN: if-then-else can be defined as: test x u w = “apply x to u w ” = ( λ k. λ m. λ n. k m n) x u w Define the set of free variables of a term t, FV(t), as =: test if t = x ∈ V, then FV(t) = { x } tru := λ m. λ n. m fls := λ m. λ n. n if t = λ x. t 1 , then FV(t) = FV(t 1 ) \ { x } if t = t 1 t 2 , then FV(t) = FV(t 1 ) ∪ FV(t 2 ) β− red. β− red. … u test tru u w 3. Church Booleans and Numerals 3. Church Booleans and Numerals How to encode BOOLEANS into the lambda calculus? How to encode BOOLEANS into the lambda calculus? tru � takes two arguments, selects the FIRST tru � takes two arguments, selects the FIRST fls � takes two arguments, selects the SECOND fls � takes two arguments, selects the SECOND := λ m. λ n. m := λ m. λ n. m tru tru := λ m. λ n. n := λ m. λ n. n fls fls := λ k. λ m. λ n. k m n := λ k. λ m. λ n. k m n test test How to do “ and ” on these BOOLEANS? How to do “ and ” on these BOOLEANS? = “apply u to w fls ” = “apply u to w fls ” and u w and u w := ( λ m. λ n. m n fls ) u w := ( λ m. λ n. m n fls ) u w =: and =: and � Define the or and not functions! 3. Church Booleans and Numerals 3. Church Booleans and Numerals How to encode NUMBERS into the lambda calculus? How to encode NUMBERS into the lambda calculus? c0 := λ s. λ z. z c0 := λ s. λ z. z c1 := λ s. λ z. s z c1 := λ s. λ z. s z scc := λ n. λ s. λ z. s (n s z) c2 := λ s. λ z. s (s z) c2 := λ s. λ z. s (s z) c3 := λ s. λ z. s (s (s z)) c3 := λ s. λ z. s (s (s z)) etc. How to do “ plus ” and “ times ” on these Church Numerals? THEN, the successor function can be defined as := λ m. λ n. λ s. λ z. m s (n s z) scc := λ n. λ s. λ z. s (n s z) plus “apply m times the successor to n” β− red. β− red. λ s. λ z. s ( c0 s z) λ s. λ z. s z = c1 scc c0 just like fls ! Select the second argument. 4
3. Church Booleans and Numerals 3. Church Booleans and Numerals How to encode NUMBERS into the lambda calculus? How to encode NUMBERS into the lambda calculus? c0 := λ s. λ z. z c0 := λ s. λ z. z scc := λ n. λ s. λ z. s (n s z) c1 := λ s. λ z. s z scc := λ n. λ s. λ z. s (n s z) c1 := λ s. λ z. s z := λ m. λ n. λ s. λ z. m s (n s z) plus c2 := λ s. λ z. s (s z) c2 := λ s. λ z. s (s z) c3 := λ s. λ z. s (s (s z)) c3 := λ s. λ z. s (s (s z)) How to do “ plus ” and “ times ” on these Church Numerals? Questions: := λ m. λ n. λ s. λ z. m s (n s z) plus 1. Write a function subt for subtraction on Church Numerals. “apply m times the successor to n” 2. How can other datatypes be encoded into the lambda calculus, := λ m. λ n. m ( plus n) c0 like, e.g., lists, trees, arrays, and variant records? times “apply m times ( plus n) to c0 ” 4. Lazy vs. Eager Evaluation 4. Lazy vs. Eager Evaluation What does this lambda term evaluate to?? What does this lambda term evaluate to?? tru id omega tru id omega ( λ m. λ n. m) ( λ x. x) (( λ x. x x) ( λ x. x x)) � where to start evaluating? which redex?? apply apply apply λ m λ x λ x id λ n apply apply m x x x x 4. Lazy vs. Eager Evaluation 4. Lazy vs. Eager Evaluation What does this lambda term evaluate to?? What does this lambda term evaluate to?? tru id omega tru id omega ( λ m. λ n. m) ( λ x. x) (( λ x. x x) ( λ x. x x)) ( λ m. λ n. m) ( λ x. x) (( λ x. x x) ( λ x. x x)) � where to start evaluating? which redex?? � where to start evaluating? which redex?? apply apply redex1 redex1 redex2 redex2 apply apply apply apply λ m λ x λ x λ m λ x λ x id id λ n apply apply λ n apply apply � if we always reduce redex2 m x x x x m x x x x then this lambda term has NO semantics. 5
Recommend
More recommend