Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "12/2/20151 GC16/3011 Functional Programming Lecture 2 The Lambda Calculus: A Simple Introduction."— Presentation transcript:

1

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

3 12/2/20152 Notices: ä Beware – lecture on 9 th Feb cancelled

4 12/2/20153 Contents The “assembly language” for Miranda Very simple syntax Rules for evaluation Order of applying the rules Terminology: “bound” and “free”

5 12/2/20154 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

6 12/2/20155 program :: expression expression :: x | expression expression | x. expression a function an argument … applied to … Lambda Calculus Syntax

7 12/2/20156 program expression expression x expressionexpression x. expression Lambda Calculus Syntax (2)

8 12/2/20157 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 treat them differently Though values and operators are often not distinguished, we will treat them differently Types (such as Int, Bool) Types (such as Int, Bool) but we will not cover the typed lambda calculus but we will not cover the typed lambda calculus Extra brackets for grouping (such as (x) ) Extra brackets for grouping (such as (x) ) Syntax extensions

9 12/2/20158 program :: expression expression :: x | constant | constant | operator | operator | expression expression | expression expression | x. expression | x. expression | ( expression ) | ( expression ) Untyped Extended Syntax

10 12/2/20159 Functions do not have names! Functions do not have names! They can be defined but must be used immediately They can be defined but must be used immediately Function arguments DO have names Function arguments DO have names used inside the function used inside the function Functions can be arguments to other functions Functions can be arguments to other functions that way they can have names that way they can have names and can be used many times and can be used many times (inside the other function) (inside the other function) Lambda Calculus Functions

11 12/2/201510 To define the (anonymous) function taking one argument (called “x”) which adds 1 to x and returns the sum as its result: To define the (anonymous) function taking one argument (called “x”) which adds 1 to x and returns the sum as its result:  x. ( (+ x) 1) Often simplified to one of the following (since + needs both arguments): Often simplified to one of the following (since + needs both arguments):  x. ( + x 1 )  x. ( x + 1 ) Defining a function

12 12/2/201511 program :: expression expression :: x | constant | constant | operator | operator | expression expression | expression expression | expression operator expression | expression operator expression | x. expression | x. expression | ( expression ) | ( expression ) Untyped Extended Syntax (2)

13 12/2/201512 To apply the previously defined function to the constant number 3: To apply the previously defined function to the constant number 3: (  x. ( x + 1 ) ) 3 Using (“applying”) a function

14 12/2/201513   reduction   reduction   reduction ( if x not free in E)  x. E  y. E [ y / x ]  x. E ) z E [ z / x ]  x. ( E x ) E Rules for Evaluation (1)

15 12/2/201514   rules  there is a separate rule for evaluating each primitive operator (such as +)  Example: the rule for + says that 3 + 4 evaluates to 7  Name clashes :  x. ( (  x. ( x + 3 ) ) ( x + 4 ) ) ) 5  x. ( 5 + 3 ) ) ( 5 + 4 ) WRONG!!! Rules for Evaluation (2)

16 12/2/201515 Normal Order Reduction Normal Order Reduction “leftmost-outermost” first “leftmost-outermost” first guaranteed to terminate (if termination possible) guaranteed to terminate (if termination possible) Other possible reduction orders Other possible reduction orders applicative order applicative order parallel reduction parallel reduction All strategies guaranteed to give same result (caveat termination): “Normal Form” !!!!!! All strategies guaranteed to give same result (caveat termination): “Normal Form” !!!!!! Reduction Orders

17 12/2/201516 ( 5 + 3 ) 8 using the  rule for + Lambda Calculus Examples (1)

18 12/2/201517 (  x. ( x + 3 ) ) 5 ( 5 + 3 ) using  reduction 8 using the  rule for + Lambda Calculus Examples (2)

19 12/2/201518  x. ( (  y. ( x + y ) ) 3 ) ) 5 (  y. ( 5 + y ) ) 3 using  reduction 8 as before Lambda Calculus Examples (3)

20 12/2/201519  x. ( (  x. ( x + 3 ) ) x ) ) 5 (  x. ( x + 3 ) ) 5  y. ( (  x. ( x + 3 ) ) y ) ) 5   Lambda Calculus Examples (4)

21 12/2/201520  x. ( x 5 ) ) (  x. ( x + 3 ) ) ( 5 + 3 )  x. ( x + 3 ) ) 5 )   Lambda Calculus Examples (5)

22 12/2/201521  x. ( ( x 5 ) + ( x 4 ) ) (  x. ( x + 3 ) ))  x. ( x + 3 ) ) 5 ) + ( (  x. ( x + 3 ) ) 4 )   ( 5 + 3 ) + ( (  x. ( x + 3 ) ) 4 ) Lambda Calculus Examples (6)

23 12/2/201522 Binding Binding A BINDING links a name to a value A BINDING links a name to a value This happens whenever a function is applied This happens whenever a function is applied Bound and Not Bound (a.k.a. “Free”) Bound and Not Bound (a.k.a. “Free”) In (  x. ( x + y ) ) we say that x is BOUND and y is NOT BOUND (or “FREE”) In (  x. ( x + y ) ) we say that x is BOUND and y is NOT BOUND (or “FREE”) This is because we know what x must be - it is the function argument. “y” is unknown. This is because we know what x must be - it is the function argument. “y” is unknown. Terminology

24 12/2/201523 The “assembly language” for Miranda The “assembly language” for Miranda Very simple syntax Very simple syntax Only Four rules for evaluation Only Four rules for evaluation Apply the rules in any order Apply the rules in any order Caveat termination Caveat termination “Normal Order” guaranteed to terminate “Normal Order” guaranteed to terminate if termination is possible if termination is possible Terminology: “bound” and “free” Terminology: “bound” and “free” Summary


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

Similar presentations


Ads by Google