t he u ntyped c alculus s yntax
play

T HE U NTYPED -C ALCULUS (S YNTAX ) Syntax is very simple; only - PowerPoint PPT Presentation

A N INTRODUCTION TO THE A N INTRODUCTION TO THE - CALCULUS AND TYPE THEORY CS3234 CS3234 Aquinas Hobor and Martin Henz F IRST Q UESTION What is a ? Wh i ? 2 F IRST Q UESTION What is a ? Wh i ?


  1. A N INTRODUCTION TO THE A N INTRODUCTION TO THE λ - CALCULUS AND TYPE THEORY CS3234 CS3234 Aquinas Hobor and Martin Henz

  2. F IRST Q UESTION  What is a “ λ ”? Wh i “ ”? 2

  3. F IRST Q UESTION  What is a “ λ ”? Wh i “ ”?  Greek letter “lambda”. Pronounced like “L”  Lower case: λ  Upper case: Λ 3

  4. S ECOND QUESTION  What is the λ -calculus about? Wh i h l l b ? 4

  5. S ECOND QUESTION  What is the λ -calculus about? Wh i h l l b ?  It is a method for writing and reasoning about functions .  Functions, of course, are central to both mathematics and computer science. th ti d t i  The ideas were developed by Alonzo Church in Th id d l d b Al Ch h i the 1930s during investigations into the foundations of mathematics foundations of mathematics. 5 5

  6. T HIRD QUESTION  What is type theory? 6

  7. T HIRD QUESTION  What is type theory?  A method of classifying computations according to the types (kinds, sorts) of values produced.  You are already familiar with the basics:  int myint; i i  Object myobject;  Analogous to units in scientific computation. 7

  8. T OPICS C OVERED T ODAY  Untyped lambda calculus  Simply-typed lambda calculus  Polymorphic lambda calculus (System F) 8

  9. T HE U NTYPED λ -C ALCULUS (S YNTAX )  Syntax is very simple; only three kinds of terms: S t i i l l th ki d f t e = = x, y, z, … (V (Variables) i bl ) λ x. e (Functions) e 1 e 2 e e (Application) (Application) Examples: Examples:  x  λ x. x  z y  ( λ x. x) ( λ y. y) ( ) ( y y) 9 9

  10. T HE U NTYPED λ -C ALCULUS (S EMANTICS )  Semantics is also very simple – only one rule! ( λ x. e 1 ) e 2 [x → e 2 ] e 1  We substitute the term e 2 for x in the term e 1 . Examples:  ( λ x. x) ( λ y. y) ( λ y. y)   ( λ x. x x) ( λ y. y) ( λ y. y) ( λ y. y)  ( λ y. y)  10 10

  11. MORE EXAMPLES ( RENAMING )  ( λ x. λ y. x y) ( λ y. y y) z ( ) ( )  ( λ y. ( λ y. y y) y) z  ( λ y. y y) z  z z First question: what is the difference between ( λ y. y) and ( λ x. x)? Convention: these are identical : lambda terms are equal to any “uniform” renaming. 11 11

  12. MORE EXAMPLES ( RENAMING ) Our convention means we can rename as we like:  ( λ x. λ y. x y) ( λ y. y) z  ( λ y. ( λ a. a) y) z  ( λ a. a) z  z This process helps avoid variable-capture, etc. 12 12

  13. MORE EXAMPLES ( EVALUATION ORDER ) Consider this example: C id thi l ( ( λ x. x) ( λ y. y) ) ( ( λ a. a) ( λ b. b) ) There are multiple ways that it could evaluate: ( λ y. y) (( λ a. a) ( λ b. b)) 1. (( λ a. a) ( λ b. b)) 1. λ b. b b b  ( λ y. y) ( λ b. b) 2. λ b. b λ b. b   (( λ x. x) ( λ y. y)) ( λ b. b) 2. ( λ y. y) ( λ b. b) 1. λ b b λ b. b 13 13 

  14. O BSERVATION  There are lots!  But they lead to the same place – do we care?  For now, leave this question aside. We will choose to follow the convention used in programming languages: to evaluate e 1 e 2 , i l t l t Evaluate e 1 first until it reaches a λ -term E l t fi t til it h λ t 1. Evaluate e 2 as far as you can 2. 14 14 D h Do the substitution b i i 3.

  15. S EE THIS BEHAVIOR IN C Suppose that f is a function pointer to a function S th t f i f ti i t t f ti that takes a single integer argument. How does this line behave? (*f) (3 + x); Dereference f 1. Add 3 + x 2. C ll th f Call the function with the result ti ith th lt 3. This is called call-by-value This is called, call-by-value 15 15

  16. C ALL -B Y -V ALUE , F ORMALLY e 1  e 1 ’ e 1 e 2  e 1 ’ e 2 e 2  e 2 ’ ( λ x. e 1 ’) e 2  ( λ x. e 1 ’) e 2 ’ ( λ x. e 1 ’) v  [x → v] e 1 ’ 16 16

  17. P ROGRAMMING IN THE LAMBDA CALCULUS  Since what we are really describing is a kind of Si h t ll d ibi i ki d f computation, there is a natural question: how can we encode the standard ideas in programming? we e co e e s a a eas p og a g?  For example, if-then-else? p ,  Definitions:  fls λ x. λ y. y ≡  tru λ x. λ y. x ≡  if  if λ b. λ t. λ e. b t e λ b λ t λ e b t e ≡ ≡ (convention: “a b c” is “(a b) c”) (convention: a b c is (a b) c ) 17 17

  18. I F -T HEN -E LSE  Definitions: D fi iti  fls λ x. λ y. y ≡  tru tru λ x λ y x λ x. λ y. x ≡ ≡  if λ b. λ t. λ e. b t e ≡ if tru a b = ( λ b. λ t. λ e. b t e) ( λ x. λ y. x) a b  ( λ t. λ e. ( λ x. λ y. x) t e) a b  ( λ e. ( λ x. λ y. x) a e) b  ( λ x. λ y. x) a b  ( λ y. a) b  18 18 a

  19. I F -T HEN -E LSE  Definitions: D fi iti  fls λ x. λ y. y ≡  tru tru λ x λ y x λ x. λ y. x ≡ ≡  if λ b. λ t. λ e. b t e ≡ if fls a b = ( λ b. λ t. λ e. b t e) ( λ x. λ y. y) a b  ( λ t. λ e. ( λ x. λ y. y) t e) a b  ( λ e. ( λ x. λ y. y) a e) b  ( λ x. λ y. y) a b  ( λ y. y) b  19 19 b b

  20. W HAT ABOUT N UMBERS ?  Definitions:  zero λ x. λ y. y ≡  one λ x. λ y. x y λ ≡ λ  two λ x. λ y. x (x y) ≡  three  three λ x. λ y. x (x (x y)) λ x λ y x (x (x y)) ≡ ≡  succ λ n. λ x. λ y. x (n x y) y ( y) ≡  Notice: “zero” and “fls” are the same!  This is common in computer science…  This is common in computer science… 20 20

  21. C ALCULATING WITH NUMBERS  Definitions:  zero λ x. λ y. y ≡  one λ x. λ y. x y λ ≡ λ  succ λ n. λ x. λ y. x (n x y) ≡ succ zero = ( λ n λ x λ y x (n x y)) ( λ x λ y y) ( λ n. λ x. λ y. x (n x y)) ( λ x. λ y. y)   λ x. λ y. x (( λ x. λ y. y) x y) Is this the same as “one”? 21 21

  22. E VALUATION O RDER , R EVISTED succ zero = λ x. λ y. x (( λ x. λ y. y) x y) (( ) ) one = λ x. λ y. x y Obviously these are not identical… but look at what happens when we apply both to the arguments “a” and “b”… 22 22

  23. E VALUATION O RDER , R EVISITED succ zero = λ x. λ y. x (( λ x. λ y. y) x y) λ (( λ λ ) ) λ one = λ x. λ y. x y ( λ x. λ y. x (( λ x. λ y. y) x y)) a b  ( λ y. a (( λ x. λ y. y) a y)) b ( λ y a (( λ x λ y y) a y)) b   a (( λ x. λ y. y) a b)  a (( λ y y) b) a (( λ y. y) b)   a b ( λ x. λ y. x y) a b  * a b 23 23

  24. E VALUATION O RDER , R EVISITED  So while the functions are not the same, they are similar: they will reach the same final result  Maybe a more familiar example of this kind of difference: both quicksort and mergesort produce difference: both quicksort and mergesort produce the same result when applied to the same input – but they are not the same function (different but they are not the same function (different running time!)  An interesting question: does the evaluation order only effect the running time? 24 24

  25. M ORE NUMBERS …  Definitions:  zero λ x. λ y. y ≡  one λ x. λ y. x y λ ≡ λ  two λ x. λ y. x (x y) ≡  three  three λ x. λ y. x (x (x y)) λ x λ y x (x (x y)) ≡ ≡  succ λ n. λ x. λ y. x (n x y) y ( y) ≡  plus λ n. λ m. λ x. λ y. m x (n x y) ≡  mult λ n. λ m. λ x. λ y. n (m x) y ≡  … 25 25  Possible to define subtraction, etc. etc.

  26. O THER COMPUTATION FEATURES …  One feature you may have noticed: λ -terms are O f t h ti d λ t anonymous. That is, the functions are unnamed.  λ x. x  int foo(int x) { return x; }  Here, the function has a name ( foo ).  There are other differences, too – like the types.  We will discuss these later  Observation: the lambda calculus is concise  Observation: the lambda calculus is concise . 26 26

  27. N AMED VS . UNNAMED  What do functions really need names for?  For a function like foo , not much.  But maybe another function wants to call it…  … still, that issue can be worked around.  More serious: what if you want a recursive function? Then you need a way to call yourself. 27 27

  28. N ONTERMINATING C OMPUTATION  Can we express nonterminating computation? C t i ti t ti ?  Y  Yes! !  Consider the term: diverge ≡ ( λ x x x) ( λ x x x)  Consider the term: diverge ≡ ( λ x. x x) ( λ x. x x) ( λ x. x x) ( λ x. x x) ( λ x x x) ( λ x x x)   [x → ( λ x. x x)] x x = ( λ x. x x) ( λ x. x x) ( ) ( )  ( λ x. x x) ( λ x. x x)  … 28 28

  29. N ONTERMINATING C OMPUTATION  What if we want a more general form of nontermating computation?  We can define a term fix fi … ( ill h (will show later) l t ) ≡ Now let’s suppose we want to define the factorial function, written in pseudo-form like this: fact ≡ λ x. if (iszero x) 1 (mult x (fact (pred x))) 29 29

  30. N ONTERMINATING C OMPUTATION f fact ≡ λ x. if (iszero x) 1 (mult x (fact (pred x))) t if (i ) 1 ( lt (f t ( d ))) λ The problem with this definition is that in Th bl ith thi d fi iti i th t i mathematics we are not allowed to write circular definitions definitions… We could write one iteration as follows: We could write one iteration as follows: λ f. λ x. if (iszero x) 1 (mult x (f (pred x))) ( ) ( ( (p ))) Now “f” is being used as the recursive call. g 30 30

Recommend


More recommend