Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First commit #204

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

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!

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