12/2/20151 GC16/3011 Functional Programming Lecture 2 The Lambda Calculus: A Simple Introduction.

Post on 13-Dec-2015

224 views 0 download

Tags:

Transcript of 12/2/20151 GC16/3011 Functional Programming Lecture 2 The Lambda Calculus: A Simple Introduction.

04/18/2304/18/23 11

GC16/3011 Functional Programming

Lecture 2

The Lambda Calculus:A Simple IntroductionA Simple Introduction

04/18/2304/18/23 22

Notices:Notices:

Beware – lecture on 9Beware – lecture on 9thth Feb cancelled Feb cancelled

04/18/2304/18/23 33

Contents

• The “assembly language” for Miranda• Very simple syntax• Rules for evaluation• Order of applying the rules• Terminology: “bound” and “free”

04/18/2304/18/23 44

• This is the “assembly language” for Functional Languages

• Very simple (few operators and few rules)

• NOT SEQUENTIAL

• A program is an expression (like arithmetic) rather than a sequence of instructions

• All it does is return a value (no side effects)

The Lambda Calculus

04/18/2304/18/23 55

program :: expression

expression :: x

| expression expression

| x . expression

a function

an argument

… applied to …

Lambda Calculus Syntax

04/18/2304/18/23 66

programprogram

expressionexpression

expressionexpression

xx

expressionexpression expressionexpression

xx .. expressionexpression

Lambda Calculus Syntax (2)

04/18/2304/18/23 77

• This minimal syntax is often extended with:This minimal syntax is often extended with:• Constants: Constants:

• values (such as 3) and operators (such as +, * ) values (such as 3) and operators (such as +, * ) • Though values and operators are often not distinguished, we will Though values and operators are often not distinguished, we will

treat them differentlytreat them differently

• Types (such as Int, Bool)Types (such as Int, Bool)• but we will not cover the typed lambda calculusbut we will not cover the typed lambda calculus

• Extra brackets for grouping (such as (x) )Extra brackets for grouping (such as (x) )

Syntax extensions

04/18/2304/18/23 88

program :: expressionprogram :: expression

expression :: xexpression :: x | constant| constant | operator| operator | expression expression| expression expression

| | x x .. expression expression | ( expression )| ( expression )

Untyped Extended Syntax

04/18/2304/18/23 99

• Functions do Functions do notnot have names! have names!• They can be defined but must be used immediatelyThey can be defined but must be used immediately

• Function arguments DO have namesFunction arguments DO have names• used inside the functionused inside the function

• Functions can be arguments to other functionsFunctions can be arguments to other functions• that way they can have names that way they can have names • and can be used many timesand can be used many times• (inside the other function)(inside the other function)

Lambda Calculus Functions

04/18/2304/18/23 1010

• To define the (anonymous) function taking one To define the (anonymous) function taking one argument (called “x”) which adds 1 to x and returns argument (called “x”) which adds 1 to x and returns the sum as its result:the sum as its result:

x . ( (+ x) 1)x . ( (+ x) 1)

• Often simplified to one of the following (since + needs Often simplified to one of the following (since + needs both arguments):both arguments):

x . ( + x 1 )x . ( + x 1 ) x . ( x + 1 )x . ( x + 1 )

Defining a function

04/18/2304/18/23 1111

program :: expressionprogram :: expression

expression :: xexpression :: x | constant| constant | operator| operator | expression expression| expression expression | expression operator expression| expression operator expression

| | x x .. expression expression | ( expression )| ( expression )

Untyped Extended Syntax (2)

04/18/2304/18/23 1212

• To apply the previously defined function to the To apply the previously defined function to the constant number 3:constant number 3:

( ( x . ( x + 1 ) ) 3 x . ( x + 1 ) ) 3

Using (“applying”) a function

04/18/2304/18/23 1313

reductionreduction

reductionreduction

reduction reduction ( if x not free in E)( if x not free in E)

x . Ex . E y . E [ y / x ]y . E [ y / x ]

x . E ) z x . E ) z E [ z / x ]E [ z / x ]

x . ( E x )x . ( E x ) E E

Rules for Evaluation (1)

04/18/2304/18/23 1414

rulesrules there is a separate rule for evaluating each primitive there is a separate rule for evaluating each primitive

operator (such as +)operator (such as +) Example: the rule for + says that 3 + 4 evaluates to Example: the rule for + says that 3 + 4 evaluates to

