对于monad的"对立面",是否有一个标准的monad?

Gus*_*rra 6 monads f#

也许monad允许链接一组所有可能失败的运算符(通过返回None),并且Some result如果每个子操作成功,或者None如果有任何失败,则返回结束.这是一个小例子:

type MaybeBuilder() =
    member this.Return(x) = 
        Some x
    member this.Bind(m, f) = 
        match m with
        | Some x -> f x
        | None -> None

let maybe = MaybeBuilder()

let list = [1;2;3;4]

// evaluates to Some 3
maybe {
    let! x1 = List.tryFind ((=) 1) list
    let! x2 = List.tryFind ((=) 2) list
    return x1 + x2
}

// evaluates to None 
maybe {
    let! x1 = List.tryFind ((=) 1) list
    let! x2 = List.tryFind ((=) 6) list
    return x1 + x2
}
Run Code Online (Sandbox Code Playgroud)

这大致相当于:

// evaluates to Some 3
match List.tryFind ((=) 1) list with
| None -> None
| Some x1 ->
    match List.tryFind ((=) 2) list with
    | None -> None
    | Some x2 -> Some (x1 + x2)

// evaluates to None
match List.tryFind ((=) 1) list with
| None -> None
| Some x1 ->
    match List.tryFind ((=) 6) list with
    | None -> None
    | Some x2 -> Some (x1 + x2)
Run Code Online (Sandbox Code Playgroud)

在我的一段代码中,我正在做与此相反的"反面",返回第一个成功命中:

// evaluates to Some 1
match List.tryFind ((=) 1) list with
| Some x1 -> Some x1
| None ->
    match List.tryFind ((=) 2) list with
    | Some x2 -> Some x2
    | None -> None

// evaluates to Some 2
match List.tryFind ((=) 6) list with
| Some x1 -> Some x1
| None ->
    match List.tryFind ((=) 2) list with
    | Some x2 -> Some x2
    | None -> None
Run Code Online (Sandbox Code Playgroud)

这也可以用monad来获得漂亮的计算表达式语法吗?

Tom*_*cek 8

前段时间,我写了一篇博客,在F#中实现命令式计算构建器.例如,以下是返回0并且从不执​​行printfn语句的计算:

let test() = imperative {
  return 0
  printfn "after return!"
  return 1 }
Run Code Online (Sandbox Code Playgroud)

我认为您的代码示例可以写成:

imperative { return! List.tryFind ((=) 1) list 
             return! List.tryFind ((=) 2) list }
Run Code Online (Sandbox Code Playgroud)

它是如何工作的?

正如你所建议的那样(和Lee也提到过),类型也是基于option<'T>类型,但是我使用延迟选项值的小差异(这样你可以在不评估它们的情况下组成计算),所以monadic的类型类型实际上是:

type Imperative<'T> = unit -> option<'T>
Run Code Online (Sandbox Code Playgroud)

计算构建器中的关键技巧是添加Combine(其行为类似于mplusLee的Haskell版本),它运行第一次计算并返回其结果(如果是Some)或运行其余的(如果是None)(两者a并且b实际上是函数 -所以我们需要调用它们并延迟结果):

member x.Combine(a, b) = (fun () ->
  match a() with 
  | Some(v) -> Some(v) // if it returned, we can return the result immediately
  | _ -> b() )         // otherwise, we need to run the second part
Run Code Online (Sandbox Code Playgroud)

它实际上非常好用 - 你可以添加对循环和异常处理的支持,如果你使类型更复杂,你可以添加其他功能,如break:

imperative { 
  for x in 1 .. 5 do 
    if (x % 2 = 0) then do! continue
    printfn "number = %d" x }
Run Code Online (Sandbox Code Playgroud)


Gus*_*rra 6

托马斯的解决方案

imperative { 
    return! List.tryFind ((=) 1) list
    return! List.tryFind ((=) 2) list }
Run Code Online (Sandbox Code Playgroud)

做我想要的,但我刚刚意识到我也可以通过这个简单地实现我所需要的:

// evaluates to Some 1
[1;2] |> List.tryPick (fun x -> List.tryFind ((=) x) list)
// evaluates to Some 2
[6;2] |> List.tryPick (fun x -> List.tryFind ((=) x) list)
Run Code Online (Sandbox Code Playgroud)

  • +1当有一个大锤子时,很容易忽略简单的解决方案! (2认同)

Lee*_*Lee 5

Haskell使用MonadPlus定义为的类型类来完成此操作:

class Monad m => MonadPlus m where
  mzero :: m a
  mplus :: m a -> m a -> m a
Run Code Online (Sandbox Code Playgroud)

Maybe 将此类型类实现为

instance MonadPlus Maybe where
  mzero                   = Nothing
  Nothing `mplus` Nothing = Nothing
  Just x  `mplus` Nothing = Just x
  Nothing `mplus` Just x  = Just x
  Just x  `mplus` Just y  = Just x
Run Code Online (Sandbox Code Playgroud)

看来,mzeromplus成员MonadPlus对应于ZeroCombine部件所使用的F#计算表达式.