4

ウィキの記事に従って、GHC.Genericsの使用方法の最小限の実例を作成しようとしています。ここに私が持っているものがあります:

{-# LANGUAGE DefaultSignatures, DeriveGeneric, TypeOperators, FlexibleContexts #-}

import GHC.Generics

data Bit = O | I deriving Show

class Serialize a where
  put :: a -> [Bit]

  default put :: (Generic a, GSerialize (Rep a)) => a -> [Bit]
  put a = gput (from a)

class GSerialize f where
  gput :: f a -> [Bit]

instance GSerialize U1 where
  gput U1 = []

instance (GSerialize a, GSerialize b) => GSerialize (a :*: b) where
  gput (a :*: b) = gput a ++ gput b

instance (GSerialize a, GSerialize b) => GSerialize (a :+: b) where
  gput (L1 x) = O : gput x
  gput (R1 x) = I : gput x

instance (GSerialize a) => GSerialize (M1 i c a) where
  gput (M1 x) = gput x

instance (Serialize a) => GSerialize (K1 i a) where
  gput (K1 x) = put x


--
-- Try it out...
--

data UserTree a = Node a (UserTree a) (UserTree a) | Leaf
  deriving Generic

instance (Serialize a) => Serialize (UserTree a)

instance Serialize Int


main = do
  print . put $ (Leaf :: UserTree Int)
  print . put $ (Node 7 Leaf Leaf :: UserTree Int)
  print . put $ (3 :: Int)

ただし、これを実行しようとすると、プログラムがハングします。

λ> main
[I]
[O     -- the program hangs here

私は何を間違っていますか?

4

1 に答える 1