Googleã§é©å½ã«æ¤ç´¢ããã¨
ã¨ãºã©ãªã¨åºã¦ããã
ãªãã¸ã§ã¯ãæå v.s. é¢æ°åããã°ã©ãã³ã°
ããã°ã©ããç¥ãã¹ã97ã®ãã¨/é¢æ°åããã°ã©ãã³ã°ãå¦ã¶ãã¨ã®éè¦æ§
ã¨ãããªãã«åèã«ãªããããªæ å ±ã¯ãããã®ã®ãç¡é§ã«è¤éåããããªãã¸ã§ã¯ãæåãã¹ããã¼ãã³ã«ãããããªè¨äºã°ãã(ããã ãä»ã¾ã§ã®ãªãã¸ã§ã¯ãæåã«ã¿ããªããããããããã¦ããã®ã ãã)ã§ããããããã¡ãã¨èªåèªèº«ãé¢æ°åããã°ã©ãã³ã°ã¨ãªãã¸ã§ã¯ãæåã®åãé¢ããæ¹ãã«ã¤ãã¦ã¯ã£ããããã¦ãããããã¨èããæ¦å¿µæ´çããçµè«ãæ¸ãã¾ãã
ã¾ã端çãªçµè«
çµè«ã端çã«è¨ãã¨ã
ã¹ããªã¼ã å¤æå¨ + æç¶ãã®èªç¶å¤æ*1 â ãªãã¸ã§ã¯ãæå
ã®é¢ä¿ã«ãªã£ã¦ããã
ãé¢æ°åããã°ã©ãã³ã°ã¨ãªãã¸ã§ã¯ãæåãè¨æ©å¿å¤ã«ä½µç¨ããã
ã ã¨ãã
ãé¢æ°åããã°ã©ãã³ã°ã¨ãªãã¸ã§ã¯ãæåã®ãã«ããã©ãã¤ã ã§ããã
ã¨ããã¨ãä»ã¾ã§è¦ãã¦ããªãã£ãé¨åã§æ¦å¿µã®éè¤ãèµ·ãããããããããããããã¨ã«ãªããç解ã«ããã¦ãå®è·µã«ããã¦ãæ··ä¹±ãæããå®éæ··ä¹±ãæãã¦ãããã¨ãããã¨ã«ãªãã¾ãã
é¢æ°ãã®ãã®ããèãã
è¶çªã¯ããã¾ã§ãããç´ç²ãªé¢æ°fãèãã¦ã¿ããã:ã®å·¦ã¯èå¥ååãå³ã¯åã·ã°ããã£ã§ããã
f : A â B
fã¯å ·ä½çãªåAã®å¤ãåãåããBãè¿ããfã¯ç´ç²ãªã®ã§ããã¤å¤ã渡ããã¨ãæ¥æ¬ã®é¦é½ãé³¥åã«ãªããã¨ãåãå¤ã渡ãã°åãå¤ãè¿ã£ã¦ããã
ã¨ãããå®ç¨ä¸ã¯ãä»ã¾ã§ã«åãåã£ãå¤ã«å¿ãã¦ä»å¾ã®åä½ãå¤ãããå ´é¢ãå¤ã ããããã¼ãµã¼ãWebã¢ããªã±ã¼ã·ã§ã³ãªã©ããã®ä¾ã (Aã«æåããªã¯ã¨ã¹ããBã«ãã¼ã¿ãã¬ã¹ãã³ã¹ãå ¥ã)ãããã表ç¾ããã«ã¯ã©ãããã°ããã ãããï¼Bã ãã§ã¯ãªããã次ã®ãããããä¸ç·ã«è¿ãã°ããã®ã ã
data TransAB where TransAB : (A â (B, TransAB)) â TransAB runTransAB : TransAB â A â (B, TransAB)
ãã®ããã«ãå ¥åã«ãã£ã¦åºåã¨æ¬¡ã®ç¶æ ã決ã¾ãæ¦å¿µããã¼ãªãã·ã³(mealy machine)ã¨å¼ã¶ã
ã次ã®ãããããããã¨ããå¶ç´ã«ãã£ã¦ãé¢æ°ã®è¡¨ç¾åãå¢ããã®ã ã
ãã¦ãHaskellãPureScriptãªã©ã®ä¸é¨ã®è¨èªã§ã¯ãæç¶ããã®ãã®ããã¼ã¿æ§é ã¨ãã¦æ±ããã¨ãä¸è¬çã«è¡ããã¦ãããããç¨åº¦ã®åã®è¡¨ç¾åãããã°ãæç¶ãã¨ãæç¶ãã®éã®é¢æ°ãå®ç¾©ã§ããã
ãã£ã¨ãç°¡åãªæç¶ããå®ç¾©ãã¦ã¿ããã
data Yo x where Yo : Yo Result
è¦ãã¨ãããYoãããªãã®ã§ãYoãè¶ ãããã¨ã¯ã§ããªãããYoã®çµæã«å¯¾ãã¦æ¼ç®ãè¡ããã¯ã§ããããYoã2åãããã¡ããç¡çãã¾ãã¦ããåã®YoãæåãããYoããªãããªã©å¤¢ã®ã¾ã夢ã ã
ããããªãã¨ãããããã«ããã¢ãããã¨ããæ¦å¿µãä½ããããä¸ã®ããã«Yoã®æ©è½ãå¢ããã¨ãåã®Yoã«ä¾åããYoãå¯è½ã«ãªããPureã¨Bindã®å°å ¥ã«ã¤ãã¦ã¯ãã¢ããã¨ã¯ã¢ããã§ãããåç §ããããã
data Yo x where Pure : a â Yo a Bind : Yo a â (a â Yo b) â Yo b Yo : Yo Result
Yoãå ·ä½çãªåä½ã«å¤æãããHaskellã«é常ã«è¿ãè¨èªã«ããå®è£ ã示ããã
runYo : â x. Yo x â IO x runYo Yo = do print "Yo" return Success runYo (Pure a) = return a runYo (Bind m k) = runYo m >>= runYo . k
æç¶ãã®å¤æãè¨èã§è¡¨ãã¨ãæç¶ãã®çµæãä½ã§ãã£ã¦ããä¸èº«ã®æ§é ãä¿ã¤é¢æ°ãã¨ãªããæåã«æ±ã£ãç´ç²ãªé¢æ°ã¨æ¯ã¹ãã¨ãä»»æã®çµæã«å¯¾å¿ããªããã°ãªããªãåããã表ç¾åãå¼·ããåè«ã§ã¯ãããã£ãå¤æãèªç¶å¤æ(natural transformation)ã¨å¼ã¶ããã ãrunYoã¯ç´ç²ãªã®ã§ããã¤Yoãå¤æãã¦ããå ·ä½çã«è¡ãããå 容ã¯å¤ãããªãã
ããã¾ã§ç´¹ä»ããã2種é¡ã®é¢æ°ã®çºå±ãå°å³ã«æãã¦ã¿ããã
å¤æå¨ããæç¶ãã¸ã®é©ç¨ãæ¹åã«ä¼¸ã°ããèªç¶å¤æããç¶æ ä¾åãæ¹åã«ä¼¸ã°ããå ã«å®ç©ãããæ°ããã¦ããªãã ãããï¼
å¤æå¨ã®é åãç·©ããã«é²ãã§ããç§ã«ããåèµä¸æ¯ãã¨ãªã«ããåãããããã¦ãçªå¦ã¨ãã¦ããã¯å§¿ãç¾ãã(表è¨ã®çµ±ä¸ã®ããã«ä¸è¬åããã代æ°çãã¼ã¿å(GADT)ãç¨ããããå®è£ ã«å¿ é ã§ã¯ãªã)ã
data Treasure m n where Treasure : (â x. m x â n (x, Treasure m n)) â Treasure m n runTreasure : Treasure m n â m x â n (x, Treasure m n)
ããæç¶ããåãåãã¨ããã®çµæã¨æ¬¡ã®ç¶æ ãè¿ãæç¶ããçã¿åºããåãã¦ç®ã«ããããã¨ã¦ããããããããä¸æè°ãªæè¦ã
ã¨ããããããã¥ã¼ã¿ãã«ãªå¤æ°ãä½ã£ã¦ã¿ããã
data Identity x where Identity : a â Identity a data GetSet s x where Get : GetSet s s Set : s â GetSet s () variable : s â Treasure (GetSet s) Identity variable s = Treasure (handle s) handle :: s â GetSet s a â Identity (a, Treasure (GetSet s) Identity) handle s Get = Identity (s, variable s) handle s (Set s') = Identity ((), variable s')
ç¶æ ãä¿æããè½åã確èªã§ããã次ã«ããã®æ¦å¿µãé¢æ°ã®ãããªæ§è³ªãæã¤ã確ããã¦ã¿ããã¾ããæçé¢æ°ç¸å½ã
echo : Functor m â Treasure m m echo = Treasure (map (λx â (x, echo)))
(Haskellã®å ´åãmapã¯fmapã¨èªã¿æ¿ãã¦ã»ãã)
ç¶ãã¦ãåæã$ã¯ã¿ããªå¤§å¥½ãé¢æ°é©ç¨æ¼ç®å(f x $ g y = f x (g y)
)ã
(>>>) : Functor n â Treasure l m â Treasure m n â Treasure l n Treasure m >>> Treasure n = Treasure $ λe â map (λ((x, m'), n') â (x, m' >>> n')) (n (m e))
確ãã«é¢æ°ã®ããã«ãµãã¾ãããã ã
ãã®æ§é ã¯ç§ã«æ¢è¦æãæãããããããã®æ£ä½ãããã£ããOOPã«ããããªãã¸ã§ã¯ãã®ããã«ãµãã¾ãã®ã ã
ãªãã¸ã§ã¯ãã¯å é¨ç¶æ ãæã¡ãå¼ã°ããã¡ã½ããã«å¿ãã¦ãå¿ è¦ãããã°å é¨ç¶æ ãæ´æ°ããå¯ä½ç¨ãå¼ãèµ·ããââã¡ããã©ãæç¶ãã®å¤æã¨ãã¼ãªãã·ã³ã®ä¸¡æ¹ã®æ§è³ªãæã£ã¦ããã
ãããããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°è¨èªã«ãããè¨è¿°
result = obj.Foo(x, y)
ããã®ããæ¹ã§è§£éããã¨ã
(result, obj') â runTreasure obj (Foo x y)
ã¨ãã表ç¾ã«ãªãã
å ±éããæ§è³ªã¯ããã¡ã½ããã¬ãã«ã®ãæç¶ããããå®è£ ã¬ãã«ã®ãæç¶ããã«å¤æããã¤ã¤ããèªåã®ç¶æ ãæ´æ°ãããã¨ããç¹ã ã
TreasureãObjectã«æ¹åãã¦ãå ¸åçãªOOPã¨ã®å¯¾å¿ãã¾ã¨ãã¦ã¿ããã
å ¸åçOOP | é¢æ°åOOP |
---|---|
ããã°ã©ã | IO åã®å¤ |
ã¯ã©ã¹ | Object M IO åã®å¤ã®å®ç¾© |
ã¤ã³ã¹ã¿ã³ã¹ | Object M IO åã®åç
§*2 |
ã³ã³ã¹ãã©ã¯ã¿ | Object M IO ãçæããé¢æ° |
ã¤ã³ã¿ã¼ãã§ã¼ã¹ | M |
ã¡ã½ãã | M åã®å¤ |
ãã£ã¼ã«ã | get : M s ã¨set : s â M () ã®åå¨ |
ç¶æ¿ | Object M' M ã¨ã®åæ |
ãªã¼ãã¼ã©ã¤ã | Object M' Mã®å®è£ ã®ä¸ä¾ |
è±å½¢ç¶æ¿ | ã¡ã½ããã¸ã®ãã¿ã¼ã³ããã |
ããã¯ã¿ã¤ãã³ã° | åå¨éå |
ã«ãã»ã«å | ã¡ã½ãã以å¤ã®ã¢ã¯ã»ã¹æ段ãªã |
This | ä¸è¦ |
ä¸å¯è½ | IOãå¶éãããã¯ã©ã¹(Object M Limited ) |
ä¸å¯è½ | ã¯ã©ã¹ã®å®è£
ãã®ãã®ã¸ã®æä½((>>>t) ) |
ãªãã¸ã§ã¯ãæåã®ä»çµã¿ãããã£ãä¸ã¤ã®åã®ç¹æ®ãªå ´åã¨ãã¦è¡¨ç¾ã§ããã®ã¯ããªããªãé åçã§ã¯ãªãã ãããã
å¿ç¨
ãã£ãããObjectã®Haskellã«ããå®è£ ãä½ã£ããObjectåã«å ããObjectã®ã¤ã³ã¹ã¿ã³ã¹ã¨ãã¦å©ç¨ããããã®ã¦ã¼ãã£ãªãã£é¢æ°ãåãã¦ããã
ç¾å¨ãcallã¨ããã²ã¼ã ã¨ã³ã¸ã³ã¸ã®å¿ç¨ã試ã¿ã¦ããããªã³ã¯å ã®ã³ã¼ãã®ããã«ãããã«ããªãã¸ã§ã¯ãæåãããè¨è¿°ãå®éã«å¯è½ã«ãã¦ããã
çµè«
ãªãã¸ã§ã¯ãæåã¯ãé¢æ°åããã°ã©ãã³ã°ã¨ç¸å®¹ããªãä½ãã§ã¯ãªããå®è£ ãããã®ã ã
å ãã¿
ãã®è¨äºã¯IIJ-IIã®ã¢ã«ãã¤ãã®ãããåå¿é²ãå ¼ãã¦æ°ããããã°ã©ãã³ã°ã¹ã¿ã¤ã«ã¸ã®ãã¥ã¼ããªã¢ã«ã¨ãã¦æ¸ããã