statusfailed
statusfailed

Reputation: 900

Haskell/GHC Generics documentation example missing a typeclass constraint?

I'm following the tutorial for building a Generic "encode" function for datatypes from the hackage documentation for GHC.Generics. I've copy-pasted the code up to (and including) the section "The wrapper and generic default", but I get the following error:

Main.hs:35:14:
    Could not deduce (Encode' (Rep a)) arising from a use of ‘encode'’
    from the context (Encode a)
      bound by the class declaration for ‘Encode’
      at Main.hs:(32,1)-(35,29)
    or from (Generic a)
      bound by the type signature for encode :: Generic a => a -> [Bool]
      at Main.hs:33:13-23
    In the expression: encode' (from x)
    In an equation for ‘encode’: encode x = encode' (from x)
Failed, modules loaded: none.

The code I've copied is as follows:

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE DeriveAnyClass    #-}
module Main where

import GHC.Generics

class Encode' f where
  encode' :: f p -> [Bool]

instance Encode' V1 where
  encode' x = undefined

instance Encode' U1 where
  encode' U1 = []

instance (Encode' f, Encode' g) => Encode' (f :+: g) where
  encode' (L1 x) = False : encode' x
  encode' (R1 x) = True  : encode' x

instance (Encode' f, Encode' g) => Encode' (f :*: g) where
  encode' (x :*: y) = encode' x ++ encode' y

instance (Encode c) => Encode' (K1 i c) where
  encode' (K1 x) = encode x

instance (Encode' f) => Encode' (M1 i t f) where
  encode' (M1 x) = encode' x

class Encode a where
  encode :: a -> [Bool]
  default encode :: (Generic a) => a -> [Bool]
  encode x = encode' (from x)

I think the problem is at the final class declaration (class Encode a where ...). I've fixed it by adding an extra constraint, to get this:

class Encode a where
  encode :: a -> [Bool]                                                                                                                                                              
  default encode :: (Generic a, Encode' (Rep a)) => a -> [Bool]
  encode x = encode' (from x)

This seems to work as advertised - I can declare new datatypes and make them encodable with DeriveAnyClass and deriving. However, I'm not really sure why my fix is necessary. My question is:

Upvotes: 4

Views: 96

Answers (1)

kosmikus
kosmikus

Reputation: 19657

Yes, I think the docs are wrong. The constraint is required.

Upvotes: 4

Related Questions