connections-0.3.2: Orders, Galois connections, and lattices.

Safe HaskellSafe
LanguageHaskell2010
ExtensionsPatternSynonyms

Data.Connection

Contents

Description

This library provides connections between common types, combinators & accessors, including lawful versions of floor, ceiling, round, and truncate. There is also a separately exported Connection class. Lastly, there are Semilattice and Algebra classes based on the same construction.

connections is extensively tested, and it exports properties for use in testing your own connections. See Data.Connection.Property and Data.Lattice.Property. In addition to the property tests there are several doctests scattered around, these are runnable as a standalone executable. See the doctest stanza in the library's cabal file.

Synopsis

Documentation

What is a Galois connection?

A Galois connection is an adjunction in the category of preorders: a pair of monotone maps f :: p -> q and g :: q -> p between preorders p and q, such that

f x <= y if and only if x <= g y

We say that f is the left or lower adjoint, and g is the right or upper adjoint of the connection.

For illustration, here is a simple example from 7 Sketches:

Note that the two component functions are each monotonic:

 x1 <= x2 implies f x1 <= f x2

and furthermore they are are interlocked (i.e. adjoint) in the specific way outlined above:

 f x <= y if and only if x <= g y

See the README for a more extensive overview.

data Side Source #

A data kind distinguishing links in a chain of Galois connections of length 2 or 3.

If a connection is existentialized over this value (i.e. has type forall k. Cast k a b) then it can provide either of two functions f, h :: a -> b.

This is useful because it enables rounding, truncation, medians, etc.

Constructors

L 
R 

Types

CastL

castL :: Connection L a b => Cast L a b Source #

pattern CastL :: (a -> b) -> (b -> a) -> Cast L a b Source #

A Galois connection between two monotone functions.

A Galois connection between f and g, written \(f \dashv g \) is an adjunction in the category of preorders.

Each side of the connection may be defined in terms of the other:

\( g(x) = \sup \{y \in E \mid f(y) \leq x \} \)

\( f(x) = \inf \{y \in E \mid g(y) \geq x \} \)

Caution: CastL f g must obey \(f \dashv g \). This condition is not checked.

For further information see Property.

CastR

castR :: Connection R a b => Cast R a b Source #

pattern CastR :: (b -> a) -> (a -> b) -> Cast R a b Source #

A Galois connection between two monotone functions.

CastR is the mirror image of CastL.

If you only require one connection there is no particular reason to use one version over the other. However many use cases (e.g. rounding) require an adjoint triple of connections that can lower into a standard connection in either of two ways.

Caution: CastR f g must obey \(f \dashv g \). This condition is not checked.

For further information see Property.

Cast

cast :: Connection k a b => Cast k a b Source #

pattern Cast :: (a -> b) -> (b -> a) -> (a -> b) -> Cast k a b Source #

An adjoint triple of Galois connections.

An adjoint triple is a chain of connections of length 3:

\(f \dashv g \dashv h \)

When applied to a CastL or CastR, the two functions of type a -> b returned will be identical.

Caution: Cast f g h must obey \(f \dashv g \dashv h\). This condition is not checked.

For detailed properties see Property.

data Cast (k :: Side) a b Source #

A chain of Galois connections of length 2 or 3.

Connections have many nice properties wrt numerical conversion:

>>> upper ratf32 (1 / 8)    -- eighths are exactly representable in a float
1 % 8
>>> upper ratf32 (1 / 7)    -- sevenths are not
9586981 % 67108864
>>> floor ratf32 &&& ceiling ratf32 $ 1 % 8
(0.125,0.125)
>>> floor ratf32 &&& ceiling ratf32 $ 1 % 7
(0.14285713,0.14285715)

Another example avoiding loss-of-precision:

>>> f x y = (x + y) - x
>>> maxOdd32 = 1.6777215e7
>>> f maxOdd32 2.0 :: Float
1.0
>>> round2 f64f32 f maxOdd32 2.0
2.0
Instances
Category (Cast k :: Type -> Type -> Type) Source # 
Instance details

Defined in Data.Connection.Cast

Methods

id :: Cast k a a #

(.) :: Cast k b c -> Cast k a b -> Cast k a c #

Accessors

midpoint :: Fractional a => (forall k. Cast k a b) -> a -> a Source #

Return the midpoint of the interval containing x.

For example, the (double-precision) error of the single-precision floating point approximation of pi is:

>>> pi - midpoint f64f32 pi
3.1786509424591713e-8

interval :: (Num a, Preorder a) => (forall k. Cast k a b) -> a -> Maybe Ordering Source #

Determine which half of the interval between 2 representations of a a particular value lies.

 interval c x = pcompare (x - lower1 c id x) (upper1 c id x - x)
>>> maybe False (== EQ) $ interval f64f32 (midpoint f64f32 pi)
True

