-
Notifications
You must be signed in to change notification settings - Fork 206
WIP Bijective Read/Show instances for patterns #465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c0322a1
WIP Bijective Read/Show instances for patterns
archaephyrryx a41d302
WIP readshow for CmsgId and ghc-cpp guards
archaephyrryx f77142f
FIX: short-circuit for unsupported patterns
archaephyrryx 831f1aa
FIX: UnsupportedSocketOption pattern short-circuit
archaephyrryx 68c9b65
FIX missing export in Network.Socket.Options
archaephyrryx 57c0fa7
FIX added Win32.Cmsg RecordWildCards pragma
archaephyrryx 827bb27
FIX show Unsupported* as full pattern name
archaephyrryx 1b374cf
Unified ReadShow usage patterns
archaephyrryx b60eec8
FIX bijective read/show for negative CInt values
archaephyrryx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,95 @@ | ||
| {-# LANGUAGE RecordWildCards #-} | ||
| {-# LANGUAGE PatternGuards #-} | ||
|
|
||
|
|
||
| module Network.Socket.ReadShow where | ||
|
|
||
| import qualified Text.Read as P | ||
|
|
||
| -- 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] | ||
| } | ||
|
|
||
| -- | apply a bijection over an LHS-value | ||
| forward :: (Eq a) => Bijection a b -> a -> b | ||
| forward Bijection{..} = lookForward defFwd pairs | ||
|
|
||
| -- | apply a bijection over an RHS-value | ||
| backward :: (Eq b) => Bijection a b -> b -> a | ||
| backward Bijection{..} = lookBackward defBwd pairs | ||
|
|
||
| -- | show function for Int-like types that encodes negative numbers | ||
| -- with leading '_' instead of '-' | ||
| _showInt :: (Show a, Num a, Ord a) => a -> String | ||
| _showInt n | n < 0 = let ('-':s) = show n in '_':s | ||
| | otherwise = show n | ||
|
|
||
| -- | parse function for Int-like types that interprets leading '_' | ||
| -- as if it were '-' instead | ||
| _readInt :: (Read a) => String -> a | ||
| _readInt ('_':s) = read $ '-':s | ||
| _readInt s = read s | ||
|
|
||
|
|
||
| -- | parse a quote-separated pair into a tuple of Int-like values | ||
| -- should not be used if either type might have | ||
| -- literal quote-characters in the Read pre-image | ||
| _parse :: (Read a, Read b) => String -> (a, b) | ||
| _parse xy = | ||
| let (xs, '\'':ys) = break (=='\'') xy | ||
| in (_readInt xs, _readInt ys) | ||
| {-# INLINE _parse #-} | ||
|
|
||
| -- | inverse function to _parse | ||
| -- show a tuple of Int-like values as quote-separated strings | ||
| _show :: (Show a, Num a, Ord a, Show b, Num b, Ord b) => (a, b) -> String | ||
| _show (x, y) = _showInt x ++ "'" ++ _showInt y | ||
| {-# INLINE _show #-} | ||
|
|
||
| defShow :: Eq a => String -> (a -> b) -> (b -> String) -> (a -> String) | ||
| defShow name unwrap sho = \x -> name ++ (sho . unwrap $ x) | ||
| {-# INLINE defShow #-} | ||
|
|
||
| defRead :: Read a => String -> (b -> a) -> (String -> b) -> (String -> a) | ||
| defRead name wrap red = \s -> | ||
| case splitAt (length name) s of | ||
| (x, sn) | x == name -> wrap $ red sn | ||
| _ -> error $ "defRead: unable to parse " ++ show s | ||
| {-# INLINE defRead #-} | ||
|
|
||
| -- | Apply a precedence-invariant one-token parse function within ReadPrec monad | ||
| tokenize :: (String -> a) -> P.ReadPrec a | ||
| tokenize f = P.lexP >>= \(P.Ident x) -> return $ f x | ||
| {-# INLINE tokenize #-} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.