Download Concepts of Functional Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Concepts of Functional Programming
Neil Sculthorpe
Department of Computer Science
Royal Holloway, University of London
[email protected]
Nottingham, England
1st April 2016
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
What is Functional Programming?
Functional programming is a paradigm.
Based on evaluating expressions, not executing commands.
Functional programs express output as a function of input,
rather than as a sequence of steps to be performed.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Why Functional Programming?
Concise programs
Code reuse
Types provide more compile-time checks
Fewer bugs
Rapid prototyping
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Overview
Higher-order Functions
Purity
Recursion
Algebraic Data Types
Lazy Evaluation
Polymorphism
Dependent Types
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Function Syntax (Unary)
C-like languages
int sqr(int i) {
return i * i;
}
Mathematics
sqr : Z → Z
sqr (i) = i × i
Functional language
(Haskell)
sqr :: Int → Int
sqr i = i ∗ i
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Function Syntax (Binary)
C-like languages
int hypot(int i, int j) {
return sqrt(sqr(i)+sqr(j));
}
Functional language
(Haskell)
hypot :: Int → Int → Int
hypot i j = sqrt (sqr i + sqr j)
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Higher-order Functions
Functions are first-class values.
Higher-order functions take functions as arguments and/or return
functions as results.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Higher-order Functions
Functions are first-class values.
Higher-order functions take functions as arguments and/or return
functions as results.
apply :: (Int → Int) → Int → Int
apply f i = f i
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Higher-order Functions
Functions are first-class values.
Higher-order functions take functions as arguments and/or return
functions as results.
apply :: (Int → Int) → Int → Int
apply f i = f i
second :: (Int → Int) → (Int → Int) → (Int → Int)
second f g = g
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Higher-order Functions - Lambda Abstractions
Lambda abstractions construct anonymous functions inline.
Syntax: (λ Argument → Result)
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Higher-order Functions - Lambda Abstractions
Lambda abstractions construct anonymous functions inline.
Syntax: (λ Argument → Result)
E.g.
x :: Int
x = apply sqr 3
sqr :: Int → Int
sqr i = i ∗ i
is equivalent to
x :: Int
x = apply (λi → i ∗ i) 3
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Higher-order Functions - Manipulating Functions
compose :: (A → B) → (B → C ) → (A → C )
compose f g = λa → g (f a)
flip :: (A → B → C ) → (B → A → C )
flip f = λb a → f a b
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Purity
The result of a pure function is completely determined by its
arguments.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Purity
The result of a pure function is completely determined by its
arguments.
A pure function thus has no computational side effects.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Purity
The result of a pure function is completely determined by its
arguments.
A pure function thus has no computational side effects.
Advantages for: clarity, testing, debugging, refactoring,
optimisation, trustworthiness.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Purity
The result of a pure function is completely determined by its
arguments.
A pure function thus has no computational side effects.
Advantages for: clarity, testing, debugging, refactoring,
optimisation, trustworthiness.
Data structures are immutable.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Purity
The result of a pure function is completely determined by its
arguments.
A pure function thus has no computational side effects.
Advantages for: clarity, testing, debugging, refactoring,
optimisation, trustworthiness.
Data structures are immutable.
No imperative variables!
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Recursion
Functional programs use recursion instead of iteration.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Recursion
Functional programs use recursion instead of iteration.
C-like language
int fact(int i) {
int result = 1;
for (int n = i; n > 1; n--) {
result = result * n;
}
return result;
}
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Recursion
Functional programs use recursion instead of iteration.
C-like language
Functional language
(Haskell)
int fact(int i) {
int result = 1;
for (int n = i; n > 1; n--) {
result = result * n;
}
return result;
}
fact :: Int → Int
fact 0 = 1
fact i = i ∗ fact (i − 1)
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Algebraic Data Types
Functional data structures are built using algebraic data types.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Algebraic Data Types
Functional data structures are built using algebraic data types.
Formed from sums, products and recursion.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Algebraic Data Types
Functional data structures are built using algebraic data types.
Formed from sums, products and recursion.
Functions can be defined by pattern matching.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Algebraic Data Types
Functional data structures are built using algebraic data types.
Formed from sums, products and recursion.
Functions can be defined by pattern matching.
Days of the week:
data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Algebraic Data Types
Functional data structures are built using algebraic data types.
Formed from sums, products and recursion.
Functions can be defined by pattern matching.
Days of the week:
data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun
isWeekend
isWeekend
isWeekend
isWeekend
isWeekend
isWeekend
isWeekend
isWeekend
:: Day → Bool
Mon = False
Tue = False
Wed = False
Thu = False
Fri = False
Sat = True
Sun = True
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Algebraic Data Types
Functional data structures are built using algebraic data types.
Formed from sums, products and recursion.
Functions can be defined by pattern matching.
Days of the week:
data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun
isWeekend :: Day → Bool
isWeekend Sat = True
isWeekend Sun = True
isWeekend
= False
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Algebraic Data Types
Functional data structures are built using algebraic data types.
Formed from sums, products and recursion.
Functions can be defined by pattern matching.
Days of the week:
data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun
isWeekend :: Day → Bool
isWeekend Sat = True
isWeekend Sun = True
isWeekend
= False
Binary tree with integer leaves:
data BinaryTree = Leaf Int | Node BinaryTree BinaryTree
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Algebraic Data Types - Singly Linked Lists
data List = Nil | Cons Int List
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Algebraic Data Types - Singly Linked Lists
data List = Nil | Cons Int List
sqrList :: List → List
sqrList Nil
= Nil
sqrList (Cons i l) = Cons (sqr i) (sqrList l)
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Algebraic Data Types - Singly Linked Lists
data List = Nil | Cons Int List
sqrList :: List → List
sqrList Nil
= Nil
sqrList (Cons i l) = Cons (sqr i) (sqrList l)
mapList :: (Int → Int) → List → List
mapList f Nil = Nil
mapList f (Cons i l) = Cons (f i) (mapList f l)
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Algebraic Data Types - Singly Linked Lists
data List = Nil | Cons Int List
sqrList :: List → List
sqrList Nil
= Nil
sqrList (Cons i l) = Cons (sqr i) (sqrList l)
mapList :: (Int → Int) → List → List
mapList f Nil = Nil
mapList f (Cons i l) = Cons (f i) (mapList f l)
sqrList :: List → List
sqrList l = mapList sqr l
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Algebraic Data Types - Singly Linked Lists
data List = Nil | Cons Int List
sqrList :: List → List
sqrList Nil
= Nil
sqrList (Cons i l) = Cons (sqr i) (sqrList l)
mapList :: (Int → Int) → List → List
mapList f Nil = Nil
mapList f (Cons i l) = Cons (f i) (mapList f l)
sqrList :: List → List
sqrList l = mapList sqr l
sqrtList :: List → List
sqrtList l = mapList sqrt l
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Lazy Evaluation
Purity enables lazy evaluation.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Lazy Evaluation
Purity enables lazy evaluation.
Defer evaluation of expressions until the result is needed.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Lazy Evaluation
Purity enables lazy evaluation.
Defer evaluation of expressions until the result is needed.
Infinite data structures can be expressed directly.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Lazy Evaluation
Purity enables lazy evaluation.
Defer evaluation of expressions until the result is needed.
Infinite data structures can be expressed directly.
E.g.
intsFrom :: Int → List
intsFrom i = Cons i (intsFrom (i + 1))
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Polymorphism (Parametric)
Functional languages can infer the types of functions.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Polymorphism (Parametric)
Functional languages can infer the types of functions.
Functions can have polymorphic types.
apply :: (a → b) → a → b
apply f x = f x
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Polymorphism (Parametric)
Functional languages can infer the types of functions.
Functions can have polymorphic types.
apply :: (a → b) → a → b
apply f x = f x
Data types can also be polymorphic:
data List a = Nil | Cons a (List a)
mapList :: (a → b) → List a → List b
mapList f Nil = Nil
mapList f (Cons x l) = Cons (f x) (mapList f l)
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Polymorphism (Ad-hoc)
Ad-hoc polymorphic functions are restricted to classes of types.
sqr :: Num a ⇒ a → a
sqr x = x ∗ x
sort :: Ord a ⇒ List a → List a
sort l = . . .
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Polymorphism (Ad-hoc)
Ad-hoc polymorphic functions are restricted to classes of types.
sqr :: Num a ⇒ a → a
sqr x = x ∗ x
sort :: Ord a ⇒ List a → List a
sort l = . . .
Type constructors can also be polymorphic.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Polymorphism (Ad-hoc)
Ad-hoc polymorphic functions are restricted to classes of types.
sqr :: Num a ⇒ a → a
sqr x = x ∗ x
sort :: Ord a ⇒ List a → List a
sort l = . . .
Type constructors can also be polymorphic.
E.g. rather than,
mapList
:: (a → b) → List a
→ List b
mapTree :: (a → b) → Tree a → Tree b
mapVector :: (a → b) → Vector a → Vector b
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Polymorphism (Ad-hoc)
Ad-hoc polymorphic functions are restricted to classes of types.
sqr :: Num a ⇒ a → a
sqr x = x ∗ x
sort :: Ord a ⇒ List a → List a
sort l = . . .
Type constructors can also be polymorphic.
E.g. rather than,
mapList
:: (a → b) → List a
→ List b
mapTree :: (a → b) → Tree a → Tree b
mapVector :: (a → b) → Vector a → Vector b
we can define a single general purpose map:
map :: Functor t ⇒ (a → b) → t a → t b
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Dependent Types
Def: A function has a dependent type when the type of its result
depends on the value of its argument.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Dependent Types
Def: A function has a dependent type when the type of its result
depends on the value of its argument.
In practice: types and values can be intermixed.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Dependent Types
Def: A function has a dependent type when the type of its result
depends on the value of its argument.
In practice: types and values can be intermixed.
Extremely powerful for encoding compile-time checks in a program.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Dependent Types
Def: A function has a dependent type when the type of its result
depends on the value of its argument.
In practice: types and values can be intermixed.
Extremely powerful for encoding compile-time checks in a program.
E.g. we could add the length of a list to its type:
map :: (a → b) → List n a → List n b
append :: List m a → List n a → List (m + n) a
sort :: List n a → List n a
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Dependent Types
Def: A function has a dependent type when the type of its result
depends on the value of its argument.
In practice: types and values can be intermixed.
Extremely powerful for encoding compile-time checks in a program.
E.g. we could add the length of a list to its type:
map :: (a → b) → List n a → List n b
append :: List m a → List n a → List (m + n) a
sort :: List n a → List n a
Logical properties of the program can be encoded within the
program itself.
Neil Sculthorpe
Concepts of Functional Programming
FP? Overview Syntax Higher-order Purity Recursion ADTs Lazyness Polymorphism Dependent Types Conclusion
Conclusion
Functional programming involves several (complementary)
concepts, including:
Higher-order Functions
Purity
Recursion
Algebraic Data Types
Lazy Evaluation
Polymorphism
Dependent Types
Not all concepts appear in every functional language.
Increasingly more of these concepts are being added to
non-functional languages.
Neil Sculthorpe
Concepts of Functional Programming
Related documents