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

Safe HaskellSafe
LanguageHaskell2010

Data.Connection.Conn

Contents

Synopsis

Conn

data Kan Source #

A data kind distinguishing the directionality of a Galois connection:

Constructors

L 
R 

data Conn (k :: Kan) a b Source #

A (chain of) Galois connections.

A Galois connection between preorders P and Q is a pair of monotone maps `f :: p -> q` and `g :: q -> p` such that:

f x <= y iff x <= g y

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

Connections have many nice properties wrt numerical conversion:

>>> range (conn @_ @Rational @Float) (1 :% 8) -- eighths are exactly representable in a float
(0.125,0.125)
>>> range (conn @_ @Rational @Float) (1 :% 7) -- sevenths are not
(0.14285713,0.14285715)

See the README file for a slightly more in-depth introduction.

Instances
Category (Conn k :: Type -> Type -> Type) Source # 
Instance details

Defined in Data.Connection.Conn

Methods

id :: Conn k a a #

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

embed :: Conn k a b -> b -> a Source #

Retrieve the upper adjoint of a ConnL, or lower adjoint of a ConnR.

range :: Conn k a b -> a -> (b, b) Source #

Retrieve the left and/or right adjoints of a connection.

range c = floor c &&& ceiling c
>>> range f64f32 pi
(3.1415925,3.1415927)
>>> range f64f32 (0/0)
(NaN,NaN)

identity :: Conn k a a Source #

The identity Conn.

(>>>) :: 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

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

Lift a Conn into a functor.

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

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

Lift two Conns into a Conn 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

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

Lift two Conns into a Conn 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

Connection L

pattern ConnL :: (a -> b) -> (b -> a) -> ConnL 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: ConnL f g must obey \(f \dashv g \). This condition is not checked.

For further information see Property.

connL :: ConnR a b -> ConnL b a Source #

Witness to the symmetry between ConnL and ConnR.

connL . connR = id
connR . connL = id

upper :: ConnL a b -> b -> a Source #

Obtain the upper adjoint of a ConnL, or lower adjoint of a ConnR.

upper1 :: ConnL a b -> (b -> b) -> a -> a Source #

Map over a ConnL from the right.

This is the unit of the resulting monad:

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

upper2 :: ConnL a b -> (b -> b -> b) -> a -> a -> a Source #

Zip over a ConnL from the right.

join :: ConnL (a, a) b -> a -> a -> b infixr 5 Source #

Semigroup operation on a join-semilattice.

ceiling :: ConnL a b -> a -> b Source #

Extract the lower half of a ConnL.

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

The latter law is the adjoint functor theorem for preorders.

>>> Data.Connection.ceiling ratf32 (0 :% 0)
NaN
>>> Data.Connection.ceiling ratf32 (1 :% 0)
Infinity
>>> Data.Connection.ceiling ratf32 (13 :% 10)
1.3000001
>>> Data.Connection.ceiling f64f32 pi
3.1415927

ceiling1 :: ConnL a b -> (a -> a) -> b -> b Source #

Map over a ConnL from the left.

This is the counit of the resulting comonad:

x >~ ceiling1 c id x
>>> ceiling1 (conn @_ @() @Ordering) id LT
LT
>>> ceiling1 (conn @_ @() @Ordering) id GT
LT

ceiling2 :: ConnL a b -> (a -> a -> a) -> b -> b -> b Source #

Zip over a ConnL from the left.

Connection R

pattern ConnR :: (b -> a) -> (a -> b) -> ConnR a b Source #

A Galois connection between two monotone functions.

ConnR is the mirror image of ConnL:

connR :: ConnL a b -> ConnR b a

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

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

For further information see Property.

connR :: ConnL a b -> ConnR b a Source #

Witness to the symmetry between ConnL and ConnR.

connL . connR = id
connR . connL = id

lower :: ConnR a b -> b -> a Source #

Obtain the lower adjoint of a ConnR.

lower1 :: ConnR a b -> (b -> b) -> a -> a Source #

Map over a ConnR from the left.

This is the counit of the resulting comonad:

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

lower2 :: ConnR a b -> (b -> b -> b) -> a -> a -> a Source #

Zip over a ConnR from the left.

meet :: ConnR (a, a) b -> a -> a -> b infixr 6 Source #

Semigroup operation on a meet-semilattice.

floor :: ConnR a b -> a -> b Source #

Extract the upper half of a ConnR

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

The latter law is the adjoint functor theorem for preorders.

>>> Data.Connection.floor ratf32 (0 :% 0)
NaN
>>> Data.Connection.floor ratf32 (1 :% 0)
Infinity
>>> Data.Connection.floor ratf32 (13 :% 10)
1.3
>>> Data.Connection.floor f64f32 pi
3.1415925

floor1 :: ConnR a b -> (a -> a) -> b -> b Source #

Map over a ConnR from the right.

This is the unit of the resulting monad:

