用 None 证明 monad 选项?

问题描述 投票:0回答:1

给定这个选项 monad:

module OptMonad = 
  struct
    type 'a t = 'a option
    let return v = Some v
    let bind m f = 
      match m with
      | Some v -> f v (***)
      | None -> None
    let (>>=) = bind (* as always *)
  end

我想证明这两个定律:

Law 1:  (return x) >>= f ⇔ f x
Law 2:  m >>= return ⇔ m

  • 对于法则 1:

    return x = Some x, so: Some x >>= f ⇔ f x

  • 对于法则 2:

    Case 1: m = Some x, so: Some x >>= return ⇔ Some x = m, Case 2: m = None, so: None >>= return ⇔ None = m

但是对于法则 1,我们不必考虑

None
的情况?

functional-programming ocaml
1个回答
0
投票

OptMonad.return
会产生
None
吗?

© www.soinside.com 2019 - 2024. All rights reserved.