Bob*_*een 1 haskell types instance
我尝试在类型实例的where-body中使用type变量.但是GHC不对类型实例中的函数使用类型变量.
我尝试实现类型类Bits的[a].
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
Run Code Online (Sandbox Code Playgroud)
GHC说:
Could not deduce (Num a) arising from the literal ‘0’
Run Code Online (Sandbox Code Playgroud)
预期:
这0应该是类型a,它是在中定义的类型变量instance forall a. Bits a => Bits [a]
在不同的上下文中有不同的方法来写"零".
你只有一个约束Bits a,那么写一个"零"的方法就是zeroBits.
0是具有Num a实例的类型的"零" .