Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data Relation a b
- empty :: Relation a b
- singleton :: a -> b -> Relation a b
- fromList :: (Ord a, Ord b) => [(a, b)] -> Relation a b
- fromManyDom :: (Foldable f, Ord a, Ord b) => f a -> b -> Relation a b
- fromManyRan :: (Foldable f, Ord a, Ord b) => a -> f b -> Relation a b
- fromMap :: (Ord a, Ord b) => Map a b -> Relation a b
- fromMultimap :: (Ord a, Ord b) => Map a (Set b) -> Relation a b
- fromSet :: (Ord a, Ord b) => Set (a, b) -> Relation a b
- unsafeFromMultimaps :: Map a (Set b) -> Map b (Set a) -> Relation a b
- null :: Relation a b -> Bool
- size :: Relation a b -> Int
- member :: (Ord a, Ord b) => a -> b -> Relation a b -> Bool
- notMember :: (Ord a, Ord b) => a -> b -> Relation a b -> Bool
- memberDom :: Ord a => a -> Relation a b -> Bool
- memberRan :: Ord b => b -> Relation a b -> Bool
- lookupDom :: Ord a => a -> Relation a b -> Set b
- lookupRan :: Ord b => b -> Relation a b -> Set a
- manyDom :: Ord a => a -> Relation a b -> Bool
- manyRan :: Ord b => b -> Relation a b -> Bool
- (<$|) :: (Ord a, Ord b) => Set a -> Set b -> Relation a b -> Set a
- (|$>) :: (Ord a, Ord b) => Set a -> Set b -> Relation a b -> Set b
- searchDom :: (Ord a, Ord b) => (a -> Ordering) -> Relation a b -> Set b
- searchDomG :: (Ord a, Monoid c) => (a -> Set b -> c) -> (a -> Ordering) -> Relation a b -> c
- searchRan :: (Ord a, Ord b) => (b -> Ordering) -> Relation a b -> Set a
- filter :: (Ord a, Ord b) => ((a, b) -> Bool) -> Relation a b -> Relation a b
- filterM :: (Applicative m, Ord a, Ord b) => ((a, b) -> m Bool) -> Relation a b -> m (Relation a b)
- filterDom :: (Ord a, Ord b) => (a -> Bool) -> Relation a b -> Relation a b
- filterDomM :: (Applicative m, Ord a, Ord b) => (a -> m Bool) -> Relation a b -> m (Relation a b)
- filterManyDom :: (Ord a, Ord b) => Relation a b -> Relation a b
- filterRan :: (Ord a, Ord b) => (b -> Bool) -> Relation a b -> Relation a b
- filterRanM :: (Applicative m, Ord a, Ord b) => (b -> m Bool) -> Relation a b -> m (Relation a b)
- subtractDom :: (Ord a, Ord b) => Set a -> Relation a b -> Relation a b
- (<||) :: (Ord a, Ord b) => Set a -> Relation a b -> Relation a b
- subtractRan :: (Ord a, Ord b) => Set b -> Relation a b -> Relation a b
- (||>) :: (Ord a, Ord b) => Relation a b -> Set b -> Relation a b
- restrictDom :: (Ord a, Ord b) => Set a -> Relation a b -> Relation a b
- (<|) :: (Ord a, Ord b) => Set a -> Relation a b -> Relation a b
- restrictRan :: (Ord a, Ord b) => Relation a b -> Set b -> Relation a b
- (|>) :: (Ord a, Ord b) => Relation a b -> Set b -> Relation a b
- collectRan :: Ord a => Ord c => (b -> Maybe c) -> Relation a b -> Relation a c
- foldlStrict :: (a -> b -> a) -> a -> [b] -> a
- map :: (Ord a, Ord b, Ord c, Ord d) => ((a, b) -> (c, d)) -> Relation a b -> Relation c d
- mapDom :: (Ord a, Ord a', Ord b) => (a -> a') -> Relation a b -> Relation a' b
- mapDomMonotonic :: (Ord a, Ord a', Ord b) => (a -> a') -> Relation a b -> Relation a' b
- mapRan :: (Ord a, Ord b, Ord b') => (b -> b') -> Relation a b -> Relation a b'
- mapRanMonotonic :: (Ord a, Ord b, Ord b') => (b -> b') -> Relation a b -> Relation a b'
- bimap :: (Ord a, Ord b, Ord c, Ord d) => (a -> c) -> (b -> d) -> Relation a b -> Relation c d
- bitraverse :: (Applicative f, Ord a, Ord b, Ord c, Ord d) => (a -> f c) -> (b -> f d) -> Relation a b -> f (Relation c d)
- swap :: Relation a b -> Relation b a
- insert :: (Ord a, Ord b) => a -> b -> Relation a b -> Relation a b
- insertManyDom :: (Foldable f, Ord a, Ord b) => f a -> b -> Relation a b -> Relation a b
- insertManyRan :: (Foldable f, Ord a, Ord b) => a -> f b -> Relation a b -> Relation a b
- delete :: (Ord a, Ord b) => a -> b -> Relation a b -> Relation a b
- deleteDom :: (Ord a, Ord b) => a -> Relation a b -> Relation a b
- deleteRan :: (Ord a, Ord b) => b -> Relation a b -> Relation a b
- deleteDomWhere :: (Ord a, Ord b) => (a -> Bool) -> b -> Relation a b -> Relation a b
- deleteRanWhere :: (Ord a, Ord b) => (b -> Bool) -> a -> Relation a b -> Relation a b
- replaceDom :: (Ord a, Ord b) => a -> a -> Relation a b -> Relation a b
- replaceRan :: (Ord a, Ord b) => b -> b -> Relation a b -> Relation a b
- updateDom :: (Ord a, Ord b) => (a -> a) -> b -> Relation a b -> Relation a b
- updateRan :: (Ord a, Ord b) => (b -> b) -> a -> Relation a b -> Relation a b
- difference :: (Ord a, Ord b) => Relation a b -> Relation a b -> Relation a b
- difference1 :: (Ord a, Ord b) => Relation a b -> Relation a b -> Maybe (Relation a b)
- intersection :: (Ord a, Ord b) => Relation a b -> Relation a b -> Relation a b
- joinDom :: (Ord a, Ord b, Ord c) => Relation a b -> Relation a c -> Relation a (b, c)
- joinRan :: (Ord a, Ord b, Ord c) => Relation a c -> Relation b c -> Relation (a, b) c
- innerJoinDomMultimaps :: (Ord a, Ord b, Ord c) => Relation a b -> Relation a c -> Map a (Set b, Set c)
- innerJoinRanMultimaps :: (Ord a, Ord b, Ord c) => Relation a c -> Relation b c -> Map c (Set a, Set b)
- outerJoinDomMultimaps :: (Ord a, Ord b, Ord c) => Relation a b -> Relation a c -> Map a (Set b, Set c)
- outerJoinRanMultimaps :: (Ord a, Ord b, Ord c) => Relation a c -> Relation b c -> Map c (Set a, Set b)
- union :: (Ord a, Ord b) => Relation a b -> Relation a b -> Relation a b
- unions :: (Ord a, Ord b) => [Relation a b] -> Relation a b
- unionDomainWith :: (Ord a, Ord b) => (a -> Set b -> Set b -> Set b) -> Relation a b -> Relation a b -> Relation a b
- unionRangeWith :: (Ord a, Ord b) => (b -> Set a -> Set a -> Set a) -> Relation a b -> Relation a b -> Relation a b
- toList :: Relation a b -> [(a, b)]
- domain :: Relation a b -> Map a (Set b)
- range :: Relation a b -> Map b (Set a)
- toMap :: Ord a => Relation a b -> Maybe (Map a b)
- toMultimap :: Relation a b -> Map a (Set b)
- toUnzippedMultimap :: Ord a => Ord b => Ord c => Relation a (b, c) -> Map a (Set b, Set c)
- dom :: Relation a b -> Set a
- ran :: Relation a b -> Set b
- toSet :: (Ord a, Ord b) => Relation a b -> Set (a, b)
Documentation
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.
- Always be careful with the associated set of the key.
- If you union two relations, apply union to the set of values.
- 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
(Ord a, Ord b) => Monoid (Relation a b) Source # | |
(Ord a, Ord b) => Semigroup (Relation a b) Source # | |
(Show a, Show b) => Show (Relation a b) Source # | |
(NFData a, NFData b) => NFData (Relation a b) Source # | |
Defined in Unison.Util.Relation | |
(Eq a, Eq b) => Eq (Relation a b) Source # | |
(Ord a, Ord b) => Ord (Relation a b) Source # | |
Defined in Unison.Util.Relation |
Initialization
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)].
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
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
manyDom :: Ord a => a -> Relation a b -> Bool Source #
True if a value appears more than one time in the relation.
Searches
searchDomG :: (Ord a, Monoid c) => (a -> Set b -> c) -> (a -> Ordering) -> Relation a b -> c Source #
Filters
filterM :: (Applicative m, Ord a, Ord b) => ((a, b) -> m Bool) -> Relation a b -> m (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
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 a
s.
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 b
s.
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.
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
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)
.
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
insert :: (Ord a, Ord b) => a -> b -> Relation a b -> Relation a b Source #
Insert a relation x
and y
in the relation r
delete :: (Ord a, Ord b) => a -> b -> Relation a b -> Relation a b Source #
Delete an association in the relation.
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
.
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.
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:
a
s that do not exist in bothxs
andys
are dropped.- The
a
s that remain are therefore associated with non-empty sets ofb
s andc
s.
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.
unionDomainWith :: (Ord a, Ord b) => (a -> Set b -> Set b -> Set b) -> Relation a b -> Relation a b -> Relation a b Source #
unionRangeWith :: (Ord a, Ord b) => (b -> Set a -> Set a -> Set a) -> Relation a b -> Relation a b -> Relation a b Source #
Converting to other data structures
Multimap
Set
dom :: Relation a b -> Set a Source #
Returns the domain in the relation, as a Set, in its entirety.
O(a).