unison-util-relation-0.0.0
Safe HaskellSafe-Inferred
LanguageHaskell2010

Unison.Util.Relation

Synopsis

Documentation

data Relation a b Source #

This implementation avoids using "Set (a,b)" because it it is necessary to search for an item without knowing both D and R.

In Set, you must know both values to search.

Thus, we have are two maps to updated together.

  1. Always be careful with the associated set of the key.
  2. If you union two relations, apply union to the set of values.
  3. If you subtract, take care when handling the set of values.

As a multi-map, each key is associated with a Set of values v.

We do not allow the associations with the empty Set.

Instances

Instances details
(Ord a, Ord b) => Monoid (Relation a b) Source # 
Instance details

Defined in Unison.Util.Relation

Methods

mempty :: Relation a b #

mappend :: Relation a b -> Relation a b -> Relation a b #

mconcat :: [Relation a b] -> Relation a b #

(Ord a, Ord b) => Semigroup (Relation a b) Source # 
Instance details

Defined in Unison.Util.Relation

Methods

(<>) :: Relation a b -> Relation a b -> Relation a b #

sconcat :: NonEmpty (Relation a b) -> Relation a b #

stimes :: Integral b0 => b0 -> Relation a b -> Relation a b #

(Show a, Show b) => Show (Relation a b) Source # 
Instance details

Defined in Unison.Util.Relation

Methods

showsPrec :: Int -> Relation a b -> ShowS #

show :: Relation a b -> String #

showList :: [Relation a b] -> ShowS #

(NFData a, NFData b) => NFData (Relation a b) Source # 
Instance details

Defined in Unison.Util.Relation

Methods

rnf :: Relation a b -> () #

(Eq a, Eq b) => Eq (Relation a b) Source # 
Instance details

Defined in Unison.Util.Relation

Methods

(==) :: Relation a b -> Relation a b -> Bool #

(/=) :: Relation a b -> Relation a b -> Bool #

(Ord a, Ord b) => Ord (Relation a b) Source # 
Instance details

Defined in Unison.Util.Relation

Methods

compare :: Relation a b -> Relation a b -> Ordering #

(<) :: Relation a b -> Relation a b -> Bool #

(<=) :: Relation a b -> Relation a b -> Bool #

(>) :: Relation a b -> Relation a b -> Bool #

(>=) :: Relation a b -> Relation a b -> Bool #

max :: Relation a b -> Relation a b -> Relation a b #

min :: Relation a b -> Relation a b -> Relation a b #

Initialization

empty :: Relation a b Source #

Construct a relation with no elements.

singleton :: a -> b -> Relation a b Source #

Builds a Relation consiting of an association between: x and y.

fromList :: (Ord a, Ord b) => [(a, b)] -> Relation a b Source #

The list must be formatted like: [(k1, v1), (k2, v2),..,(kn, vn)].

fromManyDom :: (Foldable f, Ord a, Ord b) => f a -> b -> Relation a b Source #

fromManyRan :: (Foldable f, Ord a, Ord b) => a -> f b -> Relation a b Source #

fromMap :: (Ord a, Ord b) => Map a b -> Relation a b Source #

fromMultimap :: (Ord a, Ord b) => Map a (Set b) -> Relation a b Source #

fromSet :: (Ord a, Ord b) => Set (a, b) -> Relation a b Source #

unsafeFromMultimaps :: Map a (Set b) -> Map b (Set a) -> Relation a b Source #

Construct a relation from a mapping from the domain and range mappings.

Precondition: the multimaps together form a valid relation; i.e. if x is related to y in one map then y is related to x in the other.

O(1).

Queries

null :: Relation a b -> Bool Source #

True if the relation r is the empty relation.

O(1).

size :: Relation a b -> Int Source #

size r returns the number of tuples in the relation.

member :: (Ord a, Ord b) => a -> b -> Relation a b -> Bool Source #

True if the relation contains the association x and y

notMember :: (Ord a, Ord b) => a -> b -> Relation a b -> Bool Source #

True if the relation does not contain the association x and y

memberDom :: Ord a => a -> Relation a b -> Bool Source #

True if the element exists in the domain.

memberRan :: Ord b => b -> Relation a b -> Bool Source #

True if the element exists in the range.

lookupDom :: Ord a => a -> Relation a b -> Set b Source #

lookupRan :: Ord b => b -> Relation a b -> Set a Source #

manyDom :: Ord a => a -> Relation a b -> Bool Source #

True if a value appears more than one time in the relation.

