| 1 | ppx_let |
| 2 | ======= |
| 3 | |
| 4 | A ppx rewriter for monadic and applicative let bindings, match expressions, and |
| 5 | if expressions. |
| 6 | |
| 7 | Overview |
| 8 | -------- |
| 9 | |
| 10 | The aim of this rewriter is to make monadic and applicative code look nicer by |
| 11 | writing custom binders the same way that we normally bind variables. In OCaml, |
| 12 | the common way to bind the result of a computation to a variable is: |
| 13 | |
| 14 | ```ocaml |
| 15 | let VAR = EXPR in BODY |
| 16 | ``` |
| 17 | |
| 18 | ppx\_let simply adds two new binders: `let%bind` and `let%map`. These are |
| 19 | rewritten into calls to the `bind` and `map` functions respectively. These |
| 20 | functions are expected to have |
| 21 | |
| 22 | ```ocaml |
| 23 | val map : 'a t -> f:('a -> 'b) -> 'b t |
| 24 | val bind : 'a t -> f:('a -> 'b t) -> 'b t |
| 25 | ``` |
| 26 | |
| 27 | for some type `t`, as one might expect. |
| 28 | |
| 29 | These functions are to be provided by the user, and are generally expected to be |
| 30 | part of the signatures of monads and applicatives modules. This is the case for |
| 31 | all monads and applicatives defined by the Jane Street's Core suite of |
| 32 | libraries. (see the section below on getting the right names into scope). |
| 33 | |
| 34 | ### Parallel bindings |
| 35 | |
| 36 | ppx\_let understands parallel bindings as well. i.e.: |
| 37 | |
| 38 | ```ocaml |
| 39 | let%bind VAR1 = EXPR1 and VAR2 = EXPR2 and VAR3 = EXPR3 in BODY |
| 40 | ``` |
| 41 | |
| 42 | The `and` keyword is seen as a binding combination operator. To do so it expects |
| 43 | the presence of a `both` function, that lifts the OCaml pair operation to the |
| 44 | type `t` in question: |
| 45 | |
| 46 | ```ocaml |
| 47 | val both : 'a t -> 'b t -> ('a * 'b) t |
| 48 | ``` |
| 49 | |
| 50 | Some applicatives have optimized `map` functions for more than two arguments. |
| 51 | These applicatives will export functions like `map4` shown below: |
| 52 | |
| 53 | ```ocaml |
| 54 | val map4: 'a t -> 'b t -> 'c t -> 'd t -> f:('a -> 'b -> 'c -> 'd -> 'r) -> 'r t |
| 55 | ``` |
| 56 | |
| 57 | In order to use these optmized functions, ppx\_let provides the `let%mapn` |
| 58 | syntax, which picks the right `map{n}` function to call based on the amount of |
| 59 | applicatives bound by the syntax. |
| 60 | |
| 61 | ### Match statements |
| 62 | |
| 63 | We found that this form was quite useful for match statements as well. So for |
| 64 | convenience ppx\_let also accepts `%bind` and `%map` on the `match` keyword. |
| 65 | Morally `match%bind expr with cases` is seen as `let%bind x = expr in match x |
| 66 | with cases`. |
| 67 | |
| 68 | ### If statements |
| 69 | |
| 70 | As a further convenience, ppx\_let accepts `%bind` and `%map` on the `if` |
| 71 | keyword. The expression `if%bind expr1 then expr2 else expr3` is morally |
| 72 | equivalent to `let%bind p = expr1 in if p then expr2 else expr3`. |
| 73 | |
| 74 | ### Function statements |
| 75 | |
| 76 | We accept `function%bind` and `function%map` too. |
| 77 | |
| 78 | ```ocaml |
| 79 | let f = function%bind |
| 80 | | Some a -> g a |
| 81 | | None -> h |
| 82 | ``` |
| 83 | |
| 84 | is equivalent to |
| 85 | |
| 86 | ```ocaml |
| 87 | let f = fun temp -> |
| 88 | match%bind temp with |
| 89 | | Some a -> g a |
| 90 | | None -> h |
| 91 | ``` |
| 92 | |
| 93 | ### While statements |
| 94 | |
| 95 | We also expand `while%bind expr1 do expr2 done` as |
| 96 | |
| 97 | ```ocaml |
| 98 | let rec loop () = |
| 99 | if%bind expr1 |
| 100 | then ( |
| 101 | let%bind () = expr2 in |
| 102 | loop ()) |
| 103 | else return () |
| 104 | in loop () |
| 105 | ``` |
| 106 | |
| 107 | Note that this form will (potentially) evaluate the textual form of |
| 108 | expr1 multiple times! |
| 109 | |
| 110 | We do not support `while%map`, as that cannot be implemented without |
| 111 | `bind`. |
| 112 | |
| 113 | Syntactic forms and actual rewriting |
| 114 | ------------------------------------ |
| 115 | |
| 116 | `ppx_let` adds seven syntactic forms |
| 117 | |
| 118 | ```ocaml |
| 119 | let%bind P = M in E |
| 120 | |
| 121 | let%map P = M in E |
| 122 | |
| 123 | match%bind M with P1 -> E1 | P2 -> E2 | ... |
| 124 | |
| 125 | match%map M with P1 -> E1 | P2 -> E2 | ... |
| 126 | |
| 127 | if%bind M then E1 else E2 |
| 128 | |
| 129 | if%map M then E1 else E2 |
| 130 | |
| 131 | while%bind M do E done |
| 132 | ``` |
| 133 | |
| 134 | that expand into |
| 135 | |
| 136 | ```ocaml |
| 137 | bind M ~f:(fun P -> E) |
| 138 | |
| 139 | map M ~f:(fun P -> E) |
| 140 | |
| 141 | bind M ~f:(function P1 -> E1 | P2 -> E2 | ...) |
| 142 | |
| 143 | map M ~f:(function P1 -> E1 | P2 -> E2 | ...) |
| 144 | |
| 145 | bind M ~f:(function true -> E1 | false -> E2) |
| 146 | |
| 147 | map M ~f:(function true -> E1 | false -> E2) |
| 148 | |
| 149 | let rec loop () = bind M ~f:(function true -> bind E ~f:loop | false -> return ()) in loop () |
| 150 | ``` |
| 151 | |
| 152 | respectively. |
| 153 | |
| 154 | As with `let`, `let%bind` and `let%map` also support multiple *parallel* |
| 155 | bindings via the `and` keyword: |
| 156 | |
| 157 | ```ocaml |
| 158 | let%bind P1 = M1 and P2 = M2 and P3 = M3 and P4 = M4 in E |
| 159 | |
| 160 | let%map P1 = M1 and P2 = M2 and P3 = M3 and P4 = M4 in E |
| 161 | ``` |
| 162 | |
| 163 | that expand into |
| 164 | |
| 165 | ```ocaml |
| 166 | let x1 = M1 and x2 = M2 and x3 = M3 and x4 = M4 in |
| 167 | bind |
| 168 | (both x1 (both x2 (both x3 x4))) |
| 169 | ~f:(fun (P1, (P2, (P3, P4))) -> E) |
| 170 | |
| 171 | let x1 = M1 and x2 = M2 and x3 = M3 and x4 = M4 in |
| 172 | map |
| 173 | (both x1 (both x2 (both x3 x4))) |
| 174 | ~f:(fun (P1, (P2, (P3, P4))) -> E) |
| 175 | ``` |
| 176 | |
| 177 | respectively. (Instead of `x1`, `x2`, ... ppx\_let uses variable names that are |
| 178 | unlikely to clash with other names) |
| 179 | |
| 180 | As with `let`, names introduced by left-hand sides of the let bindings are not |
| 181 | available in subsequent right-hand sides of the same sequence. |
| 182 | |
| 183 | Getting the right names in scope |
| 184 | -------------------------------- |
| 185 | |
| 186 | The description of how the `%bind` and `%map` syntax extensions expand left out |
| 187 | the fact that the names `bind`, `map`, `both`, and `return` are not used |
| 188 | directly., but rather qualified by `Let_syntax`. For example, we use |
| 189 | `Let_syntax.bind` rather than merely `bind`. |
| 190 | |
| 191 | This means one just needs to get a properly loaded `Let_syntax` module |
| 192 | in scope to use `%bind` and `%map`. The intended way to do this is to |
| 193 | create a module `Let_syntax` with a signature like: |
| 194 | ```ocaml |
| 195 | module Let_syntax : sig |
| 196 | module Let_syntax : sig |
| 197 | val bind : ... |
| 198 | val map : ... |
| 199 | ... |
| 200 | end |
| 201 | ... |
| 202 | end |
| 203 | ``` |
| 204 | and then use `open Let_syntax` to make the inner `Let_syntax` module |
| 205 | available. |
| 206 | |
| 207 | Alternatively, the extension can use values from a `Let_syntax` module |
| 208 | other than the one in scope. If you write `%map.A.B.C` instead of |
| 209 | `%map`, the expansion will use `A.B.C.Let_syntax.Let_syntax.map` |
| 210 | instead of `Let_syntax.map` (and similarly for all extension points). |
| 211 | |
| 212 | For monads, `Core.Monad.Make` produces a submodule `Let_syntax` of the |
| 213 | appropriate form. |
| 214 | |
| 215 | For applicatives, the convention for these modules is to have a submodule |
| 216 | `Let_syntax` of the form: |
| 217 | |
| 218 | ```ocaml |
| 219 | module Let_syntax : sig |
| 220 | module Let_syntax : sig |
| 221 | val return : 'a -> 'a t |
| 222 | val map : 'a t -> f:('a -> 'b) -> 'b t |
| 223 | val both : 'a t -> 'b t -> ('a * 'b) t |
| 224 | module Open_on_rhs : << some signature >> |
| 225 | end |
| 226 | end |
| 227 | ``` |
| 228 | |
| 229 | The `Open_on_rhs` submodule is used by variants of `%map` and `%bind` called |
| 230 | `%map_open` and `%bind_open`. It is locally opened on the right hand sides of |
| 231 | the rewritten let bindings in `%map_open` and `%bind_open` expressions. For |
| 232 | `match%map_open` and `match%bind_open` expressions, `Open_on_rhs` is opened for |
| 233 | the expression being matched on. |
| 234 | |
| 235 | `Open_on_rhs` is useful when programming with applicatives, which operate in a |
| 236 | staged manner where the operators used to construct the applicatives are |
| 237 | distinct from the operators used to manipulate the values those applicatives |
| 238 | produce. For monads, `Open_on_rhs` contains `return`. |
| 239 | |
| 240 | Local values |
| 241 | ------------ |
| 242 | |
| 243 | `ppx_let` can operate on local values. This requires a compiler that supports the `local_` |
| 244 | keyword and stack allocation, which as of 2023-03 is a nonstandard compiler extension. |
| 245 | |
| 246 | 1. Use `%mapl` and `%bindl` instead of `%map` and `%bind`. |
| 247 | |
| 248 | 2. Implement a `Let_syntax` module that matches the following |
| 249 | signature: |
| 250 | |
| 251 | ```ocaml |
| 252 | module Let_syntax : sig |
| 253 | module Let_syntax : sig |
| 254 | val return : local_ 'a -> local_ 'a t |
| 255 | val map : local_ 'a t -> f:local_ (local_ 'a -> local_ 'b) -> local_ 'b t |
| 256 | val both : local_ 'a t -> local_ 'b t -> local_ ('a * 'b) t |
| 257 | module Open_on_rhs : << some signature >> |
| 258 | end |
| 259 | end |
| 260 | ``` |