x <~ floor1 c id x
>>> floor1 (conn @_ @() @Ordering) id LT
GT
>>> floor1 (conn @_ @() @Ordering) id GT
GT

floor2 :: ConnR a b -> (a -> a -> a) -> b -> b -> b Source #

Zip over a ConnR from the right.

Connection k

pattern Conn :: (a -> b) -> (b -> a) -> (a -> b) -> Conn 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 ConnL or ConnR, the two functions of type a -> b returned will be identical.

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

For detailed properties see Property.

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

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

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

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

Return the midpoint of the interval containing x.

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

round :: (Num a, Preorder a) => (forall k. Conn 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 larger absolute value (i.e. round away from zero).

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

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

Lift a unary function over a Trip.

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

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

Lift a binary function over a Trip.

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

Example avoiding 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 :: (Num a, Preorder a) => (forall k. Conn k a b) -> a -> b Source #

Truncate towards zero.

truncate identity = id

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

Lift a unary function over a Trip.

Results are truncated towards zero.

truncate1 identity = id

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

Down

upL :: ConnL (Down a) (Down b) -> ConnL b a Source #

Convert an inverted ConnL to a ConnL.

upL . downL = downL . upL = id

upR :: ConnR (Down a) (Down b) -> ConnR b a Source #

Convert an inverted ConnR to a ConnR.

upR . downR = downR . upR = id

downL :: ConnL a b -> ConnL (Down b) (Down a) Source #

Convert a ConnL to an inverted ConnL.

>>> let counit = upper1 (downL $ conn @_ @() @Ordering) id
>>> counit (Down LT)
Down LT
>>> counit (Down GT)
Down LT

downR :: ConnR a b -> ConnR (Down b) (Down a) Source #

Convert a ConnR to an inverted ConnR.

>>> let unit = lower1 (downR $ conn @_ @() @Ordering) id
>>> unit (Down LT)
Down GT
>>> unit (Down GT)
Down GT

filterL :: Preorder b => ConnL a b -> a -> b -> Bool Source #

Obtain the principal filter in B generated by an element of A.

A subset B of a lattice is an filter if and only if it is an upper set that is closed under finite meets, i.e., it is nonempty and for all x, y in B, the element meet c x y is also in b.

filterL and filterR commute with Down:

filterL c a b <=> ideal c (Down a) (Down b)
filterL c (Down a) (Down b) <=> ideal c a b

filterL c a is upward-closed for all a:

a <= b1 && b1 <= b2 => a <= b2
a1 <= b && a2 <= b => meet c (ceiling c a1) (ceiling c a2) <= b

See https://en.wikipedia.org/wiki/Filter_(mathematics)

filterR :: Preorder b => ConnR a b -> a -> b -> Bool Source #

Obtain the principal ideal in B generated by an element of A.

A subset B of a lattice is an ideal if and only if it is a lower set that is closed under finite joins, i.e., it is nonempty and for all x, y in B, the element join c x y is also in B.

filterR c a is downward-closed for all a:

a >= b1 && b1 >= b2 => a >= b2
a1 >= b && a2 >= b => join c (floor c a1) (floor c a2) >= b

See https://en.wikipedia.org/wiki/Ideal_(order_theory)

newtype Down a #

The Down type allows you to reverse sort order conveniently. A value of type Down a contains a value of type a (represented as Down a). If a has an Ord instance associated with it then comparing two values thus wrapped will give you the opposite of their normal sort order. This is particularly useful when sorting in generalised list comprehensions, as in: then sortWith by Down x

Since: base-4.6.0.0

Constructors

Down a 
Instances
Monad Down

Since: base-4.11.0.0

Instance details

Defined in Data.Ord

Methods

(>>=) :: Down a -> (a -> Down b) -> Down b #

(>>) :: Down a -> Down b -> Down b #

return :: a -> Down a #

fail :: String -> Down a #

Functor Down

Since: base-4.11.0.0

Instance details

Defined in Data.Ord

Methods

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

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

Applicative Down

Since: base-4.11.0.0

Instance details

Defined in Data.Ord

Methods

pure :: a -> Down a #

(<*>) :: Down (a -> b) -> Down a -> Down b #

liftA2 :: (a -> b -> c) -> Down a -> Down b -> Down c #

(*>) :: Down a -> Down b -> Down b #

(<*) :: Down a -> Down b -> Down a #

Foldable Down

Since: base-4.12.0.0

Instance details

Defined in Data.Foldable

Methods

fold :: Monoid m => Down m -> m #

foldMap :: Monoid m => (a -> m) -> Down a -> m #

foldr :: (a -> b -> b) -> b -> Down a -> b #

foldr' :: (a -> b -> b) -> b -> Down a -> b #

foldl :: (b -> a -> b) -> b -> Down a -> b #

foldl' :: (b -> a -> b) -> b -> Down a -> b #

foldr1 :: (a -> a -> a) -> Down a -> a #

foldl1 :: (a -> a -> a) -> Down a -> a #

toList :: Down a -> [a] #

null :: Down a -> Bool #

length :: Down a -> Int #

elem :: Eq a => a -> Down a -> Bool #

maximum :: Ord a => Down a -> a #

minimum :: Ord a => Down a -> a #

sum :: Num a => Down a -> a #

product :: Num a => Down a -> a #

Traversable Down

Since: base-4.12.0.0

Instance details

Defined in Data.Traversable

Methods

traverse :: Applicative f => (a -> f b) -> Down a -> f (Down b) #

sequenceA :: Applicative f => Down (f a) -> f (Down a) #

mapM :: Monad m => (a -> m b) -> Down a -> m (Down b) #

sequence :: Monad m => Down (m a) -> m (Down a) #

Eq1 Down

Since: base-4.12.0.0

Instance details

Defined in Data.Functor.Classes

Methods

liftEq :: (a -> b -> Bool) -> Down a -> Down b -> Bool #

Ord1 Down

Since: base-4.12.0.0

Instance details

Defined in Data.Functor.Classes

Methods

liftCompare :: (a -> b -> Ordering) -> Down a -> Down b -> Ordering #

Read1 Down

Since: base-4.12.0.0

Instance details

Defined in Data.Functor.Classes

Methods

liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Down a) #

liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [Down a] #

liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (Down a) #

liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [Down a] #

Show1 Down

Since: base-4.12.0.0

Instance details

Defined in Data.Functor.Classes

Methods

liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Down a -> ShowS #

liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [Down a] -> ShowS #

Eq a => Eq (Down a)

Since: base-4.6.0.0

Instance details

Defined in Data.Ord

Methods

(==) :: Down a -> Down a -> Bool #

(/=) :: Down a -> Down a -> Bool #

Num a => Num (Down a)

Since: base-4.11.0.0

Instance details

Defined in Data.Ord

Methods

(+) :: Down a -> Down a -> Down a #

(-) :: Down a -> Down a -> Down a #

(*) :: Down a -> Down a -> Down a #

negate :: Down a -> Down a #

abs :: Down a -> Down a #

signum :: Down a -> Down a #

fromInteger :: Integer -> Down a #

Ord a => Ord (Down a)

Since: base-4.6.0.0

Instance details

Defined in Data.Ord

Methods

compare :: Down a -> Down a -> Ordering #

(<) :: Down a -> Down a -> Bool #

(<=) :: Down a -> Down a -> Bool #

(>) :: Down a -> Down a -> Bool #

(>=) :: Down a -> Down a -> Bool #

max :: Down a -> Down a -> Down a #

min :: Down a -> Down a -> Down a #

Read a => Read (Down a)

Since: base-4.7.0.0

Instance details

Defined in Data.Ord

Show a => Show (Down a)

Since: base-4.7.0.0

Instance details

Defined in Data.Ord

Methods

showsPrec :: Int -> Down a -> ShowS #

show :: Down a -> String #

showList :: [Down a] -> ShowS #

Generic (Down a) 
Instance details

Defined in GHC.Generics

Associated Types

type Rep (Down a) :: Type -> Type #

Methods

from :: Down a -> Rep (Down a) x #

to :: Rep (Down a) x -> Down a #

Semigroup a => Semigroup (Down a)

Since: base-4.11.0.0

Instance details

Defined in Data.Ord

Methods

(<>) :: Down a -> Down a -> Down a #

sconcat :: NonEmpty (Down a) -> Down a #

stimes :: Integral b => b -> Down a -> Down a #

Monoid a => Monoid (Down a)

Since: base-4.11.0.0

Instance details

Defined in Data.Ord

Methods

mempty :: Down a #

mappend :: Down a -> Down a -> Down a #

mconcat :: [Down a] -> Down a #

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

Defined in Data.Order

Methods

(<~) :: Down a -> Down a -> Bool Source #

(>~) :: Down a -> Down a -> Bool Source #

(?~) :: Down a -> Down a -> Bool Source #

(~~) :: Down a -> Down a -> Bool Source #

(/~) :: Down a -> Down a -> Bool Source #

plt :: Down a -> Down a -> Bool Source #

pgt :: Down a -> Down a -> Bool Source #

similar :: Down a -> Down a -> Bool Source #

pmax :: Down a -> Down a -> Maybe (Down a) Source #

pmin :: Down a -> Down a -> Maybe (Down a) Source #

pcompare :: Down a -> Down a -> Maybe Ordering Source #

Generic1 Down 
Instance details

Defined in GHC.Generics

Associated Types

type Rep1 Down :: k -> Type #

Methods

from1 :: Down a -> Rep1 Down a #

to1 :: Rep1 Down a -> Down a #

type Rep (Down a)

Since: base-4.12.0.0

Instance details

Defined in GHC.Generics

type Rep1 Down

Since: base-4.12.0.0

Instance details

Defined in GHC.Generics