ãã®è¨äºã¯ãMonad ã§ã¤ã¾ã¥ãã Haskeller ã®ããã® Monad åå ¥éã§ãã
Monadã¨ã¯ä½ã§ããï¼
Monad ã¨ã¯ãåãªãåã¯ã©ã¹ã®ä¸ã¤ã§ããé£ããã¨ãã風è©ãããã¾ããããã以ä¸ã§ããã以ä¸ã§ãããã¾ããã
ãã®åã¯ã©ã¹ã®ã¡ã½ããã¯ãreturn 㨠>>= ã§ãã
class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a
ã¤ã¾ãã以ä¸ãæºããåã®éåã Monad ã§ãã
- m a ã§è¡¨ç¾ã§ããããã«ä¸ã¤ã®åå¤æ°ãæ ¼ç´ããã³ã³ããå
- >>= 㨠return ãå®è£
return ã¯æ°ããã³ã³ãããä½ãã>>= ã¯äºã¤ã®ã³ã³ãããåæãã¾ãã
Monad ã®ã¤ã³ã¹ã¿ã³ã¹ã¯å¤±æç³»ã¨ç¶æ ç³»ã«å¤§å¥ã§ãã¾ãã以ä¸ã«ä»£è¡¨çãªã¤ã³ã¹ã¿ã³ã¹ã示ãã¾ãã
- 失æç³»: Maybeã[] (ãªã¹ã)
- ç¶æ ç³»: ParserãIO
Monad ã®ä¸å¹¸ã¯ãMaybe 以å¤ãåããã«ãããã¨ã§ãã
åãªãåã¯ã©ã¹ãªã®ã«ãã©ããã¦ãããé¨ãããã®ã§ããããï¼
ããã¯ãdo ã¨ããç¹æ®ãªæ¸å¼ã使ãããã¨ãå¯ä½ç¨ãèµ·ããã«ã¯ IO ã¨ãã Monad ãããã¦éããªããã¨ãçç±ã§ãããã do ã IO ã«ã¤ãã¦ã¯å¾è¿°ãã¾ãã
åãªãåã¯ã©ã¹ã¨è¨ããã¦ããã£ã±ãåããã¾ãã
Monad ãç解ããã«ã¯ãã¾ã Functor 㨠Applicative ã¨ããåã¯ã©ã¹ãç解ããã¨ããã§ããããFunctor ãç¹æ®åãã㨠Applicative ã«ãªããApplicative ãç¹æ®åãã㨠Monad ã«ãªãã¾ãã
ã§ã¯ãFunctor ã¨ã¯ä½ã§ããï¼
Functor ã¨ã¯ãfmap ã¨ããã¡ã½ãããæä¾ããåã¯ã©ã¹ã§ãã
class Functor f where fmap :: (a -> b) -> f a -> f b
ãªã¹ãã«ç¹åãã¦ãã fmap ã map ã§ããmap ãªãåããã¾ãããï¼
> map (+1) [1,2,3,4] [2,3,4,5]
ãªã¹ãã® fmap 㯠map ãªã®ã§ã以ä¸ã®ããã« fmap ã使ãã¾ãã
> fmap (+1) [1,2,3,4] [2,3,4,5]
MaybeãParserãIO ã Functor ã®ã¤ã³ã¹ã¿ã³ã¹ã§ãã
> fmap (+1) (Just 1) Just 2
ãã以éããã®è¨äºã§ã¯ fmap ã®å¥åã§ãã <$> ã使ããã¨ã«ãã¾ãã<$> ã¯ãControl.Applicative ã§å®ç¾©ããã¦ãã¾ãã
> :m Control.Applicative > (+1) <$> [1,2,3,4] [2,3,4,5] > (+1) <$> Just 1 Just 2
<$> ã®åã¯ã
(<$>) :: (a -> b) -> f a -> f b
ã§ããã-> ã¯å³çµåãªã®ã§ã以ä¸ã®ããã«æ¬å¼§ãè£ãã¾ãã
(<$>) :: (a -> b) -> (f a -> f b)
ããããã¨ã第ä¸å¼æ°ã®é¢æ°ãã³ã³ãã対å¿ã«å¤æ´ãã¦ãããã¨ãã¯ã£ãããã¾ãã
FunctorãApplicativeãMonad ã®ã³ã³ããã¯ããæèãã¨å¼ã°ãããã¨ãããã¾ããããªãããMonad ãã¯ã£ããä¼å¾ããæããã®ã¯ããã£ã¨ãMonad ã¯æèãªãã ãã¨ãããã¨ãç解ããã¨ãã§ãã
æèã¨ããè¨èã使ã£ã¦ <$> ã説æããã¨ã<$> ã¯ç¬¬ä¸å¼æ°ã®é¢æ°ãæèã«ããã¡ããããããã®é¢æ°ã§ãããã¨ãã°ã
> (+1) <$> Just 1
ã® (+1) <$> ã¯ã(+1) ã¨ããæèéä¾åã®é¢æ°ã Maybe ã¨ããæèã«æã¡ä¸ãã¦ããã®ã§ãããªããMaybe ã¯ã失æãããããããªããã¨ããæèã表ãã¾ãã®ã§ãã失æããªãé¢æ°ã失æãããããããªãé¢æ°ã«å¤æ´ãã¦ãããã¨èãã¦ãããã§ãããã
Functor ã«æ¬ ãã¦ããã®ã¯ãã³ã³ããå士ããã£ä»ããæ©è½ã§ããå°ããªè¨ç®ãçµã¿åããã¦ã大ããªè¨ç®ãä½ããªãã®ã§ãããããå®ç¾ããã®ã Applicative ã§ãã
Applicative ã¨ã¯ä½ã§ããï¼
Applicative ã¨ã¯ãpure 㨠<*> ã¨ããã¡ã½ãããæä¾ããåã¯ã©ã¹ã§ããApplicative ã®è¦ªã¯ã©ã¹ã¯ãFunctor ã§ãã
class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b
ãããã£ã±ã«è¨ãã°ãpure 㯠return ã®å¥åã§ããã¤ã¾ããæ°ããã³ã³ãããä½ãé¢æ°ã§ãã<*> ã¯ãã³ã³ããã®ä¸ã«ããé¢æ°ããã³ã³ããã®ä¸ã§é¢æ°é©ç¨ããããã®é¢æ°ã§ãã<*> ã®åãããf ã¨ããæèãåãé¤ãã¦ä¸ãããé¢æ°é©ç¨ãããªãã¡ $ ã¨åãåã«ãªããã¨ãåããã¾ããã
($) :: (a -> b) -> a -> b
Applicative ã® <*> ã¯ãFunctor ã® <$> ã¨å ±ã«ãApplicative ã¹ã¿ã¤ã«ãå½¢æãã¾ããApplicative ã¹ã¿ã¤ã«ã¨ã¯ã以ä¸ã®ãããªæ¸å¼ã§ãã
func <$> arg1 <*> arg2 <*> ....
<$> 㨠<*> ãé ã®ä¸ã§æ¶ãã°ãfunc arg1 arg2 ... ã¨ãªããããã¯åãªãé¢æ°é©ç¨ã§ããApplicative ã¹ã¿ã¤ã«ã¨ã¯ãæèã®ä¸ã§ã®é¢æ°é©ç¨ã簡便ã«æ¸ãæ¸å¼ã§ãããApplicative ã¨ã¯ãããå®ç¾ããããã®åã¯ã©ã¹ã§ãã
do ã®ã話ã¯ã¾ã ãã¦ãã¾ããããåããã¨ã do ã§æ¸ãã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
do a1 <- arg1 a2 <- arg2 ... return $ func a1 a2 ...
æ¯ã¹ã¦ã¿ãã¨ãããã« Applicative ã¹ã¿ã¤ã«ãç°¡æ½ã§ããããåããã§ãããã
Applicative ã¹ã¿ã¤ã«ã¯ããããã¼ãµã¼ã¨å ±ã«ä½¿ããã¾ãããã¨ãã°ãåè¡ã«ãã¡ã¤ã«åããªãã»ããããã¤ãæ°ãæ¸ããããã¡ã¤ã«ã解æãããã¨ãèãã¾ãããã以ä¸ã®ãããªã³ã¼ãã«ãªãã¾ãã(<* ã¯ãå³ã®å¼æ°çµæãæ¨ã¦ã¦ãå·¦ã®å¼æ°ã®çµæãè¿ãé¢æ°ã§ãã)
import Text.Parsec import Text.Parsec.String import Control.Applicative hiding (many,(<|>)) data FileInfo = FileInfo FilePath Integer Integer fileInfos :: Parser [FileInfo] fileInfos = many fileInfo fileInfo :: Parser FileInfo fileInfo = FileInfo <$> fileName <*> offset <*> size fileName :: Parser FilePath fileName = many1 (letter <|> digit) <* char ' ' offset :: Parser Integer offset = (read <$> many1 digit) <* char ' ' size :: Parser Integer size = (read <$> many1 digit) <* char '\n'
fileInfo ãè¦ãã°ããã¼ã¿æ§æå FileInfo ã Parser ã®æèã®ä¸ã§ã次ã ã«é¢æ°é©ç¨ããã¦ãããã¨ãåããã§ããããããªãã¡ãå°ã㪠Parser ãçµã¿åããã¦ã大ã㪠Parser ãä½ã£ã¦ãã¾ãã
ãã®ããã«ãApplicative ã¯é次çã«ã³ã³ãããé£çµã§ãã¾ãããã ããããããã®ã³ã³ããã¯ç¬ç«ããè¨ç®ãå®è¡ãã¾ãã
Applicative ã«ã§ããªããã¨ã¯ãåã®ã³ã³ããã®è¨ç®çµæãç¨ãã¦ãå¾ã®ã³ã³ãããæ¯ãèããå¤ãããã¨ã§ããå¥ã®è¨ãæ¹ãããã°ãåã®ã³ã³ããã®è¨ç®çµæããå¾ã®ã³ã³ããã«æ¸¡ãæ¹æ³ãããã¾ããããããå®ç¾ããã®ã Monad ãªã®ã§ãã
ã§ã¯ãåã³ Monad ã¨ã¯ä½ã§ããï¼
Haskell ã® Monad ã«ã¯æ®å¿µãªãã Applicative ã®å¶ç´ãããã¾ãããããã¹ã¦ã® Monad 㯠Applicative ã㤠Functor ã«ãããã¨ãã§ãã¾ããããã¦ãMonad ã®ã¤ã³ã¹ã¿ã³ã¹ã®å¤ãã¯ãFunctor ã Applicative ã®ã¤ã³ã¹ã¿ã³ã¹ã¨ãã¦å®ç¾©ããã¦ãã¾ãã
ããã§ãããã§ã¯ä»¥ä¸ã®ãããªçæ³åãããä¸çãèãã¾ãããã
class Functor m where (<$>) :: (a -> b) -> m a -> m b class Functor m => Applicative m where return :: a -> m a (<*>) :: m (a -> b) -> m a -> m b class Applicative m => Monad m where (>>=) :: m a -> (a -> m b) -> m b
pure 㨠return ã¯åä¸è¦ã§ããã®ã§ãããã§ã¯ãªãã¿æ·±ã return ãæ¡ç¨ãã¾ãã
ãã®å®ç¾©ãè¦ã㨠Monad ã®æ ¸å¿ãã>>= ã ã¨ã¯ã£ããåããã¾ããã第äºå¼æ°ã®é¢æ°ãã第ä¸å¼æ°ã®è¨ç®çµæãåããã¨ã«æ³¨ç®ãã¦ä¸ããã
ã§ã¯ãApplicative ã«ã§ããªãã¦ãMonad ã«ã§ããã³ã¼ãã®ä¸ä¾ã示ãã¾ããããä¾ã¨ãã¦ã¯ãTLV (TypeãLengthãValue) ãã¼ãµã¼ãæãããã¾ããValue ã解æããã¨ãã¯ãç´åã® Length ã®çµæã使ããªãã¦ã¯ãªãã¾ããã
å ·ä½çãªå®è£ ã¯çç¥ãã¾ããã以ä¸ã®ãããªã³ã¼ãã«ãªãã¾ãã
data TLV = TLV Type Int Value tlv :: Parser TLV tlv = typ >>= \t -> len >>= \l -> val l >>= \v -> return (TLV t l v) typ :: Parser Type len :: Parser Int val :: Int -> Parser Value
ç解ãã¹ããã¨ã¯
- val 㯠len ã®è¨ç®çµæ l ãåãåã£ã¦ãã
- TLV t l v ã¯æèéä¾åã®å¤ãªã®ã§ãreturn ã§æèã«å ¥ãã¦ãã
ãã¨ã§ãã
Monad ãã¾ã¨ãã¾ãã
- ã³ã³ããã§ãã
- ã³ã³ãããé£çµãã¦å¤§ããªã³ã³ãããä½æã§ãã
- åã®ã³ã³ããã®çµæã«ä¾åãã¦ãå¾ã®ã³ã³ããã®æ¯ãèããå¤ãããã
IO ãã³ã³ãããªã®ã§ããï¼
ããã§ãã
IO ã¯å½ä»¤æ¸ã§ãããå½ä»¤ã§ã¯ããã¾ããã>>= ã使ãã°ãå°ããªå½ä»¤æ¸ãåæãã¦ã大ããªå½ä»¤æ¸ãä½æã§ãã¾ããæçµçãªå½ä»¤æ¸ã«ã¯ main ã¨ããååãä»ãã¾ãããããå®è¡ããã®ã¯ã©ã³ã¿ã¤ã ã§ãã
ãã¨ãã°ãgetChar ã¨ããé¢æ°ãèãã¾ãããã
getChar :: IO Char
å½ä»¤åè¨èªãããã£ã¦ãã人ã¯ããã®é¢æ°ãå¼ã¶ã¨ãæåãè¿ã£ã¦ããã¨æãã§ããããããã¦ãå¼ã³åºããç¶æ³ã«å¿ãã¦è¿ã£ã¦ããæåã¯éãã®ã§ãHaskell ã«ã¯å¯ä½ç¨ãããã¨æãã§ãããã
ããããå®é getChar ãè¿ãã®ã¯ãä¸æåèªã¿è¾¼ããã¨ããå½ä»¤æ¸ã§ããã©ããªç¶æ³ã«ããã¦ããåãå½ä»¤æ¸ãè¿ãã¾ããã§ããããå¯ä½ç¨ã¯ãªãã®ã§ããå¯ä½ç¨ãçºçãããã®ã¯ãå½ä»¤æ¸ãå®è¡ããã©ã³ã¿ã¤ã ã§ãã
ãã ãIO ãå½ä»¤æ¸ã§ãããã¨ãæ´»ãããããã«ãªãã«ã¯ãç¸å½ä¿®è¡ãç©ãå¿ è¦ãããã¾ããããã§æåã®ãã¡ã¯ããIO ã¯å¯ä½ç¨ãèµ·ããé¢æ°ã表ãã¦ãããã¨ç解ãã¦ãåé¡ã¯ããã¾ãããIO ãä»ãã¦ããªããã°ç´ç²ã§ãIO ãä»ãã¦ããã°å¯ä½ç¨ããã£ã¦ãããããæ確ã«åºå¥ã§ãããã Haskell ã¯ç´ æ´ããã¨ç解ãã¦ãçµæ§ã§ãã
ãã ãHaskell ã使ã£ã¦ããã©ããã®æç¹ã§ãæ£ããç解ããã¦ä¸ããã
Monad ã ã¨å®è¡é åºãæå®ã§ããã®ã§ããï¼
ãããããã㯠Monad ã®æ©è½ã§ã¯ããã¾ããã
Haskell ã§ã¯ãããã°ã©ãã¼ãè¨è¿°ããé åºã§ããã°ã©ã ãå®è¡ãããã¨ã¯éãã¾ããããã®å ·ä½çãªä¾ã¯遅延評価だけだと出力の順番が定まらない例ãè¦ã¦ä¸ããã
ãã ãããã°ã©ãã¼ãè¨è¿°ããé åºã§ããã°ã©ã ãå®è¡ãããå ´åãããã¾ããããã¯ã¨ã¦ãç°¡åãªçå±ã§ãåã®çµæã«å¾ãä¾åããå ´åãåãå ã«å®è¡ããå¿ è¦ãããããã§ãããã¨ãã°ã以ä¸ã®ã³ã¼ãã¯è¨è¿°é ã«å®è¡ããã¾ãã
isLetter x || isAscii x
ããã¯ãããã§ãããï¼ || ã¯ã以ä¸ã®ããã«å®ç¾©ããã¦ãã¾ãã
(||) :: Bool -> Bool -> Bool True || _ = True False || y = y
ãããã¬ãã«ã§ã®ãã¿ã¼ã³ãããã¯ãcase ã®æ§æç³è¡£ã§ãããã以ä¸ã®ããã«æ¸ãæããã¾ãã
(||) :: Bool -> Bool -> Bool x || y = case x of True -> True False -> y
ã¤ã¾ããy ãè©ä¾¡ãããå¦ãã¯ãx ã«ä¾åãã¦ãã¾ããããã«ãx 㯠Bool ã¨ããæ£æ ¼ãã¼ã¿ã§ããã§ããããè©ä¾¡ã®é çªãè¨è¿°éãã«ãªãã®ã§ãã
IO ãè¨è¿°é ã«å®è¡ãããã®ããåãçå±ã§ãããã¨ãã°ãIO ã®å®ç¾æ¹æ³ã¨ãã¦ãWorldã¨ããæ£æ ¼ãã¼ã¿ãããªãã¡å®è¡ç°å¢ã渡ãã¦ããæ¹æ³ãããã¾ãã
data IO a = IO (World -> (a,World)) instance Monad IO where (IO m) >>= f = IO $ \world -> case m world of (a, world') -> let IO m' = f a in m' world'
失æ系㮠Monad ã®å®ä½ãå¤ãã®ãã®ã§ããã®ã«å¯¾ããç¶æ 系㮠Monad ã®å®ä½ã¯é¢æ°ã§ãã®ã§ãæ £ããªãã¨å°ãé£ããããããã¾ãããã§ããè¨è¿°é ã«å®è¡ãããçå±ã¯ || ã¨å¤ãããªãã®ã§ãã
Monad ãå®è¡é ã決ããã®ã§ã¯ãªããMonad ã®ããã¤ã³ã¹ã¿ã³ã¹ã® >>= ããå®è¡é ãå®ãããã«å®ç¾©ããã¦ããã ãã§ãã
do ã¨ã¯ä½ã§ããï¼
>>= ã®æ§æç³è¡£ã§ãã
å ã»ã©ã® tlv ãæãåºãã¾ããã
tlv = typ >>= \t -> len >>= \l -> val l >>= \v -> return (TLV t l v)
ã¡ãã£ã¨æ°æã¡ãæªãã®ã§ãããæãè¿ãå ´æãå¤ãã¦ã¿ã¾ãããã
tlv = typ >>= \t -> len >>= \l -> val l >>= \v -> return (TLV t l v)
ããã§ã<- ã¨ããè¨å·ãå°å ¥ããã°ãåã ã®è¡ãããããä»£å ¥ãã¦ãããããªã³ã¼ãã«ãªããã¨ã«æ°ä»ãã人ããã¾ãã
tlv = do t <- typ l <- len v <- val l return (TLV t l v)
ããã do ã®æ£ä½ã§ããåãªã >>= ã®æ§æç³è¡£ã«éãã¾ããã
Monad åãæãã¦ä¸ããã
以ä¸ã Monad åã§ãã
1: return x >>= f == f x 2: m >>= return == m 3: (m >>= f) >>= g == m >>= (\x -> f x >>= g)
Monad å1 㯠return ãå·¦åä½å ã ã¨ããæå³ã§ãããããã°ã©ãã¼ã«ã¯ã©ãã§ããã話ã§ããããã¯ãããããæ¸ãæããä¿è¨¼ããã¦ããã®ã§ãå·¦ã®å½¢ã¯åé·ã ãããå³ã®ããã«æ¸ãã¾ãããã¨ãããã¨ã§ããdo ã§è¡¨ãã¨ãããã«åããæãããããã¾ããã
do y <- return x == do f x f y
å·¦ã¯åé·ã§ãå³ã¯ç°¡æ½ã§ãããå®ã¯ do f x ãåé·ã§ãf x ã§ååã§ããåé·ãªã³ã¼ããªãã¦æ¸ããªãã¨æãããããã¾ããããã³ã¼ããæ¸ãç´ãã¨ç¥ããªããã¡ã«ãã®å½¢ã«ãªã£ã¦ãããã¨ãããã¾ãã
Monad å2 㯠return ãå³åä½å ã ã¨ããæå³ã§ãããããã°ã©ãã¼ã«ã¯ã©ãã§ããã話ã§ããããããMonad å1ã¨åæ§ã«ãæ¸ãæããä¿è¨¼ããã¦ããã¨ããæå³ã§ãããã¡ãã do ã§æ¸ãã¦ã¿ã¾ãããã
do x <- m == do m return x
å·¦ã¯åé·ã§ãå³ã¯ç°¡æ½ã§ãããå®ã¯ do m ãåé·ã§ãåã« m ã§ååã§ãã
Monad å3 ã¯ãã©ãããé çªã§ã³ã³ãããåæãã¦ãæå³ã¯åãã ã¨ãããã¨ã表ãã¦ãã¾ããã¤ã¾ããm 㨠f ãåæãã¦ãã g ã¨åæãã¦ããm ããf 㨠g ãåæãããã®ã«ãåæãã¦ããåãã ã¨è¨ã£ã¦ãã¾ãã
å·¦å³ãé対称ãªã®ã§ãå¤æ°ãè£ã£ã¦å¯¾ç§°æ§ãæãããã¨ããããªãã¾ãã
(m >>= \x -> f x) >>= \y -> g y == m >>= (\x -> f x >>= \y -> g y)
do ã§æ¸ãã¨ãããªãã¾ãã
do y <- do x <- m == do x <- m f x do y <- f x g y g y
å³ã¯çµå±ãããããæå³ã§ãã
do x <- m y <- f x g y
ã¤ã¾ããæ¸ããªãæå¾ã®ããã«ãã£ããæ¸ãã¾ãããã¨ãããã¨ã§ãã
ããåãããªã人ã¯ãdo ãå½ä»¤åè¨èªã®ãããã¯ãã¤ã¾ã {...} ã«ç½®ãæãã¦ä¸ãããã©ãã { } ã§ããããã¨ãæå³ã¯å¤ãããªãã¯ãã§ããåã«ããã ãã®ãã¨ãªãã§ãã
ãªãã>>= ã¯å·¦çµåã§ãã®ã§ãåã«
m >>= f >>= g
ã¨æ¸ãã¨ã
(m >>= f) >>= g
ã¨ããæå³ã«ãªãã¾ããããããç¡åé¢æ°ã以ä¸ã®ããã«æ··ããã¨ã
m >>= \x -> f x >>= \y -> g y
以ä¸ã®ããã«è§£éããã¾ãã
m >>= (\x -> f x >>= \y -> g y)
ãã®ãããã§ãå¾ãã®æ¹ã§åã®å¤æ°ãåç §ã§ãã¾ãããã¨ãã°ããããªæãã§ãã
m >>= \x -> f x >>= \y -> g (x,y)
å®éã« Monad ãä½ã£ã¦ã¿ã¦ä¸ããã
Maybe ã®å®è£ ä¾ã¯è¦ããã¨ãããã¨æãã®ã§ãããã§ã¯ Identity ã¨ãã Maybe ãããç°¡å㪠Monad ãå®ç¾©ãã¦ã¿ã¾ãã
data Identity a = Identity a deriving Show instance Monad Identity where return x = Identity x (Identity x) >>= f = f x
使ã£ã¦ã¿ã¾ãããã
> Identity 1 >>= \x -> return (x + 1) Identity 2
ä½ã®æå³ããªãããã«æããããããã¾ãããããã§éè¦ãªã®ã¯ãIdentity ã¯æèã表ãã¦ãããã¨ã§ãã
ç¹°ãè¿ãã¾ãããMonad ã¨ã¯æèã§ããåãæèå士ã®ã¿ãåæã§ãã¾ããParser 㯠Parser ã¨ã®ã¿ãIO 㯠IO ã¨ã®ã¿åæã§ãã¾ããããã¦ãã®çµæã¯ããããã Parser 㨠IO ã§ããæèã¯ä¿æãããã®ã§ãã
Monad ã¤ã³ã¹ã¿ã³ã¹ã®æèã®æå³ãåæãã¦ã¿ã¾ãã
- [] (ãªã¹ã)ã¯ãé決å®æ§ã®æèã表ã
- Maybeã¯ã失æãããããããªãã¨ããè¨ç®ã®æèã表ã
- Parser ã¯ããã¼ãµã¼ã¨ããæèã表ã
- IO ã¯ãå¯ä½ç¨ã表ç¾ããã¨ããæèã表ã
Monad ã®æèããéããã«ã¯ãæèãã¡ããã¨å¦çããå¿ è¦ãããã¾ãããã¨ãã°ãMaybe ããéããã«ã¯ã失æã表ã Nothing ã¨ããå ´åãå¦çããªãã¨ããã¾ãããããã§ãå¦çå¿ããé²ããã®ã§ãã
ãªããIO ã¨ããæèããéããæ¹æ³ã¯ãé»éè¡ãé¤ãã¦ã¯ãåå¨ãã¾ããã
id ãé¢æ°åæã®åæå¤ã«ä½¿ãããããã«ãIdentity 㯠Monad ãã©ã³ã¹ãã¡ã¼ã®åºç¤ Monad ã¨ãã¦ä½¿ããã¾ãã
MonadPlus ã¨ã¯ä½ã§ããï¼
Monad ã® return 㨠>>= ã¯ããããã 1 㨠* ã«ç¸å½ãã¾ããTrue 㨠&& ã«ç¸å½ããã¨æã£ã¦ãçµæ§ã§ãã
MonadPlus ã® mzero 㨠mplus ã¯ããããã 0 㨠+ ã«ç¸å½ãã¾ããFalse 㨠|| ã«ç¸å½ããã¨æã£ã¦ãæ§ãã¾ããã
ã¤ã¾ããMonad ã¯é£æ¥ãå®ç¾ããMonadPlus ã¯é¸æãå®ç¾ããã®ã§ãã
>>=ã¨ããå ±éã®ã¡ã½ãããæã¤æå³ã¯ä½ã§ããï¼
å ±éã®ã¡ã½ããã§å®è£ ãã¦ããã°ãMonad ã® m ã®é¨åãããªãã¡æèã®æå®ãå¤ããã ãã§ãã³ã¼ãã®åä½ãå¤æ´ã§ãã¾ãããã¨ãã°ãåã失æ系㮠Maybe ã¨ãªã¹ããèãã¾ãããããããã®æèã¯ä»¥ä¸ã®ããã«èãããã¨ãã§ãã¾ãã
- Maybe - Nothing ã失æã§ãJust a ãæåã表ããããªãã¡ãçãã 0 åã 1 åã®æèã表ãã
- ãªã¹ã - [] ã失æã§ããã以å¤ãæåã表ããããªãã¡ãçãã 0 å以ä¸ã®æèã表ãã
ã§ã¯ãäºåæ¨ã®æ¢ç´¢åé¡ãèãã¾ããããæ¨ã¯ä»¥ä¸ã®ãããªåãæã¡ãæ¢ç´¢æ¨ã§ã¯ãªãã®ã§è¦ç´ ã¯ä»»æã®é çªã§æ ¼ç´ããã¦ããã¨ãã¾ãã
data Tree = Empty | Tree Int Tree Tree
ãã®æ¨ãæ¢ç´¢ãããã¹ã¦ã®çããè¿ãã³ã¼ãã¯ãå¹çãç¡è¦ããã¨ä»¥ä¸ã®ããã«æ¸ãã¾ãã
search :: (Int -> Bool) -> Tree -> [Int] search _ Empty = [] search p (Tree c l r) | p c = search p l ++ [c] ++ search p r | otherwise = search p l ++ search p r
使ã£ã¦ã¿ã¾ãããã
> search odd (Tree 3 (Tree 2 Empty Empty) (Tree 1 Empty Empty)) [3,1]
é 延è©ä¾¡ã§ãã®ã§ãå é ããé ã«çµæãåãåºãã¨ããã¯ãã©ãã¯ãã¦ããã®ã¨åãå¹æãæã¡ã¾ããé決å®æ§ã®ä¸è¬çãªå®è£ æ¹æ³ã¯ããã¯ãã©ãã¯ã§ããããé 延è©ä¾¡ã®ä¸ã§ã¯ãªã¹ãã¯é決å®æ§ã®æèã表ãã¨è¨ãããã®ã§ãã
ããã§ãçµæãä¸ã¤ã ãåãåºãããã«å¤æ´ãããã¨æã£ãã¨ãã¾ããããæ½è±¡åããã¦ããªããã°ãåãã¨ã«ã³ã¼ããæ¸ããªãã¨ããã¾ããã
å®ã¯ããªã¹ãã Maybe ã MonadPlus ã®ã¤ã³ã¹ã¿ã³ã¹ã§ãããã以ä¸ã®ããã«æ½è±¡åãã¦æ¸ãç´ããã¨ãã§ãã¾ãã
msearch :: MonadPlus m => (Int -> Bool) -> Tree -> m Int msearch _ Empty = mzero msearch p (Tree c l r) | p c = msearch p l `mplus` return c `mplus` msearch p r | otherwise = msearch p l `mplus` msearch p r
ããã ã¨ããªã¹ãã¨ãã¦ä½¿ããã¨ãã§ãã¾ããã
> msearch odd (Tree 3 (Tree 2 Empty Empty) (Tree 1 Empty Empty)) :: [Int] [3,1]
Maybe ã¨ãã¦ä½¿ããã¨ãã§ãã¾ãã
> msearch odd (Tree 3 (Tree 2 Empty Empty) (Tree 1 Empty Empty)) :: Maybe Int Just 3
å¤ãã® Parser ã¯ãMonad ã¨ãã¦å®è£ ããã¦ãã¾ãããã使ãåæã¯ä¸ç·ã§ãããã¨ãã°ãParsec ãç¿å¾ãã¦ãããªããattoparsec ã使ãã®ã¯å®¹æã§ãã
Monad ã«æ £ããã°ãMonad ãèªç¶ã¨ãªããMonad ã«ã§ããã®ã«ãã¦ãªããã¼ã¿æ§é ã¯ãéã«ä¸èªç¶ã«æããããã«ãªãã¾ãã