Type Introduction Illustrated for Casual Haskellers

Type Introduction Illustrated for Casual Haskellers

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

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    0 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us