package ppx_tydi

  1. Overview
  2. Docs
Let expressions, inferring pattern type from expression.

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_tydi-v0.16.0.tar.gz
sha256=a363153a1f58b35b789321dca9b5b03da46bc1c9152a38d307ffb338b3e25ac6

Description

Provides a ppx for [let%tydi]: type-directed [let] bindings. In [let%tydi a = b in ...], [a]'s type is inferred from [b] rather than the other way around. This is convenient for record patterns whose fields are not in scope.

Published: 02 Jun 2023

README

README.mdx

"ppx_tydi: concise type-directed dismabiguation of records in let-bindings"
===========================================================================

`ppx_tydi` allows concise type-directed disambiguation of record
patterns on the left-hand side of let-bindings:

<!-- $MDX file=test/test.mlt,part=simple-good -->
```ocaml
let () =
  let%tydi { foo } = r in
  ignore (foo : int)
;;
```

where an ordinary let-binding would give an error:

<!-- $MDX file=test/test.mlt,part=simple-bad -->
```ocaml
let () =
  let { foo } = r in
  ignore (foo : int)
;;

[%%expect {|
Line _, characters _-_:
Error: Unbound record field foo
|}]
```

`ppx_tydi` rewrites let-bindings to match statements, e.g.

<!-- $MDX file=doc/generate-snippets-for-readme/before.ml -->
```ocaml
let () =
  let%tydi (() as unit) = () in
  unit
;;
```

goes to

<!-- $MDX file=doc/generate-snippets-for-readme/before.ml.pp -->
```ocaml
let () = match () with | () as unit -> unit
```

`tydi` stands for "Type-directed" and is pronounced "Tidy".

The problem is that the compiler cannot use the type of the expression
on the right hand side to disambiguate the record pattern on the left.
Let-bindings with record patterns are checked from left to right.

Confusingly, let-bindings of variant patterns are fine. They get
type-checked from right to left. This is an implementation detail
related to type-checking of GADTs.

Dependencies (4)

  1. ppxlib >= "0.28.0"
  2. dune >= "2.0.0"
  3. base >= "v0.16" & < "v0.17"
  4. ocaml >= "4.14.0"

Dev Dependencies

None

Used by (1)

  1. ppx_jane = "v0.16.0"

Conflicts

None

OCaml

Innovation. Community. Security.