Stream: coqbot devs & users

Topic: Using binding lets


view this post on Zulip Ali Caglayan (Mar 16 2022 at 17:14):

Here is something I learnt from dune. Since we use Lwt stuff all over the place, it makes it tricky to use let constructs since you have to bind things explicitly. If you do something like:

let (let*) = Lwt.bind
let (and*) = Lwt.both

You can replace let some_string : () -> string Lwt.t with let* some_String : () -> string which makes reasoning about it later easier.

view this post on Zulip Ali Caglayan (Mar 16 2022 at 17:14):

This hopefully will save having to write >>= and match on Ok or Error all the time

view this post on Zulip Théo Zimmermann (Mar 16 2022 at 17:15):

In fact, there even is a ppx_lwt package (or something like this) that allows you to have these notations defined I think.

view this post on Zulip Théo Zimmermann (Mar 16 2022 at 17:16):

If you want to experiment with replacing a few monadic binds with such lets, please go ahead.

view this post on Zulip Ali Caglayan (Mar 16 2022 at 17:21):

I think the ppx package is a bit more verbose, you would have to write something like let%lwt. The (let*) operator was introduced in 4.08: https://ocaml.org/manual/bindingops.html

view this post on Zulip Emilio Jesús Gallego Arias (Mar 16 2022 at 17:22):

Yup the ppx package was mainly useful for pre 4.08 time

view this post on Zulip Emilio Jesús Gallego Arias (Mar 16 2022 at 17:22):

tho it may have some more fancy things that 4.08 doesn't

view this post on Zulip Ali Caglayan (Mar 16 2022 at 17:24):

Even simpler actually, open Lwt.Syntax gives us access to let*

view this post on Zulip Ali Caglayan (Mar 16 2022 at 17:26):

I guess if we use ppx we can start using multiple versions of let especially for result

view this post on Zulip Ali Caglayan (Mar 16 2022 at 17:27):

But this has a risk of being completely unreadable

view this post on Zulip Théo Zimmermann (Mar 16 2022 at 17:27):

Let's try with just Lwt.Syntax for now.

view this post on Zulip Ali Caglayan (Mar 16 2022 at 18:49):

I'm looking for a projection for Lwt. We have the pair as Lwt.both : 'a Lwt.t -> 'b Lwt.t -> ('a * 'b) Lwt.t, do we have something like Lwt.fst : ('a * 'b) Lwt.t -> 'a Lwt.t?

view this post on Zulip Ali Caglayan (Mar 16 2022 at 18:53):

oh nevermind it is just map

view this post on Zulip Ali Caglayan (Mar 29 2022 at 15:09):

Just realised that there is Lwt_result.Syntax which was what I was wanting before

view this post on Zulip Ali Caglayan (Mar 29 2022 at 15:16):

I'm gonna experiment with it later since I don't want to do too many things at once.


Last updated: May 28 2023 at 18:29 UTC