Haskell/GHC 泛型文档示例缺少类型类约束?

Haskell/GHC Generics documentation example missing a typeclass constraint?

我正在按照 GHC.Genericshackage documentation 数据类型构建通用 "encode" 函数的教程进行操作。我已将代码复制粘贴到(包括)"The wrapper and generic default" 部分,但出现以下错误:

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.

我复制的代码如下:

{-# 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)

我认为问题出在最后的 class 声明 (class Encode a where ...)。我通过添加一个额外的约束来修复它,得到这个:

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

这似乎像宣传的那样工作 - 我可以声明新的数据类型并使它们可以用 DeriveAnyClassderiving 编码。但是,我不太确定为什么我的修复是必要的。我的问题是:

是的,我认为文档是错误的。约束是必需的。