effa82d5801facc0acdbcfcd402606fa8615a8ea
[clean-tests.git] / datatype / Serialise.hs
1 {-# LANGUAGE DefaultSignatures, FlexibleContexts, TypeOperators #-}
2 module Serialise where
3
4 import Data.Char
5
6 import GHC.Generics
7
8 class Serialise a where
9 serialise :: a -> Int
10
11 --class serialise a where
12 -- serialise :: a -> int -> [int] -> [int]
13 -- default serialise :: (generic a, gserialise (rep a)) => a -> int -> [int] -> [int]
14 -- serialise = gserialise . from
15 --
16 --class GSerialise f where
17 -- gserialise :: f a -> Int -> [Int] -> [Int]
18 --
19 ----Void
20 --instance GSerialise V1 where
21 -- gserialise _ _ = id
22 ----Unit
23 --instance GSerialise U1 where
24 -- gserialise _ _ = id
25 ----Pair
26 --instance (GSerialise a, GSerialise b) => GSerialise (a :*: b) where
27 -- gserialise (l :*: r) _ = gserialise l 0 . gserialise r 0
28 ----Constants, additional parameters and recursion of kind *
29 --instance Serialise a => GSerialise (K1 i a) where
30 -- gserialise (K1 a) i = serialise a i
31 ----Either not supported because we don't support sumtypes in our stack machine
32 --instance (GSerialise a, GSerialise b) => GSerialise (a :+: b) where
33 -- gserialise (L1 l) c = gserialise l (c * 2)
34 -- gserialise (R1 r) c = gserialise r (c + 1)
35 ----Datatype, Constructor or Selector
36 --instance (GSerialise a) => GSerialise (M1 i c a) where
37 -- gserialise (M1 l) c = (c:) . gserialise l 0
38
39 instance Serialise Int where
40 serialise i = i
41 instance Serialise Bool where
42 serialise b = if b then 1 else 0
43 instance Serialise Char where
44 serialise c = ord c