FP With Haskell: CS5205: Foundations in Programming Languages
FP With Haskell: CS5205: Foundations in Programming Languages
Programming Languages
FP with Haskell
CS5205 Haskell 1
Haskell
CS5205 Haskell 2
Functions
…. inputs …. inputs
…. output …. output
pure impure
CS5205 Haskell 3
Example of Functions
• Double a given input.
square :: Int -> Int
square x = x*x
CS5205 Haskell 4
Expression-Oriented
• Instead of command/statement :
if e1 then stmt1 else stmt2
CS5205 Haskell 5
Expression-Oriented
• An example function:
fact :: Integer -> Integer
fact n = if n=0 then 1
else n * fact (n-1)
• Alternatively:
fact n = case n of
0 -> 1
a -> a * fact (a-1)
CS5205 Haskell 6
Conditional Case Construct
• Conditional;
if e1 then e2 else e3
• Can be translated to
case e1 of
True -> e2
False -> e3
CS5205 Haskell 8
Layout Rule
• Haskell uses two dimensional syntax that relies on
declarations being “lined-up columnwise”
let y = a+b
f x = (x+y)/y is being parsed as:
in f c + f d
let { y = a+b
; f x = (x+y)/y }
in f c + f d
CS5205 Haskell 9
Expression Evaluation
• Expression can be computed (or evaluated) so that it is
reduced to a value. This can be represented as:
e .. v
if e :: t Æ e v , it follows that v :: t
CS5205 Haskell 10
Values and Types
• As a purely functional language, all computations are
done via evaluating expressions (syntactic sugar) to
yield values (normal forms as answers).
• Each expression has a type which denotes the set of
possible outcomes.
• v :: t can be read as value v has type t.
• Examples of typings, associating a value with its
corresponding type are:
5 :: Integer
'a' :: Char
[1,2,3] :: [Integer]
('b', 4) :: (Char, Integer)
“cs5” :: String (same as [Char])
CS5205 Haskell 11
Built-In Types
• They are not special:
data Char = ‘a’ | ‘b’ | …
data Int = -65532 | … | -1 | 0 | 1 | …. | 65532
data Integer = … | -2 | -1 | 0 | 1 | 2 | …
CS5205 Haskell 12
User-Defined Algebraic Types
• Can describe enumerations:
data Bool = False | True
data Color = Red | Green | Blue | Violet
CS5205 Haskell 13
Recursive Types
• Some types may be recursive:
data Tree a = Leaf a | Branch (Tree a) (Tree a)
CS5205 Haskell 14
Polymorphic Types
• Support types that are universally quantified in some
way over all types.
• 8 c. [c] denotes a family of types, for every type c, the type
of lists of c.
CS5205 Haskell 15
Polymorphic Types
• This polymorphic function can be used on list of any type..
length [1,2,3] ) 2
length [‘a’, ’b’, ‘c’] ) 3
length [[1],[],[3]] ) 3
• More examples :
head :: [a] -> a
head (x:xs) = x
CS5205 Haskell 18
Type Classes and Overloading
• Parametric polymorphism works for all types. However,
ad-hoc polymorphism works for a class of types and can
systematically support overloading.
• For example, what is the type for (+) operator?
(+) :: a -> a -> a
(+) :: Int -> Int -> Int
(+) :: Float -> Float -> Float
CS5205 Haskell 20
Members of Eq Type Class
• The class is open and can be extended, as follows:
instance Eq Integer where
x == y = x ‘integerEq’ y
CS5205 Haskell 21
More Members of Eq Type Class
• Similarly, we may use structural equality for List:
instance (Eq a) => Eq ([a]) where
[] == [] = True
(x:xs) == (y:ys) = (x==y) && (xs==ys)
_ == _ = False
CS5205 Haskell 22
Numeric Class
• There is a hierarchy of numeric classes. Most basic is:
class Num a where
(+),(-),(*) :: a -> a -> a
negate,abs,signum :: a -> a
fromInteger :: Integer -> a
x – y = x + (negate y)
negate x = 0 – x
CS5205 Haskell 24
Functions and its Type
• Some examples
(\x -> x+1) 3.2
inc 3
CS5205 Haskell 25
Function Abstraction
time
time p ()
CS5205 Haskell 26
Higher-Order Functions
• Higher-order programming treats functions as first-class,
allowing them to be passed as parameters, returned as
results or stored into data structures.
CS5205 Haskell 27
Functions
• Functions can be written in two main ways:
add :: Integer -> Integer -> Integer
add x y = x+y
CS5205 Haskell 29
Genericity
• Replace specific entities (0 and +) by parameters.
sumList ls =
case ls of
[] -> 0
x:xs -> x+(sumList xs)
foldr f u ls =
case ls of
[] -> u
x:xs -> f x (foldr f u xs)
CS5205 Haskell 30
Polymorphic, Higher-Order Types
CS5205 Haskell 31
Instantiating Generic Functions
sumL2 [1, 2, 3] )
CS5205 Haskell 32
Instantiating Generic Functions
prodL [1, 2, 5] )
CS5205 Haskell 33
Instantiating Generic Functions
• Can you express map in terms of foldr?
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = (f x) : (map f xs)
map f xs = foldr … … … xs
CS5205 Haskell 34
Instantiating Generic Functions
• Filtering a list of elements with a predicate.
filter :: (a -> Bool) -> [a] -> [a]
filter f [] = []
filter f (x:xs) =
if (f x) then x : (filter f xs)
else x : filter f xs
CS5205 Haskell 35
Pipe/Compose
cmd1 | cmd2
CS5205 Haskell 36
Iterator Construct
CS5205 Haskell 37
Right Folding
foldr f u [x1,x2,..,xn]
associate to
right
CS5205 Haskell 38
Left Folding – Tail Recursion
• Accumulate result in a parameter:
foldl f u ls =
case ls of
[] -> u
x:xs -> foldl f (f u x) xs
based on accumulation
• What is the type of foldl?
CS5205 Haskell 39
Left Folding
foldl f u [x1,x2,..,xn]
left is here!
CS5205 Haskell 40
Instance of Left Folding
• Summing a list by accumulation.
sumT acc ls =
case ls of
[] -> 0
x:xs -> sumT (x+acc) xs
sumList ls = sumT 0 ls
CS5205 Haskell 41
Infix Operator
• Infix operators are distinguished entirely by symbols:
(++) :: [a]-> [a] -> [a]
[] ++ ys = ys
(x:xs) ++ ys = x : (xs ++ ys)
• Other examples:.
inc = (+ 1)
add = (+)
CS5205 Haskell 43