Share
Embed
sugoih 8
すごいHaskell読書会 in 大阪 2週目 #8 : ATND
https://atnd.org/events/53833
USAMI Kosuke
August 20, 2014
Transcript
55 slides
-
ʢલฤͷ͓͞Β͍ʣ ৽͍͠σʔλܕΛఆٛ͢Δ data ܕ໊ = ίϯετϥΫλ data Bool = False
| True data Shape = Circle Float Float Float | Rectangle Float Float Float Float data Point = Point Float Float -
ʢલฤͷ͓͞Β͍ʣ Ϩίʔυߏจ data Person = Person { firstName :: String
, lastName :: String , age :: Int , height :: Float , phoneNumber :: String , flavor :: String } -
ʢલฤͷ͓͞Β͍ʣ ܕҾ data ܕίϯετϥΫλ = ίϯετϥΫλ ʢܕ໊ ܕҾʣ data Maybe
a = Nothing | Just a data Either a b = Left a | Right b -- ̏࣍ݩϕΫτϧͷྫ data Vector a = Vector a a a -
ʢલฤͷ͓͞Β͍ʣ Πϯελϯεͷࣗಈಋग़ data Day = Monday | Tuesday | Wednesday
| Thursday | Friday | Saturday | Sunday deriving (Eq, Ord, Show, Read, Bounded, Enum) -- ܕΫϥεͷ࣮͕ิΘΕɺҎԼ͕ՄೳʹͳΔ ghci> Saturday == Sunday False ghci> show Wednesday Wednesday -
ʢલฤͷ͓͞Β͍ʣ ܕγϊχϜ type String = [Char] type Name = String
type PhoneNumber = String type PhoneBook = [(Name, PhoneNumber)] -- ҎԼͷྫͰ AssocList ܕίϯετϥΫλ type AssocList k v = [(k, v)] -
ޙฤͷ༰ 4 ࠶ؼతͳσʔλߏ 4 ܕΫϥεதڃߨ࠲ 4 Yes ͱ No ͷܕΫϥε
4 Functor ܕΫϥε 4 ܕΛ࢘Δͷɺछྨ -
࠶ؼతͳσʔλܕ 4 ίϯετϥΫλͷϑΟʔϧυʹࣗࣗͷܕΛ࣋ͨͤΔ 4 ྫ͑ɺϦετ࠶ؼతͳσʔλܕ 4 [3,4,5,6] 3:[4,5,6] Ͱ͋Δ
4 3:4:[5,6] Ͱ͋Δ 4 3:4:5:[6] 4 3:4:5:6:[] -
ྫɿಠࣗϦετܕ data List a = Empty | Cons a (List
a) deriving (Show, Read, Eq, Ord) -- Ϩίʔυߏจͷ߹ data List a = Empty | Cons { listhead :: a , listtail :: List a } deriving (Show, Read, Eq, Ord) -
ಠࣗϦετܕͷϦετදݱ 4 ઌͷಠࣗఆٛͷ Cons ඪ४ͷ : ʹ͋ͨΔͷ Empty -- []
ʹ͋ͨΔ 5 `Cons` Empty -- 5:[] ʹ͋ͨΔ 4 `Cons` (5 `Cons` Empty) -- 4:5:[] ʹ͋ͨΔ 3 `Cons` (4 `Cons` (5 `Cons` Empty)) -- 3:4:5:[] ʹ͋ͨΔ -
ಠࣗϦετܕͷվળ 4 ίϯετϥΫλΛதஔؔʹʢ: Ͱ࢝ΊΔ͜ͱʣ 4 ݁߹ͷ༏ઌΛఆٛʢinfixl infixr Λ͏ʣ infixr
5 :-: data List a = Empty | a :-: (List a) deriving (Show, Read, Eq, Ord) -
վળ݁Ռ 4 ϦετΛ͜͏ॻ͚ΔΑ͏ʹͳͬͨ 5 :-: Empty -- 5 `Cons` Empty
4 :-: 5 :-: Empty -- 4 `Cons` (5 `Cons` Empty) 3 :-: 4 :-: 5 :-: Empty -- 3 `Cons` (4 `Cons` (5 `Cons` Empty)) -
ྫɿಠࣗϦετܕͷ݁߹ infixr 5 ^++ (^++) :: List a -> List
a -> List a Empty ^++ ys = ys (x :-: xs) ^++ ys = x :-: (xs ^++ ys) 4 ࣮ύλʔϯϚονͱɺίϯετϥΫλͷϚον 4 ඪ४ͷ : ίϯετϥΫλ -
ผͷྫɿೋ୳ࡧ data Tree a = EmptyTree | Node a (Tree
a) (Tree a) deriving (Show) 4 ೋ୳ࡧɿ֤ཁૉʹ͍ͭͯɺ 4 ࠨ෦ͷશཁૉ͕ͦͷཁૉΑΓখ͍͞ 4 ӈ෦ͷશཁૉ͕ͦͷཁૉΑΓେ͖͍ -
ʹཁૉΛՃ͢Δؔ 4 ༩͑ΒΕͨͷߋ৽ෆՄɺ৽͍͠Λ࡞ͬͯฦ͢ singleton :: a -> Tree a singleton
x = Node x EmptyTree EmptyTree treeInsert :: (Ord a) => a -> Tree a -> Tree a treeInsert x EmptyTree = singleton x treeInsert x (Node y left right) | x == y = Node y left right -- ಉ͡ͳΒૠೖ͠ͳ͍ | x < y = Node y (treeInsert x left) right | x > y = Node y left (treeInsert x right) -
ͷੜ ghci> let nums = [8,6,4,1,7,3,5] ghci> let numsTree =
foldr treeInsert EmptyTree nums 4 tree1 = treeInsert 5 EmptyTree 4 tree2 = treeInsert 3 tree1 4 tree3 = treeInsert 7 tree2 4 ... 4 numsTree = treeInsert 8 tree7 -
ʹཁૉ͕ଐ͢Δ͔ௐΔؔ treeElem :: (Ord a) => a -> Tree a
-> Bool treeElem x EmptyTree = False treeElem x (Node y left right) | x == y = True | x < y = treeElem x left | x > y = treeElem x right -
ܕΫϥεதڃߨ࠲ 4 ܕΫϥεͷ෮श 4 ͋Δܕ T ͕͋ΔܕΫϥε C ͷΠϯελϯε =
ܕ T ʹରͯ͠ܕΫϥε C ͕ఆٛ͢ΔؔΛ͑Δ 4 ܕΫϥεΛࣗͰ࡞Δʹʁ -
Eq ܕΫϥεͷએݴ class Eq a where (==) :: a ->
a -> Bool (/=) :: a -> a -> Bool x == y = not (x /= y) x /= y = not (x == y) 4 a ܕҾͰɺEq ͷΠϯελϯεͱͳΔܕ -
Eq ܕΫϥεͷΠϯελϯεએݴ data TrafficLight = Red | Yellow | Green
instance Eq TrafficLight where Red == Red = True Yellow == Yellow = True Green == Green = True _ == _ = False 4 ҙɿ௨ৗ deriving Ͱࣗಈಋग़͢Δ -
࠷খશఆٛ x == y = not (x /= y) x
/= y = not (x == y) 4 Eq ͷΠϯελϯε == ͔ /= ͷͲͪΒ͔Λఆٛ͢Δඞཁ͕ ͋Δ 4 ยํΛఆٛʢσϑΥϧτ࣮Λ্ॻ͖ʣ͢Εɺ͏ยํ ఆٛ͞ΕΔ -
Show ܕΫϥεͷΠϯελϯεએݴ 4 ࣗಈಋग़ͰͷఆٛͰͳ͘ɺࣗલͰఆ͍ٛͨ͠߹ʹ instance Show TrafficLight where show Red
= "Red light" show Yellow = "Yellow light" show Green = "Green light" -
ܕΫϥεએݴʹܕΫϥε੍Λ͚ͭ Δ 4 Num ܕΫϥε Eq ܕΫϥεͷαϒΫϥε class (Eq a)
=> Num a where ... -
Maybe Λ Eq ͷΠϯελϯεʹ͢ Δʁ class Eq a where (==)
:: a -> a -> Bool 4 ܕҾ a ۩ମܕͰͳͯ͘μϝʢؔఆٛʹ͋Θͳ͍ʣ 4 Maybe ۩ମܕͰͳ͘ଟ૬ܕʢܕίϯετϥΫλʣ 4 ͡Ό͋۩ମܕ Maybe Char Λ Eq ͷΠϯελϯεʹ͢Δʁ 4 ͦΕͰ Maybe Int ʁ ɾɾɾ͖Γ͕ͳ͍ -
ଟ૬ܕΛܕΫϥεͷΠϯελϯεʹ ͢Δ 4 ΠϯελϯεએݴʹܕҾΛ͚ͭΔ instance Eq (Maybe m) where Just
x == Just y = x == y Nothing == Nothing = True _ == _ = False 4 ҙɿ͜ͷએݴෆेʢ࣍ϖʔδࢀরʣ -
ΠϯελϯεએݴʹܕΫϥε੍Λ ͚ͭΔ 4 ܕ m ͕ Eq ܕΫϥεͰͳ͍ͱμϝ 4 ܕΫϥε੍Λ͚ͭΔ
instance (Eq m) => Eq (Maybe m) where Just x == Just y = x == y Nothing == Nothing = True _ == _ = False -
࠶ཧͯ͠ΈΔ 4 Eq ܕΫϥεએݴͰɺa ͕۩ମܕͰͳ͍ͱμϝ 4 (==) :: a ->
a -> Bool ͱΘΕΔ͔Β 4 a ʹ Maybe ͤͳ͍ 4 (==) :: Maybe -> Maybe -> Bool μϝ 4 a ʹ Maybe m ͳΒͤΔ 4 (==) :: (Eq m) => Maybe m -> Maybe m -> Bool -
Yes ͱ No ͷܕΫϥε 4 Haskell Ͱਅཧ͕ඞཁͳՕॴͰݫີʹ Bool ܕΛ͏ 4
ଞͷݴޠͰͦ͏Ͱͳ͍ͷ͋ΔɿJavaScript ͷྫ 4 if (0) 4 if ("") 4 if (false) 4 ྨࣅͷ͜ͱΛಠࣗͷܕΫϥεͰ࣮ݱͯ͠ΈΔ -
ܕΫϥεએݴͱΠϯελϯεએݴ class YesNo a where yesno :: a -> Bool
instance YesNo Int where yesno 0 = False yesno _ = True instance YesNo [a] where yesno [] = False yesno _ = True -
instance YesNo Bool where yesno = id -- ͦͷ··ฦ͢ instance
YesNo (Maybe a) where yesno Nothing = False yesno (Just _) = True instance YesNo (Tree a) where yesno EmptyTree = False yesno _ = True instance YesNo TrafficLight where yesno Red = False yesno _ = True -
if ͷସ yesnoIf :: (YesNo y) => y -> a
-> a -> a yesnoIf yesnoVal yesResult noResult = if yesno yesnoVal then yesResult else noResult ghci> yesnoIf [] "YEAH!" "NO!" "NO!" ghci> yesnoIf [2,3,4] "YEAH!" "NO!" "YEAH!" -
Functor ܕΫϥε class Functor f where fmap :: (a ->
b) -> f a -> f b 4 ͜ͷ f ۩ମܕͰͳ͘ɺܕίϯετϥΫλ 4 Ҿ̍ : ܕ a ͔Βܕ b ͷؔܕ 4 Ҿ̎ : ܕίϯετϥΫλ f ʹܕҾ a Λద༻ͨ͠ܕ 4 Γ : ܕίϯετϥΫλ f ʹܕҾ b Λద༻ͨ͠ܕ -
Ϧετͷ map -- Ϧετͷ map map :: (a -> b)
-> [a] -> [b] -- Functor ͷ fmap fmap :: (a -> b) -> f a -> f b 4 map ϦετݶఆͰಈ࡞͢Δ fmap ͩͱ͍͑Δ -
Ϧετͷ Functor Πϯελϯεએݴ instance Functor [] where fmap = map
4 ͜͜Ͱɺ[] ܕίϯετϥΫλͰ͋Δ͜ͱʹҙ 4 [Int] [String] ͳͲ͕۩ମܕ -
Maybe ͷ Functor Πϯελϯεએݴ instance Functor Maybe where fmap f
(Just x) = Just (f x) fmap f Nothing = Nothing 4 Maybe ܕίϯετϥΫλ -
ೋ୳ࡧͷ Functor Πϯελϯε એݴ instance Functor Tree where fmap f
EmptyTree = EmptyTree fmap f (Node x left right) = Node (f x) (fmap f left) (fmap f right) 4 Tree ܕίϯετϥΫλʢTree a ͕۩ମܕʣ 4 ҙɿҙͷؔΛద༻ͨ͠ޙೋ୳ࡧͷੑ࣭Λอͨ ͳ͍ -
Either ͷ߹ʁ 4 Either ܕҾΛ 2 ͭͱΔܕίϯετϥΫλ 4 Either a
b ͕۩ମܕ 4 1 ͚ͭͩ෦ద༻͢Δͱ Functor ʹͰ͖Δ 4 Either a ܕҾΛ 1 ͭͱΔܕίϯετϥΫλ -
Either ͷ Functor Πϯελϯεએݴ instance Functor (Either a) where fmap
f (Right x) = Right (f x) fmap f (Left x) = Left x 4 data Either a b = Left a | Right b 4 Left ͱ Right Ͱܕ͕ҧ͏͜ͱʹҙ 4 fmap Ͱద༻͢Δؔ f b -> c ܕ 4 Right ଆʹద༻Ͱ͖Δ͕ɺLeft ଆʹద༻Ͱ͖ͳ͍ -
Data.Map ͷ߹ʁʢ࿅शʣ 4 Map k v k ܕ͕Ωʔɺv ܕ͕ͷσʔλߏ
4 fmap Ͱద༻͢Δؔ v -> v' ܕ 4 ɿͲͷΑ͏ʹ Functor ͷΠϯελϯεʹͳΔ͔ʁ 4 MyFunctor ܕΫϥεએݴΛॻ͘ 4 Map ͷ MyFunctor ΠϯελϯεએݴΛॻ͘ 4 Map ʹ myfmap ͰؔΛద༻͢Δ -
ܕͷछྨ ghci> :k Int Int :: * ghci> :k Maybe
Maybe :: * -> * ghci> :k Maybe Int Maybe Int :: * 4 * ۩ମܕΛද͢ه߸ 4 Maybe ۩ମܕΛҾʹͱͬͯ۩ମܕΛฦ͢ -
ɺܕɺछྨ 4 ͷϥϕϧ͕ܕ 4 ܕͷϥϕϧ͕छྨ 4 GHCi ͷ :t ίϚϯυͷܕΛௐΔ
4 GHCi ͷ :k ίϚϯυܕͷछྨΛௐΔ -
ܕίϯετϥΫλͷ෦ద༻ 4 ܕίϯετϥΫλ͕ΧϦʔԽ͞Ε͍ͯͯ෦ద༻Ͱ͖Δ ghci> :k Either Either :: * ->
* -> * ghci> :k Either String Either String :: * -> * -
Functor ʹͳΕΔܕͷछྨ class Functor f where fmap :: (a ->
b) -> f a -> f b 4 f a f b ͷछྨ *ʢؔͷܕએݴʹΘΕ͍ͯΔͨΊʣ 4 f ͷछྨ * -> * Ͱͳͯ͘ͳΒͳ͍ -
ୈ7ষޙฤ͓ΘΓ 4 ࠶ؼతͳσʔλߏ 4 ܕΫϥεதڃߨ࠲ 4 Yes ͱ No ͷܕΫϥε
4 Functor ܕΫϥε 4 ܕΛ࢘Δͷɺछྨ