次の計算シーケンスはエラーなしで機能します。
type Monad_1 () =
member M.Bind (so : 'T option, bf : 'T -> 'T option) : 'T option =
match so with
| Some s -> bf s
| None -> None
member M.Delay (df : unit -> 'T) : 'T = // SEE CORRECTION 1
df ()
member M.Return (rv : 'T) : 'T option =
Some rv
let test_1 () : unit =
let m_1 = Monad_1 ()
let cero =
m_1 {
let x1 = 10
let! x2 = Some 20
let! x3 = Some 30
return x1 + x2 + x3}
match cero with
| Some cer -> printfn "%d" cer
| None -> printfn "None"
test_1 () // Output: 60
同じモナドを整数型に特殊化するとします。
type Monad_2 () =
member M.Bind (so : int option, bf : int -> int option) : int option =
match so with
| Some s -> bf s
| None -> None
member M.Delay (df : unit -> int) : int = // SEE CORRECTION 2
df ()
member M.Return (rv : int) : int option =
Some rv
let test_2 () : unit =
let m_2 = Monad_2 ()
let cero =
m_2 {
let x1 = 10
let! x2 = Some 20 // ERROR HERE: This expression was expected to have type int, but here has type int option
let! x3 = Some 30
return x1 + x2 + x3}
match cero with
| Some cer -> printfn "%d" cer
| None -> printfn "None"
test_2 () // ERROR
誰かがここで私の理解の失敗を説明してもらえますか? 完全な説明を書き出すのに時間がかかりすぎる場合に備えて、ヒントも役に立ちます。