Mikan
Safe HaskellNone
LanguageHaskell2010

Mikan.Utils.BiMap

Description

Partly invertible finite maps.

Time complexities are given under the assumption that all relevant instance functions, as well as arguments of function type, take constant time, and "n" is the number of keys involved in the operation.

Synopsis

Documentation

data BiMap k v Source #

Finite maps from k to v, with a way to quickly get from v to k for certain values of type v (those for which tag is defined).

Every value of this type must satisfy biMapInvariant.

Constructors

BiMap 

Fields

Instances

Instances details
NFData InteractionPoints Source # 
Instance details

Defined in Mikan.TypeChecking.Monad.Base

Methods

rnf :: InteractionPoints -> () #

(EmbPrj k, EmbPrj v, EmbPrj (Tag v)) => EmbPrj (BiMap k v) Source # 
Instance details

Defined in Mikan.TypeChecking.Serialise.Instances.General

Methods

icode :: BiMap k v -> S Word32 Source #

icod_ :: BiMap k v -> S Word32 Source #

value :: Word32 -> R (BiMap k v) Source #

Null (BiMap k v) Source # 
Instance details

Defined in Mikan.Utils.BiMap

Methods

empty :: BiMap k v Source #

null :: BiMap k v -> Bool Source #

NFData (BiMap RawTopLevelModuleName ModuleNameHash) Source # 
Instance details

Defined in Mikan.TypeChecking.Monad.Base

Generic (BiMap k v) Source # 
Instance details

Defined in Mikan.Utils.BiMap

Associated Types

type Rep (BiMap k v) 
Instance details

Defined in Mikan.Utils.BiMap

