anf

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2024 License: AGPL-3.0 Imports: 2 Imported by: 3

Documentation

Overview

Package anf provides an implementation of sets that are stored in Algebraic Normal Form (ANF).

In ANF, each set is stored as the symmetric difference of intersections (or in boolean terms, as the XOR of ANDs).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Property

type Property interface {
	// Equal returns whether this property is equal to another.
	Equal(other Property) bool

	// IsSatisfied returns whether this property is true for a particular
	// element.
	IsSatisfied(element any) bool

	// Precedence returns an ordering for [Property] instances, to help you
	// ensure that [Property.Intersection] and [Property.SymmetricDifference]
	// are symmetric.
	Precedence() int

	// Intersection determines whether the intersection of this property and
	// another is empty; and if not, whether it can be represented by a
	// different, simpler, [Property] instance.
	//
	// If the two properties are disjoint, then their intersection is empty. In
	// that case, return [Remove].
	//
	// If the intersection is non-empty, and there is a simpler, single
	// [Property] instance that contains EXACTLY the same elements as the
	// intersection, return [Replace] along with the simplified property.  If
	// not (that is, the two existing Property instances are the best
	// representation of their intersection), then return [Keep].
	//
	// As a common case of the above, we will test ourselves whether the two
	// properties are equal, in which case they will be replaced with a single
	// copy of the property.  You do not have to test for that situation.
	//
	// To help you ensure that this method is symmetric, we will always ensure
	// that the precedence of the method receiver is <= the precedence of other.
	Intersection(other Property) (Simplification, Property)

	// SymmetricDifference determines whether the symmetric difference of this
	// property and another is empty; and if not, whether it can be represented
	// by a different, simpler, [Property] instance.
	//
	// If the two properties are equivalent, then their symmetric difference is
	// empty. In that case, return [Remove].
	//
	// If the symmetric difference is non-empty, and there is a simpler, single
	// [Property] instance that contains EXACTLY the same elements as the
	// symmetric difference, return [Replace] along with the simplified
	// property.  If not (that is, the two existing [Property] instances are the
	// best representation of their symmetric difference), then return [Keep].
	//
	// As a common case of the above, we will test ourselves whether the two
	// properties are equal, in which case they will be removed.  You do not
	// have to test for that situation.
	//
	// To help you ensure that this method is symmetric, we will always ensure
	// that the precedence of the method receiver is <= the precedence of other.
	SymmetricDifference(other Property) (Simplification, Property)
}

Property defines a property that may be true or false for each possible element. Each property must match at least one element, and must reject at least one element.

Implementations must be:

  • Symmetric: p1.Intersection(p2) and p2.Intersection(p1) must return the same result. (Ditto for SymmetricDifference)

  • Confluent: Given any number of properties, calling Intersection or SymmetricDifference on them in any order must produce the same result.

(Note that this interface is parameterized on itself as well as the element type, so that we can ensure that the interface methods operate on properties of the same type.)

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set is a set of elements stored in algebraic normal form. E is the type of the elements stored in the set. P defines properties that may be true or false for each possible element. A set is then the XOR of zero or more clauses, each of which is the AND of one or more properties.

func Empty

func Empty() Set

Empty returns a Set that contains no elements.

func Intersection

func Intersection(sets ...Set) Set

Intersection returns the intersection of zero or more sets.

func Only

func Only(prop Property) Set

Only returns a Set containing the elements for which a single property is true.

func SymmetricDifference

func SymmetricDifference(sets ...Set) Set

SymmetricDifference returns the symmetric difference of zero or more sets.

func Union

func Union(sets ...Set) Set

Union returns the union of zero or more sets.

func Universe

func Universe() Set

Universe returns a Set that contains every possible element.

func (Set) Clone

func (s Set) Clone() Set

Clone returns a copy of a set.

func (Set) Contains

func (s Set) Contains(element any) bool

Contains returns whether a set contains a particular element.

func (Set) Difference

func (lhs Set) Difference(rhs Set) Set

Difference returns a set that includes elements that are in this set but not in `other`.

func (Set) Equal

func (s Set) Equal(other Set) bool

Equal returns whether two sets containing exactly the same elements.

func (Set) ForEach

func (s Set) ForEach(f func([]Property))

ForEach invokes a callback for each clause in a set.

func (Set) Format

func (s Set) Format(f fmt.State, verb rune)

func (Set) IncludesUniverse

func (s Set) IncludesUniverse() bool

IncludesUniverse returns whether this set includes the universe (the set containing all possible elements) as one of its clauses. (In ANF terms, this is whether the set includes 1⊕... as one of its terms.)

func (Set) Intersection

func (lhs Set) Intersection(rhs Set) Set

Intersection returns a set that includes elements that are in both this set and `other`.

func (Set) IsEmpty

func (s Set) IsEmpty() bool

IsEmpty returns whether a set is empty.

func (Set) Negated

func (s Set) Negated() Set

Negated returns a set that contains exactly the opposite elements as the current one.

func (Set) String

func (s Set) String() string

func (Set) SymmetricDifference

func (lhs Set) SymmetricDifference(rhs Set) Set

SymmetricDifference returns a set that includes element are in either one set or another, but not both.

func (Set) Union

func (lhs Set) Union(rhs Set) Set

Union returns a set that includes elements that are in either this set or another.

type Simplification

type Simplification int

Simplification indicates what kind of result is returned from Property.Intersection and Property.SymmetricDifference.

const (
	// Remove indicates that the two properties being simplified cancel each
	// other out and should both be removed.
	Remove Simplification = iota
	// Keep indicates that the two properties cannot be simplified, and should
	// be kept as-is.
	Keep
	// Replace indicates that the two properties can be simplified, and should
	// collectively be replaced with a different property.
	Replace
)

Directories

Path Synopsis
Package anftest defines a test suite for anf set implementations
Package anftest defines a test suite for anf set implementations

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL