Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE-321 Programming Languages Introduction to Functional Programming POSTECH 박성우.

Similar presentations


Presentation on theme: "CSE-321 Programming Languages Introduction to Functional Programming POSTECH 박성우."— Presentation transcript:

1 CSE-321 Programming Languages Introduction to Functional Programming POSTECH 박성우

2 2 Programming Paradigms Structured programming –C, Pascal, … Object-oriented programming –C++, Java, … Logic programming –Prolog, … Functional programming –SML, Haskell, Objective Caml, Lisp, Scheme, F#, …

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

4 4 Imperative Language C A program consists of commands. –command = “do something” Nothing wrong: if (x == 1) then x = x + 1; else x = x - 1; Nothing wrong either: if (x == 1) then x = x + 1;

5 5 Functional Language OCaml A program consists of expressions. –expression = “obtain a value” Nothing wrong: if (x = 1) then x + 1 else x - 1 But this does not make sense: if (x = 1) then x + 1 –what is the value if x <> 1 ?

6 6 Evaluation Expression Value An expression “evaluates” to a value. We “evaluate” an expression to obtain a value.

7 7 Integer Evaluation 1 + 1 2 1 - 1 0 … 1 * 1 1

8 8 Boolean Evaluation 1 = 1 true 1 <> 1 false … 1 <> 0 true

9 9 An Integer Expression if 1 = -1 then 10 else -10 -10 if false then 10 else -10

10 10 Values as Expressions 1 ???

11 11 Everything is an Expression! 1 1 = -1 10 -10 if 1 = -1 then 10 else -10

12 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

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

14 14 Variable Declaration - let x = 1 + 1;; val x : int = 2 A variable x is “bound” to value 2. From now on, any occurrence of x is replaced by 2. - let y = x + x;; val x : int = 4

15 15 Local Declaration 8 let x = 1 in let y = x + x in let x = y + y in z + z + x

16 16 Nested Local Declaration let x = 1 in x + x let y = in y + y let y = let x = 1 in x + x in y + y

17 17 Why “Local”? let y = let x = 1 in x + x in x + y okay???

18 18 Variables are NOT variable. The contents of a variable never change. –immutability of variables Surprise? –That’s because you are thinking about variables in imperative programming. variables in OCaml <> variables in C

19 19 Then Why Variables? Any advantage in using variables at all? ((1 + 1) + (1 + 1)) + ((1 + 1) + (1 + 1)) VS. What if it takes 10 hours to evaluate 1 ? let x = 1 in let y = x + x in let z = y + y in z + z

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

21 21 When is the first time you learned the concept of function?

22 22 함수의 사전 정의 함수 ( 函數 ): 두 변수 x, y 간에 어떤 관계가 있어 x 의 변화에 따라 y 가 일정한 법칙으로 변화할 때 y 를 x 의 함수라 함. (function) ( 동아 마스타 국어사전 )

23 23 한자 함 函 1. 함 ( 함 ). 2. 편지 ( 함 ) 3. 갑옷 ( 함 ) 4. 넣을, 들일 ( 함 ) 예 : ( 書函 ) 서함 : 책을 넣는 상자

24 24 Function = 函數 = Box Number!

25 25 Using a Box Number

26 26 Using a Box Number - Generalized … …

27 27 Function in OCaml = Box Number fun x -> x + 1 n =

28 28 Function Application We “apply” (fun x -> x + 1) to n. x is called a formal argument/parameter. n is called an actual argument/parameter. … (fun x -> x + 1) n

29 29 Evaluating a Function Application … … (fun x -> x + 1) n n + 1

30 30 Functions in OCaml Nameless function –fun x -> x + 1;; Storing a nameless function to a variable –let incr = fun x -> x + 1;; Function declaration –let incr x = x + 1;;

31 31 Function Applications (fun x -> x + 1) 1 2 1 + 1 incr 1

32 32 First-Class Stamps

33 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 –…

34 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!

35 35 Functions = First-class Objects in OCaml Functions: –can be passed as an argument to a function. –can be returned as a return value of a function.

36 36 Box Number as Output … … …… such that

37 37 Box Number as Output … … x +x+x

