Concepts of Higher Programming Languages Chapter 2: A Taste of - - PowerPoint PPT Presentation

concepts of higher programming languages chapter 2 a
SMART_READER_LITE
LIVE PREVIEW

Concepts of Higher Programming Languages Chapter 2: A Taste of - - PowerPoint PPT Presentation

Concepts of Higher Programming Languages Chapter 2: A Taste of Functional Programming and Haskell Jonathan Thaler Department of Computer Science 1 / 24 What is Functional Programming? A style of programming with the basic method of computation


slide-1
SLIDE 1

Concepts of Higher Programming Languages Chapter 2: A Taste of Functional Programming and Haskell

Jonathan Thaler

Department of Computer Science

1 / 24

slide-2
SLIDE 2

What is Functional Programming?

A style of programming with the basic method of computation is application of functions to arguments. Functions are first-class citizens

Can be assigned to variables Can be passed as arguments to other functions Can be constructed as return values from functions.

Declarative style of programming: describing what to compute instead of how. A functional language supports and encourages the functional style. Built on foundations of the Lambda Calculus.

2 / 24

slide-3
SLIDE 3

Lambda Calculus

Is a notation for expressing computation based on the concepts of:

  • 1. Function abstraction
  • 2. Function application
  • 3. Variable binding
  • 4. Variable substitution

Calculus A calculus is a notation that can be manipulated mechanically to achieve some end.

3 / 24

slide-4
SLIDE 4

Function Abstraction

Function Abstraction Function abstraction allows to define functions in the Lambda Calculus. The λ symbol denotes an expression of a function which takes exactly one argument which is used in the body-expression of the function to calculate something which is then the result. Examples f(x) = x2 − 3x + a Lambda Expression: λx.x2 − 3x + a f(x, y) = x2 + y2 Lambda Expression: λx.λy.x2 + y2

4 / 24

slide-5
SLIDE 5

Function Application

Function Application To get the result of a function one applies arguments to it. Examples x = 3, y = 4 f(x, y) = x2 + y2 f(3, 4) = 25 λx.λy.x2 + y2 ((λx.λy.x2 + y2) 3) 4 = 25

5 / 24

slide-6
SLIDE 6

Variable Substitution

Variable Substitution To compute the result of a Lambda expression (evaluating the expression) it is necessary to subsitute the bound variables by the argument to the function. This process is called β-reduction. β-reduction ((λx.λy.x2 + y2) 3) 4 {substitute x with 4} (λy.42 + y2) 3 {substitute y with 3} (42 + 32) = 25

6 / 24

slide-7
SLIDE 7

Variable Substitution

β-reduction ((λx.λy.x2 + y2)3)y {substitute x with y?} (λy.y2 + y2)3 = 32 + 32 = 18 No, we need to perform α-conversion!

7 / 24

slide-8
SLIDE 8

Variable Binding

Definition In the Lambda expression λx.x2 − 3x + a the variable x is bound in the body of the function whereas a is said to be free. A bound variable can be renamed within its scope without changing the meaning

  • f the expression: λy.y2−3y+a has the same meaning as the expression λx.x2−

3x + a. A free variable must not be renamed because it would change the meaning of the expression. This process is called α-conversion and it becomes sometimes necessary to avoid name-conflicts in variable substitution.

8 / 24

slide-9
SLIDE 9

Variable Substitution

β-reduction with α-conversion ((λx.λy.x2 + y2) 3) y {α-conversion: rename bound y to z} ((λx.λz.x2 + z2) 3) y {subsitute x with free y} (λz.y2 + z2) 3 {subsitute z with 3} (y2 + 32) 3 = y2 + 9 The result is actually a new function!

9 / 24

slide-10
SLIDE 10

Lambda Calculus

Function Examples Identity Function (λx.x) (λx.x) 42 = 42 Constant Function (λx.y) (λx.y) 42 = y ? Function (λx.xx) (λx.xx) 42 = 42 42 (λx.xx)(λx.xx) = (λx.xx)(λx.xx)

10 / 24

slide-11
SLIDE 11

Imperative Programming

Summing the integers 1 to 10 in Java int total = 0; for (int i = 1; i <= 10; i++) total = total + i; The computation method is destructive variable assignment.

11 / 24

slide-12
SLIDE 12

Declarative Programming

Summing the integers 1 to 10 in Haskell sum [1..10] The computation method is function application.

12 / 24

slide-13
SLIDE 13

Historical Background

1930s: Alonzo Church develops the lambda calculus, a simple but powerful theory of functions.

13 / 24

slide-14
SLIDE 14

Historical Background

1950s: John McCarthy develops Lisp, the first functional language, with some influences from the lambda calculus, but retaining variable assignments.

14 / 24

slide-15
SLIDE 15

Historical Background

1960s: Peter Landin develops ISWIM, the first pure functional language, based strongly on the Lambda Calculus, with no assignments.

15 / 24

slide-16
SLIDE 16

Historical Background

1970s: John Backus develops FP, a functional language that emphasizes higher-order functions and reasoning about programs.

16 / 24

slide-17
SLIDE 17

Historical Background

1970s: Robin Milner and others develop ML, the first modern functional language, which introduced type inference and polymorphic types.

17 / 24

slide-18
SLIDE 18

Historical Background

1970s - 1980s: David Turner develops a number of lazy functional languages, culminating in the Miranda system.

18 / 24

slide-19
SLIDE 19

Historical Background

1987: An international committee starts the development of Haskell, a standard lazy functional language.

19 / 24

slide-20
SLIDE 20

Historical Background

1990s: Phil Wadler and others develop Type Classes and Monads, two of the main innovations of Haskell.

20 / 24

slide-21
SLIDE 21

Historical Background

2003: The committee publishes the Haskell Report, defining a stable version of the language; an updated version was published in 2010.

21 / 24

slide-22
SLIDE 22

Historical Background

2010-date: Standard distribution, library support, new language features, development tools, use in industry, influence on other languages, etc.

22 / 24

slide-23
SLIDE 23

Historical Background

Future Haskell will take over the world of software programming.

23 / 24

slide-24
SLIDE 24

A Taste of Haskell

What is this code doing? f [] = [] f (x:xs) = f ys ++ [x] ++ f zs where ys = [a | a <- xs, a <= x] zs = [b | b <- xs, b > x]

24 / 24