-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Bijective Read/Show instances for patterns
adds helper module Network.Socket.ReadShow for defining bijections between types, to be used for simultaneous read-show equivalence definitions implements read/show instances for SocketOption, SockType, and Family according to this paradigm additionally removes a bug in Network.Socket.Options where the OOBInline pattern name was unintentionally allcaps-ed in one of the CPP ifdef branches
- Loading branch information
1 parent
db2e47b
commit dcc2cb1
Showing
4 changed files
with
225 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{-# LANGUAGE RecordWildCards #-} | ||
|
||
module Network.Socket.ReadShow where | ||
|
||
-- type alias for individual correspondences of a (possibly partial) bijection | ||
type Pair a b = (a, b) | ||
|
||
-- | helper function for equality on first tuple element | ||
{-# INLINE eqFst #-} | ||
eqFst :: Eq a => a -> (a, b) -> Bool | ||
eqFst x = \(x',_) -> x' == x | ||
|
||
-- | helper function for equality on snd tuple element | ||
{-# INLINE eqSnd #-} | ||
eqSnd :: Eq b => b -> (a, b) -> Bool | ||
eqSnd y = \(_,y') -> y' == y | ||
|
||
-- | Return RHS element that is paired with provided LHS, | ||
-- or apply a default fallback function if the list is partial | ||
lookForward :: Eq a => (a -> b) -> [Pair a b] -> a -> b | ||
lookForward defFwd ps x | ||
= case filter (eqFst x) ps of | ||
(_,y):_ -> y | ||
[] -> defFwd x | ||
|
||
-- | Return LHS element that is paired with provided RHS, | ||
-- or apply a default fallback function if the list is partial | ||
lookBackward :: Eq b => (b -> a) -> [Pair a b] -> b -> a | ||
lookBackward defBwd ps y | ||
= case filter (eqSnd y) ps of | ||
(x,_):_ -> x | ||
[] -> defBwd y | ||
|
||
data Bijection a b | ||
= Bijection | ||
{ defFwd :: a -> b | ||
, defBwd :: b -> a | ||
, pairs :: [Pair a b] | ||
} | ||
|
||
forward :: (Eq a) => Bijection a b -> a -> b | ||
forward Bijection{..} = lookForward defFwd pairs | ||
|
||
backward :: (Eq b) => Bijection a b -> b -> a | ||
backward Bijection{..} = lookBackward defBwd pairs |
Oops, something went wrong.