38 38 x y+xy+x y Box Number as Output

39 39 x y fun y -> y+x y+xy+x Box Number as Output

40 40 x y fun y -> y+x y+xy+x Box Number as Output

41 41 x y fun y -> y+x fun x -> (fun y -> y+x) y+xy+x Box Number as Output

42 42 In OCaml Recall the following declarations are equivalent: –let incr = fun x -> x + 1;; –let incr x = x + 1;; Then: –let add = fun x -> (fun y -> y + x); –let add x = fun y -> y + x; –let add x y = y + x; add can take a single argument to return a function.

43 43 Adding Two Integers (fun x -> (fun y -> y + x)) 1 2 3 add 1 2 (fun y -> y + 1) 2 2 + 1

44 44 Box Number as Input true,false)(

45 45 Box Number as Input true,false)( fun f -> (f true, f false) f f f

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

47 47 Types A type specifies what kind of value a given expression evaluates to. –1 + 1 : int –true && false : bool –’A’ : char –”hello” : string –(1, true) : int * bool –(1, -1, true) : int * int * bool –1.0 : float –() : unit

48 48 Type Preservation Expression : T Value : T An evaluation preserves the type of a given expression.

49 49 Example let x = 1 in let y = x + x in let z = y + y in z + z 8 : int : int

50 50 Function Types T -> T’ –type of functions: taking arguments of type T returning values of type T’ Example: # let incr = fun x -> x + 1;; val incr : int -> int = # let incr x = x + 1;; val incr : int -> int = Explicit type annotation # let inc = fun (x:int) -> x + 1;; val inc : int -> int = # let incr (x:int) = x + 1;; val incr : int -> int =

51 51 x fun y -> y+x fun x -> (fun y -> y+x) Type of add

52 52 fun y -> y+x fun x -> (fun y -> y+x) Type of add int

53 53 fun x -> (fun y -> y+x) Type of add int int -> int

54 54 int -> (int -> int) Type of add int int -> int

55 55 What is the Type? true,false)( fun f -> (f true, f false) f f f

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

57 57 But why is it f : bool -> int ?

58 58 Why not f : bool -> char ? true,false)( fun f -> (f true, f false) f f f : (bool -> char) -> char * char

59 59 Then why not f : bool -> string ? f : bool -> int * string ? f : bool -> unit ? f : bool -> (int -> int) ? f : bool -> ? …

60 60 So we need Polymorphism.

61 61 What is the Type of ? true,false)( fun f -> (f true, f false) f f f All we know about f is that it takes booleans as arguments.

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

63 63 f : bool -> ? fun f -> (f true, f false) : (bool -> ? ) -> ? * ?

64 64 f : bool -> 'a fun f -> (f true, f false) : (bool -> 'a) -> 'a * 'a 'a –type variable –usually read as alpha –means 'for any type alpha'

