Data.Ranged.Ranges
 Contents Construction Predicates Membership Set Operations QuickCheck properties
Description
A range has an upper and lower boundary.
Synopsis
data Range v = Range {
 rangeLower, rangeUpper :: (Boundary v)
}
emptyRange :: DiscreteOrdered v => Range v
fullRange :: DiscreteOrdered v => Range v
rangeIsEmpty :: DiscreteOrdered v => Range v -> Bool
rangeOverlap :: DiscreteOrdered v => Range v -> Range v -> Bool
rangeEncloses :: DiscreteOrdered v => Range v -> Range v -> Bool
rangeSingletonValue :: DiscreteOrdered v => Range v -> Maybe v
rangeHas :: Ord v => Range v -> v -> Bool
rangeListHas :: Ord v => [Range v] -> v -> Bool
singletonRange :: DiscreteOrdered v => v -> Range v
rangeIntersection :: DiscreteOrdered v => Range v -> Range v -> Range v
rangeUnion :: DiscreteOrdered v => Range v -> Range v -> [Range v]
rangeDifference :: DiscreteOrdered v => Range v -> Range v -> [Range v]
Construction
data Range v
A Range has upper and lower boundaries.
Constructors
Range
 rangeLower, rangeUpper :: (Boundary v)
Instances
 (Arbitrary v, DiscreteOrdered v, Show v) => Arbitrary (Range v) DiscreteOrdered a => Eq (Range a) DiscreteOrdered a => Ord (Range a) (Show a, DiscreteOrdered a) => Show (Range a)
emptyRange :: DiscreteOrdered v => Range v
The empty range
fullRange :: DiscreteOrdered v => Range v
The full range. All values are within it.
Predicates
rangeIsEmpty :: DiscreteOrdered v => Range v -> Bool
A range is empty unless its upper boundary is greater than its lower boundary.
rangeOverlap :: DiscreteOrdered v => Range v -> Range v -> Bool
Two ranges overlap if their intersection is non-empty.
rangeEncloses :: DiscreteOrdered v => Range v -> Range v -> Bool
The first range encloses the second if every value in the second range is also within the first range. If the second range is empty then this is always true.
rangeSingletonValue :: DiscreteOrdered v => Range v -> Maybe v
If the range is a singleton, returns Just the value. Otherwise returns Nothing.
Membership
rangeHas :: Ord v => Range v -> v -> Bool
True if the value is within the range.
rangeListHas :: Ord v => [Range v] -> v -> Bool
True if the value is within one of the ranges.
Set Operations
singletonRange :: DiscreteOrdered v => v -> Range v
A range containing a single value
rangeIntersection :: DiscreteOrdered v => Range v -> Range v -> Range v
Intersection of two ranges, if any.
rangeUnion :: DiscreteOrdered v => Range v -> Range v -> [Range v]

Union of two ranges. Returns one or two results.

If there are two results then they are guaranteed to have a non-empty gap in between, but may not be in ascending order.

rangeDifference :: DiscreteOrdered v => Range v -> Range v -> [Range v]
range1 minus range2. Returns zero, one or two results. Multiple results are guaranteed to have non-empty gaps in between, but may not be in ascending order.
QuickCheck properties

Range union

``` prop_union r1 r2 n =
(r1 `rangeHas` n || r2 `rangeHas` n)
== (r1 `rangeUnion` r2) `rangeListHas` n
```

Range intersection

``` prop_intersection r1 r2 n =
(r1 `rangeHas` n && r2 `rangeHas` n)
== (r1 `rangeIntersection` r2) `rangeHas` n
```

Range difference

``` prop_difference r1 r2 n =
(r1 `rangeHas` n && not (r2 `rangeHas` n))
== (r1 `rangeDifference` r2) `rangeListHas` n
```

Singleton range

``` prop_singletonHas v =
singletonRange v `rangeHas` v
```
``` prop_singletonConverse v =
rangeSingletonValue (singletonRange v) == Just v
```