RobertBieber-FunctionalProgramming

RobertBieber-Functio - Preliminaries A Functional Programming System Practical Functional Programming Summary Functional Programming Robert Bieber

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Preliminaries A Functional Programming System Practical Functional Programming Summary Functional Programming Robert Bieber March 1, 2011 Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Outline 1 Preliminaries John Backus Issues With von Neumann Languages 2 A Functional Programming System Foundations Components of an FP System The Algebra of Programs 3 Practical Functional Programming Haskell Ruby Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary John Backus Issues With von Neumann Languages John Backus Born 1924. Died 2007. Directed development of FORTRAN, released in 1957. Backus-Naur Form. Received Turing Award in 1977 for his work on programming languages. Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary John Backus Issues With von Neumann Languages The von Neumann Architecture Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary John Backus Issues With von Neumann Languages Excessive "Housekeeping" Code /∗ ∗ Computes d o t p r o d u c t o f two v e c t o r s ∗/ public i n t d o t P r o d u c t ( i n t [ ] a , i n t [ ] b ) { i n t res = 0; f o r ( i n t i = 0 ; i < a . l e n g t h ; i ++) r e s += a [ i ] ∗ b [ i ] ; return res ; } Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary John Backus Issues With von Neumann Languages Lack of Useful Algebraic Properties Example: f (x ) + 2f (x ) + 3f (x ) (1) = (1 + 2 + 3)f (x ) (2) = 6f (x ) (3) Compare to: i n t y = 1∗ f ( x ) + 2∗ f ( x ) + 3∗ f ( x ) ; Is y == 6*f(x)? Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary John Backus Issues With von Neumann Languages Potential Problems Not if f references global variables. int f ( int x) { r e t u r n someGlobalVariable ++; } Or if f interacts with the user. int f ( int x) { int ret ; s c a n f ( "%d " , & r e t ) ; return r e t ; } Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Charasteristics of an FP System The system is not history-sensitive. Functions have no side-effects. Functions are formed by combining other functions. Programs built in an FP system can be transformed by useful algebraic properties. Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Structure of an FP System A set O of objects. A set F of primitive functions. The application operation. A set F of functional forms. A set D of defined functions. Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Objects Objects are the data on which our programs operate. Atoms are numbers and combinations of characters: 12, T , F , FOO Lists are any combination of atoms: < 3, 4, 5 >, < A, B , < C , D >>, φ ⊥ represents “bottom,” or “undefined” Any list containing ⊥ is equivalent to ⊥, and any function that operates on ⊥ returns ⊥. Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Primitive Functions A function is a mapping from the set of objects to the set of objects. Functions accept a single argument and return a single result. The primitive functions are those “built-in” to the system. Functions are applied with the application operator :. For any function f and any object x , f : x ≡ the result of applying f to x. Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Function Application Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Examples of Function Application id : A = A + :< 3, 4 >= 7 2 :< 5, 6, 7 >= 6 − : 1 =⊥ tl :⊥=⊥ (Remember that all functions are ⊥ preserving). trans :<< 1, 2 >, < 5, 6 >>=<< 1, 5 >, < 2, 6 >> Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Functional Forms Functional forms are expressions that represent functions. Functional forms are operators with functions as their operands. We can use functional forms to construct more useful functions from the supplied primitives. Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Examples of Functional Forms Composition: f ◦ g : x ≡ f : (g : x ) Apply to All: αf :< x1 , x2 , ...xn >≡< f : x1 , f : x2 , ...f : xn > Insert: /f :< x1 , x2 , ...xn >≡ f :< x1 , /f < x2 , ...xn >> Construction: [f1 , f2 , ...fn ] : x ≡< f1 : x , f2 : x , ...fn : x > Condition: (p → f ; g ) ≡ (p : x ) = T → f : x ; g : x Constant: x : y ≡ x Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Composition f ◦g Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Construction [f , g ] Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Conditional p → f;g Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Definitions A definition, or defined function, associates a name with a functional form. Definitions are analogous to function definitions in von Neumann languages. A Definition consists of a name on the left side and an expression consisting only of functional forms involving primitive and other defined functions on the right side. Takes the form Def name ≡ expression Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Examples of Functional Programs Def sub1 ≡ − ◦ [id , 1] Def eq 0 ≡ eq ◦ [id , 0] Def fact ≡ eq 0 → 1; × ◦ [id , fact ◦ sub1] Collatz Sequence: Def eq 1 ≡ eq ◦ [id , 1] Def div 2 ≡ ÷ ◦ [id , 2] Def mult 3add 1 ≡ + ◦ [1, × ◦ [id , 3]] Def collatzstep ≡ even → div 2; mult 3add 1 Def collatz ≡ eq 1 → < 1 >; apndl ◦ [id , collatz ◦ collatzstep] Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Algebraic Properties of FP Systems Unlike von Neumann languages, FP systems exhibit useful algebraic properties. Using these properties, we can manipulate programs algebraically the way we manipulate mathematical expressions in high school algebra. We can prove properties of programs using the programming language itself. Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Foundations Components of an FP System The Algebra of Programs Examples of Algebraic Laws of the FP System [f1 , ...fn ] ◦ g ≡ [f1 ◦ g , ...fn ◦ g ] αf ◦ [g1 , ...gn ] ≡ [f ◦ g1 , ...f ◦ g2 ] (p → f ; g ) ◦ h ≡ p ◦ h → f ◦ h; g ◦ h [..., ⊥, ...] ≡ ⊥ f ◦⊥≡⊥ f ◦ id ≡ f Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Haskell Ruby Properties of Haskell Pure functional language: no concept of state, functions have no side-effects (except for I/O). Unlike Backus’ FP system, handles multiple argument functions by currying. User writes program by specifying type signatures and function definitions. Functions are first class objects and can be passed to other functions. Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Haskell Ruby Example Haskell Program c o l l a t z S t e p : : I n t −> I n t c o l l a t z S t e p x = i f x ‘ mod ‘ 2 == 0 then x ‘ div ‘ 2 else 3 ∗ x + 1 c o l l a t z : : I n t −> [ I n t ] c o l l a t z x = i f x == 1 then [ 1 ] else x : ( c o l l a t z $ c o l l a t z S t e p x ) main = do x <− getLine putStrLn $ show $ c o l l a t z $ read x Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Haskell Ruby Execution Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Haskell Ruby Pointfree Style Avoid use of variable names in function definitions. Favor function composition and partial application. Very similar to Backus’ style. Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Haskell Ruby Examples A function, sumx2, which multiplies every element of a list by 2 and sums the resultant list. Normal: sumx2 xs = sum (map (∗2) xs) Pointfree: sumx2 = sum . map (∗2) Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Haskell Ruby Properties of Ruby Dynamic object-oriented scripting lanugage. Allows methods to accept "blocks" as their final argument: basically anonymous functions. Blocks minimize "housekeeping code" that Backus cited. Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Haskell Ruby File I/O Example C Example: FILE ∗ f i n = fopen ( " t e s t . t x t " , " r " ) ; while ( ! f e o f ( f i n ) ) { f s c a n f ( f i n , "%s \ n " , t e x t ) ; p r i n t f ( "%s \ n " , t e x t ) ; } fclose ( f i n ) ; Ruby Example: IO . f o r e a c h ( ’ t e s t . t x t ’ ) do | l i n e | puts l i n e end Robert Bieber Functional Programming Preliminaries A Functional Programming System Practical Functional Programming Summary Summary Imperative programming languages are inflexible, and don’t exhibit useful algebraic properties. FP systems allow the user to create programs by combining simpler functions, and these programs can be manipulated algebraically. In modern languages we have both purely functional languages and imperative/object-oriented languages which incorporate functional features. Robert Bieber Functional Programming Appendix For Further Reading For Further Reading I J. Backus. Can Programming Be Liberated from the von Neumann Style? A Functional Style and Its Algebra of Programs. Communications of the ACM, 21(8):, 1978. Haskell Wiki. Pointfree http://www.haskell.org/haskellwiki/Pointfree Ruby Documentation Guides and API Documentation http://www.ruby-doc.org/ Robert Bieber Functional Programming ...
View Full Document

This note was uploaded on 07/30/2011 for the course COP 4810 taught by Professor Staff during the Spring '11 term at University of Central Florida.

Ask a homework question - tutors are online