65 65 Polymorphic Types Types involving type variables 'a, 'b, 'c,... E.g. –fun x -> x : 'a -> 'a –fun x -> fun y -> (x, y) : 'a -> 'b -> ('a * 'b) –fun (x : 'a) -> fun (y : 'a) -> x = y : 'a -> 'a -> bool

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

67 67 Recursion vs. Iteration Recursion in OCaml let rec sum n = if n = 0 then 0 else 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 tool if you are used to functional programming. Recursion seems elegant but inefficient!

68 68 Recursion in Action let rec sum n = if n = 0 then 0 else sum (n - 1) + n call stack evaluation f 10 f 8 f 9... f 1 f 0 55 36 + 9 45 + 10... 0 + 1 1 + 2 0 further computation

69 69 Funny Recursion let rec zero n = if n = 0 then 0 else zero (n - 1) call stack evaluation f 10 f 9 f 8... f 1 f 0 0 0 0 0 0... 0 no further computation

70 70 Funny Recursion Optimized let rec zero n = if n = 0 then 0 else zero (n - 1) call stack evaluation f 10 f 9 f 8... f 1 f 0 0 0 0 0 0... 0

71 71 Funny Recursion Further Optimized let rec zero n = if n = 0 then 0 else zero (n - 1) call stack evaluation f 10f 9f 8... f 1f 0 0

72 72 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!

73 73 Example Tail recursive sum let rec sum' accum k = if k = 0 then accum else sum' (accum + k) (k - 1) let sum n = sum' 0 n Non-tail recursive sum let rec sum n = if n = 0 then 0 else sum (n - 1) + n Think about the invariant of sum' : –given: sum' accum k –invariant: accum = n + (n - 1) +... (k + 1)

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

75 75 Enumeration Types in C enum shape { Circle, Rectangle, Triangle}; Great flexibility –e.g. Circle + 1 == Rectangle Triangle - 1 == Rectangle (Circle + Triangle) / 2 == Rectangle But is this good or bad?

76 76 Datatypes in OCaml type shape = Circle | Rectangle | Triangle No flexibility –e.g. Circle + 1(x) Triangle - 1(x) Circle + Triangle(x) But high safety.

77 77 Set type set = Empty | Many # Empty ;; - : set = Empty # Many ;; - : set = Many

78 78 Set with Arguments type set = Empty | Many of int # Many 5 ;; - : set = Many 5

79 79 Set with Type Parameters type 'a set = Empty | Singleton of 'a | Pair of 'a * 'a # Singleton 0;; - : int set = Singleton 0 # Pair (0, 1);; - : int set = Pair (0, 1)

80 80 Set with Type Parameters type 'a set = Empty | Singleton of 'a | Pair of 'a * 'a # Pair (Singleton 0, Pair (0, 1)) ;; - : int set set = Pair (Singleton 0, Pair (0, 1))

81 81 Set with Type Parameters type 'a set = Empty | Singleton of 'a | Pair of 'a * 'a # Pair (0, true);;

82 82 Set with Type Parameters type 'a set = Empty | Singleton of 'a | Pair of 'a * 'a # Pair (0, true);; Error: This expression has type bool but an expression was expected of type int

83 83 Recursive Set with Type Parameters type 'a set = Empty | NonEmpty of 'a * 'a set This is essentially the definition of datatype list.

84 84 Datatype list type 'a list = [] | (* defined internally *) :: of 'a * 'a list (* infix *) # [] ;; - : 'a list = [] # 2 :: [] ;;(* infix *) - : int list = [2] # [2] ;; - : int list = [2] # [1; 2] ;; - : int list = [1; 2] # 1 :: 2 :: [] ;;(* right associative *) -: int list = [1; 2] # 1 :: (2 :: []) ;; - : int list = [1; 2]

85 85 Datatype list type 'a list = [] | (* defined internally *) :: of 'a * 'a list (* infix *) # 1 :: [2] :: [];; # [1] :: 2 :: [] ;; # [1] :: [2] :: [] ;; - : int list list = [[1]; [2]] # [1] :: [[2]] ;; - : int list list = [[1]; [2]] (X)

86 86 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?

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

88 88 Simple Pattern type shape = Circle | Rectangle | Triangle (* convertToEnum : shape -> int *) let convertToEnum (x : shape) : int = match x with Circle -> 0 | Rectangle -> 1 | Triangle -> 2

89 89 Pattern with Arguments type set = Empty | Many of int let size (x : set) : int = match x with Empty -> 0 | Many n -> n

90 90 Wildcard Pattern _ : "don't care" type 'a set = Empty | Singleton of 'a | Pair of 'a * 'a let isEmpty (x : 'a set) : bool = match x with Empty -> true | _ -> false

91 91 Pattern with Type Annotation type 'a list = [] | (* defined internally *) :: of 'a * 'a list (* infix *) let rec length (x : 'a list) : int = match x with ([] : 'a list) -> 0 | (_ : 'a) :: (tail : 'a list) -> 1 + length tail

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

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

94 94 Why "Higher-order"? T 0 ::= int | bool | float | unit |... T 1 ::= T 0 -> T 0 | T 0 (* 1st order *) T 2 ::= T 1 -> T 1 | T 1 (* 2nd order *) T 3 ::= T 2 -> T 2 | T 2 (* higher order *)...

95 95 Higher-order Functions in List val exists : ('a -> bool) -> 'a list -> bool val for_all : ('a -> bool) -> 'a list -> bool val map : ('a -> 'b) -> 'a list -> 'b list val filter : ('a -> bool) -> 'a list -> 'a list val iter : ('a -> unit) -> 'a list -> unit (* print_int : int -> unit *) let print_int i = print_string ((string_of_int i) ^ "\n" );; List.iter print_int [1; 2; 3];;

96 96 List Fold Function - Left fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a fold_left f a 0 [b 0 ; b 1 ; b 2 ;...; b n-1 ] a0a0 f a1a1 f a2a2 f anan f a3a3 a n-2 b0b0 b1b1 b n-1 b2b2 b n-2 f a n-1... current resultcurrent elementnew resultinitial resultelementsfinal result

97 97 List Fold Function - Right fold_right : (‘b -> ‘a -> ‘a) -> ‘b list -> ‘a -> ‘a fold_right f [b 0 ; b 1 ; b 2 ;...; b n-1 ] a n anan b0b0 b1b1 b n-1 b2b2 b n-2 a0a0 f a1a1 f a2a2 f f a3a3 a n-2 f a n-1... current resultcurrent elementnew resultinitial resultelementsfinal result

98 98 Summation let sum (l : int list) = List.fold_left (fun accum a -> a + accum) 0 l fold_left (+) 0 [a 0 ; a 1 ; a 2 ;...; a n-1 ] 0 a0a0 + ¢ a1a1 + ¢ a n-1 +  a2a2 + ¢ ¢ a n-2 + ¢... (+)

99 99 List Reversal let reverse (l : 'a list) = List.fold_left (fun rev a -> a :: rev) [] l Whenever you need iterations over lists, first consider fold_left and fold_right.

100 100 More Examples let exists f l = List.fold_left (fun a b -> a || f b) false l let for_all f l = List.fold_left (fun a b -> a && f b) true l let map f l = List.fold_right (fun a b -> f a :: b) l [] let filter f l = List.fold_right (fun a b -> if f a then a :: b else b) l []

101 101 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 OCaml manual. Modules

102 102 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

103 103 Structures and Signatures Structure –collection of type declarations, exceptions, values, and so on. module Set = struct type 'a set = 'a list let empty_set = [] let singleton x = [x] let union s1 s2 = s1 @ s2 end Signature –conceptually type of structures. module type SET = sig type 'a set val empty_set : 'a set val singleton : 'a -> 'a set val union : 'a set -> 'a set -> 'a set end

104 104 Transparent Constraints Type definition is exported to the outside. module Set = struct type 'a set = 'a list let empty_set = [] let singleton x = [x] let union s1 s2 = s1 @ s2 end # Set.singleton 1 = [1] ;; - : bool = true

105 105 Opaque Constraints module Set : SET = struct type 'a set = 'a list let empty_set = [] let singleton x = [x] let union s1 s2 = s1 @ s2 end # Set.singleton 1 = [1] ;; Error: This expression has type 'a list but an expression was expected of type int Set.set module type SET = sig type 'a set val empty_set : 'a set val singleton : 'a -> 'a set val union : 'a set -> 'a set -> 'a set end No type definition is exported.

106 106 Overriding Opaque Constraints module Set : SET with type ‘a set = ‘a list = struct type 'a set = 'a list let empty_set = [] let temp x = [x] let singleton x = temp x let union s1 s2 = s1 @ s2 end # Set.singleton 1 = [1] ;; - : bool = true module type SET = sig type 'a set val empty_set : 'a set val singleton : 'a -> 'a set val union : 'a set -> 'a set -> 'a set end Use the with keyword.

107 107 Functors Functions on structures –takes a structure as an argument –returns a structure as the result The most powerful tool for structuring programs See Assignment #3. structure

108 For more information on OCAML, visit: http://caml.inria.fr/pub/docs/manual-ocaml-4.01/ https://realworldocaml.org/ http://caml.inria.fr/pub/docs/manual-ocaml-4.01/ https://realworldocaml.org/


Download ppt "CSE-321 Programming Languages Introduction to Functional Programming POSTECH 박성우."

Similar presentations


Ads by Google