Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Synopsis
- data Kan
- data Conn (k :: Kan) a b
- pattern Conn :: (a -> b) -> (b -> a) -> (a -> b) -> Conn k a b
- embed :: Conn k a b -> b -> a
- range :: Conn k a b -> a -> (b, b)
- identity :: Conn k a a
- type ConnK a b = forall k. Conn k a b
- half :: (Num a, Preorder a) => ConnK a b -> a -> Maybe Ordering
- midpoint :: Fractional a => ConnK a b -> a -> a
- roundWith :: forall a b. (Num a, Preorder a) => ConnK a b -> a -> b
- roundWith1 :: (Num a, Preorder a) => ConnK a b -> (a -> a) -> b -> b
- roundWith2 :: (Num a, Preorder a) => ConnK a b -> (a -> a -> a) -> b -> b -> b
- truncateWith :: (Num a, Preorder a) => ConnK a b -> a -> b
- truncateWith1 :: (Num a, Preorder a) => ConnK a b -> (a -> a) -> b -> b
- truncateWith2 :: (Num a, Preorder a) => ConnK a b -> (a -> a -> a) -> b -> b -> b
- type ConnL = Conn 'L
- pattern ConnL :: (a -> b) -> (b -> a) -> ConnL a b
- upL :: ConnL (Down a) (Down b) -> ConnL b a
- downL :: ConnL a b -> ConnL (Down b) (Down a)
- swapL :: ConnR a b -> ConnL b a
- counit :: ConnL a b -> b -> b
- upper :: ConnL a b -> a -> a
- upper1 :: ConnL a b -> (b -> b) -> a -> a
- upper2 :: ConnL a b -> (b -> b -> b) -> a -> a -> a
- filterWith :: Preorder b => ConnL a b -> a -> b -> Bool
- ceilingWith :: ConnL a b -> a -> b
- ceilingWith1 :: ConnL a b -> (a -> a) -> b -> b
- ceilingWith2 :: ConnL a b -> (a -> a -> a) -> b -> b -> b
- type ConnR = Conn 'R
- pattern ConnR :: (b -> a) -> (a -> b) -> ConnR a b
- upR :: ConnR (Down a) (Down b) -> ConnR b a
- downR :: ConnR a b -> ConnR (Down b) (Down a)
- swapR :: ConnL a b -> ConnR b a
- unit :: ConnR a b -> b -> b
- lower :: ConnR a b -> a -> a
- lower1 :: ConnR a b -> (b -> b) -> a -> a
- lower2 :: ConnR a b -> (b -> b -> b) -> a -> a -> a
- idealWith :: Preorder b => ConnR a b -> a -> b -> Bool
- floorWith :: ConnR a b -> a -> b
- floorWith1 :: ConnR a b -> (a -> a) -> b -> b
- floorWith2 :: ConnR 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
- 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)
Conn
A data kind distinguishing the directionality of a Galois connection:
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)
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
.
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)
Connection k
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
.
midpoint :: Fractional a => ConnK a b -> a -> a Source #
Return the midpoint of the interval containing x.
>>>
pi - midpoint f64f32 pi
3.1786509424591713e-8
roundWith :: forall a b. (Num a, Preorder a) => ConnK a b -> a -> b Source #
Return the nearest value to x.
roundWith 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).
roundWith1 :: (Num a, Preorder a) => ConnK a b -> (a -> a) -> b -> b Source #
Lift a unary function over a ConnK
.
Results are rounded to the nearest value with ties away from 0.
roundWith2 :: (Num a, Preorder a) => ConnK a b -> (a -> a -> a) -> b -> b -> b Source #
Lift a binary function over a ConnK
.
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>>>
roundWith2 ratf32 f maxOdd32 2.0
2.0
truncateWith :: (Num a, Preorder a) => ConnK a b -> a -> b Source #
Truncate towards zero.
truncateWith identity = id
Connection L
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.
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.
filterWith :: 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 x
is also in b.meet
y
filterWith and idealWith commute with Down:
filterWith c a b <=> idealWith c (Down a) (Down b)
filterWith c (Down a) (Down b) <=> idealWith c a b
filterWith c a is upward-closed for all a:
a <= b1 && b1 <= b2 => a <= b2
a1 <= b && inf a2 <= b => ceiling a1 `meet` ceiling a2 <= b
ceilingWith :: ConnL a b -> a -> b Source #
ceilingWith2 :: ConnL a b -> (a -> a -> a) -> b -> b -> b Source #
Connection R
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.
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.
idealWith :: 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 x join
y is also in B.
idealWith c a is downward-closed for all a:
a >= b1 && b1 >= b2 => a >= b2
a1 >= b && a2 >= b => floor a1 `join` floor a2 >= b
floorWith2 :: ConnR a b -> (a -> a -> a) -> b -> b -> b Source #
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