type Rep (BiMap k v) = D1 ('MetaData "BiMap" "Mikan.Utils.BiMap" "Mikan-2.9.0-CZbYRMbHmng2A9zjJ31VFu" 'False) (C1 ('MetaCons "BiMap" 'PrefixI 'True) (S1 ('MetaSel ('Just "biMapThere") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Map k v)) :*: S1 ('MetaSel ('Just "biMapBack") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Map (Tag v) k))))

Methods

from :: BiMap k v -> Rep (BiMap k v) x #

to :: Rep (BiMap k v) x -> BiMap k v #

(Show k, Show v) => Show (BiMap k v) Source # 
Instance details

Defined in Mikan.Utils.BiMap

Methods

showsPrec :: Int -> BiMap k v -> ShowS #

show :: BiMap k v -> String #

showList :: [BiMap k v] -> ShowS #

(Eq k, Eq v) => Eq (BiMap k v) Source # 
Instance details

Defined in Mikan.Utils.BiMap

Methods

(==) :: BiMap k v -> BiMap k v -> Bool #

(/=) :: BiMap k v -> BiMap k v -> Bool #

(Ord k, Ord v) => Ord (BiMap k v) Source # 
Instance details

Defined in Mikan.Utils.BiMap

Methods

compare :: BiMap k v -> BiMap k v -> Ordering #

(<) :: BiMap k v -> BiMap k v -> Bool #

(<=) :: BiMap k v -> BiMap k v -> Bool #

(>) :: BiMap k v -> BiMap k v -> Bool #

(>=) :: BiMap k v -> BiMap k v -> Bool #

max :: BiMap k v -> BiMap k v -> BiMap k v #

min :: BiMap k v -> BiMap k v -> BiMap k v #

type Rep (BiMap k v) Source # 
Instance details

Defined in Mikan.Utils.BiMap

type Rep (BiMap k v) = D1 ('MetaData "BiMap" "Mikan.Utils.BiMap" "Mikan-2.9.0-CZbYRMbHmng2A9zjJ31VFu" 'False) (C1 ('MetaCons "BiMap" 'PrefixI 'True) (S1 ('MetaSel ('Just "biMapThere") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Map k v)) :*: S1 ('MetaSel ('Just "biMapBack") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Map (Tag v) k))))

class HasTag a where Source #

Partial injections from a type to some tag type.

The idea is that tag should be injective on its domain: if tag x = tag y = Just i, then x = y. However, this property does not need to hold globally. The preconditions of the BiMap operations below specify for which sets of values tag must be injective.

Associated Types

type Tag a Source #

Methods

tag :: a -> Maybe (Tag a) Source #

Instances

Instances details
HasTag ModuleNameHash Source # 
Instance details

Defined in Mikan.Syntax.TopLevelModuleName.Boot

Associated Types

type Tag ModuleNameHash 
Instance details

Defined in Mikan.Syntax.TopLevelModuleName.Boot

HasTag InteractionPoint Source # 
Instance details

Defined in Mikan.TypeChecking.Monad.Base

Associated Types

type Tag InteractionPoint 
Instance details

Defined in Mikan.TypeChecking.Monad.Base

HasTag (TopLevelModuleName' range) Source # 
Instance details

Defined in Mikan.Syntax.TopLevelModuleName.Boot

Associated Types

type Tag (TopLevelModuleName' range) 
Instance details

Defined in Mikan.Syntax.TopLevelModuleName.Boot

Queries

source :: Ord k => k -> BiMap k v -> Bool Source #

\(\mathcal{O}(\log n)\). Is the value a source key?

target :: Ord (Tag v) => Tag v -> BiMap k v -> Bool Source #

\(\mathcal{O}(\log n)\). Is the value a target key?

lookup :: Ord k => k -> BiMap k v -> Maybe v Source #

\(\mathcal{O}(\log n)\). Look up a key.

invLookup :: Ord (Tag v) => Tag v -> BiMap k v -> Maybe k Source #

\(\mathcal{O}(\log n)\). Inverse key lookup.

Creation

singleton :: HasTag v => k -> v -> BiMap k v Source #

\(\mathcal{O}(1)\). Create a singleton map.

Insertion

insert :: (Ord k, HasTag v, Ord (Tag v)) => k -> v -> BiMap k v -> BiMap k v Source #

\(\mathcal{O}(\log n)\). Insert an element into a map. Overwrites existing values.

Precondition: See insertPrecondition.

insertLookupWithKey :: (Ord k, Ord (Tag v), HasTag v) => (k -> v -> v -> v) -> k -> v -> BiMap k v -> (Maybe v, BiMap k v) Source #

Inserts a binding into the map. If a binding for the key already exists, then the value obtained by applying the function to the key, the new value and the old value is inserted, and the old value is returned.

Precondition: See insertLookupWithKeyPrecondition.

Update

alter :: (Ord k, Ord (Tag v), HasTag v) => (Maybe v -> Maybe v) -> k -> BiMap k v -> BiMap k v Source #

\(\mathcal{O}(\log n)\). Modifies the value at the given position, if any. If the function returns Nothing, then the value is removed.

Precondition: See alterPrecondition.

alterM :: (Ord k, Ord (Tag v), HasTag v, Monad m) => (Maybe v -> m (Maybe v)) -> k -> BiMap k v -> m (BiMap k v) Source #

(mathcal{O}(log n).) Modifies the value at the given position, if any. If the function returns Nothing, then the value is removed.

The precondition for alterM f k m is that, if the value v is inserted into m, and tag v is defined, then no key other than k may map to a value v' for which tag v' = tag v.

update :: (Ord k, Ord (Tag v), HasTag v) => (v -> Maybe v) -> k -> BiMap k v -> BiMap k v Source #

\(\mathcal{O}(\log n)\). Modifies the value at the given position, if any. If the function returns Nothing, then the value is removed.

Precondition: See updatePrecondition.

adjust :: (Ord k, Ord (Tag v), HasTag v) => (v -> v) -> k -> BiMap k v -> BiMap k v Source #

\(\mathcal{O}(\log n)\). Modifies the value at the given position, if any.

Precondition: See adjustPrecondition.

Combining

union :: (Ord k, Ord (Tag v)) => BiMap k v -> BiMap k v -> BiMap k v Source #

Left-biased union. For the time complexity, see union.

Precondition: See unionPrecondition.

Traversal

mapWithKey :: (Ord k, Ord (Tag v), HasTag v) => (k -> v -> v) -> BiMap k v -> BiMap k v Source #

\(\mathcal{O}(n \log n)\). Map a function over all values in the map.

Precondition: See mapWithKeyPrecondition.

mapWithKeyFixedTags :: (k -> v -> v) -> BiMap k v -> BiMap k v Source #

\(\mathcal{O}(n)\). Changes all the values using the given function, which is also given access to keys.

Precondition: See mapWithKeyFixedTagsPrecondition. Note that tags must not change.

Conversion

fromList :: (Ord k, Ord (Tag v), HasTag v) => [(k, v)] -> BiMap k v Source #

\(\mathcal{O}(n \log n)\). Conversion from lists of pairs. Later entries take precedence over earlier ones.

Precondition: See fromListPrecondition.

fromDistinctAscendingLists :: ([(k, v)], [(Tag v, k)]) -> BiMap k v Source #

\(\mathcal{O}(n)\). Conversion from two lists that contain distinct keys/tags, with the keys/tags in ascending order.

Precondition: See fromDistinctAscendingListsPrecondition.

toList :: BiMap k v -> [(k, v)] Source #

\(\mathcal{O}(n)\). Conversion to lists of pairs, with the keys in ascending order.

toDistinctAscendingLists :: BiMap k v -> ([(k, v)], [(Tag v, k)]) Source #

\(\mathcal{O}(n)\). Generates input suitable for fromDistinctAscendingLists.

keys :: BiMap k v -> [k] Source #

\(\mathcal{O}(n)\). The keys, in ascending order.

elems :: BiMap k v -> [v] Source #

\(\mathcal{O}(n)\). The values, ordered according to the corresponding keys.

Debugging

tagInjectiveFor :: (Eq v, Eq (Tag v), HasTag v) => [v] -> Bool Source #

Checks if the function tag is injective for the values in the given list for which the function is defined.

biMapInvariant :: (Eq k, Eq v, Ord (Tag v), HasTag v) => BiMap k v -> Bool Source #

The invariant for BiMap.

insertPrecondition :: (Eq k, Eq (Tag v), HasTag v) => k -> v -> BiMap k v -> Bool Source #

The precondition for insert k v m: If v has a tag (tag v ≠ Nothing), then m must not contain any mapping k' ↦ v' for which k ≠ k' and tag v = tag v'.

insertLookupWithKeyPrecondition :: (Ord k, Eq (Tag v), HasTag v) => (k -> v -> v -> v) -> k -> v -> BiMap k v -> Bool Source #

The precondition for insertLookupWithKey f k v m is that, if the value v' is inserted into m, and tag v' is defined, then no key other than k may map to a value v'' for which tag v'' = tag v'.

alterPrecondition :: (Ord k, Eq (Tag v), HasTag v) => (Maybe v -> Maybe v) -> k -> BiMap k v -> Bool Source #

The precondition for alter f k m is that, if the value v is inserted into m, and tag v is defined, then no key other than k may map to a value v' for which tag v' = tag v.

updatePrecondition :: (Ord k, Eq (Tag v), HasTag v) => (v -> Maybe v) -> k -> BiMap k v -> Bool Source #

The precondition for update f k m is that, if the value v is inserted into m, and tag v is defined, then no key other than k may map to a value v' for which tag v' = tag v.

adjustPrecondition :: (Ord k, Eq (Tag v), HasTag v) => (v -> v) -> k -> BiMap k v -> Bool Source #

The precondition for adjust f k m is that, if the value v is inserted into m, and tag v is defined, then no key other than k may map to a value v' for which tag v' = tag v.

unionPrecondition :: (Ord k, Eq v, Eq (Tag v), HasTag v) => BiMap k v -> BiMap k v -> Bool Source #

mapWithKeyPrecondition :: (Eq k, Eq v, Eq (Tag v), HasTag v) => (k -> v -> v) -> BiMap k v -> Bool Source #

The precondition for mapWithKey f m: For any two distinct mappings k₁ ↦ v₁, k₂ ↦ v₂ in m for which the tags of f k₁ v₁ and f k₂ v₂ are defined the values of f must be distinct (f k₁ v₁ ≠ f k₂ v₂). Furthermore tag must be injective for { f k v | (k, v) ∈ m }.

mapWithKeyFixedTagsPrecondition :: (Eq (Tag v), HasTag v) => (k -> v -> v) -> BiMap k v -> Bool Source #

The precondition for mapWithKeyFixedTags f m is that, if m maps k to v, then tag (f k v) == tag v.

fromListPrecondition :: (Eq k, Eq v, Eq (Tag v), HasTag v) => [(k, v)] -> Bool Source #

The precondition for fromList kvs: For all pairs (k₁, v₁), (k₂, v₂) in kvs for which the tags of v₁ and v₂ are defined, if v₁ = v₂ then k₁ = k₂. Furthermore tag must be injective for the values in the list.

fromDistinctAscendingListsPrecondition :: (Ord k, Eq v, Ord (Tag v), HasTag v) => ([(k, v)], [(Tag v, k)]) -> Bool Source #

The precondition for fromDistinctAscendingLists (kvs, kks): The lists must contain distinct keys/tags, and must be sorted according to the keys/tags. Furthermore, for every pair (k, v) in the first list for which tag v = Just k' there must be a pair (k', k) in the second list, and there must not be any other pairs in that list. Finally tag must be injective for {v | (_, v) ∈ kvs }.