Copyright | (c) Jost Berthold, 2010-2015, |
---|---|
License | BSD3 |
Maintainer | Jost Berthold <jost.berthold@gmail.com> |
Stability | experimental |
Portability | no (depends on GHC internals) |
Safe Haskell | None |
Language | Haskell2010 |
Serialized type for the packman library, instances and helpers
The data type
includes a phantom type Serialized
aa
to ensure
type safety within one and the same program run. Type a
can be
polymorphic (at compile time, that is) when
is not used
apart from being argument to Serialized
a
.deserialize
The Show
, Read
, and Binary
instances of Serialized a
require an
additional Typeable
context (which requires a
to be monomorphic)
in order to implement dynamic type checks when parsing and deserialising
data from external sources.
- data Serialized a = Serialized {}
- showWArray :: UArray Int TargetWord -> String
- parseP :: ReadS (Int, FP, [TargetWord])
- digit :: ReadP Char
- colon :: ReadP Char
- tabSpace :: ReadP String
- newline :: ReadP String
- hexNum :: ReadP TargetWord
- data FP = FP Word64 Word64
- matches :: Typeable a => a -> FP -> Bool
- toFP :: Fingerprint -> FP
- typeFP :: Typeable a => a -> FP
- prgHash :: FP
- type TargetWord = Word64
- hexWordFmt :: [Char]
Documentation
data Serialized a Source
The type of Serialized data. Phantom type a
ensures that we
unpack data as the expected type.
Typeable * a => Read (Serialized a) Source | Reads the format generated by the |
Typeable * a => Show (Serialized a) Source | prints packet as Word array in 4 columns (Word meaning the machine word size), and additionally includes Fingerprint hash values for executable binary and type. |
Typeable * a => Binary (Serialized a) Source | The binary format of |
The power of evaluation-orthogonal serialisation is that one can
externalise partially evaluated data (containing thunks), for
instance write it to disk or send it over a network.
Therefore, the module defines a Binary
instance for 'Serialized a',
as well as instances for Read
and Show
@ which satisfy
read . show == id :: 'Serialized' a -> 'Serialized' a
The phantom type is enough to ensure type-correctness when serialised data remain in one single program run. However, when data from previous runs are read from an external source, their type needs to be checked at runtime. Type information must be stored together with the (binary) serialisation data.
The serialised data contain pointers to static data in the generating program (top-level functions and constants) and very likely to additional library code. Therefore, the exact same binary must be used when reading in serialised data from an external source. A hash of the executable is included in the representation to ensure this.
showWArray :: UArray Int TargetWord -> String Source
Helper to show a serialized structure as a packet (Word Array)
parseP :: ReadS (Int, FP, [TargetWord]) Source
Packet Parser, reads the format generated by the Read
instance.
Could also consume other formats of the array (not implemented).
Returns: (data size in words, type fingerprint, array values)
The module uses a custom GHC fingerprint type with its two Word64 fields, to be able to read fingerprints
matches :: Typeable a => a -> FP -> Bool Source
checks whether the type of the given expression matches the given Fingerprint
toFP :: Fingerprint -> FP Source
creates an FP
from a GHC Fingerprint
To check that the program (executable) is identical when packing
and unpacking, the fingerprint type from above is used (Read/Show
instances required). An FP
fingerprint of the executable is
computed once, by unsafePerformIO inside this CAF (safe to inline,
just inefficient).
type TargetWord = Word64 Source
The target word size is the size of a machine word on the platform we run on.
This type is only used in Binary, Read and Show instances, where
packets are stored as UArrays
of TargetWord
.
Actually, GHC uses machine word size (as Haskell 2010 spec. does not fix it) so we could just use Word. See http://www.haskell.org/ghc/docs/7.8.3/html/users_guide/bugs-and-infelicities.html#haskell-98-2010-undefined
hexWordFmt :: [Char] Source