まとめ : Algebraic Effects and Handlers とか
- [プログラミング言語] Recursion Schemes, Part IV: Time is of the Essence
- explains Recursion Schemes, especially futumorphism and histomorphism
- 2年前に書いた dynamorphism の記事も参照 手前味噌 http://45deg.github.io/rogyAdC2015/
- [プログラミング言語] GitHub - Marwes/haskell-compiler: A mostly functional haskell compiler written in rust
[プログラミング言語] Algebraic Effects and Handlers
-
Algebraic effects and handlers provide a modular abstraction for expressing effectful computation, allowing the programmer to separate the expression of an effectful computation from its implementation.
*1- a more modular alternative to monads という人もいたりする。
- Haskeller にとっては Freer モナドでおなじみですね。
- シンプルな例 (擬似コード、Effでの例 → https://gist.github.com/45deg/055cd32e8723ead25a64d8256f292f03)
-
printName = { name <- readLine; print ("hello " . name) }
という副作用のあるプログラム(関数)があったとする。 - ここで
inputJohn = fun e -> match e with readLine () k -> (k "John")
*2 という handler を定義する。これは副作用を制御する関数 (handler)である。- k は継続で元の呼び出し部に帰る。これを discard すれば呼び出し部には戻らない。
- Multicore OCaml 等だと
continue k "John"
という呼び出しをする。
-
handle printName with inputJohn
のように組み合わせるように記述する。この場合 キーボードの入力はされずに "hello john" という文字列が即座に出力される。 - つまり 副作用のあるコードの表現 (the expression of an effectful computation) とその(副作用の)実際の実装 (its implementation)を分離して記述することが出来る抽象化の手法である。
-
- より詳しい例は GitHub - ocamllabs/ocaml-effects-tutorial: Concurrent Programming with Effect Handlers (CUFP'17) が詳しい (OCaml)。演習問題付き。
- 言語: Eff, Koka, Multicore OCaml など
- Multicore OCaml は fiber (coroutine) が concurrency primitive になってるっぽい https://www.cl.cam.ac.uk/~sd601/papers/multicore_slides.pdf
- 参考文献リスト: GitHub - yallop/effects-bibliography: A collaborative bibliography of work related to the theory and practice of computational effects
- 中でも An Introduction to Algebraic Effects and Handlers (MFPS 2015) は入門記事っぽくておすすめ http://www.eff-lang.org/handlers-tutorial.pdf
- 個人的にはテストの時便利そうだなと思った。( IO 等の mock として handler が有効活用できそう )
*1: Effective Concurrency with Algebraic Effects · KC Sivaramakrishnan
*2:e: effect, readLine : unit -> continuation -> unit