Programming with Lambda (Pre Lecture)

Programming with Lambda (Pre Lecture)

Programming with Lambda (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2021 Dantam (Mines CSCI-400) Lambda Spring 2021 1/29 Origin of Lambda: What’s an “algorithm”? Entscheidungsproblem: mathematical true algorithm statement false λ-calculus Turing machines David Hilbert Alonzo Church Alan Turing Dantam (Mines CSCI-400) Lambda Spring 2021 2/29 Introduction Lambda Outcomes ◮ Lambda: the ultimate operator ◮ Implement common language ◮ Everything programmable can be constructs using Lambda programmed with lambda ◮ Practical: if a language lacks a desired feature, but has lambda, you can implement that feature with lambda (see also: JavaScript) Dantam (Mines CSCI-400) Lambda Spring 2021 3/29 Lambda Expression Review Function Definition Function Application formal parameter function λ . z}|{x α z}|{f y body actual parameter |{z} |{z} ◮ Create an unnamed function. ◮ Bind actual parameter y to ◮ The function is itself a value. the formal parameter of f . ◮ Do not evaluate the body until applied (called) ◮ Evaluate the body of f . Example a = 2 + (λa . 1+ a) 2 1 + 2 3 Dantam (Mines CSCI-400) Lambda Spring 2021 4/29 Lambda: Very Important! Review ◮ Lambda can perform any possible computation! ◮ Trivial with Lambda: ◮ N-ary functions ◮ Variable binding (let) “I’m kind of a big deal.” ◮ Statements (;) ◮ Named functions ◮ OOP ◮ Also possible with Lambda: ◮ Recursion λ ◮ Conditionals (if) ◮ Lists ◮ Structs ◮ Arithmetic (+, −, etc.) Dantam (Mines CSCI-400) Lambda Spring 2021 5/29 Lambda: Very Important! Review ◮ Lambda can perform any possible computation! ◮ Trivial with Lambda: ◮ N-ary functions ◮ Variable binding (let) “I’m kind of a big deal.” ◮ Statements (;) ◮ Named functions ◮ OOP ◮ Also possible with Lambda: ◮ Recursion λ ◮ Conditionals (if) ◮ Lists ◮ Structs ◮ Arithmetic (+, −, etc.) Dantam (Mines CSCI-400) Lambda Spring 2021 6/29 Binary and N-ary Functions Outline Binary and N-ary Functions Bindings Dantam (Mines CSCI-400) Lambda Spring 2021 7/29 Binary and N-ary Functions Binary Functions Reduction of Binary to Unary Functions Example λ . Binary Unary ( ab a + b) 1 2 formal params. body λx .λy . e λ x y . e z}|{ z}|{ (λa .λb . a + b) 1 2 ◮ Convert to two lambdas a = 1 ◮ Outer lambda binds first param (x) and (λb . 1+ b) 2 returns inner lambda ◮ Inner lambda binds second param (y) and b = 2 evaluates 1 + 2 Each function call creates new bindings of its arguments. Dantam (Mines CSCI-400) Lambda Spring 2021 8/29 Binary and N-ary Functions N-ary Functions N-ary functions Example arity: n + 1 arity: n (λabc . a + b + c) 1 2 3 λx0 x1 ... xn . e λx0 . (λx1 ... xn . α) (λa .λbc . a + b + c) 1 2 3 ◮ Recursively convert to unary functions. (λa .λb .λc . a + b + c) 1 2 3 Dantam (Mines CSCI-400) Lambda Spring 2021 9/29 Binary and N-ary Functions N-ary Functions example, continued Example (λabc . a + b + c) 1 2 3 (λb .λc . 1+ b + c) 2 3 b = 2 λ .λ . λ . ( a bc a + b + c) 1 2 3 = 1 ( c 1+2+ c) 3 a c = 3 (λa .λb .λc . a + b + c) 1 2 3 1 + 2 + 3 + 6 Dantam (Mines CSCI-400) Lambda Spring 2021 10/29 Binary and N-ary Functions Currying: Specializing Functions Binary Curry curry ≡ λf a . (λb . f ab) Function curry(f, a) 1 function g(b) is f(a,b) 2 g Example curry (λab . a + b) 42 Haskell B. Curry λb . (λab . a + b) 42 b λb . 42 + b Dantam (Mines CSCI-400) Lambda Spring 2021 11/29 Binary and N-ary Functions Historical Interlude 0: Mathematics of Lambda Curry, Hilbert, Church, and Turing “decision problem” via λ-calculus “decision problem” via Turing machines David Hilbert Alonzo Church Haskell Curry Alan Turing Dantam (Mines CSCI-400) Lambda Spring 2021 12/29 Binary and N-ary Functions Exercise: Currying Reduce the n-ary functions to unary functions: ◮ λxy . xy ◮ λxyz . (xy)+ z Dantam (Mines CSCI-400) Lambda Spring 2021 13/29 Bindings Outline Binary and N-ary Functions Bindings Dantam (Mines CSCI-400) Lambda Spring 2021 14/29 Bindings Let Expression Review Let Expression Evaluation ◮ let x ← e1 in e2 Evaluate e1 v1 ◮ identifier binding exp. body exp. Substitute v1 for x in e2, |{z} |{z} |{z} ′ yielding new e2 ′ ◮ Evaluate e2 v2 Dantam (Mines CSCI-400) Lambda Spring 2021 15/29 Bindings Example: Let and Lambda Example (Variable Binding) Example (Function Application) let a ← 2 in 1+ a (λa . 1+ a) 2 a = 2 a=2 1 + 2 1 + 2 + + 3 3 Notice the similarity? Dantam (Mines CSCI-400) Lambda Spring 2021 16/29 Bindings Let to Lambda Reduction of Let to Lambda Let Lambda identifier binding exp. body exp. identifier body exp. binding exp. λ . let z}|{x ← z}|{e1 in z}|{e2 ( z}|{x z}|{e2 ) z}|{e1 ◮ Identifier x becomes lambda parameter ◮ Body exp. e2 becomes the lambda body ◮ Binding exp. e1 becomes the call argument Example let λ a = 2 + let a ← 2 in 1+ a (λa . 1+ a) 2 1 + 2 3 Dantam (Mines CSCI-400) Lambda Spring 2021 17/29 Bindings Sequential Definition Sequential Definition identifier binding exp. Example var x ← e1 ; z}|{ z}|{ e2 var a ← 2; 1+ a body |{z} a = 2 Evaluation ◮ 1 + 2 Evaluate e1 v1 ◮ + Substitute v1 for x in e2, ′ yielding new e2 3 ′ ◮ Evaluate e2 v2 Dantam (Mines CSCI-400) Lambda Spring 2021 18/29 Bindings Example: Sequential Definitions and Let Example (Sequential Definition) Example (Let) var a ← 2; let a ← 2 in 1+ a 1+ a a = 2 a = 2 1 + 2 1 + 2 + + 3 3 Dantam (Mines CSCI-400) Lambda Spring 2021 19/29 Bindings Sequential Definition to Let Reduction of Sequential Definition to Let Sequential Def. identifier binding exp. Let var x ← e1 ; identifier binding exp. body z}|{ z}|{ e2 let x ← e1 in e2 z}|{ z}|{ z}|{ body |{z} Example var a ← 2; let a ← 2 in 1+ a (λa . 1+ a) 2 1 + 2 3 1+ a Dantam (Mines CSCI-400) Lambda Spring 2021 20/29 Bindings Named Local Functions Example flet f (a) → 1+ a in f 2 Local Functions name a = 2 formal param. fun. body flet body 1 + 2 flet f x → e1 in e2 z}|{ z}|{ z}|{ z}|{ + 3 Dantam (Mines CSCI-400) Lambda Spring 2021 21/29 Bindings Example: Local Functions and Let/Lambda Example (Local Function) Example (Let and Lambda) flet f (a) → 1+ a in f 2 let f ← λa . 1+ a in f 2 a = 2 f = λa . 1+ a 1 + 2 (λa . 1+ a) 2 + a = 2 3 1 + 2 + 3 Dantam (Mines CSCI-400) Lambda Spring 2021 22/29 Bindings Local Functions to Let and Lambda Reduction of Local Functions to Let and Lambda Local Function name Let / Lambda formal param. fun. body flet body let f ← λx . e1 in e2 flet f x → e1 in e2 z}|{ z}|{ z}|{ z}|{ Example ... flet f (a) → 1+ a in f 2 let f ← λa . 1+ a in f 2 (λa . 1+ a) 2 Dantam (Mines CSCI-400) Lambda Spring 2021 23/29 Bindings Global Functions to Local Functions Reduction Global Functions to Sequential Assignment Global Function name formal param. fun body defun f x → e1 ; Local Function z}|{ z}|{ z}|{ flet f (x) → e1 in e2 e2 more expressions |{z} Example defun f (a) → 1+ a; ... flet f (a) → 1+ a in f 2 let f ← λa . 1+ a in f 2 f 2 Dantam (Mines CSCI-400) Lambda Spring 2021 24/29 Bindings Example: Reduction to Lambda name binding exp body name body binding exp ◮ let a=2 let a ← 2 in 1+ a λ a . 1+ a 2 1 + 2 z}|{ z}|{ z }| { z}|{ z }| { z}|{ + 3 ◮ let x ← 1 in (let y ← 2 in x + y) let x let y (λx . let y ← 2 in x + y) 1 (λx . λy . x + y) 1 2 x=1 y=1 + (λy . 1+ y) 2 1 + 2 3 ◮ flet f (x) → if x then 1 else 0 in f true flet let f ← λx . if x then 1 else 0 in f true let (λf . f true)(λx . if x then 1 else 0) f =λx . ... (λx . if x then 1 else 0) true x=true if if true then 1 else 0 1 Dantam (Mines CSCI-400) Lambda Spring 2021 25/29 Bindings Exercise: Reduction to Lambda ◮ let x ← 1 in λa . x + a ◮ (let x ← 1 in λa . x + a) 2 ◮ flet f (a b) → a + b in f 1 2 ◮ flet f (n) → (if n = 0 then 1 else n ∗ f (n − 1)) in f 2 Dantam (Mines CSCI-400) Lambda Spring 2021 26/29 Bindings Summary We can program with lambda? ◮ Everything is an expression var n-ary ◮ Everything is a lambda ◮ We’ve seen: let, var, flet, defun ◮ Also possible: Recursion, Conditionals, Lists, Arithmetic ◮ More to come: defun let ◮ How do we actually implement lambda? λ ◮ How do we detect errors at compile time and avoid errors at runtime? ◮ What other programming “tricks” can we now use? if {+, −, /, ∗} Dantam (Mines CSCI-400) Lambda Spring 2021 27/29 Bindings Historical Interlude 1: From Lambda to Functional Programming Alonzo Church. A set of postulates for the foundation of logic (Lambda Calculus). 1932. John McCarthy. Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I. (Lisp). 1960. Guy Steele, Jr. Lambda: The ultimate {imperative, declarative}. (Scheme). 1976. Dantam (Mines CSCI-400) Lambda Spring 2021 28/29 Bindings References ◮ Pierce. Type Systems and Programming Languages. ◮ Ch 5.2 Programming in the Lambda-Calculus Dantam (Mines CSCI-400) Lambda Spring 2021 29/29.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    29 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us