package ppx_distr_guards

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

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_distr_guards-0.3.tbz
sha256=a05dc97065a18ef83eb5b8fcfeea9dfa07dcefbf7daf2e41f7fc09cfddea7f55
sha512=f4aba0549c93a67d935a3b1554c189db20efcf26c0d8474c9f015a252e17d7b641996e3d72ef466f537b83ef0ea6c35baa7d1d67788b51686d9c77da387973e8

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: 30 Jun 2021

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 (2)

  1. ppxlib >= "0.15.0"
  2. dune >= "2.7"

Dev Dependencies (1)

  1. odoc with-doc

Used by (1)

  1. goblint < "2.0.0"

Conflicts

None