-ã¢ãããšã¯äœã§ããïŒ
-å¯äœçšããçŽç²ãªèšç®ãåé¢ããããã
ïŒHaskellèšèªã«é¢ãããªã³ã©ã€ã³ãã£ã¹ã«ãã·ã§ã³ããïŒ
ã·ã£ãŒããã¯ããŒã ãºãšã¯ããœã³å士ã¯æ°çã§é£ãã§ããŸãã æ¿é§ã«ãªããæ¹åã倱ããŸãã å°ããªã®ã£ããããããŸã-ãããŠã圌ãã¯å°çäžã®äººéãèŠãŸãã
-芪æãªããç§ãã¡ãã©ãã«ãããæããŠããããŸããïŒ
ã颚è¹ããã«å ¥ããŠã
ãã®åŸã圌ãã¯ããããããã«éã³ãåã³åœŒãã¯äœãèŠããŸããã
ãããã¯æ°åŠè ã§ããããšããŒã ãºã¯èšããŸãã
ãã§ããªãïŒã
-圌ã®çãã¯çµ¶å¯Ÿã«æ£ç¢ºã§ããã絶察ã«åœ¹ã«ç«ã¡ãŸããã
ïŒåè«ïŒ
å€ä»£ãšãžãã人ã5å¹ã®éãæ°ãããšæžããããšæã£ããšãã圌ãã¯5å¹ã®éã®çµµãæããã 圌ãã70人ãæ°ãããšæžããããšæã£ããšãã圌ãã¯70人ã®äººç©ãæããŸããã 圌ãã矀ãã§300å¹ã®çŸãæ°ãããšæžãããšãã圌ãã¯...-ãŸããäžè¬çã«ãããªãã¯ç解ããŠããŸãã ãã®ãããå€ä»£ãšãžãã人ã¯ãæãç¥çã§æ zyãªäººã ãããããã¹ãŠã®èšé²ã«å ±éããäœããèŠããŸã§èŠãã¿ãæ°ããŠããéã®æŠå¿µãæ°ããŠãããã®ã®æ§è³ªããåé¢ããŸããã ãããŠãå¥ã®è³¢ãæ zyãªãšãžãã人ã¯ã人ã ãæ°åãæå®ããããã«äœ¿çšããŠããå€ãã®ã¹ãã£ãã¯ãã¯ããã«å°ãªãæåæ°ã§çœ®ãæããçãçµã¿åããã§èšå€§ãªæ°ã®ã¹ãã£ãã¯ã眮ãæããããšãã§ããŸããã
ãããã®è³¢ãæ zyãªãšãžãã人ãããããšã¯ãæœè±¡åãšåŒã°ããŸãã 圌ãã¯ãäœãã®éã«é¢ãããã¹ãŠã®èšé²ã«ç¹åŸŽçãªå ±éç¹ã«æ°ã¥ãããã®å ±éç¹ãã«ãŠã³ãããããªããžã§ã¯ãã®ç¹å®ã®ããããã£ããåé¢ããŸããã ä»æ¥ã®æ°åãšåŒã°ãããã®æœè±¡åã®æå³ãšãããã人ã ã®ç掻ãã©ãã»ã©æ¥œã«ããŠããã®ããç解ããŠããã°ãHaskellèšèªã®æœè±¡åãç解ããããšã¯é£ãããããŸããã ã«ããŽãªã®æ°åŠççè«ããç§ãã¡ã«æ¥ãæãããååã«ãããããããããããç解ããããšã¯ãæ°åããšåŒã°ããæœè±¡åããé£ãããããŸããã ããããç解ããããã«ãã«ããŽãªãŒã®çè«ããŸãã¯é«æ ¡ã®ããªã¥ãŒã ã®æ°åŠãããç¥ãå¿ èŠã¯ãããŸããïŒç®æ°ã§ååã§ãïŒã ãŸããæãããå€ãã®æ°åŠçæŠå¿µã«é Œããã«ããããã説æããããšãã§ããŸãã ãããŠãHaskellã®æœè±¡åã®æå³ã¯æ°åã®æå³ãšãŸã£ããåãã§ã-ããã°ã©ããŒã«ãšã£ãŠç掻ã楜ã«ãªããŸãïŒãããŠã©ãã ãæ³åããããšãã§ããŸããïŒïŒã
æ©èœããã°ã©ã ãšåœä»€ããã°ã©ã ã®éã
çŽç²ãªæ©èœã®å©ç¹ãçºèŠãã
èšç®ãšããã®ä»ã
ãä»ã®äœããã®ã«ãã»ã«å
ãã¡ã³ã¯ã¿ãŒã¯ç°¡åã§ã¯ãããŸããããéåžžã«ç°¡åã§ãïŒ
é©çšãã¡ã³ã¯ã¿ãŒãéåžžã«ç°¡åã§ãïŒ
ããªãã¯ç¬ããŸãããã¢ãããç°¡åã§ãïŒ
ãããŠãããã«ããã€ãã®ã¢ãããå®çŸ©ããŸããã
ã¢ãããé©çšãã
ã©ã€ã¿ãŒã¢ãããå®çŸ©ããã¢ãã€ããç¥ã
ã¢ãã€ããšãã¡ã³ã¯ã¿ãŒãé©çšãã¡ã³ã¯ã¿ãŒããã³ã¢ããã®æ³å
åã¯ã©ã¹ïŒå€æ°ã®æ©èœãç¡æã§ïŒ
I / OïŒIOã¢ãã
æœè±¡åãç解ïŒããã³åãå ¥ãïŒããã«ã¯ãéåžžã人ã ã¯ããããããã€ãã®è§åºŠããèŠãå¿ èŠããããŸãã
æåã«ãææ¡ãããæœè±¡åã®åœ¢åŒã§è€éãã®ã¬ãã«ãè¿œå ããããšã§ãåžžã«ééããéåžžã«é«ãã¬ãã«ã®è€éããæé€ã§ããããšãç解ããå¿ èŠããããŸãã ãããã£ãŠãçŽç²ãªé¢æ°ã䜿çšããŠããã°ã©ããééããããšã®ãªãèšå€§ãªæ°ã®åé¡ã«ã€ããŠèª¬æããŸãïŒå¿é ããããšã¯ãããŸããã以äžã«èª¬æããŸãïŒã
第äºã«ãææ¡ãããæœè±¡åãã©ã®ããã«å®è£ ããããããããŠãã®å®è£ ãã©ã®ããã«ç¹å®ã®ã±ãŒã¹ã§ã¯ãªãéåžžã«å€æ§ãªç°ãªãç¶æ³ã§ããã䜿çšã§ããããç解ããå¿ èŠããããŸãã ãããã£ãŠãHaskellã®æœè±¡åãå®è£ ããããžãã¯ã説æããããããé©çšå¯èœãªã ãã§ãªããä¿¡ããããªãã»ã©å€ãã®ç¶æ³ã§å€§ããªå©ç¹ãæäŸããããšã瀺ããŸãã
ãããŠç¬¬äžã«ã人ã ã¯æœè±¡åãã©ã®ããã«å®è£ ããããã ãã§ãªããããããæ¥åžžç掻ã«ã©ã®ããã«é©çšããããç解ããå¿ èŠããããŸãã ãããã£ãŠãããã«ã€ããŠã¯èšäºã§èª¬æããŸãã ããã«ãããã¯åçŽã§ã¯ãããŸããããéåžžã«åçŽã§ã-ãããã®æœè±¡åãã©ã®ããã«å®è£ ãããŠããããç解ãããããç°¡åã§ãïŒãããŠã説æãããæœè±¡åã®å®è£ ãç解ããããšã¯é£ãããªãããšãããããŸãïŒã
ãã ããå°å ¥ã¯å€å°é ãããããããããéå§ããŸãã èšäºã«ã¯ã³ãŒããã»ãšãã©ãªãã®ã§ããã®æœè±¡åã®çŸãããšãã¯ãŒãç解ããŠè©äŸ¡ããããã«Haskellã®æ§æã«ç²Ÿéããå¿ èŠã¯ãããŸããã
å
責äºé
ç§ã¯çµéšè±å¯ãªHaskellããã°ã©ããŒã§ã¯ãããŸããã ç§ã¯ãã®èšèªãæ¬åœã«å¥œãã§ãçŸæç¹ã§ã¯ãŸã åŠç¿éçšã«ãããŸãïŒç¥èã®ç¿åŸã ãã§ãªããæèã®åæ§ç¯ãå¿
èŠãªãããããã¯æéã®ããã»ã¹ã§ã¯ãããŸããïŒã æè¿ãç§ã¯é¢æ°åããã°ã©ãã³ã°ãšHaskellã«ã€ããŠãåœä»€åããã°ã©ãã³ã°èšèªã®ã¿ã«ç²ŸéããŠããããã°ã©ããŒãšäœåºŠã話ããªããã°ãªããŸããã§ããã ãã®éçšã§ãHaskellèšèªã®äž»èŠãªæœè±¡åã«ã€ããŠãããæ確ã§æ§é åããã説æã«åãçµãå¿
èŠãããããšã«æ°ä»ããŸããã ãã®è³æã¯ããã®ãããªæ§é åã®è©Šã¿ã«ãããŸããã èªè
ã®çããããç§ã®ãã¬ãŒã³ããŒã·ã§ã³ã®äžæ£ç¢ºãªå¯èœæ§ãšãããªãã«ååã«ç解ãããŠããªãããã«æããç¬éã®äž¡æ¹ãç§ã«ææããŠããã ããã°å¹žãã§ãã
æ©èœããã°ã©ã ãšåœä»€ããã°ã©ã ã®éã
é¢æ°åèšèªãšåœä»€åèšèªã§æžãããããã°ã©ã ãé³¥ç°å³ã§èŠããšãéãã¯ãããŸããã ãããã®ããã°ã©ã ãšä»ã®ããã°ã©ã ã¯ã©ã¡ããããœãŒã¹ããŒã¿ãåãå ¥ãããœãŒã¹ããå€æãããä»ã®ããŒã¿ãåºåããäžçš®ã®ãã©ãã¯ããã¯ã¹ã§ãã ãã©ãã¯ããã¯ã¹å ã§ããŒã¿å€æãã©ã®ããã«è¡ãããããæ£ç¢ºã«ç解ããããã«ããã©ãã¯ããã¯ã¹å ã確èªããå Žåã«éãã確èªããŸãã
åœä»€åãã©ãã¯ããã¯ã¹ã確èªãããšãããã«å«ãŸããããŒã¿ãå€æ°ã«å²ãåœãŠãããå¿ èŠãªããŒã¿ãååŸãããŸã§ãããã®å€æ°ãç¹°ãè¿ãå€æŽããããã©ãã¯ããã¯ã¹ããçºè¡ãããããšãããããŸãã
æ©èœçãªãã©ãã¯ããã¯ã¹ã§ã¯ãçä¿¡ããŒã¿ã®çºä¿¡ããŒã¿ãžã®ãã®å€æã¯ãçä¿¡ããŒã¿ãžã®äŸåã®èŠ³ç¹ããæçµçµæãè¡šãããç¹å®ã®åŒãé©çšããããšã«ãã£ãŠè¡ãããŸãã åŠæ ¡ã®ã«ãªãã¥ã©ã ããã移åã®å¹³åé床ãäœã«äŸåããŠãããèŠããŠããŸããïŒ ããã§ãïŒç§»åãããã¹ãšç§»åããæéããã åæããŒã¿ïŒãã¹Sãšæét ïŒãããã³å¹³åéåºŠïŒ S / t ïŒã®èšç®åŒããããã°ãæçµçµæïŒå¹³åé床ïŒãèšç®ã§ããŸãã æçµçµæãåæããŒã¿ã«äŸåãããšããåãåçã«åŸã£ãŠãé¢æ°ã¹ã¿ã€ã«ã§èšè¿°ãããããã°ã©ã ã®æçµçµæãèšç®ãããŸãã åæã«ãåœä»€åããã°ã©ãã³ã°ãšã¯ç°ãªããèšç®ã®ããã»ã¹ã§ã¯ãå€æ°ã®å€æŽã¯ãããŸãã-ããŒã«ã«ã§ãã°ããŒãã«ã§ããããŸããã
å®éãåã®æ®µèœã§ã¯ãåèªformulaã®ä»£ããã«åèªfunctionã䜿çšããæ¹ãæ£ããã§ãããã åœä»€åããã°ã©ãã³ã°èšèªã®é¢æ°ãšããèšèã¯ãæ°åŠãç©çåŠãããã³é¢æ°åããã°ã©ãã³ã°èšèªã§ãã®çšèªãæå³ãããã®ãšã¯ãŸã£ããåŒã°ããªãããšãå€ããããç§ã¯ãããããŸããã§ããã åœä»€åèšèªã§ã¯ãé¢æ°ã¯ãã°ãã°ããæ£ç¢ºã«ããã·ãŒãžã£ãšåŒã°ãããã®ãã€ãŸããããã°ã©ã ïŒãµãããã°ã©ã ïŒã®ååä»ãéšåãšåŒã°ããç¹°ãè¿ãçºçããã³ãŒãã®ç¹°ãè¿ããåé¿ããããã«äœ¿çšãããŸãã å°ãåŸã«ãé¢æ°åããã°ã©ãã³ã°èšèªã®é¢æ°ïŒããããpureé¢æ° ããŸãã¯pureé¢æ° ïŒãåœä»€åããã°ã©ãã³ã°èšèªã®é¢æ°ãšã©ã®ããã«ç°ãªãããç解ã§ããŸãã
泚ïŒããã°ã©ãã³ã°èšèªã®åœä»€åãšæ©èœåãžã®åå²ã¯ãããªãarbitraryæçã§ãã åœä»€åãšã¿ãªãããèšèªã§ã¯é¢æ°åã¹ã¿ã€ã«ã§ãé¢æ°åãšã¿ãªãããèšèªã§ã¯åœä»€åã¹ã¿ã€ã«ã§ããã°ã©ãã³ã°ã§ããŸãïŒ Haskellã®åœä»€åã¹ã¿ã€ã«ã§éä¹ãèšç®ããCã®åãããã°ã©ã ãšæ¯èŒããããã°ã©ã ã®äŸã§ã ïŒ-ããã¯åã«äžäŸ¿ã§ãã ãããã£ãŠãåœä»€åããã°ã©ãã³ã°ã奚å±ããåœä»€åèšèªãšãé¢æ°åããã°ã©ãã³ã°ã奚å±ããèšèªãšããŠæ©èœåèšèªãèããŠã¿ãŸãããã
çŽç²ãªæ©èœã®å©ç¹ãçºèŠãã
Haskellããã°ã©ããŒãããããçŽç²ãªé¢æ°ãæ±ãæéã®å€§éšåïŒãã¡ããããã¹ãŠã¯ããã°ã©ããŒã«äŸåããŸãããããã§ã¯ã©ã®ããã«ãã¹ããã«ã€ããŠè©±ããŠããŸãïŒã å®éããããã®é¢æ°ã¯ãçŽç²ããšåŒã°ãããããåœä»€åããã°ã©ãã³ã°ã§ãé¢æ°ããšããçšèªãæå³ãããã®ãšæ··åãããŸããã å®éããããã¯çšèªã®æ°åŠçãªç解ã«ãããŠæãäžè¬çãªæ©èœã§ãã 以äžã«ã3ã€ã®æ°åãè¿œå ãããã®ãããªé¢æ°ã®æãç°¡åãªäŸã瀺ããŸãã
addThreeNumbers xyz = x + y + z
Haskellæ§æã«äžæ
£ããªäººã®ããã®èª¬æ
=èšå·ã®å·ŠåŽã®é¢æ°ã®éšåã§ã¯ãé¢æ°ã®ååãåžžã«æåã«æ¥ãŠã次ã«ã¹ããŒã¹ã§åºåãããŠããã®é¢æ°ã®åŒæ°ãè¡ããŸãã ãã®å Žåãé¢æ°åã¯addThreeNumbersã§ããã x ã yãããã³zã¯ãã®åŒæ°ã§ãã
=èšå·ã®å³åŽã«ã¯ãåŒæ°ã®èŠ³ç¹ãããé¢æ°ã®çµæãã©ã®ããã«èšç®ããããã瀺ãããŠããŸãã
=èšå·ã®å³åŽã«ã¯ãåŒæ°ã®èŠ³ç¹ãããé¢æ°ã®çµæãã©ã®ããã«èšç®ããããã瀺ãããŠããŸãã
=èšå·ïŒ çå· ïŒã«æ³šæããŠãã ããã åœä»€åããã°ã©ãã³ã°ãšã¯ç°ãªããå²ãåœãŠæäœãæå³ãããã®ã§ã¯ãããŸããã çå·ã¯ã圌ã®å·Šã«ãããã®ã圌ã®å³ã«ããè¡šçŸãšåãã§ããããšãæå³ããŸãã æ°åŠã®ããã«ïŒ 6 + 4ã¯10 ãšåããªã®ã§ã 6 + 4 = 10ãšæžããŸãã ã©ã®èšç®ã§ãã10ã®ä»£ããã«åŒïŒ6 + 4ïŒã眮ãæããããšãã§ãã10ã眮ãæããå ŽåãšåãçµæãåŸãããŸãã Haskellã®åãããšïŒ
addThreeNumbers xyz
代ããã«ãåŒ
x + y + z
眮ãæããããšãã§ããåãçµæãåŸãããŸãã ãšããã§ãã³ã³ãã€ã©ã¯ãŸãã«ãããè¡ããŸã-é¢æ°åã«ééãããšã代ããã«ãã®æ¬äœã§å®çŸ©ãããåŒã眮ãæããŸãã
ãã®æ©èœã®ãçŽåºŠããšã¯äœã§ããïŒ
é¢æ°ã®çµæã¯ãåŒæ°ã®ã¿ã«äŸåããŸãã åãåŒæ°ã§ãã®é¢æ°ãäœåºŠåŒã³åºããŠããé¢æ°ã¯å€éšç¶æ ãåç §ããªããããåžžã«åãçµæãè¿ããŸãã 圌女ã¯å€ã®äžçããå®å šã«éé¢ãããŠãããèšç®ã§ã¯ãç§ãã¡ã圌女ã®è°è«ãšããŠæ瀺çã«äŒãããã®ã ããèæ ®ããŠããŸãã æŽå²ãªã©ã®ç§åŠãšã¯ç°ãªããæ°åŠçèšç®ã®çµæã¯ãå ±ç£å ãæš©åãæ¡ã£ãŠããããæ°äž»å å¡ããããŒãã³å€§çµ±é ãã«ãã£ãŠå·Šå³ãããŸããã ç§ãã¡ã®é¢æ°ã¯æ°åŠã«ç±æ¥ããŸã-ããã¯æž¡ãããåŒæ°ã«ã®ã¿äŸåãããã以äžã«ã¯äŸåããŸããã
èªåã§ç¢ºèªã§ããŸãããã®é¢æ°ã®åŒæ°ãšããŠå€1ã2ã4ãäœåæž¡ããŠããåžžã«7ã«ãªããŸããã3ãã®ä»£ããã«ãïŒ2 + 1ïŒããæž¡ãããšãã§ããŸãã ã4ã-ãïŒ2 * 2ïŒãã ãããã®åŒæ°ã§å¥ã®çµæãååŸãããªãã·ã§ã³ã¯ãããŸããã
addThreeNumbers
é¢æ°ã¯ãå€éšç¶æ ã«äŸåããªãã ãã§ãªããå€æŽãã§ããªããããçŽç²ãšaddThreeNumbers
åŒã°ããŸãã 圌女ã¯åŒæ°ãšããŠæž¡ãããããŒã«ã«å€æ°ãå€æŽããããšããã§ããŸããã 圌女ãã§ããïŒãããŠãã¹ãïŒããšã¯ã圌女ã«æž¡ãããåŒæ°ã®å€ã«åºã¥ããŠçµæãèšç®ããããšã§ãã ã€ãŸãããã®æ©èœã«ã¯å¯äœçšã¯ãããŸããã
ããã«ããäœãåŸãããŸããïŒ ãªãHaskellistsã¯ãããŒã«ã«ããã³ã°ããŒãã«å€æ°ã®å€ç°ã«åºã¥ããŠæ§ç¯ãããåœä»€åããã°ã©ãã³ã°èšèªã®äŒçµ±çãªæ©èœãèŠãŠããã®ãããªè»œpurçãªæ¹æ³ã§ãã®æ©èœã®ãçŽåºŠããä¿æããã®ã§ããããã
çŽç²ãªé¢æ°ã®èšç®çµæã¯å€éšç¶æ ã«ãŸã£ããäŸåãããå€éšç¶æ ãå€æŽããªããããå ±éã®ãªãœãŒã¹ãããã£ãŠç«¶åããããŒã¿ã®ç«¶åãå¿é ããããšãªãããããã®é¢æ°ã䞊åã«èšç®ã§ããŸãã å¯äœçšã¯äžŠåèšç®ã®æ»ã§ãããç§ãã¡ã®çŽç²ãªé¢æ°ã«ã¯ãããããªããããå¿é ããå¿ èŠã¯ãããŸããã é¢æ°ãèšç®ããé åºããèšç®ã䞊ååããæ¹æ³ãæ°ã«ããã«ãçŽç²ãªé¢æ°ãèšè¿°ããã ãã§ãã Haskellã§èšè¿°ããŠãããããããããã«äžŠååã§ããŸãã
ããã«ãçŽç²ãªé¢æ°ãåãåŒæ°ã§è€æ°ååŒã³åºããããåžžã«åãçµæãåŸãããããšãä¿èšŒãããŠãããããHaskellã¯äžåºŠèšç®ãããçµæãèšæ¶ããåãåŒæ°ã§é¢æ°ãå床åŒã³åºããããšãå床è©äŸ¡ããã«ä»¥åã«èšç®ãããçµæã眮ãæããŸãã ããã¯ã¡ã¢åãšåŒã°ããŸãã ããã¯éåžžã«åŒ·åãªæé©åããŒã«ã§ãã çµæãåžžã«åãã§ããããšãããã£ãŠããã®ã«ããªãåã³ã«ãŠã³ãããã®ã§ããããïŒ
åœä»€åããã°ã©ãã³ã°ã®æ¬è³ªãå³å¯ã«å®çŸ©ãããã·ãŒã±ã³ã¹å ã®å€æ°ã®çªç¶å€ç°ïŒå€æŽïŒã«ããå Žåãé¢æ°åããã°ã©ãã³ã°ã®æ¬è³ªã¯ããŒã¿ã®äžå€æ§ãšé¢æ°ã®æ§æã«ãããŸãã
é¢æ°
g :: a -> b
ïŒãåaã®åŒæ°ãåããåbã®å€ãè¿ãé¢æ°gããšèªãïŒãšé¢æ°
f :: b -> c
ãããå Žåãããããåæããããšã§é¢æ°
h :: a -> c
ååŸã§ããŸã
h :: a -> c
ã ã¿ã€ãaã®å€ãé¢æ°gã®å ¥åã«äŸçµŠããããšã«ãããåºåã§ã¿ã€ãbã®å€ãååŸããŸã-é¢æ°fã¯ãŸãã«ãã®ã¿ã€ãã®å ¥åãåãåããŸãã ãããã£ãŠãé¢æ°gã®èšç®çµæãé¢æ°fã«ããã«è»¢éã§ããŸãããã®çµæã¯ãã¿ã€ãcã®å€ã«ãªããŸãã 次ã®ããã«æžãããŠããŸãã
h :: a -> c h = f . g
é¢æ°fãšgã®éã®ãã€ã³ãã¯ã次ã®ã¿ã€ãã®åææŒç®åã§ãã
(.) :: (b -> c) -> (a -> b) -> (a -> c)
åææŒç®åã¯ãéåžžã®é¢æ°ãšåãæ¹æ³ïŒæ¬åŒ§å ïŒã§æ¬åŒ§å ã§äœ¿çšããããããããã§ã¯æ¬åŒ§ã§å²ãŸããŠããŸãã 2ã€ã®åŒæ°ã®éã§äžçœ®ã¹ã¿ã€ã«ã§äœ¿çšããå Žåãæ¬åŒ§ãªãã§äœ¿çšãããŸãã
åææŒç®åã¯ãé¢æ°fã«å¯Ÿå¿ããæåã®åŒæ°ãšããŠé¢æ°
b -> c
ãåããŸãïŒç¢å°ã¯å-é¢æ°ã®åã瀺ããŸãïŒã 2çªç®ã®åŒæ°ã圌ã¯é¢æ°ãåãåããŸã-ãã ããã¿ã€ã
a -> b
ãããã¯é¢æ°gã«å¯Ÿå¿ããŸãã ãããŠãæ§ææŒç®åã¯ãé¢æ°
h :: a -> c
a -> c
ã«å¯Ÿå¿ããa-
a -> c
åã®æ°ããé¢æ°ãusã«è¿ããŸãã æ©èœç¢å°ã«ã¯å³çµåæ§ããããããæåŸã®æ¬åŒ§ãçç¥ã§ããŸãã
(.) :: (b -> c) -> (a -> b) -> a -> c
ããã§ãæ§ææŒç®åã¯ã
b -> c
ããã³
b -> c
a -> b
åã®2ã€ã®é¢æ°ãšã2çªç®ã®é¢æ°ã®å ¥åã«è»¢éãããaåã®åŒæ°ãæž¡ãå¿ èŠãããããšãããããŸããåºåã§ã¯ã
c
åã®å€ãååŸããŸããæ©èœã
æ§ææŒç®åããããã§ç€ºãããçç±
æ°åŠã§ã¯ã
ãšããè¡šèšã¯ãé¢æ°ã®æ§æã瀺ãããã«äœ¿çšãããŸããããã¯ããf after gããæå³ããŸãã ãã€ã³ãã¯ãã®ã·ã³ãã«ã«äŒŒãŠãããããæ§ææŒç®åãšããŠéžæãããŸããã
f â g
ãšããè¡šèšã¯ãé¢æ°ã®æ§æã瀺ãããã«äœ¿çšãããŸããããã¯ããf after gããæå³ããŸãã ãã€ã³ãã¯ãã®ã·ã³ãã«ã«äŒŒãŠãããããæ§ææŒç®åãšããŠéžæãããŸããã
æ©èœã®æ§æ
f . g
f . g
ã¯
f (gx)
ãšåãæå³-ã€ãŸã é¢æ°
g
ãåŒæ°
x
é©çšããçµæã«é©çšãããé¢æ°
f
ã¡ãã£ãšåŸ
ã£ãŠïŒ ãããŠãé¢æ°h = fã®å®çŸ©ã§å€±ãããã¿ã€ãaã®åŒæ°ã¯ã©ãã«ãããŸãããã gïŒ åææŒç®åãžã®åŒæ°ãšããŠ2ã€ã®é¢æ°ã衚瀺ãããŸãããgé¢æ°ãžã®å
¥åã«æž¡ãããå€ã¯è¡šç€ºãããŸããïŒ
åãåŒæ°ãé¢æ°å®çŸ©ã®ã=ãèšå·ã®å·Šãšå³ã®æåŸã®å Žæã«ããããã®åŒæ°ãä»ã®ã©ãã§ã䜿çšãããŠããªãå Žåãçç¥ã§ããŸãïŒãã ããåžžã«äž¡åŽããïŒã æ°åŠã§ã¯ãåŒæ°ã¯ãé¢æ°ã®é©çšãã€ã³ãããšåŒã°ããããããã®èšè¿°ã¹ã¿ã€ã«ã¯ãç¡æå³ããšåŒã°ããŸãïŒéåžžãåææŒç®åã®ãããªãã€ã³ãã®èšé²ã§ã¯ãå€æ°ãããŸã:)ïŒã
é¢æ°ã®åæãé¢æ°åããã°ã©ãã³ã°èšèªã®æ¬è³ªã§ããã®ã¯ãªãã§ããïŒ ã¯ããé¢æ°åèšèªã§æžãããããã°ã©ã ã¯é¢æ°ã®åæã«éããªãããã§ãïŒ é¢æ°ã¯ãããã°ã©ã ã®æ§æèŠçŽ ã§ãã ããããæ§æãããšãä»ã®é¢æ°ãååŸããç¬èªã®æ¹æ³ã§ãæ°ããé¢æ°ãååŸããããã«æ§æããŸã-ãªã©ã ããŒã¿ã¯ããé¢æ°ããå¥ã®é¢æ°ã«æµããå€æããé¢æ°ãæ§æããããã®å¯äžã®æ¡ä»¶ã¯ãããé¢æ°ã«ãã£ãŠè¿ãããããŒã¿ã次ã®é¢æ°ãåããã®ãšåãåãæã€ããšã§ãã
Haskellã®é¢æ°ã¯ã¯ãªãŒã³ã§ãããæ瀺çã«æž¡ãããåŒæ°ã«ã®ã¿äŸåããŠãããããé¢æ°æ§æãã§ãŒã³ããããçš®ã®ãããªãã¯ããç°¡åã«ãåŒãåºãããŠããªãã¡ã¯ã¿ãªã³ã°ãŸãã¯å®å šã«çœ®ãæããããšããã§ããŸãã 泚æããå¿ èŠãããã®ã¯ãæ°ããããªãã¯é¢æ°ãå€ãããªãã¯é¢æ°ãšåãåã®å ¥åå€ãšåºåå€ã§åãå ¥ããããšã ãã§ãã ããã ãã§ãïŒ çŽç²ãªé¢æ°ã¯å€éšç¶æ ã«äŸåããªããããé¢æ°ã«é¢ä¿ãªãé¢æ°ããã¹ãã§ããŸãã ããã°ã©ã å šäœããã¹ããã代ããã«ãåã ã®æ©èœããã¹ãããŸãã ç§ãã¡ã®å Žåããã®éåžžã«éèŠãªè©±ã§èª¬æãããŠããç¶æ³ã¯ãåã«äžå¯èœã«ãªããŸãã
ããŒã±ãã£ã³ã°æ åœè ã¯ããã°ã©ããŒã«æ¬¡ã®ããšãå°ããŸãã
-倧èŠæš¡ãããžã§ã¯ãããµããŒãããé£ããã¯äœã§ããïŒ
ããŸããããªããäœå®¶ã§ãããæŠäºãšå¹³åãããžã§ã¯ãããµããŒãããŠãããšæ³åããŠãã ããããšããã°ã©ããŒã¯çããŸãã -ããªãã¯TKãæã£ãŠããŸã-ãã¿ãŒã·ã£ã»ãã¹ãã¯ãéšã®äžã§å ¬åãæ©ããæ¹æ³ã«ã€ããŠã®ç« ãæžããŸãã ãéšãéã£ãŠããããšæžããŠä¿åãããšãããã¿ãŒã·ã£ã»ãã¹ããŽã¡ã亡ããªããŸãããç¶è¡ã§ããŸããããšãããšã©ãŒã¡ãã»ãŒãžã衚瀺ãããŸãã ã©ãããŠæ»ãã ã®ïŒã©ãããŠæ»ãã ã®ïŒ ããªãã¯ç解ãå§ããŸãã ããšãŒã«ã»ããºããã®ã€ãã€ãããéŽã圌ãèœã¡ãéãå°é¢ã«ã¶ã€ããããã¹ãããã®åŒŸäžžããã¿ãŒã·ã£ã«è·³ãè¿ã£ãããšãããããŸããã ã©ããã éãã¢ã€ãã«ç¶æ ã«å é»ããŸããïŒ éŽã亀æããŸããïŒ æ±ãåãå€ãããšã«ããŸããã åé€ããŠä¿åãããRzhevskyäžeuãæ»äº¡ããŸãããããšããã¡ãã»ãŒãžãåãåããŸãã ããäžåºŠãããªãã¯åº§ã£ãŠç解ãã次ã®ç« ã§åœŒã¯ãã¯ãããŒã«ã«åŸããŠããããšãããããŸã...
HaskellistsãçŽç²ãªæ©èœãããã»ã©éèŠããŠããçç±ãç解ããŠãã ããã 第äžã«ãããŒã¿ç«¶åãå¿é ããããšãªããåªåãªãã§äžŠååãããã³ãŒããæžãããšãã§ããŸãã 次ã«ãã³ã³ãã€ã©ãŒãèšç®ãå¹ççã«æé©åã§ããããã«ããŸãã ãããŠç¬¬äžã«ãå¯äœçšããªããçŽç²ãªé¢æ°ãå€éšç¶æ ããç¬ç«ããŠãããããããã°ã©ãã¯éåžžã«å€§ããªãããžã§ã¯ãã§ãç°¡åã«ãµããŒãããã¹ãããªãã¡ã¯ã¿ãªã³ã°ã§ããŸãã
èšãæããã°ãHaskellèšèªã®äœæè ã¯ãäžçã®å€éšç¶æ ããå®å šã«éé¢ãããäžçãã€ãŸããã¹ãŠã®æ©èœããããã§ãç¶æ ããªãããã¹ãŠãäžå¯èœã«æé©åãããŠããããã¹ãŠãåªåãªãã§äžŠååãããäžçã®å€éšç¶æ ããå®å šã«éé¢ãããäžçãæãä»ããŸããåŽã èšèªã§ã¯ãªããå€¢ïŒ Eugenio Moggi ãã¢ããã®æŠå¿µã«é¢ããç§åŠçç 究ã§ãªã¹ããããäºçŽ°ãªäºããã©ãããããç解ããããšã ããæ®ã£ãŠããŸãã
ç空ã®ãã®éåžžã«ç圢ã®éŠ¬ã§ãç§ãã¡ãéé¢ãããŠããå€ã®äžçããã ãæ¥ãããã°ã©ã ã®åæããŒã¿ãååŸããã«ã¯ã©ãããã°ããã®ã§ããããïŒ ãã¡ããããŠãŒã¶ãŒå ¥åã®çµæãçŽç²ãªé¢æ°ïŒããšãã°ãããŒããŒãããã®æåå ¥åãåãä»ããgetChar
é¢æ°ïŒã®åŒæ°ãšããŠäœ¿çšã§ããŸããããŸãããã®æ¹æ³ã§ãå± å¿å°ã®è¯ãã¯ãªãŒã³ãªäžçã«å¿ èŠãªãããŒãã£ãé¢æ°ãäœæããŸããããã¯ããã§äžæãã次ã«ããã®ãããªé¢æ°ã¯åžžã«åãåŒæ°ïŒgetChar
é¢æ°ïŒãæã¡ãŸããããŠãŒã¶ãŒïŒããã§ã¯åŸ ã¡äŒãïŒïŒãåžžã«ç°ãªãããŒãæŒããããèšç®å€ã¯åžžã«ç°ãªããŸãã
ããã°ã©ã ã®çµæã§ãããå± å¿å°ã®è¯ããçŽç²ã«æ©èœçãªäžçããéé¢ããããå€ã®äžçã«çµæãäžããæ¹æ³ã¯ïŒ çµå±ãæ°åŠçãªæå³ã§ã®é¢æ°ã¯åžžã«çµæãè¿ãå¿ èŠããããäžéšã®ããŒã¿ãå€éšã«éä¿¡ããé¢æ°ã¯äœãè¿ããªããããé¢æ°ã§ã¯ãããŸããïŒ
ããããéšåçã«å®çŸ©ãããé¢æ°ãã€ãŸãããã¹ãŠã®åŒæ°ã«å¯ŸããŠå®çŸ©ãããŠããªãé¢æ°ãã©ãããŸããïŒ ããšãã°ãããç¥ãããŠããé€ç®é¢æ°ã¯ãŒãã«ããé€ç®ã«å¯ŸããŠå®çŸ©ãããŠããŸããã ãã®ãããªé¢æ°ã¯ãçšèªã®æ°åŠçãªæå³ã§ã®æ¬æ Œçãªé¢æ°ã§ããããŸããã ãã¡ããããã®ãããªåŒæ°ã«å¯ŸããŠäŸå€ãã¹ããŒã§ããŸããã...
...ããããäŸå€ã¯ã©ãããŸããïŒ äŸå€ã¯ãçŽç²ãªé¢æ°ããæåŸ ãããçµæã§ã¯ãããŸããïŒ
é決å®çã³ã³ãã¥ãŒãã£ã³ã°ãã©ããããïŒ ã€ãŸããæ£ããèšç®çµæã1ã€ã§ã¯ãªããå€ãã®å Žåã§ãã ããšãã°ãåèªã®ç¿»èš³ãååŸãããå Žåãããã°ã©ã ã¯ãã®æå³ã®ããã€ããäžåºŠã«ç€ºãããããããæ£ããçµæã«ãªããŸãã çŽç²ãªé¢æ°ã¯åžžã«1ã€ã®çµæã®ã¿ãè¿ãå¿ èŠããããŸãã
ãããŠãç¶ç·šãã©ããããïŒ ç¶ç¶ãšã¯ãããã€ãã®èšç®ãå®è¡ããåŸãããããå®äºããã®ãåŸ ããã«çŸåšã®ç¶æ ãä¿åããä»ã®ã¿ã¹ã¯ã«åãæ¿ããããšã§ãããã®ãããå®äºåŸã«äžå®å šãªèšç®ã«æ»ããäžæãããšããããç¶è¡ããŸãã ç¶æ ããªããããããªãçŽç²ã«æ©èœçãªäžçã§ã¯ãã©ã®ãããªç¶æ ã«ã€ããŠè©±ããŠããã®ã§ããããïŒ
ãããŠãæåŸã«ãã©ããããããå€éšç¶æ ãèæ ®ããã ãã§ãªããäœããã®æ¹æ³ã§ãããå€æŽããå¿ èŠãããå Žåãäœããã¹ãã§ããããïŒ
èšç®ãã¯ãªãŒã³ã«ä¿ã¡ãè¡šæãããåé¡ã解決ããæ¹æ³ãäžç·ã«èããŸãããã ãããŠãããããã¹ãŠã®åé¡ã«å¯ŸããŠå ±éã®è§£æ±ºçãèŠã€ãããã©ãããèŠãŠã¿ãŸãããã
èšç®ãšããã®ä»ã
ãã®ãããçŽç²ãªæ©èœã«ç²Ÿéãããã®çŽç²ããããã°ã©ããçŽé¢ããæãè€éãªåé¡ãåãé€ãããšãã§ããããšã«æ°ä»ããŸããã ããããçŽç²ãªæ©èœã掻çšããèœåãç¶æããããã«è§£æ±ºããªããã°ãªããªãå€ãã®åé¡ã«ã€ããŠã説æããŸããã ç§ã¯ããããåã³äžããŸãïŒI / Oã«é¢é£ããåé¡ãåãé€ããŸãããããã«ã€ããŠã¯åŸã§èª¬æããŸãïŒããããã®äžè¬çãªãã¿ãŒã³ãèŠãããšãã§ããããã«ããããããããåç·šæããŸãïŒ
ãã¹ãŠã®åŒæ°ã«å¯ŸããŠå®çŸ©ãããŠããªãé¢æ°ãããå ŽåããããŸãã é¢æ°ãå®çŸ©ãããŠãããã®é¢æ°ã«åŒæ°ãæž¡ããšããçµæãèšç®ããå¿ èŠããããŸãã ããããå®çŸ©ãããŠããªãåŒæ°ãæž¡ãå Žåãé¢æ°ãäœãä»ã®ãã® ïŒäŸå€ããšã©ãŒã¡ãã»ãŒãžããŸãã¯åœä»€ånull
é¡äŒŒç©ïŒãè¿ãããã«ããŸã ã
é¢æ°ãçµæã1ã€ã§ã¯ãªããä»ã®äœã ïŒããšãã°ãçµæã®ãªã¹ãå šäœããŸãã¯ãŸã£ããçµæãªãïŒçµæã®ç©ºã®ãªã¹ãïŒïŒãäžããããšããããŸãã
é¢æ°ã®å€ãèšç®ããããã«ãåŒæ°ã ãã§ãªãã äœãä»ã®ãã® ïŒããšãã°ãå€éšç°å¢ããã®ããŒã¿ãæ§æãã¡ã€ã«ããã®èšå®ãªã©ïŒãååŸãããå ŽåããããŸãã
次ã®é¢æ°ãæž¡ãããã«èšç®ã®çµæãååŸããã ãã§ãªãã ãããä»ã®äœãã«åŒæ°ãšããŠé©çšãããå ŽåããããŸãïŒäœããã®ç¶æ ãååŸããåŸãæ»ã£ãŠèšç®ãç¶è¡ããããšãã§ããŸããããã¯ç¶ç¶ã®æå³ã§ãïŒã
èšç®ãå®è¡ããã ãã§ãªãã äœãä»ã®ããšãå®è¡ãããå ŽåããããŸã ïŒããšãã°ããã°ã«äœããæžã蟌ãïŒã
é¢æ°ãäœæãããšãã«ãèšç®ã®çµæã ãã§ãªãã ä»ã®äœã ïŒããšãã°ãæåã«ã©ããããèªã¿åãã次ã«äœããã®æ¹æ³ã§å¶åŸ¡ãããæ¹æ³ã§å€æŽãããªã©ïŒã次ã®é¢æ°ã«æž¡ãããå ŽåããããŸã ã
äžè¬çãªãã¿ãŒã³ã«æ°ã¥ããŸãããïŒ æ¬äŒŒã³ãŒãã§ã¯ã次ã®ããã«èšè¿°ã§ããŸãã
( / - ) { // / // - return ( / - ) }
ãã¡ããããã®ãä»ã®äœãããé¢æ°ã®è¿œå åŒæ°ãšããŠæž¡ãããšãã§ããŸãïŒãã®ã¢ãããŒãã¯åœä»€åããã°ã©ãã³ã°ã§äœ¿çšããããã¹ã¬ããç¶æ ããšåŒã°ããŸãïŒããçŽç²ãªèšç®ãšãä»ã®äœããäžåºŠã«ç©ã¿éããããšã¯æè¯ã®ã¢ã€ãã¢ã§ã¯ãããŸããã ãŸããããã«ããã説æãããã¹ãŠã®ç¶æ³ã«å¯ŸããŠåäžã®ãœãªã¥ãŒã·ã§ã³ãååŸããããšã¯ã§ããŸããã
åãã«è°è«ãããæ°åãçºæããå€ä»£ãšãžãã人ãæãåºããŸãããã å€ãã®çŸã®å³ãæã代ããã«ã圌ãã¯èšç®ããã®æèããåé¢ããŸãã ã çŸä»£çã«ã¯ãã³ã³ãã¥ãŒãã£ã³ã°ãšãã®ã³ã³ããã¹ããã«ãã»ã«åããŸããã ãããŠããããã®åã«éãèšç®ããæŠå¿µãç§ãã¡ãèããŠããããšãšå¯æ¥ã«é¢é£ããŠããå Žåã圌ãã®é©æ°ã¯ããã2ã€ã®äžŠè¡ãããå®è¡ãããŒãã«åå²ããŸãã-èšç®ã«çŽæ¥æ¥ç¶ãããã¹ããªãŒã ãšãã㯠-ã€ãŸããèšç®ã®ã³ã³ããã¹ãã§ãïŒèšç®äžã«ã³ã³ããã¹ããä¿åã§ããã ãã§ãªããããšãã°çŸ€ãã®çŸããäœå¹ã®ã±ãããåŸãããããèšç®ãããšãã³ã³ããã¹ããå€æŽãããããã§ãïŒã
Haskellã§ãä»ã®äœãããè¡šçŸããåæã«æãäžè¬åããããœãªã¥ãŒã·ã§ã³ãååŸãããå Žåããã®ãä»ã®äœãããè¿œå ã®ã¿ã€ããšããŠè¡šçŸããŸãã ãã ããåçŽåã§ã¯ãªããä»ã®åãåŒæ°ãšããŠåãé¢æ°åã§ãã è€éã§åããã«ããã§ããã å¿é ããªãã§ãã ãããããã¯éåžžã«ç°¡åã§ãæ°ååŸã«ããªãèªèº«ã§èŠãã§ãããã
ãä»ã®äœããã®ã«ãã»ã«å
1998幎12æ11æ¥ãç«æãç 究ããããã«ç«ææ°åãªãŒãã¿ãŒå®å®è¹ãæã¡äžããããŸããã ããã€ã¹ãç«æã«å°éããåŸãããã¯å€±ãããŸããã 調æ»åŸãå¶åŸ¡ããã°ã©ã ã§ã¯ãäžéšã®è·é¢ãã€ã³ãåäœã§èæ ®ãããä»ã®è·é¢ã¯ã¡ãŒãã«åäœã§èæ ®ãããããšãå€æããŸããã ã©ã¡ãã®å Žåãããããã®å€ã¯
Double
åã§è¡šãããŠããŸããã é¢æ°ãã€ã³ãåäœã§ã«ãŠã³ãããçµæãã¡ãŒãã«åäœã§è¡šãããåŒæ°ãæž¡ããããããåœç¶ãèšç®ã§ãšã©ãŒãçºçããŸããã
ãã®ãããªãšã©ãŒãåé¿ãããå Žåã¯ãã¡ãŒãã«ã§è¡šãããå€ãã€ã³ãã§è¡šââãããå€ãšç°ãªãå¿ èŠããããŸãããããã£ãŠãééã£ãåäœã§è¡šãããå€ãé¢æ°ã«æž¡ãããšãããšãã³ã³ãã€ã©ãŒããšã©ãŒãéç¥ããŸãã Haskellã§ã¯ãããã¯éåžžã«ç°¡åã§ãã 2ã€ã®æ°ããåã宣èšããŸãããã
data DistanceInMeters = Meter Double data DistanceInInches = Inch Double
DistanceInMeters
ãš
DistanceInInches
ã¯ã¿ã€ãã³ã³ã¹ãã©ã¯ã¿ãŒãšåŒã°ãã
Meter
ãš
Inch
ã¯ããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒãšåŒã°ããŸãïŒã¿ã€ãã³ã³ã¹ãã©ã¯ã¿ãŒãšããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒã¯ç°ãªãã¹ã³ãŒãã«ååšãããããåãããã«ããããšãã§ããŸãïŒã
ãããã®å宣èšãèŠãŠãã ããã ããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒã¯é¢æ°ã®ããã«åäœããåŒæ°ãšããŠ
Double
åã®å€ãåããèšç®ã®çµæãšããŠ
DistanceInMeters
åãŸãã¯
DistanceInInches
åã®å€ãè¿ããšæããŸãããïŒ ããã§ã-ããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒãé¢æ°ã§ãïŒ ãããŠã以åã«èª€ã£ãŠ
Double
åã®å€ã
Double
ãåãé¢æ°ã«æž¡ãããšãã§ããå Žåããã®é¢æ°ã§ã¯ãåŒæ°ã«
Double
åã®å€ã ãã§ãªãã äœãä»ã®ãã® ãã€ãŸã- «»
Meter
Inch
.
ãã ãããã®å Žåãæãäžè¬çãªãœãªã¥ãŒã·ã§ã³ã¯åŸãããŸããã§ãããåœç€Ÿã®æ©èœkonstruktory_dannyhã®åŒæ°
Meter
ãš
Inch
ã®åã®å€ã®ã¿ãåãããšãã§ããŸã
Double
ãããã¯ããã®ç¹å®ã®ã¿ã¹ã¯ã®ããžãã¯ã«ãã£ãŠæ±ºå®ãããŸãããã¡ã€ã³ã¿ã¹ã¯ã解決ããããã«-çŽç²ãªã³ã³ãã¥ãŒãã£ã³ã°ããä»ã®äœããããåé¢ããŸã-ãã®ãä»ã®äœãããè¡šçŸãããã©ãããŒãã圌ãã®åŒæ°ãåãããšãã§ããå¿ èŠããããŸãã¿ã€ãããŸãããã®ã¿ã¹ã¯ã¯Haskellã§éåžžã«ç°¡åã«è§£æ±ºã§ããŸããHaskellçµã¿èŸŒã¿ã¿ã€ãã®1ã€ãèŠãŠãã ããã
data Maybe a = Nothing | Just a
ããã«æžãããŠããããšãç解ããŠããªã人ã®ããã®èª¬æ
,
,
. « » , â
,
,
, . ,
2 â
(
). «»:
,
- (,
) â
(
,
).
Maybe
,
a
. « » , â
Double
,
Bool
,
DistanceInMeters
, . ,
Maybe a
2 â
Nothing
Just
(
a
). «»:
Nothing
,
Just
- (,
Just True
) â
Maybe a
(
Just
True
,
Maybe Bool
).
èŠãŠã
Maybe
ã©ããªã¿ã€ãã®å€ã§ãåãããšãã§ããã©ãããŒããããŸãããã®ã©ãããŒã«ã¯ãäœããã®å€ãå«ããããšãã§ããŸãïŒããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒã䜿çšããå Žå
Just
ïŒããäœãå«ããããšã¯ã§ããŸããïŒããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒã䜿çšããå Žå
Nothing
ïŒãã©ãããŒå ã«ããŒã¿ããããã©ããã確èªããã«ã¯ãã©ãããŒ
Maybe
ãæ€æ»ããã ãã§ãããããç®±ã®ãããªãã®ã§ããç®±ã空ãã©ããã確èªããããã«ãç®±ãéããå¿ èŠã¯ãããŸãããç®±ãè³ã«æã£ãŠããŠæ¯ãã ãã§ããHaskellã¯ã
å
Maybe
ã䜿çšããŠåé¡ã®1ã€ã解決ããŸãããã¹ãŠã®åŒæ°ã«å¯ŸããŠå®çŸ©ãããŠããªãçŽç²ãªé¢æ°ãã©ãåŠçãããã§ããããšãã°ãé¢æ°ããããŸã
lookup
ãããŒãšãã¢ã®é£æ³ãªã¹ãïŒããŒãå€ïŒãæž¡ããŠããã®ããŒã«é¢é£ä»ããããå€ãèŠã€ããããšãã§ããŸãããã ãããã®é¢æ°ã¯ãæž¡ãããããŒãšã®ãã¢ãèŠã€ããããªãå ŽåããããŸãããã®å Žåãããã¯ç§ãã¡
Nothing
ã«è¿ããããããèŠã€ãã£ãå Žåãã«ã©ãããããå€ãç§ãã¡ã«è¿ã
Just
ãŸããã€ãŸãé¢æ°ã«å®çŸ©ãããå€ãæž¡ããšãèšç®ã®çµæãïŒã©ãããŒã§
Just
ïŒååŸããå®çŸ©ãããŠããªãå€ãæž¡ããšããäœãä»ã®ãã®ãïŒ
Nothing
ïŒãååŸããŸãã
ããã
Nothing
ãã ãã§ãªããé¢æ°ãèšç®ã®çµæã§ã¯ãªããä»ã®äœãããè¿ããçç±ã«é¢ããã¡ãã»ãŒãžãååŸãããå Žåã¯ã©ãã§ãããããåé¡ãããæ確ã«å®çŸ©ããŸãããïŒèšç®ãæåããå Žåãçµæãè¿ãããå Žåãããã³å€±æããå Žåã¯ããšã©ãŒã¡ãã»ãŒãžãèšç®ã®çµæããšã©ãŒã¡ãã»ãŒãžã¯ããŸããŸãªã¿ã€ãã«ãªããŸãã OKããã®ããã«æžããŸãããïŒ
data Either ab = Left a | Right b
åã³ã³ã¹ãã©ã¯ã¿
Either
ã¯ãåã®2ã€ã®å€æ°ãåãå ¥ããããšãããããŸã-
a
ããã³
b
ïŒç°ãªãåã§ããåãåã§ãããŸããŸããïŒãèšç®ã®çµæãæåããå Žåãããããã©ãããŒã§ååŸã
Right
ïŒèšç®ã®çµæã¯typeã«ãªããŸã
b
ïŒãèšç®ã倱æããå ŽåãããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒã®ã©ãããŒã§aã®ãšã©ãŒãååŸããŸã
Left
ã
ããŠãå€éšç°å¢ã§ã®äœæ¥ã¯ã©ãã§ããïŒèšç®ã®å€ãäœããã®å€éšç°å¢ã«äŸåããŠããå Žåã¯ãå¿ èŠãªå€ãèšç®ããé¢æ°ã«èªã¿èŸŒãã§åŒæ°ãšããŠæž¡ãå¿ èŠããããŸããïŒè¿°ã¹ãããã«ããããŠæžããŠãã ããïŒ
data Reader ea = Reader (e -> a)
èšç®çµæãäŸåããç°å¢ã¯åå€æ°ã§ç€ºãã
e
ïŒåå€æ°ã®ä»£ããã«å¿ èŠãªåã«çœ®ãæããããšãã§ããããšãæãåºããŠãã ããïŒãèšç®çµæã®åã¯åå€æ°ã§ç€ºãããŸã
a
ãããã«ãèšç®èªäœã«ã¯typeã
e -> a
ãããŸããç°å¢ããå¿ èŠãªäŸ¡å€ãžã®æ©èœã§ãã
åãããšããåäžã®çµæãŸãã¯ä»ã®äœãïŒãŒãã®çµæãŸãã¯å€ãã®çµæïŒãè¿ãå¯èœæ§ã®ããé決å®çèšç®ã«ãåœãŠã¯ãŸããŸããããããè¿œå ã®åã§ã©ããããŸãããããŠãããªãã¯ããããç¥ã£ãŠãããã®ã¿ã€ã-ãªã¹ãã®ãã®ã¿ã€ã
[a]
ïŒã®ããã«æžãããšãã§ããŸãããã®ãä»ã®äœãããè¡šããå€æ°ã®å-åœç€Ÿã®çŽèšç®ã®ã¿ã€ãïŒã
[] a
[]
a
çŽç²ãªèšç®ã®å®è¡ãšäžŠè¡ããŠå€æŽããå¿ èŠãããç¶æ ã§ããããšãããã°ã©ã ã®å®è¡äžã«çºçããå¯èœæ§ãããäŸå€ã§ããããšããä»ã®äœããã«ã€ããŠãåãããšãè¡ããŸããç§ãã¡ã¯ãç§ãã¡ã§ãã©ããããã¡ã®çŽç²ãªèšç®ã®ã¯ã©ã¹ã§ã¯ããã®ãäœããããã«ãã»ã«åãããä»ã®äœããã®åŠçãš2ã€ã®äžŠåã¹ããªãŒã äžã®ãããã®èšç®ãå ±æããç§ãã¡ãåãçµãã§ããããããããæ確ã«ã
ãã®æç¹ã§åŠãã ããšããŸãšããŠèŠçŽããŸãããã
çŽç²ãªé¢æ°ã䜿çšããããšã«ãããèšç®ã®äžŠååãã³ã³ãã€ã©ã«ããèšç®ã®æé©åãéåžžã«å€§ããªããã°ã©ã ã§ããã¹ãããµããŒãããªãã¡ã¯ã¿ãªã³ã°ã®å®¹æãã«é¢é£ãã倧ããªå©ç¹ãåŸãããšãã§ããŸãã
, , , «- ». , , «- », «- ».
«- », . «- » .
«- » «» , «» .
:
a -> mb
m
â « »,b
.
, . , - :
a -> b
, .. .ma
.ma -> mb
, , «»a -> b
m
,a -> b
,mb
.
, , first class citizens. ã€ãŸã , â , .. , , , «»m
.f
,a
, ,mf
ma
, « »:
mf ` ` ma => m (f ` ` a).
, , , , , , .f :: b -> c
g :: a -> b
,f . g
,g
,f
.f :: b -> mc
g :: a -> mb
?mb
b
â , ,b
«»m
.
«»b
m
次ã®é¢æ°ã®å€ãšããŠæž¡ããããå®éãçŽç²ãªèšç®ãšäžŠè¡ããŠããã©ãããŒãã«ã¯ãä»ã®äœããã®èšç®ãããããã®èšç®ã®çµæã次ã®é¢æ°ã«æž¡ãå¿ èŠããããŸããäžè¬çã«ãæã ã¯æ©èœãkompozirovatã§ããæ¹æ³ãææ¡ããå¿ èŠãããa -> mb
ãšb -> mc
ç§ãã¡ã¯ããããæ°ããæ©èœãåŸãããšãã§ããããša -> mc
ãããã³ãã®çµæç©ã¯ãæã ã¯ããä»ã®äœããã®ãªãèšç®ããããèšç®ã倱ã£ãŠããªãããŠããªããšããããã«ãããããæ¢ã«æšæž¬ãããŠããããã«ãç§ãã¡ã®ãœãªã¥ãŒã·ã§ã³ãæ®éçã§ãªããã°ãªããŸããã
ãã¡ã³ã¯ã¿ãŒã¯ç°¡åã§ã¯ãããŸããããéåžžã«ç°¡åã§ãïŒ
ãããã£ãŠã3ã€ã®ã¿ã¹ã¯ããããŸãã
ã©ãããããå€ã«éåžžã®å€ã§æ©èœããæ¢åã®é¢æ°ãã©ã®ããã«é©çšã§ããããç解ããããã
, , , .
, , â , , , «- », .
ååãšããŠãfunktsionalschikiã¯æ æ°ãªäººã§ã¯ãªãã£ãå Žåã圌ãã¯èã
isChar :: a -> Bool
æž¡ãããå€ãåã®å€
Char
ã§ãããã©ããããã§ãã¯ããé¢æ°ã®é¡äŒŒç©ã決å®ããã«ã¯ãã©ãããŒåã®ããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒãšåãæ°ã®æ¹çšåŒãèšè¿°ããå¿ èŠããããŸããããšãã°ãã©ãããŒã¿ã€ã
Maybe a
ã«ã¯2ã€ã®ããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒã
Just
ãã
Nothing
ãŸãã
maybeIsChar :: Maybe Char -> Maybe Char -> Maybe Bool maybeIsChar (Just x) = Just (isChar x) maybeIsChar Nothing = Nothing
ãã®ãããããããïŒããã¯èŠãç®ã§ã¯ãããŸããïŒããã«ãã©ãããããããŒã¿ãæäœããããã®åçŽç²é¢æ°ã®é¡äŒŒç©ãå®çŸ©ã§ããŸãããããŠãé¢æ°ããšã«ã ãã§ãªããã©ãããŒããšã«å¯Ÿå¿ããã¢ããã°ãèšè¿°ããå¿ èŠããããŸãïŒ
ããããããã¯å¥ã®æ¹æ³ã§è¡ãããšãã§ããŸããæ¢ã«æã£ãŠããçŽç²ãªé¢æ°ãæåã®åŒæ°ãšããŠåãåãããããã©ãããŒã«å«ãŸããå€ã«é©çšããæ°ããé¢æ°ãå®çŸ©ããŠãåãã©ãããŒã«ã©ãããããæ°ããå€ãè¿ãããšãã§ããŸãããã®é¢æ°ãåŒã³åºããŸã
fmap
ïŒ
fmap :: (a -> b) -> ma -> mb
ããã§ãã©ãããŒã¿ã€ãããšã«éåžžã®é¢æ°ã®äœçŸãã®é¡äŒŒç©ãå®çŸ©ãã代ããã«ãã©ãããŒã¿ã€ãããšã«é¢æ°ã1ã€ã ãå®çŸ©ã§ããŸã
fmap
ã
fmap
ã©ãããŒã¿ã€ãã®é¢æ°ãå®çŸ©ããŸããã
Maybe a
ïŒ
fmap f (Just x) = Just (fx) fmap _ Nothing = Nothing
ãããŠã2çªç®ã®åŒã®fmapé¢æ°ã®æåã®åŒæ°ã®ä»£ããã«ãã®äžç·ã¯äœã§ããïŒ
. , , , , . - , . , .
fmap
a -> b
. , , , , . - , . , .
ããã§ãã©ãããããåã®å€ã«
Maybe a
åé¢æ°ãé©çšã§ããŸã
a -> b
ãé¢æ°
fmap
ã1ã€ã ãå®çŸ©ããããšã§ãå€ãã®è¿œå äœæ¥ããªããããšã«åæããŸããåããã¬ãŒãºã®çºé³ã¯ç°ãªãå ŽåããããŸããã©ãããŒåã
Maybe a
ãã¡ã³ã¯ã¿ãŒã«ãããããå€ãã®è¿œå äœæ¥ããªããªããŸããã
ã¯ããã¯ãïŒã©ãããŒåããã¡ã³ã¯ã¿ãŒã«ããã«ã¯ãé¢æ°ãå®çŸ©ããå¿ èŠããããŸã
fmap
ãããã«ãããéåžžã®å€ã§æ©èœããé¢æ°ãã©ãããŒã«ãæ³šå ¥ãã§ããŸãããã¡ã³ã¯ã¿ãŒã¯éåžžã«ã·ã³ãã«ã ãšèšããŸããïŒäŸ¿å©ã§ã ããã«ããã以åã«å®çŸ©ãããçŽç²ãªé¢æ°ãéåžžã ãã§ãªããã©ãããããå€ã§ã䜿çšã§ããŸãã
é©çšãã¡ã³ã¯ã¿ãŒãéåžžã«ç°¡åã§ãïŒ
ã©ãããããŠããªãå€ã§æ©èœããé¢æ°ãã©ãããããå€ã«é©çšããæ¹æ³ãèŠã€ããŸãããããããé¢æ°èªäœãã©ãããããŠããå Žåã¯ã©ãã§ããããïŒã©ãããããå€ã«ã©ã®ããã«é©çšããŸããïŒ
æšæž¬ãããšæããŸããã©ãããããé¢æ°ãæåã®åŒæ°ãšããŠãã©ãããããå€ã2çªç®ã®åŒæ°ãšããŠãšãé¢æ°ã宣èšãããã®ãããªæäœãå¿ èŠãªã©ãããŒã¿ã€ãããšã«ãã®é¢æ°ãå®çŸ©ããå¿ èŠããããŸãããã®é¢æ°ãåŒã³åºããŸã
<*>
ïŒread apply; é¢æ°ã®ååãââå°æåã§ã¯ãªãç¹æ®æåã§å§ãŸããšããäºå®ã¯ãäžçœ®åœ¢åŒã§äœ¿çšããå¿ èŠãããããšã瀺ããŠããŸããéåžžã®é¢æ°ã®ããã«æ¥é èŸåœ¢åŒã§äœ¿çšããå Žåã¯ãæ¬åŒ§ã§å²ãå¿ èŠããããŸãïŒïŒ
(<*>) :: m (a -> b) -> ma -> mb
typeã«å¯ŸããŠå®£èšãããé¢æ°ãå®çŸ©ããŸããã
Maybe a
ãåæã«ããã®åã«ã¯2ã€ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãããããšãæãåºããŠãã ãããã€ãŸãããã®åã§ã©ãããããé¢æ°ãšã©ãããããå€ã¯ïŒ
Just
ïŒãŸãã¯
Nothing
ïŒã®ããããã§ãã
(Just f) <*> Nothing = Nothing Nothing <*> _ = Nothing (Just f) <*> (Just x) = Just (fx)
ããã§ãæåã¯éåžžã®å€ã§ããåäœããªãã£ãã©ãããããé¢æ°ãã©ãããããå€ã«é©çšã§ããŸãïŒã©ãããŒãtypeã§ããå ŽåïŒ
Maybe
ãä»ã®ã©ãããŒã§ãåãããšãã§ããããã«ãããå Žåãå¿ èŠãªã®ã¯ãããã®ããããã«å¯ŸããŠé¢æ°ãå®çŸ©ããããš
(<*>)
ã§ããèšãæãããšããããã®ã©ãããŒãã¢ããªã«ãã£ããã¡ã³ã¯ã¿ã«ããå¿ èŠããããŸããé¢æ°
(<*>)
ãšãå®çŸ©ãããŠããã©ãããŒã¿ã€ãã¯ã¢ããªã«ãã£ããã¡ã³ã¯ã¿ã§ããããã§ã
pure
ã
é¢æ°
pure
ã¯äœãããŸããïŒããããã¡ã³ã¯ã¿ãŒãå¿çšãã¡ã³ã¯ã¿ãŒãããç°¡åã§ãïŒãã®é¢æ°
pure
ã¯éåžžã®å€ãåããããããã©ãããããå€ãäœæããŸãã圌女ã®ã¿ã€ãã¯æ¬¡ã®ãšããã§ãã
pure :: a -> ma
pure
ã©ãããŒåã®é¢æ°ãå®çŸ©
Maybe
ããŠãå®éã®é©çšå¯èœãªãã¡ã³ã¯ã¿ãŒã«ããŸãã
pure x = Just x
ãã¹ãŠãéåžžã«è€éã§ãããïŒïŒç¢æãSarcasmïŒãã®ãã¬ãŒãïŒ
ãšããã§ãã©ãããŒåãé©çšå¯èœãªãã¡ã³ã¯ã¿ãŒã«ããããšã§ã察å¿ããæ°ã®ã©ãããããåŒæ°ã«ä»»æã®æ°ã®éåžžã®åŒæ°ãåãé¢æ°ãé©çšã§ããŸãïŒãã¡ã³ã¯ã¿ãŒã¯ã1ã€ã®åŒæ°ã®éåžžã®é¢æ°ã®ã¿ãã©ãããããå€ã«é©çšã§ããŸãïŒãããã¯ãäŸãã°ãæã ãè¿œå ã§ããããã§ãã
Just 2
ãš
Just 3
ïŒ
pure (+) <*> Just 2 <*> Just 3 > Just 5
ã³ãŒãã¯å®å
šã«æ確ã§ã¯ãããŸãããïŒ
pure
, . 2
.
Maybe
(+)
, . 2
(<*>)
.
ãã®æ§æã¯å¥œãã§ã¯ãããŸãããïŒãããè©ŠããŠãã ããïŒ
, , .
, A Applicative (functor), N , , . (+), :
, :
liftAN
, A Applicative (functor), N , , . (+), :
liftA2 (+) (Just 3) (Just 2) > Just 5
, :
( | a + b | )
( | (Just 3) + (Just 2) | ) > Just 5
ããªãã¯ç¬ããŸãããã¢ãããç°¡åã§ãïŒ
ããã§ãïŒ1ã€ã®åŒæ°ã®ïŒéåžžã®é¢æ°ãã©ãããããå€ã«é©çšããæ¹æ³ãèŠã€ããŸããããããè¡ãã«ã¯ãã©ãããŒã¿ã€ãã®é¢æ°ãå®çŸ©ããå¿ èŠããããŸã
fmap
ããããŠããã®é¢æ°ãã©ãããŒã¿ã€ãã«å®è£ ããããããã¡ã³ã¯ã¿ãŒã«ãªãããã«å¿ èŠãªãã®ã¯äœããªããããèªãããã«ãã¡ã³ã¯ã¿ãŒãšåŒã°ããæš©å©ããããŸãã
ãŸããã©ãããããå€ã«ã©ãããããé¢æ°ãé©çšããæ¹æ³ãèŠã€ããŸããããããè¡ãã«ã¯ãã©ãããŒã¿ã€ãã«2ã€ã®é¢æ°ãå®çŸ©ããå¿ èŠããããŸã-
pure
ãããŠ
<*>
-ãŸããããã«ãããä»»æã®æ°ã®åŒæ°ãåãã©ãããããå€ã«éåžžã®é¢æ°ãé©çšã§ããŸããããããŠããããã®é¢æ°ãã©ãããŒã¿ã€ãã«å®çŸ©ãããšããã«ãã¢ããªã±ãŒã·ã§ã³ãã¡ã³ã¯ã¿ãšåŒã°ããæš©å©ãç²åŸããŸããããšããã§ãã©ãããŒåãé©çšå¯èœãªãã¡ã³ã¯ã¿ãŒã«ããããã«ã¯ãæåã«éåžžã®ãã¡ã³ã¯ã¿ãŒã«ããå¿ èŠããããŸãïŒãããŠãããŒãããããšã¯ã§ããŸãã-ã³ã³ãã€ã©ãŒã¯ããã«åŸããŸãïŒãããã«ã¯è«ççãªïŒãããŠããã€ãã®ããã«ãç°¡åãªïŒèª¬æããããŸãããã®èšäºã¯ãã§ã«éåžžã«èšãäžãã£ãŠããã®ã§ãããªãèªèº«ã§å匷ããããã«æ®ããŠãããŸãã
ããã¯ãæã ã¯2ã€ã®æ©èœã®æ§å³ãäœãããšãã§ããæ¹æ³ãç解ããããã«ç§ãã¡ã®ããã«æ®ã£ãŠãã
a -> mb
ããš
b -> mc
ãã®ãããçŽç²ãªèšç®ã®çµæãšãã©ãããŒã«å«ãŸãããä»ã®äœãããèšç®ããçµæã®äž¡æ¹ããæåã®é¢æ°ãã2çªç®ã®é¢æ°ã«è»¢éãããŸããããããæ¢ã«æšæž¬ãããŠããããã«ããã®ããã«ã¯ãã©ãããŒã¿ã€ãã«å¯ŸããŠ1ã€ãŸãã¯2ã€ã®é¢æ°ãå®çŸ©ããå¿ èŠããããŸãããããŠãæãç¬åµçãªäººã¯ããããã®é¢æ°ãå®çŸ©ãããã©ãããŒåãã¢ãããšåŒã°ããããšããã§ã«ç解ããŠããŸãã
ãããã®é¢æ°ã®æåã¯é¢æ°
return
ã§ãã
return
é¢æ°ããã®åºå£ç¹ãå®çŸ©ããããšã¯å¿ é ã§ã¯ãããŸããã Haskellé¢æ°
return
ã¯éåžžã®å€ãåããããããã©ãããããå€ãäœæããŸãã
return :: a -> ma
pure
ã©ãããŒåãé©çšå¯èœãªãã¡ã³ã¯ã¿ãŒã«å€ããç« ã®é¢æ°ã®ããã«èãããŸããïŒã€ãŸãããããã®é¢æ°ã¯åãä»äºãããŸãããããŠãã¢ãããäœæãããã©ãããŒåã¯ãæåã«é©çšå¯èœãªãã¡ã³ã¯ã¿ãŒïŒãããŠãã®åã«-åãªããã¡ã³ã¯ã¿ãŒïŒã«ãªããªããã°ãªããªããšããã«ãŒã«ããããŸããã€ãŸãããã®ã©ãããŒåã«å¯ŸããŠããã§ã«çŽç²é¢æ°
return
ãå®çŸ©ããŠããã®ã§ãé¢æ°ãéåžžã«å®çŸ©ã§ããŸãã·ã³ãã«ïŒ
return = pure
ã©ãããŒåãã¢ããã«ããããã«å®çŸ©ããå¿ èŠããã2çªç®ã®é¢æ°ãåŒã³åºãããŸã
(>>=)
ïŒèªã¿åããã€ã³ãïŒã次ã®ã¿ã€ãããããŸãã
(>>=) :: mb -> (b -> mc) -> mc
ããŒã...ããã¯å®éã«æ©èœã®æ§æã«äŒŒãŠããªãäœããããã§ããé¢æ°
(>>=)
ã¯ã©ãããããå€ãštype ã®é¢æ°ãåãåãã
a -> mb
ã¿ã€ãã©ãããŒã§ã©ãããããçŽç²ãªèšç®ã®çµæãšãäœãããèšç®ããçµæïŒãŸãã¯ãããä¿åããçµæïŒã®äž¡æ¹ããã®é¢æ°ã«æž¡ãæ¹æ³ã決å®ããå¿ èŠããããŸãã©ãããŒèªäœã«å«ãŸãããèšç®ããè¡ãããªãã£ãå Žåã¯ããã®ä»ããã€ãŸããã®å Žåãé¢æ°ã衚瀺ãã
a -> mb
ããã®çµæãšããŠåã®å€ãååŸããŸãã
mb
ãã€ãŸãããã§ã«ã©ããã«ãããšããããšã§ãããã ãããã®é¢æ°ã䜿çšããŠãæ§æé¢æ°ãå°ãåŸã§å®çŸ©ããŸã
(>>=)
ããããŸã§ã®éããããããŠãã ãããã©ãããŒã®typeã
å®è£
(>>=)
ããŸããã
Maybe
ã圌ã«ã¯2ã€ã®ããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒããããããããã«ã¯2ã€ã®æ¹çšåŒãå¿ èŠã§ãããarrow to Leysleyããšããååããã
b -> mc
æåã®ãããªé¢æ°ãåŒã³åºããŸãããïŒéåžžã®å€ãåããã©ãããããå€ãè¿ãé¢æ°ã¯ãã¹ãŠãClaysley ArrowsããšåŒã°ãã以åã«å®è£ ããé¢æ°ããClaysley Arrowãã§ãïŒïŒ
k
return
â Nothing, Nothing Nothing >>= _ = Nothing â , "" k (Just x) >>= k = kx
以äžã§ããã©ãããŒå
Maybe
ã¯ã¢ããã«ãªããŸããïŒãããããããã«ããªããã°ãªããªãã£ãããšã¯ãæ©èœ
return
ãšããã決å®ããããš
(>>=)
ã§ããã
ïŒçŽç²ãªæ©èœãæäŸããä¿æããå©ç¹ã«å ããŠïŒããã¯ç§ãã¡ã«äœãäžããŸãããïŒæå³ãäŒãããã¯ã©ã€ãºãªãŒã®ç¢å°ã®ã³ã³ãã€ãŒå šäœãæ³åããŠãã ããããããã®åClaysleyç¢å°ã¯
Maybe
ãããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒã䜿çšããŠã©ãããŒã«ã©ãããããå€
Just
ããŸãã¯
Nothing
ãæããã«ããã®ãã§ãŒã³ããã®ããçš®ã®Kleisleyç¢å°ããäžãã
Nothing
å Žåããã®å€ããã€ãã©ã€ã³ã«æ²¿ã£ãŠããã«æž¡ãããšã¯æå³ããããŸãããããã§ãç§ãã¡ã¯äœãããŸããïŒåç¢å°ã®æäœåŸãã¯ã¬ã€ãºãªãŒã¯å©ããåããŠãã§ãã¯ã
if then else
ãåã®é¢æ°ãè¿ããŸããã§ãã
Nothing
ãïŒ
çåžã¯ãŸãã«ãããè¡ããå€ãã®ãã¹ããããæ§é ããstructuresãæ§é ãæ§ç¯ããŸã
if then else
ããããã
(>>=)
ãã®ãããªé«ãªãã§ãã®åé¡ã解決ããé¢æ°ãå®çŸ©ããŸãããèªåã§ç¢ºèªããŠãã ããïŒã©ãã
Nothing
ã«çŸããå ŽåãæŒç®åã¯
(>>=)
é¢æ°ã«æž¡ããã«ãã€ãã©ã€ã³ã®æåŸãŸã§åçŽã«ãã¹ãã¬ãããããŸãããããã£ãŠã
Nothing
ãã¢ããã䜿çšãããšãçŽç²ãªé¢æ°ã䜿çšããå©ç¹ãç¶æã§ããã ãã§ãªããã³ãŒããã¯ããã«å°ãªãèšè¿°ã§ããã³ãŒãèªäœãã¯ããã«èªã¿ããããªããŸãã
ãããŠãããã«ããã€ãã®ã¢ãããå®çŸ©ããŸããã
ãã1ã€ã®ã¢ãããå®çŸ©ããŸããããïŒå
Either ab
ããããšã©ãŒãšäŸå€ãããæ確ã«åŠçã§ããã©ãããŒåã䜿çšããŸã
Maybe
ããã®ã¿ã€ãã®å®çŸ©ãæãåºããŠã¿ãŸãããã
data Either ab = Left a | Right b
-ãã®ã¿ã€ãã¯2ã€ã®ã³ã³ã¹ãã©ã¯ã¿ãã®ãããããæã£ãŠãã
Left
-å ¥åããããã«èšå®ãããŠãã
a
-ããã¯æã ãã©ãããŒã§ãããšã©ãŒã¡ãã»ãŒãžã«äœ¿çšããããšã®ã¿ã€ãã¯ããã®ããã«ãä»ã®äœãããããŠããã第äºã¯ã -
Right
-ã¿ã€ãã«èšå®ãããŠããŸã
b
-ããããåºæ¬çãªãèšç®ã®ã¿ã€ãã§ãã ãåºæ¬çãªãèšç®ãéå°ã«è¡ãããªãå Žåãèšç®ã®å€ã¯Claysleyç¢å°ã®æ§æã®ãã§ãŒã³ã«æ²¿ã£ãŠããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒã§ã©ãããã
Right
ãŸãããããŠããšã©ãŒãçºçãããšããã«-ããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒã䜿çšããŠã©ãããããçµæã®ã¡ãã»ãŒãžãååŸããŸã
Left
ã
æåã«é¢æ°ãå®çŸ©ããŸã
return
ïŒ
return x = Right x
ããã§ã¯ãã¹ãŠãæããã§ãã
return
ãšã©ãŒã¡ãã»ãŒãžãé¢æ°ã«æž¡ãã®ã§ã¯ãªããäœããã®åã®å€
b
ãæž¡ããããããŒã¿ã³ã³ã¹ãã©ã¯ã¿é¢æ°ããã®å€ã«é©çšã
Right
ãŠåã®å€ãååŸããŸã
Either ab
ã
ããã§ãæŒç®åãå®çŸ©ããŸã
(>>=)
ãããã§ã®ããžãã¯ã¯ã¢ãããšåã
Maybe
ã§ãïŒãã§ãŒã³ã«æ²¿ã£ãŠã¿ã€ãå€ãæž¡ãããå°ãªããšã1ã€ã®Kleisleyç¢å°
Either ab
ããããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°
Left
ã§ã©ããããããšã©ãŒã¡ãã»ãŒãžã衚瀺ããå Žåãèšç®ãã§ãŒã³å šäœã®çµæã¯ãã®ãšã©ãŒã¡ãã»ãŒãžã«ãªããŸãããã¹ãŠã®èšç®ãæåããå ŽåïŒã€ãŸããåClaysleyç¢å°ãdata_constructoré¢æ°ã䜿çšããŠã©ãããããèšç®ã®çµæãè¿ããå ŽåïŒ
Right
ïŒã次ã®åæ©èœããã®çµæã«é©çšããå¿ èŠããããŸãã
(Left x) >>= _ = Left x (Right x) >>= k = kx
ã¢ãã
Maybe
ãªã©
Either
ãã©ã¡ãã«ã2ã€ã®ããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒãããããã®ãã¡ã®1ã€ã¯èšç®ã®å€±æã瀺ããŸãïŒãããã£ãŠã次ã®é¢æ°ã«æž¡ããã«ãã¯ã¬ã€ãºãªãŒã®ç¢å°ã§ã³ã³ããžã·ã§ã³ã®æåŸã«ããã©ãã°ãããå¿ èŠããããŸãïŒãäž¡æ¹ã®ã¢ããã®2çªç®ã®ããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒã¯ãèšç®ãæ£åžžã«å®äºããããšãæå³ãããããã®èšç®ã®å€ã¯æ¬¡ã®Kleisleyç¢å°ã«æž¡ãããŸãã
ããã§ã¯ã以åã«å®è£ ãããã¢ãããšã¯ç°ãªãã¢ããããªã¹ãã¢ãããå®è£ ããŸãããããªã¹ãã¢ããã®Claysleyç¢å°ã®ã¿ã€ãã¯
a -> [b]
ã§ããæŒç®åã®æåã®åŒæ°
(>>=)
ã¯ã©ãããããåã®å€ã§ã
ma
ããã®å Žåãããã¯
[a]
ïŒåã®å€ã®ãªã¹ã
a
ïŒã§ããããã«ããªã¹ãã¯ç©ºã®å Žåãããã°ãã¿ã€ãã®1ã€ä»¥äžã®å€ãå«ãå ŽåããããŸã
a
ã
ãã®å ŽåãClaysleyç¢å°ãå€ã®ãªã¹ãã«é©çšãããšã¯ã©ãããæå³ã§ããïŒããã¯ããªã¹ãã®åå€ã«é©çšããå¿ èŠãããããšãæå³ããŸãã空ã®ãªã¹ãã®å Žåããã¹ãŠãæ確ã§ããClaysleyç¢å°ã䜿çšãããã®ã¯äœããªãã®ã§ãçµæãšããŠç©ºã®ãªã¹ããååŸããŸãã空ã§ãªããªã¹ãã®åå€ã«å¯ŸããŠãé¢æ°ã䜿çšããŠClaysleyç¢å°ãé©çšã§ããŸã
fmap
ïŒãªã¹ãããã¢ãããäœæãããããããã¯ãªã¹ãããã¡ã³ã¯ã¿ãŒã§ãããããšãæå³ããŸã-èŠããŠããŸããïŒïŒããã ããfunctionã®ã¿ã€ããæãåºããŠã¿ãŸãããã
fmap
䟿å®äžãæœè±¡ã©ãããŒã¿ã€ã
m
ãç¹å®ã®ã©ãããŒãªã¹ãã¿ã€ãã«çœ®ãæããŸãã
fmap :: (a -> b) -> [a] -> [b]
次ã«ãæž¡ãã
fmap
ãé¢æ°ã®ã¿ã€ããClaysleyç¢å°ã®ã¿ã€ãã«çœ®ãæããŸãã
fmap :: (a -> [b]) -> [a] -> [[b]]
Kleisleyç¢å°ãæž¡ããçµæ
fmap
ãtype
mb
ã§ã¯ãªãtypeã®å€ãååŸããããšãããããŸã
mmb
ãäºéã«ã©ããããŸããããã¯operatorã®ã¿ã€ãã«å¯Ÿå¿ããªã
(>>=)
ãããã©ãããŒã®1ã€ããåé€ãããå¿ èŠããããŸãããããè¡ãã«
concat
ã¯ããªã¹ãã®ãªã¹ããåãå ¥ããå éšãªã¹ããé£çµããéåžžã®å€ã®ãªã¹ããè¿ãé¢æ°ããããŸããããã§
(>>=)
ãªã¹ãã¢ããã®æŒç®åãå®çŸ©ããæºåãã§ããŸããïŒ
[] >>= _ = [] xs >>= k = (concat . fmap k) xs
æŒç®åã決å®ããããžãã¯ã¯
(>>=)
ããã¹ãŠã®å Žåã§åãã§ããããšãããããŸããã©ãããããåå€ã«ã¯ãèšç®ã®çµæãšãä»ã®äœããããããå¥ã®é¢æ°ã«æž¡ããšãã«èšç®ãšãã®ãä»ã®äœããã§äœãããå¿ èŠãããããèããŸãã ããã®ä»ãã¯ãèšç®ã®æåãŸãã¯å€±æã®ããŒã«ãŒãèšç®ã®æåãŸãã¯ãšã©ãŒã¡ãã»ãŒãžã®ããŒã«ãŒãèšç®ããŒãããç¡éã®çµæãè¿ãããšãã§ããããŒã«ãŒã®å ŽåããããŸãã ãä»ã®äœããã¯ãã°ãšã³ããªãã€ãŸãèªã¿åãããåºæ¬ãèšç®ã®åŒæ°ãšããŠæž¡ãç¶æ ã«ããããšãã§ããŸãããŸãã¯ããåºæ¬ãèšç®ãšäžŠè¡ããŠãèªã¿åããå€æŽãããã³å¥ã®é¢æ°ã«æž¡ãããç¶æ ãå床å€æŽããŸãã
ã¢ããã«ã¯è€éãªãã®ã¯äœããªãããšãèªããªããã°ãªããŸããïŒãã¡ã³ã¯ã¿ãŒãå¿çšãã¡ã³ã¯ã¿ãŒã®ããã«ïŒãã¢ããã¯ã2ã€ã®é¢æ°ãå®çŸ©ãããŠããåãªãã©ãããŒã¿ã€ãã§ã-
return
ãš
(>>=)
ã
ãã ãã
(>>=)
ã¹ããŒããã«ã©ãããŒã®å®çŸ©ã¯ããè€éã§ãããããã®å®è£ ã«ã¯ããã®èšäºã§çŽ¹ä»ãããã®ãããHaskellã®æ§æã«ç²ŸéããŠããå¿ èŠããããããããã§ã¯çŽ¹ä»ããŸãããããããç§ã¯ããªããå®å¿ããããã§ãã第äžã«ãéåžžã«é«åºŠãªHaskellããã°ã©ããŒã§ãããéåžžã¯ã¢ãããäœæãããçµã¿èŸŒã¿èšèªã䜿çšããŸããããã¯ãããããå Žåã«ååã§ãã第äºã«ãã¢ããïŒç¶æ ã§åäœãããã®ãå«ãïŒã䜿çšããããšã¯ãã¢ãããå®çŸ©ãããããã¯ããã«ç°¡åã§ããããã«ã€ããŠã¯ã次ã®ç« ã§èª¬æããŸãã
ã¢ãããç解ããã«ã¯ãåçŽãªååãç解ããå¿ èŠããããŸãããåºæ¬çãªãèšç®ãšã䞊è¡ããŠçºçãããä»ã®äœããã®èšç®ããããŸãããŸãããã¯ã¬ã€ãºãªãŒã·ã¥ãŒã¿ãŒãã®ãã³ã³ãã¢ãã§ãããã®èšç®ãã©ã®çšåºŠæ£ç¢ºã«è¡ããããã¯ããªãã¬ãŒã¿ãŒã決å®ã
(>>=)
ãŸãããããã£ãŠãæŒç®åãèªå
(>>=)
ã§å®çŸ©ããå¿ èŠã¯ã»ãšãã©ãããŸããããããã«äœãã©ã®ããã«çºçããããããããç解ããããã«ãããŸããŸãªçµã¿èŸŒã¿ã¢ããåã«å¯ŸããŠå®çŸ©ãããæ¹æ³ãç解ããããšã¯éåžžã«åœ¹ç«ã¡ãŸãã
ã¡ãªã¿ã«ãæŒç®å
(>>=)
ãã¯ã¬ã€ãºãªãŒã®ç¢å°ã®åæã®åãæšãŠãããããŒãžã§ã³ã§ãããšèšã£ããšããç§ã¯ãããéããŠå®éã®åæã決å®ããããšãçŽæããŸãããããã¯Haskellèšèªã®æšæºé¢æ°ã§ããããšè¡šç€ºãã
(>=>)
ããéãïŒãéæŒç®åãïŒãšçºé³ãããŸãã
(>=>) :: (a -> mb) -> (b -> mc) -> a -> mc (f >=> g) x = fx >>= g
x
ç§ãã¡ãæã£ãŠããã¿ã€ãã¯value
a
ã§
f
ããã
g
Kleisleyã®ç¢å°ã§ãã Claysleyç¢å°
f
ãvalue
x
ã«é©çšãããšãã©ãããããå€ãååŸãããŸãããããŠãèŠããŠããããã«ãã©ãããããå€ãClaysleyã®æ¬¡ã®ç¢å°ã«è»¢éããæ¹æ³ã¯ããªãã¬ãŒã¿ãŒãç¥ã£ãŠã
(>>=)
ãŸãã
次ã®ããŒãã§ã¯ãHaskellèšèªã§å®çŸ©ãããã¢ããã䜿çšããæ¹æ³ãèŠãŠãããŸãïŒãããŠã倧å€æ°ã®ããã°ã©ããŒã¯ä»ã®ã¢ãããå¿ èŠãšããŸããïŒã
Writer
ïŒãã°ãšã³ããªã®ãä»ã®äœããã§è¡šçŸãããŠããïŒãã¢ãã€ããšã¯äœãããªãã¢ãã€ããå¿ èŠãªã®ãã説æããæ£åœãªçç±ããããŸãããããŠããã¿ã€ãã¯ã©ã¹ããšåŒã°ãããã1ã€ã®åŒ·åãªHaskellã¡ã«ããºã ã«ã€ããŠèª¬æãããã§ã«ãäŒããããã¡ã³ã¯ã¿ãŒãå¿çšãã¡ã³ã¯ã¿ãŒãã¢ããããã¢ãã€ããã¿ã€ãã¯ã©ã¹ãšã©ã®ããã«çµã³ä»ããŠãããã説æããŠã話ãçµãããŸãããŸã äŒããŠããŸããããããŠæåŸã«ãç§ã¯çŽæãæãããéåžžã®ã¢ãããšã¯ç°ãªãI / Oã¢ããã«ã€ããŠç°¡åã«è©±ããŸãïŒãã ããå®è£ ã®ã¿ãç°ãªãã䜿çšäžã¯ä»ã®ã¢ãããšåããããç°¡åã§ãïŒã