An ordered finite set of elements of type k, backed by a red-black tree.
$$\text{Set'}(k) \cong \text{RBMap}(k, \text{Unit}, \text{compare})$$
Provides $O(\log n)$ membership, insert, and delete.
Named Set' to avoid clashing with Lean's Set (α → Prop).
Equations
- Data.Set' k = Lean.RBMap k Unit compare
Instances For
The empty set. $$\text{empty} = \emptyset$$
Equations
Instances For
A set with a single element. $$\text{singleton}(x) = \{x\}$$
Equations
Instances For
Build a set from a list of elements. $$\text{fromList}([x_1, \ldots, x_n]) = \{x_1, \ldots, x_n\}$$
Equations
- Data.Set'.fromList l = List.foldl (fun (s : Data.Set' k) (x : k) => Lean.RBMap.insert s x ()) Lean.RBMap.empty l
Instances For
Test membership. $$\text{member}(x, s) \iff x \in s$$
Equations
- Data.Set'.member x s = Lean.RBMap.contains s x
Instances For
Is the set empty? $$\text{null}(s) \iff s = \emptyset$$
Equations
- s.null = Lean.RBMap.isEmpty s
Instances For
The number of elements. $$\text{size}(s) = |s|$$
Equations
- s.size' = Lean.RBMap.size s
Instances For
Insert an element. $$\text{insert}(x, s) = s \cup \{x\}$$
Equations
- Data.Set'.insert' x s = Lean.RBMap.insert s x ()
Instances For
Delete an element. $$\text{delete}(x, s) = s \setminus \{x\}$$
Equations
- Data.Set'.delete x s = Lean.RBMap.erase s x
Instances For
Intersection of two sets. $$\text{intersection}(s_1, s_2) = s_1 \cap s_2$$
Equations
- s1.intersection s2 = Lean.RBMap.intersectBy (fun (x : k) (x_1 x_2 : Unit) => ()) s1 s2
Instances For
Difference of two sets. $$\text{difference}(s_1, s_2) = s_1 \setminus s_2$$
Equations
- s1.difference s2 = Lean.RBMap.filter (fun (key : k) (x : Unit) => !Lean.RBMap.contains s2 key) s1
Instances For
Is s1 a subset of s2?
$$\text{isSubsetOf}(s_1, s_2) \iff s_1 \subseteq s_2$$
Equations
- s1.isSubsetOf s2 = Lean.RBMap.all s1 fun (key : k) (x : Unit) => Lean.RBMap.contains s2 key
Instances For
Map a function over all elements. The result may be smaller if f maps
distinct elements to the same value.
$$\text{map}(f, s) = \{f(x) \mid x \in s\}$$
Equations
- Data.Set'.mapSet f s = Lean.RBMap.fold (fun (acc : Data.Set' k₂) (key : k) (x : Unit) => Lean.RBMap.insert acc (f key) ()) Lean.RBMap.empty s
Instances For
Filter elements satisfying a predicate. $$\text{filter}(p, s) = \{x \in s \mid p(x)\}$$
Equations
- Data.Set'.filter p s = Lean.RBMap.filter (fun (key : k) (x : Unit) => p key) s
Instances For
Left fold over elements in ascending order. $$\text{foldl}(f, z, s) = f(\ldots f(f(z, x_1), x_2) \ldots, x_n)$$
Equations
- Data.Set'.foldl f init s = Lean.RBMap.fold (fun (acc : α) (key : k) (x : Unit) => f acc key) init s
Instances For
Right fold over elements in ascending order. $$\text{foldr}(f, z, s) = f(x_1, f(x_2, \ldots f(x_n, z)))$$
Equations
- Data.Set'.foldr f init s = Lean.RBMap.revFold (fun (acc : α) (key : k) (x : Unit) => f key acc) init s
Instances For
Equations
- Data.Set'.instEmptyCollection = { emptyCollection := Data.Set'.empty }
Equations
- Data.Set'.instInhabited = { default := Data.Set'.empty }