• Stars
    star
    108
  • Rank 321,259 (Top 7 %)
  • Language
    OCaml
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Monadic let-bindings

ppx_let

A ppx rewriter for monadic and applicative let bindings, match expressions, and if expressions.

Overview

The aim of this rewriter is to make monadic and applicative code look nicer by writing custom binders the same way that we normally bind variables. In OCaml, the common way to bind the result of a computation to a variable is:

let VAR = EXPR in BODY

ppx_let simply adds two new binders: let%bind and let%map. These are rewritten into calls to the bind and map functions respectively. These functions are expected to have

val map  : 'a t -> f:('a -> 'b)   -> 'b t
val bind : 'a t -> f:('a -> 'b t) -> 'b t

for some type t, as one might expect.

These functions are to be provided by the user, and are generally expected to be part of the signatures of monads and applicatives modules. This is the case for all monads and applicatives defined by the Jane Street's Core suite of libraries. (see the section below on getting the right names into scope).

Parallel bindings

ppx_let understands parallel bindings as well. i.e.:

let%bind VAR1 = EXPR1 and VAR2 = EXPR2 and VAR3 = EXPR3 in BODY

The and keyword is seen as a binding combination operator. To do so it expects the presence of a both function, that lifts the OCaml pair operation to the type t in question:

val both : 'a t -> 'b t -> ('a * 'b) t

Some applicatives have optimized map functions for more than two arguments. These applicatives will export functions like map4 shown below:

