Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't the type of take be changed to use any Integral?

Tags:

haskell

take :: Int -> [a] -> [a]
genericTake :: Integral i => i -> [a] -> [a]

I have read that the inconvenient type of take is due to historical reasons, and that changing it could cause some code to break.

But can't I replace take with genericTake everywhere without breaking anything? What's the problem?

like image 338
Bravery Onions Avatar asked Jun 23 '13 16:06

Bravery Onions


1 Answers

A breaking case

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

This will break for genericTake.

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

This is a cooked up example but you can understand some type inference occuring on the first argument of take where it is assumed that it is Int, now when you change type to Integral i => i some problems might occur as above.

like image 64
Satvik Avatar answered Dec 06 '22 14:12

Satvik