CS 430 Spring 2015 λ · 2015. 2. 19. · Rule 2: Colors If an alligator is about to eat a family...

50
CS 430 Spring 2015 Professor Lam Semantics and Lambda Calculus λ

Transcript of CS 430 Spring 2015 λ · 2015. 2. 19. · Rule 2: Colors If an alligator is about to eat a family...

  • CS 430Spring 2015

    Professor Lam

    Semantics and Lambda Calculus

    λ

  • Overview

    ● Syntax: form of a program– Described using regular expressions and BNF

    ● Semantics: meaning of a program– Much more difficult to describe clearly

    ● Implementation: how a program executes– Described using algorithms and code

  • Operational Semantics

    ● Describe a program's effects on the machine– Use notation that is closer to the hardware

    for (i=0; i

  • Axiomatic Semantics

    ● Describes what can be proven about programs– Express programs as proof trees– Loops can be difficult to handle

    {P} if e1 then e2 else e3 {Q}

    {P ^ e1} e2 {Q} {P ^ ¬e1} e3 {Q}

    {x=10} if x > 5 then y := 3 else y := 7 {x=10 ^ y=3}

    {x=10 ^ x>5} y:=3 {x=10 ^ y=3}

    ...

    SConditional

    SConditional

    SAssign

  • Denotational Semantics

    ● Describes a program's results using functions– Must also track system state

    eval :: (Program, State) → (Value, State)

    eval(e1 + e2, S) = let (v1, S') = eval(e1, S) in let (v2, S'') = eval(e2, S') in (v1 + v2, S'')

    eval(while e1 do e2, S) = let (v, S') = eval(e1, S) in if not v then S' else let (_, S'') = eval(e2, S') eval(while e1 do e2, S'')

  • Semantics

    ● Three main approaches:– Operational semantics: program/machine effects

    ● Describes a program's effect on a mathematical model of themachine on which it is executing

    ● We'll revisit some of this in implementation topics– Axiomatic semantics: programs are proofs

    ● Fascinating subject but out of scope for CS430– Denotational semantics: programs are functions

    ● This approach most closely relates to our next unit● There is something fundamental about functions

  • Computational Models

    ● What is computation?● One common model: universal Turing machine

    Alan Turing

  • Computational Models

    ● Another model: λ-calculus by Alonzo Church– "Lambda calculus"

    ● Minimal “core” language

    E → x (name/variable) | λx.E (function) | E E (application)

    Alonzo Church

  • Lambda Calculus

    ● But first, a puzzle game!

    http://worrydream.com/AlligatorEggs/

  • Alligator Eggs

    ● Hungry alligators: eat and guard family

    ● Old alligators: guard family (cannot eat)

    ● Eggs: hatch into new family

  • Alligator Eggs

    ● Families are shown in columns● Alligators guard families below them

  • Rule 1: Eating

    ● If families are side-by-side:– Top left alligator eats

    the entire family toher right

    – The top left alligatordies

    – Any eggs she wasguarding of the samecolor hatch into whatshe just ate

  • Rule 1: Eating

    ● What happens to these alligators?

  • Rule 1: Eating

  • Rule 1: Eating

  • Rule 1: Eating

  • Rule 1: Eating

  • Rule 1: Eating

  • Rule 1: Eating

  • Rule 1: Eating

  • Rule 1: Eating

  • Rule 1: Eating

  • Rule 1: Eating

  • Rule 1: Eating

    “reduces to”

    (

  • Exercise

    ● What happens to these alligators?

  • Exercise

    ● What happens to these alligators?

    (λx.(λy.λz.y z)x) (λa.a)

  • Exercise

    ● What happens to these alligators?

    (λx.(λy.λz.y z)x) (λa.a)(λx.(λz.x z)) (λa.a)

  • Exercise

    ● What happens to these alligators?

    (λx.(λy.λz.y z)x) (λa.a)(λx.(λz.x z)) (λa.a)

    (λz.(λa.a) z)

  • Exercise

    ● What happens to these alligators?

    (λx.(λy.λz.y z)x) (λa.a)(λx.(λz.x z)) (λa.a)

    (λz.(λa.a) z)(λz.z)

  • Rule 2: Colors

    ● If an alligator is about to eat a family and a color appears in bothfamilies then we need to change that color in one of the families.– In the picture below, green and red appear in both the first and second

    families. So, in the second family, we switch all of the greens to cyan,and all of the reds to blue.

    (λx.λy.x y) (λx.λy.x y) (λa.a) (λx.λy.x y) (λc.λd.c d) (λa.a)

  • Rule 2: Colors

    ● If an alligator is about to eat a family and a color appears in bothfamilies then we need to change that color in one of the families.– In the picture below, green and red appear in both the first and second

    families. So, in the second family, we switch all of the greens to cyan,and all of the reds to blue.

    (λx.λy.x y) (λx.λy.x y) (λa.a) (λx.λy.x y) (λc.λd.c d) (λa.a)(λy.(λc.λd.c d) y) (λa.a)

    ...

  • Rule 2: Colors

    ● If a color appears in both families, but only asan egg, no color change is made.

    (λx.x y) (λx.x y) (λx.x y) (λz.z y)

  • Rule 2: Colors

    ● If a color appears in both families, but only asan egg, no color change is made.

    (λx.x y) (λx.x y) (λx.x y) (λz.z y)((λz.z y) y)

  • Rule 2: Colors

    ● If a color appears in both families, but only asan egg, no color change is made.

    (λx.x y) (λx.x y) (λx.x y) (λz.z y)((λz.z y) y)

    (y y)

  • Rule 3: Old Alligators

    ● An old alligator that is guarding only one familydies.

    ((λx.x)(λx.x)) (λz.z)

    ((λx.x)) (λz.z) (λx.x) (λz.z)

  • Exercises

    ● Try to reduce these groups of alligators as much as possibleusing the three puzzle rules:

    (λx.λy.x x y) (λa.λb.a b) (λc.c) ((λx.x)(λy.y)) (λz.z)

    ((λx.λy.x y y) (λz.z)) (λa.λb.a)

    (λx.x x) (λy.y z) a

  • Lambda Calculus

    ● Lambda calculus expressions:

    ● Parentheses used to group expressions– (old alligators)

    E → x (name/variable) (x: egg) | λx.E (function) (λx: alligator) | E E (application) (adjacency)

  • Lambda Calculus

    ● Operational semantics– Very simple: all we have are functions– Function application results in a binding

    ● Names are bound to values● Values are themselves lambda expressions (families), and may

    be names OR functions!● Beta-reduction: (λx.e1) e2 → e1[x/e2]

    – “Evaluate e1 with x bound to e2”– Same as Rule 1: Eating

  • Lambda Calculus

    ● Static scoping– Always use innermost possible binding– Example: (λx.x (λx.x))

    ● Rightmost x refers to second binding

    ● Alpha-conversion: λx.x → λx.x → λx.x– Same as Rule 2: Colors– (λx.x (λx.x)) = (λx.x (λy.y))

  • Lambda Calculus

    ● The scope of λ extends as far to the right as possible– λx.λy.x y means λx.(λy.(x y))

    ● Function application is left-associative– x y z means (x y) z

  • Examples

    ● (λx.x) z → ?● (λx.y) z → ?● (λx.x y) z → ?● (λx.x y) (λz.z) → ?● (λx.λy.x y) z → ?● (λx.λy.x y) (λz.z z) x → ?

  • Examples

    ● (λx.x) z → z● (λx.y) z → ?● (λx.x y) z → ?● (λx.x y) (λz.z) → ?● (λx.λy.x y) z → ?● (λx.λy.x y) (λz.z z) x → ?

  • Examples

    ● (λx.x) z → z● (λx.y) z → y● (λx.x y) z → z y

    – A function that applies its argument to y● (λx.x y) (λz.z) → (λz.z) y → y● (λx.λy.x y) z → λy.z y

    – A curried function of two arguments that applies its first argumentto its second

    ● (λx.λy.x y) (λz.z z) x →– λy.((λz.z z)y)x → (λz.z z)x → x x

  • Well that was fun...

    ● But how do we do “practical” computation?– Variables are only “names”– How do we handle integers, floats, booleans, etc.?– We could extend the language

    ● This is what functional programming languages do– Another (purer) method: family names!

    ● i.e., “encoding”

  • Family Names

    ● When family Not eats family True it becomes familyFalse and when Not eats False it becomes True

    What color shouldthe “?” eggs be?

    λx.λy.x λx.λy.yλz.z(λx.λy.?)(λx.λy.?)

    “A function thattakes two

    arguments andreturns the

    first”

    “A function thattakes two

    arguments andreturns the

    second”

  • Family Names

    ● When family Not eats family True it becomes familyFalse and when Not eats False it becomes True

    λx.λy.x λx.λy.yλz.z(λx.λy.y)(λx.λy.x)

    “A function thattakes two

    arguments andreturns the

    first”

    “A function thattakes two

    arguments andreturns the

    second”

    “A function that takes aboolean value and returns its

    logical negation”

  • Church-Turing Thesis

    ● (Very) Informally: if an algorithm exists describing a particularcomputation, then– the same calculation can be computed by a Turing machine, and– the same calculation can also be represented by a λ-function

    ● Thus, we can encode any computation in lambda expressions– Sometimes we have to be clever about how we encode information

    ● Example: the boolean representation we saw earlier● More examples: pairs and Church numerals

    – Usually it is somewhat tedious to work out by hand

    Stephen Kleene

  • Lambda Calculus

    ● Online lambda calculus reduction workbench:– http://www.itu.dk/~sestoft/lamreduce/lamframes.html

    – Use “\” instead of “λ”

    ● True/False example:– true: \x.\y.x– false: \x.\y.y– not: (\z.z(\x.\y.y)(\x.\y.x))

    – not true: (\z.z(\x.\y.y)(\x.\y.x)) (\x.\y.x)● Result: false (\x.\y.y)

    – not false: (\z.z(\x.\y.y)(\x.\y.x)) (\x.\y.y)● Result: true (\x.\y.x)

    http://www.itu.dk/~sestoft/lamreduce/lamframes.html

  • Next Time

    ● Functional programming– Based on functions (as was lambda calculus)– LISP adds lists– ML adds strong static typing– Haskell adds lazy evaluation and pure IO

  • Next Time

    ● Functional programming– Based on functions (as was lambda calculus)– LISP adds lists– ML adds strong static typing– Haskell adds lazy evaluation and pure IO– Result:

    Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50