lambda calculus slides

Upload: praveen-nayaka

Post on 29-May-2018

240 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Lambda Calculus Slides

    1/33

    Introductory Courseon Logic and Automata Theory

    Introduction to the lambda calculus

    [email protected]

    Based on slides by Jeff Foster, UMD

    Introduction to lambda calculus . 1/

  • 8/8/2019 Lambda Calculus Slides

    2/33

    History

    Formal mathematical systemSimplest programming language

    Intended for studying functions, recursion

    Invented in 1936 by Alonzo Church (1903-1995)Churchs Thesis:

    Every effectively calculable function (effectively decidable predicate) is general recursive i.e. can be computed by lambda calculus

    Churchs Theorem:

    First order logic is undecidable

    Introduction to lambda calculus . 2/

  • 8/8/2019 Lambda Calculus Slides

    3/33

    Syntax

    Simple syntax:

    e ::= x Variables

    | x.e Functions| e e Function applications

    Pure lambda calculus: only functionsArguments are functionsReturned value is functionA function on functions is higher-order

    Introduction to lambda calculus . 3/

  • 8/8/2019 Lambda Calculus Slides

    4/33

    Semantics

    Evaluating function application: ( x.e1) e2Replace every x in e1 with e2Evaluate the resulting term

    Return the result of the evaluationFormally: -reduction

    ( x.e1) e2 e1[e2/ x]A term that can be -reduced is a redex We omit when obvious

    Introduction to lambda calculus . 4/

  • 8/8/2019 Lambda Calculus Slides

    5/33

    Convenient assumptions

    Syntactic sugar for declarationslet x = e1 in e2

    Scope of extends as far to the right as possible

    x. y. x y is x.( y.( x y))Function application is left-associative

    x y zmeans ( x y) z

    Introduction to lambda calculus . 5/

  • 8/8/2019 Lambda Calculus Slides

    6/33

    Scoping and parameter passing

    -reduction is not yet well-dened:( x.e1) e2 e1[e2/ x]There might be many x dened in e1

    ExampleConsider the programlet x = a in

    let y = z. x inlet x = b in y xWhich x is bound to a , and which to b?

    Introduction to lambda calculus . 6/

  • 8/8/2019 Lambda Calculus Slides

    7/33

    Lexical scoping

    Variable refers to closest denitionWe can rename variables to avoid confusion:let x = a in

    let y = z. x inlet w = b in y w

    Introduction to lambda calculus . 7/

  • 8/8/2019 Lambda Calculus Slides

    8/33

    Free/bound variables

    The set of free variables of a term is

    FV ( x) = xFV ( x.e) = FV (e) \ { x}FV (e1 e2) = FV (e1)FV (e2)

    A term e is closed if FV (e) = /0A variable that is not free is bound

    Introduction to lambda calculus . 8/

  • 8/8/2019 Lambda Calculus Slides

    9/33

    -conversion

    Terms are equivalent up to renaming of bound variables x.e = y.e[ y/ x] if y /FV (e)Renaming of bound variables is called -conversion

    Used to avoid having duplicate variables, capturingduring substitution

    Introduction to lambda calculus . /

  • 8/8/2019 Lambda Calculus Slides

    10/33

    Substitution

    Formal denition

    x[e/ x] = e y[e/ x] = y when x = y

    (e1 e2)[e/ x] = (e1[e/ x] e2[e/ x])( y.e1)[e/ x] = y.(e1[e/ x]) when y = x and y /FV (e)

    Example( x. y x) x = (w. y w) x y xWe omit writing -conversion

    Introduction to lambda calculus . 10/

  • 8/8/2019 Lambda Calculus Slides

    11/33

    Functions with many arguments

    We cant yet write functions with many argumentsFor example, two arguments: ( x, y).e

    Solution: take the arguments, one at a time

    x. y.eA function that takes x and returns another function thattakes y and returns e

    ( x. y.e) a b ( y.e[a / x]) b e[a / x][b/ y]This is called Currying Can represent any number of arguments

    Introduction to lambda calculus . 11/

  • 8/8/2019 Lambda Calculus Slides

    12/33

    Representing booleans

    true = x. y. xfalse = x. y. yif a then b else c = a b cFor example:

    if true then b else c ( x. y. x) b c ( y.b) c bif false then b else c ( x. y. y) b c ( y. y) c c

    Introduction to lambda calculus . 12/

  • 8/8/2019 Lambda Calculus Slides

    13/33

    Combinators

    Any closed term is also called a combinator true and false are combinators

    Other popular combinators:

    I = x. xK = x. y. xS = x. y. z. x z ( y z)

    We can dene calculi in terms of combinatorsThe SKI-calculusSKI-calculus is also Turing-complete

    Introduction to lambda calculus . 13/

  • 8/8/2019 Lambda Calculus Slides

    14/33

    Encoding pairs

    (a , b) = x.if x then a else bfst = p. p truesnd = p. p falseThen

    fst (a , b) ... asnd (a , b) ... b

    Introduction to lambda calculus . 14/

  • 8/8/2019 Lambda Calculus Slides

    15/33

    Natural numbers (Church)

    0 = x. y. y1 = x. y. xy2 = x. y. x ( x y)i.e. n = x. y. apply x n times to ysucc = z. x. y. x ( z x y)

    iszero = z. z ( y.false ) true

    Introduction to lambda calculus . 15/

  • 8/8/2019 Lambda Calculus Slides

    16/33

    Natural numbers (Scott)

    0 = x. y. x1 = x. y. y 02 = x. y. y 1i.e. n = x. y. y (n 1)succ = z. x. y. yzpred

    = z

    . z

    0 ( x

    . x

    )iszero = z. z true ( x.false )

    Introduction to lambda calculus . 16/

  • 8/8/2019 Lambda Calculus Slides

    17/33

    Nondeterministic semantics

    ( x.e1) e2 e1[e2/ x]e e

    ( x.e) ( x.e )

    e1 e1

    e1 e2 e 1 e2e2 e

    2

    e1 e2 e1 e 2Question: why is this semantics non-deterministic?

    Introduction to lambda calculus . 17/

  • 8/8/2019 Lambda Calculus Slides

    18/33

    Example

    We can apply reduction anywhere in the term( x.( y. y) x (( z.w) x) x.( x (( z.w) x) x. x w( x.( y. y) x (( z.w) x) x.( y. y) x w x. x w

    Does the order of evaluation matter?

    Introduction to lambda calculus . 18/

  • 8/8/2019 Lambda Calculus Slides

    19/33

    The Church-Rosser Theorem

    Lemma (The Diamond Property):If a b and a c, then there exists d such that b d and c d

    Church-Rosser theorem:If a b and a c, then there exists d such thatb d and c d

    Proof by diamond propertyChurch-Rosser also called conuence

    Introduction to lambda calculus . 1 /

  • 8/8/2019 Lambda Calculus Slides

    20/33

    Normal form

    A term is in normal form if it cannot be reducedExamples: x. x, x. y. z

    By the Church-Rosser theorem, every term reduces to at

    most one normal formOnly for pure lambda calculus with non-deterministicevaluation

    Notice that for function application, the argument need not bein normal form

    Introduction to lambda calculus . 20/

    i l

  • 8/8/2019 Lambda Calculus Slides

    21/33

    -equivalence

    Let = be the reexive, symmetric, transitive closure of E.g., ( x. x) y y ( z.w. z) y y so all three are-equivalent

    If a = b, then there exists c such that a

    c and b

    cFollows from Church-Rosser theorem

    In particular, if a = b and both are normal forms, then they

    are equal

    Introduction to lambda calculus . 21/

    N t t h l f

  • 8/8/2019 Lambda Calculus Slides

    22/33

    Not every term has a normal form

    Consider = x. x xThen

    In general, self application leads to loops. . . which is good if we want recursion

    Introduction to lambda calculus . 22/

    Fi i t bi t

  • 8/8/2019 Lambda Calculus Slides

    23/33

    Fixpoint combinator

    Also called a paradoxical combinatorY = f .( x. f ( x x)) ( x. f ( x x))There are many versions of the Y combinator

    Then, Y F = F (Y F )Y F = ( f .( x. f ( x x)) ( x. f ( x x))) F ( x.F ( x x)) ( x.F ( x x))

    F (( x.F ( x x)) ( x.F ( x x))) F (Y F )

    Introduction to lambda calculus . 23/

    E ample

  • 8/8/2019 Lambda Calculus Slides

    24/33

    Example

    fact (n) = if (n = 0) then 1 else nfact (n 1)Let G = f .n.if (n = 0) then 1 else nf (n 1)Y G 1 = G (Y G) 1

    = ( f .n.if (n = 0) then 1 else nf (n 1)) (Y G) 1= if (1 = 0) then 1 else 1((Y G) 0)

    = if (1 = 0) then 1 else 1(G (Y G) 0)

    = if (1 = 0) then 1 else 1( f .n.if (n = 0) then 1 else nf (n 1) (Y G) 0)

    = if (1 = 0) then 1 else 1(if (0 = 0) then 1 else 0((Y G) 0))

    = 11 = 1

    Introduction to lambda calculus . 24/

    I th d

  • 8/8/2019 Lambda Calculus Slides

    25/33

    In other words

    The Y combinator unrolls or unfolds its argument aninnite number of timesY G = G (Y G) = G (G (Y G)) = G (G (G (Y G))) = . . .G needs to have a base case to ensure termination

    But, only works because we follow call-by-nameDifferent combinator(s) for call-by-value

    Z = f .( x. f ( y. x x y)) ( x. f ( y. x x y))Why is this a xed-point combinator? How does itsdifference from Y work for call-by-value?

    Introduction to lambda calculus . 25/

    Why encodings

  • 8/8/2019 Lambda Calculus Slides

    26/33

    Why encodings

    Its fun!Shows that the language is expressive

    In practice, we add constructs as languages primitives

    More efcientMuch easier to analyze the program, avoid mistakesOur encodings of 0 and true are the same, we may want

    to avoid mixing them, for clarity

    Introduction to lambda calculus . 26/

    Lazy and eager evaluation

  • 8/8/2019 Lambda Calculus Slides

    27/33

    Lazy and eager evaluation

    Our non-deterministic reduction rule is ne for theory, butawkward to implement

    Two deterministic strategies:

    Lazy : Given ( x.e1) e2 , do not evaluate e2 if e1 does notneed x anywhereAlso called left-most, call-by-name, call-by-need,applicative, normal-order evaluation (with slightlydifferent meanings)

    Eager : Given ( x.e1) e2 , always evaluate e2 to a normalform, before applying the function

    Also called call-by-value

    Introduction to lambda calculus . 27/

    Lazy operational semantics

  • 8/8/2019 Lambda Calculus Slides

    28/33

    Lazy operational semantics

    ( x.e1) l ( x.e1)

    e1 l x.e e[e2/ x] l e

    e1 e2 l e

    The rules are deterministic, big-step The right-hand side is reduced all the way

    The rules do not reduce under The rules are normalizing:

    If a is closed and there is a normal form b such thata b, then a l d for some d

    Introduction to lambda calculus . 28/

    Eager (big-step) semantics

  • 8/8/2019 Lambda Calculus Slides

    29/33

    Eager (big-step) semantics

    ( x.e1) e ( x.e1)

    e1 e x.e e2 e e e[e / x] e e

    e1 e2 e e This big-step semantics is also deterministic and does notreduce under But is not normalizing!

    Example: let x = in ( y. y)

    Introduction to lambda calculus . 2 /

    Lazy vs eager in practice

  • 8/8/2019 Lambda Calculus Slides

    30/33

    Lazy vs eager in practice

    Lazy evaluation (call by name, call by need)Has some nice theoretical propertiesTerminates more often

    Lets you play some tricks with innite objectsMain example: Haskell

    Eager evaluation (call by value)

    Is generally easier to implement efcientlyBlends more easily with side-effectsMain examples: Most languages (C, Java, ML, . . . )

    Introduction to lambda calculus . 30/

    Functional programming

  • 8/8/2019 Lambda Calculus Slides

    31/33

    Functional programming

    The calculus is a prototypical functional programminglanguageHigher-order functions (lots!)No side-effects

    In practice, many functional programming languages are notpure: they permit side-effects

    But youre supposed to avoid them. . .

    Introduction to lambda calculus . 31/

    Functional programming today

  • 8/8/2019 Lambda Calculus Slides

    32/33

    Functional programming today

    Two main campsHaskell Pure, lazy functional language; no side-effectsML (SML, OCaml) Call-by-value, with side-effects

    Old, still around: Lisp, SchemeDisadvantage/feature: no static typing

    Introduction to lambda calculus . 32/

    Inuence of functional programming

  • 8/8/2019 Lambda Calculus Slides

    33/33

    Inuence of functional programming

    Functional ideas move to other langaugesGarbage collection was designed for Lisp; now most newlanguages use GCGenerics in C++/Java come from ML polymorphism, orHaskell type classesHigher-order functions and closures (used in Ruby, existin C#, proposed to be in Java soon) are everywhere in

    functional languagesMany object-oriented abstraction principles come fromMLs module system

    . . .

    Introduction to lambda calculus 33/