administration
play

Administration HW1 due September 11 in class CS 611 modify only - PDF document

Administration HW1 due September 11 in class CS 611 modify only interpretation.sml Advanced Programming Languages New TA: James Cheney (jcheney@cs) Andrew Myers Cornell University Lecture 7: Lambda calculus 8 Sep 00 CS 611


  1. Administration • HW1 due September 11 in class CS 611 – modify only interpretation.sml Advanced Programming Languages • New TA: James Cheney (jcheney@cs) Andrew Myers Cornell University Lecture 7: Lambda calculus 8 Sep 00 CS 611 Lecture 7 – Andrew Myers, Cornell University 2 Untyped Lambda Calculus Open vs. closed terms • IMP: no functions • term = expression denoting a value • Lambda calculus: all functions • Closed term: all identifiers bound by e ::= x | e 0 e 1 | λ x e 0 closest containing abstraction x Identifier. refers to variable defined by surrounding context. e 0 e 1 Application. Applies the function e 0 to the ( λ x … x ( λ y … y … )…) argument e 1 • Open term: some identifier(s) not λ x e 0 Abstraction/lambda term. Defines a new bound: ( λ x (y x)) function with argument variable x and body e 0 (ala ML’s fn x => e 0 ) • Legal lambda calculus programs: all closed terms • Universal, simple, core language (but not Lisp/Scheme ) CS 611 Lecture 7 – Andrew Myers, Cornell University 3 CS 611 Lecture 7 – Andrew Myers, Cornell University 4 Evaluation Higher-order functions • Application is evaluated by β -reduction: • Can express functions that return (or (( λ x e 1 ) e 2 ) → e 1 { e 2 / x } accept) other functions easily (all values are only functions) e 1 { e 2 / x } means “ e 1 with e 2 substituted for • A function that applies another function occurrences of x ” to 5: ( λ f (f 5)) ( note: defining “substituted” is tricky) • A function that returns a function that (( λ x x) e ) → x { e / x } = e applies another function to its (( λ x ( λ x x)) e ) → ( λ x x) { e / x } = ( λ x x) argument: ((( λ x ( λ y ( y x ))) 3) INC) → ( λ v ( λ f (f v))) (( λ y ( y 3)) INC) → (INC 3) → 4 CS 611 Lecture 7 – Andrew Myers, Cornell University 5 CS 611 Lecture 7 – Andrew Myers, Cornell University 6 1

  2. Example Simulating multiple arguments • Don’t we need multiple arguments? ((( λ x ( λ y ( y x ))) 3) INC) → e ::= … | e 0 e 1 … e n | λ ( x 1 …x n ) e 0 (( λ y ( y 3)) INC) → (INC 3) → 4 • Can desugar (rewrite syntactically) into single-argument calculus: Shorthand: ( λ ( x 1 ... x n ) e ) � ( λ x 1 ( λ … ( λ x n e )…)) (( λ ( x y ) ( y x )) = ( λ x ( λ y ( y x ))) ( e 0 e 1 e 2 … e n ) � (...(( e 0 e 1 ) e 2 )… e n ) • Multi-argument functions are curried – applied one argument at a time (( λ ( x y ) ( y x )) 3 INC) → (INC 3) → 4 (+ 1 5) � ((+ 1) 5) CS 611 Lecture 7 – Andrew Myers, Cornell University 7 CS 611 Lecture 7 – Andrew Myers, Cornell University 8 Operational semantics Small-step semantics • Large-step semantics: configuration is ( β red’n) ( λ x e 1 ) e 2 → e 1 { e 2 / x } an expression of the language (no store) e 1 → e � 1 e 0 � λ x e 2 e 2 { e 1 / x } � v e 1 e 2 → e � 1 e 2 e 0 e 1 � v call-by-name • Call-by-name semantics: e 1 is not e 1 → e � 1 evaluated before substitution e 1 e 2 → e � 1 e 2 ( λ x e 1 ) v → e 1 { v / x } • Call-by-name + β -reduction: any e 2 → e � 2 v e 2 → v e � 2 lambda term is a value : v ::= λ x e call-by-value CS 611 Lecture 7 – Andrew Myers, Cornell University 9 CS 611 Lecture 7 – Andrew Myers, Cornell University 10 An infinite loop Simulating let • Lambda calculus has no “let” statement ala ML ( λ x (x x)) ( λ x (x x)) → ? This expression diverges (never stops taking ( λ x e 2 ) e 1 ( let x = e 1 in e 2 ) small steps) CS 611 Lecture 7 – Andrew Myers, Cornell University 11 CS 611 Lecture 7 – Andrew Myers, Cornell University 12 2

  3. Definitions Representing booleans • Lambda calculus terms can become long; for • Lambda calculus is universal: no primitive compactness we will use names, multiple boolean type or “if” statement is needed arguments as shorthand (not part of language!) TRUE � ( λ x ( λ y x)) ~ ( λ (x y) x) IDENTITY � ( λ x x) FALSE � ( λ x ( λ y y)) ~ ( λ (x y) y) INC � (+ 1) if e 1 then e 2 else e 3 � ( IF e 1 e 2 e 3 ) APPLY - TO - FIVE � ( λ f (f 5)) IF � ( λ (x y z) (x y z)) COMPOSE � ( λ (f g) ( λ x (f (g x)))) ( IF TRUE e 2 e 3 ) → (( λ x ( λ y x)) e 2 ) e 3 ) TWICE � ( λ f ( COMPOSE f f )) → (( λ y e 2 ) e 3 ) → e 2 (( COMPOSE INC INC ) 2) → 4 (( TWICE ( TWICE INC )) 0) → 4 • Call-by-name important! e 2 and e 3 are not evaluated eagerly by IF CS 611 Lecture 7 – Andrew Myers, Cornell University 13 CS 611 Lecture 7 – Andrew Myers, Cornell University 14 Representing pairs (lists) Natural numbers • Pair/list operations: • Model the number n as a function that ( CONS x y) : construct list with head x, tail y composes an arbitrary function n times ( FIRST p) : return first item in list/first item in : Church numerals pair 0 � ( λ ( f a ) a ) (= FALSE ) ( REST p) : return remainder of list/second item in pair 1 � ( λ ( f a ) ( f a )) • One way to implement these operations: 2 � ( λ ( f a ) ( f ( f a ))) CONS � ( λ (x y) ( λ f (f x y))) 3 � ( λ ( f a ) ( f ( f ( f a )))) FIRST � ( λ p (p ( λ (x y) x))) (= ( λ p (p TRUE ))) n � ( λ ( f a ) ( f ( ... ( f a )...))) REST � ( λ p (p ( λ (x y) y))) (= ( λ p (p FALSE ))) n times CS 611 Lecture 7 – Andrew Myers, Cornell University 15 CS 611 Lecture 7 – Andrew Myers, Cornell University 16 Arithmetic • Can define INC function that adds one by writing a function that interposes an extra call to the function: n � ( λ ( f a ) ( f n a )) INC � ( λ n ( λ ( f a ) ( f (( n f ) a )))) Can define + and other arithmetic operators: + � ( λ ( n 1 n 2 ) ( λ ( f a ) (( n 1 f ) (( n 2 f ) a ))) or + � ( λ ( n 1 n 2 ) (( n 1 INC ) n 2 )) CS 611 Lecture 7 – Andrew Myers, Cornell University 17 3

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