ãã8æ1æ¥ãè¨ç®æ©ç§å¦ã®æ ¹å¹¹ãæºãããããã¹ã³åé¡ãåºé¡ãããã¨ã³ã¸ãã¢ãã¡ã¯éæ¼ããï¼æå³: é¢ç½åé¡ãåºãã®ã§ããªãã¹ããã³ãã³ãªè§£æ³ã使ã£ã¦å·±ã®æå·§ãèªç¤ºããããã«ã¨ã³ã¸ãã¢ãã¡ã¯ç«¶ã£ã¦ã³ã¼ããæ¸ãã¯ãããï¼ã
ãåé¡ãé å{"ãã","ã¹ã³"}ããã©ã³ãã ã«è¦ç´ ãæ¨æºåºåãç¶ããããã®ä¸¦ã³ããããã¹ã³ã¹ã³ã¹ã³ãã3åç¹°ãè¿ãããã®ãã«ä¸è´ããã¨ãã«ãã©ãæ³¨å ¥â¡ãã¨æ¨æºåºåãã¦çµäºããããã°ã©ã ãä½æãã(é ç¹:5ç¹)
— ((ð++)) (@Sheeeeepla) 2022å¹´8æ1æ¥
ããã§ãé¢æ°åãã¯ããã¯ããªãã¨ãããããã 解æ³ãä½ã£ãã®ã§ããã«ç¤ºãã
import higherkindness.droste.Coalgebra import higherkindness.droste.data.list.{ListF, ConsF, NilF} import higherkindness.droste.scheme val ds = () => scala.util.Random.nextInt(2) val dss = "ã¹ã³" :: "ãã" :: Nil val dodosukoCoalgebra = Coalgebra[ListF[Int, *], (Int, Int)] { case (_, 2184) => NilF case (b, st) => print(dss(b)); ConsF(b, (ds(), st << 1 & 4095 | b)) } val dodosukoAnamorphism = scheme.ana(dodosukoCoalgebra) def injectLove() = println("ã©ãæ³¨å ¥â¡")
å®è¡ãã:
dodosukoAnamorphism(ds() -> Nil); injectLove()
ã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ããããããã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããããããã¹ã³ããã¹ã³ããã¹ã³ããããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ããããããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããããã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããããã¹ã³ã¹ã³ããããããããã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ã¹ã³ããããããã¹ã³ããããããããããã¹ã³ããããã¹ã³ããããããã¹ã³ããããããããããããããã¹ã³ã¹ã³ããããã¹ã³ããã¹ã³ããã¹ã³ã¹ã³ããããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããããããããããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ããããããããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ããããã¹ã³ã¹ã³ã¹ã³ããããããããããã¹ã³ã¹ã³ã¹ã³ããããããã¹ã³ããã¹ã³ã¹ã³ããããããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ããããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ããããããã¹ã³ã¹ã³ã¹ã³ããããã¹ã³ããããããããããã¹ã³ããããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããããããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ããããã¹ã³ã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããããã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ããããã¹ã³ã¹ã³ããããããããã¹ã³ããã¹ã³ã¹ã³ããããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããããããããããããã¹ã³ããã¹ã³ããã¹ã³ããã¹ã³ããããããã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããããããããã¹ã³ã¹ã³ã¹ã³ããããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ããããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããããããã¹ã³ããã¹ã³ã¹ã³ããããã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ããããããã¹ã³ããã¹ã³ããããããã¹ã³ããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ããããããã¹ã³ããã¹ã³ããããããã¹ã³ããããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããããã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããããã¹ã³ããããã¹ã³ããããããããã¹ã³ããã¹ã³ããã¹ã³ããããã¹ã³ããã¹ã³ã¹ã³ããããã¹ã³ããããã¹ã³ã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããããããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ããããã¹ã³ããã¹ã³ã¹ã³ããããã¹ã³ã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ããããã¹ã³ããããã¹ã³ã¹ã³ããããããã¹ã³ããã¹ã³ã¹ã³ããããã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ããã¹ã³ããããã¹ã³ã¹ã³ããããããããã¹ã³ã¹ã³ããããããã¹ã³ã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ããããããããããã¹ã³ããããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããããããã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ããã¹ã³ããããã¹ã³ããã¹ã³ããããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããããããã¹ã³ã¹ã³ããããããããããããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ã©ãæ³¨å ¥â¡
å®é¨ã¯æåã ï¼
ã¡ãªã¿ã«ä¾åã©ã¤ãã©ãªçã®ãã«ãè¨å®ã¯ãã®éãã
// build.sbt val sv = "2.13.8" addCompilerPlugin( "org.typelevel" % "kind-projector" % "0.13.2" cross CrossVersion.full ) lazy val root = project .in(file(".")) .settings( name := "recursion-scheme-exercise", version := "0.1.0-SNAPSHOT", scalaVersion := sv, libraryDependencies ++= Seq( "org.typelevel" %% "cats-core" % "2.8.0", "io.higherkindness" %% "droste-core" % "0.9.0" ) ) javaOptions := Seq("-Xss256m")
Recursion Schemeã表ç¾ããã©ã¤ãã©ãªã¨ãã¦Drosteã使ã£ãã
å®è£ 解説
ãã®ããã¹ã³ã¢ã«ã´ãªãºã ã¯ãRecursion Schemeã¨ããå帰å¦çãæ½è±¡åããæ¦å¿µããã¾ãå©ç¨ãã¦è§£ãã¦ãããRecursion Schemeèªä½ã®è§£èª¬è¨äºã¯ãã®ãã¡æ¸ãã¨ãã¦ãããã¹ã³ã¢ã«ã´ãªãºã ãæ§æããããã¤ãã®è¦ç´ ã«çç®ãã¦ã¿ããã
ããã¹ã³ã¢ã«ã´ãªãºã ã¯ã以ä¸ã®è¦ç´ ããæ§æããã¦ãã:
- å¼ã³åºãã¨ã©ã³ãã ã«1ã¾ãã¯0ãè¿ã
ds
ã¹ã³
ã¨ãã
ããã®é ã«æ ¼ç´ããã¦ããdss
ãªã¹ãds
ãå¼ã³åºãç¶ããããã«å¯¾å¿ãããã
ãããã¯ã¹ã³
ã表示ãç¶ããä¸å®ãã¿ã¼ã³ï¼ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³
ï¼ãåºåããæã«çæãä¸æ¢ããdodosukoCoalgebra
- Coalgebraã¨ã¯ä½ä»£æ°ã®ãã¨ã ããã¹ã³ä½ä»£æ° ã¨å¼ã¶ãã¨ã«ãããã
dodosukoCoalgebra
ãå©ç¨ããåæç¶æ ãä¸ãã¦å帰å¦çãæ å½ããdodosukoAnamorphism
- Anamorphismï¼
ana
ï¼ã¨ã¯ãããã¾ãã«è¨ãã°unfold
ãä¸è¬åãã¦ã©ããªãã¼ã¿æ§é ã«å¯¾ãã¦ã使ããããã«ãããã®ã ããã¹ã³ã¢ãã¢ã¼ãã£ãºã ã¨çºé³ããã
- Anamorphismï¼
- ã©ãã注å
¥ãã
ã©ãæ³¨å ¥â¡
ã¨ç»é¢ã«åºåããinjectLove()
ãã®5ã¤ã®ã³ã³ãã¼ãã³ãã«ãã£ã¦ããã¹ã³ã¢ã«ã´ãªãºã ã¯åä½ããã
ããã¹ã³ãã¿ã¼ã³ã®æ¤åº
ä¸è¬ã«ãCoalgebraã¯å®éã®æ¬¡ã®å¤ãè¨ç®ããå帰ï¼ã¤ã¾ãçæå¦çï¼ãç¶è¡ããããããã§ãã¼ã¿æ§ç¯ãå®äºããããé¸æã§ãããããã¦Anamorphismã¯å¾ãããå¤ãå度次ã Coalgebraã«éãè¾¼ã¿ãå帰ã®é¢åãªç®æã ããã£ã¦ãããã
å ·ä½çã«ãããã¹ã³ä½ä»£æ°ã¯ã以ä¸ã®ããã«ãã¦å帰ãå®äºãããã©ãããå¤å®ãã¦ãã:
ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³ããã¹ã³ã¹ã³ã¹ã³
ã¯ãããã0ã«ãã¹ã³ã1ã«å²ãå½ã¦ããã¨ã§ãã¤ããªè¡¨ç¾ï¼ä»¥ä¸ã ããã¹ã³ãã¿ã¼ã³ ï¼ã«ã§ãããããã¯10é²æ°ã§ã¯2184
- ã¹ããããã¨ã«ç¶æ
st
ï¼Int
åï¼ãæ´æ°ãã次ã®ã¹ãããã«ãå¼ãç¶ããst
ã¯ãç¾å¨ã®ã¹ãããã§å¾ãds
ã®è¿ãå¤ãst
ã®ä¸çªå³ã«æ¿å ¥ããããã«ããã¹ã³ãã¿ã¼ã³ã®å¹ ã§ãã12ãããåã®å¹ ã§ãã¹ã¯ãã- ããå
·ä½çã«ã¯ã
st
ã1ãããå·¦ã«ã·ããããst | ds()
ã«ãªãããã«ãããåãåãã12ãããåã1ã«ãªã£ã¦ãããã¹ã¯ï¼ããã¹ã³ãã¹ã¯ï¼ã§ãããç©ãåã - ããã¹ã³ãã¹ã¯
0b111111111111
ã¯10é²æ°ã§ã¯4095
ã«ãªã
- ããã¹ã³ãã¿ã¼ã³ãå®æããï¼
st
ãããã¹ã³ãã¿ã¼ã³ã«ãªã£ãï¼ã¨ããå帰ã¯çµäºãã
ããããã®è¡¨ç¾ã®ä¸ã«æ¼ãè¾¼ãããã¦ãã:
// æçµçã«List[Int]ãçæãããå帰ããã¨ãã次ã®ã¹ãããã«ã¯(Int, Int)ã渡ã val dodosukoCoalgebra = Coalgebra[ListF[Int, *], (Int, Int)] { // stã2184ã«ãªã£ãã¨ããããã¹ã³ãã¿ã¼ã³ãå®æãã¦ããã®ã§ããã§å帰ãçµãã case (_, 2184) => NilF // stã2184ã§ã¯ãªãã¨ã // ç¾å¨ã®ç¶æ ããããã¹ã³ãã表示ãã // ããã¦ããã¹ã³ãã¿ã¼ã³ãå®æãããããã«ç¶æ ãæ´æ°ãã case (b, st) => print(dss(b)); ConsF(b, (ds(), st << 1 & 4095 | b)) }
ãã®ããã¹ã³ä½ä»£æ°ã«ã¯å帰èªä½ã®å¦çã¯å«ã¾ãã¦ããªããä½ä»£æ°ã§ã¯ãã©ããã¼ã¿ãæ§ç¯ãããã ããèããã°è¯ãããã«ãªã£ã¦ãããå帰èªä½ã®å¦çã¯scheme.ana
ã«éé¢ããã¦ããããã ã
Recursion Schemeãããã
åé ã§ãå帰å¦çãæ½è±¡åãããã®ãRecursion Schemeã ã¨æ¸ãããå
ç¨ä½¿ã£ãana
ãRecursion Schemeã®ä¸å¡ã ãana
ãã¯ããã¨ãã¦ãRecursion Schemeã«ã¯æ§ã
ãªç¨®é¡ãåå¨ãã:
- ãã¼ã¿ç³ã¿è¾¼ãã§ããç³» --
Algebra
ã使ãcata
--fold
ã®ä¸è¬åpara
--cata
ã«å ãã¦ããã£ãã¾ã§è¦ã¦ããå ¨ã¦ã®ãã¼ã¿ãåç §ã§ãã
- ãã¼ã¿æ§ç¯ãã¦ããç³» --
Coalgebra
ã使ãana
--unfold
ã®ä¸è¬åapo
--ana
ã«å ãã¦ãå帰éä¸ã§å¼·å¶çã«å¥ã®å¤ãå帰çµæã¨ãã¦è¿ããã¨ãã§ãã
- ã³ã³ãæ
hylo
--cata
ãã¦ããana
ãã
- etc...
Recursion Schemeã®ç¨®é¡ã¯ç³ã¿è¾¼ãç³»ã¨å±éããç³»ã¨ã«ããã¾ãã«åããã¦ãããããã¦å®éã«ã©ã®ãããªåé¡ã解ããããã¯ãå¥éAlgebra/Coalgebraã§å®è£ ããæçµçã«çµã¿åãããã¨å帰ãã§ãããã¨ããä»çµã¿ã«ãªã£ã¦ãããå¿ç¨ã¨ãã¦ãä¾ãã°ãã£ããããæ°åã®ãããªå帰çæ°åãRecursion Schemeã®çµã¿åããã§è§£ããã¨ãã§ããï¼Drosteã®READMEãè¦ã¦ã»ããï¼ã
ãã£ã²ãï½ãå帰èªä½ã®å¦çã¨åãæ±ãåé¡èªä½ã¨ãåé¢ããã¦ãã®ãã§ã¼ããããã¼ï¼ã¨æããåã¯æè½ããããã以ä¸ã®ã¹ã©ã¤ããèªããï¼
âãããä¸çªãªã¹ã¹ã¡ã ï¼ï¼ï¼
âããã¯ãã¼ãã·ã¼ãã ï¼ï¼ï¼
æ¬ ç¹
æ«å°¾å帰ãèããå®è£ ãã && ããããããå帰ãã¦ããã®ã§ããã¾ã«Stack Overflowãã¦ãã¾ããã¨ããããããã¼ãããã«ããã¦ãã¾ããã¦ãããã©ãã«ã¯å±éºãä»ãç©ã ã
ã¾ããShortã§ããå ´æãIntã§å®è£ ãã¦ãããããã¯ã³ã¼ããåé·ã«ãªãã®ãåé¿ããããã§ãShortã使ãã¨ãããªãã«ã¡ã¢ãªå¹çãè¯ããªãã¯ãã ã
ææ³
ããã¹ã³åé¡ãè¦ã¦ããã¨ãããã®ãRecursion Schemeã§æ¸ãããã ãªï½ã¨ãããã¨ã¯åãã£ã¦ãããã®ã®ããã®ããã®Scalaã®ã©ã¤ãã©ãªã§ããDrosteã®ä½¿ãæ¹ãããããåããããææ¢ãã§åå¼·ãã¦ããã5æ¥ãçµéãã¦ãã¾ã£ãããæ®éã«Drosteã使ããããã«ãªã£ã¦ãã¾ã£ãã®ã§ã¾ãçµæãªã¼ã©ã¤ã ãªï¼
ãã¨ã§æ¸ã
ãã®è¨äºã§æ¸ããªãã£ããã¨ï¼ãã¨ã§æ¸ãï¼:
Fix
㨠Pattern Functor- Algebra / Coalgebraã¨ã¯
- Recursion Schemeã®è¸ã¿è¾¼ãã 解説
- åschemeã®ãããã£ã±ãªä½¿ãæ¹ï¼è¨èªã¯Scala ã©ã¤ãã©ãªã¯Drosteã®å ´åï¼
æ¸ãã