为什么不能改变使用任何积分的类型?

Bra*_*ons 9 haskell

take :: Int -> [a] -> [a]
genericTake :: Integral i => i -> [a] -> [a]
Run Code Online (Sandbox Code Playgroud)

我已经读过,不方便的类型take是由于历史原因,并且更改它可能会导致某些代码中断.

但是我不能在没有破坏任何东西的情况下替换takegenericTake有什么问题?

Sat*_*vik 10

破案

genericTake :: Integral i => i -> [a] -> [a]
genericTake n xs = take (fromIntegral n) xs

class Foo a where
   bar :: a -> String

instance Foo Int where
   bar _ = "int" 

foo :: String -> [a] -> [a]
foo ns xs = let y = read ns
                z = bar y
            in take y xs
Run Code Online (Sandbox Code Playgroud)

这将打破genericTake.

No instance for (Foo i0) arising from a use of `bar'
    The type variable `i0' is ambiguous
Run Code Online (Sandbox Code Playgroud)

这是一个熟练的例子,但你可以理解在第一个参数中出现的一些类型推断,假定它是Int,现在当你改变类型时,Integral i => i某些问题可能会如上所述发生.