summaryrefslogtreecommitdiff
path: root/compiler/utils/Serialized.hs
diff options
context:
space:
mode:
author'Jose Pedro Magalhaes <jpm@cs.uu.nl>2008-12-09 12:55:51 +0000
committer'Jose Pedro Magalhaes <jpm@cs.uu.nl>2008-12-09 12:55:51 +0000
commitfa1e4ffe87bc607e95a1e82c160ff1e821d28b6e (patch)
treea4156fdad9e05e06de2a8348554246a7a894408e /compiler/utils/Serialized.hs
parent27992bf89c008991fd6450b2b5bb224eda1f068a (diff)
downloadhaskell-fa1e4ffe87bc607e95a1e82c160ff1e821d28b6e.tar.gz
Fix #2759: add ability to serialize Rational
Diffstat (limited to 'compiler/utils/Serialized.hs')
-rw-r--r--compiler/utils/Serialized.hs21
1 files changed, 11 insertions, 10 deletions
diff --git a/compiler/utils/Serialized.hs b/compiler/utils/Serialized.hs
index 9a0e4c5d17..82cc065aa9 100644
--- a/compiler/utils/Serialized.hs
+++ b/compiler/utils/Serialized.hs
@@ -86,16 +86,16 @@ deserializeWithData' bytes = deserializeConstr bytes $ \constr_rep bytes ->
serializeConstr :: ConstrRep -> [Word8] -> [Word8]
serializeConstr (AlgConstr ix) = serializeWord8 1 . serializeInt ix
serializeConstr (IntConstr i) = serializeWord8 2 . serializeInteger i
-serializeConstr (FloatConstr d) = serializeWord8 3 . serializeDouble d
+serializeConstr (FloatConstr r) = serializeWord8 3 . serializeRational r
serializeConstr (StringConstr s) = serializeWord8 4 . serializeString s
deserializeConstr :: [Word8] -> (ConstrRep -> [Word8] -> a) -> a
deserializeConstr bytes k = deserializeWord8 bytes $ \constr_ix bytes ->
case constr_ix of
- 1 -> deserializeInt bytes $ \ix -> k (AlgConstr ix)
- 2 -> deserializeInteger bytes $ \i -> k (IntConstr i)
- 3 -> deserializeDouble bytes $ \d -> k (FloatConstr d)
- 4 -> deserializeString bytes $ \s -> k (StringConstr s)
+ 1 -> deserializeInt bytes $ \ix -> k (AlgConstr ix)
+ 2 -> deserializeInteger bytes $ \i -> k (IntConstr i)
+ 3 -> deserializeRational bytes $ \r -> k (FloatConstr r)
+ 4 -> deserializeString bytes $ \s -> k (StringConstr s)
x -> error $ "deserializeConstr: unrecognised serialized constructor type " ++ show x ++ " in context " ++ show bytes
@@ -140,11 +140,11 @@ deserializeInt :: [Word8] -> (Int -> [Word8] -> a) -> a
deserializeInt = deserializeFixedWidthNum
-serializeDouble :: Double -> [Word8] -> [Word8]
-serializeDouble = serializeString . show
+serializeRational :: (Real a) => a -> [Word8] -> [Word8]
+serializeRational = serializeString . show . toRational
-deserializeDouble :: [Word8] -> (Double -> [Word8] -> a) -> a
-deserializeDouble bytes k = deserializeString bytes (k . read)
+deserializeRational :: (Fractional a) => [Word8] -> (a -> [Word8] -> b) -> b
+deserializeRational bytes k = deserializeString bytes (k . fromRational . read)
serializeInteger :: Integer -> [Word8] -> [Word8]
@@ -171,4 +171,5 @@ deserializeList deserialize_element bytes k = deserializeInt bytes $ \len bytes
go :: Int -> [Word8] -> ([a] -> [Word8] -> b) -> b
go len bytes k
| len <= 0 = k [] bytes
- | otherwise = deserialize_element bytes (\elt bytes -> go (len - 1) bytes (k . (elt:))) \ No newline at end of file
+ | otherwise = deserialize_element bytes (\elt bytes -> go (len - 1) bytes (k . (elt:)))
+