cse-321 programming languages introduction to functional programming

62
CSE-321 Programming Languages Introduction to Functional Programming POSTECH March 8, 2006 박박박

Upload: marek

Post on 07-Jan-2016

107 views

Category:

Documents


1 download

DESCRIPTION

CSE-321 Programming Languages Introduction to Functional Programming. 박성우. POSTECH March 8, 2006. Programming Paradigms. Structural programming C, Pascal, … Object-oriented programming C++, Java, … Logic programming Prolog, … Functional programming - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSE-321 Programming Languages Introduction to Functional Programming

CSE-321 Programming Languages

Introduction to Functional Programming

POSTECH

March 8, 2006

박성우

Page 2: CSE-321 Programming Languages Introduction to Functional Programming

2

Programming Paradigms• Structural programming

– C, Pascal, …• Object-oriented programming

– C++, Java, …• Logic programming

– Prolog, …• Functional programming

– SML, Haskell, Objective Caml, Lisp, Scheme, …

Page 3: CSE-321 Programming Languages Introduction to Functional Programming

3

Outline• Expressions and values• Variables• Functions• Types• Recursion• Datatypes• Pattern matching• Higher-order functions• Exceptions• Modules

Page 4: CSE-321 Programming Languages Introduction to Functional Programming

4

C• A program consists of commands.

– command = “do something”

• Nothing wrong:

if (x == 1) thenx = x + 1;

elsex = x - 1;

• Nothing wrong either:

if (x == 1) thenx = x + 1;

Page 5: CSE-321 Programming Languages Introduction to Functional Programming

5

SML• A program consists of expressions.

– expression = “obtain a value”

• Nothing wrong:if (x = 1) then

x + 1else

x - 1

• But this does not make sense:if (x = 1) then

x + 1– what is the value if x <> 1?

Page 6: CSE-321 Programming Languages Introduction to Functional Programming

6

Evaluation

Expression Value

• An expression “evaluates” to a value.• We “evaluate” an expression to obtain a value.

Page 7: CSE-321 Programming Languages Introduction to Functional Programming

7

Integer Evaluation

1 + 1 2

1 - 1 0

…1 * 1 1

Page 8: CSE-321 Programming Languages Introduction to Functional Programming

8

Boolean Evaluation

1 = 1 true

1 <> 1 false

…1 <> 0 true

Page 9: CSE-321 Programming Languages Introduction to Functional Programming

9

An Integer Expression

if 1 = ~1 then 10 else ~10

~10

if false then 10 else ~10

Page 10: CSE-321 Programming Languages Introduction to Functional Programming

10

Values as Expressions

1 ???

Page 11: CSE-321 Programming Languages Introduction to Functional Programming

11

Everything is an Expression!

• 1• ~1• 1 = ~1• 10• ~10• if 1 = ~1 then 10 else ~10

if 1 = ~1 then 10 else ~10

Page 12: CSE-321 Programming Languages Introduction to Functional Programming

12

Actually Not Everything

• Ill-formed expressions

– if 1 = ~1 then 10 (x)

– if 1 = ~1 then 10 else ~10(x)

if 1 = ~1 then 10 else ~10

Page 13: CSE-321 Programming Languages Introduction to Functional Programming

13

Outline• Expressions and values V• Variables• Functions• Types• Recursion• Datatypes• Pattern matching• Higher-order functions• Exceptions• Modules

Page 14: CSE-321 Programming Languages Introduction to Functional Programming

14

Variable Declaration- val x = 1 + 1;val x = 2 : int

• A variable x is “bound” to value 2.• From now on, any occurrence of x is replaced by 2.

- val y = x + x;val y = 4 : int

Page 15: CSE-321 Programming Languages Introduction to Functional Programming

15

Local Declaration

letval x = 1val y = x + xval z = y + y

inz + z

end

8

Page 16: CSE-321 Programming Languages Introduction to Functional Programming

16

Nested Local Declaration

letval x = 1

inx + x

end

letval y = <expression>

iny + y

end

letval y = let val x = 1 in x + x end

iny + y

end

Page 17: CSE-321 Programming Languages Introduction to Functional Programming

17

Why “Local”?

letval y = let

val x = 1 in x + x end

inx + y

end

okay???

Page 18: CSE-321 Programming Languages Introduction to Functional Programming

18

Variables are NOT variable.• The contents of a variable never change.• Surprise?

– That’s because you are thinking about variables in imperative programming.

variables in SML <> variables in C

• Once you get used to functional programming,immutability of variables in functional programming will be taken for granted!

Page 19: CSE-321 Programming Languages Introduction to Functional Programming

19

Then Why Variables?• Any advantage in using variables at all?

letval x = 1val y = x + xval z = y + y

inz + z

end

((1 + 1) + (1 + 1)) + ((1 + 1) + (1 + 1))VS.

