我正在学习continuations,因为我想要对协程进行一些有趣的应用……无论如何,我想更好地理解我找到的一种实现方法。
为了做到这一点,我想重新编写实现方法,而不使用计算表达式(continuation Monad),但我还不能完全做到。
我有这个:
当然它不起作用 :(
(顺便说一下:是否有一些广泛的文档,介绍编译器将计算重写为纯F#所应用的所有规则?)
为了做到这一点,我想重新编写实现方法,而不使用计算表达式(continuation Monad),但我还不能完全做到。
我有这个:
type K<'T,'r> = (('T -> 'r) -> 'r)
let returnK x = (fun k -> k x)
let bindK m f = (fun k -> m (fun a -> f a k))
let runK (c:K<_,_>) cont = c cont
let callcK (f: ('T -> K<'b,'r>) -> K<'T,'r>) : K<'T,'r> =
fun cont -> runK (f (fun a -> (fun _ -> cont a))) cont
type ContinuationBuilder() =
member __.Return(x) = returnK x
member __.ReturnFrom(x) = x
member __.Bind(m,f) = bindK m f
member this.Zero () = this.Return ()
let K = new ContinuationBuilder()
/// The coroutine type from http://fssnip.net/7M
type Coroutine() =
let tasks = new System.Collections.Generic.Queue<K<unit,unit>>()
member this.Put(task) =
let withYield = K {
do! callcK (fun exit ->
task (fun () ->
callcK (fun c ->
tasks.Enqueue(c())
exit ())))
if tasks.Count <> 0 then
do! tasks.Dequeue() }
tasks.Enqueue(withYield)
member this.Run() =
runK (tasks.Dequeue()) ignore
// from FSharpx tests
let ``When running a coroutine it should yield elements in turn``() =
// This test comes from the sample on http://fssnip.net/7M
let actual = System.Text.StringBuilder()
let coroutine = Coroutine()
coroutine.Put(fun yield' -> K {
actual.Append("A") |> ignore
do! yield' ()
actual.Append("B") |> ignore
do! yield' ()
actual.Append("C") |> ignore
do! yield' ()
})
coroutine.Put(fun yield' -> K {
actual.Append("1") |> ignore
do! yield' ()
actual.Append("2") |> ignore
do! yield' ()
})
coroutine.Run()
actual.ToString() = "A1B2C"
``When running a coroutine it should yield elements in turn``()
因此,我想重写Coroutine类的Put
成员,而不使用计算表达式K
。
我当然已经阅读了this和this以及其他几篇关于catamorphisms的文章,但是重写这个续航monand并不像重写Write Monad那样容易...
我尝试了几种方法,这是其中之一:
member this.Put(task) =
let withYield =
bindK
(callcK (fun exit ->
task (fun () ->
callcK (fun c ->
tasks.Enqueue(c())
exit ()))))
(fun () ->
if tasks.Count <> 0
then tasks.Dequeue()
else returnK ())
tasks.Enqueue(withYield)
当然它不起作用 :(
(顺便说一下:是否有一些广泛的文档,介绍编译器将计算重写为纯F#所应用的所有规则?)
bindK
函数中倒置了参数顺序,先是m
再是f
,这样它的签名与构造器相同。你给出的Put
版本虽然编译通过,但测试无法编译,因为yield'
应该是(unit -> K<unit,unit>)
。 - saboterolet bindK m f = ...
。 不过,yield'
应该是(unit -> K<unit, unit>)
。 - saboterodo! yield' ()
更改为do! yield'
,然后它就起作用了。 - AMieres