如何在 Haskell instance-where 中使用类型变量

How to use type variable in Haskell instance-where

我尝试在类型实例的 where-body 中使用类型变量。但是 GHC 不要将类型变量用于类型实例中的函数。

我尝试为 [a].

实现类型 class Bits
instance forall a. Bits a => Bits [a] where
    xor = zipWith xor
    rotateL list dis = keeped .|. overlap
    where
        overlap = tail moved ++ [head moved] 
        (keeped, moved) = unzip $ map (\n -> let rot = rotate n dis in (rot.&.mask, rot.&.filter)) list
        mask = (complement 0) `shiftL` dis -- this line
        filter = complement mask

GHC 说:

Could not deduce (Num a) arising from the literal ‘0’

预计:

0 应为 a 类型,即 instance forall a. Bits a => Bits [a]

中定义的类型变量

"zero"在不同的上下文中有不同的写法。

你只有一个约束Bits a,那么"zero"的一种写法是zeroBits

0 是具有 Num a 实例的类型的 "zero"。