F# ã§é«éåã®ã¨ãã¥ã¬ã¼ã·ã§ã³
ãã®è¨äºã§ã¯ãF# 㧠é«éå (higher kinded types) ãã¨ãã¥ã¬ã¼ã·ã§ã³ãããã¨ã«ã¤ãã¦è¨è¿°ãã¾ãã
æ¬å½ã¯ 函数型なんたらの集い 2014 in Tokyo - connpass ã§è©±ããã¨æã£ã¦ããã®ã§ãããéå¶ãèããã¨çºè¡¨ã§ããæ°ãããªããªã£ã*1ã®ã§ãè¨äºã«ãã¦ãè¶ãæ¿ãã¾ãã
注æäºé
- F# ã§æ¨å¥¨ãããå®è£ ãã¬ã¤ãã©ã¤ã³ããé¸è±ããå®è£ ã«ãªã£ã¦ãã¾ã
- åå®å ¨ãã©ããã¯èå¯ãã¾ãã(æéã足ããªã)
- ãã®è¨äºãã¿ãã ãã§"F# ã¯æ®å¿µãªè¨èª"ã¨ããå¤æãä¸ãã®ã¯èª¤ãã§ã
- å°é家ã§ã¯ãªãã®ã§ãå³å¯ãªãã¨ã¯æ¸ãã¾ãããçæããç®ã§è¦å®ã£ã¦ãã ããã
- Haskell, Scala(z), Java ã®ã³ã¼ããç»å ´ãã¾ã
åèæç®?
- highj - Haskell-style type classes in Java - Google Project Hosting
- ocamllabs/higher · GitHub
- xuwei-k/free-monad-java · GitHub
æ¬è¨äºã«ç»å ´ãã FSharp å®è£ ã¾ã¨ã
æ¬è¨äºã§å®è£ ããåã«å¹¾ã¤ãä¿®æ£ã追å ãè¡ã£ãã©ã¤ãã©ãªãä¸è¨ã«ãªãã¾ãã
pocketberserker/FSharp.Karma · GitHub
åé¡ç¹: ãã®ä¸çã«å¥´ã¯ããªã
ä¸ã®ä¸ã«ã¯é«éåã¨ãããã®ãåå¨ãã¾ããé«éåã«ã¤ãã¦ã¯ã
(もりそば)Scalaによる高階型変数 - Higher-kind Generics - ( ꒪⌓꒪) ゆるよろ日記
ã¨ããèªãã§ã¿ã¦ãã ããã
é«éåãåå¨ããªãã¨ã©ãããåé¡ç¹ãããã®ãã¨ãã話ã«ã¤ãã¦ã¯ã
関数を扱えるだけでは、モナドを表現するには不十分過ぎる - scalaとか・・・
ãèªã¿ã¾ãããã
ãã¦ã F# ãé«éåããµãã¼ããã¦ããªãè¨èªã®ä¸ã¤ã§ãã
æ½è±¡åãå ±éåã®ã¿ãèããå ´åãF# ã«ã¯ overload ã"éçã«è§£æ±ºãããåãã©ã¡ã¼ã¿"ã使ãã°ã解決ã§ããé¨åãããã¾ãã ãããã®æ©è½ã使ã£ã¦å®è£ ãããã©ã¤ãã©ãªã¨ãã¦ã¯
ãããã¾ãã
ããããåã³ã³ã¹ãã©ã¯ã¿ã§é«éåãè¦æ±ããããããªåã¯ã©ã¹ã¯ãé«éåããªããã°å®è£ ãå°é£ã§ãã*2
解決æ¡: ãªããªãâ¦ä½ããããã!
åèæç®ã«ããã highj ã higherã free-monad-java ã§ã¯ é«éåãã¨ãã¥ã¬ã¼ã·ã§ã³ããããã®åãç¨æãã¦ãã¾ãã ãã®æ¹æ³ã¯ãF# ã§ãå©ç¨å¯è½ã§ãã æ¬è¨äºã§ã¯ free-monad-java ã®ã¹ã¿ã¤ã«ãæ¡ç¨ãã¦è¨è¿°ãã¦ããã¾ãã
// Scala ã® F[A] ã表ãå type _1<'F, 'A> = interface end
_1
㯠åãã©ã¡ã¼ã¿ãä¸ã¤æã¤é«éåã§ãã
'F
ã¯å¯¾è±¡ã®åãã 'A
ã¯ãã®åããã¤åãã©ã¡ã¼ã¿ã表ãã¾ãã
ä»å㯠highj ã«ããã μ
ã®ä»£æ¿ã¨ãã¦å¤å¥å
±ç¨ä½ãç¨ãã¾ãã
ä¾: Identity
æãç°¡åãª(ããã¦æ¨æºã«åå¨ããªãåã®)ä¾ã¨ãã¦ãIdentityåãå®ç¾©ãã¦ã¿ã¾ãããã
// ååã®å¤å¥å ±ç¨ä½ãç¨æ // highj ã® Id.μ ã free-monad-java ã® Id.z ç¸å½ type Id = Id // å®éã® Id å type Id<'A> = { Value: 'A } with interface _1<Id, 'A>
ã¾ããId åã表ãé«éåç¨ã®å¤å¥å ±ç¨ä½ Id ãç¨æãã¾ãã
次ã«ãå®éã«å©ç¨ãã Id åã« _1
ãå®è£
ãã¾ãã
ãããããã¨ã§ããã®åãé«éåã§ãããã¨ã表ç¾åºæ¥ã¾ãã
ãã¨ã¯ãä¾ãã° Functor ãªã©ãç¨æãã¦ãããã°ã
type Functor<'F> = abstract member Map: ('A -> 'B) * _1<'F, 'A> -> _1<'F, 'B> module Functor = // ä»»æã® Functor ã対象ã«ã§ãã let map (fa: Functor<_>) f a = fa.Map(f, a)
ãã¦ã³ãã£ã¹ãã«ãã£ã¦ããããªãã«æ½è±¡åã§ãã¾ãã
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] module Id = // functor 㯠F# ã®äºç´èªãªã®ã§ä½¿ç¨ã§ããªã let functor_ = { new Functor<Id> with // fa 㯠_1<Id, 'A> ãªã®ã§ãId<'A> ãã¦ã³ãã£ã¹ãå¯è½ member this.Map(f, fa) = (fa :?> Id<_>) |> map f :> _1<Id, _> }
ä¾: Free
ããã¾ã§ã¯ãF# ã 㨠overload 㨠éçã«è§£æ±ºãããåãã©ã¡ã¼ã¿ãç¨ããã°ãã話ã§ããã
ãããã Free Monad ã®å®è£ ã«ã¯é«éåãå¿ è¦ã«ãªãã®ã§ãåãã©ã¡ã¼ã¿ã§ãªãã¨ããªãã¨ã¯éãã¾ããã Free Monad ã«ã¤ãã¦ã¯æ¤ç´¢ãããè²ã ã¨è¨äºãããã®ã§æ¢ãã¦ãã ããã
Haskell å®è£
ã¾ãã¯ãFree Monad ã® Haskell å®è£ ããè¦ã¦ã¿ã¾ãããã ä»å㯠ekmett æ°ã® free ãåèã«ãã¾ãã
f
ãé«éåã«ãªã£ã¦ããã Free åã³ã³ã¹ãã©ã¯ã¿å
㧠Free åãæã£ã¦ãã¾ãã
Scala å®è£
Scala ã§ã¯ Scalaz ã®ãã®ãæåã§ãã
S
ãé«éåã¨ãªã£ã¦ãã¾ãã
Haskell ã§ã® Free åã³ã³ã¹ãã©ã¯ã¿ã«å¯¾å¿ãã Suspend åã³ã³ã¹ãã©ã¯ã¿ãã¿ãã¨
Free ãæã¤ãã¨ããããã¾ãã
_1 ãç¨ãã¦ã¨ãã¥ã¬ã¼ã·ã§ã³
Suspend ã _1 ã使ã£ã¦è¡¨ç¾ããã¨ã次ã®ããã«ãªãã¾ãã
type private Suspend<'F, 'A> (a: _1<'F, Free<'F, 'A>>) = ...
ãã¨ã¯ãã¾ãå ·åã«åãåããã¦ãããã°ãFree Monadã®å®æã§ãã ããã«é¢ãã¦ã¯æ¬è¨äºã®å 容ã§ã¯ãªãã®ã§ããã£ã¨è©³ããç¥ãããæ¹ã¯
JavaでFreeモナドを表現するためのテクニックやexistential type(存在型)の話 - scalaとか・・・
ãèªãã§ãã ããã Java 㨠F# ã§ã®éãã¯
- F# ã¯æ«å°¾å帰æé©åããã
- F# 㯠type erasure ã§ã¯ãªããããåãã©ã¡ã¼ã¿ã®åãç°ãªã£ã¦ãã㨠InvalidCastException
- F# ã«ã¦ãã¨ã¼ã«åæ¨è«ãããã¨ãGosubã®åãã©ã¡ã¼ã¿ã obj ã«æ¨è«ããã¦æ»ã¬(ã®ã§ãçµå±æ示ããªãã¨ã ã)
ãããã§ãã
åé¡ç¹
- mixin ããªãã®ã§ãæ¢åã®åã« 1 ãå®è£ ã§ããªã(Wrapper åã«å¯¾ã㦠1 ãå®è£ ããã¯ãã«)
- æ示çã«ãã£ã¹ãããå¿ è¦ãããã®ã§ããã¦ã³ãã£ã¹ããã¢ãããã£ã¹ãã®åµã«
次åäºå
ãã¿ããããã£ããFree Monadãã§ãã...ã?ã
GitHubãxuwei-k pushed to develop at xuwei-k/free-monad-java operational monadã
ãã¿ããããã³ã!?ã¯ã¤ã«ãã«ã¼ããã³ã!?ã