upper

upper :: Cast L a b -> b -> a Source #

Extract the upper half of a CastL.

>>> upper ratf32 (1 / 8)    -- eighths are exactly representable in a float
1 % 8
>>> upper ratf32 (1 / 7)    -- sevenths are not
9586981 % 67108864

upper1 :: Cast L a b -> (b -> b) -> a -> a Source #

Map over a CastL from the right.

This is the unit of the resulting monad:

x <~ upper1 c id x
>>> compare pi $ upper1 f64f32 id pi
LT

upper2 :: Cast L a b -> (b -> b -> b) -> a -> a -> a Source #

Zip over a CastL from the right.

lower

lower :: Cast R a b -> b -> a Source #

Extract the lower half of a CastR.

lower1 :: Cast R a b -> (b -> b) -> a -> a Source #

Map over a CastR from the left.

This is the counit of the resulting comonad:

x >~ lower1 c id x
>>> compare pi $ lower1 f64f32 id pi
GT

lower2 :: Cast R a b -> (b -> b -> b) -> a -> a -> a Source #

Zip over a CastR from the left.

ceiling

ceiling :: Cast L a b -> a -> b Source #

Extract the lower half of a CastL.

ceiling identity = id
ceiling c (x \/ y) = ceiling c x \/ ceiling c y

The latter law is the adjoint functor theorem for preorders.

>>> ceiling ratf32 (0 :% 0)
NaN
>>> ceiling ratf32 (13 :% 10)
1.3000001
>>> ceiling f64f32 pi
3.1415927

ceiling1 :: Cast L a b -> (a -> a) -> b -> b Source #

Map over a CastL from the left.

ceiling1 identity = id

This is the counit of the resulting comonad:

x >~ ceiling1 c id x

ceiling2 :: Cast L a b -> (a -> a -> a) -> b -> b -> b Source #

Zip over a CastL from the left.

floor

floor :: Cast R a b -> a -> b Source #

Extract the upper half of a CastR

floor identity = id
floor c (x /\ y) = floor c x /\ floor c y

The latter law is the adjoint functor theorem for preorders.

>>> floor ratf32 (0 :% 0)
NaN
>>> floor ratf32 (13 :% 10)
1.3
>>> floor f64f32 pi
3.1415925

floor1 :: Cast R a b -> (a -> a) -> b -> b Source #

Map over a CastR from the right.

floor1 identity = id

This is the unit of the resulting monad:

x <~ floor1 c id x

floor2 :: Cast R a b -> (a -> a -> a) -> b -> b -> b Source #

Zip over a CastR from the right.

round

round :: (Num a, Preorder a) => (forall k. Cast k a b) -> a -> b Source #

Return the nearest value to x.

round identity = id

If x lies halfway between two finite values, then return the value with the smaller absolute value (i.e. round towards from zero).

See https://en.wikipedia.org/wiki/Rounding.

round1 :: (Num a, Preorder a) => (forall k. Cast k a b) -> (a -> a) -> b -> b Source #

Lift a unary function over an adjoint triple.

round1 identity = id

Results are rounded to the nearest value with ties away from 0.

round2 :: (Num a, Preorder a) => (forall k. Cast k a b) -> (a -> a -> a) -> b -> b -> b Source #

Lift a binary function over an adjoint triple.

round2 identity = id

Results are rounded to the nearest value with ties away from 0.

For example, to avoid a loss of precision:

>>> f x y = (x + y) - x
>>> maxOdd32 = 1.6777215e7
>>> f maxOdd32 2.0 :: Float
1.0
>>> round2 ratf32 f maxOdd32 2.0
2.0

truncate

truncate :: (Num a, Preorder a) => (forall k. Cast k a b) -> a -> b Source #

Truncate towards zero.

truncate identity = id

truncate1 :: (Num a, Preorder a) => (forall k. Cast k a b) -> (a -> a) -> b -> b Source #

Lift a unary function over an adjoint triple.

truncate1 identity = id

Results are truncated towards zero.

truncate2 :: (Num a, Preorder a) => (forall k. Cast k a b) -> (a -> a -> a) -> b -> b -> b Source #

Lift a binary function over an adjoint triple.

truncate2 identity = id

Results are truncated towards zero.

max/min

maximize :: Cast L (a, b) c -> a -> b -> c Source #

Generalized maximum.

minimize :: Cast R (a, b) c -> a -> b -> c Source #

Generalized minimum.

median :: (forall k. Cast k (a, a) a) -> a -> a -> a -> a Source #

Birkoff's median operator.

median x x y = x
median x y z = median z x y
median x y z = median x z y
median (median x w y) w z = median x w (median y w z)
>>> median f32f32 1.0 9.0 7.0
7.0
>>> median f32f32 1.0 9.0 (0.0 / 0.0)
9.0

Combinators

(>>>) :: Category cat => cat a b -> cat b c -> cat a c infixr 1 #

Left-to-right composition

(<<<) :: Category cat => cat b c -> cat a b -> cat a c infixr 1 #

Right-to-left composition

swapL :: Cast R a b -> Cast L b a Source #

Witness to the mirror symmetry between CastL and CastR.

swapL . swapR = id
swapR . swapL = id

swapR :: Cast L a b -> Cast R b a Source #

Witness to the mirror symmetry between CastL and CastR.

swapL . swapR = id
swapR . swapL = id

mapped :: Functor f => Cast k a b -> Cast k (f a) (f b) Source #

Lift a Cast into a functor.

Caution: This function will result in an invalid connection if the functor alters the internal preorder (e.g. Down).

choice :: Cast k a b -> Cast k c d -> Cast k (Either a c) (Either b d) Source #

Lift two connections into a connection on the coproduct order

(choice id) (ab >>> cd) = (choice id) ab >>> (choice id) cd
(flip choice id) (ab >>> cd) = (flip choice id) ab >>> (flip choice id) cd

select :: Cast k c a -> Cast k c b -> Cast k c (Either a b) infixr 3 Source #

Lift two connections into a connection on the coproduct order

strong :: Cast k a b -> Cast k c d -> Cast k (a, c) (b, d) Source #

Lift two connections into a connection on the product order

(strong id) (ab >>> cd) = (strong id) ab >>> (strong id) cd
(flip strong id) (ab >>> cd) = (flip strong id) ab >>> (flip strong id) cd

divide :: Ord c => Cast k a c -> Cast k b c -> Cast k (a, b) c infixr 4 Source #

Lift two connections into a connection on the product order

Extended

extended :: b -> b -> (a -> b) -> Extended a -> b Source #

Eliminate an Extended.

data Extended r #

Extended r is an extension of r with positive/negative infinity (±∞).

Constructors

NegInf

negative infinity (-∞)

Finite !r

finite value

PosInf

positive infinity (+∞)

Instances
Functor Extended 
Instance details

Defined in Data.ExtendedReal

Methods

fmap :: (a -> b) -> Extended a -> Extended b #

(<$) :: a -> Extended b -> Extended a #

Connection k Double (Extended Int32) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Double (Extended Int16) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Double (Extended Int8) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Double (Extended Word32) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Double (Extended Word16) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Double (Extended Word8) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Float (Extended Int16) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Float (Extended Int8) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Float (Extended Word16) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Float (Extended Word8) Source # 
Instance details

Defined in Data.Connection.Class

HasResolution res => Connection k Rational (Extended (Fixed res)) Source # 
Instance details

Defined in Data.Connection.Class

Methods

cast :: Cast k Rational (Extended (Fixed res)) Source #

Connection k Rational (Extended SystemTime) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Rational (Extended Integer) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Rational (Extended Int) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Rational (Extended Int64) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Rational (Extended Int32) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Rational (Extended Int16) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Rational (Extended Int8) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Rational (Extended Natural) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Rational (Extended Word) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Rational (Extended Word64) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Rational (Extended Word32) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Rational (Extended Word16) Source # 
Instance details

Defined in Data.Connection.Class

Connection k Rational (Extended Word8) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Double (Extended Int) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Double (Extended Int64) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Double (Extended Integer) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Double (Extended Natural) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Double (Extended Word) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Double (Extended Word64) Source # 
Instance details

Defined in Data.Connection.Class

HasResolution res => Connection L Double (Extended (Fixed res)) Source # 
Instance details

Defined in Data.Connection.Class

Methods

cast :: Cast L Double (Extended (Fixed res)) Source #

Connection L Double (Extended SystemTime) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Float (Extended Int) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Float (Extended Int32) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Float (Extended Int64) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Float (Extended Integer) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Float (Extended Natural) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Float (Extended Word) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Float (Extended Word32) Source # 
Instance details

Defined in Data.Connection.Class

Connection L Float (Extended Word64) Source # 
Instance details

Defined in Data.Connection.Class

HasResolution res => Connection L Float (Extended (Fixed res)) Source # 
Instance details

Defined in Data.Connection.Class

Methods

cast :: Cast L Float (Extended (Fixed res)) Source #

Connection L Float (Extended SystemTime) Source # 
Instance details

Defined in Data.Connection.Class

Heyting a => Algebra R (Extended a) Source # 
Instance details

Defined in Data.Lattice

Methods

algebra :: Extended a -> Cast R (Extended a) (Extended a) Source #

Join a => Semilattice L (Extended a) Source # 
Instance details

Defined in Data.Lattice

Meet a => Semilattice R (Extended a) Source # 
Instance details

