cse-321 programming languages introduction to functional programming (part ii)

50
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 12, 2007 박박박

Upload: molimo

Post on 18-Mar-2016

44 views

Category:

Documents


0 download

DESCRIPTION

CSE-321 Programming Languages Introduction to Functional Programming (Part II). 박성우. POSTECH March 12, 2007. Outline. Expressions and values V Variables V Functions V Types Polymorphism Recursion Datatypes Pattern matching Higher-order functions Exceptions Modules. f. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

CSE-321 Programming Languages

Introduction to Functional Programming(Part II)

POSTECHMarch 12, 2007

박성우

Page 2: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

2

Outline• Expressions and values V• Variables V• Functions V• Types

– Polymorphism• Recursion• Datatypes• Pattern matching• Higher-order functions• Exceptions• Modules

Page 3: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

3

What is the Type of ?

true,

false)

(

fn f => (f true, f false)

f

f

f

All we know about f is that it takes booleans as arguments.

Page 4: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

4

f

All we know about f is that it takes booleans as arguments.

bool

?

Page 5: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

5

f : bool -> ?

fn f => (f true, f false) : (bool -> ?) -> ? * ?

Page 6: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

6

f : bool -> 'a

fn f => (f true, f false) : (bool -> 'a) -> 'a * 'a

• 'a– type variable– usually read as alpha– means 'for any type alpha'

Page 7: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

7

Polymorphic Types• Types involving type variables 'a, 'b, 'c, ...• E.g.

– fn x => x : 'a -> 'a

– fn x => fn y => (x, y) : 'a -> 'b -> ('a * 'b)

– fn (x : 'a) => fn (y : 'a) => x = y :'a -> 'a -> bool(* actually does not typecheck! *)

Page 8: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

8

Equality Types• Motivation

– Equality (=) is not defined on every type.– E.g.

• comparing two functions for equality?

• Type variables with equality– ''a, ''b, ''c, ...– ''a means 'for any type alpha

for which equality is defined'– fn (x : ''a) => fn (y : ''a) => x = y :

''a -> ''a -> bool

Page 9: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

9

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

Page 10: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

10

Recursion vs. Iteration• Recursion in SML

fun sum n =if n = 0 then 0else sum (n - 1) + n

• Iteration in C

int i, sum;for (i = 0, sum = 0;

i <= n; i++)

sum += n;

• Recursion is not an awkward toolif you are used to functional programming.

• Recursion seems elegant but inefficient!

Page 11: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

11

Recursion in Actionfun sum n =

if n = 0 then 0else sum (n - 1) + n

call stack

evaluationf 10

f 8f 9

...

f 1f 0

55

36 + 945 + 10

...0 + 1

1 + 2

0further computation

Page 12: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

12

Funny Recursionfun zero n =

if n = 0 then 0else zero (n - 1)

call stack

evaluationf 10

f 9f 8

...

f 1f 0

00

0

00

...0

no further computation

Page 13: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

13

Funny Recursion Optimizedfun zero n =

if n = 0 then 0else zero (n - 1)

call stack

evaluationf 10

f 9f 8

...

f 1f 0

000

00

...

0

Page 14: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

14

Funny Recursion Further Optimizedfun zero n =

if n = 0 then 0else zero (n - 1)

call stack

evaluationf 10 f 9 f 8 .

.

.

f 1 f 0 0

Page 15: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

15

Tail Recursive Function• A tail recursive function f:

– A recursive call to f is the last step in evaluating the function body.

– That is, no more computation remains after calling f itself.

• A tail recursive call needs no stack!• A tail recursive call is as efficient as iteration!

Page 16: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

16

Example• Tail recursive sumfun sum' accum k =

if k = 0 then accumelse sum' (accum + k) (k - 1)

fun sum n = sum' 0 n

• Non-tail recursive sumfun sum n =

if n = 0 then 0else sum (n - 1) + n

• Think about the invariant of sum:– given:

sum' accum k– invariant:

accum = n + (n - 1) + ... (k + 1)

Page 17: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

17

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

Page 18: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

18

Enumeration Types in Cenum shape { Circle, Rectangle, Triangle};

• Great flexibility– e.g.

Circle + 1 == RectangleTriangle - 1 == Rectangle(Circle + Triangle) / 2 == Rectangle

• But is this good or bad?

Page 19: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

19

Datatypes in SMLdatatype shape = Circle | Rectangle | Triangle

• No flexibility– e.g.

Circle + 1 (x)Triangle - 1 (x)Circle + Triangle (x)

• But high safety.

Page 20: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

20

Setdatatype set =

Empty | Many

- Empty;val it = Empty : set- Many;val it = Many : set

Page 21: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

21

Set with Argumentsdatatype set =

Empty | Many of int

- Many 5;val it = Many 5 : set

Page 22: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

22

Set with Type Parametersdatatype 'a set =

Empty | Singleton of 'a | Pair of 'a * 'a

- Singleton 0;val it = Singleton 0 : int set- Pair (0, 1);val it = Pair (0, 1) : int set

Page 23: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

23

Set with Type Parametersdatatype 'a set =

Empty | Singleton of 'a | Pair of 'a * 'a

- Pair (Singleton 0, Pair (0, 1))val it = Pair (Singleton 0, Pair (0, 1)) :

int set set

Page 24: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

24

Set with Type Parametersdatatype 'a set =

Empty | Singleton of 'a | Pair of 'a * 'a

- Pair (0, true);stdIn:27.1-27.15 Error: operator and operand

don't agree [literal] operator domain: int * int operand: int * bool in expression: Pair (0,true)

Page 25: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

25

Recursive Set with Type Parametersdatatype 'a set =

Empty | NonEmpty of 'a * 'a set

• This is essentially the definition of datatype list.

Page 26: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

26

Datatype listdatatype 'a list =

nil | :: of 'a * 'a list

- nil;val it = [] : 'a list- 2 :: nil; (* :: infix *)val it = [2] : int list- 1 :: (2 :: nil);val it = [1,2] : int list- 1 :: 2 :: nil; (* :: right associative *)val it = [1,2] : int list

Page 27: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

27

Datatype listdatatype 'a list =

nil | :: of 'a * 'a list

- 1 :: [2] :: nil;- [1] :: 2 :: nil;- [1] :: [2] :: nil;val it = [[1],[2]] : int list list- [1] :: [[2]];val it = [[1],[2]] : int list list

(X)(X)

Page 28: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

28

Using Datatypes• We know how to create values of various datatypes:

– shape– set– int set– int list– ...

• But how do we use them in programming?• What is the point of creating datatype values that

are never used?

Page 29: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

29

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

Page 30: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

30

Simple Patterndatatype shape = Circle | Rectangle |

Triangle

(* convertToEnum : shape -> int *)fun convertToEnum (x : shape) : int =

case x ofCircle => 0

| Rectangle => 1| Triangle => 2

Page 31: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

31

Pattern with Argumentsdatatype set =

Empty | Many of int

fun size (x : set) : int =case x ofEmpty => 0

| Many n => n

Page 32: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

32

Wildcard Pattern _ : "don't care"datatype 'a set =

Empty | Singleton of 'a | Pair of 'a * 'a

fun isEmpty (x : 'a set) : bool =case x ofEmpty => true

| _ => false

Page 33: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

33

Pattern with Type Annotationdatatype 'a list =

nil | :: of 'a * 'a list

fun length (x : 'a list) : int =case x of

(nil : 'a list) => 0| (_ : 'a) :: (tail : 'a list) =>

1 + length tail

Page 34: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

34

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

Page 35: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

35

Higher-order Functions• Take functions as arguments.• Return functions as the result.

Page 36: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

36

Why "Higher-order"?• T0 ::= int | bool | real | unit | ...• T1 ::= T0 -> T0 | T0 (* 1st order *)• T2 ::= T1 -> T1 | T1 (* 2nd order *)• T3 ::= T2 -> T2 | T2 (* higher

order *)• ...

Page 37: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

37

Higher-order Functions in List• val exists : ('a -> bool) -> 'a list -> bool• val all : ('a -> bool) -> 'a list -> bool• val map : ('a -> 'b) -> 'a list -> 'b list• val filter : ('a -> bool) -> 'a list -> 'a list• val app : ('a -> unit) -> 'a list -> unit

(* printInt : int -> unit *)fun printInt i = TextIO.print ((Int.toString i) ^ "\n" );List.app printInt [1, 2, 3];

Page 38: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

38

List Fold Function• foldl : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b

foldr : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b

• foldl f b0 [a0, a1, a2, ..., an-1]

b0

a0

f b1

a1

f b2

an-1

f bn

a2

f b3 bn-2

an-2

f bn-1...

...

Page 39: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

39

Summationfun sum (l : int list) =

List.foldl (fn (a, accum) => a + accum) 0 l

• foldl op+ 0 [a0, a1, a2, ..., an-1]

0

a0

+ ¢

a1

+ ¢

an-1

+

a2

+ ¢ ¢

an-2

+ ¢ ...

...

op+

Page 40: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

40

List Reversalfun reverse (l : 'a list) =

List.foldl (fn (a, rev) => a :: rev) nil l

fun reverse (l : 'a list) =List.foldl op:: nil l

• Whenever you need iterations over lists,first consider foldl and

foldr.

Page 41: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

41

More ExamplesList.exists f l =

List.foldl (fn (a, b) => b orelse f a) false lList.all f l =

List.foldl (fn (a, b) => b andalso f a) true lList.app f l =

List.foldl (fn (a, _) => f a) () l List.map f l =

List.foldr (fn (a, b) => f a :: b) nil lList.filter f l =

List.foldr (fn (a, b) => if f a then a :: b else b) nil l

Page 42: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

42

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

– exception: please see the course notes.• Modules

Page 43: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

43

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

Page 44: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

44

Structures and Signatures• Structure

– collection of type declarations, exceptions, values, and so on.

structure Set =struct

type 'a set = 'a listval emptySet = nilfun singleton x = [x]fun union s1 s2 = s1 @ s2

end

• Signature– conceptually type of

structures.

signature SET =sig

type 'a setval emptySet : 'a setval singleton : 'a -> 'a setval union : 'a set -> 'a set -> 'a set

end

Page 45: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

45

Structures + Signatures• Transparent constraint

– Type definition is exported to the outside.

structure Set : SET =struct

type 'a set = 'a listval emptySet = nilfun singleton x = [x]fun union s1 s2 = s1 @ s2

end

- Set.singleton 1 = [1];val it = true : bool

• Opaque constraint– No type definition is

exported.structure Set :> SET =struct

type 'a set = 'a listval emptySet = nilfun singleton x = [x]fun union s1 s2 = s1 @ s2

end

- Set.singleton 1 = [1];(* Error! *)

Page 46: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

46

We need to test your 46 structures!structure ahwbestOne :> HW_ONE = ...structure bopyOne :> HW_ONE = ...structure diozzOne :> HW_ONE = structure freeOne :> HW_ONE = ......structure zistinOne :> HW_ONE = ...

• How can we test 46 structures? ) They all conform to the same signature. ) We use a functor!

Page 47: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

47

Functors• Functions on structures

– takes a structure as an argument– returns a structure as the result

structure

structure

Page 48: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

48

HW1 Test Functorsignature HW_TEST =sig

val score : intend

structure S = HwSolOne

functor Hw1TestFn (P : HW_ONE val name : string) : HW_TEST =

structopen P...val sumScore = test "sum" 5 sum S.sum [1, 5, 10]

val facScore = test "fac" 5 fac S.fac [1, 5, 10 ] val gcdScore = test "gcd" 4 gcd S.gcd [(15,20), (24,12), (10000,0)]

... val cartprodScore = test "cartprod" 8 (cartprod [1,2,3]) (S.cartprod [1,2,3]) [["a","b","c","d"]]

val score = sumScore + facScore + ... + cardprodScoreval _ = print ("\n " ^ name ^ ", score : " ^ (Int.toString score) ^ "\n" )...

end

Page 49: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

49

HW1 Test Functor in Actionstructure ahwbestOneTest =

Hw1TestFn (structure P = ahwbestOne val name = "ahwbest")

structure bopyOneTest = Hw1TestFn (structure P = bopyOne

val name = "bopy")structure diozzOneTest =

Hw1TestFn (structure P = diozzOneval name = "diozz")

structure freeOneTest = Hw1TestFn (structure P = freeOne

val name = "free")...structure zistinOneTest =

Hw1TestFn (structure P = zistinOneval name = "zistin")

Page 50: CSE-321 Programming Languages Introduction to Functional Programming (Part II)

50

So it is YOU who will grade your homework!

Learn functors to find your score!