Filtering multisets by a predicate #
Main definitions #
Multiset.filter
:filter p s
is the multiset of elements ins
that satisfyp
.Multiset.filterMap
:filterMap f s
is the multiset ofb
s wheresome b ∈ map f s
.
Filter p s
returns the elements in s
(with the same multiplicities)
which satisfy p
, and removes the rest.
Equations
- Multiset.filter p s = Quot.liftOn s (fun (l : List α) => ↑(List.filter (fun (b : α) => decide (p b)) l)) ⋯
Instances For
Simultaneously filter and map elements of a multiset #
filterMap f s
is a combination filter/map operation on s
.
The function f : α → Option β
is applied to each element of s
;
if f a
is some b
then b
is added to the result, otherwise
a
is removed from the resulting multiset.
Equations
- Multiset.filterMap f s = Quot.liftOn s (fun (l : List α) => ↑(List.filterMap f l)) ⋯
Instances For
countP #
Multiplicity of an element #
Multiset.map f
preserves count
if f
is injective on the set of elements contained in
the multiset
Multiset.map f
preserves count
if f
is injective
Subtraction #
Associate to an embedding f
from α
to β
the order embedding that maps a multiset to its
image under f
.
Equations
Instances For
Mapping a multiset through a predicate and counting the True
s yields the cardinality of the set
filtered by the predicate. Note that this uses the notion of a multiset of Prop
s - due to the
decidability requirements of count
, the decidability instance on the LHS is different from the
RHS. In particular, the decidability instance on the left leaks Classical.decEq
.
See here
for more discussion.