What if it takes 10 hours to evaluate 1?

Page 20: CSE-321 Programming Languages Introduction to Functional Programming

20

Outline• Expressions and values V• Variables V• Functions• Types• Recursion• Datatypes• Pattern matching• Higher-order functions• Exceptions• Modules

Page 21: CSE-321 Programming Languages Introduction to Functional Programming

21

When is the first time you learned the concept of

function?

Page 22: CSE-321 Programming Languages Introduction to Functional Programming

22

즐거운 낱말 공부 시간• 함수 ( 函數 ): 두 변수 x, y 간에 어떤 관계가 있어 x

의 변화에 따라 y 가 일정한 법칙으로 변화할 때 y를 x 의 함수라 함 . (function)

( 동아 마스타 국어사전 )

Page 23: CSE-321 Programming Languages Introduction to Functional Programming

23

즐거운 한자 공부 시간

• 函1. 함 ( 함 ).

2. 편지 ( 함 )

3. 갑옷 ( 함 )

4. 넣을 , 들일 ( 함 )

예 :

( 書函 ) 서함 : 책을 넣는 상자

Page 24: CSE-321 Programming Languages Introduction to Functional Programming

24

Function = 函數 = Box Number!

Page 25: CSE-321 Programming Languages Introduction to Functional Programming

25

Using a Box Number

Page 26: CSE-321 Programming Languages Introduction to Functional Programming

26

Using a Box Number - Generalized

Page 27: CSE-321 Programming Languages Introduction to Functional Programming

27

Function in SML = Box Number

(fn x => x + 1) n

=

Page 28: CSE-321 Programming Languages Introduction to Functional Programming

28

Function Application

• We “apply” (fn x => x + 1) to n.• x is called a formal argument/parameter.• n is called an actual argument/parameter.

(fn x => x + 1) n

Page 29: CSE-321 Programming Languages Introduction to Functional Programming

29

Evaluating a Function Application…

(fn x => x + 1) n

n + 1

Page 30: CSE-321 Programming Languages Introduction to Functional Programming

30

Functions in SML• Nameless function

– fn x => x + 1;

• Storing a nameless function to a variable

– val incr = fn x => x + 1;

• Function declaration

– fun incr x = x + 1;

Page 31: CSE-321 Programming Languages Introduction to Functional Programming

31

Function Applications

(fn x => x + 1) 1

2

1 + 1

incr 1

Page 32: CSE-321 Programming Languages Introduction to Functional Programming

32

So far,

So good,

So easy.

Page 33: CSE-321 Programming Languages Introduction to Functional Programming

33

First-class Objects• First-class objects = primitive objects

– can be stored in a variable.– can be passed as an argument to a function.– can be returned as a return value of a function.

• Examples:– integers– booleans– characters– floating-point numbers– …

Page 34: CSE-321 Programming Languages Introduction to Functional Programming

34

First-class Objects in C• First-class objects

– integers– characters– floating-point numbers– pointers– structures– …

• Functions?– Function pointers are first-class objects.– But functions are not.

• Why? You cannot create new functions on the fly!

Page 35: CSE-321 Programming Languages Introduction to Functional Programming

35

Functions = First-class Objects in SML

• Functions:– can be passed as an argument to a function.– can be returned as a return value of a function.

Page 36: CSE-321 Programming Languages Introduction to Functional Programming

36

Box Number as Output

… …

such that

Page 37: CSE-321 Programming Languages Introduction to Functional Programming

37

Box Number as Output

x

+x

Page 38: CSE-321 Programming Languages Introduction to Functional Programming

38

x

y+x

y

Box Number as Output

Page 39: CSE-321 Programming Languages Introduction to Functional Programming

39

x

y

fn y => y+x

y+x

Box Number as Output

Page 40: CSE-321 Programming Languages Introduction to Functional Programming

40

x

y

fn y => y+x

fn y => y+x

y+x

Box Number as Output

Page 41: CSE-321 Programming Languages Introduction to Functional Programming

41

x

y

fn y => y+x

fn y => y+x

fn x => (fn y => y+x)

y+x

Box Number as Output

Page 42: CSE-321 Programming Languages Introduction to Functional Programming

42

In SML• Recall the following declarations are equivalent:

– val incr = fn x => x + 1;– fun incr x = x + 1;

• Then:

– val add = fn x => (fn y => y + x);– fun add x = fn y => y + x;– fun add x y = y + x;

• add takes only one argument, not two!• In fact, every function in SML takes only a single

argument.

Page 43: CSE-321 Programming Languages Introduction to Functional Programming

43

Adding Two Integers

(fn x => (fn y => y + x)) 1 2

3

add 1 2

(fn y => y + 1) 2

2 + 1

Page 44: CSE-321 Programming Languages Introduction to Functional Programming

44

Box Number as Input

true,

false)

