FRPã¯ã©ã¤ã·ã¹
FRP(Functional Reactive Programming)ã¯ããªã¢ã¯ãã£ãããã°ã©ãã³ã°ã¨é¢æ°åããã°ã©ãã³ã°ã®æ§è³ªãæã¤ããã°ã©ãã³ã°ãã©ãã¤ã ã§ãããFRPã¯å¤å ¸çFRPã¨ç¢ç§ã®FRPã«å¤§å¥ãããã
å¤å ¸çFRP
å¤å ¸ç(Classical)FRPã¯ãéé£ç¶çãªå¤ã®åEventã¨ã常ã«ä½ããã®å¤ãåãBehaviourã®äºç¨®é¡ã®æ§é ãå°å ¥ãããã®ã§ããã 代表çãªå®è£ ã¨ãã¦reactive-bananaãeuphoriaãreflexãªã©ãæããããã
Haskellã«ããã¦ã¯ãEventã¯IOãéãã¦éåæçã«çæã§ããè¨è¨ãå¤ããEventã¯ãããããã£ã«ã¿ãªã³ã°ãã§ããã¢ãã¤ãã¨ãã¦åæãããã¨ãã§ããããªããGenã¯FRPã®æ§é ãæ±ãã®ã«è¦æ±ãããã¢ããã§ãå®è£
ã®é½åä¸ãã°ãã°å¿
è¦ã¨ãªããBehaviourã¯ç¾å¨ã®å¤ãåãåºããä»ãHaskellãªãApplicativeã®ã¤ã³ã¹ã¿ã³ã¹ã«ããªãããèªã¿åãå°ç¨ã®IOãã¨ãè¨ãããããããªããCFRPã®æ§æè¦ç´ ãã¾ã¨ããã¨ä»¥ä¸ã®ããã«ãªãã
Eventãç³ã¿è¾¼ãaccum
ãèã§ãBehaviorããµã³ããªã³ã°ããapply
ãå¿ã§ããã
ç¢ç§ã®FRP
ç¢ç§ã®(Arrowized)FRPã¯ãå¤ã§ã¯ãªãå¤æã«çç®ããå¤ãå¤æããæ©æ§ãå°å
¥ãããCategory
åã³Arrow
ã®ã¤ã³ã¹ã¿ã³ã¹ã§ãããããé¢æ°ã¨å
±éã®æ¼ç®ãæã¤ãå®è£
ã¯Yampaãwiresãªã©ãããã
(>>>) :: Category (~>) => (b ~> c) -> (a ~> b) -> a ~> c arr :: Arrow (~>) => (a -> b) -> a ~> b (***) :: Arrow (~>) => (a ~> b) -> (c ~> d) -> (a, c) ~> (b, d)
ãã®å®æ
ã¯å¤ãã®å ´åãã¼ãªãã·ã³ã§ãããç¶æ
ãä¿æã§ããã表ç¾ããæ¹æ³ã¯è²ã
ããããä¾ãã°ä»¥ä¸ã®ãããªæ§é ãªããå
¥åã«å¿ãã¦æ¬¡ã®Mealy
ã決ããã¨ãããã¨ãã§ããã
newtype Mealy a b = Mealy { runMealy :: a -> (b, Mealy a b) }
(>>>)
ã(***)
ãªã©ãç´æ¥çµã¿åãããã®ã¯éª¨ãæããããArrowè¨æ³ãç¨ãããã¨ã§æ¯è¼çç°¡åã«è¨è¿°ã§ãããarteryã¨ããã©ã¤ãã©ãªã§ã·ã³ã»ãµã¤ã¶ã¼ãå®è£
ããä¾ãç´¹ä»ããã
sineWave :: Artery m (Float, Float) Float -- å¨æ³¢æ°ã¨ä½ç¸ãå ¥åã¨ããæ£å¼¦æ³¢ãåºåãã genADSR :: Float -> Float -> Float -> Float -> Artery m Bool Float -- ãªããããªç«ã¡ä¸ããã»ç«ã¡ä¸ãããæã¤ã¨ã³ããã¼ããçæãã bell :: Artery m (Float, Bool) Float bell = proc (freq, gate) -> do env <- genADSR 0.01 0.4 0.2 0.4 -< gate m <- sineWave -< (64 * freq, 0) sineWave -< (freq * 2, m * env * 0.5)
å ¥åãæ示çã«è¡¨ç¾ããå¿ è¦ãããããCFRPã«æ¯ã¹ãã¨ããã©ã¼ãã³ã¹ã«ããã¦ã¯åªãã¦ããå¾åããããã¾ããç¢ç§ã®FRPã¯åºæã®æ¦å¿µãå°ãªãã®ãé·æã§ããã
FRPã®åé¡ç¹
両è ã®å ±éã®å©ç¹ã§ãããè½ã¨ãç©´ã§ãããã®ã¯ãç¶æ ãé è½ã§ããã¨ããã¨ããã ãç¶æ ãå®å ¨ã«é è½ãããã¨ã§çµã¿åããããããªãå ´åãããä¸æ¹ã§ãä¸åº¦FRPã§æ§ç¯ãããã®ã¯ãç¶æ ãé ãã¦ããããã«æ¡å¼µæ§ãä¹ãããå¤å ¸çFRPã«ããã¦ã¯ãå ¥åã¸ã®ä¾åæ§ãé è½ããã¦ãããããç¹ã«æ³¨æãå¿ è¦ã§ãããé ããç¶æ ãå¢ããã»ã©ãããã°ã©ã ã®æ§è³ªã¯è¤éã§ææ¡ããã®ãé£ãããªããåæçºçããã¤ãã³ãã®å¦çã注æãå¿ è¦ã§ããã
注æãã¹ããã¨
- ä½ã§ãFRPã§æ¸ããã¨ããªã: FRPã¯ãç°ãªã£ãå é¨ç¶æ ãé è½ããåæå¯è½ãªå ±éã®åã«ãã£ã¦æ±ããã¨ãå¯è½ã«ããããããããã®å©ç¹ãæ´»ããªããããªå ´æã«é©ç¨ããã°ãåã«åå©ç¨æ§ã¨ããã©ã¼ãã³ã¹ãç ç²ã«ãªãã ãã§ããã
- ã©ã¤ãã©ãªã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ãFRPã«éå®ããªã: APIãFRPã«éå®ãããã¨ã¯ãFRPã«èµ·å ããåé¡ãåé¿ããæ段ããªããã¨ãæå³ããã¦ã¼ã¶ã¼ã«ã¨ã£ã¦è¶³æ·ã¨ãªããFRPã®APIã¯è²ã ãªæä½ãã¾ã¨ãããã¨ã§æãç«ã¤ãããã®å é¨ã®æä½ãã¨ã¯ã¹ãã¼ãããã¦ããã°æè»ãªä½¿ãæ¹ãã§ããããã£ã½ã©ã®ä¿¡å¿µããªãéããFRPã¯ã¢ããªã±ã¼ã·ã§ã³ã®ã³ã¼ãã ãã«ã¨ã©ããã©ã¤ãã©ãªã®å®è£ ã§ã¯ä½¿ããªãã®ãå¾çã ããã
- FRPã«ãã£ã¦ä½ãå¾ãããä½ã失ãããã常ã«æèãã: FRPã§ããã°ã©ã ã表ç¾ãããã¨ã¯ãå é¨ç¶æ ã«ã¢ã¯ã»ã¹ãããæèã«ä¾åããåä½ããã¨ããæ§è³ªãç ç²ã«ãã¦ãç¶æ ã®é è½ãç°¡åãªåæãå¯è½ã«ãããæ¬å½ã«èªä¿¡ããªãéããFRPã使ãã¹ãã§ã¯ãªãã
- FRPã¯ãããã°ã©ã ã®æ³¨æç¹ãå¢ããã決ãã¦æ¸ãããã¨ã¯ãªã: FRPãæ°ããªæ§é ã¨æ¼ç®ãå°å ¥ãã以ä¸ãåé·æ§ããããã解æ¶ã§ãã¦ããæ¬è³ªçãªè¤éãããªããããã§ã¯ãªããå®å ¨ã«ä½¿ãããªãã«ã¯ä»çµã¿ã®ç解ãä¸å¯æ¬ ã§ãããå¤ãã®CFRPã©ã¤ãã©ãªã®ããã«ãå®è£ ãé è½ããã¦ããå ´åã¯ç¹ã«æ°ãã¤ããã¹ãã§ããã
FRPã®ä»£ããã«ãªããããã®
- STM TVarãä¸å¿ã¨ãããç¶æ ãä¿æããæ§é ã«å¯¾ããæä½(ãã©ã³ã¶ã¯ã·ã§ã³)ããSTMã¨ããå°ç¨ã®ã¢ããã§è¨è¿°ããããã©ã³ã¶ã¯ã·ã§ã³ã¯ç¶æ ãæ´æ°ãããæã«å®è¡ãããããã«ã§ãããªã¢ã¯ãã£ããªåä½ã表ç¾ã§ããã並è¡å¦çã¨ã®è¦ªåæ§ãé«ãã®ãé åã§ããã
- ã¹ããªã¼ã å¦çã©ã¤ãã©ãª å ¥åºåã¨ç¶æ ãæ±ãã¨ããé¢ã§ã¯FRPã¨ã®å ±éç¹ãå¤ããä½ããã¡ã¤ã«ããããã¯ã¼ã¯ãªã©ã«éå®ããå¿ è¦ã¯ãªããã¢ããªã±ã¼ã·ã§ã³ã®ãã¸ãã¯ãã¹ããªã¼ã å¦çã©ã¤ãã©ãªã§è¨è¿°ããã®ãã¢ãªã ã
- objective objectiveã¯Haskellã§ãªãã¸ã§ã¯ãæåãå®ç¾ããã©ã¤ãã©ãªã§ããããªãã¸ã§ã¯ãã¯èªç¶å¤æã¨ãã¼ãªãã·ã³ã®æ§è³ªãä½µãæã£ã¦ãããç¢ç§ã®FRPã®çºå±å½¢ã¨ãã¦ãè¦ããã¨ãã§ãããFRPã¨åæ§ãå®æãªä½¿ç¨ã¯ã¢ã³ããã¿ã¼ã³ã§ããã
ç·è©
ç§ã¯ä»äºã§å¤å ¸çFRPã使ã£ãã³ã¼ããä¿å®ãã¦ãããã(ç§ããè¦ã¦)éå°ã«ä½¿ããã¦ãããããããªãã®éè·ã§ãããå°ãã§ãFRPãæ¸ããã¦ä¿å®æ§ãé«ããã¹ããç¹ç¶å¼·åãã©ã¹ããã¯ã®ãã¨ã決æãåºããã