ModuleAgda-2.8.0.2Haskell2010
Agda.Utils.Set1
Non-empty sets.
Provides type Set1 of non-empty sets.
Import: @
import Agda.Utils.Set1 (Set1) import qualified Agda.Utils.Set1 as Set1
@
- 2 types
- 74 values
- PackageAgda-2.8.0.2
- Exports80
- LanguageHaskell2010
- LicenceMIT
- SourceSet1.hs
Lossless toSet. Opposite of nonEmptySet.
A more general type would be Null m => Set a -> (Set1 a -> m) -> m
but this type is problematic as we do not have a general
instance Applicative m => Null (m ()).
O(1). The IsNonEmpty and IsEmpty patterns allow you to treat
a Set as if it were either a IsNonEmpty n (where n is
a NESet) or an IsEmpty.
Matching on IsEmpty means that the original Set was empty.
A case statement handling both IsNonEmpty and IsEmpty provides complete coverage.
This is a bidirectional pattern, so you can use IsEmpty as an expression, and it will be interpreted as empty.
See IsNonEmpty for more information.
O(1) match, O(log n) usage of contents. The IsNonEmpty and
IsEmpty patterns allow you to treat a Set as if it were either
a IsNonEmpty n (where n is a NESet) or an IsEmpty.
For example, you can pattern match on a Set:
myFunc :: Set X -> Y
myFunc (IsNonEmpty n) = -- here, the user provided a non-empty set, and n is the NESet
myFunc IsEmpty = -- here, the user provided an empty set
Matching on IsNonEmpty n means that the original Set was not
empty, and you have a verified-non-empty NESet n to use.
Note that patching on this pattern is O(1). However, using the contents requires a O(log n) cost that is deferred until after the pattern is matched on (and is not incurred at all if the contents are never used).
A case statement handling both IsNonEmpty and IsEmpty provides complete coverage.
This is a bidirectional pattern, so you can use IsNonEmpty to convert a NESet back into a Set, obscuring its non-emptiness (see toSet).
Same as difference.
Calculate the Cartesian product of two sets.
cartesianProduct xs ys = fromList $ liftA2 (,) (toList xs) (toList ys)
Example:
cartesianProduct (fromList (1:|[2])) (fromList ('a':|['b'])) =
fromList ((1,'a') :| [(1,'b'), (2,'a'), (2,'b')])
O(log n). Delete an element from a set.
O(log n). Delete the element at index, i.e. by its zero-based index in the sorted sequence of elements. If the index is out of range (less than zero, greater or equal to size of the set), error is called.
Returns a potentially empty set (Set), because this could potentailly delete the final element in a singleton set.
deleteAt 0 (fromList (5:|[3])) == singleton 5
deleteAt 1 (fromList (5:|[3])) == singleton 3
deleteAt 2 (fromList (5:|[3])) Error: index out of range
deleteAt (-1) (fromList (5:|[3])) Error: index out of rangeO(log n). Delete and find the minimal element.
Note that unlike Data.Set.deleteFindMax for Set, this cannot ever
fail, and so is a total function. However, the result Set is
potentially empty, since the original set might have contained just
a single item.
deleteFindMax (fromList (5 :| [3, 10])) == (10, Data.Set.fromList [3, 5])O(1). Delete and find the minimal element. It is constant-time, so
has better asymptotics that Data.Set.minView for Set.
Note that unlike Data.Set.deleteFindMin for Set, this cannot ever
fail, and so is a total function. However, the result Set is
potentially empty, since the original set might have contained just
a single item.
deleteFindMin (fromList (5 :| [3, 10])) == (3, Data.Set.fromList [5, 10])O(log n). Delete the maximal element. Returns a potentially empty set (Set), because we might delete the final item in a singleton set.
deleteMax (fromList (5 :| [3, 7])) == Data.Set.fromList [3, 5]
deleteMax (singleton 5) == Data.Set.emptyO(log n). Delete an element from a set, returning Nothing if the result would be empty.
This is more efficient than nonEmptySet . delete x because it avoids
converting the known-minimum representation back through Set when the
deleted element is not the minimum.
O(1). Delete the minimal element. Returns a potentially empty set
(Set), because we might delete the final item in a singleton set. It
is constant-time, so has better asymptotics than Data.Set.deleteMin.
deleteMin (fromList (5 :| [3, 7])) == Data.Set.fromList [5, 7]
deleteMin (singleton 5) == Data.Set.emptyO(m*log(n/m + 1)), m <= n. Difference of two sets.
Returns a potentially empty set (Set) because the first set might be a subset of the second set, and therefore have all of its elements removed.
O(n+m). Check whether two sets are disjoint (i.e. their intersection is empty).
disjoint (fromList (2:|[4,6])) (fromList (1:|[3])) == True
disjoint (fromList (2:|[4,6,8])) (fromList (2:|[3,5,7])) == False
disjoint (fromList (1:|[2])) (fromList (1:|[2,3,4])) == FalseCalculate the disjoint union of two sets.
disjointUnion xs ys = map Left xs `union` map Right ysExample:
disjointUnion (fromList (1:|[2])) (fromList ("hi":|["bye"])) =
fromList (Left 1 :| [Left 2, Right "hi", Right "bye"])
Drop a given number of elements in order, beginning with the smallest ones.
Returns a potentailly empty set (Set), in the case that drop is called with a number equal to or greater the number of items in the set, and we drop every item.
drop n = Data.Set.fromDistinctAscList . Data.List.NonEmpty.drop n . toAscList
O(log n). Drop while a predicate on the elements holds. The user is
responsible for ensuring that for all elements j and k in the set,
j < k ==> p j >= p k. See note at spanAntitone.
Returns a potentially empty set (Set) because the predicate might be true for all items.
dropWhileAntitone p = Data.Set.fromDistinctAscList . Data.List.NonEmpty.dropWhile p . toList
dropWhileAntitone p = filter (not . p)
O(log n). Retrieve an element by its index, i.e. by its zero-based index in the sorted sequence of elements. If the index is out of range (less than zero, greater or equal to size of the set), error is called.
elemAt 0 (fromList (5:|[3])) == 3
elemAt 1 (fromList (5:|[3])) == 5
elemAt 2 (fromList (5:|[3])) Error: index out of rangeO(n). An alias of toAscList. The elements of a set in ascending order.
O(n). Filter all elements that satisfy the predicate.
Returns a potentially empty set (Set) because the predicate might filter out all items in the original non-empty set.
O(log n). Return the index of an element, which is its zero-based index in the sorted sequence of elements. The index is a number from 0 up to, but not including, the size of the set. Calls error when the element is not a member of the set.
findIndex 2 (fromList (5:|[3])) Error: element is not in the set
findIndex 3 (fromList (5:|[3])) == 0
findIndex 5 (fromList (5:|[3])) == 1
findIndex 6 (fromList (5:|[3])) Error: element is not in the setO(log n). The maximal key of a set Note that this is total, making lookupMin obsolete.
findMax (fromList (5 :| [3])) == 5O(1). The minimal element of a set. Note that this is total, making
lookupMin obsolete. It is constant-time, so has better
asymptotics than Data.Set.lookupMin and Data.Map.findMin as well.
findMin (fromList (5 :| [3])) == 3O(n). A strict version of foldl1. Each application of the operator
is evaluated before using the result in the next application. This
function is strict in the starting value.
O(n). A strict version of foldr1. Each application of the operator
is evaluated before using the result in the next application. This
function is strict in the starting value.
O(n). Build a set from an ascending list in linear time. /The precondition (input list is ascending) is not checked./
O(n). Build a set from a descending list in linear time. The precondition (input list is descending) is not checked.
O(n). Build a set from an ascending list of distinct elements in linear time. The precondition (input list is strictly ascending) is not checked.
O(n). Build a set from a descending list of distinct elements in linear time. The precondition (input list is strictly descending) is not checked.
O(log n). Insert an element in a set. If the set already contains an element equal to the given value, it is replaced with the new value.
O(log n). Convert a Set into an NESet by adding a value. Because of this, we know that the set must have at least one element, and so therefore cannot be empty.
See insertSetMin for a version that is constant-time if the new value is strictly smaller than all values in the original set
insertSet 4 (Data.Set.fromList [5, 3]) == fromList (3 :| [4, 5])
insertSet 4 Data.Set.empty == singleton 4 "c"O(log n) Convert a Set into an NESet by adding a value where the value is strictly less than all values in the input set The values in the original map must all be strictly greater than the new value. The precondition is not checked.
While this has the same asymptotics as insertSet, it saves a constant factor for key comparison (so may be helpful if comparison is expensive) and also does not require an Ord instance for the key type.
insertSetMin 7 (Data.Set.fromList [5, 3]) == fromList (3 :| [5, 7])
valid (insertSetMin 7 (Data.Set.fromList [5, 3])) == True
valid (insertSetMin 2 (Data.Set.fromList [5, 3])) == False
valid (insertSetMin 5 (Data.Set.fromList [5, 3])) == FalseO(1) Convert a Set into an NESet by adding a value where the value is strictly less than all values in the input set The values in the original map must all be strictly greater than the new value. The precondition is not checked.
insertSetMin 2 (Data.Set.fromList [5, 3]) == fromList (2 :| [3, 5])
valid (insertSetMin 2 (Data.Set.fromList [5, 3])) == True
valid (insertSetMin 7 (Data.Set.fromList [5, 3])) == False
valid (insertSetMin 3 (Data.Set.fromList [5, 3])) == FalseO(m*log(n/m + 1)), m <= n. The intersection of two sets.
Returns a potentially empty set (Set), because the two sets might have an empty intersection.
Elements of the result come from the first set, so for example
import qualified Data.Set.NonEmpty as NES
data AB = A | B deriving Show
instance Ord AB where compare _ _ = EQ
instance Eq AB where _ == _ = True
main = print (NES.singleton A `NES.intersection` NES.singleton B,
NES.singleton B `NES.intersection` NES.singleton A)prints (fromList (A:|[]),fromList (B:|[])).
O(n+m). Is this a proper subset? (ie. a subset but not equal).
O(n+m). Is this a subset?
(s1 `isSubsetOf` s2) tells whether s1 is a subset of s2.
O(log n). Find smallest element greater or equal to the given one.
lookupLT 3 (fromList (3 :| [5])) == Just 3
lookupLT 4 (fromList (3 :| [5])) == Just 5
lookupLT 6 (fromList (3 :| [5])) == NothingO(log n). Find smallest element greater than the given one.
lookupLT 4 (fromList (3 :| [5])) == Just 5
lookupLT 5 (fromList (3 :| [5])) == NothingO(log n). Lookup the index of an element, which is its zero-based index in the sorted sequence of elements. The index is a number from 0 up to, but not including, the size of the set.
isJust (lookupIndex 2 (fromList (5:|[3]))) == False
fromJust (lookupIndex 3 (fromList (5:|[3]))) == 0
fromJust (lookupIndex 5 (fromList (5:|[3]))) == 1
isJust (lookupIndex 6 (fromList (5:|[3]))) == FalseO(log n). Find largest element smaller or equal to the given one.
lookupLT 2 (fromList (3 :| [5])) == Nothing
lookupLT 4 (fromList (3 :| [5])) == Just 3
lookupLT 5 (fromList (3 :| [5])) == Just 5O(log n). Find largest element smaller than the given one.
lookupLT 3 (fromList (3 :| [5])) == Nothing
lookupLT 5 (fromList (3 :| [5])) == Just 3O(n*log n).
map f s is the set obtained by applying f to each element of s.
It's worth noting that the size of the result may be smaller if,
for some (x,y), x /= y && f x == f y
O(n).
mapMonotonic f s == map f s, but works only when f is strictly
increasing. The precondition is not checked. Semi-formally, we have:
and [x < y ==> f x < f y | x <- ls, y <- ls]
==> mapMonotonic f s == map f s
where ls = Data.Foldable.toList sO(log n). Is the element in the set?
O(log n). Is the element not in the set?
O(n). Partition the map according to a predicate.
Returns a These with potentially two non-empty sets:
This n1means that the predicate was true for all items.That n2means that the predicate was false for all items.These n1 n2givesn1(all of the items that were true for the predicate) andn2(all of the items that were false for the predicate).
See also split.
partition (> 3) (fromList (5 :| [3])) == These (singleton 5) (singleton 3)
partition (< 7) (fromList (5 :| [3])) == This (fromList (3 :| [5]))
partition (> 7) (fromList (5 :| [3])) == That (fromList (3 :| [5]))Calculate the power set of a non-empty: the set of all its (non-empty) subsets.
t `member` powerSet s == t `isSubsetOf` s
Example:
powerSet (fromList (1 :| [2,3])) =
fromList (singleton 1 :| [ singleton 2
, singleton 3
, fromList (1 :| [2])
, fromList (1 :| [3])
, fromList (2 :| [3])
, fromList (1 :| [2,3])
]
)
We know that the result is non-empty because the result will always at least contain the original set.
O(log n). Divide a set at the point where a predicate on the
elements stops holding. The user is responsible for ensuring that for
all elements j and k in the set, j < k ==> p j >= p k.
Returns a These with potentially two non-empty sets:
This n1means that the predicate never failed for any item, returning the original setThat n2means that the predicate failed for the first item, returning the original setThese n1 n2givesn1(the set up to the point where the predicate stops holding) andn2(the set starting from the point where the predicate stops holding)
spanAntitone p xs = partition p xs
Note: if p is not actually antitone, then spanAntitone will split the set
at some unspecified point where the predicate switches from holding to not
holding (where the predicate is seen to hold before the first element and to fail
after the last element).
O(log n). The expression (split x set) is potentially a These
containing up to two NESets based on splitting the set into sets
containing items before and after the value x. It will never return
a set that contains x itself.
Nothing means that
xwas the only value in the the original set, and so there are no items before or after it.Just (This n1)meansxwas larger than or equal to all items in the set, andn1is the entire original set (minusx, if it was present)Just (That n2)meansxwas smaller than or equal to all items in the set, andn2is the entire original set (minusx, if it was present)Just (These n1 n2)givesn1(the set of all values from the original set less thanx) andn2(the set of all values from the original set greater thanx).
split 2 (fromList (5 :| [3])) == Just (That (fromList (3 :| [5])) )
split 3 (fromList (5 :| [3])) == Just (That (singleton 5) )
split 4 (fromList (5 :| [3])) == Just (These (singleton 3) (singleton 5))
split 5 (fromList (5 :| [3])) == Just (This (singleton 3) )
split 6 (fromList (5 :| [3])) == Just (This (fromList (3 :| [5])) )
split 5 (singleton 5) == NothingO(log n). Split a set at a particular index i.
O(log n). The expression (splitMember x set) splits a set just
like split but also returns member x set (whether or not x was
in set)
splitMember 2 (fromList (5 :| [3])) == (False, Just (That (fromList (3 :| [5)]))))
splitMember 3 (fromList (5 :| [3])) == (True , Just (That (singleton 5)))
splitMember 4 (fromList (5 :| [3])) == (False, Just (These (singleton 3) (singleton 5)))
splitMember 5 (fromList (5 :| [3])) == (True , Just (This (singleton 3))
splitMember 6 (fromList (5 :| [3])) == (False, Just (This (fromList (3 :| [5])))
splitMember 5 (singleton 5) == (True , Nothing)O(1). Decompose a set into pieces based on the structure of the underlying tree. This function is useful for consuming a set in parallel.
No guarantee is made as to the sizes of the pieces; an internal, but deterministic process determines this. However, it is guaranteed that the pieces returned will be in ascending order (all elements in the first subset less than all elements in the second, and so on).
Note that the current implementation does not return more than four subsets, but you should not depend on this behaviour because it can change in the future without notice.
O(log n). Take while a predicate on the elements holds. The user is
responsible for ensuring that for all elements j and k in the set,
j < k ==> p j >= p k. See note at spanAntitone.
Returns a potentially empty set (Set) because the predicate might fail on the first input.
takeWhileAntitone p = Data.Set.fromDistinctAscList . Data.List.NonEmpty.takeWhile p . toList
takeWhileAntitone p = filter p
O(n). Convert the set to an ascending non-empty list of elements.
O(n). Convert the set to a descending non-empty list of elements.
O(m*log(n/m + 1)), m <= n. Union of a possibly-empty Set and a non-empty set.
O(m*log(n/m + 1)), m <= n. Union of a non-empty set and a possibly-empty Set.
O(log n). Unsafe version of nonEmptySet. Coerces a Set into an NESet, but is undefined (throws a runtime exception when evaluation is attempted) for an empty Set.
O(n). A strict version of foldl. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.
O(n). A strict version of foldr. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.
O(n*log n). Create a set from a list of elements.
O(log n). Smart constructor for an NESet from a Set. Returns
Nothing if the Set was originally actually empty, and Just n
with an NESet, if the Set was not empty.
nonEmptySet and maybe empty toSet form an
isomorphism: they are perfect structure-preserving inverses of
eachother.
See IsNonEmpty for a pattern synonym that lets you "match on" the possiblity of a Set being an NESet.
nonEmptySet (Data.Set.fromList [3,5]) == Just (fromList (3:|[5]))O(1). Create a singleton set.
O(1). The number of elements in the set. Guaranteed to be greater than zero.
O(n). Convert the set to a non-empty list of elements.
O(log n). Convert a non-empty set back into a normal possibly-empty map, for usage with functions that expect Set.
Can be thought of as "obscuring" the non-emptiness of the set in its
type. See the Data.Set.NonEmpty.IsNotEmpty pattern.
nonEmptySet and maybe empty toSet form an
isomorphism: they are perfect structure-preserving inverses of
eachother.
toSet (fromList ((3,"a") :| [(5,"b")])) == Data.Set.fromList [(3,"a"), (5,"b")]O(m*log(n/m + 1)), m <= n. The union of two sets, preferring the first set when equal elements are encountered.
The union of a non-empty list of sets
O(n). Test if the internal set structure is valid.
withNonEmpty O(log n). A general continuation-based way to consume a Set as if
it were an NESet. withNonEmpty def f will take a Set. If set is
empty, it will evaluate to def. Otherwise, a non-empty set NESet
will be fed to the function f instead.
nonEmptySet == withNonEmpty Nothing JustA variant of foldl that has no base case, and thus may only be applied to non-empty structures.
This function is non-total and will raise a runtime exception if the structure happens to be empty.
foldl1 f = foldl1 f . toListExamples
Basic usage:
foldl1 (+) [1..4]10
foldl1 (+) []*** Exception: Prelude.foldl1: empty list
foldl1 (+) Nothing*** Exception: foldl1: empty structure
foldl1 (-) [1..4]-8
foldl1 (&&) [True, False, True, True]False
foldl1 (||) [False, False, True, True]True
foldl1 (+) [1..]* Hangs forever *
A variant of foldr that has no base case, and thus may only be applied to non-empty structures.
This function is non-total and will raise a runtime exception if the structure happens to be empty.
Examples
Basic usage:
foldr1 (+) [1..4]10
foldr1 (+) []Exception: Prelude.foldr1: empty list
foldr1 (+) Nothing*** Exception: foldr1: empty structure
foldr1 (-) [1..4]-2
foldr1 (&&) [True, False, True, True]False
foldr1 (||) [False, False, True, True]True
foldr1 (+) [1..]* Hangs forever *
A non-empty (by construction) set of values a. At least one value
exists in an NESet a at all times.
Functions that take an NESet can safely operate on it with the assumption that it has at least one item.
Functions that return an NESet provide an assurance that the result has at least one item.
Data.Set.NonEmpty re-exports the API of Data.Set, faithfully reproducing asymptotics, typeclass constraints, and semantics. Functions that ensure that input and output sets are both non-empty (like insert) return NESet, but functions that might potentially return an empty map (like delete) return a Set instead.
You can directly construct an NESet with the API from Data.Set.NonEmpty; it's more or less the same as constructing a normal Set, except you don't have access to empty. There are also a few ways to construct an NESet from a Set:
The nonEmptySet smart constructor will convert a
Set ainto aMaybe (NESet a), returning Nothing if the original Set was empty.You can use the insertSet family of functions to insert a value into a Set to create a guaranteed NESet.
You can use the IsNonEmpty and IsEmpty patterns to "pattern match" on a Set to reveal it as either containing a NESet or an empty map.
withNonEmpty offers a continuation-based interface for deconstructing a Set and treating it as if it were an NESet.
You can convert an NESet into a Set with toSet or IsNonEmpty, essentially "obscuring" the non-empty property from the type.
Instances22Foldable, Foldable1, Eq1, Ord1, Show1, Singleton, …
Foldable NESetDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.InternalFoldable1 NESetDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.InternalTraverses elements in ascending order
Eq1 NESetDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.InternalOrd1 NESetDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.InternalShow1 NESetDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.InternalSingleton a (NESet a)Defined in Agda-2.8.0.2 · Agda.Utils.SingletonOrd a => IsList (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.InternalEq a => Eq (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal(Data a, Ord a) => Data (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.InternalOrd a => Ord (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal(Read a, Ord a) => Read (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.InternalShow a => Show (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.InternalOrd a => Semigroup (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.InternalLeft-biased union
NFData a => NFData (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal(FromJSON a, Ord a) => FromJSON (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.InternalToJSON a => ToJSON (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.InternalOrd a => IsNonEmptyList (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Containers.NonEmpty.ListHasRange a => HasRange (Set1 a)Defined in Agda-2.8.0.2 · Agda.Syntax.Position(Ord a, KillRange a) => KillRange (Set1 a)Defined in Agda-2.8.0.2 · Agda.Syntax.PositionHasRangeWithoutFile a => HasRangeWithoutFile (Set1 a)Defined in Agda-2.8.0.2 · Agda.Syntax.Position(Ord a, EmbPrj a) => EmbPrj (Set1 a)Defined in Agda-2.8.0.2 · Agda.TypeChecking.Serialise.Instances.Common · orphantype Item (NESet a) = aDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal