Type introduction illustrated for casual haskellers
to get over the Foldable
Takenobu T.
Rev. 0.01.2 WIP “What is this description ?!” foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b NOTE - This document shows one of the mental model. - Please see also references. - This is written for Haskell, especially ghc7.10/8.0 and later. Contents
1. Introduction Appendix I – Various types - Values, Types, Type classes - Bool, Char, Int, Float - Polymorphic types - Maybe, List, Either, Tuple - Type constructors - Polymorphic and type constructors Appendix II – Various type classes - Eq, Ord 2. more, Types and Type classes - Num - Function types - Foldable - Type class operations - Functor, Applicative, Monad - Monoid 3. What is this? - Traversable
Appendix III – Advanced topics
References 1. Introduction 1. Introduction
Values, Types, Type classes Values
‘a’ False 1.0 1 2 ‘h’
1.5 True ‘5’ 700 3.14
References : [B2] Ch.2, [B3] Ch.2 Types
“Bool” type “Int” type “Float” type “Char” type
1 1.0 ‘a’ False 2 1.5 ‘h’
True 700 3.14 ‘5’ ......
A type is a collection of values which have common property.
References : [B2] Ch.2, [B3] Ch.2, [B1] Ch.2, [D2], [B5] Ch.8, [H1] Ch.4 Type classes
“Num” type class
Bool Int Float Char
1 1.0 ‘a’ False 2 1.5 ‘h’ ... True 700 3.14 ‘5’ ......
A type class is a collection of types which have common operations.
References : [B1] Ch.2, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6, [H1] Ch.4 1. Introduction
Polymorphic types Proper types
Bool Int Float Char
1 1.0 ‘a’ Proper False 2 1.5 ‘h’ types True 700 3.14 ‘z’ ......
References : [B1] Ch.2, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6 Polymorphic types
a
Polymorphic types
Parametric polymorphism
...
Bool Int Float Char
1 1.0 ‘a’ Proper False 2 1.5 ‘h’ types True 700 3.14 ‘z’ ......
References : [B1] Ch.7, [B3] Ch.3, [B4] Ch.6, [D1] Week 2, [H1] Ch.4 Polymorphic types restricted with type classes
Num a => a
Polymorphic types
limited with the type class
...
Bool Int Float Char
1 1.0 ‘a’ Proper False 2 1.5 ‘h’ ... types True 700 3.14 ‘z’ ......
Num type class
References : [B1] Ch.2, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6, [D1] Week 4 Polymorphic types
a
Polymorphic types
Num a => a polymorphic types with type class
Int Proper types
References : [B1] Ch.2, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6, [D1] Week 2, 4 1. Introduction
Type constructors Type constructors
nullary type constructor
Int
References : [B1] Ch.7 Type constructors
unary type constructor
Maybe Int
“Maybe” type constructor takes one type argument (unary).
References : [B1] Ch.7 Type constructors
nullary unary binary 3-ary ...
Int Maybe Int Either Int Char (,,) Int Int Float
Float IO Int State Int Char :
Int -> Bool [] Int (,) Int Char
[ Int ] (Int, Char) Syntactic sugar Syntactic sugar : : : References : [B1] Ch.7, [H1] Ch.4 1. Introduction
Polymorphic types and type constructors Polymorphic types and type constructors
a
Polymorphic types ?
Int Maybe Int
Type constructors
References : [B1] Ch.2, 7, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6 Polymorphic types and type constructors
a
Polymorphic types t a
Maybe a
Int Maybe Int
Type constructors
References : [B1] Ch.2, 7, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6 Polymorphic types and type constructors
t a
...
Maybe a [] a Tree a IO a
... Polymorphic types
References : [B1] Ch.2, 7, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6 Polymorphic types and type constructors
Maybe a
Polymorphic types
...
Maybe Int Maybe Char Maybe Float Maybe Bool
Proper types ...
References : [B1] Ch.2, 7, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6 Polymorphic types and type constructors
a
nullary unary binary 3-ary ... t a t a b t a b c Polymorphic types ......
Proper Int Maybe Int Either Int Char (,,) Int Int Float types
: Float [] Int (,) Int Char
Int -> Bool IO Int State Int Char
: : : References : [B1] Ch.2, 7, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6 2. more, Types and Type classes 2. more, Types and Type classes
Function types Function type
Int fun :: Int -> Bool Bool
fun
The “->” represents the function type.
References : [B2] Ch.2, [B1] Ch.5, [B3] Ch.7, [B5] Ch.9, [H1] Ch.4 Function type with multiple arguments
equivalent to f :: Int -> (Float -> Bool) Int fun :: Int -> Float -> Bool Bool fun Float
Int fun :: Int -> Float -> Char -> Bool Float Bool fun
Char
References : [B2] Ch.2, [B1] Ch.5, [B3] Ch.7 Function type with same type
Int fun :: Int -> Int -> Int
fun
References : [B2] Ch.2, [B1] Ch.5, [B3] Ch.7 Function type with function as argument
Int -> Int
Int -> Int fun :: (Int -> Int) -> Int -> Bool Bool
fun
Int
References : [B2] Ch.2, [B1] Ch.5, [B3] Ch.7 Function type with function as result
Int fun :: Int -> (Int -> Int) Int -> Int
fun Int -> Int
References : [B2] Ch.2, [B1] Ch.5, [B3] Ch.7 Function type with polymorphic function
a fun :: a -> Bool Bool fun
Polymorphic types Parametric polymorphism
... Proper types
Int fun :: Int -> Bool Bool Char fun :: Char -> Bool Bool fun fun
References : [B2] Ch.2, [B1] Ch.5, [B3] Ch.7 Function type for polymorphic function with type class
a fun :: Num a => a -> Bool Bool fun
Polymorphic types limited with the type class
...... Proper types
Int fun :: Int -> Bool Bool Char fun :: Char -> Bool Bool fun fun
References : [B2] Ch.2, [B1] Ch.5, [B3] Ch.7 2. more, Types and Type classes
Type class operations A type class has the class operations
Class a => a class operations (class methods / common operations)
?
References : [B1] Ch.2, 7, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6 A type class has the class operations
Eq a => a Bool Eq a => a -> a -> Bool
==
Eq class has “==“ (equality) operation.
References : [B1] Ch.2, 7, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6 A type class has the class operations
a Eq a => a -> a -> Bool Bool ==
Polymorphic Ad-hoc polymorphism (overloading) types
... Proper types
Int Char Int -> Int -> Bool Bool Char -> Char -> Bool Bool == ==
Each type, that belongs to the type class, must be support the overloaded operations.
References : [B1] Ch.2, 7, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6 Declaration of a type class and instances
class Eq a where (==) :: a -> a -> Bool
a Eq a => a -> a -> Bool Bool ==
Polymorphic types
... Proper types
Int Char Int -> Int -> Bool Bool Char -> Char -> Bool Bool == ==
instance Eq Int where instance Eq Char where (==) = eqInt (==) = ...
References : [B1] Ch.2, 7, [B2] Ch.2, [B3] Ch.3, [B4] Ch.6 3. What is this? What is this ?!
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b foldr = ... ?
References : [B1] Ch.12, 5, 7, [B2] Ch.6, 2, [B3] Ch.7, 3, [B4] Ch.4, 6 What is this ?!
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
b
Foldable t => t a
foldr
a -> b -> b
a b b
References : [B1] Ch.12, 5, 7, [B2] Ch.6, 2, [B3] Ch.7, 3, [B4] Ch.4, 6 What is this ?!
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
b
Foldable t => t a
foldr
a -> b -> b
a b b
References : [B1] Ch.12, 5, 7, [B2] Ch.6, 2, [B3] Ch.7, 3, [B4] Ch.4, 6 What is this ?!
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
b
Foldable t => t a
foldr
a -> b -> b
a b b
References : [B1] Ch.12, 5, 7, [B2] Ch.6, 2, [B3] Ch.7, 3, [B4] Ch.4, 6 What is this ?!
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
b
Foldable t => t a
foldr
a -> b -> b
a b b
References : [B1] Ch.12, 5, 7, [B2] Ch.6, 2, [B3] Ch.7, 3, [B4] Ch.4, 6 What is this ?!
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
b
Foldable t => t a
foldr
a -> b -> b
a b b
References : [B1] Ch.12, 5, 7, [B2] Ch.6, 2, [B3] Ch.7, 3, [B4] Ch.4, 6 What is this ?!
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
b
Foldable t => t a
foldr
a -> b -> b
a b b
References : [B1] Ch.12, 5, 7, [B2] Ch.6, 2, [B3] Ch.7, 3, [B4] Ch.4, 6 What is this ?!
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
b
Foldable t => t a
foldr
a -> b -> b
a b b
References : [B1] Ch.12, 5, 7, [B2] Ch.6, 2, [B3] Ch.7, 3, [B4] Ch.4, 6 Example of polymorphism on foldr b
Foldable t => t a foldr
a -> b -> b Polymorphic types ...
b b
[a] Tree a foldr foldr
a -> b -> b a -> b -> b
References : [B1] Ch.12, 5, 7, [B2] Ch.6, 2, [B3] Ch.7, 3, [B4] Ch.4, 6 Example of polymorphism on foldr
b
[a]
foldr
a -> b -> b Polymorphic types ...
Proper
types Int Int
[Char] [Int] foldr foldr
example Char -> Char -> Int Int -> Int -> Int foldr (+) 0 [1, 2, 3] foldr (*) 1 [1, 2, 3] foldr max 0 [1, 2, 3] : References : [B1] Ch.12, 5, 7, [B2] Ch.6, 2, [B3] Ch.7, 3, [B4] Ch.4, 6 Appendix I – Various types Bool, Char, Int, Float types
Bool Char Int Float
‘a’ 1 1.0 False ‘h’ 2 1.5
True ‘z’ 700 3.14 ......
References : [B2] Ch.2, [B3] Ch.2, [B1] Ch.2, [H1] Ch.6, [S1] Maybe type
Maybe a
Polymorphic types
...
Maybe Bool Maybe Int Maybe Float Maybe Char
Nothing Nothing Nothing Nothing Proper Just 1 Just 1.0 Just ‘a’ Just True types Just 2 Just 1.5 Just ‘h’ Just False Just 700 Just 3.14 Just ‘z’ ......
References : [B1] Ch.7, [S1] List type
Syntactic sugar [a] [] a
Polymorphic types
...
[ Bool ] [ Int ] [ Float ] [ Char ]
[] [] [] [] [1.0] Proper [True] [1] [‘a’] types [1, 2] [1.5, 20.0] [‘h’] [True, False] [700, ..] [3.14] [‘a’, ‘b’, ‘c’] ......
References : [B1] Ch.1, [B2] Ch.4, [B3] Ch.3, [H1] Ch.6, [S1] Either type
Either a b
Polymorphic types
...
Either Bool Int Either Char Int Either Int Float Either Float Char
Left True Left 1 Proper Left ‘a’ Left 1.0 Left False Right 2 Right 1.5 types Left 20.0 Right 7 Right 700 Right 3.14 Right ‘z’ ......
References : [B1] Ch.7, [S1] Tuple (pair) type
Syntactic sugar (a, b) (,) a b
Polymorphic types
...
(Bool, Int) (Char, Int) (Int, Float) (Float, Char)
(True, 1) (‘a’, 2) (1, 1.0) Proper (1.0, ‘A’) (False, 7) (‘b’, 5) (5, 1.5) types (20.0, ‘b’) (True, 10) (‘z’, 700) (100, 3.14) (3.14, ‘z’) ......
References : [B1] Ch.1, [B2] Ch.2, [B3] Ch.3, [H1] Ch.6, [S1] Appendix II – Various type classes Eq class‘s characteristic operations
Eq a => a Bool
==
/=
The Eq class has equality operations.
References : [B1] Ch.1, [B2] Ch.2, [B3] Ch.3, [H1] Ch.6, [S1] Ord class‘s characteristic operations
Ord a => a Bool
<
<=
>
>=
The Ord class has comparison operations.
References : [B1] Ch.1, [B2] Ch.2, [B3] Ch.3, [H1] Ch.6, [S1] Num class‘s characteristic operations
Num a => a
+
-
*
abs
The Num class has arithmetic operations.
References : [B1] Ch.1, [B2] Ch.2, [B3] Ch.3, [H1] Ch.6, [S1] Foldable class‘s characteristic operations
b
Foldable t => t a
foldr
a -> b -> b
References : [B1] Ch.12, [B2] Ch.6, [B3] Ch.7, [D3], [S1] Functor class‘s characteristic operations
example [] b Maybe b Tree b IO b : Functor f => f a Functor f => f b fmap :: Functor f => (a -> b) -> f a -> f b
fmap
a -> b
References : [B1] Ch.7, [D3], [H1] Ch.6, [S1] Applicative class‘s characteristic operations a
pure
Applicative f => f a Applicative f => f b
<*>
Applicative f => f (a -> b)
References : [B1] Ch.11, [D3], [S1] Monad class‘s characteristic operations a
return
Monad m => m a Monad m => m b
>>=
Monad m => a -> m b
References : [B1] Ch.12, [B2] Ch.10, [D3], [H1] Ch.6, [S1] Monoid class‘s characteristic operations
Monoid a => a
mappend
mempty
References : [B1] Ch.12, [D3], [S1] Related topics: monoid laws
Monoid a => a
mappend binary operation
Monoid a => a
mempty mappend unit law
Monoid a => a mappend mappend mappend mappend associative law
same
Programmers should satisfy the monoid laws.
References : [B1] Ch.12, [D3], [S1] Traversable class‘s characteristic operations
Traversable t => t a Traversable t => f (t b)
traverse
a -> f b
References : [D3], [S1] Appendix III – Advanced topics Universally quantified types
forall a . a a
implicitly quantified with universal quantification (∀a .) Polymorphic types
...
Bool Int Float Char
1 1.0 ‘a’ Proper False 2 1.5 ‘h’ types True 700 3.14 ‘z’ ......
References : [B5] Ch.23, [H1] Ch.4, [H2] “GHC Language Features” Kinds and type constructors
nullary unary binary 3-ary ... kind kind kind Maybe :: * -> * Either :: * -> * -> * (,,) :: * -> * -> * -> * kind Int :: * Maybe Int :: * Either Int Char :: * (,,) Int Int Float :: *
Int Maybe Int Either Int Char (,,) Int Int Float
Float IO Int State Int Char
Int -> Bool [] Int (,) Int Char
References : [B1] Ch.7, [B5] Ch.29, [H1] Ch.4 Type systems
TAPL [5] Ch.23 TAPL [5] Ch.29
corresponding to System F corresponding to System Fω
a Polymorphic types t a
(Quantified types) Maybe a
Int Maybe Int
TAPL [5] Ch.30
corresponding to λω
Type constructors (Type operators)
References : [B5] Ch.23, 29, 30 References References
Books
[B1] Learn You a Haskell for Great Good! (LYAH) http://learnyouahaskell.com/
[B2] Thinking Functionally with Haskell (IFPH 3rd edition) http://www.cs.ox.ac.uk/publications/books/functional/
[B3] Programming in Haskell http://www.cs.nott.ac.uk/~pszgmh/book.html
[B4] Real World Haskell (RWH) http://book.realworldhaskell.org/
[B5] Types and Programming Languages (TAPL) https://mitpress.mit.edu/books/types-and-programming-languages
Documents
[D1] CIS 194: Introduction to Haskell http://www.seas.upenn.edu/~cis194/lectures.html
[D2] Type Systems http://dev.stephendiehl.com/fun/004_type_systems.html
[D3] Typeclassopedia http://www.cs.tufts.edu/comp/150FP/archive/brent-yorgey/tc.pdf https://wiki.haskell.org/Typeclassopedia References
Search
[S1] Hoogle https://www.haskell.org/hoogle
Specifications
[H1] Haskell 2010 Language Report https://www.haskell.org/definition/haskell2010.pdf
[H2] The Glorious Glasgow Haskell Compilation System (GHC user’s guide) https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/index.html https://downloads.haskell.org/~ghc/latest/docs/users_guide.pdf References
Furthermore readings
[A1] What I Wish I Knew When Learning Haskell http://dev.stephendiehl.com/hask/
[A2] How to learn Haskell https://github.com/bitemyapp/learnhaskell
[A3] Documentation https://www.haskell.org/documentation
[A4] A Haskell Implementation Reading List http://www.stephendiehl.com/posts/essential_compilers.html
[A5] The GHC reading list https://ghc.haskell.org/trac/ghc/wiki/ReadingList