【问题标题】:Pause Monad - What should the monadic type look like?Pause Monad - monadic 类型应该是什么样的?
【发布时间】:2015-03-20 16:08:31
【问题描述】:

我看到的典型 Pause monad 实现如下所示(基于 Giulia Costantini 和 Giuseppe Maggiore 的 Friendly F# 中的第 5 章)。

open System

type Process<'a> = unit -> 'a Step
and Step<'a> =
| Continue of 'a
| Paused of 'a Process

type PauseMonad () =
    member this.Return x = fun () -> Continue x
    member this.ReturnFrom x = x
    member this.Bind (result, rest) =
        fun () ->
            match result () with
            | Continue x -> rest x ()
            | Paused p -> Paused (this.Bind (p, rest))

let yield_ () =
    fun () ->
        Paused (fun () ->
            Continue ())

let get_process_step process_ step = do printfn "Process %d, step %d." process_ step
let get_last_process_step process_ = do printfn "Process %d finished." process_

let rec get_process process_ step_count =
    PauseMonad () {
        do! yield_ ()
        if step_count = 0 then
            do get_last_process_step process_
            return ()
        else
            do get_process_step process_ step_count
            return! get_process process_ <| step_count - 1
    }

let rec race p1 p2 =
    match p1 (), p2 () with
    | Continue _, _ -> do printfn "Process 1 finished first."
    | _, Continue _ -> do printfn "Process 2 finished first."
    | Paused p1_, Paused p2_ -> race (p1_) (p2_)

[<EntryPoint>]
let main _ =
    let process_1 = get_process 1 5
    let process_2 = get_process 2 7
    do race process_1 process_2
    0

Here 是 Haskell 中的类似实现。

但是,摆脱相互递归的类型 Process 和 Step 似乎更简单,而只使用单个递归类型 Process,如下所示。

open System

type Process<'a> =
| Continue of 'a
| Paused of (unit -> 'a Process)

type PauseMonad () =
    member this.Return x = Continue x
    member this.ReturnFrom x = x
    member this.Bind (result, rest) =
        match result with
        | Continue x -> Paused (fun () -> rest x)
        | Paused p -> Paused (fun () -> this.Bind (p (), rest))

let yield_ () =
    Paused (fun () ->
        Continue ())

let get_process_step process_ step = do printfn "Process %d, step %d." process_ step
let get_last_process_step process_ = do printfn "Process %d finished." process_

let rec get_process process_ step_count =
    PauseMonad () {
        do! yield_ ()
        if step_count = 0 then
            do get_last_process_step process_
            return ()
        else
            do get_process_step process_ step_count
            return! get_process process_ <| step_count - 1
    }

let rec race p1 p2 =
    match p1, p2 with
    | Continue _, _ -> do printfn "Process 1 finished first."
    | _, Continue _ -> do printfn "Process 2 finished first."
    | Paused p1_, Paused p2_ -> race (p1_ ()) (p2_ ())

[<EntryPoint>]
let main _ =
    let process_1 = get_process 1 5
    let process_2 = get_process 2 7
    do race process_1 process_2
    0

这些实现中的任何一个都给了我相同的输出:

Process 1, step 5.
Process 2, step 7.
Process 1, step 4.
Process 2, step 6.
Process 1, step 3.
Process 2, step 5.
Process 1, step 2.
Process 2, step 4.
Process 1, step 1.
Process 2, step 3.
Process 1 finished.
Process 2, step 2.
Process 1 finished first.

为了便于区分,我已使这两种实现尽可能相似。据我所知,唯一的区别是:

  1. 在第一个版本中,yield_、PauseMonad.Return 和 PauseMonad.Bind 为返回值添加了延迟。在第二个版本中,PauseMonad.Return 在 Paused 包装器中添加了延迟。

  2. 在第一个版本中,PauseMonad.Bind 运行结果过程的一个步骤以查看返回值是否匹配 Continue 或 Paused。在第二个版本中,PauseMonad.Bind 仅在确定与 Paused 匹配后才运行结果过程的一个步骤。

  3. 在第一个版本中,race 在每个进程中运行一个步骤,检查两个结果是否与 Paused 匹配,并与剩余进程递归。在第二个版本中,race 检查两个进程是否匹配 Paused,然后运行每个进程的一个步骤,并使用这些步骤的返回值进行递归。

第一个版本更好有什么原因吗?

【问题讨论】:

    标签: f# monads


    【解决方案1】:

    将代码从 Haskell 转换为 F# 有点棘手,因为 Haskell 是惰性的,所以每当你看到任何值时,比如在 Haskell 中说 'a,你可以将其解释为 unit -&gt; 'a(或更准确地说,为 Lazy&lt;'a&gt; ) - 所以一切都被隐式延迟了。

    但是让我们来比较一下 F# 中的两个定义:

    // Process is always delayed
    type Process1<'a> = unit -> 'a Step1
    and Step1<'a> = Continue1 of 'a | Paused1 of 'a Process1
    
    // Process is a value or a delayed computation
    type Process2<'a> = Continue2 of 'a | Paused2 of (unit -> 'a Process2)
    

    关键区别在于,当您想要表示一个立即产生值的计算时,在第一种情况下它必须是一个完全评估的值,但它可以是一个函数,在第二种情况下执行某些操作并返回一个值案子。例如:

    let primitive1 : Process1<int> = fun () ->
      printfn "hi!"    // This will print when the computation is evaluated
      Continue1(42) )
    
    let primitive2 : Process2<int> = 
      printfn "hi!"    // This will print immediately and returns a monadic value
      Continue2(42)
    

    当您将Delay 成员添加到计算中时,这会变得很有趣,这样您就可以编写如下内容 评估副作用:

    process { 
      printfn "Hi" // Using Process1, we can easily delay this
                   // Using Process2, this is trickier (or we run it immediately)
      return 42 }
    

    对此有很多话要说,您可以找到更多信息in a recent article I wrote about computation expressions

    【讨论】:

    • 感谢您的回复托马斯。你是对的,我应该小心我所谓的“类似”Haskell 实现。我从您的回答中得知,我应该使用哪个定义与情况相关,并且第一个定义不一定是 Pause monad 的“规范”定义?谢谢你,我会再次仔细阅读那篇文章,尽管我担心它在第 3.2 节中途开始让我头晕目眩。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 2013-04-22
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多