210 likes | 368 Vues
This document explores key concepts in OCaml programming, focusing on partial functions, recursion, and their implications in functional programming. It introduces the notion of curried versus uncurried functions through practical examples, like `add_three` and `add_triple`. The text also covers higher-order functions such as `map` and `fold`, demonstrating their applications in list processing. Furthermore, it delves into advanced topics such as continuation-passing style (CPS) transformation, lambda lifting, and the use of record types in OCaml.
E N D
Munawar Hafiz 2219 SC, UIUC http://www.cs.illinois.edu/class/cs421/ Programming Languages and Compilers (CS 421) Based in part on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa Gunter
Partial application of functions let add_three x y z = x + y + z;; # let h = add_three 5 4;; val h : int -> int = <fun> # h 3;; - : int = 12 # h 7;; - : int = 16
Functions as arguments # let thrice f x = f (f (f x));; val thrice : ('a -> 'a) -> 'a -> 'a = <fun> # let g = thrice plus_two;; val g : int -> int = <fun> # g 4;; - : int = 10 # thrice (fun s -> "Hi! " ^ s) "Good-bye!";; - : string = "Hi! Hi! Hi! Good-bye!"
Save the Environment! A closure is a pair of an environment and an association of a sequence of variables (the input variables) with an expression (the function body), written: f < (v1,…,vn) exp, f > Where f is the environment in effect when f is defined (if f is a simple function)
Curried vs Uncurried • Recall val add_three : int -> int -> int -> int = <fun> • How does it differ from # let add_triple (u,v,w) = u + v + w;; val add_triple : int * int * int -> int = <fun> • add_three is curried; • add_triple is uncurried
Curried vs Uncurried # add_triple (6,3,2);; - : int = 11 # add_triple 5 4;; Characters 0-10: add_triple 5 4;; ^^^^^^^^^^ This function is applied to too many arguments, maybe you forgot a `;' # fun x -> add_triple (5,4,x);; : int -> int = <fun>
Functions Over Lists # let rec map f list = match list with [] -> [] | (h::t) -> (f h) :: (map f t);; val map : ('a -> 'b) -> 'a list -> 'b list = <fun> # map plus_two fib5;; - : int list = [10; 7; 5; 4; 3; 3] # map (fun x -> x - 1) fib6;; : int list = [12; 7; 4; 2; 1; 0; 0]
Iterating over lists # let rec fold_left f a list = match list with [] -> a | (x :: xs) -> fold_left f (f a x) xs;; val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = <fun> # fold_left (fun () -> print_string) () ["hi"; "there"];; hithere- : unit = ()
Iterating over lists # let rec fold_right f list b = match list with [] -> b | (x :: xs) -> f x (fold_right f xs b);; val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b = <fun> # fold_right (fun s -> fun () -> print_string s) ["hi"; "there"] ();; therehi- : unit = ()
Encoding Recursion with Fold # let rec append list1 list2 = match list1 with [ ] -> list2 | x::xs -> x :: append xs list2;; val append : 'a list -> 'a list -> 'a list = <fun> Base Case Operation Recursive Call # let append list1 list2 = fold_right (fun x y -> x :: y) list1 list2;; val append : 'a list -> 'a list -> 'a list = <fun> # append [1;2;3] [4;5;6];; - : int list = [1; 2; 3; 4; 5; 6]
Example of Tail Recursion # let prod list = let rec prod_aux l acc = match l with [] -> acc | (y :: rest) -> prod_aux rest (acc * y) in prod_aux list 1;; val prod : int list -> int = <fun> # let prod list = List.fold_left (fun acc -> fun y -> acc * y) 1 list;; val prod : int list -> int = <fun>
Lambda Lifting • You must remember the rules for evaluation when you use partial application # let add_two = (+) (print_string "test\n"; 2);; test val add_two : int -> int = <fun> # let add2 =(* lambda lifted *) fun x ->(+) (print_string "test\n"; 2)x;; val add2 : int -> int = <fun>
CPS Transformation • Step 1: Add continuation argument to any function definition: • let f arg = e let f arg k = e • Idea: Every function takes an extra parameter saying where the result goes • Step 2: A simple expression in tail position should be passed to a continuation instead of returned: • return a k a • Assuming a is a constant or variable. • “Simple” = “No available function calls.”
CPS Transformation • Step 3: Pass the current continuation to every function call in tail position • return f arg f arg k • The function “isn’t going to return,” so we need to tell it where to put the result. • Step 4: Each function call not in tail position needs to be built into a new continuation (containing the old continuation as appropriate) • return op (f arg) f arg (fun r -> k(op r)) • op represents a primitive operation
Before: let rec add_list lst = match lst with [ ] -> 0 | 0 :: xs -> add_list xs | x :: xs -> (+) x (add_list xs);; After: let rec add_listk lst k = (* rule 1 *) match lst with | [ ] -> k 0 (* rule 2 *) | 0 :: xs -> add_listk xs k (* rule 3 *) | x :: xs -> add_listk xs (fun r -> k ((+) x r));; (* rule 4 *) Example
Record Types • Record types must be declared before they can be used in OCaml # typeperson = {name : string;ss: (int * int * int);age : int};; type person = { name : string; ss : int * int * int; age : int; } • person is the type being introduced • name, ss and age are the labels, or fields
Variants - Syntax (slightly simplified) • type name = C1[of ty1] | . . . | Cn[of tyn] • Introduce a type called name • (fun x -> Ci x) : ty1 -> name • Ci is called a constructor; if the optional type argument is omitted, it is called a constant • Constructors are the basis of almost all pattern matching
Recursive Data Types # type int_Bin_Tree = Leaf of int | Node of (int_Bin_Tree * int_Bin_Tree);; type int_Bin_Tree = Leaf of int | Node of (int_Bin_Tree * int_Bin_Tree)
Recursive Functions # let rec first_leaf_value tree = match tree with (Leaf n) -> n | Node (left_tree, right_tree) -> first_leaf_value left_tree;; val first_leaf_value : int_Bin_Tree -> int = <fun> # let left = first_leaf_value bin_tree;; val left : int = 3
Recursive Data Type Values bin_tree = Node Node Leaf (-7) Leaf 3 Leaf 6