package ppx_distr_guards

  1. Overview
  2. Docs
Extension to distribute guards over or-patterns

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_distr_guards-v0.2.tbz
sha256=a6e7efa882e0492648e0d08f5c8c4fdaa3fc47b546d15ea3aebecc0736c36e1f
sha512=4fbfa77d3947106dbe384eb5f6e61e235b2c4a13e7f49ca59e9479eb9ce4f75239be26a5f0633e17cf41bf7d1e53d3d50808ca53e145e847e61380602d0e9b84

Description

function%distr A x, _ | _, A x when p x -> e will result in function A x, _ when p x -> e | _, A x when p x -> e

Published: 26 Feb 2020

README

ppx_distr_guards

OCaml ppx extension to distribute guards over or-patterns (Warning 57)

Problem

type t = A of int | B of int
let f = function
  | A x, _ (* A 0, A 1 would match here and then fail the guard *)
  | _, A x when x<>0 -> 1 (* warning 57 *)
  | _ -> 2
(* sadly there's no easy way to have the guard checked for every pattern: *)
let f = function
  | A x, _ when x<>0 -> 1
  | _, A x when x<>0 -> 1 (* if these were big expressions, we would need to pull out a function for each case to avoid duplication *)
  | _ -> 2
File "test.ml", line 28, characters 4-19:
Warning 57: Ambiguous or-pattern variables under guard;
variable x may match different arguments. (See manual section 8.5)

Solution

Different possibilities (see test.ml). Decided on the following:

let f = function%distr (* applies to all cases, no brackets, does not compile w/o ppx *)
  | A x, _ | _, A x when x<>0 -> 1
  | _ -> 2

Which will result in

let f = function
  | A x, _ when x<>0 -> 1 | _, A x when x<>0 -> 1
  | _ -> 2

Dependencies (3)

  1. ocaml-migrate-parsetree >= "1.6.0" & < "2.0.0"
  2. dune >= "2.3.0"
  3. ocaml

Dev Dependencies

None

Used by (1)

  1. goblint < "2.0.0"

Conflicts

None