Documentation

Std.Data.DHashMap.Basic

Dependent hash maps #

This file develops the type Std.Data.DHashMap of dependent hash maps.

The operations map and filterMap on Std.Data.DHashMap are defined in the module Std.Data.DHashMap.AdditionalOperations.

Lemmas about the operations on Std.Data.DHashMap are available in the module Std.Data.DHashMap.Lemmas.

See the module Std.Data.DHashMap.Raw for a variant of this type which is safe to use in nested inductive types.

For implementation notes, see the docstring of the module Std.Data.DHashMap.Internal.Defs.

def Std.DHashMap (α : Type u) (β : αType v) [BEq α] [Hashable α] :
Type (max 0 u v)

Dependent hash maps.

This is a simple separate-chaining hash table. The data of the hash map consists of a cached size and an array of buckets, where each bucket is a linked list of key-value pais. The number of buckets is always a power of two. The hash map doubles its size upon inserting an element such that the number of elements is more than 75% of the number of buckets.

The hash table is backed by an Array. Users should make sure that the hash map is used linearly to avoid expensive copies.

The hash map uses == (provided by the BEq typeclass) to compare keys and hash (provided by the Hashable typeclass) to hash them. To ensure that the operations behave as expected, == should be an equivalence relation and a == b should imply hash a = hash b (see also the EquivBEq and LawfulHashable typeclasses). Both of these conditions are automatic if the BEq instance is lawful, i.e., if a == b implies a = b.

These hash maps contain a bundled well-formedness invariant, which means that they cannot be used in nested inductive types. For these use cases, Std.Data.DHashMap.Raw and Std.Data.DHashMap.Raw.WF unbundle the invariant from the hash map. When in doubt, prefer DHashMap over DHashMap.Raw.