(

Page 45: CSE-321 Programming Languages Introduction to Functional Programming

45

Box Number as Input

true,

false)

(

fn f => (f true, f false)

f

f

f

Page 46: CSE-321 Programming Languages Introduction to Functional Programming

46

Outline• Expressions and values V• Variables V• Functions V• Types• Recursion• Datatypes• Pattern matching• Higher-order functions• Exceptions• Modules

Page 47: CSE-321 Programming Languages Introduction to Functional Programming

47

Types• A type specifies what kind of value a given

expression evaluates to.– 1 + 1 : int– true andalso false : bool– #”A” : char– ”hello” : string– (1, true) : int * bool– (1, ~1, true) : int * int * bool– 1.0 : real– () : unit

Page 48: CSE-321 Programming Languages Introduction to Functional Programming

48

Type Preservation

Expression : T Value : T

• An evaluation preserves the type of a given expression.

• Type preservation will be discussed in detail later in this course. – It’s a lot more important than you might think!

Page 49: CSE-321 Programming Languages Introduction to Functional Programming

49

Example

letval x = 1val y = x + xval z = y + y

inz + z

end

8 : int: int

Page 50: CSE-321 Programming Languages Introduction to Functional Programming

50

Function Types• T -> T’

– type of functions:• taking arguments of type T• returning values of type T’

• Example:– val incr = fn x => x + 1;

val incr = fn : int -> int

– fun incr x = x + 1;val incr = fn : int -> int

• Explicit type annotation– val incr = fn (x:int) => x + 1;

val incr = fn (x:int) : int => x + 1;

– fun incr (x:int) = x + 1;fun incr (x:int) : int = x + 1;

Page 51: CSE-321 Programming Languages Introduction to Functional Programming

51

x

fn y => y+x

fn x => (fn y => y+x)

Type of add

Page 52: CSE-321 Programming Languages Introduction to Functional Programming

52

fn y => y+x

fn x => (fn y => y+x)

Type of add

int

Page 53: CSE-321 Programming Languages Introduction to Functional Programming

53

fn x => (fn y => y+x)

Type of add

int

int -> int

Page 54: CSE-321 Programming Languages Introduction to Functional Programming

54

int -> (int -> int)

Type of add

int

int -> int

Page 55: CSE-321 Programming Languages Introduction to Functional Programming

55

What is the Type?

true,

false)

(

fn f => (f true, f false)

f

f

f

Page 56: CSE-321 Programming Languages Introduction to Functional Programming

56

f : bool -> int ?

true,

false)

(

fn f => (f true, f false)

f

f

f

: (bool -> int) -> int * int

Page 57: CSE-321 Programming Languages Introduction to Functional Programming

57

But why is it

f : bool -> int ?

Page 58: CSE-321 Programming Languages Introduction to Functional Programming

58

Why not f : bool -> char ?

true,

false)

(

fn f => (f true, f false)

f

f

f

: (bool -> char) -> char * char

Page 59: CSE-321 Programming Languages Introduction to Functional Programming

59

Then why not

f : bool -> string ?f : bool -> int * string ?f : bool -> unit ?f : bool -> (int -> int) ?f : bool -> <crazy type> ?…

Page 60: CSE-321 Programming Languages Introduction to Functional Programming

60

So we need

Polymorphism.

Page 61: CSE-321 Programming Languages Introduction to Functional Programming

61

Polymorph in Warcraft 2

• Cost: 2000 GoldMana 200Range: 10

Time to Upgrade: 200 Time Units.

• Perhaps the most fearsome of the Mage spells, Polymorph alters the physical form of its target. This metamorphosis changes man to beast, forever warping both mind and body. This sorcery wholly transforms the victim into a creature of the wolds, thus losing their reasoning - making commands for direction or battle impossible.

• This turns any living creature into the critter for that map type such as Pig for Wasteland, Seal for Snow, Sheep for Forest, Boar for Swamp. Although the unit hasn't been killed it has been in effect killed as it cannot be turned back into the previous unit or commanded in anyway.

• Polymorph is good for instantly taking out units, especially costly and dangerous units. The primary use for Polymorph is for hit and run attacks against enemy Mages, Death Knights, and Gryphons.

• Polymorph is really helpful on defense, especially on water maps. When the enemy lands you can quickly Polymorph the most dangerous enemy units right after they unload from the Transport.

• Make a Mage with at least 200 Mana Invisible, sneak up to an enemy Mage or Dragon and Polymorph it. If it is sea, do the pop out of the Transport Polymorph then run.

• Often you can cast Polymorph before the enemy even notices it, there is no warning to enemy players that their units have been Polymorphs. Against inattentive players you might be able to Polymorph quite a few units using hit and run attacks before they catch on.

Page 62: CSE-321 Programming Languages Introduction to Functional Programming

62

Polymorphism in Warcraft 3 will be discussed in the next lecture.