Implement a Haskell type class Serialize with a default implementation using…
- User
Implement a Haskell type class
Serializewith a default implementation usingGHC.Genericsto automatically derive binary serialization for any record. Assistant
The user wants a Haskell type class
Serializethat provides binary serialization. The key constraint is that it must have a default implementation usingGHC.Genericsso that it can be automatically derived for any record (or data type).1. Understanding the Goal
- Type Class:
Serialize a. Needs at least two methods:put :: a -> ByteString(or similar) andget :: Get a(or similar). - Mechanism: Default signatures using
GHC.Generics. This allows writinginstance Serialize MyTypewithout writing the body. - Backend: Binary serialization usually implies
Data.BinaryorData.ByteString. For simplicity and demonstration, I'll stick to a custom lightweight implementation or useData.Binary's primitives but defined within a custom class to show how the generic deriving works from scratch. Or better, just useByteStringbuilder and parser (likecerealorbinarystyle). - Let's keep the scope contained. I will create a simple
Putmonad andGetstate monad, or just use the standardBinaryclass logic but reimplemented to show the Generic machinery. - Actually, usually when people ask for "Implement a type class... using GHC.Generics", they want to see the
Genericinstance selection logic (M1,K1,:*:,:+:).
2. Core Components
- The Class:
class Serialize a where put :: a -> Put get :: Get a default put :: (Generic a, GSerialize (Rep a)) => a -> Put default get :: (Generic a, GSerialize (Rep a)) => Get a - The Generic Class (
GSerialize): This operates on theRep atypes (the generic representation). - Serialization Format:
- Primitives:
Int,Char, etc. need manual instances. - Products (Records/Args): Serialize fields sequentially.
- Sums (Constructors): Need tags (word8) to distinguish which constructor it is.
- Primitives:
3. The Generic Instances
- Datatype (
D1/MetaData): Usually just a wrapper, metadata ignored for pure binary serialization.- Instance for
M1 D t f.
- Instance for
- Constructor (
C1/MetaCons):- If it's a Sum type, we need to encode the constructor tag here.
- However, handling sums generically is often done at the
:+:level. - Let's look at how standard libraries do it.
- Approach A: Sum encoding at the
:+:level. - Approach B: Metadata at
C1level. - Let's go with the standard structure:
M1 D: Delegate to inner.M1 C: Delegate to inner (if encoding is positional) or handle tagging if we want to get fancy. Actually, standard generic binary usually handles the sum logic at:+:and the product logic at:*:.M1 S: Selector metadata (field names), usually ignored for binary. Delegate to inner.
- Sum (
:+:):put (L1 l) = putWord8 0 >> gput lput (R1 r) = putWord8 1 >> gput rget: read Word8, if 0L1 <$> gget, elseR1 <$> gget.
- Product (
:*:):put (l :*: r) = gput l >> gput rget = (:*:) <$> gget <*> gget
- Atomic Value (
K1):put (K1 x) = put x(recurses back to the mainSerializeclass).get = K1 <$> get
- Unit (
U1):put U1 = pure ()get = pure U1
4. Implementation Details (The "M" in M1)
M1 i c f pwhereiis the type of metadata (D, C, S).GSerializeshould be a class over* -> *(kind ofRep).
5. Setting up the environment
To make the code runnable, I need:
Data.ByteString(Strict or Lazy? Lazy is standard forBinary, strict forCereal. I'll use LazyByteStringandBuilderfor efficiency, or just strictByteStringandPut/Getcombinators for simplicity).- Let's write a very simple
Put(Writer) andGet(State) to avoid external dependencies besidesGHC.Generics. This makes the solution self-contained and educational.
Drafting the code:
-
Imports:
GHC.Generics,Data.Word,Data.ByteString.Lazy(Builder),Data.Binary(Wait, if I useData.Binary, I don't need to implementSerialize. The prompt asks to implementSerialize. So I should write the machinery). -
Helper classes:
-- Define the generic helper class class GSerialize f where gput :: f a -> Put gget :: Get (f a) -
Instances:
M1(Meta): Wraps specific metadata types.:+:(Sum): Handle 0/1 tagging.:*:(Product): Sequence.K1(Field): Recursive call to user'sSerialize.
-
Sum Handling Edge Case:
- If I have 3 constructors?
A | B | C. A=L1 ...B=R1 (L1 ...)C=R1 (R1 ...)- Tags become
0,1,1? No. - Wait, nested sums
(:+:)handle this recursively. L1puts 0.R1puts 1. If theR1contains another sum, that sum puts its own tag.- Actually, standard generic binary puts the tag before traversing the sum structure or uses a specific scheme.
- Common approach: Calculate the constructor index. This is hard without passing state.
- Alternative approach used in
generic-binary: The instance for(:+:)writes0for Left,1for Right. Nested sums result in multiple bytes.L1 x->0.R1 (L1 y)->1then0. - Is this efficient? Not space-wise for tag, but correct.
- Better approach:
GSumSerialize. - Let's stick to the simple "1 bit per branch" approach for the
(:+:)instance for simplicity, or just a Word8 prefix for every branch. The Word8 prefix is easier to read.
Refined Sum Strategy:
put (L1 x) = putWord8 0 >> gput xput (R1 x) = putWord8 1 >> gput xIfR1is another sum, it puts another0or1. Example:A | B | C.A(L1) -> 0B(R1 L1) -> 1, 0C(R1 R1) -> 1, 1 This is perfectly valid binary serialization. - If I have 3 constructors?
-
The
SerializeClass:class Serialize a where put :: a -> PutM () -- Using a Writer monad concept get :: Get a default put :: (Generic a, GSerialize (Rep a)) => a -> PutM () default get :: (Generic a, GSerialize (Rep a)) => Get a -
Putting it together: I will provide a complete, compile-able example. I need a
PutandGetimplementation. I'll useData.Binary.PutandData.Binary.Getfrom thebinarypackage as the underlying mechanism because writing a raw parser/printer is boilerplate noise. The prompt asks for the Class implementation, not the Byte parser implementation.Self-correction: Using
binarypackage types makes it standard. If the user wants no dependencies, I'd have to writePut/Get. Given "Haskell type class", usually usingbinaryorcerealis expected. I will assume I can useData.Binaryfor the primitives (Word8,Builder) but I will implement theSerializeclass myself.
6. Code Structure
- Extensions:
DeriveGeneric,DefaultSignatures. - Imports:
GHC.Generics,Data.Binary.Put,Data.Binary.Get,Data.Word,Data.ByteString.Lazy. - Class
Serialize. - Class
GSerialize. - Instances for
M1,:+:,:*:,K1,U1. - Instances for primitives (
Int,Stringetc) to make the example work. - Example usage.
7. Refining the
GSerializeinstances- Metadata (
M1):instance GSerialize f => GSerialize (M1 i c f) where gput (M1 x) = gput x gget = M1 <$> gget - Product (
:*:):instance (GSerialize f, GSerialize g) => GSerialize (f :*: g) where gput (x :*: y) = gput x >> gput y gget = (:*:) <$> gget <*> gget - Sum (
:+:):instance (GSerialize f, GSerialize g) => GSerialize (f :+: g) where gput (L1 x) = putWord8 0 >> gput x gput (R1 x) = putWord8 1 >> gput x gget = do tag <- getWord8 case tag of 0 -> L1 <$> gget 1 -> R1 <$> gget _ -> fail "Invalid sum tag" - Field (
K1):instance Serialize a => GSerialize (K1 i a) where gput (K1 x) = put x -- Recursion back to main class gget = K1 <$> get - Unit (
U1):instance GSerialize U1 where gput U1 = pure () gget = pure U1
8. Primitives
Need instances for
SerializeforInt,String,Boolto test.Int: useputWord64le(generic size) or standardBinaryencoding. Let's use standardBinaryencoding logic viaencode/decodeor just implement manually forInt. Actually, to keep it self-contained, I'll define:put = putWord64le . fromIntegralget = fromIntegral <$> getWord64leForString, map over chars. ForBool, 0/1.9. Final Check
- Does it handle records? Yes,
:*:handles fields. - Does it handle multiple constructors? Yes,
:+:handles sums. - Does it handle metadata? Yes,
M1passes through.
One detail:
DefaultSignaturesrequires the method type to match the default type signature.put :: a -> Putdefault put :: (Generic a, GSerialize (Rep a)) => a -> PutThe code looks solid. I will present the solution using
Data.ByteString.Lazyand a simplifiedPut/Getpair usingbinarypackage primitives to ensure it's robust, but defining theSerializeclass from scratch as requested.Wait, I should check if I need to import
binaryor justbytestring. If I only importbytestring, I have to writePutasBuilderandGetas a parser.binaryis a very common dependency. I will assumebinaryis available for the primitives (Put,Get,Word8), as implementing a bit-precise parser from scratch is likely outside the scope of "implement a type class using Generics".However, if I want to be dependency-light (only
baseandbytestring), I can do:type Put = PutM ()usingBuilder.newtype Get a = Get ...This might be too much boilerplate. I'll stick to usingData.Binary.GetandData.Binary.Puttypes for the implementation of the class methods, as they are standard types for binary serialization in Haskell.Plan:
- Define
Serializeclass withDefaultSignatures. - Define
GSerializeclass forReptypes. - Implement instances for Generic types (
M1,:+:,:*:,K1,U1). - Implement instances for basic types (
Int,Bool) to allow derivation. - Provide a usage example with a
deriving instance Generic MyType.
One nuance:
Repkind is* -> *. TheGSerializeclass:class GSerialize f where gput :: f p -> Put gget :: Get (f p)The
Serializeclass:class Serialize a where put :: a -> Put get :: Get a default put :: (Generic a, GSerialize (Rep a)) => a -> Put put = gput . from default get :: (Generic a, GSerialize (Rep a)) => Get a get = to <$> ggetThis looks correct.
One final thought: Selector names (
M1 S). The instance forM1coversD,C, andSuniversally if we just delegate. This is the standard "structural" serialization.Let's write the code.
{-# LANGUAGE DefaultSignatures #-}{-# LANGUAGE DeriveGeneric #-}{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE TypeOperators #-}
I will create a complete file content.
Constructing the response:
- Explain the concept:
GHC.Genericsconverts data types into a generic representation (Rep) composed of sum (:+:), product (:*:), and metadata (M1). We write class instances for these combinators. - Provide the code.
- Use
Data.Binary.PutandData.Binary.Getfor the underlying binary primitives (standard approach). - Define
Serialize. - Define
GSerialize. - Write instances.
- Write instances for primitives.
- Show Example.
- Use
Is there any catch with
K1?K1holds the actual value.instance Serialize a => GSerialize (K1 i a)This creates the recursion bridge.putcallsgput,gputcallsput.Example types:
data Point = Point Int Int deriving Genericdata Color = Red | Green | Blue deriving GenericI will include a helper to run the serialization
runSerialize :: Serialize a => a -> ByteString.Let's double check the Sum encoding.
L1-> 0.R1-> 1. If I havedata T = A | B | C.A=L1 ...B | C=R1 (L1 ...) | R1 (R1 ...)Aencodes as0.Bencodes as1+0.Cencodes as1+1. This is correct prefix encoding.Wait, if I wrap a sum type in a record?
data Wrapper = Wrapper { inner :: Either Int String }Wrapperis a product:*:(one arg).innerisK1.K1callsSerializeforEither.Eitheris a sum. IfEitherderivesGeneric, and we rely onSerializedefault... wait.Eitheris defined inbase. Does it haveGeneric? Yes. Can we define an orphan instanceinstance (Serialize a, Serialize b) => Serialize (Either a b)? Or do we rely onGeneric? If we deriveSerializeforEither, the compiler generatesinstance Serialize (Either a b). ButEitheris inData.Either. We can't add aderivingclause to it in user code. However, the user prompt says "automatically derive... for any record". It implies user-defined types. ForEither, we might need to define the instance manually or via standalone deriving (if allowed). For the example, I'll stick to user-defined types to avoid orphan instance complexity or assumingSerializeinstances for Prelude types. I will provide instances forIntandBoolmanually in the code.One small issue:
Genericinstance forEitherexists, but we needSerializeinstance forEitherto use it inside a record that is being serialized. SinceEitheris not a "record" in the strict sense (it's a sum), but the user said "any record", I will focus on user-defined types. I will addinstance Serialize a => GSerialize (K1 i a)which handles the recursion. If I wantEitherto work inside the record, I should probably defineinstance Serialize a => Serialize (Maybe a)or similar, or just stick to simple fields in the demo.Okay, ready to generate. Here is a complete implementation of a
Serializetype class usingGHC.Generics.This implementation defines a
Serializeclass capable of converting Haskell data types to and from lazyByteStrings. It handles product types (structs/records) and sum types (enums/constructors) automatically.The Code
{-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE TypeOperators #-} module GenericSerialize where import Data.ByteString.Lazy (ByteString) import qualified Data.ByteString.Lazy as BS import Data.Word (Word8) import GHC.Generics (Generic (..), (:*:) (..), (:+:) (..), K1 (..), M1 (..), Rep, U1 (..)) import Data.Binary.Get (Get, getWord8) import Data.Binary.Put (Put, putWord8, runPut, runGet) -- | The main type class. class Serialize a where -- | Encode a value to a binary ByteString put :: a -> Put -- | Decode a value from a binary ByteString get :: Get a -- Default implementation using GHC Generics default put :: (Generic a, GSerialize (Rep a)) => a -> Put put = gput . from default get :: (Generic a, GSerialize (Rep a)) => Get a get = to <$> gget -- | The internal class for handling the generic representation of the type. -- We operate on types of kind `* -> *` (like M1, K1, etc.). class GSerialize f where gput :: f p -> Put gget :: Get (f p) -- ========================================== -- Instances for Generic Types -- ========================================== -- 1. Metadata (M1) -- M1 wraps data constructors, record selectors, and type definitions. -- We simply pass through to the underlying structure. instance GSerialize f => GSerialize (M1 i c f) where gput (M1 x) = gput x gget = M1 <$> gget -- 2. Products (Rec0 / K1) -- K1 represents actual data fields (constants). -- We delegate back to the original `Serialize` class for the field type. instance Serialize a => GSerialize (K1 i a) where gput (K1 x) = put x gget = K1 <$> get -- 3. Multiparameter Constructors (:*:) -- Represents a sequence of fields (e.g., data T = T Int Bool). -- We serialize the left side, then the right side. instance (GSerialize f, GSerialize g) => GSerialize (f :*: g) where gput (x :*: y) = gput x >> gput y gget = (:*:) <$> gget <*> gget -- 4. Sum Types (:+:) -- Represents a choice between constructors (e.g., data T = A | B). -- We write a tag (0 for Left, 1 for Right) to distinguish them. -- Note: This naive implementation uses 1 byte per level of nesting. instance (GSerialize f, GSerialize g) => GSerialize (f :+: g) where gput (L1 x) = putWord8 0 >> gput x gput (R1 x) = putWord8 1 >> gput x gget = do tag <- getWord8 case tag of 0 -> L1 <$> gget 1 -> R1 <$> gget _ -> fail "Invalid sum type tag" -- 5. Unit Types (U1) -- Represents constructors with no arguments. instance GSerialize U1 where gput U1 = pure () gget = pure U1 -- ========================================== -- Instances for Primitive Types -- ========================================== -- To make the generic deriving work, we need instances for leaf types. instance Serialize Int where put = putWord8 . fromIntegral -- Simplified: truncates to Word8 for demo get = fromIntegral <$> getWord8 instance Serialize String where put str = do put (length str :: Int) -- Write length mapM_ (put . fromEnum) str -- Write chars as ints get = do len <- get :: Get Int mapM_ (getChar) (replicate len ()) where getChar = toEnum <$> getWord8 -- ========================================== -- Helper functions to run the serialization -- ========================================== serialize :: Serialize a => a -> ByteString serialize = runPut . put deserialize :: Serialize a => ByteString -> Either String a deserialize bs = runGet get bsExample Usage
You can now derive
Serializefor any record or data type automatically usingGeneric.data User = User { userName :: String , userAge :: Int } deriving (Show, Generic) -- No manual instance needed! The default signature handles it. instance Serialize User data Status = Active | Inactive deriving (Show, Generic) instance Serialize Status main :: IO () main = do let user = User "Alice" 30 print "Original:" print user -- Serialize to ByteString let bytes = serialize user print $ "Encoded bytes: " ++ show (BS.unpack bytes) -- Deserialize back case deserialize bytes of Right decoded -> print $ "Decoded: " ++ show (decoded :: User) Left err -> print $ "Error: " ++ errHow it works
-
GHC.GenericsConversion: When you deriveGeneric, Haskell converts your data type into a system of generic types representing the structure:M1: Metadata (module name, constructor name, field names). We usually ignore this for binary formats and just serialize the payload.K1: Actual values (fields likeInt,String).:*:: Product types (combining fields, e.g.,Int :*: String).:+:: Sum types (choice between constructors, e.g.,Left | Right).U1: Constructors with no fields.
-
Type Class Induction:
- The instance for
K1callsputrecursively on the field type. This allows nesting (e.g., a Record inside another Record). - The instance for
:*:serializes fields sequentially. - The instance for
:+:writes a "tag" byte (0 or 1) to indicate which constructor was used, allowing the deserializer to know which path to take.
- The instance for
-
Default Signatures: The
default putin theSerializeclass converts the valueainto its generic representationRep ausingfrom, and then hands it off togput.
- Type Class: