data-interval-1.2.0: Interval arithmetic for both open and closed intervals

Copyright (c) Masahiro Sakai 2011-2013 BSD-style masahiro.sakai@gmail.com provisional non-portable (ScopedTypeVariables, DeriveDataTypeable) Safe-Inferred Haskell2010

Data.Interval

Description

Interval datatype and interval arithmetic.

Unlike the intervals package (http://hackage.haskell.org/package/intervals), this module provides both open and closed intervals and is intended to be used with `Rational`.

For the purpose of abstract interpretation, it might be convenient to use `Lattice` instance. See also lattices package (http://hackage.haskell.org/package/lattices).

Synopsis

# Interval type

data Interval r Source

The intervals (i.e. connected and convex subsets) over real numbers R.

Instances

 Eq r => Eq (Interval r) (Real r, Fractional r) => Fractional (Interval r) (Ord r, Data r) => Data (Interval r) (Num r, Ord r) => Num (Interval r) (Ord r, Read r) => Read (Interval r) (Ord r, Show r) => Show (Interval r) NFData r => NFData (Interval r) Hashable r => Hashable (Interval r) Ord r => JoinSemiLattice (Interval r) Ord r => MeetSemiLattice (Interval r) Ord r => Lattice (Interval r) Ord r => BoundedJoinSemiLattice (Interval r) Ord r => BoundedMeetSemiLattice (Interval r) Ord r => BoundedLattice (Interval r) Typeable (* -> *) Interval

type EndPoint r = Extended r Source

Endpoints of intervals

# Construction

Arguments

 :: Ord r => (Extended r, Bool) lower bound and whether it is included -> (Extended r, Bool) upper bound and whether it is included -> Interval r

smart constructor for `Interval`

(<=..<=) infix 5 Source

Arguments

 :: Ord r => Extended r lower bound `l` -> Extended r upper bound `u` -> Interval r

closed interval [`l`,`u`]

(<..<=) infix 5 Source

Arguments

 :: Ord r => Extended r lower bound `l` -> Extended r upper bound `u` -> Interval r

left-open right-closed interval (`l`,`u`]

(<=..<) infix 5 Source

Arguments

 :: Ord r => Extended r lower bound `l` -> Extended r upper bound `u` -> Interval r

left-closed right-open interval [`l`, `u`)

(<..<) infix 5 Source

Arguments

 :: Ord r => Extended r lower bound `l` -> Extended r upper bound `u` -> Interval r

open interval (`l`, `u`)

whole :: Ord r => Interval r Source

whole real number line (-∞, ∞)

empty :: Ord r => Interval r Source

singleton :: Ord r => r -> Interval r Source

singleton set [x,x]

# Query

null :: Ord r => Interval r -> Bool Source

Is the interval empty?

member :: Ord r => r -> Interval r -> Bool Source

Is the element in the interval?

notMember :: Ord r => r -> Interval r -> Bool Source

Is the element not in the interval?

isSubsetOf :: Ord r => Interval r -> Interval r -> Bool Source

Is this a subset? `(i1 `isSubsetOf` i2)` tells whether `i1` is a subset of `i2`.

isProperSubsetOf :: Ord r => Interval r -> Interval r -> Bool Source

Is this a proper subset? (i.e. a subset but not equal).

Lower endpoint (i.e. greatest lower bound) of the interval.

• `lowerBound` of the empty interval is `PosInf`.
• `lowerBound` of a left unbounded interval is `NegInf`.
• `lowerBound` of an interval may or may not be a member of the interval.

Upper endpoint (i.e. least upper bound) of the interval.

• `upperBound` of the empty interval is `NegInf`.
• `upperBound` of a right unbounded interval is `PosInf`.
• `upperBound` of an interval may or may not be a member of the interval.

lowerBound' :: Interval r -> (Extended r, Bool) Source

`lowerBound` of the interval and whether it is included in the interval. The result is convenient to use as an argument for `interval`.

upperBound' :: Interval r -> (Extended r, Bool) Source

`upperBound` of the interval and whether it is included in the interval. The result is convenient to use as an argument for `interval`.

width :: (Num r, Ord r) => Interval r -> r Source

Width of a interval. Width of an unbounded interval is `undefined`.

# Universal comparison operators

(<!) :: Ord r => Interval r -> Interval r -> Bool infix 4 Source

For all `x` in `X`, `y` in `Y`. `x < y`?

(<=!) :: Ord r => Interval r -> Interval r -> Bool infix 4 Source

For all `x` in `X`, `y` in `Y`. `x <= y`?

(==!) :: Ord r => Interval r -> Interval r -> Bool infix 4 Source

For all `x` in `X`, `y` in `Y`. `x == y`?

(>=!) :: Ord r => Interval r -> Interval r -> Bool infix 4 Source

For all `x` in `X`, `y` in `Y`. `x >= y`?

(>!) :: Ord r => Interval r -> Interval r -> Bool infix 4 Source

For all `x` in `X`, `y` in `Y`. `x > y`?

(/=!) :: Ord r => Interval r -> Interval r -> Bool infix 4 Source

For all `x` in `X`, `y` in `Y`. `x /= y`?

Since 1.0.1

# Existential comparison operators

(<?) :: Ord r => Interval r -> Interval r -> Bool infix 4 Source

Does there exist an `x` in `X`, `y` in `Y` such that `x < y`?

(<=?) :: Ord r => Interval r -> Interval r -> Bool infix 4 Source

Does there exist an `x` in `X`, `y` in `Y` such that `x <= y`?

(==?) :: Ord r => Interval r -> Interval r -> Bool infix 4 Source

Does there exist an `x` in `X`, `y` in `Y` such that `x == y`?

Since 1.0.0

(>=?) :: Ord r => Interval r -> Interval r -> Bool infix 4 Source

Does there exist an `x` in `X`, `y` in `Y` such that `x >= y`?

(>?) :: Ord r => Interval r -> Interval r -> Bool infix 4 Source

Does there exist an `x` in `X`, `y` in `Y` such that `x > y`?

(/=?) :: Ord r => Interval r -> Interval r -> Bool infix 4 Source

Does there exist an `x` in `X`, `y` in `Y` such that `x /= y`?

Since 1.0.1

# Existential comparison operators that produce witnesses (experimental)

(<??) :: (Real r, Fractional r) => Interval r -> Interval r -> Maybe (r, r) infix 4 Source

Does there exist an `x` in `X`, `y` in `Y` such that `x < y`?

Since 1.0.0

(<=??) :: (Real r, Fractional r) => Interval r -> Interval r -> Maybe (r, r) infix 4 Source

Does there exist an `x` in `X`, `y` in `Y` such that `x <= y`?

Since 1.0.0

(==??) :: (Real r, Fractional r) => Interval r -> Interval r -> Maybe (r, r) infix 4 Source

Does there exist an `x` in `X`, `y` in `Y` such that `x == y`?

Since 1.0.0

(>=??) :: (Real r, Fractional r) => Interval r -> Interval r -> Maybe (r, r) infix 4 Source

Does there exist an `x` in `X`, `y` in `Y` such that `x >= y`?

Since 1.0.0

(>??) :: (Real r, Fractional r) => Interval r -> Interval r -> Maybe (r, r) infix 4 Source

Does there exist an `x` in `X`, `y` in `Y` such that `x > y`?

Since 1.0.0

(/=??) :: (Real r, Fractional r) => Interval r -> Interval r -> Maybe (r, r) infix 4 Source

Does there exist an `x` in `X`, `y` in `Y` such that `x /= y`?

Since 1.0.1

# Combine

intersection :: forall r. Ord r => Interval r -> Interval r -> Interval r Source

intersection of two intervals

intersections :: Ord r => [Interval r] -> Interval r Source

intersection of a list of intervals.

Since 0.6.0

hull :: forall r. Ord r => Interval r -> Interval r -> Interval r Source

convex hull of two intervals

hulls :: Ord r => [Interval r] -> Interval r Source

convex hull of a list of intervals.

Since 0.6.0

# Operations

pickup :: (Real r, Fractional r) => Interval r -> Maybe r Source

pick up an element from the interval if the interval is not empty.

`simplestRationalWithin` returns the simplest rational number within the interval.

A rational number `y` is said to be simpler than another `y'` if

• `abs (numerator y) <= abs (numerator y')`, and
• `denominator y <= denominator y'`.

(see also `approxRational`)

Since 0.4.0