ãã®ã·ãªãŒãºã®ä»¥åã®èšäºã§ã¯ãã¢ããã®æŠå¿µçåºç€ãç 究ããŸããããè°è«ã¯ããŸãã«ãæœè±¡çã§ããã ã¢ãããšã¯äœãããªãã¢ãããå¿ èŠãªã®ããç解ã§ããã®ã§ãç¹å®ã®ã¢ããã詳现ã«èª¿ã¹ãæãæ¥ãŸããã ããã¯ãåã«èŠãå€ãã®ç°ãªãèšç®æŠå¿µã«å¯ŸããŠã Monadåã¯ã©ã¹ã®æ£ããå身ãå®çŸ©ããããšãæå³ããŸãã ç¥èã䜿çšããŠãåå Žåã«ã¢ããã¢ããªã±ãŒã·ã§ã³ïŒæŒç®å>> = ïŒãä»ããŠã¢ããæ§æãååŸããã¢ããã®æ³åã䜿çšããŠreturnã®å®çŸ©ãå°åºããŸãã
ãã¶ãã¢ãã
éåžžãMonadã¯äœ¿ãããããå®è£ ããç解ãããããããHaskellã®ããã¥ã¢ã«ã§æåã«çŽ¹ä»ãããã®ãæ®éã§ãã æåã«ã MaybeããŒã¿åã®å®çŸ©ãèŠãŠãã ããã
ããŒã¿ å€å a = Nothing | ãã
Maybeã¯ïŒç¹å®ã®ïŒããŒã¿åãååŸããããã«ç¹å®ã®åaãé 眮ãããåã³ã³ã¹ãã©ã¯ã¿ãŒã§ãããšè¿°ã¹ãŠããŸãã 圌ãã¯ãŸãã å€å ãå€æ æ§ãããŒã¿åã§ãããæå³ã¯åãã ãšèšã£ãŠããŸãã ãããã£ãŠã aãIntã®å Žåã次ã®ããã«ãªããŸãã
ããŒã¿ å€å Int = Nothing | ã¡ããã©int
äžèšã®æœè±¡çãªå®çŸ©ã¯ãã¹ãŠã®ã¿ã€ãã«é©çšããããããä»ã ããããçŽæ¥èšè¿°ããå¿ èŠã¯ãããŸããã
ã¿ã€ãaã®å€ã¯ãååšããå Žåãšååšããªãå ŽåããããŸãã å€ãNothingã®å Žåããããã§ãªãå Žåãã§ããã xã®ããå€ã«å¯ŸããŠJust xã§ããå Žåãããã¯xã® ãã¡ããã©ãå€ã§ãã ããã¯ã0åã®èŠçŽ ãŸãã¯1åã®èŠçŽ ãããã³ã³ãããšèããããšãã§ããŸãã ïŒèŠããŠãããŠãã ããïŒç§ã¯ãã€ãŠã¢ããå€ã誀ã£ãŠã³ã³ãããšããŠæ瀺ãããããšããããšèšã£ãŠããŸããããããäºå®ã§ããïŒ
å€ååããªã¢ãŒãã£ãã¯åã¯ãåºåå€ãšããŠäœããçæããããã¯ã©ãã·ã¥ããŠå€ãè¿ãããšãã§ããªããæ¡åŒµé¢æ°ãã®ã¢ãã«ãšããŠäœ¿çšã§ãããšããç¹ã§äŸ¿å©ã§ãã ïŒã€ãŸãããã®ãããªé¢æ°ã¯å€±æããå¯èœæ§ããããŸããïŒããã¯æ¬¡ã®ããã«æžãããŠããŸãã
f :: a- > ãã¶ã b
é¢æ°fã¯ãã¿ã€ãaã®å€ãåãã Nothing ïŒå€±æã®å åïŒãŸãã¯å€Just xãè¿ããŸãïŒ xã¯ã¿ã€ãbã æã¡ãŸãïŒã fã®ãããªé¢æ°ã¯Maybeã¢ããã§åäœãããã®ãããª2ã€ã®é¢æ°ã®æ§æã¯æ¬¡ã®ãšããã§ãã
f :: a- > ãã¶ã b -fãã©ããã§å®çŸ©ãããŠãããšä»®å®ãã
g :: b- > ãã¶ã c -gãã©ããã§å®çŸ©ãããŠãããšä»®å®ãã
h :: a- > ãã¶ã c -fãšgã®åé åæ
h = f > => g- èŠããŠãããŠãã ããïŒ> =>ã¯åé åæã®æŒç®åã§ã
ãã¹ãŠã®ã¢ããã¯åã³ã³ã¹ãã©ã¯ã¿ã§ãªããã°ãªããªãããšèšããŸããã å€ååã³ã³ã¹ãã©ã¯ã¿ãªã®ã§ãããã§ã¯ãã¹ãŠãããŸããããŸãã ãããã ãã¶ãã¢ããã«ãªãããã«ã¯ã Monadåã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãäœæããå¿ èŠããããŸããã€ãŸãã次ã®å®çŸ©ãå ¥åããå¿ èŠããããŸãã
ã€ã³ã¹ã¿ã³ã¹ Monad ãã¶ã ã©ã
ïŒ >> = ïŒ = {-å®çŸ©>> =ãã¶ã-}
return = {-ãã¶ããªã¿ãŒã³ã®å®çŸ©-}
Maybeã«ã©ã®ããã«èšå®ïŒ>> =ïŒããŠæ»ãããšãã§ããŸããïŒ
ãŸãã >> =ã®å®çŸ©ãã¬ãŒã ã¯ãŒã¯ãèšè¿°ãã å€åaåã®å·Šãªãã©ã³ãã®2ã€ã®å¯èœãªã±ãŒã¹ãã«ããŒããŸãã
ãªã>> = f = {-è¿œå ããå¿ èŠããããŸã-}
x >> = f = {-è¿œå ããå¿ èŠããããŸã-}
xã¯ã¿ã€ãaã§ãã å®çŸ©ã®å·ŠåŽã¯å¥ã®æ¹æ³ã§æžãããšãã§ããŸãïŒ
ïŒ >> = ïŒäœãf = {-è¿œå ããå¿ èŠã¯ãããŸãã-}
ïŒ >> = ïŒ ïŒ Just x ïŒ f = {-è¿œå ãå¿ èŠ-}
ããããæŒç®åïŒ>> =ïŒãé¢æ°ãšããŠã§ã¯ãªãæŒç®åãšããŠæå®ãããŠãããšãHaskellã¯ãããèš±å¯ããŸãã
ãã®å®çŸ©ãå®äºããããã«ã Maybeã¢ããã®ã¢ããæ§æããäœã欲ãããèããŠã¿ãŸãããã é¢æ°fãšgã䜿çšããŠäŸããšããããããåé çµåããé¢æ°hãååŸããŸãã
f :: a- > ãã¶ã b
g :: b- > ãã¶ã c
h :: a- > ãã¶ã c
h = f > => g
åŒæ°ãé¢æ°fã«æž¡ãã Nothingãè¿ãïŒã€ãŸãã倱æããïŒå Žåãé¢æ°hã¯äœãè¿ãå¿ èŠããããŸããïŒ
f x =ãªã
h x = ïŒ f > => g ïŒ x = ???
fxãNothingãè¿ãå ŽåãåŒïŒé¢æ°f ïŒã®äžéšãçµæãè¿ãããšãã§ããªãã£ãå ŽåãåŒå šäœïŒé¢æ°h ïŒãçµæãè¿ããªãããã hãNothingãè¿ãå¿ èŠãããããšã¯æããã§ãã hãå€ãè¿ããšãã®å¯äžã®ãªãã·ã§ã³ã¯ã fãçµæãè¿ããšãïŒ yãåŒã³åºããŸãããïŒãé¢æ°gã«æž¡ããã gyãæ£ããçµæã«ãªããŸãã fãŸãã¯gã倱æãããšã hã倱æããŸããã€ãŸãã hxã®èšç®ã¯Nothingã«ãªããŸãã
ããã念é ã«çœ®ããŠã hã®å®çŸ©ãã次ã®ããã«ãªããŸãã
h = f > => g
h = \ x -> ïŒ f x >> = g ïŒ -åçïŒå®çŸ©> =>ããïŒ
h x = f x >> = g- åç
-f x == Nothingãšä»®å®ãã
h x = ïŒãªã>> = g ïŒ
=ãªã
-ãã®ããã«ïŒ
ãªã>> = g =ãªã
ããã§ãæŒç®åïŒ>> =ïŒãNothingåŒæ°ã«ã©ã®ããã«åå¿ããããããããŸãã-åã«åãNothingãè¿ãã ãã§ãïŒ
ãªã>> = f =ãªã
x >> = f = {-è¿œå ããå¿ èŠããããŸã-}
ããã§gãfã«çœ®ãæããããšã«æ³šæããŠãã ãããããã¯æ£ããããšã§ããé¢æ°ã®ååã¯éèŠã§ã¯ãªãããã§ãã å®éã«ã¯ãå¯èœãªå Žåã¯éåžžé¢æ°åãåãé€ãã次ã®ããã«ç¹å¥ãªæŒç®å_ ïŒã¢ã³ããŒã¹ã³ã¢ïŒã«çœ®ãæããŸãã
ãªã>> = _ =ãªã
ããã¯ãå®çŸ©ã§é¢æ°fã䜿çšããããã2çªç®ã®åŒã§ã¯å®è¡ã§ããŸããã
ããã§ã¯ãä»ã®æ¹æ³ã«è¡ããŸãããã fx ã倱æããªãå Žåãçµæã¯äžéšã®yã«å¯ŸããŠJust yã®å€ã«ãªããŸãã Just yããyã®å€ããã¢ã³ããã¯ãããå¿ èŠãããããããé¢æ°gã«æž¡ããŸããgyã¯é¢æ°hå šäœã®çµæã§ãã
h = f > => g
h = \ x -> ïŒ f x >> = g ïŒ -åçïŒå®çŸ©> =>ããïŒ
h x = f x >> = g- åç
-f x == Just yãšä»®å®ãã
h x = ïŒã¡ããã©y >> = g ïŒ
= g y
ããã«ãããå®çŸ©ã®2çªç®ã®éšåãåŸãããŸãã
ãªã>> = f =ãªã
ã¡ããã©x >> = f = f x
yãxã« ã gãfã«çœ®ãæããããšã«æ³šæããŠãã ããã ç¹°ãè¿ããŸãããå€æ°ãšé¢æ°ã®ååã¯äžè²«ããŠããéãéèŠã§ã¯ãããŸããã
ããã§ã Maybeã¢ããã®>> =æŒç®åã®å®çŸ©ãçµäºããŸãã 次ã«ããã®ã¢ããã®ãªã¿ãŒã³ãååŸããå¿ èŠããããŸãã
return x = ???
xã®ä»»æã®å€ã«å¯Ÿã㊠ã ã©ã®ãããªãªãã·ã§ã³ããããŸããïŒ ç§ãã¡ã¯ãããèšãããšãã§ããŸã
return x =ãªã
ä»»æã®xã«å¯Ÿã㊠ã ãã ãããããè¡ããšã¢ããæ³ã«éåããŸãã
return x >> = f == f x
äœããªã>> = f == f x
äœããªã== f x- ééã£ãŠããŸãïŒ
å°ãªããšãäžéšã® fxã Nothing ã§ãªããšä»®å®ãããšïŒããšãã°ãã¢ããé¢æ°fx = Just xãèæ ®ããŠïŒããšã©ãŒãçºçããŸãã å¥ã®ãªãã·ã§ã³ããããŸãïŒ
return x =ã¡ããã©x
ãããŠãããã¯ã¢ããã®æ³åãæºãããŸãïŒ
return x >> = f
= ïŒ Just x ïŒ >> = f- å®çŸ©ã«ãããMaybeã¢ãããè¿ã
= f x- å®çŸ©ã«ãã>> =ã¢ããã®å Žå
-æåã®ã¢ããæ³ã®å®è£
ã¡ããã©x >> = return
= return x- å®çŸ©ã«ãã>> =ãã¶ãã¢ãã
=ã¡ããã©x- å®çŸ©ã«ãããMaybeã¢ãããè¿ã
-第2ã¢ããæ³ã®å®è£
äœããªã>> = æ»ã
=ãªã-å®çŸ©ã«ãã>> =ã¢ããã®å Žå
-第2ã¢ããæ³ã®å®è£
æ³åŸãå°éããããããã®ãªãã·ã§ã³ãéžæããŸãã Maybeã¢ããã®å®å šãªå®çŸ©ã¯æ¬¡ã®ããã«ãªããŸãã
ã€ã³ã¹ã¿ã³ã¹ Monad ãã¶ã ã©ã
return x =ã¡ããã©x
ãªã>> = f =ãªã
ã¡ããã©x >> = f = f x
ããïŒ æåã®ã¢ãããäœæããŸããïŒ
ç§ãã¡èªèº«ãä¿è·ããããã«ãããã第3ã¢ããæ³ãæºãããŠããããšãæ€èšŒããŸãã
ïŒ mv >> = f ïŒ >> = g == mv >> = ïŒ \ x -> ïŒ f x >> = g ïŒ ïŒ
ãŸããmv = Nothingã®å Žåã®æ³åã確èªããŸãã
ïŒãªã>> = f ïŒ >> = g- å·ŠåŽ
=ãªã>> = g- å®çŸ©ã«ãã>> =
=ãªã-å®çŸ©ã«ãã>> =
ãªã>> = ïŒ \ x -> ïŒ f x >> = g ïŒ ïŒ -å³åŽ
=ãªã-å®çŸ©ã«ãã>> =
OKããã§ãã¯ã¯æåããŸããã 次ã«ã mv = Just vã§æ©èœãããã©ãããèŠãŠã¿ãŸããããããã§ã vã¯å€ã§ãã
ïŒ ïŒ Just v ïŒ >> = f ïŒ >> = g- å·ŠåŽ
= f v >> = g- å®çŸ©ã«ãã>> =
ïŒã¡ããã©v ïŒ >> = ïŒ \ x -> ïŒ f x >> = g ïŒ ïŒ -å³åŽ
= ïŒ \ x -> ïŒ f x >> = g ïŒ ïŒ v- å®çŸ©ã«ãã>> =
= f v >> = g- é¢æ°ã®éåžžã®äœ¿çšïŒããŒã¿åæžïŒ
ãããŠãŸãæåã æ³åŸãæœè¡ãããŠããŸãïŒ ããã¯æ¬åœã«ã¢ããã®æ£ããå®çŸ©ãããããŸããïŒ ãããŠèŠ³å®¢ã¯å€¢äžã«ãªããŸãïŒ
ãã®ãã¹ãŠã®ãã€ã³ãã¯äœã§ããïŒ ããã¯ã Maybeã¢ããã®ã¢ããé¢æ°ã®æãç°¡åã«çµåã§ããããšãæå³ããŸãã ãªããããéèŠãªã®ãçåã«æããããããŸããã Maybeã¢ããã®å€ãã®ã¢ããé¢æ°ãã€ãŸã倱æããå¯èœæ§ã®ããé¢æ°ãæ³åããããšã¯ãŸã£ããé£ãããããŸããã ã¿ã€ããInt-> Maybe Intã§ãããšããŸãããã åæ§ã®3ã€ã®æ©èœã次ã«ç€ºããŸãã
f :: Int- > å€å Int
f x = if x ` mod` 2 == 0 then Nothing else Just ïŒ 2 * x ïŒ
g :: Int- > å€å Int
g x = if x ` mod` 3 == 0 then else Nothing Just ïŒ 3 * x ïŒ
h :: Int- > å€å Int
h x = if x ` mod` 5 == 0 then else Nothing Just ïŒ 5 * x ïŒ
ãããã1ã€ã®é¢æ°ã«çµåããããšæããŸããããã¯ã f ã g ã hã®é ã«é©çšããçµæã§ãã
k :: Int- > å€å Int
ãŸãã3ã€ã®é¢æ°ã®ããããã倱æããå Žåãé¢æ°kã¯Nothingãè¿ãå¿ èŠããããŸãã ãã®é¢æ°ã¯ã2ã3ããŸãã¯5ã§å²ãåããæŽæ°ã§ãªãå Žåãå ¥åæ°ã«30ãæããŸãïŒå²ãåããå Žåãé¢æ°ã¯Nothingãè¿ããŸãïŒã
åã®è³æããããããããç解ããŠããã°ãã¢ããåæãä»ããŠkãæå®ã§ããããšã¯æããã§ãã
k = f > => g > => h
ãŸãã¯ã >> =æŒç®åã䜿çšã§ããŸãã
k x = f x >> = g >> = h
ãŸãã¯ã do- abstractã奜ããããããŸããïŒ
k x = do y < -f x
z < -g y
h z
誰ã§ãèšããããšã¯ç°¡åã§ãã {1ïŒåæã§ã¯ããä»»æã®æ¹æ³ã§ã¹ã©ã€ã¹ããããšããæ°žç¶çãªè¡šçŸã§ãæå³ã䌌ãŠããŸãã -泚 ãããã}
äžè¬ã«ãé¢æ°hã¯ã¢ããæ§é ãªãã§å®å šã«å®çŸ©ã§ããŸã;ãã®ããã«ãªããŸãïŒ
k x = ã±ãŒã¹ f x
ãªã->ãªã
ã¡ããã©y- > ã±ãŒã¹ g y ã®
ãªã->ãªã
ã¡ããã©z- > h z
ãªãã¢ãããéèŠãªã®ãã¯æããã§ãã è€æ°ã®Maybeé¢æ°ããã§ãŒã³åããããšã«ãããã³ãŒããåçã«ç°¡çŽ åããŸãã ãã®åœ¢åŒã§10åã®Maybeé¢æ°ãæ§æããããã®å€§ãŸããªéã¢ããã³ãŒããæ³åããŠãã ããã å³åŽã«éåžžã«å€§ããªã€ã³ãã³ãããããšãèªã¿ããããã²ã©ã圱é¿ãåããå ¥ãåã«ãªã£ãcaseåŒã®è¿·è·¯ã§ã¯èšç®ã®äžè¬çãªæ§é ã倱ãããŸãã ããããã¢ããã®å©ããåããŠã10åã®é¢æ°ã®æ§æã¯ç°¡åã«æžãããŠããŸãïŒ
f11 = f1 > => f2 > => f3 > => f4 > => f5 > => f6 > => f7 > => f8 > => f9 > => f10
ãŸãã¯ïŒ >> =ã䜿çšïŒïŒ
f11 x = f1 x >> = f2 >> = f3 >> = f4 >> = f5 >> = f6 >> = f7 >> = f8 >> = f9 >> = f10
ã¢ããã䜿çšãããšãã¢ããé¢æ°ã®æ§æã¯ãéåžžã®ïŒéã¢ããïŒé¢æ°ã®æ§æãšåããããç°¡åã§ãã
ã¢ããã¯åºæ¬çãªæŠå¿µã説æããã®ã«éåžžã«åœ¹ç«ã€ãããããŸããããæ··ä¹±ãããå¯èœæ§ããããŸãïŒå€ãã®äººã ã¯ãã¢ããã®å¯äžã®åœ¹å²ã¯éæ©èœèšç®ãã€ãŸãå ¥å/åºåïŒã³ã³ãœãŒã«ãŸãã¯ãã¡ã€ã«ã§ïŒãåŠçããããšã§ãããšèª€ã£ãŠä¿¡ããŠããŸããå¯å€ã°ããŒãã«ç¶æ ãªã©ã ãããŠãããã€ãã®ã¢ããèšç®ãã¢ãããªãã§åãæåã§å®è¡ã§ããããšã瀺ããŸããã ã¢ããã¯å¿ é ã®ãã®ã§ã¯ãªãã éåžžã«äŸ¿å©ã§ããããšãããããŸãã ãã®ãããéæ©èœã³ã³ãã¥ãŒãã£ã³ã°çšã®ã¢ããïŒ IOãäœ¿çš ïŒãçºæããåœåã®çç±ã«ãããããããã¯ããã«å€§ããªé©çšæ§ãããããšãããããŸããã ãã®ãããã¢ããã¯è¯ãã§ãã
次ã®ã¢ããã«ç§»ããŸãããã
ã¢ãããªã¹ã ïŒãªã¹ãïŒ
ãã¶ãããªããã¢ããã奜ããªããããªãã¯ãªã¹ãã»ã¢ããããæããã§ãããã ;-)ãã®å Žåã次ã®å®çŸ©ãå ¥åããŸãã
ã€ã³ã¹ã¿ã³ã¹ Monad [ ] ããã§
ïŒ >> = ïŒ = {-ãªã¹ãã®å®çŸ©>> =-}
return = {-ãªã¹ãã®æ»ãå€ã®å®çŸ©-}
空ã®ãªã¹ã[]ãè¡šãã«ã¯ããªã¹ãåã³ã³ã¹ãã©ã¯ã¿ãŒã䜿çšããããšã«æ³šæããŠãã ããã ããã¯å°ããªããã¯ã§ãïŒHaskellã®ãªã¹ãã«å¯ŸããŠç¹å¥ãªæ§æãµããŒããæäŸãããŸãïŒã ããããããã¹ãããšã¯äœããããŸããã
ãã¹ãŠã®ã¢ãããšåæ§ã«ãæåã®ã¿ã¹ã¯ã¯ãã®ã¢ããã®ã¢ããé¢æ°ãäœã§ããããç解ããããšã§ãã ãªã¹ãã®å Žåãåé é¢æ°fã¯æ¬¡ã®ããã«ãªããŸãã
f :: a- > [ b ]
ïŒ [b]ã¯ããã¡ããããã¿ã€ãbã®èŠçŽ ã®ãªã¹ãããæå³ããŸãïŒã ã¢ããé¢æ°ã®äžè¬åãããå®çŸ©ã¯æ¬¡ã®ããã«æžãããŠããããšãæãåºããŠãã ããïŒ
f :: a- > m b
äžéšã®ã¢ããmã«ã€ããŠã¯ãåã³ã³ã¹ãã©ã¯ã¿ã§ããå¿ èŠããããŸãã ãªã¹ãã¯ã¢ããã®æãããªåè£ã§ããããªã¹ããã¯åã³ã³ã¹ãã©ã¯ã¿ã§ããããã§ãïŒæ§æãHaskellã«çµã¿èŸŒãŸããŠããå Žåã§ãïŒã ãªãã·ã§ã³ã§ããªã¹ããèªåã§å®çŸ©ã§ããŸãã
ããŒã¿ãªã¹ãa = Nil | çæïŒãªã¹ãa ïŒ
ããã«å¯Ÿããã¢ããé¢æ°ã®ã¿ã€ãã¯ããã«å¿ããŠèŠããã§ãããïŒ
f :: a- >ãªã¹ãb
ãã ããæšæºã®æ§æã¯åŒãç¶ã䜿çšããŸãã
ãã®çš®ã®æ©èœã¯äœã§ããïŒ éåžžããããã¯ãã¿ã€ãaã®å ¥åå€ãåãåãã1ã€ã®äŸ¿å©ãªã³ã³ãããŒïŒãªã¹ãïŒã«åéãããã¿ã€ãbã®å€ã®æãçæããé¢æ°ãšããŠç解ãããŸãã ïŒãŸããã³ã³ããã®ããã«èŠããã¢ããããããŸããïŒããããå€ãã®å€ãè¿ãé¢æ°ãšèããå¥ã®æ¹æ³ãã€ãŸãããã®ãããªé¢æ°ã¯ã1ã€ã®ãç°ãªãå€ã®æãè¿ããŸãã ïŒããã§ã¯ãããŸãããã䞊ååŠçãæå³ãããããã䞊åããšããæå³ã§ã¯ãããŸãããïŒè€æ°ã®åºåå€ã¯åãªããªã¹ãã¢ã€ãã ã§ãã 次ã®ãããªé¢æ°ã䜿çšãããšã䟿å©ãªããŒã¹ãã¯ãã£ããéããŸãã
f :: Int- > [ Int ]
g :: Int- > [ Int ]
ããã§ã¯ã fãšgã®äž¡æ¹ãåãIntå€ãåããå€ãã®Intå€ãè¿ããŸãã ããããé¢æ°fã®åçµæãååŸããŠãé¢æ°gã®åçµæã«é©çšããã¢ããªã±ãŒã·ã§ã³ã®çµæãåéããå Žåã¯ã©ãã§ããããã é¢æ°gããã³fã®çµæãªã¹ãããåèŠçŽ ãã¢ã³ããã¯ããã«ããããçŽæ¥è¡ãããšãã§ããã°çŽ æŽãããã§ãããã ãããŠãããã¯ãªã¹ãã¢ããã䜿çšããŠè¡ãããšãã§ããŸãã
ãããã®é¢æ°ã®ããå ·äœçãªäŸã«ç§»ããŸãããïŒ
f :: Int- > [ Int ]
f x = [ x - 1 ã x ã x + 1 ]
g :: Int- > [ Int ]
g x = [ -x ã x ]
ããã2ã€ã®é¢æ°ãã©ã®ããã«ãæ§æãããŸããïŒ fxã¯ãªã¹ããè¿ããåèŠçŽ ã«gãé©çšããã«ã¯ã mapé¢æ°ãå¿ èŠã§ãã
f 10- > [ 9ã10ã11 ]
ããã g ïŒ f 10 ïŒ -> [ [ -9ã9 ] ã [ - 10ã10 ] ã [ - 11ã11 ] ]
ãã®æ°ããçµæã¯èå³æ·±ããã®ã§ããã fãšgã®åæã«ã¯ãªããŸãããç°ãªãã¿ã€ãïŒ Intãªã¹ãã ãã§ãªãã Intãªã¹ãã®ãªã¹ãïŒãããããã§ãã concaté¢æ°ã䜿çšããŠåçŽãªãªã¹ãã«æ»ããã«ããããšãã§ããŸãïŒãªã¹ãã1ã€ã«åçŽã«é£çµããŸãïŒã
-é£çµã¿ã€ãã«ç¹ã«æ³šæããŠãã ããïŒ[[a]]-> [a]
concat ïŒ map g ïŒ f 10 ïŒ ïŒ -> [ -9ã9ã-10ã10ã-11ã11 ]
æŽæ°ã«fãé©çšãã次ã«fã®åŸã«èµ·ãã£ãããšã«gãé©çšããããšã«ãã£ãŠçæããããã¹ãŠã®çµæã®ã»ãããåŸãŸããã fãšgãããã§å€ãã®çµæãçæããé¢æ°ãšèããå Žåããããã®åºåå€ã¯fã䜿çšããŠãã次ã«gã䜿çšãããã¹ãŠã®å¯èœãªã»ããã«ãªããŸãã ãããå³ã§è¡šãããšãã§ããŸãã
g | -9 | 9 ----> | | | 9 | f | g | -10 10 ----> | 10 ----> | | | 10 | | g | -11 | 11 ----> | | 11
fãšgã®æ§æã¯ã fãšgã®éã®ãã¹ãŠã®ãã¹ã®ã»ããã§ããããšãæ確ã«ããããŸãã
å¥åŠãªããšã«ããªã¹ãã¢ããã«å¯ŸããŠæŒç®å>> =ãå®çŸ©ããŸããïŒ æ¬¡ã®ããã«èšå®ãããŸãã
-mv :: [a]
-g :: a-> [b]
mv >> = g = concat ïŒ map g mv ïŒ
ããã§ã mvã¯ãªã¹ãã¢ããã®ã¢ããå€ã§ãïŒã¿ã€ãaã®å€ã®ãªã¹ãã«ãããŸããïŒã åã®äŸã§ã¯ã mvã¯f 10ã®èšç®çµæã§ãã å®çŸ©ã¯ç©ºã®ãªã¹ã[]ã«å¯ŸããŠãæ©èœããŸããé¢æ°ã空ã®ãªã¹ãã«ãããã³ã°ãããšç©ºã®ãªã¹ããåŸããã空ã®ãªã¹ãã®é£çµãåžžã«ç©ºã®ãªã¹ãã«ãªãããã§ãã çµæã¯ãæŒç®å>> =ã®éåžžã«åçŽãªå®çŸ©ã§ãã
[GHCãã¡ã³ãžã®æ³šæïŒGHCã³ã³ãã€ã©ã®>> =æŒç®åã¯ãåãããšãè¡ããŸãããããå¹ççãã€ç°ãªãæ¹æ³ã§å®è£ ããããšèããŠããŸãã]
ãã®ã¢ããã®æ»ãå€ãèšå®ããæ¹æ³ã¯ïŒ ã¢ããã®ãªã¹ãã®å€ããå€ãã®å€ãè¿ããã¢ã¯ã·ã§ã³ããšèããŠã¿ãŸãããã ä»ã®ã¢ããã®ããã«ã returnã¯ãŠãããé¢æ°ãšåçã§ãªããã°ãªããªãããšãæãåºããŠãã ããã ãªã¹ãã¢ããã®åäžã®é¢æ°ã«çžåœãããã®ã¯äœã§ããïŒ å€ãåãããèšç®ãã®åŸãåã«ãã®å€ãè¿ããã¢ã¯ã·ã§ã³ããè¿ãå¿ èŠããããŸãã ãã®ããã returnã¯åã«ç©ºã®ãªã¹ããè¿ãããšãã§ããªãããšã«æ°ä»ããŸããã returnã«ã€ããŠæ¬¡ã®ãããªãã®ãæ³å®ããã®ã¯åççã§ãã
return :: a- > [ b ]
return x = [ x ]
ã€ãŸãã returnã¯ãåäžã®å€ãããªã¹ããèŠçŽ çã«äœæããŸãã ãã®å Žåãã¢ããã®æ³åãé å®ãããŠãããã©ããã確èªããŸãã
-f :: a-> [b]
-x :: a
return x >> = f = concat ïŒ map f ïŒ return x ïŒ ïŒ -å®çŸ©ã«ãã>> =
= concat ïŒ map f [ x ] ïŒ -å®çŸ©ã«ãã
= concat [ f x ] -å®çŸ©ãããã«ãã
= f x- å®çŸ©ã«ãããé£çµ
-æåã®ã¢ããæ³ã®å®è£
-mv :: [a]
mv >> = return = concat ïŒ map return mv ïŒ -å®çŸ©ã«ãã>> =
= concat ïŒ map ïŒ \ x- > [ x ] ïŒ mv ïŒ -å®çŸ©ã«ãã
-2ã€ã®ã±ãŒã¹ïŒ
-ã±ãŒã¹1ïŒmv == []
= concat ïŒ map ïŒ \ x- > [ x ] ïŒ [ ] ïŒ -å®çŸ©ã«ãããmv
= concat [ ] -å®çŸ©ã«ããããã
= [ ] -å®çŸ©ã«ããconcat
= mv- å®çŸ©ã«ããmv
-ã±ãŒã¹2ïŒmv == [v1ãv2ã...]
= concat ïŒ map ïŒ \ x- > [ x ] ïŒ [ v1 ã v2 ã ... ] ïŒ -å®çŸ©ã«ãããmv
= concat [ [ v1 ] ã [ v2 ] ã ... ] -å®çŸ©ã«ããããã
= [ v1 ã v2 ã ... ] -å®çŸ©ã«ããconcat
= mv- å®çŸ©ã«ããmv
-第2ã¢ããæ³ã®å®è£
ããŠãã¢ããã®2ã€ã®æ³åã蚌æãããŠããŸãã ãªã¿ãŒã³ã®ä»ã®å®çŸ©ãè©ŠããŠã¿ããšããå ŽåããããŸãïŒ ãªã¿ãŒã³ãè¿ãããå Žåãããšãã°ç¹å®ã®ãªã¹ã[0ã2ã3] ããŸãã¯åŒæ°ã®ã³ããŒãç¡éã«è¿ãå ŽåïŒããããã¯ãã¹ãŠã¢ããã®æ³åã«éåããŠããããšãããããŸãã ããã¯ãã¢ããã®æ³åãå®è·µããè¯ãæ¹æ³ã§ãã
ãªã¹ããå®éã®ã¢ãããšåŒã¶åã«ã3çªç®ã®ã¢ããã®æ³åã蚌æããããšãæ®ã£ãŠããŸãã ããã¯èšããªããã°ãªããªããããã¯ããé£ãããããšã«ããããããšããŸãã ã¿ã¹ã¯ãç°¡çŽ åããããã«ã3çªç®ã®ã¢ããæ³åïŒã¢ããæ§æã§å®çŸ©ïŒã®ãå¿«é©ãªã圢åŒãåããŸãã ãŸãããªã¹ãã®åé åæã®å®çŸ©ãå¿ èŠã§ãã
-3çªç®ã®ã¢ããæ³ïŒå¿«é©ãªããŒãžã§ã³ïŒïŒ
ïŒ f > => g ïŒ > => h = f > => ïŒ g > => h ïŒ
-å®çŸ©ã«ããïŒ
f > => g = \ x- > f x >> = g
-ãªã¹ãã¢ããã®å®çŸ©>> =ã䜿çšããŸãã
f > => g = \ x- > concat ïŒ map g ïŒ f x ïŒ ïŒ
-æ§ææŒç®åïŒãïŒã䜿çšããŠåŒãæžãæããããšãã§ããŸãã
f > => g = concat ããã g ã f
ããã«ã concatããã³mapé¢æ°ã®ããã€ãã®ããããã£ã䜿çšããŸãã ããªãã¯ä»ã®ãšãã圌ããä¿¡ä»°ã«é£ããŠè¡ããªããã°ãªããªãã§ãããã 次ã«ãããããååŸããæ¹æ³ã瀺ããŸãã
-æ¹çšåŒ1ïŒ
map ïŒ fãg ïŒ = ããã f ã å°å³ g
-æ¹çšåŒ2ïŒ
ããã f ã concat = concat ã ããã ïŒ ããã f ïŒ
-æ¹çšåŒ3ïŒ
concat ã concat = concat ã å°å³ é£çµ
以åããããïŒãïŒã¯ïŒçŽç²ãªïŒåææŒç®åã§ãããšèšããŸããã é¢æ°ãã€ãŸããããfã®ãããªåŒãé©çšãããããåªå é äœãäœããªããŸãã ãããgã¯å¹³åã®ã¿ïŒãããfïŒã ïŒãããgïŒ ã Haskellããã°ã©ããŒã¯éåžžãå¯èœã§ããã°æ¬åŒ§ãåãé€ããŸãã ãŸããããšãã°ã map fé¢æ°ã¯ãå®éã«ã¯2ã€ã®åŒæ°ãæã€ãããé¢æ°ïŒãªã¹ãã¢ã€ãã çšã®é¢æ°ãšãªã¹ãèªäœïŒã§ããããšãç解ããããšãéèŠã§ãã ã«ãªãŒåã«ã€ããŠè©±ããŠããããšãæãåºããšã ãããfã¯1ã€ã®ãªã¹ããååŸããŠå¥ã®ãªã¹ããè¿ãé¢æ°ã§ããã fã¯åèŠçŽ ã«é©çšãããŠãããšæšæž¬ã§ããŸãã ä»ããã«ã¬ãŒããããã䜿ããŸãã
ãããã£ãŠããããŸã§ã«è¿°ã¹ãããããšããã¹ãŠèæ ®ã«å ¥ãã蚌æ ã®çµè«ïŒ
ïŒ f > => g ïŒ > => h
= ïŒ concatãmap gãf ïŒ > => h- å®çŸ©ã«ãã> =>
= concat ã ããã h ã ïŒ concatãmap gãf ïŒ -å®çŸ©ã«ãã> =>
= concat ã ããã h ã concat ã ããã g ã f- äžèŠãªè§ãã£ããåé€ãã
f > => ïŒ g > => h ïŒ
= f > => ïŒ concatãmap hãg ïŒ -å®çŸ©ã«ãã> =>
= concat ã map ïŒ concatãmap h.g ïŒ ã f- å®çŸ©ã«ãã> =>
= concat ã map ïŒ ïŒ concatãmap h ïŒ .g ïŒ ã f ã¯åçã®å€æã§ã
= concat ã ïŒ map ïŒ concatãmap h ïŒ ïŒ ã ïŒ ããã g ïŒ ã f- åŒ1ã«ãã
= concat ã ããã ïŒ é£çµ ããã h ïŒ ã ããã g ã f- äžèŠãªè§ãã£ããåé€ãã
= concat ã å°å³ é£çµ å°å³ ïŒ å°å³ h ïŒ ã ããã g ã f- åŒ1ã«ãã
次ã®ããšã瀺ãå¿ èŠããããŸãã
concat ã ããã h ã concat = concat ã å°å³ é£çµ å°å³ ïŒ å°å³ h ïŒ
ããã蚌æããŸãããã
-æ確ã«ããããã«æ¬åŒ§ãè¿œå ããŸãã
concat ã ïŒ map hãconcat ïŒ = concat ã å°å³ é£çµ å°å³ ïŒ å°å³ h ïŒ
-åŒ2ã«ãããšïŒ
concat ã concat ã map ïŒ map h ïŒ = concat ã å°å³ é£çµ å°å³ ïŒ å°å³ h ïŒ
-æ確ã«ããããã«æ¬åŒ§ãè¿œå ããŸãã
ïŒ concatãconcat ïŒ ã map ïŒ map h ïŒ = concat ã å°å³ é£çµ å°å³ ïŒ å°å³ h ïŒ
-åŒ3ã«åŸã£ãŠïŒ
concat ã å°å³ é£çµ map ïŒ map h ïŒ = concat ã å°å³ é£çµ å°å³ ïŒ å°å³ h ïŒ
ãããŠããã§çµããã§ãã ãµãïŒ å®éãHaskellistsããããè¡ãããšã¯ãã£ãã«ãããŸããããçãããã¢ãããæ¬åœã«ã¢ããã§ããããšã瀺ã蚌æ ãå¿ èŠã§ãã
éçããŒãïŒ ããã/é£çµã«ããã¢ã€ãã³ãã£ãã£ã®å°åºïŒåŒ1ã2ãããã³3ïŒ
æºåãã
身å ã®èšŒæãé²ããåã«ãä»ã®ããã€ãã®èšŒæãããå¿ èŠããããŸãïŒæ°åŠã¯é£ããïŒïŒã ãããããªã¹ãããŸãã
-åŒ4ïŒ
concat ïŒ xïŒxs ïŒ = x ++ concat xs
-åŒ5ïŒ
concat ïŒ x ++ y ïŒ = concat x ++ concat y
-åŒ6ïŒ
map f ïŒ x ++ y ïŒ = map f x ++ map f y
åŒ4ã¯concatã®å®çŸ©ããåŸãããŸãã åŒ5ã¯ãåŒ4ã䜿çšããxã®åž°çŽæ³ã«ãã£ãŠç°¡åã«èšŒæãããŸãã
-ããŒã¹ã±ãŒã¹ïŒx-空ã®ãªã¹ã
concat ïŒ [ ] ++ y ïŒ = concat [ ] ++ concat y
concat y = [ ] ++ concat y- å®çŸ©ã«ããconcat []
concat y = concat y- å®çŸ©ã«ãã++
-ããã§ãã
-èªå°ïŒãªã¹ãxã¯ç©ºã§ã¯ãããŸããã x1ã¯ãªã¹ãã®å é ã§ãã xsã¯ãªã¹ãã®æ«å°Ÿã§ãã
concat ïŒ ïŒ x1ïŒxs ïŒ ++ y ïŒ
= concat ïŒ x1ïŒ ïŒ xs ++ y ïŒ ïŒ -å®çŸ©ã«ãã++
= x1 ++ concat ïŒ xs ++ y ïŒ -åŒ4
= x1 ++ concat xs ++ concat y- åž°çŽç仮説
concat ïŒ x1ïŒxs ïŒ ++ concat y
= x1 ++ concat xs ++ concat y- åŒ4
-蚌ââæããå¿ èŠããã£ãã®ã¯äºå®ã§ãã
åŒ6ã¯åãæ¹æ³ã§èšŒæã§ããŸãã
-ããŒã¹ã±ãŒã¹ïŒx-空ã®ãªã¹ã
map f ïŒ [ ] ++ y ïŒ = map f [ ] ++ map f y
map f y = [ ] ++ map f y
map f y = map f y
-ããã§ãã
-èªå°ïŒãªã¹ãxã¯ç©ºã§ã¯ãããŸããã x1ã¯ãªã¹ãã®å é ã§ãã xsã¯ãªã¹ãã®æ«å°Ÿã§ãã
ããã f ïŒ x ++ y ïŒ
= ããã f ïŒ ïŒ x1ïŒxs ïŒ ++ y ïŒ
= map f ïŒ x1ïŒ ïŒ xs ++ y ïŒ ïŒ -å®çŸ©ã«ãã++
= f x1ïŒ map f ïŒ xs ++ y ïŒ -mapã§å®çŸ©ãããŠãã
= f x1ïŒ ïŒ map f xs ++ map f y ïŒ -åž°çŽç仮説
= ïŒ f x1ïŒ map f xs ïŒ ++ map f y- å®çŸ©ã«ãã++
= map f ïŒ x1ïŒxs ïŒ ++ map f y -mapã§å®çŸ©ãããŠãã
= map f x ++ map f y- å®çŸ©ã«ããx
-蚌ââæããå¿ èŠããã£ãã®ã¯äºå®ã§ãã
ããã§ãæ¹çšåŒ1ã2ãããã³3ã蚌æããŸãã
åŒ1ïŒ
map ïŒ fãg ïŒ = ããã f ã å°å³ g
mapã®å®çŸ©ã ãã§ãªããäž¡åŽã®æé»ã®ãªã¹ãåŒæ°ã«åž°çŽæ³ã䜿çšããŸã ã
-åºæ¬ã±ãŒã¹ïŒç©ºã®ãªã¹ã
map ïŒ fãg ïŒ [ ] = [ ]
ïŒ ããã f ã ããã g ïŒ [ ] = ããã f ïŒ ããã g [ ] ïŒ = ããã f [ ] = [ ]
-OK
-èªå°ïŒç©ºã§ãªããªã¹ãïŒ
ããã ïŒ fãg ïŒ ïŒ xïŒxs ïŒ
= ïŒ ïŒ fãg ïŒ x ïŒ ïŒïŒ map ïŒ fãg ïŒ xs ïŒ -å®çŸ©ããã
= ïŒ f ïŒ g x ïŒ ïŒ ïŒïŒ map ïŒ fãg ïŒ xs ïŒ -å®çŸ©ã«ããïŒãïŒ
ïŒ ããã f ã ããã g ïŒ ïŒ xïŒxs ïŒ
= map f ïŒ map g ïŒ xïŒxs ïŒ ïŒ -å®çŸ©ã«ããïŒãïŒ
= map f ïŒ ïŒ g x ïŒ ïŒïŒ map g xs ïŒ ïŒ -å®çŸ©ã«ããããã
= ïŒ f ïŒ g x ïŒ ïŒ ïŒïŒ map f ïŒ map g xs ïŒ ïŒ -å®çŸ©ã«ããmap
= ïŒ f ïŒ g x ïŒ ïŒ ïŒ ïŒ ïŒ map fãmap g ïŒ xs ïŒ -å®çŸ©ã«ããïŒãïŒ
= ïŒ f ïŒ g x ïŒ ïŒ ïŒïŒ map ïŒ fãg ïŒ xs ïŒ -åž°çŽç仮説
-蚌ââæããå¿ èŠããã£ãã®ã¯äºå®ã§ãã
åŒ2ïŒ
ããã f ã concat = concat ã ããã ïŒ ããã f ïŒ
åž°çŽæ³ã§èšŒæããïŒ
-åºæ¬ã±ãŒã¹ïŒç©ºã®ãªã¹ã
ïŒ map fãconcat ïŒ [ ] = map f ïŒ concat [ ] ïŒ = map f [ ] = [ ]
ïŒ concatãmap ïŒ map f ïŒ ïŒ [ ] = concat ïŒ map ïŒ map f ïŒ [ ] ïŒ = concat [ ] = [ ]
-OK
-èªå°ïŒç©ºã§ãªããªã¹ã
ïŒ map fãconcat ïŒ ïŒ xïŒxs ïŒ
= map f ïŒ concat ïŒ xïŒxs ïŒ ïŒ -å®çŸ©ã«ããïŒãïŒ
= map f ïŒ x ++ concat xs ïŒ -åŒ4
= map f x ++ ïŒ map f ïŒ concat xs ïŒ ïŒ -åŒ6
= map f x ++ ïŒ ïŒ map fãconcat ïŒ xs ïŒ -å®çŸ©ã«ããïŒãïŒ
= map f x ++ ïŒ ïŒ concatãmap ïŒ map f ïŒ ïŒ xs ïŒ -åž°çŽç仮説
= map f x ++ concat ïŒ map ïŒ map f ïŒ xs ïŒ -å®çŸ©ã«ããïŒãïŒ
ïŒ concatãmap ïŒ map f ïŒ ïŒ ïŒ xïŒxs ïŒ
= concat ïŒ map ïŒ map f ïŒ ïŒ xïŒxs ïŒ ïŒ -å®çŸ©ã«ããïŒãïŒ
= concat ïŒ map f xïŒ map ïŒ map f ïŒ xs ïŒ -å®çŸ©ã«ããmap
= map f x ++ concat ïŒ map ïŒ map f ïŒ xs ïŒ -åŒ4
-蚌ââæããå¿ èŠããã£ãã®ã¯äºå®ã§ãã
åŒ3ïŒ
concat ã concat = concat ã å°å³ é£çµ
ãã€ãã®ããã«ãèªå°ã䜿çšããŸãã
-åºæ¬ã±ãŒã¹ïŒç©ºã®ãªã¹ã
ïŒ concatãconcat ïŒ [ ] = concat ïŒ concat [ ] ïŒ = concat [ ] = [ ]
ïŒ concatãmap concat ïŒ [ ] = concat ïŒ map concat [ ] ïŒ = concat [ ] = [ ]
-ãã
-èªå°ïŒç©ºã§ãªããªã¹ã
ïŒ concatãconcat ïŒ ïŒ xïŒxs ïŒ
= concat ïŒ concat ïŒ xïŒxs ïŒ ïŒ -å®çŸ©ã«ããïŒãïŒ
= concat ïŒ x ++ concat xs ïŒ -åŒ4
= concat x ++ concat ïŒ concat xs ïŒ -åŒ5
ïŒ concatãmap concat ïŒ ïŒ xïŒxs ïŒ
= concat ïŒ map concat ïŒ xïŒxs ïŒ ïŒ -å®çŸ©ã«ããïŒãïŒ
= concat ïŒ concat xïŒ map concat xs ïŒ -map㧠å®çŸ©ãããŠãã
= concat x ++ concat ïŒ map concat xs ïŒ -åŒ4
= concat x ++ ïŒ concatãmap concat ïŒ xs- å®çŸ©ã«ããïŒãïŒ
= concat x ++ ïŒ concatãconcat ïŒ xs- åž°çŽç仮説
= concat x ++ concat ïŒ concat xs ïŒ -å®çŸ©ã«ããïŒãïŒ
-蚌ââæããå¿ èŠããã£ãã®ã¯äºå®ã§ãã
ãªã¹ãã¢ãããæ¬åœã«ã¢ããã§ããããšãçãäœå°ããªãããšãé¡ã£ãŠããŸãã ;-)
ãããŠãã¡ãããããã§æãèå³æ·±ã質åã¯ããã§ãïŒã¢ãããªãã§ã¯é£ãããªã¹ãã¢ããã§äœãã§ããã§ããããïŒ ç°¡åãªäŸã次ã«ç€ºããŸããåèšã7ã§ãã1ã6ã®æ°åã®ãã¢ããã¹ãŠèŠã€ããŸãïŒæ°åã¯ãµã€ã³ããªã©ïŒã ãªã¹ãã¢ããã䜿çšãããšãåé¡ã解決ããã®ã¯ç°¡åã§ãã
-<font color = blue> do </ font>è¡šèšã䜿çšããŸãã
do n1 <- [ 1 .. 6 ]
n2 <- [ 1 .. 6 ]
n1 + n2 == 7ã® å Žåã ïŒ n1 ã n2 ïŒ else [ ]ã è¿ã ãŸã
-çµæïŒ[ïŒ1,6ïŒãïŒ2,5ïŒãïŒ3,4ïŒãïŒ4,3ïŒãïŒ5,2ïŒãïŒ6,1ïŒ]
ãŸãã doè¡šèšãªãã§æžãæããããšãã§ããŸãããæ確ã§ã¯ãããŸããã
[ 1 .. 6 ] >> = \ n1- >
[ 1 .. 6 ] >> = \ n2- >
n1 + n2 == 7ã® å Žåã ïŒ n1 ã n2 ïŒ else [ ]ã è¿ã ãŸã
ã©ã®ããã«æ©èœããŸããïŒ åº§ã£ãŠ>> =ã«é¢é£ãããã¹ãŠã®èšç®ããã¬ãŒã¹ãããªã¹ãã«æ»ãå¿ èŠããããŸãããããã§ã¯æã®èª¬æã瀺ããŸãã ãããã£ãŠïŒ [1..6]ã¯ãªã¹ãã¢ããã®ã¢ããå€ã§ããã n1ã¯äžåºŠã«ãã¹ãŠã®ã¢ããå€ãå®è¡ããŸã ã n2-ãŸã£ããåãã ãããŠãåèšãæ£ãããã¹ãŠã®ãã¢ïŒn1ãn2ïŒãè¿ãããŸãã ããã¯ããããããããã1ã€ã®èŠçŽ ã§ãããã®ããã«ããã¹ãŠã®èŠçŽ ã§é¢æ°ãèšç®ããæ¹æ³ã§ãã ãããã¢ãããªã¹ãã®æ¬è³ªã§ãã
Haskellããã°ã©ãã³ã°ã®ç¥èãè±å¯ã§ããã°ãããããé ã®äžã§ãµã€ã¬ã³ãèãããã§ãããã ããªããŠããšã ïŒãç§ã¯ããªãã®digããèããã ããªããªã¹ãå å è¡šèšã䜿çšããªãã®ã§ããïŒããããŠæ¬åœã«ïŒ
[ ïŒ n1 ã n2 ïŒ | n1 <- [ 1 .. 6 ] ã n2 <- [ 1 .. 6 ] ã n1 + n2 == 7 ]
ãªã¹ãã¢ãããšãªã¹ããžã§ãã¬ãŒã¿ãŒã®æ©èœã¯åãã§ãã ãã®æ§æãŸãã¯ãã®æ§æã®éžæã¯èšå®ã«äŸåããã¿ã¹ã¯ã«ãã£ãŠæ±ºå®ããããšãã§ããŸãã 圌ã®èšäºãComprehending Monadsãã§ã¯ã Phil Walder ïŒåœŒã®å€ãã®èšäºã®ããã«ã¿ã€ãã«ã«ãããããããŸãïŒã¯ããªã¹ããžã§ãã¬ãŒã¿ãŒã®æ§æãä»»æã®ã¢ããã«æ¡åŒµããããšããææ¡ããŸããã ããã«ããããããããã®ææ¡ã¯çŸåšã®èšé²ãæ¯æããŠæåŠãããŸããã
ãªã¹ãã¢ããã¯ããªã¹ããžã§ãã¬ãŒã¿ãŒã®åãªã代æ¿æ段ã§ã¯ãããŸããã äžæ¹ã§ã¯ãããããã¢ããã®å身ãšé£æºããéåžžã«äžè¬çãªé¢æ°ããããããããŸãã ãªã¹ãã¢ããã§ãåäœããŸãã äžæ¹ã MonadPlusãšåŒã°ããMonadåã¯ã©ã¹ã«ã¯æ¡åŒµæ©èœããããŸãã ã¢ããã®æ©èœãè£å®ããŸãïŒå ·äœçã«ã¯ãã¢ããã®ããŒããèŠçŽ ãå®çŸ©ãã2ã€ã®ã¢ããå€ããè¿œå ãããæäœãå°å ¥ããŸãïŒã ãªã¹ãã¯MonadãšMonadPlusã®äž¡æ¹ã®å身ã«ãã£ãŠäœæãããŸããã€ãŸãã MonadPlusã®å ±éæ©èœã¯ãªã¹ãã§ãæ©èœããŸãã ïŒããšãã°ã concaté¢æ°ã®äžè¬å-ãªã¹ããå«ãMonadPlusã®ãã¹ãŠã®ã€ã³ã«ããŒã·ã§ã³ã§æ©èœããmsum é¢æ° ïŒããããŸããå€ãã®ããŒã¿åã§æ©èœã§ããããååã«åå¥ã«æå®ããªãäžè¬åæ©èœã䜿çšããã®ã¯çŽ æŽãããããšã§ãã ããã¯æãããªåå©ã§ãã
次å
次ã®èšäºã§ã¯ããšã©ãŒã远跡ã§ããã¢ããã«ã€ããŠèª¬æããŸãã
å 容
ããŒã1ïŒåºæ¬
ããŒã2ïŒé¢æ°>> =ããã³return
ããŒã3ïŒã¢ããã®æ³å
ããŒã4ïŒå€åã¢ãããšãªã¹ãã¢ãã