Copyright (c) 2016-2017 Young W. Lim. Permission is grnted to copy, distribute nd/or modify this document under the terms of the GNU Free Documenttion License, Version 1.2 or ny lter version published by the Free Softwre Foundtion; with no Invrint Sections, no Front-Cover Texts, nd no Bck-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documenttion License". Plese send corrections (or suggestions) to youngwlim@hotmil.com. This document ws produced by using OpenOffice.
Bsed on http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss http://lernyouhskell.com/functors-pplictive-functors-nd-monoids Hskell in 5 steps https://wiki.hskell.org/hskell_in_5_steps 3
Function Definition Function Definition I. squre x = x * x - function type is inferred not efficient Type Inference Function Definition II. squre :: Double -> Double function type declrtion squre x = x * x Function Type Declrtion function type declrtion function definition Type Declrtion the declrtion of n identifier's type the identifier nme :: the type nme... type nmes in Hskell lwys begin with cpitl letter, identifier nmes (including function identifiers) must lwys begin with lower-cse letter http://www.toves.org/books/hsfun/ 4
Function Types nd Type Clsses Function Definition I. squre x = x * x function definition = Function Definition II. squre :: Double -> Double squre x = x * x function definition function type declrtion = type clss set of types function type 1 function type 2 function type n http://www.toves.org/books/hsfun/ 5
Typeclsses nd Instnces Typeclsses re like interfces defines some behvior compring for equlity compring for ordering enumertion such behvior is defined by function type declrtion only function definition Instnces of tht typeclss types possessing such behvior function definition (==) :: -> -> Bool - type declrtion x == y = not (x /= y) function type (==) :: -> -> Bool - type declrtion A function definition cn be overloded http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 6
Typeclsses nd Type Typeclsses re like interfces defines some behvior compring for equlity compring for ordering enumertion type is n instnce of typeclss implies the function types declred by the typeclss re defined (implemented) in the instnce so tht we cn use the functions tht the typeclss defines with tht type Instnces of tht typeclss types possessing such behvior No reltion with clsses in Jv or C++ http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 7
Cr Type Exmple the Eq typeclss defines the functions == nd /= type Cr compring two crs c1 nd c2 with the equlity function == The Cr type is n instnce of Eq typeclss Instnces : vrious types Typeclss : group or clss of these similr types type Cr type Bg type Phone instnces Eq typeclss functions == nd /= http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 8
TrfficLight Type Exmple (1) clss Eq where (==) :: -> -> Bool - type declrtion (/=) :: -> -> Bool - type declrtion x == y = not (x /= y) - function definition x /= y = not (x == y) - function definition dt TrfficLight = Red Yellow Green instnce Eq TrfficLight where Red == Red = True Green == Green = True Yellow == Yellow = True _ == _ = Flse ghci> Red == Red True ghci> Red == Yellow Flse ghci> Red `elem` [Red, Yellow, Green] True http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 9
TrfficLight Type Exmple (2) clss Show where show :: -> String * * * - type declrtion dt TrfficLight = Red Yellow Green instnce Show TrfficLight where show Red = "Red light" show Yellow = "Yellow light" show Green = "Green light" Instnce type TrfficLight Eq typeclss Show typeclss ghci> [Red, Yellow, Green] [Red light,yellow light,green light] http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 10
Clss Constrints clss (Eq ) => Num where... (Eq ) => Eq typeclss clss Num where... instnce clss constrint on clss declrtion Num typeclss n instnce of Eq before being n instnce of Num Num : subclss of Eq the required function bodies cn be defined in the clss declrtion n instnce declrtions, we cn sfely use == becuse is prt of Eq http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 11
Clss Constrints : clss & instnce declrtions clss constrints in clss declrtions to mke typeclss subclss of nother typeclss subclss clss (Eq ) => Num where clss constrints in instnce declrtions to express requirements bout the contents of some type. requirements instnce (Eq x, Eq y) => Eq (Pir x y) where Pir x0 y0 == Pir x1 y1 = x0 == x1 && y0 == y1 http://cmsc-16100.cs.uchicgo.edu/2016/lectures/07-type-clsses.php http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 12
Clss constrints in instnce declrtion exmples instnce (Eq m) => Eq (Mybe m) where Just x == Just y = x == y Nothing == Nothing = True _ == _ = Flse Eq m instnce (Eq x, Eq y) => Eq (Pir x y) where Pir x0 y0 == Pir x1 y1 = x0 == x1 && y0 == y1 Eq (Pir x y) Eq x Eq y Derived instnce http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 13
A Concrete Type nd Type Constructor : concrete type Mybe : not concrete type : type constructor tht tkes one prmeter produces concrete type. Mybe : concrete type http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 14
Functor typeclss the Functor typeclss is bsiclly for things tht cn be mpped over ex) mpping over lists the list type is prt of the Functor typeclss http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 15
Functor typeclss clss Functor f where fmp :: ( -> b) -> f -> f b func b The Functor typeclss defines one function, fmp no defult implementtion the type vrible f not concrete type ( concrete type cn hold vlue) type constructor tking one type prmeter f fmp function fmp function func type constructor f f b Mybe Int : concrete type Mybe : type constructor tht tkes one type s the prmeter http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 16
Function mp & fmp clss Functor f where fmp :: ( -> b) -> f -> f b fmp tkes function from one type to nother ( -> b) Functor f pplied with one type (f ) fmp returns Functor f pplied with nother type (f b) function type type ( -> b) -> f -> f b func mp :: ( -> b) -> [] -> [b] mp tkes function from one type to nother (* 2) tke list of one type [ 1, 2, 3 ] returns list of nother type [ 2, 4, 6 ] http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 17
List : n instnce of the Functor typeclss clss Functor f where fmp :: ( -> b) -> f -> f b func b mp :: ( -> b) -> [] -> [b] f fmp f b mp is just fmp tht works only on lists list is n instnce of the Functor typeclss. function fmp function func type constructor f instnce Functor [ ] where fmp = mp func b f : type constructor tht tkes one type [ ] : type constructor tht tkes one type [] : concrete type ([Int], [String] or [[String]] ) mp [ ] [ b ] http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 18
List Exmples clss Functor f where fmp :: ( -> b) -> f -> f b mp :: ( -> b) -> [] -> [b] instnce Functor [ ] where fmp = mp mp :: ( -> b) -> [] -> [b] ghci> fmp (*2) [1..3] [2,4,6] ghci> mp (*2) [1..3] [2,4,6] *2 1 2 mp [1,2,3] [2,4,6] http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 19
Mybe : n instnce of the Functor typeclss clss Functor f where fmp :: ( -> b) -> f -> f b func b instnce Functor Mybe where fmp func (Just x) = Just (func x) f fmp f b fmp func Nothing = Nothing f Mybe func b f Mybe f b Mybe b Mybe fmp Mybe b ( -> b) func http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 20
Mybe : type constructor clss Functor f where fmp :: ( -> b) -> f -> f b func b instnce Functor Mybe where fmp func (Just x) = Just (func x) f fmp f b fmp func Nothing = Nothing f : type vrible func b f : type constructor tking one type prmeter Mybe : n instnce of Functor typeclss Mybe fmp Mybe b http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 21
Mybe : n rgument to fmp, together with clss Functor f where fmp :: ( -> b) -> f -> f b func b instnce Functor Mybe where fmp func (Just x) = Just (func x) f fmp f b fmp func Nothing = Nothing func b fmp :: ( -> b) -> f -> f b Mybe fmp Mybe b fmp func (Just x) = Just (func x) Just x Just (func x) fmp func Nothing = Nothing Nothing Nothing http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 22
A function rgument to fmp nd Functor f clss Functor f where fmp :: ( -> b) -> f -> f b f f b instnce Functor Mybe where fmp func (Just x) = Just (func x) func f fmp f b fmp func Nothing = Nothing instnce Functor Mybe where func b fmp f (Just x) = Just ( f x) f fmp f Nothing = Nothing f b f is different from the type constructor f func : -> b Mybe fmp Mybe b f : -> b http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 23
Mybe Exmples (1) clss Functor f where fmp :: ( -> b) -> f -> f b f func b instnce Functor Mybe where fmp f (Just x) = Just (f x) f f fmp f b fmp f Nothing = Nothing ghci> fmp (*2) (Just 200) Just 400 ghci> fmp (*2) Nothing Nothing *2 200 400 fmp Just 200 Just 400 http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 24
Mybe Exmples (2) clss Functor f where fmp :: ( -> b) -> f -> f b f func b instnce Functor Mybe where fmp f (Just x) = Just (f x) f f fmp f b fmp f Nothing = Nothing ghci> fmp (++ "BBB") (Just "AAA") "AAA" (++ "BBB") "AAABBB" Just "AAABBB" ghci> fmp (++ "BBB") Nothing Nothing Just "AAA" fmp Just "AAABBB" http://lernyouhskell.com/mking-our-own-types-nd-typeclsses#the-functor-typeclss 25
Mybe s functor Functor typeclss: trnsforming one type to nother trnsforming opertions of one type to those of nother Mybe is n instnce of functor type clss Functor provides fmp method mps functions of the bse type (such s Integer) to functions of the lifted type (such s Mybe Integer). https://stckoverflow.com/questions/18808258/wht-does-the-justsyntx-men-in-hskell 26
Mybe s functor A function f trnsformed with fmp cn work on Mybe vlue cse mybevl of Nothing -> Nothing Just vl -> Just (f vl) -- there is nothing, so just return Nothing -- there is vlue, so pply the function to it fther :: Person -> Mybe Person mother :: Person -> Mybe Person f :: Int -> Int fmp f :: Mybe Integer -> Mybe Integer Mybe Integer vlue: m_x fmp f m_x https://stckoverflow.com/questions/18808258/wht-does-the-justsyntx-men-in-hskell 27
Trnsforming opertions Functor provides fmp method mps functions of the bse type (such s Integer) to functions of the lifted type (such s Mybe Integer). f b Int f Int F fmp F b Mybe Int fmp f Mybe Int F Int fmp f F Int https://stckoverflow.com/questions/18808258/wht-does-the-justsyntx-men-in-hskell 28
Mybe s functor m_x : Mybe Integer vlue ( Just 101, Nothing, ) f :: Int -> Int you cn do fmp f m_x to pply the function f directly to the Mybe Integer without worrying whether it is Nothing or not clss Functor f where fmp :: ( -> b) -> f -> f b instnce Functor Mybe where fmp f (Just x) = Just (f x) fmp f Nothing = Nothing Function of ( -> b) fmp f m_x lifted type A Functor f pplied with one type f or f b https://stckoverflow.com/questions/18808258/wht-does-the-justsyntx-men-in-hskell 29
Mybe s functor Cn pply whole chin of lifted Integer -> Integer functions to Mybe Integer vlues nd only hve to worry bout explicitly checking for Nothing once when you're finished. Int f Int F Int g F Int Mybe Int fmp f Mybe Int Mybe F Int fmp g Mybe F Int F Int h Int Mybe F Int fmp h Mybe Int https://stckoverflow.com/questions/18808258/wht-does-the-just-syntx-men-in-hskell 30
Mybe Type Definition The Mybe type definition dt Mybe = Just Nothing deriving (Eq, Ord) clss Functor f where fmp :: ( -> b) -> f -> f b Mybe is n instnce of Eq nd Ord (s bse type) n instnce of Functor n instnce of Mond instnce Functor Mybe where fmp f (Just x) = Just (f x) fmp f Nothing = Nothing For Functor, the fmp function f moves inside the Just constructor nd is identity on the Nothing constructor. For Mond, the bind opertion psses through Just, while Nothing will force the result to lwys be Nothing. https://wiki.hskell.org/mybe 31
Mybe clss The Mybe type definition dt Mybe = Just Nothing deriving (Eq, Ord) Mybe is n instnce of Eq nd Ord (s bse type) n instnce of Functor n instnce of Mond For Functor, the fmp function f moves inside the Just constructor nd is identity on the Nothing constructor. For Mond, the bind opertion psses through Just, while Nothing will force the result to lwys be Nothing. https://wiki.hskell.org/mybe 32
Mond Mond is just specil Functor with extr fetures Monds like IO mp types to new types tht represent "computtions tht result in vlues" cn lift regulr functions into Mond types vi liftm function (like fmp function) liftm trnsform regulr function into "computtions tht results in the vlue obtined by evluting the function." https://stckoverflow.com/questions/18808258/wht-does-the-just-syntx-men-in-hskell 33
Mybe s Mond Mybe is lso Mond represents computtions tht could fil to return vlue" n immedite bort vlueless return in the middle of computtion. enble whole bunch of computtions without explicit checking for errors in ech step computtion on Mybe vlues stops s soon s Nothing is encountered https://stckoverflow.com/questions/18808258/wht-does-the-just-syntx-men-in-hskell 34
Mybe s Mond f::int -> Mybe Int f 0 = Nothing f x = Just x g :: Int -> Mybe Int g 100 = Nothing g x = Just x h ::Int -> Mybe Int h x = cse f x of Just n -> g n Nothing -> Nothing h' :: Int -> Mybe Int h' x = do n <- f x g n if x==0 then Nothing else Just x if x==100 then Nothing else Just x if f x==nothing then Nothing else g n g ( f x) h & h' give the sme results h 0 = h' 0 = h 100 = h' 100 = Nothing; h x = h' x = Just x https://wiki.hskell.org/mybe 35
Mybe s Librry Function When the module is imported import Dt.Mybe mybe :: b->(->b) -> Mybe -> b Applies the second rgument (->b) to the third Mybe, when it is Just x, otherwise returns the first rgument (b). isjust, isnothing Test the rgument, returing Bool bsed on the constructor. ListToMybe, mybetolist Convert to/from one element or empty list. mpmybe A different wy to filter list. https://wiki.hskell.org/mybe 36
Mybe s Mond mybe :: b->(->b) -> Mybe -> b The mybe function tkes defult vlue (b), function (->b), nd Mybe vlue (Mybe ). If the Mybe vlue is Nothing, the function returns the defult vlue. Otherwise, it pplies the function to the vlue inside the Just nd returns the result. >>> mybe Flse odd (Just 3) True >>> mybe Flse odd Nothing Flse https://hckge.hskell.org/pckge/bse-4.10.0.0/docs/dt- Mybe.html 37
Then Opertor (>>) nd do Sttements putstr "Hello" >> putstr " " >> putstr "world!" >> putstr "\n" do { putstr "Hello" ; putstr " " ; putstr "world!" ; putstr "\n" } https://en.wikibooks.org/wiki/hskell/do_nottion 38
Trnslting in do nottion do { ction1 ; ction2 ; ction3 } ction1 >> do { ction2 ; ction3 } ction1 ction2 ction3 do { ction1 ; do { ction2 ; ction3 } } do { ction1 ; do { ction2 ; do { ction3 } } } cn chin ny ctions s long s ll of them re in the sme mond https://en.wikibooks.org/wiki/hskell/do_nottion 39
Bind Opertor (>==) nd do sttements The bind opertor (>>=) psses vlue (the result of n ction or function), downstrem in the binding sequence. ction1 >>= (\ x1 -> ction2 >>= (\ x2 -> mk_ction3 x1 x2 )) nonymous function (lmbd expression) is used do nottion ssigns vrible nme to the pssed vlue using the <- do { x1 <- ction1 ; x2 <- ction2 ; mk_ction3 x1 x2 } https://en.wikibooks.org/wiki/hskell/do_nottion 40
Trnsltion using the bind opertor (>>=) do { x1 <- ction1 ; x2 <- ction2 ; mk_ction3 x1 x2 } ction1 >>= (\ x1 -> ction2 >>= (\ x2 -> mk_ction3 x1 x2 )) ction1 >>= (\ x1 -> ction2 >>= (\ x2 -> mk_ction3 x1 x2 )) ction1 >>= (\ x1 -> ction2 >>= (\ x2 -> mk_ction3 x1 x2 )) ction1 x1 ction2 x2 mk_ction3 https://en.wikibooks.org/wiki/hskell/do_nottion 41
Anonymous Function \x -> x + 1 (\x -> x + 1) 4 5 :: Integer (\x y -> x + y) 3 5 8 :: Integer ddone = \x -> x + 1 Lmbd Expression https://wiki.hskell.org/anonymous_function 42
Functor Typeclss instnce Functor IO where fmp f ction = do result <- ction f b return (f result) IO fmp IO b ction1 result f f ction ( -> b) -> IO -> IO b instnce Functor Mybe where fmp func (Just x) = Just (func x) fmp func Nothing = Nothing f result http://lernyouhskell.com/functors-pplictive-functors-nd-monoids 43
Functor Typeclss min = do line <- getline let line' = reverse line putstrln $ "You sid " ++ line' ++ " bckwrds!" putstrln $ "Yes, you relly sid" ++ line' ++ " bckwrds!" min = do line <- fmp reverse getline putstrln $ "You sid " ++ line ++ " bckwrds!" putstrln $ "Yes, you relly sid" ++ line ++ " bckwrds!" instnce Functor IO where fmp f ction = do result <- ction return (f result) fmp reverse getline = do result <- getline return (reverse result) http://lernyouhskell.com/functors-pplictive-functors-nd-monoids 44
$ Opertor $ opertor to void prentheses Anything ppering fter $ will tke precedence over nything tht comes before. putstrln (show (1 + 1)) putstrln (show $ 1 + 1) putstrln $ show (1 + 1) putstrln $ show $ 1 + 1 https://stckoverflow.com/questions/940382/hskell-difference-between-dot-nd-dollr-sign 45
. Opertor. opertor to chin functions putstrln (show (1 + 1)) (1 + 1) is not function, so the. opertor cnnot be pplied show cn tke n Int nd return String. putstrln cn tke String nd return n IO(). (putstrln. show) (1 + 1) Int String IO() show putstrln putstrln. show $ 1 + 1 https://stckoverflow.com/questions/940382/hskell-difference-between-dot-nd-dollr-sign 46
Functor Typeclss instnce Functor ((->) r) where fmp f g = (\x -> f (g x)) instnce Functor Mybe where fmp f (Just x) = Just (f x) fmp f Nothing = Nothing A function tkes ny thing nd returns ny thing g :: -> b g :: r -> r g g b r g b fmp :: ( -> b) -> f -> f b fmp :: ( -> b) -> ((->) r ) -> ((->) r b) fmp :: ( -> b) -> (r -> ) -> (r -> b) f b g fmp g b http://lernyouhskell.com/functors-pplictive-functors-nd-monoids 47
Functor Typeclss instnce Functor ((->) r) where fmp f g = (\x -> f (g x)) instnce Functor ((->) r) where fmp = (. ) instnce Functor Mybe where fmp f (Just x) = Just (f x) fmp f Nothing = Nothing ghci> :t fmp (*3) (+100) fmp (*3) (+100) :: (Num ) => -> ghci> fmp (*3) (+100) 1 303 ghci> (*3) `fmp` (+100) $ 1 303 ghci> (*3). (+100) $ 1 303 ghci> fmp (show. (*3)) (*100) 1 "300" (*3) fmp (+100) (+100) b b http://lernyouhskell.com/functors-pplictive-functors-nd-monoids 48
Functor Typeclss ghci> :t fmp (*2) fmp (*2) :: (Num, Functor f) => f -> f (*2) f fmp f ghci> :t fmp (replicte 3) fmp (replicte 3) :: (Functor f) => f -> f [] (replicte 3) [ ] fmp f f [ ] http://lernyouhskell.com/functors-pplictive-functors-nd-monoids 49
Functor Typeclss ghci> fmp (replicte 3) [1,2,3,4] [[1,1,1],[2,2,2],[3,3,3],[4,4,4]] ghci> fmp (replicte 3) (Just 4) Just [4,4,4] ghci> fmp (replicte 3) (Right "blh") Right ["blh","blh","blh"] ghci> fmp (replicte 3) Nothing Nothing ghci> fmp (replicte 3) (Left "foo") Left "foo" http://lernyouhskell.com/functors-pplictive-functors-nd-monoids 50
Functor Lws fmp id = id id id :: -> id x = x F fmp F instnce Functor Mybe where fmp func (Just x) = Just (func x) fmp func Nothing = Nothing F Just x id F Just x instnce Functor Mybe where Nothing Nothing fmp f (Just x) = Just (f x) fmp f Nothing = Nothing instnce Functor Mybe where fmp id (Just x) = Just (id x) fmp id Nothing = Nothing http://lernyouhskell.com/functors-pplictive-functors-nd-monoids 51
Functor Typeclss ghci> fmp id (Just 3) Just 3 ghci> id (Just 3) Just 3 ghci> fmp id [1..5] [1,2,3,4,5] ghci> id [1..5] [1,2,3,4,5] ghci> fmp id [] [] ghci> fmp id Nothing Nothing http://lernyouhskell.com/functors-pplictive-functors-nd-monoids 52
Functor Lws fmp (f. g) = fmp f. fmp g f. g fmp (f. g) F = fmp f (fmp g F) F fmp F g f F fmp F g f F fmp fmp F http://lernyouhskell.com/functors-pplictive-functors-nd-monoids 53
Functor Lws fmp (f. g) = fmp f. fmp g fmp (f. g) F = fmp f (fmp g F) instnce Functor Mybe where fmp f (Just x) = Just (f x) fmp f Nothing = Nothing fmp (f. g) Nothing = Nothing fmp f (fmp g Nothing) = Nothing fmp (f. g) (Just x) = Just ((f. g) x) = Just (f (g x)) fmp f (fmp g (Just x)) = fmp f (Just (g x)) = Just (f (g x)) http://lernyouhskell.com/functors-pplictive-functors-nd-monoids 54
References [1] ftp://ftp.geoinfo.tuwien.c.t/nvrtil/hskelltutoril.pdf [2] https://www.umics.umd.edu/~hl/docs/dume02yht.pdf