77 Name clashesName clashes::

x . ( ( x . ( ( x . ( x + 3 ) ) ( x + 4 ) ) ) 5x . ( x + 3 ) ) ( x + 4 ) ) ) 5

x . ( 5 + 3 ) ) ( 5 + 4 )x . ( 5 + 3 ) ) ( 5 + 4 ) WRONG!!!WRONG!!!

Rules for Evaluation (2)

04/18/2304/18/23 1515

• Normal Order ReductionNormal Order Reduction• ““leftmost-outermost” firstleftmost-outermost” first• guaranteed to terminate (if termination possible)guaranteed to terminate (if termination possible)

• Other possible reduction ordersOther possible reduction orders• applicative orderapplicative order• parallel reductionparallel reduction

• All strategies guaranteed to give same result (caveat All strategies guaranteed to give same result (caveat termination): termination): “Normal Form”“Normal Form” !!!!!! !!!!!!

Reduction Orders

04/18/2304/18/23 1616

( 5 + 3 )( 5 + 3 )

88

using theusing therule forrule for

++

Lambda Calculus Examples (1)

04/18/2304/18/23 1717

( ( x . ( x + 3 ) ) 5x . ( x + 3 ) ) 5

( 5 + 3 )( 5 + 3 )

usingusingreductionreduction

88

using theusing therule forrule for

++

Lambda Calculus Examples (2)

04/18/2304/18/23 1818

x . ( ( x . ( ( y . ( x + y ) ) 3 ) ) 5y . ( x + y ) ) 3 ) ) 5

( ( y . ( 5 + y ) ) 3y . ( 5 + y ) ) 3

usingusingreductionreduction

88

as beforeas before

Lambda Calculus Examples (3)

04/18/2304/18/23 1919

x . ( ( x . ( ( x . ( x + 3 ) ) x ) ) 5x . ( x + 3 ) ) x ) ) 5

( ( x . ( x + 3 ) ) 5x . ( x + 3 ) ) 5

y . ( ( y . ( ( x . ( x + 3 ) ) y ) ) 5x . ( x + 3 ) ) y ) ) 5

Lambda Calculus Examples (4)

04/18/2304/18/23 2020

x . ( x 5 ) ) ( x . ( x 5 ) ) ( x . ( x + 3 ) ) x . ( x + 3 ) )

( 5 + 3 )( 5 + 3 )

x . ( x + 3 ) ) 5 )x . ( x + 3 ) ) 5 )

Lambda Calculus Examples (5)

04/18/2304/18/23 2121

x . ( ( x 5 ) + ( x 4 ) ) ( x . ( ( x 5 ) + ( x 4 ) ) ( x . ( x + 3 ) )) x . ( x + 3 ) ))

x . ( x + 3 ) ) 5 ) + ( ( x . ( x + 3 ) ) 5 ) + ( ( x . ( x + 3 ) ) 4 )x . ( x + 3 ) ) 4 )

( 5 + 3 ) + ( ( ( 5 + 3 ) + ( ( x . ( x + 3 ) ) 4 )x . ( x + 3 ) ) 4 )

Lambda Calculus Examples (6)

04/18/2304/18/23 2222

• BindingBinding• A BINDING links a name to a valueA BINDING links a name to a value• This happens whenever a function is appliedThis happens whenever a function is applied

• Bound and Not Bound (a.k.a. “Free”)Bound and Not Bound (a.k.a. “Free”)• In ( In ( x . ( x + y ) ) we say that x is BOUND x . ( x + y ) ) we say that x is BOUND

and y is NOT BOUND (or “FREE”)and y is NOT BOUND (or “FREE”)• This is because we know what x must be - it is This is because we know what x must be - it is

the function argument. “y” is unknown.the function argument. “y” is unknown.

Terminology

04/18/2304/18/23 2323

• The “assembly language” for MirandaThe “assembly language” for Miranda• Very simple syntaxVery simple syntax• Only Four rules for evaluationOnly Four rules for evaluation• Apply the rules in any orderApply the rules in any order

• Caveat terminationCaveat termination

• ““Normal Order” guaranteed to terminateNormal Order” guaranteed to terminate• if termination is possibleif termination is possible

• Terminology: “bound” and “free”Terminology: “bound” and “free”

Summary