manyRan :: Ord b => b -> Relation a b -> Bool Source #

(<$|) :: (Ord a, Ord b) => Set a -> Set b -> Relation a b -> Set a Source #

(Case b <| r a)

(|$>) :: (Ord a, Ord b) => Set a -> Set b -> Relation a b -> Set b Source #

( Case a |> r b )

Searches

searchDom :: (Ord a, Ord b) => (a -> Ordering) -> Relation a b -> Set b Source #

searchDomG :: (Ord a, Monoid c) => (a -> Set b -> c) -> (a -> Ordering) -> Relation a b -> c Source #

searchRan :: (Ord a, Ord b) => (b -> Ordering) -> Relation a b -> Set a Source #

Filters

filter :: (Ord a, Ord b) => ((a, b) -> Bool) -> Relation a b -> Relation a b Source #

filterM :: (Applicative m, Ord a, Ord b) => ((a, b) -> m Bool) -> Relation a b -> m (Relation a b) Source #

filterDom :: (Ord a, Ord b) => (a -> Bool) -> Relation a b -> Relation a b Source #

filterDomM :: (Applicative m, Ord a, Ord b) => (a -> m Bool) -> Relation a b -> m (Relation a b) Source #

filterManyDom :: (Ord a, Ord b) => Relation a b -> Relation a b Source #

Restricts the relation to domain elements having multiple range elements

filterRan :: (Ord a, Ord b) => (b -> Bool) -> Relation a b -> Relation a b Source #

filterRanM :: (Applicative m, Ord a, Ord b) => (b -> m Bool) -> Relation a b -> m (Relation a b) Source #

subtractDom :: (Ord a, Ord b) => Set a -> Relation a b -> Relation a b Source #

Named version of (<||).

(<||) :: (Ord a, Ord b) => Set a -> Relation a b -> Relation a b Source #

Restrict the domain to not include these as.

subtractRan :: (Ord a, Ord b) => Set b -> Relation a b -> Relation a b Source #

Named version of (||>).

(||>) :: (Ord a, Ord b) => Relation a b -> Set b -> Relation a b Source #

Restrict the range to not include these bs.

restrictDom :: (Ord a, Ord b) => Set a -> Relation a b -> Relation a b Source #

Domain restriction for a relation. Modeled on z.

(<|) :: (Ord a, Ord b) => Set a -> Relation a b -> Relation a b Source #

Domain restriction for a relation. Modeled on z.

restrictRan :: (Ord a, Ord b) => Relation a b -> Set b -> Relation a b Source #

Range restriction for a relation. Modeled on z.

(|>) :: (Ord a, Ord b) => Relation a b -> Set b -> Relation a b Source #

Range restriction for a relation. Modeled on z.

collectRan :: Ord a => Ord c => (b -> Maybe c) -> Relation a b -> Relation a c Source #

Folds

foldlStrict :: (a -> b -> a) -> a -> [b] -> a Source #

This fragment provided by:

  Module      :  Data.Map
  Copyright   :  (c) Daan Leijen 2002
                 (c) Andriy Palamarchuk 2008
  License     :  BSD-style
  Maintainer  :  libraries@haskell.org
  Stability   :  provisional
  Portability :  portable

General traversals

map :: (Ord a, Ord b, Ord c, Ord d) => ((a, b) -> (c, d)) -> Relation a b -> Relation c d Source #

mapDom :: (Ord a, Ord a', Ord b) => (a -> a') -> Relation a b -> Relation a' b Source #

mapDomMonotonic :: (Ord a, Ord a', Ord b) => (a -> a') -> Relation a b -> Relation a' b Source #

Like mapDom, but takes a function that must be monotonic; i.e. compare x y == compare (f x) (f y).

mapRan :: (Ord a, Ord b, Ord b') => (b -> b') -> Relation a b -> Relation a b' Source #

mapRanMonotonic :: (Ord a, Ord b, Ord b') => (b -> b') -> Relation a b -> Relation a b' Source #

Like mapRan, but takes a function that must be monotonic; i.e. compare x y == compare (f x) (f y).

bimap :: (Ord a, Ord b, Ord c, Ord d) => (a -> c) -> (b -> d) -> Relation a b -> Relation c d Source #

bitraverse :: (Applicative f, Ord a, Ord b, Ord c, Ord d) => (a -> f c) -> (b -> f d) -> Relation a b -> f (Relation c d) Source #

Manipulations

swap :: Relation a b -> Relation b a Source #

insert :: (Ord a, Ord b) => a -> b -> Relation a b -> Relation a b Source #

