小编Bob*_*een的帖子

如何在Haskell实例中使用类型变量

我尝试在类型实例的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应该是类型 …

haskell types instance

1
推荐指数
1
解决办法
88
查看次数

标签 统计

haskell ×1

instance ×1

types ×1