Multisets #
These are implemented as the quotient of a list by permutations.
Notation #
We define the global infix notation ::ₘ
for Multiset.cons
.
Equations
Equations
- Multiset.decidableEq x✝ x = match x✝, x with | s₁, s₂ => Quotient.recOnSubsingleton₂ s₁ s₂ fun (x x_1 : List α) => decidable_of_iff' (x ≈ x_1) (_ : ⟦x⟧ = ⟦x_1⟧ ↔ x ≈ x_1)
defines a size for a multiset by referring to the size of the underlying list
Equations
- Multiset.sizeOf s = Quot.liftOn s sizeOf (_ : ∀ (x x_1 : List α), List.Perm x x_1 → sizeOf x = sizeOf x_1)
Instances For
Empty multiset #
Equations
- Multiset.instEmptyCollectionMultiset = { emptyCollection := 0 }
cons a s
is the multiset which contains s
plus one more instance of a
.
Equations
- Multiset.«term_::ₘ_» = Lean.ParserDescr.trailingNode `Multiset.term_::ₘ_ 67 68 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " ::ₘ ") (Lean.ParserDescr.cat `term 67))
Instances For
Dependent recursor on multisets.
TODO: should be @[recursor 6], but then the definition of Multiset.pi
fails with a stack
overflow in whnf
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Companion to Multiset.rec
with more convenient argument order.
Equations
- Multiset.recOn m C_0 C_cons C_cons_heq = Multiset.rec C_0 C_cons C_cons_heq m
Instances For
a ∈ s
means that a
has nonzero multiplicity in s
.
Equations
Instances For
Equations
- Multiset.instMembershipMultiset = { mem := Multiset.Mem }
Equations
- Multiset.decidableMem a s = Quot.recOnSubsingleton' s fun (l : List α) => inferInstanceAs (Decidable (a ∈ l))
Singleton #
Equations
- (_ : IsLawfulSingleton α (Multiset α)) = (_ : IsLawfulSingleton α (Multiset α))
s ⊆ t
is the lift of the list subset relation. It means that any
element with nonzero multiplicity in s
has nonzero multiplicity in t
,
but it does not imply that the multiplicity of a
in s
is less or equal than in t
;
see s ≤ t
for this relation.
Equations
- Multiset.Subset s t = ∀ ⦃a : α⦄, a ∈ s → a ∈ t
Instances For
Equations
- One or more equations did not get rendered due to their size.
Produces a list of the elements in the multiset using choice.
Equations
Instances For
s ≤ t
means that s
is a sublist of t
(up to permutation).
Equivalently, s ≤ t
means that count a s ≤ count a t
for all a
.
Equations
- Multiset.Le s t = Quotient.liftOn₂ s t (fun (x x_1 : List α) => List.Subperm x x_1) (_ : ∀ (x x_1 x_2 x_3 : List α), x ≈ x_2 → x_1 ≈ x_3 → List.Subperm x x_1 = List.Subperm x_2 x_3)
Instances For
Equations
- Multiset.decidableLE s t = Quotient.recOnSubsingleton₂ s t List.decidableSubperm
Alias of Multiset.subset_of_le
.
Equations
- Multiset.instOrderBotMultisetToLEToPreorderInstPartialOrderMultiset = OrderBot.mk (_ : ∀ (s : Multiset α), 0 ≤ s)
This is a rfl
and simp
version of bot_eq_zero
.
Additive monoid #
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Cardinality #
Induction principles #
The strong induction principle for multisets.
Equations
- Multiset.strongInductionOn s ih = ih s fun (t : Multiset α) (_h : t < s) => Multiset.strongInductionOn t ih
Instances For
Suppose that, given that p t
can be defined on all supersets of s
of cardinality less than
n
, one knows how to define p s
. Then one can inductively define p s
for all multisets s
of
cardinality less than n
, starting from multisets of card n
and iterating. This
can be used either to define data, or to prove properties.
Equations
- Multiset.strongDownwardInduction H s = H s fun {t : Multiset α} (ht : Multiset.card t ≤ n) (_h : s < t) => Multiset.strongDownwardInduction H t ht
Instances For
Analogue of strongDownwardInduction
with order of arguments swapped.
Equations
Instances For
Another way of expressing strongInductionOn
: the (<)
relation is well-founded.
Equations
- (_ : WellFoundedLT (Multiset α)) = (_ : IsWellFounded (Multiset α) fun (x x_1 : Multiset α) => x < x_1)
replicate n a
is the multiset containing only a
with multiplicity n
.
Equations
- Multiset.replicate n a = ↑(List.replicate n a)
Instances For
Multiset.replicate
as an AddMonoidHom
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Alias of the reverse direction of Multiset.eq_replicate_card
.
Erasing one copy of an element #
erase s a
is the multiset that subtracts 1 from the multiplicity of a
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Multiset.map
as an AddMonoidHom
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Multiset.fold
#
foldl f H b s
is the lift of the list operation foldl f b l
,
which folds f
over the multiset. It is well defined when f
is right-commutative,
that is, f (f b a₁) a₂ = f (f b a₂) a₁
.
Equations
- Multiset.foldl f H b s = Quot.liftOn s (fun (l : List α) => List.foldl f b l) (_ : ∀ (_l₁ _l₂ : List α), Setoid.r _l₁ _l₂ → List.foldl f b _l₁ = List.foldl f b _l₂)
Instances For
foldr f H b s
is the lift of the list operation foldr f b l
,
which folds f
over the multiset. It is well defined when f
is left-commutative,
that is, f a₁ (f a₂ b) = f a₂ (f a₁ b)
.
Equations
- Multiset.foldr f H b s = Quot.liftOn s (fun (l : List α) => List.foldr f b l) (_ : ∀ (_l₁ _l₂ : List α), Setoid.r _l₁ _l₂ → List.foldr f b _l₁ = List.foldr f b _l₂)
Instances For
Map for partial functions #
Lift of the list pmap
operation. Map a partial function f
over a multiset
s
whose elements are all in the domain of f
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
"Attach" a proof that a ∈ s
to each element a
in s
to produce
a multiset on {x // x ∈ s}
.
Equations
- Multiset.attach s = Multiset.pmap Subtype.mk s (_ : ∀ _a ∈ s, _a ∈ s)
Instances For
If p
is a decidable predicate,
so is the predicate that all elements of a multiset satisfy p
.
Equations
- Multiset.decidableForallMultiset = Quotient.recOnSubsingleton m fun (l : List α) => decidable_of_iff (∀ a ∈ l, p a) (_ : (∀ a ∈ l, p a) ↔ ∀ a ∈ ↑l, p a)
Instances For
Equations
- Multiset.decidableDforallMultiset = decidable_of_iff (∀ a ∈ Multiset.attach m, p ↑a (_ : ↑a ∈ m)) (_ : (∀ a ∈ Multiset.attach m, p ↑a (_ : ↑a ∈ m)) ↔ ∀ (a : α) (h : a ∈ m), p a h)
decidable equality for functions whose domain is bounded by multisets
Equations
- Multiset.decidableEqPiMultiset f g = decidable_of_iff (∀ (a : α) (h : a ∈ m), f a h = g a h) (_ : (∀ (a : α) (h : a ∈ m), f a h = g a h) ↔ f = g)
If p
is a decidable predicate,
so is the existence of an element in a multiset satisfying p
.
Equations
- Multiset.decidableExistsMultiset = Quotient.recOnSubsingleton m fun (l : List α) => decidable_of_iff (∃ a ∈ l, p a) (_ : (∃ a ∈ l, p a) ↔ ∃ x ∈ ↑l, p x)
Instances For
Equations
- Multiset.decidableDexistsMultiset = decidable_of_iff (∃ x ∈ Multiset.attach m, p ↑x (_ : ↑x ∈ m)) (_ : (∃ x ∈ Multiset.attach m, p ↑x (_ : ↑x ∈ m)) ↔ ∃ (a : α) (h : a ∈ m), p a h)
Subtraction #
Equations
- Multiset.instSubMultiset = { sub := Multiset.sub }
This is a special case of tsub_zero
, which should be used instead of this.
This is needed to prove OrderedSub (Multiset α)
.
This is a special case of tsub_le_iff_right
, which should be used instead of this.
This is needed to prove OrderedSub (Multiset α)
.
Equations
- (_ : OrderedSub (Multiset α)) = (_ : OrderedSub (Multiset α))
Union #
s ∪ t
is the lattice join operation with respect to the
multiset ≤
. The multiplicity of a
in s ∪ t
is the maximum
of the multiplicities in s
and t
.
Equations
- Multiset.union s t = s - t + t
Instances For
Equations
- Multiset.instUnionMultiset = { union := Multiset.union }
Intersection #
s ∩ t
is the lattice meet operation with respect to the
multiset ≤
. The multiplicity of a
in s ∩ t
is the minimum
of the multiplicities in s
and t
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Multiset.instInterMultiset = { inter := Multiset.inter }
Filter p s
returns the elements in s
(with the same multiplicities)
which satisfy p
, and removes the rest.
Equations
- One or more equations did not get rendered due to their size.
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
- One or more equations did not get rendered due to their size.
Instances For
countP #
countP p s
counts the number of elements of s
(with multiplicity) that
satisfy p
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
countP p
, the number of elements of a multiset satisfying p
, promoted to an
AddMonoidHom
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Multiplicity of an element #
count a s
is the multiplicity of a
in s
.
Equations
- Multiset.count a = Multiset.countP fun (x : α) => a = x
Instances For
count a
, the multiplicity of a
in a multiset, promoted to an AddMonoidHom
.
Equations
- Multiset.countAddMonoidHom a = Multiset.countPAddMonoidHom fun (x : α) => a = x
Instances For
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
Associate to an embedding f
from α
to β
the order embedding that maps a multiset to its
image under f
.
Equations
- Multiset.mapEmbedding f = OrderEmbedding.ofMapLEIff (Multiset.map ⇑f) (_ : ∀ (x x_1 : Multiset α), Multiset.map f.toFun x ≤ Multiset.map f.toFun x_1 ↔ x ≤ x_1)
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.
Rel r s t
-- lift the relation r
between two elements to a relation between s
and t
,
s.t. there is a one-to-one mapping between elements in s
and t
following r
.
- zero: ∀ {α : Type u_1} {β : Type v} {r : α → β → Prop}, Multiset.Rel r 0 0
- cons: ∀ {α : Type u_1} {β : Type v} {r : α → β → Prop} {a : α} {b : β} {as : Multiset α} {bs : Multiset β}, r a b → Multiset.Rel r as bs → Multiset.Rel r (a ::ₘ as) (b ::ₘ bs)
Instances For
Disjoint multisets #
Pairwise r m
states that there exists a list of the elements s.t. r
holds pairwise on this
list.
Equations
- Multiset.Pairwise r m = ∃ (l : List α), m = ↑l ∧ List.Pairwise r l
Instances For
Given a proof hp
that there exists a unique a ∈ l
such that p a
, chooseX p l hp
returns
that a
together with proofs of a ∈ l
and p a
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Given a proof hp
that there exists a unique a ∈ l
such that p a
, choose p l hp
returns
that a
.
Equations
- Multiset.choose p l hp = ↑(Multiset.chooseX p l hp)
Instances For
The equivalence between lists and multisets of a subsingleton type.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Deprecated lemmas #
Those lemmas have been deprecated on 2023-12-27.
Alias of Multiset.card_le_card
.
Alias of Multiset.card_lt_card
.