Defined in Data.Lattice

Connection k (Extended Int32) Int Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Int16) Int Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Int8) Int Source # 
Instance details

Defined in Data.Connection.Class

Methods

cast :: Cast k (Extended Int8) Int Source #

Connection k (Extended Word32) Int Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Word16) Int Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Word8) Int Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Int32) Int64 Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Int16) Int64 Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Int8) Int64 Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Word32) Int64 Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Word16) Int64 Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Word8) Int64 Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Int16) Int32 Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Int8) Int32 Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Word16) Int32 Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Word8) Int32 Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Int8) Int16 Source # 
Instance details

Defined in Data.Connection.Class

Connection k (Extended Word8) Int16 Source # 
Instance details

Defined in Data.Connection.Class

Connection L (Extended Int) (Maybe Integer) Source # 
Instance details

Defined in Data.Connection.Class

Connection L (Extended Int8) (Maybe Integer) Source # 
Instance details

Defined in Data.Connection.Class

Connection L (Extended Int16) (Maybe Integer) Source # 
Instance details

Defined in Data.Connection.Class

Connection L (Extended Int32) (Maybe Integer) Source # 
Instance details

Defined in Data.Connection.Class

Connection L (Extended Int64) (Maybe Integer) Source # 
Instance details

Defined in Data.Connection.Class

Connection L (Extended Word) (Maybe Integer) Source # 
Instance details

Defined in Data.Connection.Class

Connection L (Extended Word8) (Maybe Integer) Source # 
Instance details

Defined in Data.Connection.Class

Connection L (Extended Word16) (Maybe Integer) Source # 
Instance details

Defined in Data.Connection.Class

Connection L (Extended Word32) (Maybe Integer) Source # 
Instance details

Defined in Data.Connection.Class

Connection L (Extended Word64) (Maybe Integer) Source # 
Instance details

Defined in Data.Connection.Class

Bounded (Extended r) 
Instance details

Defined in Data.ExtendedReal

Eq r => Eq (Extended r) 
Instance details

Defined in Data.ExtendedReal

Methods

(==) :: Extended r -> Extended r -> Bool #

(/=) :: Extended r -> Extended r -> Bool #

(Fractional r, Ord r) => Fractional (Extended r)

Note that Extended r is not a field, nor a ring.

Instance details

Defined in Data.ExtendedReal

Data r => Data (Extended r) 
Instance details

Defined in Data.ExtendedReal

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Extended r -> c (Extended r) #

gunfold :: (forall b r0. Data b => c (b -> r0) -> c r0) -> (forall r1. r1 -> c r1) -> Constr -> c (Extended r) #

toConstr :: Extended r -> Constr #

dataTypeOf :: Extended r -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Extended r)) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Extended r)) #

gmapT :: (forall b. Data b => b -> b) -> Extended r -> Extended r #

gmapQl :: (r0 -> r' -> r0) -> r0 -> (forall d. Data d => d -> r') -> Extended r -> r0 #

gmapQr :: (r' -> r0 -> r0) -> r0 -> (forall d. Data d => d -> r') -> Extended r -> r0 #

gmapQ :: (forall d. Data d => d -> u) -> Extended r -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Extended r -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Extended r -> m (Extended r) #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Extended r -> m (Extended r) #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Extended r -> m (Extended r) #

(Num r, Ord r) => Num (Extended r)

Note that Extended r is not a field, nor a ring.

PosInf + NegInf is left undefined as usual, but we define 0 * PosInf = 0 * NegInf = 0 by following the convention of probability or measure theory.

Instance details

Defined in Data.ExtendedReal

Ord r => Ord (Extended r) 
Instance details

Defined in Data.ExtendedReal

Methods

compare :: Extended r -> Extended r -> Ordering #

(<) :: Extended r -> Extended r -> Bool #

(<=) :: Extended r -> Extended r -> Bool #

(>) :: Extended r -> Extended r -> Bool #

(>=) :: Extended r -> Extended r -> Bool #

max :: Extended r -> Extended r -> Extended r #

min :: Extended r -> Extended r -> Extended r #

Read r => Read (Extended r) 
Instance details

Defined in Data.ExtendedReal

Show r => Show (Extended r) 
Instance details

Defined in Data.ExtendedReal

Methods

showsPrec :: Int -> Extended r -> ShowS #

show :: Extended r -> String #

showList :: [Extended r] -> ShowS #

NFData r => NFData (Extended r) 
Instance details

Defined in Data.ExtendedReal

Methods

rnf :: Extended r -> () #

Hashable r => Hashable (Extended r) 
Instance details

Defined in Data.ExtendedReal

Methods

hashWithSalt :: Int -> Extended r -> Int #

hash :: Extended r -> Int #

Preorder a => Preorder (Extended a) Source # 
Instance details

Defined in Data.Order