这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@patrick-nicodemus
Copy link

I have started writing a new section which incorporates material from this section of the documentation : https://ocaml.org/manual/5.3/bindingops.html. There might be too much detail for an introductory textbook. Also, I haven't yet successfully managed to build the book locally, so the OCaml code may contain many errors.

@patrick-nicodemus
Copy link
Author

I am also proposing to change the organization of the existing presentation as follows:

  • first introduce bind and return but not the infix version (>>=)
  • go through the first two examples of monads using only bind and return
  • add a new section discussing the custom syntax let* x = e1 in e2 for bind e1 (fun x -> e2) and also the infix operator (>>=), drawing an explicit analogy between (>>=) and (|>) and explaining that the choice of when to use (>>=) vs let* is similar to the choice between let x = e1 in e2 and e1 |> e3 - if e3 is a lambda, then a let-binding may be clearer/easier to read.
  • redo some of the previous examples using the new syntax
  • present the third example using new syntax

@patrick-nicodemus
Copy link
Author

patrick-nicodemus commented Jan 28, 2025

The book currently presents the syntax for monads as

module type Monad = sig
  type 'a t
  val return : 'a -> 'a t
  val ( >>= ) : 'a t -> ('a -> 'b t) -> 'b t
end

I would like to surface for discussion the alternative proposal (from here https://ocaml.org/manual/5.3/bindingops.html)

module type Monad = sig
  type 'a t
  val return : 'a -> 'a t
  val ( let* ) : 'a t -> ('a -> 'b t) -> 'b t
  val ( and* ): 'a t -> 'b t -> ('a * 'b) t
end

The definition of (and*) is definable using let*, so it's not necessary, but you get the associated syntax sugar for parallel binds. In the following expression,

let* x = e1 and* y = e2 and* z = e3 in e4

all three bindings occur in parallel.

@clarksmr
Copy link
Contributor

Just FYI I probably won't get time to review this PR until after the spring semester ends. Thank you though and feel free to update!

@clarksmr clarksmr changed the title First commit Update bind and return syntax for monads May 22, 2025
@clarksmr
Copy link
Contributor

I will look into this later this summer when I'm considering what to do about #199.

@patrick-nicodemus
Copy link
Author

This sounds good. This is only partially finished as I walked away from this after I had trouble getting the book to compile but I'll take another look at it.

@patrick-nicodemus
Copy link
Author

patrick-nicodemus commented Jun 30, 2025

@clarksmr You can feel free to ping me at the point in the summer when you want to address this structural rearrangement and ask me to do some of the legwork of rewriting, if you want to lay down the framework of the high level organizational structure.

@clarksmr
Copy link
Contributor

clarksmr commented Aug 6, 2025

@patrick-nicodemus #199 is now closed. You're welcome to proceed with adapting your revisions. I apologize for the conflicts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants