package granary

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Zset.MakeSource

Make (E) builds a Z-set module over element domain E.

Parameters

module E : ELEMENT

Signature

Sourcetype elt = E.t

The element type this Z-set ranges over.

Sourcetype t

A Z-set: a finitely-supported elt -> int weight map, kept canonical (no zero-weight entries).

Sourceval zero : t

The empty Z-set (additive identity).

Sourceval is_zero : t -> bool

is_zero z is true iff z has no elements (equivalently, total weight everywhere is 0).

Sourceval singleton : elt -> int -> t

singleton e w is the Z-set mapping e to w (and zero if w = 0).

Sourceval of_list : (elt * int) list -> t

of_list pairs sums the weights of repeated elements; entries that sum to 0 are dropped.

Sourceval to_list : t -> (elt * int) list

to_list z returns the (element, nonzero-weight) pairs, ordered by the element ELEMENT.compare.

Sourceval weight : t -> elt -> int

weight z e is the weight of e in z (0 if absent).

Sourceval add : t -> t -> t

add a b is the pointwise sum of weights, dropping any that cancel to 0. Commutative, associative, with identity zero.

Sourceval negate : t -> t

negate z flips the sign of every weight (the additive inverse: add z (negate z) = zero).

Sourceval sub : t -> t -> t

sub a b is add a (negate b).

Sourceval scale : int -> t -> t

scale n z multiplies every weight by n (zero when n = 0).

Sourceval map : (elt -> elt) -> t -> t

map f z applies f to each element, summing the weights of elements that collide onto the same image (and dropping any that cancel). A linear operator: map f (add a b) = add (map f a) (map f b).

Sourceval filter : (elt -> bool) -> t -> t

filter p z keeps the entries whose element satisfies p, weights unchanged. A linear operator.

Sourceval distinct : t -> t

distinct z is the set-semantics projection: every element with weight > 0 is mapped to weight 1; elements with weight <= 0 are dropped. Idempotent.

Sourceval support : t -> elt list

support z is the list of elements with nonzero weight, ordered by ELEMENT.compare.

Sourceval cardinality : t -> int

cardinality z is the number of distinct elements with nonzero weight.

Sourceval total_weight : t -> int

total_weight z is the sum of all weights (may be negative).

Sourceval iter : (elt -> int -> unit) -> t -> unit

iter f z applies f element weight to each entry, in element order.

Sourceval fold : (elt -> int -> 'a -> 'a) -> t -> 'a -> 'a

fold f z acc folds f element weight over the entries, in element order.

Sourceval equal : t -> t -> bool

Structural equality (canonical form makes this exact set/weight equality).

Sourceval pp : Format.formatter -> t -> unit

Pretty-print a Z-set as {e1: w1, e2: w2, …}.