
Fundamentals and lambda calculus Deian Stefan (adopted from my & Edward Yang’s CSE242 slides) Logistics • Assignments: ➤ Programming assignment 1 is out ➤ Homework 1 will be released tomorrow night • Podcasting: everything should be set • Section: 8-8:50AM default; TA choice to do 3-3:50PM • Clickers, sign up: see piazza/course page for link ➤ We’ll use them today JavaScript functions • JavaScript functions are first-class ➤ Syntax is a bit ugly/terse when you want to use functions as values; recall block scoping: (function () { // ... do something }) (); • New version has cleaner syntax called “fat arrows” ➤ Semantics not always the same (this has different meaning), but for this class should always be safe to use fat-arrows.js In this lecture In this lecture In this lecture In this lecture λ What is the lambda calculus? • Simplest reasonable programming language ➤ Only has one feature: functions Why study it? • Captures the idea of first-class functions ➤ Good system for studying the concept of variable binding that appears in almost all languages • Historically important ➤ Competing model of computation introduced by Church as an alternative to Turing machines: substitution (you’ll see this today) = symbolic comp ➤ Influenced Lisp (thus JS), ML, Haskell, C++, etc. Why else? • Base for studying many programming languages ➤ You can use lambda calculus and extended it in different ways to study languages and features ➤ E.g., we can study the difference between strict languages like JavaScript and lazy ones like Haskell ➤ λ + evaluation strategy ➤ E.g., we can study different kinds of type systems ➤ Simply-typed λ calculus, polymorphic, etc. Why else? • Most PL papers describe language models that build on lambda calculus ➤ Understanding λ will help you interpret what you are reading in PL research papers ➤ Understanding λ will help you get started with other formal/theoretical foundations: ➤ Operational semantics ➤ Denotational semantics Before we get started, some terminology • Syntax (grammar) ➤ The symbols used to write a program ➤ E.g., (x + y) is a grammatical expression • Semantics ➤ The actions that occur when a program is executed • PL implementation: Syntax -> Semantics Before we get started, some terminology • Syntax (grammar) ➤ The symbols used to write a program ➤ E.g., (x + y) is a grammatical expression • Semantics ➤ The actions that occur when a program is executed • PL implementation: Syntax -> Semantics Before we get started, some terminology • Syntax (grammar) ➤ The symbols used to write a program ➤ E.g., (x + y) is a grammatical expression • Semantics ➤ The actions that occur when a program is executed • PL implementation: Syntax -> Semantics Today • Syntax of λ calculus • Semantics of λ calculus ➤ Free and bound variables ➤ Substitution ➤ Evaluation order Lambda calculus • Language syntax (grammar): ➤ Expressions: e ::= x | λx.e | e1 e2 ➤ Variables: x ➤ Functions or λ abstractions: λx.e ➤ This is the same as x => e in JavaScript! ➤ Function application: e1 e2 ➤ This is the same as e1 (e2) in JavaScript! Example terms ➤ λx.(2+x) ➤ Same as: x => (2 + x) ➤ (λx.(2 + x)) 5 ➤ Same as: (x => (2 + x)) (5) ➤ (λf.(f 3)) (λx.(x + 1)) ➤ Same as: (f => (f (3))) (x => (x+1)) Example terms LIES! What is this “2” ➤ λx.(2+x) and “+”? (Sugar.) ➤ Same as: x => (2 + x) ➤ (λx.(2 + x)) 5 ➤ Same as: (x => (2 + x)) (5) ➤ (λf.(f 3)) (λx.(x + 1)) ➤ Same as: (f => (f (3))) (x => (x+1)) Example terms ➤ λx.(2+x) ➤ Same as: x => (2 + x) ➤ (λx.(2 + x)) 5 ➤ Same as: (x => (2 + x)) (5) ➤ (λf.(f 3)) (λx.(x + 1)) ➤ Same as: (f => (f (3))) (x => (x+1)) Example terms ➤ λx.(2+x) ➤ Same as: x => (2 + x) ➤ (λx.(2 + x)) 5 ➤ Same as: (x => (2 + x)) (5) ➤ (λf.(f 3)) (λx.(x + 1)) ➤ Same as: (f => (f (3))) (x => (x+1)) Example terms ➤ λx.(2+x) ➤ Same as: x => (2 + x) ➤ (λx.(2 + x)) 5 ➤ Same as: (x => (2 + x)) (5) ➤ (λf.(f 3)) (λx.(x + 1)) ➤ Same as: (f => (f (3))) (x => (x+1)) JavaScript to λ calculus • Let’s look at function composition: (f◦f)(x) • In JavaScript: ➤ f => (x => f (f (x))) ➤ ((f => (x => f (f (x)))) (x => x+1)) (4) • In λ: ➤ λf.(λx. f (f x)) ➤ ((λf.(λx. f (f x)) (λx.x+1)) 4 JavaScript to λ calculus • Let’s look at function composition: (f◦f)(x) • In JavaScript: ➤ f => (x => f (f (x))) ➤ ((f => (x => f (f (x)))) (x => x+1)) (4) • In λ: ➤ λf.(λx. f (f x)) ➤ ((λf.(λx. f (f x)) (λx.x+1)) 4 JavaScript to λ calculus • Let’s look at function composition: (f◦f)(x) • In JavaScript: ➤ f => (x => f (f (x))) ➤ ((f => (x => f (f (x)))) (x => x+1)) (4) • In λ: ➤ λf.(λx. f (f x)) ➤ ((λf.(λx. f (f x)) (λx.x+1)) 4 Today • Syntax of λ calculus ✓ • Semantics of λ calculus ➤ Free and bound variables ➤ Substitution ➤ Evaluation order Semantics of λ calculus • Reduce a term to another as much as we can ➤ If we can’t reduce it any further, the term is said to be in normal form • How? Rewrite terms! Example terms • Example: (λx.(2 + x)) 5 → (2 + 5) → 7 ➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7 • Example: (λf.(f 3)) (λx.(x + 1)) → ((λx.(x + 1)) 3) → (3 + 1) → 4 ➤ In JavaScript: (f => (f (3))) (x => (x+1)) → ((x => (x+1)) (3) → (3+1) → 4 Example terms • Example: (λx.(2 + x)) 5 → (2 + 5) → 7 ➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7 • Example: (λf.(f 3)) (λx.(x + 1)) → ((λx.(x + 1)) 3) → (3 + 1) → 4 ➤ In JavaScript: (f => (f (3))) (x => (x+1)) → ((x => (x+1)) (3) → (3+1) → 4 Example terms • Example: (λx.(2 + x)) 5 → (2 + 5) → 7 ➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7 • Example: (λf.(f 3)) (λx.(x + 1)) → ((λx.(x + 1)) 3) → (3 + 1) → 4 ➤ In JavaScript: (f => (f (3))) (x => (x+1)) → ((x => (x+1)) (3) → (3+1) → 4 Example terms • Example: (λx.(2 + x)) 5 → (2 + 5) → 7 ➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7 • Example: (λf.(f 3)) (λx.(x + 1)) → ((λx.(x + 1)) 3) → (3 + 1) → 4 ➤ In JavaScript: (f => (f (3))) (x => (x+1)) → ((x => (x+1)) (3) → (3+1) → 4 Example terms • Example: (λx.(2 + x)) 5 → (2 + 5) → 7 ➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7 • Example: (λf.(f 3)) (λx.(x + 1)) → ((λx.(x + 1)) 3) → (3 + 1) → 4 ➤ In JavaScript: (f => (f (3))) (x => (x+1)) → ((x => (x+1)) (3) → (3+1) → 4 Example terms • Example: (λx.(2 + x)) 5 → (2 + 5) → 7 ➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7 • Example: (λf.(f 3)) (λx.(x + 1)) → ((λx.(x + 1)) 3) → (3 + 1) → 4 ➤ In JavaScript: (f => (f (3))) (x => (x+1)) → ((x => (x+1)) (3) → (3+1) → 4 Example terms • Example: (λx.(2 + x)) 5 → (2 + 5) → 7 ➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7 • Example: (λf.(f 3)) (λx.(x + 1)) → ((λx.(x + 1)) 3) → (3 + 1) → 4 ➤ In JavaScript: (f => (f (3))) (x => (x+1)) → ((x => (x+1)) (3) → (3+1) → 4 Example terms • Example: (λx.(2 + x)) 5 → (2 + 5) → 7 ➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7 • Example: (λf.(f 3)) (λx.(x + 1)) → ((λx.(x + 1)) 3) → (3 + 1) → 4 ➤ In JavaScript: (f => (f (3))) (x => (x+1)) → ((x => (x+1)) (3) → (3+1) → 4 Example terms • Example: (λx.(2 + x)) 5 → (2 + 5) → 7 ➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7 • Example: (λf.(f 3)) (λx.(x + 1)) → ((λx.(x + 1)) 3) → (3 + 1) → 4 ➤ In JavaScript: (f => (f (3))) (x => (x+1)) → ((x => (x+1)) (3) → (3+1) → 4 Easy! Pattern: for function application substitute the term you are applying the function to for the argument variable Substitution (not right) • Substitution: e1 [x := e2] ➤ Replace every occurrence of x in e1 with e2 • General reduction rule for λ calculus: ➤ (λx.e1) e2 → e1 [x := e2] ➤ Function application rewritten to e1 (the function body) with every x in e1 substituted with e2 (argument) A more complicated example A more complicated example • Compose function that adds 1 to arg & apply it to 4 ➤ ((λf.(λx. f (f x)) (λx.x+1)) 4 A more complicated example • Compose function that adds 1 to arg & apply it to 4 ➤ ((λf.(λx. f (f x)) (λx.x+1)) 4 ➤ (λx. (λx.x+1) ((λx.x+1) x)) 4 A more complicated example • Compose function that adds 1 to arg & apply it to 4 ➤ ((λf.(λx. f (f x)) (λx.x+1)) 4 ➤ (λx. (λx.x+1) ((λx.x+1) x)) 4 ➤ (λx.x+1) ((λx.x+1) 4) A more complicated example • Compose function that adds 1 to arg & apply it to 4 ➤ ((λf.(λx. f (f x)) (λx.x+1)) 4 ➤ (λx. (λx.x+1) ((λx.x+1) x)) 4 ➤ (λx.x+1) ((λx.x+1) 4) ➤ (λx.x+1) (4+1) A more complicated example • Compose function that adds 1 to arg & apply it to 4 ➤ ((λf.(λx. f (f x)) (λx.x+1)) 4 ➤ (λx. (λx.x+1) ((λx.x+1) x)) 4 ➤ (λx.x+1) ((λx.x+1) 4) ➤ (λx.x+1) (4+1) ➤ 4+1+1 A more complicated example • Compose function that adds 1 to arg & apply it to 4 ➤ ((λf.(λx. f (f x)) (λx.x+1)) 4 ➤ (λx. (λx.x+1) ((λx.x+1) x)) 4 ➤ (λx.x+1) ((λx.x+1) 4) ➤ (λx.x+1) (4+1) ➤ 4+1+1 ➤ 6 Let’s make this even more fun! Let’s make this even more fun! • Instead of 1, let’s add x to argument (& do it 2 times): ➤ (λf.(λx.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages110 Page
-
File Size-