Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Synopsis
- data Conn (k :: Kan) a b
- identity :: Conn k a a
- type Triple a b = (Left a b, Right a b)
- pattern Conn :: (a -> b) -> (b -> a) -> (a -> b) -> Conn k a b
- type ConnK a b = forall k. Conn k a b
- embed :: Conn k a b -> b -> a
- extremal :: Triple () a => Conn k a Bool
- lub :: Triple (a, a) a => a -> a -> a -> a
- glb :: Triple (a, a) a => a -> a -> a -> a
- half :: (Num a, Preorder a) => ConnK a b -> a -> Maybe Ordering
- midpoint :: Fractional a => ConnK a b -> a -> a
- range :: Conn k a b -> a -> (b, b)
- round :: forall a b. (Num a, Triple a b) => a -> b
- round1 :: (Num a, Triple a b) => (a -> a) -> b -> b
- round2 :: (Num a, Triple a b) => (a -> a -> a) -> b -> b -> b
- truncate :: (Num a, Triple a b) => a -> b
- truncate1 :: (Num a, Triple a b) => (a -> a) -> b -> b
- truncate2 :: (Num a, Triple a b) => (a -> a -> a) -> b -> b -> b
- type Left = Connection 'L
- pattern ConnL :: (a -> b) -> (b -> a) -> ConnL a b
- type ConnL = Conn 'L
- connL :: Left a b => ConnL a b
- embedL :: Left a b => b -> a
- minimal :: Left () a => a
- join :: Left (a, a) a => a -> a -> a
- ceiling :: Left a b => a -> b
- ceiling1 :: Left a b => (a -> a) -> b -> b
- ceiling2 :: Left a b => (a -> a -> a) -> b -> b -> b
- type Right = Connection 'R
- pattern ConnR :: (b -> a) -> (a -> b) -> ConnR a b
- type ConnR = Conn 'R
- connR :: Right a b => ConnR a b
- embedR :: Right a b => b -> a
- maximal :: Right () a => a
- meet :: Right (a, a) a => a -> a -> a
- floor :: Right a b => a -> b
- floor1 :: Right a b => (a -> a) -> b -> b
- floor2 :: Right a b => (a -> a -> a) -> b -> b -> b
- (>>>) :: forall k cat (a :: k) (b :: k) (c :: k). Category cat => cat a b -> cat b c -> cat a c
- (<<<) :: forall k cat (b :: k) (c :: k) (a :: k). Category cat => cat b c -> cat a b -> cat a c
- (/|\) :: Connection k (c, c) c => Conn k a c -> Conn k b c -> Conn k (a, b) c
- (\|/) :: Conn k c a -> Conn k c b -> Conn k c (Either a b)
- choice :: Conn k a b -> Conn k c d -> Conn k (Either a c) (Either b d)
- strong :: Conn k a b -> Conn k c d -> Conn k (a, c) (b, d)
- data Kan
- type ConnInteger a = Left a (Maybe Integer)
- type ConnRational a = Triple Rational a
- type ConnExtended a b = Triple a (Extended b)
- class (Preorder a, Preorder b) => Connection k a b where
Conn
data Conn (k :: Kan) a b Source #
An adjoint string of Galois connections of length 2 or 3.
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)
Connection k
type Triple a b = (Left a b, Right a b) Source #
A constraint kind representing an adjoint triple of Galois connections.
pattern Conn :: (a -> b) -> (b -> a) -> (a -> b) -> Conn k a b Source #
Obtain a Conn from an adjoint triple of monotone functions.
This is a view pattern for an arbitrary Conn
. 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
.
type ConnK a b = forall k. 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 \)
For detailed properties see Property
.
lub :: Triple (a, a) a => a -> a -> a -> a Source #
Least upper bound operator.
The order dual of glb
.
>>>
lub 1.0 9.0 7.0
7.0>>>
lub 1.0 9.0 (0.0 / 0.0)
1.0
glb :: Triple (a, a) a => a -> a -> a -> a Source #
Greatest lower bound operator.
glb x x y = x glb x y z = glb z x y glb x y z = glb x z y glb (glb x w y) w z = glb x w (glb y w z)
>>>
glb 1.0 9.0 7.0
7.0>>>
glb 1.0 9.0 (0.0 / 0.0)
9.0>>>
glb (fromList [1..3]) (fromList [3..5]) (fromList [5..7]) :: Set Int
fromList [3,5]
midpoint :: Fractional a => ConnK a b -> a -> a Source #
Return the midpoint of the interval containing x.
>>>
pi - midpoint f64f32 pi
3.1786509424591713e-8
range :: Conn k a b -> a -> (b, b) Source #
Obtain the upper and/or lower adjoints of a connection.
range c = floorWith c &&& ceilingWith c
>>>
range f64f32 pi
(3.1415925,3.1415927)>>>
range f64f32 (0/0)
(NaN,NaN)
round :: forall a b. (Num a, Triple a b) => a -> b Source #
Return the nearest value to x.
round @a @a = id
If x lies halfway between two finite values, then return the value with the larger absolute value (i.e. round away from zero).
round1 :: (Num a, Triple a b) => (a -> a) -> b -> b Source #
Lift a unary function over a Conn
.
Results are rounded to the nearest value with ties away from 0.
round2 :: (Num a, Triple a b) => (a -> a -> a) -> b -> b -> b Source #
Lift a binary function over a Conn
.
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 @Rational @Float f maxOdd32 2.0
2.0>>>
maxOdd64 = 9.007199254740991e15
>>>
f maxOdd64 2.0 :: Double
1.0>>>
round2 @Rational @Double f maxOdd64 2.0
2.0
truncate1 :: (Num a, Triple a b) => (a -> a) -> b -> b Source #
Lift a unary function over a Conn
.
Results are truncated towards 0.
truncate2 :: (Num a, Triple a b) => (a -> a -> a) -> b -> b -> b Source #
Lift a binary function over a Conn
.
Results are truncated towards 0.
Connection L
type Left = Connection 'L Source #
pattern ConnL :: (a -> b) -> (b -> a) -> ConnL a b Source #
A view pattern for a ConnL
.
Caution: ConnL f g must obey \(f \dashv g \). This condition is not checked.
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 \} \)
For further information see Property
.
Caution: Monotonicity is not checked.
connL :: Left a b => ConnL a b Source #
A specialization of conn to left-side connections.
This is a convenience function provided primarily to avoid needing to enable DataKinds.
minimal :: Left () a => a Source #
A minimal element of a preorder.
minimal
needn't be unique, but it must obey:
x <~ minimal => x ~~ minimal
ceiling :: Left a b => a -> b Source #
Extract the ceiling of a Conn
or lower half of a ConnL
.
ceiling @a @a = id ceiling (x1 `join` a2) = ceiling x1 `join` ceiling x2
The latter law is the adjoint functor theorem for preorders.
>>>
Data.Connection.ceiling @Rational @Float (0 :% 0)
NaN>>>
Data.Connection.ceiling @Rational @Float (1 :% 0)
Infinity>>>
Data.Connection.ceiling @Rational @Float (13 :% 10)
1.3000001
Connection R
type Right = Connection 'R Source #
pattern ConnR :: (b -> a) -> (a -> b) -> ConnR a b Source #
A view pattern for a ConnR
.
Caution: ConnR f g must obey \(f \dashv g \). This condition is not checked.
A Galois connection between two monotone functions.
ConnR
is the mirror image of ConnL
:
swapR :: 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 (i.e. a ConnK
) that can lower into a standard
connection in either of two ways.
connR :: Right a b => ConnR a b Source #
A specialization of conn to right-side connections.
This is a convenience function provided primarily to avoid needing to enable DataKinds.
maximal :: Right () a => a Source #
A maximal element of a preorder.
maximal
needn't be unique, but it must obey:
x >~ maximal => x ~~ maximal
floor :: Right a b => a -> b Source #
Extract the floor of a ConnK
or upper half of a ConnL
.
floor @a @a = id floor (x1 `meet` x2) = floor x1 `meet` floor x2
The latter law is the adjoint functor theorem for preorders.
>>>
Data.Connection.floor @Rational @Float (0 :% 0)
NaN>>>
Data.Connection.floor @Rational @Float (1 :% 0)
Infinity>>>
Data.Connection.floor @Rational @Float (13 :% 10)
1.3
Combinators
(>>>) :: forall k cat (a :: k) (b :: k) (c :: k). Category cat => cat a b -> cat b c -> cat a c infixr 1 #
Left-to-right composition
(<<<) :: forall k cat (b :: k) (c :: k) (a :: k). Category cat => cat b c -> cat a b -> cat a c infixr 1 #
Right-to-left composition
(/|\) :: Connection k (c, c) c => Conn k a c -> Conn k b c -> Conn k (a, b) c infixr 4 Source #
A preorder variant of &&&
.
(\|/) :: Conn k c a -> Conn k c b -> Conn k c (Either a b) infixr 3 Source #
A preorder variant of |||
.
choice :: Conn k a b -> Conn k c d -> Conn k (Either a c) (Either b d) Source #
Lift two Conn
s 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 Conn
s 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
A data kind distinguishing the directionality of a Galois connection:
type ConnInteger a = Left a (Maybe Integer) Source #
A constraint kind for Integer
conversions.
Usable in conjunction with RebindableSyntax:
fromInteger = embedL . Just :: ConnInteger a => Integer -> a
type ConnRational a = Triple Rational a Source #
A constraint kind for Rational
conversions.
Usable in conjunction with RebindableSyntax:
fromRational = round :: ConnRational a => Rational -> a
type ConnExtended a b = Triple a (Extended b) Source #
class (Preorder a, Preorder b) => Connection k a b where Source #
An adjoint string of Galois connections of length 2 or 3.
>>>
range (conn @_ @Rational @Float) (22 :% 7)
(3.142857,3.1428573)>>>
range (conn @_ @Double @Float) pi
(3.1415925,3.1415927)