Equations
Instances For
    @[inline]
    def Std.DHashMap.empty {α : Type u} {β : αType v} [BEq α] [Hashable α] (capacity : optParam Nat 8) :

    Creates a new empty hash map. The optional parameter capacity can be supplied to presize the map so that it can hold the given number of mappings without reallocating. It is also possible to use the empty collection notations and {} to create an empty hash map with the default capacity.

    Equations
    Instances For
      instance Std.DHashMap.instEmptyCollection {α : Type u} {β : αType v} [BEq α] [Hashable α] :
      Equations
      • Std.DHashMap.instEmptyCollection = { emptyCollection := Std.DHashMap.empty }
      instance Std.DHashMap.instInhabited {α : Type u} {β : αType v} [BEq α] [Hashable α] :
      Equations
      • Std.DHashMap.instInhabited = { default := }
      @[inline]
      def Std.DHashMap.insert {α : Type u} {β : αType v} :
      {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α β(a : α) → β aStd.DHashMap α β

      Inserts the given mapping into the map, replacing an existing mapping for the key if there is one.

      Equations
      Instances For
        instance Std.DHashMap.instSingletonSigma {α : Type u} {β : αType v} :
        {x : BEq α} → {x_1 : Hashable α} → Singleton ((a : α) × β a) (Std.DHashMap α β)
        Equations
        • Std.DHashMap.instSingletonSigma = { singleton := fun (x_1 : (a : α) × β a) => match x_1 with | a, b => Std.DHashMap.empty.insert a b }
        instance Std.DHashMap.instInsertSigma {α : Type u} {β : αType v} :
        {x : BEq α} → {x_1 : Hashable α} → Insert ((a : α) × β a) (Std.DHashMap α β)
        Equations
        • Std.DHashMap.instInsertSigma = { insert := fun (x_1 : (a : α) × β a) (s : Std.DHashMap α β) => match x_1 with | a, b => s.insert a b }
        instance Std.DHashMap.instLawfulSingletonSigma {α : Type u} {β : αType v} :
        ∀ {x : BEq α} {x_1 : Hashable α}, LawfulSingleton ((a : α) × β a) (Std.DHashMap α β)
        Equations
        • =
        @[inline]
        def Std.DHashMap.insertIfNew {α : Type u} {β : αType v} :
        {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α β(a : α) → β aStd.DHashMap α β

        If there is no mapping for the given key, inserts the given mapping into the map. Otherwise, returns the map unaltered.

        Equations
        Instances For
          @[inline]
          def Std.DHashMap.containsThenInsert {α : Type u} {β : αType v} :
          {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α β(a : α) → β aBool × Std.DHashMap α β

          Checks whether a key is present in a map, and unconditionally inserts a value for the key.

          Equivalent to (but potentially faster than) calling contains followed by insert.

          Equations
          Instances For
            @[inline]
            def Std.DHashMap.containsThenInsertIfNew {α : Type u} {β : αType v} :
            {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α β(a : α) → β aBool × Std.DHashMap α β

            Checks whether a key is present in a map and inserts a value for the key if it was not found.

            If the returned Bool is true, then the returned map is unaltered. If the Bool is false, then the returned map has a new value inserted.

            Equivalent to (but potentially faster than) calling contains followed by insertIfNew.

            Equations
            Instances For
              @[inline]
              def Std.DHashMap.getThenInsertIfNew? {α : Type u} {β : αType v} :
              {x : BEq α} → {x_1 : Hashable α} → [inst : LawfulBEq α] → Std.DHashMap α β(a : α) → β aOption (β a) × Std.DHashMap α β

              Checks whether a key is present in a map, returning the associated value, and inserts a value for the key if it was not found.

              If the returned value is some v, then the returned map is unaltered. If it is none, then the returned map has a new value inserted.

              Equivalent to (but potentially faster than) calling get? followed by insertIfNew.

              Uses the LawfulBEq instance to cast the retrieved value to the correct type.

              Equations
              Instances For
                @[inline]
                def Std.DHashMap.get? {α : Type u} {β : αType v} :
                {x : BEq α} → {x_1 : Hashable α} → [inst : LawfulBEq α] → Std.DHashMap α β(a : α) → Option (β a)

                Tries to retrieve the mapping for the given key, returning none if no such mapping is present.

                Uses the LawfulBEq instance to cast the retrieved value to the correct type.

                Equations
                Instances For
                  @[inline]
                  def Std.DHashMap.contains {α : Type u} {β : αType v} :
                  {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α βαBool

                  Returns true if there is a mapping for the given key. There is also a Prop-valued version of this: a ∈ m is equivalent to m.contains a = true.

                  Observe that this is different behavior than for lists: for lists, uses = and contains uses == for comparisons, while for hash maps, both use ==.

                  Equations
                  Instances For
                    instance Std.DHashMap.instMembership {α : Type u} {β : αType v} [BEq α] [Hashable α] :
                    Equations
                    • Std.DHashMap.instMembership = { mem := fun (m : Std.DHashMap α β) (a : α) => m.contains a = true }
                    instance Std.DHashMap.instDecidableMem {α : Type u} {β : αType v} [BEq α] [Hashable α] {m : Std.DHashMap α β} {a : α} :
                    Equations
                    • Std.DHashMap.instDecidableMem = inferInstance
                    @[inline]
                    def Std.DHashMap.get {α : Type u} {β : αType v} :
                    {x : BEq α} → {x_1 : Hashable α} → [inst : LawfulBEq α] → (m : Std.DHashMap α β) → (a : α) → a mβ a

                    Retrieves the mapping for the given key. Ensures that such a mapping exists by requiring a proof of a ∈ m.

                    Uses the LawfulBEq instance to cast the retrieved value to the correct type.

                    Equations
                    Instances For
                      @[inline]
                      def Std.DHashMap.get! {α : Type u} {β : αType v} :
                      {x : BEq α} → {x_1 : Hashable α} → [inst : LawfulBEq α] → Std.DHashMap α β(a : α) → [inst : Inhabited (β a)] → β a

                      Tries to retrieve the mapping for the given key, panicking if no such mapping is present.

                      Uses the LawfulBEq instance to cast the retrieved value to the correct type.

                      Equations
                      Instances For
                        @[inline]
                        def Std.DHashMap.getD {α : Type u} {β : αType v} :
                        {x : BEq α} → {x_1 : Hashable α} → [inst : LawfulBEq α] → Std.DHashMap α β(a : α) → β aβ a

                        Tries to retrieve the mapping for the given key, returning fallback if no such mapping is present.

                        Uses the LawfulBEq instance to cast the retrieved value to the correct type.

                        Equations
                        Instances For
                          @[inline]
                          def Std.DHashMap.erase {α : Type u} {β : αType v} :
                          {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α βαStd.DHashMap α β

                          Removes the mapping for the given key if it exists.

                          Equations
                          Instances For
                            @[inline]
                            def Std.DHashMap.Const.get? {α : Type u} :
                            {x : BEq α} → {x_1 : Hashable α} → {β : Type v} → (Std.DHashMap α fun (x : α) => β)αOption β

                            Tries to retrieve the mapping for the given key, returning none if no such mapping is present.

                            Equations
                            Instances For
                              @[inline]
                              def Std.DHashMap.Const.get {α : Type u} :
                              {x : BEq α} → {x_1 : Hashable α} → {β : Type v} → (m : Std.DHashMap α fun (x : α) => β) → (a : α) → a mβ

                              Retrieves the mapping for the given key. Ensures that such a mapping exists by requiring a proof of a ∈ m.

                              Equations
                              Instances For
                                @[inline]
                                def Std.DHashMap.Const.getD {α : Type u} :
                                {x : BEq α} → {x_1 : Hashable α} → {β : Type v} → (Std.DHashMap α fun (x : α) => β)αββ

                                Tries to retrieve the mapping for the given key, returning fallback if no such mapping is present.

                                Equations
                                Instances For
                                  @[inline]
                                  def Std.DHashMap.Const.get! {α : Type u} :
                                  {x : BEq α} → {x_1 : Hashable α} → {β : Type v} → [inst : Inhabited β] → (Std.DHashMap α fun (x : α) => β)αβ

                                  Tries to retrieve the mapping for the given key, panicking if no such mapping is present.

                                  Equations
                                  Instances For
                                    @[inline]
                                    def Std.DHashMap.Const.getThenInsertIfNew? {α : Type u} :
                                    {x : BEq α} → {x_1 : Hashable α} → {β : Type v} → (Std.DHashMap α fun (x : α) => β)αβOption β × Std.DHashMap α fun (x : α) => β

                                    Equivalent to (but potentially faster than) calling Const.get? followed by insertIfNew.

                                    Checks whether a key is present in a map, returning the associated value, and inserts a value for the key if it was not found.

                                    If the returned value is some v, then the returned map is unaltered. If it is none, then the returned map has a new value inserted.

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      @[inline]
                                      def Std.DHashMap.getKey? {α : Type u} {β : αType v} :
                                      {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α βαOption α

                                      Checks if a mapping for the given key exists and returns the key if it does, otherwise none. The result in the some case is guaranteed to be pointer equal to the key in the map.

                                      Equations
                                      Instances For
                                        @[inline]
                                        def Std.DHashMap.getKey {α : Type u} {β : αType v} :
                                        {x : BEq α} → {x_1 : Hashable α} → (m : Std.DHashMap α β) → (a : α) → a mα

                                        Retrieves the key from the mapping that matches a. Ensures that such a mapping exists by requiring a proof of a ∈ m. The result is guaranteed to be pointer equal to the key in the map.

                                        Equations
                                        Instances For
                                          @[inline]
                                          def Std.DHashMap.getKey! {α : Type u} {β : αType v} :
                                          {x : BEq α} → {x_1 : Hashable α} → [inst : Inhabited α] → Std.DHashMap α βαα

                                          Checks if a mapping for the given key exists and returns the key if it does, otherwise panics. If no panic occurs the result is guaranteed to be pointer equal to the key in the map.

                                          Equations
                                          Instances For
                                            @[inline]
                                            def Std.DHashMap.getKeyD {α : Type u} {β : αType v} :
                                            {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α βααα

                                            Checks if a mapping for the given key exists and returns the key if it does, otherwise fallback. If a mapping exists the result is guaranteed to be pointer equal to the key in the map.

                                            Equations
                                            Instances For
                                              @[inline]
                                              def Std.DHashMap.size {α : Type u} {β : αType v} :
                                              {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α βNat

                                              The number of mappings present in the hash map

                                              Equations
                                              • m.size = m.val.size
                                              Instances For
                                                @[inline]
                                                def Std.DHashMap.isEmpty {α : Type u} {β : αType v} :
                                                {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α βBool

                                                Returns true if the hash map contains no mappings.

                                                Note that if your BEq instance is not reflexive or your Hashable instance is not lawful, then it is possible that this function returns false even though is not possible to get anything out of the hash map.

                                                Equations
                                                • m.isEmpty = m.val.isEmpty
                                                Instances For

                                                  We currently do not provide lemmas for the functions below.

                                                  @[inline]
                                                  def Std.DHashMap.filter {α : Type u} {β : αType v} :
                                                  {x : BEq α} → {x_1 : Hashable α} → ((a : α) → β aBool)Std.DHashMap α βStd.DHashMap α β

                                                  Removes all mappings of the hash map for which the given function returns false.

                                                  Equations
                                                  Instances For
                                                    @[inline]
                                                    def Std.DHashMap.foldM {α : Type u} {β : αType v} {δ : Type w} {m : Type w → Type w} [Monad m] :
                                                    {x : BEq α} → {x_1 : Hashable α} → (δ(a : α) → β am δ)δStd.DHashMap α βm δ

                                                    Monadically computes a value by folding the given function over the mappings in the hash map in some order.

                                                    Equations
                                                    Instances For
                                                      @[inline]
                                                      def Std.DHashMap.fold {α : Type u} {β : αType v} {δ : Type w} :
                                                      {x : BEq α} → {x_1 : Hashable α} → (δ(a : α) → β aδ)δStd.DHashMap α βδ

                                                      Folds the given function over the mappings in the hash map in some order.

                                                      Equations
                                                      Instances For
                                                        @[inline]
                                                        def Std.DHashMap.partition {α : Type u} {β : αType v} :
                                                        {x : BEq α} → {x_1 : Hashable α} → ((a : α) → β aBool)Std.DHashMap α βStd.DHashMap α β × Std.DHashMap α β

                                                        Partition a hashset into two hashsets based on a predicate.

                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        Instances For
                                                          @[inline]
                                                          def Std.DHashMap.forM {α : Type u} {β : αType v} {m : Type w → Type w} [Monad m] :
                                                          {x : BEq α} → {x_1 : Hashable α} → ((a : α) → β am PUnit)Std.DHashMap α βm PUnit

                                                          Carries out a monadic action on each mapping in the hash map in some order.

                                                          Equations
                                                          Instances For
                                                            @[inline]
                                                            def Std.DHashMap.forIn {α : Type u} {β : αType v} {δ : Type w} {m : Type w → Type w} [Monad m] :
                                                            {x : BEq α} → {x_1 : Hashable α} → ((a : α) → β aδm (ForInStep δ))δStd.DHashMap α βm δ

                                                            Support for the for loop construct in do blocks.

                                                            Equations
                                                            Instances For
                                                              instance Std.DHashMap.instForMSigma {α : Type u} {β : αType v} {m : Type w → Type w} [BEq α] [Hashable α] :
                                                              ForM m (Std.DHashMap α β) ((a : α) × β a)
                                                              Equations
                                                              instance Std.DHashMap.instForInSigma {α : Type u} {β : αType v} {m : Type w → Type w} [BEq α] [Hashable α] :
                                                              ForIn m (Std.DHashMap α β) ((a : α) × β a)
                                                              Equations
                                                              • One or more equations did not get rendered due to their size.
                                                              @[inline]
                                                              def Std.DHashMap.toList {α : Type u} {β : αType v} :
                                                              {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α βList ((a : α) × β a)

                                                              Transforms the hash map into a list of mappings in some order.

                                                              Equations
                                                              • m.toList = m.val.toList
                                                              Instances For
                                                                @[inline]
                                                                def Std.DHashMap.toArray {α : Type u} {β : αType v} :
                                                                {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α βArray ((a : α) × β a)

                                                                Transforms the hash map into an array of mappings in some order.

                                                                Equations
                                                                • m.toArray = m.val.toArray
                                                                Instances For
                                                                  @[inline]
                                                                  def Std.DHashMap.Const.toList {α : Type u} :
                                                                  {x : BEq α} → {x_1 : Hashable α} → {β : Type v} → (Std.DHashMap α fun (x : α) => β)List (α × β)

                                                                  Transforms the hash map into a list of mappings in some order.

                                                                  Equations
                                                                  Instances For
                                                                    @[inline]
                                                                    def Std.DHashMap.Const.toArray {α : Type u} :
                                                                    {x : BEq α} → {x_1 : Hashable α} → {β : Type v} → (Std.DHashMap α fun (x : α) => β)Array (α × β)

                                                                    Transforms the hash map into an array of mappings in some order.

                                                                    Equations
                                                                    Instances For
                                                                      @[inline]
                                                                      def Std.DHashMap.keys {α : Type u} {β : αType v} :
                                                                      {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α βList α

                                                                      Returns a list of all keys present in the hash map in some order.

                                                                      Equations
                                                                      • m.keys = m.val.keys
                                                                      Instances For
                                                                        @[inline]
                                                                        def Std.DHashMap.keysArray {α : Type u} {β : αType v} :
                                                                        {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α βArray α

                                                                        Returns an array of all keys present in the hash map in some order.

                                                                        Equations
                                                                        • m.keysArray = m.val.keysArray
                                                                        Instances For
                                                                          @[inline]
                                                                          def Std.DHashMap.values {α : Type u} :
                                                                          {x : BEq α} → {x_1 : Hashable α} → {β : Type v} → (Std.DHashMap α fun (x : α) => β)List β

                                                                          Returns a list of all values present in the hash map in some order.

                                                                          Equations
                                                                          • m.values = m.val.values
                                                                          Instances For
                                                                            @[inline]
                                                                            def Std.DHashMap.valuesArray {α : Type u} :
                                                                            {x : BEq α} → {x_1 : Hashable α} → {β : Type v} → (Std.DHashMap α fun (x : α) => β)Array β

                                                                            Returns an array of all values present in the hash map in some order.

                                                                            Equations
                                                                            • m.valuesArray = m.val.valuesArray
                                                                            Instances For
                                                                              @[inline]
                                                                              def Std.DHashMap.insertMany {α : Type u} {β : αType v} :
                                                                              {x : BEq α} → {x_1 : Hashable α} → {ρ : Type w} → [inst : ForIn Id ρ ((a : α) × β a)] → Std.DHashMap α βρStd.DHashMap α β

                                                                              Inserts multiple mappings into the hash map by iterating over the given collection and calling insert. If the same key appears multiple times, the last occurrence takes precedence.

                                                                              Equations
                                                                              Instances For
                                                                                @[inline]
                                                                                def Std.DHashMap.Const.insertMany {α : Type u} :
                                                                                {x : BEq α} → {x_1 : Hashable α} → {β : Type v} → {ρ : Type w} → [inst : ForIn Id ρ (α × β)] → (Std.DHashMap α fun (x : α) => β)ρStd.DHashMap α fun (x : α) => β

                                                                                Inserts multiple mappings into the hash map by iterating over the given collection and calling insert. If the same key appears multiple times, the last occurrence takes precedence.

                                                                                Equations
                                                                                Instances For
                                                                                  @[inline]
                                                                                  def Std.DHashMap.Const.insertManyUnit {α : Type u} :
                                                                                  {x : BEq α} → {x_1 : Hashable α} → {ρ : Type w} → [inst : ForIn Id ρ α] → (Std.DHashMap α fun (x : α) => Unit)ρStd.DHashMap α fun (x : α) => Unit

                                                                                  Inserts multiple keys with the value () into the hash map by iterating over the given collection and calling insert. If the same key appears multiple times, the last occurrence takes precedence.

                                                                                  This is mainly useful to implement HashSet.insertMany, so if you are considering using this, HashSet or HashSet.Raw might be a better fit for you.

                                                                                  Equations
                                                                                  Instances For
                                                                                    @[inline]
                                                                                    def Std.DHashMap.ofList {α : Type u} {β : αType v} [BEq α] [Hashable α] (l : List ((a : α) × β a)) :

                                                                                    Creates a hash map from a list of mappings. If the same key appears multiple times, the last occurrence takes precedence.

                                                                                    Equations
                                                                                    Instances For
                                                                                      @[inline]
                                                                                      def Std.DHashMap.union {α : Type u} {β : αType v} [BEq α] [Hashable α] (m₁ : Std.DHashMap α β) (m₂ : Std.DHashMap α β) :

                                                                                      Computes the union of the given hash maps, by traversing m₂ and inserting its elements into m₁.

                                                                                      Equations
                                                                                      Instances For
                                                                                        instance Std.DHashMap.instUnion {α : Type u} {β : αType v} [BEq α] [Hashable α] :
                                                                                        Equations
                                                                                        • Std.DHashMap.instUnion = { union := Std.DHashMap.union }
                                                                                        @[inline]
                                                                                        def Std.DHashMap.Const.ofList {α : Type u} {β : Type v} [BEq α] [Hashable α] (l : List (α × β)) :
                                                                                        Std.DHashMap α fun (x : α) => β

                                                                                        Creates a hash map from a list of mappings. If the same key appears multiple times, the last occurrence takes precedence.

                                                                                        Equations
                                                                                        Instances For
                                                                                          @[inline]
                                                                                          def Std.DHashMap.Const.unitOfList {α : Type u} [BEq α] [Hashable α] (l : List α) :
                                                                                          Std.DHashMap α fun (x : α) => Unit

                                                                                          Creates a hash map from a list of keys, associating the value () with each key.

                                                                                          This is mainly useful to implement HashSet.ofList, so if you are considering using this, HashSet or HashSet.Raw might be a better fit for you.

                                                                                          Equations
                                                                                          Instances For
                                                                                            @[inline]
                                                                                            def Std.DHashMap.Const.unitOfArray {α : Type u} [BEq α] [Hashable α] (l : Array α) :
                                                                                            Std.DHashMap α fun (x : α) => Unit

                                                                                            Creates a hash map from an array of keys, associating the value () with each key.

                                                                                            This is mainly useful to implement HashSet.ofArray, so if you are considering using this, HashSet or HashSet.Raw might be a better fit for you.

                                                                                            Equations
                                                                                            Instances For
                                                                                              def Std.DHashMap.Internal.numBuckets {α : Type u} {β : αType v} :
                                                                                              {x : BEq α} → {x_1 : Hashable α} → Std.DHashMap α βNat

                                                                                              Returns the number of buckets in the internal representation of the hash map. This function may be useful for things like monitoring system health, but it should be considered an internal implementation detail.

                                                                                              Equations
                                                                                              Instances For
                                                                                                instance Std.DHashMap.instRepr {α : Type u} {β : αType v} [BEq α] [Hashable α] [Repr α] [(a : α) → Repr (β a)] :
                                                                                                Equations