type systems
play

Type Systems Lecture 2 Oct. 27th, 2004 Sebastian Maneth - PowerPoint PPT Presentation

Type Systems Lecture 2 Oct. 27th, 2004 Sebastian Maneth http://lampwww.epfl.ch/teaching/typeSystems/2004 Today 1. What is the Lambda Calculus? 2. Its Syntax and Semantics 3. Church Booleans and Church Numerals 4. Lazy vs. Eager


  1. Type Systems Lecture 2 Oct. 27th, 2004 Sebastian Maneth http://lampwww.epfl.ch/teaching/typeSystems/2004

  2. Today 1. What is the Lambda Calculus? 2. Its Syntax and Semantics 3. Church Booleans and Church Numerals 4. Lazy vs. Eager Evaluation (call-by-name vs. call-by-value) 5. Recursion 6. Nameless Implementation: deBruijn Indices

  3. 1. What is the Lambda Calculus introduced in late 1930’s by Alonzo Church and Stephen Kleene used in 1936 by Church to prove the undecidability of the Entscheidungsproblem is a formal system designed to investigate • function definition • function application • recursion

  4. 1. What is the Lambda Calculus 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). is a formal system designed to investigate • function definition • function application • recursion

  5. 1. What is the Lambda Calculus what do we want? � a small core language, into which other language constructs can be translated. There are many such languages: Turing Machines µ− Recursive Functions Chomsky’s Type-0 Grammars Cellular Automata etc. � why do we pick out the Lambda Calulus? because types are about values of program variables.

  6. 2. Syntax of the Lambda Calculus Let V be a countable set of variable names. The set of lambda terms (over V) is the smallest set T such that 1. if x ∈ V, then x ∈ T variable 2. if x ∈ V and t 1 ∈ T, then λ x. t 1 ∈ T abstraction 3. if t 1 , t 2 ∈ T, then t 1 t 2 ∈ T application Function abstraction: instead of f(x) = x + 5 write f = λ x.x + 5 a lambda term (i.e., ∈ T) representing a nameless function , which adds 5 to its parameter

  7. 2. Syntax of the Lambda Calculus Function application: instead of f(x) write f x Example: ( λ x. x + 5) a apply Abstract Syntax Tree λ x a (AST) x + 5 “surface syntax” “abstract syntax” Conventions (to save parenthesis) application is left associative: x y z = (x y) z ≠ x (y z) scope of abstraction extends as far to the right as possible : λ x. x y = λ x. (x y) ≠ ( λ x. x) y

  8. 2. Syntax of the Lambda Calculus λ x λ y Example: λ x. λ y. λ z. x z (y z) λ z = λ x.( λ y. λ z. x z (y z)) apply = λ x.( λ y.( λ z. (x z (y z)) apply apply = λ x. ( λ y. ( λ z. ((x z) (y z)))) z z x y “surface syntax” “abstract syntax” Conventions (to save parenthesis) application is left associative: x y z = (x y) z ≠ x (y z) scope of abstraction extends as far to the right as possible : λ x. x y = λ x. (x y) ≠ ( λ x. x) y

  9. 2. Semantics of the Lambda Calculus ( λ x.x + 5) a SUBSTITUTE a for x in x + 5 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!!

  10. 2. Semantics of the Lambda Calculus Example: ( λ x. λ y. f (y x) ) 5 ( λ x. x )

  11. 2. Semantics of the Lambda Calculus Example: ( λ x. λ y. f (y x) ) 5 ( λ x. x ) = ( ( λ x. λ y. f (y x) ) 5 ) ( λ x. x ) because App binds to the left!

  12. 2. Semantics of the Lambda Calculus Example: ( λ x. λ y. f (y x) ) 5 ( λ x. x ) = ( ( λ x. λ y. f (y x) ) 5 ) ( λ x. x ) because App binds to the left! β− red. [ x � 5 ]( λ y. f (y x) ) ( λ x. x ) = ( λ y. f (y 5) ) ( λ x. x )

  13. 2. Semantics of the Lambda Calculus Example: ( λ x. λ y. f (y x) ) 5 ( λ x. x ) = ( ( λ x. λ y. f (y x) ) 5 ) ( λ x. x ) because App binds to the left! β− red. [ x � 5 ]( λ y. f (y x) ) ( λ x. x ) = ( λ y. f (y 5) ) ( λ x. x ) β− red. [ y � λ x. x ]( f (y 5) ) = f ( λ x. x 5)

  14. 2. Semantics of the Lambda Calculus Example: ( λ x. λ y. f (y x) ) 5 ( λ x. x ) = ( ( λ x. λ y. f (y x) ) 5 ) ( λ x. x ) because App binds to the left! β− red. [ x � 5 ]( λ y. f (y x) ) ( λ x. x ) = ( λ y. f (y 5) ) ( λ x. x ) β− red. [ y � λ x. x ]( f (y 5) ) = f ( λ x. x 5) β− red. f 5 (normal form = cannot be reduced further)

  15. 2. Semantics of the Lambda Calculus Example: Does every λ -term have a normal form? � NO!!! ( λ x. x x ) ( λ x. x x ) β− red. [ x � ( λ x. x x ) ] ( x x )

  16. 2. Semantics of the Lambda Calculus Example: Does every λ -term have a normal form? � NO!!! ( λ x. x x ) ( λ x. x x ) β− red. [ x � ( λ x. x x ) ] ( x x ) = ( λ x. x x ) ( λ x. x x )

  17. 2. Semantics of the Lambda Calculus Example: Does every λ -term have a normal form? � NO!!! ( λ x. x x ) ( λ x. x x ) β− red. [ x � ( λ x. x x ) ] ( x x ) = ( λ x. x x ) ( λ x. x x ) β− red. ( λ x. x x ) ( λ x. x x ) β− red. ( λ x. x x ) ( λ x. x x ) …

  18. 2. Semantics of the Lambda Calculus Example: Does every λ -term have a normal form? � NO!!! ( λ x. x x ) ( λ x. x x ) is called the omega combinator =: omega combinator = closed lambda term = lambda term with no free variables The simplest combinator, identity: id := λ x. x

  19. 2. Semantics of the Lambda Calculus Free vs. Bound Variables: bound free λ x. x y = λ x. (x y) scope of x x is bound in its scope Define the set of free variables of a term t, FV(t), as if t = x ∈ V, then FV(t) = { x } 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 )

  20. 3. Church Booleans and Numerals How to encode BOOLEANS into the lambda calculus? tru � takes two arguments, selects the FIRST fls � takes two arguments, selects the SECOND THEN: if-then-else can be defined as: = “apply x to u w ” test x u w = ( λ k. λ m. λ n. k m n) x u w =: test tru := λ m. λ n. m fls := λ m. λ n. n β− red. β− red. … u test tru u w

  21. 3. Church Booleans and Numerals How to encode BOOLEANS into the lambda calculus? tru � takes two arguments, selects the FIRST fls � takes two arguments, selects the SECOND := λ m. λ n. m tru := λ m. λ n. n fls := λ k. λ m. λ n. k m n test How to do “ and ” on these BOOLEANS? = “apply u to w fls ” and u w := ( λ m. λ n. m n fls ) u w =: and

  22. 3. Church Booleans and Numerals How to encode BOOLEANS into the lambda calculus? tru � takes two arguments, selects the FIRST fls � takes two arguments, selects the SECOND := λ m. λ n. m tru := λ m. λ n. n fls := λ k. λ m. λ n. k m n test How to do “ and ” on these BOOLEANS? = “apply u to w fls ” and u w := ( λ m. λ n. m n fls ) u w =: and � Define the or and not functions!

  23. 3. Church Booleans and Numerals How to encode NUMBERS into the lambda calculus? c0 := λ s. λ z. z c1 := λ s. λ z. s z c2 := λ s. λ z. s (s z) c3 := λ s. λ z. s (s (s z)) etc. THEN, the successor function can be defined as scc := λ n. λ s. λ z. s (n s z) β− red. β− red. λ s. λ z. s ( c0 s z) λ s. λ z. s z = c1 scc c0 just like fls ! Select the second argument.

  24. 3. Church Booleans and Numerals How to encode NUMBERS into the lambda calculus? c0 := λ s. λ z. z c1 := λ s. λ z. s z scc := λ n. λ s. λ z. s (n s z) c2 := λ s. λ z. s (s z) c3 := λ s. λ z. s (s (s z)) How to do “ plus ” and “ times ” on these Church Numerals? := λ m. λ n. λ s. λ z. m s (n s z) plus “apply m times the successor to n”

  25. 3. Church Booleans and Numerals How to encode NUMBERS into the lambda calculus? c0 := λ s. λ z. z c1 := λ s. λ z. s z scc := λ n. λ s. λ z. s (n s z) c2 := λ s. λ z. s (s z) c3 := λ s. λ z. s (s (s z)) How to do “ plus ” and “ times ” on these Church Numerals? := λ m. λ n. λ s. λ z. m s (n s z) plus “apply m times the successor to n” := λ m. λ n. m ( plus n) c0 times “apply m times ( plus n) to c0 ”

  26. 3. Church Booleans and Numerals How to encode NUMBERS into the lambda calculus? c0 := λ s. λ z. 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) c3 := λ s. λ z. s (s (s z)) Questions: 1. Write a function subt for subtraction on Church Numerals. 2. How can other datatypes be encoded into the lambda calculus, like, e.g., lists, trees, arrays, and variant records?

  27. 4. Lazy vs. Eager Evaluation What does this lambda term evaluate to?? tru id omega

  28. 4. Lazy vs. Eager Evaluation What does this lambda term evaluate to?? 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

  29. 4. Lazy vs. Eager Evaluation What does this lambda term evaluate to?? tru id omega ( λ m. λ n. m) ( λ x. x) (( λ x. x x) ( λ x. x x)) � where to start evaluating? which redex?? apply redex1 redex2 apply apply λ m λ x λ x id λ n apply apply m x x x x

  30. 4. Lazy vs. Eager Evaluation What does this lambda term evaluate to?? tru id omega ( λ m. λ n. m) ( λ x. x) (( λ x. x x) ( λ x. x x)) � where to start evaluating? which redex?? apply redex1 redex2 apply apply λ m λ x λ x id λ n apply apply � if we always reduce redex2 m x x x x then this lambda term has NO semantics.

  31. 4. Lazy vs. Eager Evaluation A redex if outermost, if in the AST it has no ancestor that is a redex. outermost redexes ap ap ap λ λ λ

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend