Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

もうちょっとだけモナド

 もうちょっとだけモナド

第7回 スタートHaskell2 (最終回) まだまだモナド (Reader, Writer, State, Error, ...) 発表資料

Fujimura Daisuke

January 20, 2013
Tweet

More Decks by Fujimura Daisuke

Other Decks in Programming

Transcript

  1. r14 Þ Øq«ł› “¦Žéè r7 ‚ ÕÜß çHaskell2 ( ^¥‚)

    —“—“Žéè (Reader, Writer, State, Error, ...) oÚç© ̋Fq∙ (@ffu_ / http://fujimuradaisuke.com/)
  2. Š̊â∙ ̋Fq∙ (@ffu_ / http://fujimuradaisuke.com/) ô ß Õ”ö Ì þß

    °ì »;•Ruby ³̈o–o− Haskell é»Ñ Êzœo ̀l”øq•Web ¼ö Íß Ó äÕç” RR•ÙÛwmß—¡ Hspec WAI ¼ö Íß Ó ”äÕ糡− ³¿ jÚ•̈x —|¤ Haskell JSON Web API ³¢− 䣕¶©−vØø
  3. ̃”Õ ¾è•fio– Literate Haskell + Markdown ̈o– pandoc Õ ¾è³¢{|–o—

    ¡ p a n d o c - t s l i d y - s f o r _ a _ f e w _ m o n a d s _ m o r e . l h s - o f o r _ a _ f e w _ m o n a d s _ m o r e . h t m l ¡− Õ ¾èw x—¡ g h c i f o r _ a _ f e w _ m o n a d s _ m o r e . l h s ¡− ̃”ô¹¾ 1\|¤9cw㣗¡ oq̃ «ł› ŽÔ™ß ³¾ ýß ç|—¡„… { - # L A N G U A G E F l e x i b l e C o n t e x t s # - } i m p o r t D a t a . M o n o i d i m p o r t C o n t r o l . M o n a d . W r i t e r i m p o r t C o n t r o l . M o n a d . S t a t e i m p o r t C o n t r o l . M o n a d . I n s t a n c e s i m p o r t S y s t e m . R a n d o m
  4. Ê‾ Ì”<c i s B i g G a n

    g ' : : I n t - > B o o l i s B i g G a n g ' x = x > 9 * M a i n > i s B i g G a n g ' 1 9 T r u e
  5. Ê‾ Ì”<c+ c³|¤v i s B i g G a

    n g : : I n t - > ( B o o l , S t r i n g ) i s B i g G a n g x = ( x > 9 , " C o m p a r e d g a n g s i z e t o 9 " ) * M a i n > i s B i g G a n g 1 9 ( T r u e , " C o m p a r e d g a n g s i z e t o 9 " )
  6. Í c³|¤vÎ oq5è³Z›¤—— \œ•<c³ÞÂáË a p p l y L

    o g ' : : ( a , S t r i n g ) - > - - 5èôx”ß ( a - > ( b , S t r i n g ) ) - > - - ß³ ›– 5èôx”I”ß³S¡ ( b , S t r i n g ) - - 5èôx”ß a p p l y L o g ' ( x , l o g ) f = l e t ( y , n e w L o g ) = f x i n ( y , l o g + + n e w L o g ) * M a i n > ( 3 , " S m a l l i s h g a n g . " ) ` a p p l y L o g ' ` i s B i g G a n g ( F a l s e , " S m a l l i s h g a n g . C o m p a r e d g a n g s i z e t o 9 " ) * M a i n > ( 3 0 , " A f r e a k i n g p l a t o o n . " ) ` a p p l y L o g ' ` i s B i g G a n g ( T r u e , " A f r e a k i n g p l a t o o n . C o m p a r e d g a n g s i z e t o 9 " )
  7. Í c³|¤vÎÙ Ì ³ Õçº ByteString l |¤o a p

    p l y L o g : : ( M o n o i d m ) = > - - Ží¾èß Õç vB y t e S t r i n g v ( a , m ) - > - - Ží¾èßôx”ß ( a - > ( b , m ) ) - > - - ß³ ›– Ží¾èßôx”I”ß³S¡ ( b , m ) - - Ží¾èßôx”ß a p p l y L o g ( x , l o g ) f = l e t ( y , n e w L o g ) = f x i n ( y , l o g ` m a p p e n d ` n e w L o g ) Üö »Í ß Ží¾èß”u—¦Î •¶ß—|¤
  8. ×áç”âÆ'³ g¡−9c u—¦ôx;³³›– a d d D r i n

    k ̊'•×áç”âÆ'³fi¦fifi µS³Sæ|—¡ - - i m p o r t D a t a . M o n o i d t y p e F o o d = S t r i n g t y p e P r i c e = S u m I n t a d d D r i n k : : F o o d - > ( F o o d , P r i c e ) a d d D r i n k " b e a n s " = ( " m i l k " , S u m 2 5 ) a d d D r i n k " j e r k y " = ( " w h i s k e y " , S u m 9 9 ) a d d D r i n k _ = ( " b e e r " , S u m 3 0 ) ¶µ ̊'•fizâÆ'”CÔ»Ó̊—£µ g h c i > ( " b e a n s " , S u m 1 0 ) ` a p p l y L o g ` a d d D r i n k ( " m i l k " , S u m { g e t S u m = 3 5 } ) g h c i > ( " j e r k y " , S u m 2 5 ) ` a p p l y L o g ` a d d D r i n k ( " w h i s k e y " , S u m { g e t S u m = 1 2 4 } ) g h c i > ( ( " j e r k y " , S u m 1 0 ) ` a p p l y L o g ` a d d D r i n k ) ` a p p l y L o g ` a d d D r i n k ( " b e e r " , S u m { g e t S u m = 1 3 9 } ) Ô‾ß Éß + Á¾ÕÉß + òß âª”•w∙”øq¶…
  9. u—¦ôx = Writer ;“›¤ u—¦ôx³Ñ«a|¤Ø”wWriter ¡ Control.Monad.Writer wWriter w a

    ›–; ƒ”Monad ¾ ÕÜ Õ Ufl 9c³:µ|–o—¡ n e w t y p e W r i t e r w a = W r i t e r { r u n W r i t e r : : ( a , w ) } i n s t a n c e ( M o n o i d w ) = > M o n a d ( W r i t e r w ) w h e r e r e t u r n x = W r i t e r ( x , m e m p t y ) ( W r i t e r ( x , v ) ) > > = f = l e t ( W r i t e r ( y , v ' ) ) = f x i n W r i t e r ( y , v ` m a p p e n d ` v ' ) > > = »a p p l y L o g ƒ›zß ¡„ a p p l y L o g Üö ³S|–o¤ ̃¬ ³W r i t e r •|—|¤ r e t u r n » ”Ží¾èß W r i t e r ³¢›–S|–o—¡ i× ¡ g h c i > r u n W r i t e r ( r e t u r n 3 : : W r i t e r S t r i n g I n t ) ( 3 , " " ) g h c i > r u n W r i t e r ( r e t u r n 3 : : W r i t e r ( S u m I n t ) I n t ) ( 3 , S u m { g e t S u m = 0 } )
  10. g h c i > r u n W r

    i t e r ( r e t u r n 3 : : W r i t e r ( P r o d u c t I n t ) I n t ) ( 3 , P r o d u c t { g e t P r o d u c t = 1 } )
  11. Writer ³do l¦ ³q - - i m p o

    r t C o n t r o l . M o n a d . W r i t e r l o g N u m b e r : : I n t - > W r i t e r [ S t r i n g ] I n t l o g N u m b e r x = w r i t e r ( x , [ " G o t n u m b e r : " + + s h o w x ] ) m u l t W i t h L o g : : W r i t e r [ S t r i n g ] I n t m u l t W i t h L o g = d o a < - l o g N u m b e r 3 b < - l o g N u m b e r 5 t e l l [ " G o n n a m u l t i p l y t h e s e t w o " ] r e t u r n ( a * b ) g h c i > r u n W r i t e r m u l t W i t h L o g ( 1 5 , [ " G o t n u m b e r : 3 " , " G o t n u m b e r : 5 " , " G o n n a m u l t i p l y t h e s e t w o " ] )
  12. ( Vœ) Writer ³ŽéèånÚl ³q { - # L A

    N G U A G E M o n a d C o m p r e h e n s i o n s # - } ¡© d o ” )³ Õç ånÚl”øq¶l¦ ̈¦—¡ í|z»̃«œ Monad comprehensions l o g N u m b e r x : : I n t - > W r i t e r [ S t r i n g ] I n t l o g N u m b e r x = w r i t e r ( x , [ " G o t n u m b e r : " + + s h o w x ] ) ✈ g h c i f o r _ a _ f e w _ m o n a d s _ m o r e . l h s - X M o n a d C o m p r e h e n s i o n s g h c i > r u n W r i t e r [ a * b | a < - l o g N u m b e r 3 , b < - l o g N u m b e r 2 ] ( 6 , [ " G o t n u m b e r : 3 " , " G o t n u m b e r : 2 " ] ) v›̃oo ¡̈
  13. Õç”g ( + + ) : : [ a ]

    - > [ a ] - > [ a ] ( + + ) [ ] y s = y s ( + + ) ( x : x s ) y s = x : x s + + y s
  14. ìg [ 1 , 2 , 3 ] + +

    ( [ 4 , 5 , 6 ] + + [ 7 , 8 , 9 ] ) [ 1 , 2 , 3 ] + + ( 4 : [ 5 , 6 ] + + [ 7 , 8 , 9 ] ) [ 1 , 2 , 3 ] + + ( 4 : 5 : [ 6 ] + + [ 7 , 8 , 9 ] ) [ 1 , 2 , 3 ] + + ( 4 : 5 : 6 : [ ] + + [ 7 , 8 , 9 ] ) [ 1 , 2 , 3 ] + + [ 4 , 5 , 6 , 7 , 8 , 9 ] 1 : [ 2 , 3 ] + + [ 4 , 5 , 6 , 7 , 8 , 9 ] 1 : 2 : [ 3 ] + + [ 4 , 5 , 6 , 7 , 8 , 9 ] 1 : 2 : 3 : [ ] + + [ 4 , 5 , 6 , 7 , 8 , 9 ] - - 7 ‚ [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] 8 ‚ x—|¤
  15. Og ( [ 1 , 2 , 3 ] +

    + [ 4 , 5 , 6 ] ) + + [ 7 , 8 , 9 ] ( 1 : [ 2 , 3 ] + + [ 4 , 5 , 6 ] ) + + [ 7 , 8 , 9 ] ( 1 : 2 : [ 3 ] + + [ 4 , 5 , 6 ] ) + + [ 7 , 8 , 9 ] ( 1 : 2 : 3 : [ ] + + [ 4 , 5 , 6 ] ) + + [ 7 , 8 , 9 ] [ 1 , 2 , 3 , 4 , 5 , 6 ] + + [ 7 , 8 , 9 ] 1 : [ 2 , 3 , 4 , 5 , 6 ] + + [ 7 , 8 , 9 ] - - ̃̃»\›xº›¤ 1 : 2 : [ 3 , 4 , 5 , 6 ] + + [ 7 , 8 , 9 ] - - ̃̃»\›xº›¤ 1 : 2 : 3 : [ 4 , 5 , 6 ] + + [ 7 , 8 , 9 ] - - ̃̃»\›xº›¤ 1 : 2 : 3 : 4 : [ 5 , 6 ] + + [ 7 , 8 , 9 ] 1 : 2 : 3 : 4 : 5 : [ 6 ] + + [ 7 , 8 , 9 ] 1 : 2 : 3 : 4 : 5 : 6 : [ ] + + [ 7 , 8 , 9 ] - - 1 0 ‚ [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] 10 ‚vvß—|¤ Ñfi ” Õç³÷̃öÀ|–o—¡
  16. Õç³Og ++ ¡− ô7wvv− (++) »ìg ¶” Q©O•» t¶o ¡

    g h c i > : i ( + + ) ( + + ) : : [ a ] - > [ a ] - > [ a ] - - D e f i n e d i n ` G H C . B a s e ' i n f i x r 5 + + |v|{ß xö ( ( ( ( a + + b ) + + c ) + + d ) + + e ) + + f ”øq¶Og ”™•¶›–|—q̃ Øm− |łq
  17. P) Õç³³uq n e w t y p e D

    i f f L i s t a = D i f f L i s t { g e t D i f f L i s t : : [ a ] - > [ a ] } t o D i f f L i s t : : [ a ] - > D i f f L i s t a t o D i f f L i s t x s = D i f f L i s t ( x s + + ) f r o m D i f f L i s t : : D i f f L i s t a - > [ a ] f r o m D i f f L i s t ( D i f f L i s t f ) = f [ ] i n s t a n c e M o n o i d ( D i f f L i s t a ) w h e r e m e m p t y = D i f f L i s t ( \ x s - > [ ] + + x s ) ( D i f f L i s t f ) ` m a p p e n d ` ( D i f f L i s t g ) = D i f f L i s t ( \ x s - > f ( g x s ) ) ^¹”( \ x s - > f ( g x s ) ) wwo ¡ t o D i f f L i s t [ 1 , 2 , 3 ] ` m a p p e n d ` t o D i f f L i s t [ 4 , 5 , 6 ] = \ x s - > ( [ 1 , 2 , 3 ] + + ) ( ( [ 4 , 5 , 6 ] + + ) x s ) ̃©³f r o m D i f f L i s t ¡− ( [ 1 , 2 , 3 ] + + ) ( ( [ 4 , 5 , 6 ] + + ) [ ] ) [ 1 , 2 , 3 ] + + ( [ 4 , 5 , 6 ] + + [ ] ) ìg •¶ß—¡̈
  18. 9c»Žéè“›¤ 1\ i n s t a n c e

    M o n a d ( ( - > ) r ) w h e r e r e t u r n x = \ _ - > x h > > = f = \ w - > f ( h w ) w h »( m a ) fi—ß9c ¡ w ³t¡ gp³S|—¡ 9c¶” ¤ßÚ ¡„ f »( a - > m b ) ̃̃ »( h w ) ”Sægp³a •ï¾ è|– 9c( m b ) ³w |øq |—¡ ³›–Æ—¡ a d d S t u f f : : I n t - > I n t a d d S t u f f = d o a < - ( * 2 ) b < - ( + 1 0 ) r e t u r n ( a + b ) g h c i > a d d S t u f f 1 0 0 3 1 0
  19. h > > = f = \ w - >

    f ( h w ) w »…̇ > > = ”1\w ¿“ Êo—|¤ ̀l”1 ³7¡9c ü̊–Æ—|łq a d d O n e : : I n t - > I n t a d d O n e = d o a < - ( + 1 ) r e t u r n a —¢»do ³Ow|—¡ a d d O n e : : I n t - > I n t a d d O n e = ( + 1 ) > > = \ a - > r e t u r n a ìR³+‒¡− © •9c•¶ß—|¤̈ ( + 1 ) > > = ( \ a - > r e t u r n a ) \ w - > ( \ a - > r e t u r n a ) ( ( + 1 ) w ) w - - ` h > > = f = \ w - > f ( h w ) w ` \ w - > ( \ a - > ( \ _ - > a ) ) ( ( + 1 ) w ) w - - ` r e t u r n x ` = ` \ _ - > x ` \ w - > c o n s t ( ( + 1 ) w ) w - - ` \ a - > \ _ - > a ` = ` c o n s t ` \ w - > ( + 1 ) w - - ` c o n s t ` ³Qi ( + 1 ) - - r e m o v e e t a r e d u c t i o n
  20. 1\ n e w t y p e S t

    a t e s a = S t a t e { r u n S t a t e : : s - > ( a , s ) } i n s t a n c e M o n a d ( S t a t e s ) w h e r e r e t u r n x = S t a t e $ \ s - > ( x , s ) ( S t a t e h ) > > = f = S t a t e $ \ s - > l e t ( a , n e w S t a t e ) = h s - - ̀ôxS泂v” ̀ &|oßa &|o ̀n e w S t a t e ³‾> ( S t a t e g ) = f a - - t\©¤9c•a ³Qi õ” ̀ôxSæg ³>•‾©− i n g n e w S t a t e - - õ” ̀ôxSæg ³&|o ̀n e w S t a t e
  21. ÕÜá˳State Žéè - - i m p o r t

    C o n t r o l . M o n a d . S t a t e t y p e S t a c k = [ I n t ] p o p : : S t a t e S t a c k I n t p o p = s t a t e $ \ ( x : x s ) - > ( x , x s ) p u s h : : I n t - > S t a t e S t a c k ( ) p u s h a = s t a t e $ \ x s - > ( ( ) , a : x s ) s t a c k S t u f f : : S t a t e S t a c k ( ) s t a c k S t u f f = d o a < - p o p i f a = = 5 t h e n p u s h 5 e l s e d o p u s h 3 p u s h 8 g h c i > r u n S t a t e s t a c k S t u f f [ 9 , 0 , 2 , 1 , 0 ] ( ( ) , [ 8 , 3 , 0 , 2 , 1 , 0 ] )
  22. ƒc¢{P³State Žéè ̃©w t h r e e C o

    i n s : : S t d G e n - > ( B o o l , B o o l , B o o l ) t h r e e C o i n s g e n = l e t ( f i r s t C o i n , n e w G e n ) = r a n d o m g e n ( s e c o n d C o i n , n e w G e n ' ) = r a n d o m n e w G e n ( t h i r d C o i n , n e w G e n ' ' ) = r a n d o m n e w G e n ' i n ( f i r s t C o i n , s e c o n d C o i n , t h i r d C o i n ) ̃q̈¦—¡ r a n d o m S t : : ( R a n d o m G e n g , R a n d o m a ) = > S t a t e g a r a n d o m S t = s t a t e r a n d o m t h r e e C o i n s : : S t a t e S t d G e n ( B o o l , B o o l , B o o l ) t h r e e C o i n s = d o a < - r a n d o m S t b < - r a n d o m S t c < - r a n d o m S t r e t u r n ( a , b , c ) ] ̀³t|–m}− ̧¾”Ͼ ³çÕ|—¡ g h c i > r u n S t a t e t h r e e C o i n s ( m k S t d G e n 3 3 ) ( ( T r u e , F a l s e , T r u e ) , 6 8 0 0 2 9 1 8 7 2 1 0 3 4 1 0 2 6 3 )
  23. Error ³F• 1\ i n s t a n c

    e ( E r r o r e ) = > M o n a d ( E i t h e r e ) w h e r e r e t u r n x = R i g h t x R i g h t x > > = f = f x L e f t e r r > > = f = L e f t e r r f a i l m s g = L e f t ( s t r M s g m s g ) Nothing •5èwfio¤Maybe s© øo |łq g h c i > L e f t " b o o m " > > = \ x - > r e t u r n ( x + 1 ) L e f t " b o o m " g h c i > R i g h t 1 0 0 > > = \ x - > L e f t " n o w a y ! " L e f t " n o w a y ! " g h c i > R i g h t 3 > > = \ x - > r e t u r n ( x + 1 0 0 ) < i n t e r a c t i v e > : 1 : 0 : A m b i g u o u s t y p e v a r i a b l e ` a ' i n t h e c o n s t r a i n t s : ` E r r o r a ' a r i s i n g f r o m a u s e o f ` i t ' a t < i n t e r a c t i v e > : 1 : 0 - 3 3 ` S h o w a ' a r i s i n g f r o m a u s e o f ` p r i n t ' a t < i n t e r a c t i v e > : 1 : 0 - 3 3 P r o b a b l e f i x : a d d a t y p e s i g n a t u r e t h a t f i x e s t h e s e t y p e v a r i a b l e ( s ) g h c i > - - E i t h e r e a ”;ÓÌìÞ‾³ô¦−Èowm− g h c i > R i g h t 3 > > = \ x - > r e t u r n ( x + 1 0 0 ) : : E i t h e r S t r i n g I n t R i g h t 1 0 3
  24. liftM l i f t M : : ( M

    o n a d m ) = > ( a - > b ) - > m a - > m b l i f t M f m = d o x < - m r e t u r n ( f x ) fmap œ∼ ¡ g h c i > l i f t M ( * 3 ) ( J u s t 8 ) J u s t 2 4 g h c i > f m a p ( * 3 ) ( J u s t 8 ) J u s t 2 4 g h c i > r u n W r i t e r $ l i f t M n o t $ W r i t e r ( T r u e , " c h i c k p e a s " ) ( F a l s e , " c h i c k p e a s " ) g h c i > r u n W r i t e r $ f m a p n o t $ W r i t e r ( T r u e , " c h i c k p e a s " ) ( F a l s e , " c h i c k p e a s " ) g h c i > r u n S t a t e ( l i f t M ( + 1 0 0 ) p o p ) [ 1 , 2 , 3 , 4 ] ( 1 0 1 , [ 2 , 3 , 4 ] ) g h c i > r u n S t a t e ( f m a p ( + 1 0 0 ) p o p ) [ 1 , 2 , 3 , 4 ] ( 1 0 1 , [ 2 , 3 , 4 ] ) é¼O¶L Monad »Functor »¶o” liftM wi²\©–o−øq ¡
  25. ap a p : : ( M o n a

    d m ) = > m ( a - > b ) - > m a - > m b a p m f m = d o f < - m f x < - m r e t u r n ( f x ) < * > œ∼ ¡ g h c i > J u s t ( + 3 ) < * > J u s t 4 J u s t 7 g h c i > J u s t ( + 3 ) ` a p ` J u s t 4 J u s t 7 g h c i > [ ( + 1 ) , ( + 2 ) , ( + 3 ) ] < * > [ 1 0 , 1 1 ] [ 1 1 , 1 2 , 1 2 , 1 3 , 1 3 , 1 4 ] g h c i > [ ( + 1 ) , ( + 2 ) , ( + 3 ) ] ` a p ` [ 1 0 , 1 1 ] [ 1 1 , 1 2 , 1 2 , 1 3 , 1 3 , 1 4 ] é¼O¶L Monad »Applicative »¶o” liftM wi²\©–o−ø q ¡
  26. join ìÕç|¤Žéè³— Ł–z©—¡ j o i n : : (

    M o n a d m ) = > m ( m a ) - > m a j o i n m m = d o m < - m m m g h c i > j o i n ( J u s t ( J u s t 9 ) ) J u s t 9 g h c i > j o i n ( J u s t N o t h i n g ) N o t h i n g g h c i > j o i n N o t h i n g N o t h i n g g h c i > j o i n [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] [ 1 , 2 , 3 , 4 , 5 , 6 ] g h c i > r u n W r i t e r $ j o i n ( W r i t e r ( W r i t e r ( 1 , " a a a " ) , " b b b " ) ) ( 1 , " b b b a a a " ) À ́<O•»ìÕç|¤Maybe ³— Ł−”•øz³o—¡Á