Skip to content

Add predicate-cardinality combinators #694

Description

@Seddryck

Summary

Add a family of predicate-cardinality combinators:

satisfies-exactly(count, p1, p2, ...)
satisfies-at-least(count, p1, p2, ...)
satisfies-at-most(count, p1, p2, ...)

Each predicate is evaluated against the same input value. The functions return a boolean according to how many supplied predicates are satisfied.

Semantics

  • satisfies-exactly(n, ...) returns true iff exactly n predicates are satisfied.
  • satisfies-at-least(n, ...) returns true iff at least n predicates are satisfied.
  • satisfies-at-most(n, ...) returns true iff at most n predicates are satisfied.

count must be a non-negative integer.

Short-circuiting

The functions should stop evaluating predicates as soon as the final result is known.

Examples:

  • satisfies-at-least(2, ...) can return true after the second satisfied predicate.
  • satisfies-at-most(2, ...) can return false after the third satisfied predicate.
  • satisfies-exactly(2, ...) can return false after the third satisfied predicate, or when too few predicates remain to reach two.

Relationship with existing logical combinators

These functions generalize several common logical concepts:

  • any(...) is equivalent to satisfies-at-least(1, ...).
  • none(...) is equivalent to satisfies-exactly(0, ...).
  • an exactly-one(...) helper would merely be satisfies-exactly(1, ...), so no separate primitive is required.
  • all(...) remains a useful named logical concept because its target cardinality depends on the number of supplied predicates.
  • majority(...) remains a separate named combinator because its threshold also depends on the number of supplied predicates.

Requirements

  • Accept any number of predicate arguments after count.
  • Evaluate all predicate arguments against the same input value.
  • Validate the count argument.
  • Define behavior when no predicate arguments are supplied.
  • Implement short-circuit evaluation.
  • Add conformance tests and function metadata/documentation.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    new-featureRequest for a new feature in the toolpredicate-functionsFeatures and enhancements about boolean functions

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions