継続周辺の概念を俯瞰する
#俯瞰
継続はEffect系の理論の基礎になっている(?)割に、理解するまでに必要な道筋が多くて難しい
見る記事によって用語の揺れがあることもあるので、その辺も調整しつつ語彙の俯瞰をする
概念Aの説明に概念Bが出てきて、というのが連鎖しがちなので、順序をある程度定めておきたい
継続以前の基本的な用語
Call Stack
関数が呼び出されるときに積まれるスタック
末尾呼び出し
文字通り、関数の末尾で何らかの関数を呼び出すこと
末尾呼び出しの除去
文字通り、末尾呼び出しを除去し、コールスタックの積み上げを防ぐ
末尾再帰
末尾呼び出しの特に再帰になっているケース
末尾再帰の最適化
末尾再帰での、末尾呼び出しの除去
普通の末尾呼び出しの除去と異なり、Loopに変換できたりする
継続の勉強のためにGaucheに慣れる
Gauche
Lisp方言の一つであるSchemeの処理系
Gauche(あるいは他のLisp)の構文に慣れておく
Gaucheを知らなくても継続を学ぶことは出来るが、言語仕様として継続を要請していることもあり、Gaucheでの資料が多い
Lips構文がわからないとそういう資料も読めない
ここに良い説明があった
継続を学ぶ際にSchemeが適している理由など
S式とラムダ計算の調和性
動的型である
副作用を持つ
global変数に継続を代入する、などができる
継続の概念の基礎
継続
処理全体を時間軸で見た時に、ある点における継続とは、「その点以降の処理のこと」
継続渡し
継続を受け取って継続を返すような関数の書き方
CPS
全ての関数呼び出しを継続渡しにしたもの
継続渡しの特殊版
対概念は直接スタイル
CPS変換
直接スタイルをCPSに変換すること
call/cc
プログラミングGaucheの19章のコードをHaskellで書き直す
理解のために書いたやつmrsekut.icon
限定継続
「部分継続」も同じものを指す
継続はその点以降の未来全てを扱うが、それでは範囲が広すぎるのでもう少し範囲を狭めた未来を取り扱う
「区切りを入れる関数」と「部分継続を取り出す関数」の2つの関数で操作する
代表的なものがresetとshift
『shift/resetプログラミング入門』が非常にわかりやすいので最初に読むと良さそう
継続の応用
圏論に繋げる
論理学に繋げる
Curry-Howard同型対応
CPSと直観主義論理の関係
#WIP
CPSを使って再帰を末尾再帰に変換する
http://www.nct9.ne.jp/m_hiroi/func/abcscm20.html#:~:text=再帰呼び出しと継続渡しスタイル
CPS の便利な使い方
http://www.nct9.ne.jp/m_hiroi/func/abcscm20.html#:~:text=CPS%20の便利な使い方
https://blog.miz-ar.info/2022/05/lunarml-and-continuations/
継続利用の具体例
これ駆動で見ていくと良いかもしれない
継続モナド
lispに慣れていないhaskellerなら、むしろこっちでやった邦画で理解できるのでは?を検証するmrsekut.icon
なんか微妙な気もする
ノイズが多い感じがある
Trampolining
畳み込み(fold)/展開(unfold)
Algebraic Effects and Handlers
米田の補題
async/await
Recursion Scheme
/mrsekut-b/Gaucheにおける継続の解説
#??
けっきょく継続って何がすごいのか?
「HaskellにおけるMonad」と同じように、言語仕様に組み込まれていることで、
「構文を追加せずにいろいろなことができる」が実現できる、ということ?
ref モナドがあって何が嬉しいのか
まあ大体そんな感じだと思うmrsekut.icon
継続というかcall/ccがすごすぎる
この記事はcall/ccがなんでもできすぎるので逆に良くないみたいな感じのことが書かれていて、最初の2パラグラフぐらい読んだらcall/ccが抽象化としてすごいことがわかる
call/ccで取れる継続ってどこから以降の継続?
その関数のbodyは含まれるの?
例えばこういう時、cont-procには何が入るのか
このコードではcont-procの内容をreuseに代入しているので確認できる
code:aa
(define reuse #f)
(define (f x y) (* x y))
(define (g x) (+ x 100))
(define (h x) (* x 100))
(h
(call/cc
(lambda (cont-proc)
(let ((v (f 3 10)))
(set! reuse cont-proc)
(g v)
))))
(reuse 10) ; 1000
hのbodyが入ってるっぽい。
ここではreuseはhと全く同じ挙動をする
coroutine
https://mametter.hatenablog.com/entry/2019/03/27/211140
コルーチンの上位互換が継続
↓リンクがやたら多いのは、「どの文章が良い資料なのか」が不明なので、見つけたやつとりあえず全部コピっているからmrsekut.icon
教科書的な資料があれば、こんなメモは不要なんだが
http://lotz84.github.io/haskell/continuation.html
https://blog.miz-ar.info/2022/05/lunarml-and-continuations/
https://blog.miz-ar.info/2022/05/delimited-continuations-and-exceptions-and-monad/
Representing monad
継続
例外の一般化?
どのへんが?
コルーチンの上位互換?
どのへんが?
gotoと同じ?
継続を明示的に扱う方法
全てのプログラムを継続渡しで書く
call/ccを使う
async/awaitとの関係
https://keens.github.io/blog/2019/02/09/async_awaittogouseikanousei/
https://qiita.com/yaegaki/items/40d74b96163419ec29cf
http://foobar.hatenablog.com/entry/expressiveness-of-async-await
継続
/herp-technote/継続
https://archive.jlongster.com/Whats-in-a-Continuation
https://podhmo.hatenadiary.org/entry/20110723/1311432281
http://ziphil.com/diary/mathematics/55.html
λμ-計算
http://ziphil.com/diary/mathematics/54.html
λμ-計算
λc-計算
http://ziphil.com/diary/mathematics/53.html
https://keens.github.io/blog/2019/06/27/keizokutokanowadaisa_bei/
https://nymphium.github.io/2019/07/21/effect_cont.html
https://www.fos.kuis.kyoto-u.ac.jp/~igarashi/CoPL/chap11.pdf
https://www.cis.upenn.edu/~plclub/blog/2020-05-15-Defunctionalize-the-Continuation/
http://ziphil.com/diary/mathematics/52.html
https://wiki.haskell.org/Continuation
CPS
https://m-hiyama.hatenablog.com/entry/20080116/1200468797
http://www.nct9.ne.jp/m_hiroi/func/haskell38.html
https://www.is.s.u-tokyo.ac.jp/vu/jugyo/processor/process/soft/compilerresume/coverq3/coverq3.html
https://en.wikibooks.org/wiki/Haskell/Continuation_passing_style
https://fumieval.hatenablog.com/entry/2015/03/20/155326
http://logic.cs.tsukuba.ac.jp/jikken/cps.html
https://kzono.hatenablog.com/entry/2018/01/02/113735
https://bleis-tift.hatenablog.com/entry/rec2while
https://yuzumikan15.hatenablog.com/entry/2015/04/24/094610
http://blog.sigfpe.com/2008/12/mother-of-all-monads.html
https://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-Cont.html
https://en.wikibooks.org/wiki/Haskell/Continuation_passing_style
https://bitbucket.org/product/
https://jutememo.blogspot.com/2011/05/haskell-cps.html
https://hackage.haskell.org/package/mtl-c
http://aherrmann.github.io/programming/2016/01/04/resource-management-in-haskell/
https://myuon-myon.hatenablog.com/entry/2016/05/11/215734
https://teh.id.au/#/posts/2017/05/10/lambdajam-slides/index.html
http://www.cbdumas.com/posts/church_encoding.html
http://www.cbdumas.com/posts/church_encoding2.html
https://free.cofree.io/2020/01/02/cps/
https://www.cis.upenn.edu/~plclub/blog/2020-05-15-Defunctionalize-the-Continuation/
https://www.slideshare.net/RuiccRail/ss-52718653
CPS変換
https://qiita.com/fgborges/items/ffe0e50ad630b434c625
http://pllab.is.ocha.ac.jp/~asai/jpapers/ppl/sou17.pdf
https://zehnpaard.hatenablog.com/entry/2020/07/05/180439
https://keens.github.io/blog/2019/12/07/rustdecpshenkangakantanninattayotoiuhanashi/
http://fumieval.hatenablog.com/entry/2014/02/11/205916
http://junology.hatenablog.com/entry/20120403/1333467125
call/cc
手続きの定義時と、呼び出し時の両方で使うケースがある
これは区別したほうが理解しやすいかもしれない、しらんけど
Contモナドとcall/cc - ゴバブログ
Continuation モナド
http://mayah.jp/article/2004/continuation/
https://sumii.hatenablog.com/entry/20061121/p1
https://lemniscus.hatenablog.com/entry/20100210/1265802932
直観主義論理
Curry-Howard同型対応
http://www.madore.org/~david/computers/callcc.html
https://en.wikipedia.org/wiki/Call-with-current-continuation
https://twitter.com/gengar68/status/1258777260057694209
https://qiita.com/Kai101/items/eae3a00fcd1fc87e25fb
python
再実装できるの #??
http://practical-scheme.net/wiliki/wiliki2.cgi?Gauche%3Acall%2Fcc%E3%81%AE%E4%BD%BF%E7%94%A8%E4%BE%8B
http://blog.practical-scheme.net/shiro/20161222-callcc
https://mjt.hatenadiary.com/entry/20180403/p1
https://sumii.hatenablog.com/entry/20061121/p1
https://stackoverflow.com/questions/612761/what-is-call-cc
http://www.nct9.ne.jp/m_hiroi/func/abcscm20.html#:~:text=次は%20Scheme%20の「継続%20(continuation)」
https://practical-scheme.net/wiliki/wiliki.cgi?Scheme%3A使いたい人のための継続入門
https://ja.wikibooks.org/wiki/Scheme/継続の種類と利用例
http://proofcafe.org/ocaml-nagoya/index.php?%B7%D1%C2%B3
https://jutememo.blogspot.com/2013/04/haskell-callcc.html
http://www.shido.info/lisp/scheme_cc.html
https://ja.wikibooks.org/wiki/Scheme/継続の種類と利用例
call/ccによる継続はいわばgoto ref
どういうこと?
goto文は、継続に実行を移すこと、と捉えればまあそうか
継続モナド
https://qiita.com/tanakh/items/81fc1a0d9ae0af3865cb
リソース管理と継続モナド
めちゃくわしい
普通の継続の話や他のモナドの話もある
https://speakerdeck.com/inamiy/iosdc-japan-2021?slide=73
スライドだけでは分かりづらいかもだが、ここでもリソース管理の話をしている
https://speakerdeck.com/aoiroaoino/scala-niokeruji-sok-monadofalseshi-zhuang-tohuo-yong
scala
https://terazzo.hatenadiary.org/entry/20120727/1343400811
https://qiita.com/sgmryk/items/cb274102cb1062c9158d
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.43.8213
https://www.slideshare.net/RuiccRail/ss-52718653
https://postd.cc/monads-in-javascript/#continuation
http://blog.ezyang.com/2010/02/nested-loops-and-continuation/
https://maxhallinan.com/posts/2019/10/22/how-does-the-continuation-monad-work/
https://blog.poisson.chat/posts/2019-10-26-reasonable-continuations.html
https://wiki.haskell.org/MonadCont_under_the_hood
https://ro-che.info/articles/2019-06-07-why-use-contt
https://www.reddit.com/r/haskell/comments/ahu6jp/fun_fact_the_continuation_monad_cont_r_a_has_an/
https://hexagoxel.de/postsforpublish/posts/2018-09-12-cont-part-two.html
https://hexagoxel.de/postsforpublish/posts/2018-09-09-cont-part-one.html
https://haskell.e-bigmoon.com/posts/2018/06-26-cont-param.html
https://twitter.com/cdepillabout/status/972515871301120000
https://begriffs.com/posts/2015-06-03-haskell-continuations.html
https://qiita.com/sparklingbaby/items/2eacabb4be93b9b64755
https://speakerdeck.com/aoiroaoino/scala-niokeruji-sok-monadofalseshi-zhuang-tohuo-yong?slide=2
https://haskell.jp/blog/posts/2019/fallible.html
https://twitter.com/hexirp_prixeh/status/1217826511601844224
https://fits.hatenablog.com/entry/20121104/1351997814
限定継続
『shift/resetプログラミング入門』2.7ぐらいから再読する
https://okmij.org/ftp/continuations/against-callcc.html
by Oleg Kiselyov
call/ccは扱いづらく危険
言語処理系は限定継続をprimitiveに提供すべき
限定継続は継続よりprimitiveなもの?
限定継続があれば継続をエミュレートできる?
https://keigoi.hatenadiary.org/entry/20181205/ochacaml2
https://keigoi.hatenadiary.org/entry/20120911/ochacaml
https://keigoi.hatenadiary.org/entry/20100627/1277610625
https://keigoi.hatenadiary.org/entry/20100627/1277599932
chrome-extension://oemmndcbldboiebfnladdacbdfmadadm/http://pllab.is.ocha.ac.jp/~asai/papers/contfest08slide.pdf
https://miyakawataku.hatenablog.com/entry/20170323/1490281499
https://practical-scheme.net/gauche/man/gauche-refj/Bu-Fen-Ji-Sok-.html
https://en.wikipedia.org/wiki/Delimited_continuation
https://nymphium.github.io/2018/07/19/delimited-continuation%E3%81%AE%E5%A4%8F.html
https://uid0130.blogspot.com/2013/04/shiftreset.html
https://qiita.com/dico_leque/items/060135fb4850daf9a2fc
composable continuation
http://community.schemewiki.org/?p=composable-continuations-tutorial
https://reinyannyan.hatenadiary.org/entry/20090623/p1
Symmetric Lambda Calculs
http://lotz84.github.io/haskell/continuation.html
米田埋め込み
http://lotz84.github.io/haskell/continuation.html
論理学での継続
http://lotz84.github.io/haskell/continuation.html
Loan Pattern
Coroutineモナド
fold
https://jutememo.blogspot.com/2011/05/haskell-cps.html#:~:text=6.%20リストの平坦化
7も
理解してないときに書いてた謎のメモ
同期的なCPS
code:ts
const add = (a: number, b: number, cb: (r: number) => void) => {
cb(a + b);
};
add(1, 2, r => console.log(r));
非同期CPS
code:ts
const add = (a: number, b: number, cb: (r: number) => void) => {
setTimeout(() => {
cb(a + b);
}, 100);
};
console.log("beofre");
add(1, 2, r => console.log(r));
console.log("after");
setTimeoutが「非同期CPS」として実装されているから、非同期CPSとして定義できてる
#??
非同期処理でもないのにCPSを使うことの嬉しさがわからない
code:hs
-- 和を求める
add :: Num a => a -> a -> a
add x y = x + y
-- addのCPS版
addCPS :: Num a => a -> a -> (a -> result) -> result
addCPS x y cont = cont (x + y)
main = do
show $ add 1 2
addCPS 1 2 $ \sum -> show sum
この例だとCPSがないバージョンのほうが理解しやすいし簡潔
最初CPSのメリットは、ライブラリが提供する関数(ここでは例えばaddやaddCPS)について「返り値をごにょれる」というのがあるのか、と思ったが、
別にCPSを使ってなくても上のコード例のようにできる、し、そっちのほうが自然
何が嬉しい
関数呼び出し後の処理を関数側が制御できる
具体的には?mrsekut.icon*6
最適化
全ての継続をインライン展開することで、関数呼び出しのオーバヘッドを消せる
Schemeでこれをやってるんだっけ、たしかmrsekut.icon
スタック使用量も減らせる
CEK抽象機械
https://zehnpaard.hatenablog.com/entry/2022/03/17/201958
https://zehnpaard.hatenablog.com/entry/2022/03/18/200749
#??
そもそもCPSを使って嬉しいのはどういうときか
ex. 非同期処理をやるとき
ex. ライブラリを実装するとき
同期処理でわざわざコールバック関数もしくはCPSを使う利点 ってあるの?
非同期処理ではわかる
CPSの関数定義者側の利点、関数利用者側の利点
CPSの問題点は、だいたいコールバック地獄と同じか
コールバック関数の方が継続を包含する概念なので、コールバック関数の問題は同時にCPSの問題となり得る
algebraic effectsの記事で出てきた再開可能性は継続でできるんだっけ #??
try..catchでは一度throwしてしまうと、元の場所には戻れないが、、のやつ