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

Syntax proposal: F# like syntax for monads "let!" #1353

Closed
modlfo opened this issue Aug 8, 2017 · 8 comments
Closed

Syntax proposal: F# like syntax for monads "let!" #1353

modlfo opened this issue Aug 8, 2017 · 8 comments
Labels
KIND: FEATURE REQUEST Parser parsing reason code into an AST

Comments

@modlfo
Copy link

modlfo commented Aug 8, 2017

One thing that I miss from F# when working in OCaml is the possibility of writing monadic bindings (that are readable) without the need of a ppx preprocessor like ppx_let.

I think that the F# syntax could fit nicely with the current syntax of Reason. Here are more details https://docs.microsoft.com/en-gb/dotnet/fsharp/language-reference/computation-expressions

For example:

(* F# code *)
let foo () =
   async {
      let! x = do_something ()
      do! y = do_something_else ()
      return x + y
   }

which already looks pretty much like Reason:

(* Reason equivalent *)
let foo () =>
   async {
      let! x = do_something ();
      do! y = do_something_else ();
      return x + y;
   }

If you are not familiar with the F# syntax, the let! is replaced by the bind function defined in the module async.

@bassjacob
Copy link

Would this be similar to https://github.com/janestreet/ppx_let ? If so, you can use this ppx with your projects already.

If the goal is the async/awaiy equivalent, there's some really good work to make https://github.com/ocsigen/lwt work well with reason and bucklescript, which should help some.

@modlfo
Copy link
Author

modlfo commented Aug 8, 2017

I'll elaborate further.

In principle my proposal is to integrate something similar to ppx_let into Reason, but with what I think is a good syntax; the F# syntax. The reason why I think this would be better are:

  • No external preprocessor needed (no ppx).
  • The syntax is compact.
  • The syntax looks already very close to Reason.
  • The syntax has shown to be good and useful (in F#)
  • Similarly as with ppx_let it can be used for many purposes.

In addition the F# syntax described here consider other useful cases like try, for, while which are also used in ppx_lwt. It worths taking a look at it.

Here's another example comparing it to OCaml:

(* OCaml code with ppx_let *)
let foo () =
   let open MyMonad in
   let%bind x = bar () in
   let%bind y = bob x in
   return y

Same code with Reason (F# like syntax)

(* Reason code with the proposed syntax *)
let foo ()  =>
   MyMonad {
      let! x = bar () in
      let! y = bob x in
      return y
}

@hcarty
Copy link
Contributor

hcarty commented Aug 8, 2017

What does Reason use for its equivalent(s) to open!, method! and other override syntax? Part of the reason that specific let! syntax wasn't added to OCaml was because of the very different meanings between the ! suffix in those uses.

@modlfo
Copy link
Author

modlfo commented Aug 8, 2017

@hcarty If I understood correctly, what you mean is that ! can be a suffix of open and method. I think that F# implements only: let!, do!,yield!, use!, and return!. And by looking at the tokenizer code here, the tokenizer tries to match the full let! string. Therefore let ! (with a space) is not the same as let! (with no space). The ! in let! cannot be overloaded to behave differently.

@hcarty
Copy link
Contributor

hcarty commented Aug 8, 2017

@modlfo I just mean that the ! suffix on a keyword in OCaml has a meaning already which has nothing to do with monads. I think the same meaning is maintained in Reason. That should be taken into account when considering a let! syntax to avoid inconsistencies. Either a different suffix should be chosen for let in the monad case or the suffix used for overriding in other keywords should be changed.

@modlfo
Copy link
Author

modlfo commented Aug 9, 2017

@hcarty Thanks for the clarification. I was not aware that open! or method! existed since I never seen them used before.

@andreypopp
Copy link
Contributor

I proposed a generic monadic syntax based on ppx_let semantics in #1321 (comment)

@jaredly jaredly added Parser parsing reason code into an AST KIND: FEATURE REQUEST labels Jun 14, 2018
@anmonteiro
Copy link
Member

Binding ops solve this problem today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
KIND: FEATURE REQUEST Parser parsing reason code into an AST
Projects
None yet
Development

No branches or pull requests

6 participants