Functional Programming - CS Department - HomeA Functional Programming System Practical Functional...
Transcript of Functional Programming - CS Department - HomeA Functional Programming System Practical Functional...
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
Functional Programming
Robert Bieber
March 1, 2011
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
Outline
1 PreliminariesJohn BackusIssues With von Neumann Languages
2 A Functional Programming SystemFoundationsComponents of an FP SystemThe Algebra of Programs
3 Practical Functional ProgrammingHaskellRuby
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
John BackusIssues 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 onprogramming languages.
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
John BackusIssues With von Neumann Languages
The von Neumann Architecture
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
John BackusIssues With von Neumann Languages
Excessive "Housekeeping" Code
/∗∗ Computes dot product o f two vec to rs∗ /
public i n t dotProduct ( i n t [ ] a , i n t [ ] b ){
i n t res = 0;for ( i n t i = 0 ; i < a . leng th ; i ++)
res += a [ i ] ∗ b [ i ] ;return res ;
}
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
John BackusIssues 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
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
John BackusIssues With von Neumann Languages
Potential Problems
Not if f references global variables.
i n t f ( i n t x ){
return someGlobalVariable ++;}
Or if f interacts with the user.
i n t f ( i n t x ){
i n t r e t ;scanf ( "%d " , &r e t ) ;return r e t ;
}
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe 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 byuseful algebraic properties.
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe 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
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe Algebra of Programs
Objects
Objects are the data on which our programs operate.Atoms are numbers and combinations of characters:12,T ,F ,FOOLists 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
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe Algebra of Programs
Primitive Functions
A function is a mapping from the set of objects to the set ofobjects.Functions accept a single argument and return a singleresult.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 ofapplying f to x.
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe Algebra of Programs
Function Application
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe Algebra of Programs
Examples of Function Application
id : A = A+ :< 3,4 >= 72 :< 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
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe Algebra of Programs
Functional Forms
Functional forms are expressions that represent functions.Functional forms are operators with functions as theiroperands.We can use functional forms to construct more usefulfunctions from the supplied primitives.
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe 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 : xConstant: x : y ≡ x
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe Algebra of Programs
Composition
f ◦ g
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe Algebra of Programs
Construction
[f ,g]
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe Algebra of Programs
Conditional
p → f ;g
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe Algebra of Programs
Definitions
A definition, or defined function, associates a name with afunctional form.Definitions are analogous to function definitions in vonNeumann languages.A Definition consists of a name on the left side and anexpression consisting only of functional forms involvingprimitive and other defined functions on the right side.Takes the form Def name ≡ expression
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe Algebra of Programs
Examples of Functional Programs
Def sub1 ≡ − ◦ [id ,1]Def eq0 ≡ eq ◦ [id ,0]Def fact ≡ eq0→ 1;× ◦ [id , fact ◦ sub1]Collatz Sequence:Def eq1 ≡ eq ◦ [id ,1]Def div2 ≡ ÷ ◦ [id ,2]Def mult3add1 ≡ + ◦ [1,× ◦ [id ,3]]Def collatzstep ≡ even→ div2;mult3add1Def collatz ≡ eq1→ < 1 >;apndl ◦ [id , collatz ◦collatzstep]
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe Algebra of Programs
Algebraic Properties of FP Systems
Unlike von Neumann languages, FP systems exhibit usefulalgebraic properties.Using these properties, we can manipulate programsalgebraically the way we manipulate mathematicalexpressions in high school algebra.We can prove properties of programs using theprogramming language itself.
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
FoundationsComponents of an FP SystemThe 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
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
HaskellRuby
Properties of Haskell
Pure functional language: no concept of state, functionshave no side-effects (except for I/O).Unlike Backus’ FP system, handles multiple argumentfunctions by currying.User writes program by specifying type signatures andfunction definitions.Functions are first class objects and can be passed toother functions.
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
HaskellRuby
Example Haskell Program
c o l l a t z S t e p : : I n t −> I n tc o l l a t z S t e p x = i f x ‘mod‘ 2 == 0
then x ‘ div ‘ 2else 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 = dox <− getLineputStrLn $ show $ c o l l a t z $ read x
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
HaskellRuby
Execution
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
HaskellRuby
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
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
HaskellRuby
Examples
A function, sumx2, which multiplies every element of a list by 2and sums the resultant list.
Normal:sumx2 xs = sum (map (∗2) xs)Pointfree:sumx2 = sum . map (∗2)
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
HaskellRuby
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
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
HaskellRuby
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 scan f ( f i n , "%s \ n " , t e x t ) ;p r i n t f ( "%s \ n " , t e x t ) ;
}f c l o s e ( f i n ) ;
Ruby Example:
IO . foreach ( ’ t e s t . t x t ’ ) do | l i n e |puts l i n e
end
Robert Bieber Functional Programming
PreliminariesA Functional Programming SystemPractical Functional Programming
Summary
Summary
Imperative programming languages are inflexible, anddon’t exhibit useful algebraic properties.FP systems allow the user to create programs bycombining simpler functions, and these programs can bemanipulated algebraically.In modern languages we have both purely functionallanguages and imperative/object-oriented languages whichincorporate functional features.
Robert Bieber Functional Programming
Appendix For Further Reading
For Further Reading I
J. Backus.Can Programming Be Liberated from the von NeumannStyle? A Functional Style and Its Algebra of Programs.Communications of the ACM, 21(8):, 1978.
Haskell Wiki.Pointfreehttp://www.haskell.org/haskellwiki/Pointfree
Ruby DocumentationGuides and API Documentationhttp://www.ruby-doc.org/
Robert Bieber Functional Programming