F#重写计算表达式

时间:2018-12-17 17:52:57

标签: functional-programming f# continuations computation-expression

我正在研究延续性,因为我想对协程进行一些有趣的使用...无论如何,我想更好地理解我发现的一种实现。

为此,我想重写实现而不使用计算表达式(连续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 answerthis以及其他几本有关this的文章,但是重写这种连续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#重写计算的所有规则吗?)

1 个答案:

答案 0 :(得分:3)

您的Put版本几乎正确。不过有两个问题:

  • bindK函数正在向后使用,需要交换参数。
  • task应该传递Cont<_,_> -> Cont<_,_>,而不是unit -> Cont<_,_> -> Cont<_,_>

解决这些问题可能看起来像这样:

    member this.Put(task) =
        let withYield =
            bindK
                (fun () ->
                    if tasks.Count <> 0 
                    then tasks.Dequeue()
                    else returnK ())
                (callcK (fun exit ->
                    task (
                        callcK (fun c ->
                            tasks.Enqueue(c())
                            exit ()))))
        tasks.Enqueue(withYield)

当然不是太优雅。 使用bind时,最好声明一个运算符>>=

let (>>=) c f = bindK f c

那样

  • do!表示将>>= fun () ->放在
  • 之后
  • let! a =表示将>>= fun a ->放在
  • 之后

然后您的代码会看起来更好一些:

    member this.Put2(task) =
        let withYield =
            callcK( fun exit ->
                    task( callcK (fun c ->  
                        tasks.Enqueue(c())
                        exit())
                    )
                ) >>= fun () -> 
            if tasks.Count <> 0 then
                tasks.Dequeue() 
            else returnK ()
        tasks.Enqueue withYield