val map4: 'a t -> 'b t -> 'c t -> 'd t -> f:('a -> 'b -> 'c -> 'd -> 'r) -> 'r t

In order to use these optmized functions, ppx_let provides the let%mapn syntax, which picks the right map{n} function to call based on the amount of applicatives bound by the syntax.

Match statements

We found that this form was quite useful for match statements as well. So for convenience ppx_let also accepts %bind and %map on the match keyword. Morally match%bind expr with cases is seen as let%bind x = expr in match x with cases.

If statements

As a further convenience, ppx_let accepts %bind and %map on the if keyword. The expression if%bind expr1 then expr2 else expr3 is morally equivalent to let%bind p = expr1 in if p then expr2 else expr3.

Function statements

We accept function%bind and function%map too.

let f = function%bind
  | Some a -> g a
  | None -> h

is equivalent to

let f = fun temp ->
  match%bind temp with
  | Some a -> g a
  | None -> h

While statements

We also expand while%bind expr1 do expr2 done as

let rec loop () =
  if%bind expr1
  then (
    let%bind () = expr2 in
    loop ())
  else return ()
in loop ()

Note that this form will (potentially) evaluate the textual form of expr1 multiple times!

We do not support while%map, as that cannot be implemented without bind.

Syntactic forms and actual rewriting

ppx_let adds seven syntactic forms

let%bind P = M in E

let%map  P = M in E

match%bind M with P1 -> E1 | P2 -> E2 | ...

match%map  M with P1 -> E1 | P2 -> E2 | ...

if%bind M then E1 else E2

if%map  M then E1 else E2

while%bind M do E done

that expand into

bind M ~f:(fun P -> E)

map  M ~f:(fun P -> E)

bind M ~f:(function P1 -> E1 | P2 -> E2 | ...)

map  M ~f:(function P1 -> E1 | P2 -> E2 | ...)

bind M ~f:(function true -> E1 | false -> E2)

map  M ~f:(function true -> E1 | false -> E2)

let rec loop () = bind M ~f:(function true -> bind E ~f:loop | false -> return ()) in loop ()

respectively.

As with let, let%bind and let%map also support multiple parallel bindings via the and keyword:

let%bind P1 = M1 and P2 = M2 and P3 = M3 and P4 = M4 in E

let%map  P1 = M1 and P2 = M2 and P3 = M3 and P4 = M4 in E

that expand into

let x1 = M1 and x2 = M2 and x3 = M3 and x4 = M4 in
bind
  (both x1 (both x2 (both x3 x4)))
  ~f:(fun (P1, (P2, (P3, P4))) -> E)

let x1 = M1 and x2 = M2 and x3 = M3 and x4 = M4 in
map
  (both x1 (both x2 (both x3 x4)))
  ~f:(fun (P1, (P2, (P3, P4))) -> E)

respectively. (Instead of x1, x2, ... ppx_let uses variable names that are unlikely to clash with other names)

As with let, names introduced by left-hand sides of the let bindings are not available in subsequent right-hand sides of the same sequence.

Getting the right names in scope

The description of how the %bind and %map syntax extensions expand left out the fact that the names bind, map, both, and return are not used directly., but rather qualified by Let_syntax. For example, we use Let_syntax.bind rather than merely bind.

This means one just needs to get a properly loaded Let_syntax module in scope to use %bind and %map. The intended way to do this is to create a module Let_syntax with a signature like:

module Let_syntax : sig
  module Let_syntax : sig
    val bind : ...
    val map : ...
    ...
  end
  ...
end

and then use open Let_syntax to make the inner Let_syntax module available.

Alternatively, the extension can use values from a Let_syntax module other than the one in scope. If you write %map.A.B.C instead of %map, the expansion will use A.B.C.Let_syntax.Let_syntax.map instead of Let_syntax.map (and similarly for all extension points).

For monads, Core.Monad.Make produces a submodule Let_syntax of the appropriate form.

For applicatives, the convention for these modules is to have a submodule Let_syntax of the form:

module Let_syntax : sig
  module Let_syntax : sig
    val return : 'a -> 'a t
    val map    : 'a t -> f:('a -> 'b) -> 'b t
    val both   : 'a t -> 'b t -> ('a * 'b) t
    module Open_on_rhs : << some signature >>
  end
end

The Open_on_rhs submodule is used by variants of %map and %bind called %map_open and %bind_open. It is locally opened on the right hand sides of the rewritten let bindings in %map_open and %bind_open expressions. For match%map_open and match%bind_open expressions, Open_on_rhs is opened for the expression being matched on.

Open_on_rhs is useful when programming with applicatives, which operate in a staged manner where the operators used to construct the applicatives are distinct from the operators used to manipulate the values those applicatives produce. For monads, Open_on_rhs contains return.

Local values

ppx_let can operate on local values. This requires a compiler that supports the local_ keyword and stack allocation, which as of 2023-03 is a nonstandard compiler extension.

  1. Use %mapl and %bindl instead of %map and %bind.

  2. Implement a Let_syntax module that matches the following signature:

    module Let_syntax : sig
      module Let_syntax : sig
        val return : local_ 'a -> local_ 'a t
        val map    : local_ 'a t -> f:local_ (local_ 'a -> local_ 'b) -> local_ 'b t
        val both   : local_ 'a t -> local_ 'b t -> local_ ('a * 'b) t
        module Open_on_rhs : << some signature >>
      end
    end

More Repositories

1

magic-trace

magic-trace collects and displays high-resolution traces of what a process is doing
OCaml
4,420
star
2

core

Jane Street Capital's standard library overlay
OCaml
1,030
star
3

base

Standard library for OCaml
OCaml
849
star
4

incremental

A library for incremental computations
OCaml
797
star
5

hardcaml

Hardcaml is an OCaml library for designing hardware.
OCaml
558
star
6

learn-ocaml-workshop

Exercises and projects for Jane Street's OCaml Workshop
OCaml
460
star
7

incr_dom

A library for building dynamic webapps, using Js_of_ocaml.
OCaml
360
star
8

bonsai

A library for building dynamic webapps, using Js_of_ocaml
OCaml
340
star
9

ecaml

Writing Emacs plugin in OCaml
OCaml
242
star
10

core_kernel

Jane Street's standard library overlay (kernel)
OCaml
216
star
11

patdiff

File Diff using the Patience Diff algorithm. https://opensource.janestreet.com/patdiff/
OCaml
199
star
12

async

Jane Street Capital's asynchronous execution library
OCaml
183
star
13

iron

Jane Street code review system
149
star
14

vcaml

OCaml bindings for the Neovim API
OCaml
148
star
15

sexplib

Automated S-expression conversion
OCaml
142
star
16

ppx_expect

Cram like framework for OCaml
OCaml
130
star
17

ppx_inline_test

Syntax extension for writing in-line tests in ocaml code
OCaml
124
star
18

pythonlib

A library to help writing wrappers around ocaml code for python
OCaml
94
star
19

install-ocaml

Instructions for setting up an OCaml development environment
OCaml
94
star
20

jenga

Build system
89
star
21

ppx_sexp_conv

Generation of S-expression conversion functions from type definitions
OCaml
78
star
22

torch

OCaml
74
star
23

bin_prot

Binary protocol generator
OCaml
68
star
24

ppx_optcomp

Optional compilation for OCaml
OCaml
62
star
25

memtrace

Streaming client for OCaml's Memprof
OCaml
61
star
26

ocaml_plugin

Automatically build and dynlink ocaml source files
OCaml
60
star
27

ppx_yojson_conv

[@@deriving] plugin to generate Yojson conversion functions
OCaml
58
star
28

async_kernel

Jane Street Capital's asynchronous execution library (core)
OCaml
57
star
29

ppx_fields_conv

Generation of accessor and iteration functions for ocaml records
OCaml
55
star
30

accessor

A library that makes it nicer to work with nested functional data structures
OCaml
51
star
31

opam-repository

Opam repository for the development version of Jane Street packages
51
star
32

virtual_dom

OCaml bindings for the virtual-dom library
OCaml
51
star
33

spawn

Spawning sub-processes
C
48
star
34

rpc_parallel

Type-safe library for building parallel applications, built on top of Async's Rpc module.
OCaml
47
star
35

incremental_kernel

Library for incremental computations depending only on Core_kernel
OCaml
45
star
36

core_bench

Micro-benchmarking library for OCaml
OCaml
44
star
37

sexp

S-expression swiss knife
OCaml
43
star
38

async_smtp

SMTP client and server
OCaml
42
star
39

ppx_variants_conv

Generation of accessor and iteration functions for ocaml variant types
OCaml
40
star
40

re2

OCaml bindings for RE2
C++
39
star
41

higher_kinded

A library with an encoding of higher kinded types in OCaml
OCaml
36
star
42

async_parallel

Distributed computing library
OCaml
35
star
43

ppx_python

[@@deriving] plugin to generate Python conversion functions
OCaml
33
star
44

stdio

Standard IO Library for OCaml
OCaml
33
star
45

parsexp

S-expression parsing library
OCaml
32
star
46

core_extended

Jane Street Capital's standard library overlay
OCaml
32
star
47

async_unix

Jane Street Capital's asynchronous execution library (unix)
OCaml
30
star
48

ocaml_intrinsics

Provides functions to invoke amd64 instructions (such as clz,popcnt,rdtsc,rdpmc) when available, or compatible software implementation on other targets.
OCaml
29
star
49

ppx_string

ppx extension for string interpolation
OCaml
28
star
50

memtrace_viewer

Interactive memory profiler based on Memtrace
OCaml
27
star
51

async_ssl

Async wrappers for ssl
OCaml
27
star
52

incr_map

Helpers for incremental operations on map like data structures.
OCaml
26
star
53

noise-wireguard-ocaml

An implementation of the Noise Protocol, intended to be used as the base for a Wireguard implementation in OCaml.
OCaml
26
star
54

ppx_enumerate

Generate a list containing all values of a finite type
OCaml
24
star
55

ppx_jane

Standard Jane Street ppx rewriters
Makefile
23
star
56

zstandard

OCaml bindings to Zstandard
OCaml
23
star
57

tracing

Tracing library
OCaml
23
star
58

typerep

Runtime types for OCaml (beta version)
OCaml
22
star
59

camlp4-to-ppx

Convert from camlp4 + syntax extensions to regular OCaml + extension points and attributes
OCaml
22
star
60

postgres_async

OCaml/async implementation of the postgres protocol (i.e., does not use C-bindings to libpq)
OCaml
22
star
61

sexp_pretty

S-expression pretty-printer
OCaml
22
star
62

ppx_custom_printf

Printf-style format-strings for user-defined string conversion
OCaml
21
star
63

ppx_compare

Generation of comparison functions from types
OCaml
21
star
64

zarith_stubs_js

Javascripts stubs for the Zarith library
OCaml
21
star
65

fieldslib

OCaml record fields as first class values
Makefile
20
star
66

patience_diff

Tool and library implementing patience diff
OCaml
20
star
67

lwt-async

Lwt with async backend
OCaml
19
star
68

bigdecimal

Arbitrary-precision decimal based on Zarith
OCaml
19
star
69

configurator

Helper library for gathering system configuration
OCaml
19
star
70

ppx_csv_conv

Generate functions to read/write records in csv format
OCaml
19
star
71

janestreet.github.com

Front page
HTML
19
star
72

textutils

OCaml
18
star
73

ocaml-compiler-libs

compiler libraries repackaged
OCaml
18
star
74

universe

Jane Street universe
OCaml
18
star
75

jsonaf

A library for parsing, manipulating, and serializing data structured as JSON.
OCaml
18
star
76

sexplib0

Library containing the definition of S-expressions and some base converters
OCaml
17
star
77

ppx_stable

Stable types conversions generator
OCaml
17
star
78

ppx_css

A ppx that takes in css strings and produces a module for accessing the unique names defined within.
OCaml
16
star
79

core_unix

Unix-specific portions of Core
OCaml
15
star
80

async_extra

Jane Street Capital's asynchronous execution library (extra)
OCaml
15
star
81

hardcaml_of_verilog

Convert Verilog to a Hardcaml design
OCaml
15
star
82

base_quickcheck

Randomized testing framework, designed for compatibility with Base
OCaml
15
star
83

ppx_type_directed_value

Get [@@deriving]-style generation of type-directed values without writing a ppx
OCaml
15
star
84

async_websocket

A library that implements the websocket protocol on top of Async
OCaml
15
star
85

hardcaml_circuits

Hardcaml Circuits
OCaml
14
star
86

redis-async

Redis client for Async applications
OCaml
14
star
87

ppx_hash

A ppx rewriter that generates hash functions from type expressions and definitions
OCaml
14
star
88

file_path

A library for typed manipulation of UNIX-style file paths.
OCaml
14
star
89

merlin-jst

Merlin with support for Jane Street extensions
OCaml
14
star
90

ppx_assert

Assert-like extension nodes that raise useful errors on failure
OCaml
14
star
91

ppx_js_style

Code style checker for Jane Street Packages
OCaml
14
star
92

core_profiler

Profiling library
OCaml
14
star
93

result

Compat result type
OCaml
14
star
94

async_js

A small library that provide Async support for JavaScript platforms
OCaml
13
star
95

topological_sort

Topological sort algorithm
OCaml
13
star
96

hardcaml_verify

Hardcaml Verification Tools
OCaml
13
star
97

ppx_log

Ppx_sexp_message-like extension nodes for lazily rendering log messages
OCaml
13
star
98

toplevel_expect_test

Toplevel expectation test
OCaml
13
star
99

line-up-words

a small cmd line tool to align words in a sequence of lines in a smart way
OCaml
13
star
100

timezone

Time-zone handling
OCaml
12
star