9

Haskellのモナド則の説明です

F# でモナドの法則を説明するにはどうすればよいですか?

  1. bind (M, return) は M と同等です。

  2. bind ((return x), f) は f x と同等です。

  3. bind (bind (m, f),g) は bind(m, (fun x -> bind (fx, g))) と同等です。

4

1 に答える 1

13

I think that a good way to understand them in F# is to look at what they mean using the computation expression syntax. I'll write m for some computation builder, but you can imagine that this is async or any other computation type.

Left identity

m { let! x' = m { return x }   =   m { let x' = x
    return! f x' }                     return! f x' }

Right identity

m { let! x = comp              =   m { return! comp }
    return x }

Associativity

m { let! x = comp              =   m { let! y = m { let! x = comp
    let! y = f x                                    return! f x }
    return! g y }                      return! g y }

The laws essentially tell you that you should be able to refactor one version of the program to the other without changing the meaning - just like you can refactor ordinary F# programs.

于 2013-09-02T20:43:00.637 に答える