Insert a relation x and y in the relation r

insertManyDom :: (Foldable f, Ord a, Ord b) => f a -> b -> Relation a b -> Relation a b Source #

insertManyRan :: (Foldable f, Ord a, Ord b) => a -> f b -> Relation a b -> Relation a b Source #

delete :: (Ord a, Ord b) => a -> b -> Relation a b -> Relation a b Source #

Delete an association in the relation.

deleteDom :: (Ord a, Ord b) => a -> Relation a b -> Relation a b Source #

deleteRan :: (Ord a, Ord b) => b -> Relation a b -> Relation a b Source #

deleteDomWhere :: (Ord a, Ord b) => (a -> Bool) -> b -> Relation a b -> Relation a b Source #

deleteRanWhere :: (Ord a, Ord b) => (b -> Bool) -> a -> Relation a b -> Relation a b Source #

replaceDom :: (Ord a, Ord b) => a -> a -> Relation a b -> Relation a b Source #

replaceDom x y r replaces all (x, _) with (y, _) in r.

replaceRan :: (Ord a, Ord b) => b -> b -> Relation a b -> Relation a b Source #

replaceRan x y r replaces all (_, x) with (_, y) in r.

updateDom :: (Ord a, Ord b) => (a -> a) -> b -> Relation a b -> Relation a b Source #

updateRan :: (Ord a, Ord b) => (b -> b) -> a -> Relation a b -> Relation a b Source #

Combinations

difference :: (Ord a, Ord b) => Relation a b -> Relation a b -> Relation a b Source #

Compute the difference of two relations.

difference1 :: (Ord a, Ord b) => Relation a b -> Relation a b -> Maybe (Relation a b) Source #

Like difference, but returns Nothing if the difference is empty.

intersection :: (Ord a, Ord b) => Relation a b -> Relation a b -> Relation a b Source #

joinDom :: (Ord a, Ord b, Ord c) => Relation a b -> Relation a c -> Relation a (b, c) Source #

joinRan :: (Ord a, Ord b, Ord c) => Relation a c -> Relation b c -> Relation (a, b) c Source #

innerJoinDomMultimaps :: (Ord a, Ord b, Ord c) => Relation a b -> Relation a c -> Map a (Set b, Set c) Source #

innerJoinDomMultimaps xs ys returns the "inner join" of the domains of xs and ys, which has intersection-like semantics:

  • as that do not exist in both xs and ys are dropped.
  • The as that remain are therefore associated with non-empty sets of bs and cs.

O(a2 * log(a1a2 + 1)), a1 <= a2, where a1 and a2/ are the numbers of elements in each relation's domain.

innerJoinRanMultimaps :: (Ord a, Ord b, Ord c) => Relation a c -> Relation b c -> Map c (Set a, Set b) Source #

innerJoinRanMultimaps xs ys returns the "inner join" of the ranges of xs and ys. See innerJoinDomMultimaps for more info.

O(c2 * log(c1c2 + 1)), c1 <= c2, where c1 and c2/ are the numbers of elements in each relation's range.

outerJoinDomMultimaps :: (Ord a, Ord b, Ord c) => Relation a b -> Relation a c -> Map a (Set b, Set c) Source #

outerJoinRanMultimaps :: (Ord a, Ord b, Ord c) => Relation a c -> Relation b c -> Map c (Set a, Set b) Source #

union :: (Ord a, Ord b) => Relation a b -> Relation a b -> Relation a b Source #

The Relation that results from the union of two relations: r and s.

unions :: (Ord a, Ord b) => [Relation a b] -> Relation a b Source #

Union a list of relations using the empty relation.

Converting to other data structures

toList :: Relation a b -> [(a, b)] Source #

Builds a List from a Relation.

domain :: Relation a b -> Map a (Set b) Source #

range :: Relation a b -> Map b (Set a) Source #

toMap :: Ord a => Relation a b -> Maybe (Map a b) Source #

Multimap

toMultimap :: Relation a b -> Map a (Set b) Source #

toUnzippedMultimap :: Ord a => Ord b => Ord c => Relation a (b, c) -> Map a (Set b, Set c) Source #

Set

dom :: Relation a b -> Set a Source #

Returns the domain in the relation, as a Set, in its entirety.

O(a).

ran :: Relation a b -> Set b Source #

Returns the range of the relation, as a Set, in its entirety.

O(b).

toSet :: (Ord a, Ord b) => Relation a b -> Set (a, b) Source #

Builds a Set from a Relation