Historical question about type of Data.List.group

Why isn't the type of group Eq a => [a] -> [(a,[a])] That matches more exactly what group does, and it's easy to see that functions like nubOrd = map fst . group . sort are clearly safe, whereas map head . group . sort is not. Jim

Jim Apple wrote:
Why isn't the type of group :: Eq a => [a] -> [(a,[a])]
You mean that the actual type Eq a => [a] -> [[a]] does not give the information that each element of the result is non-empty. Perhaps this word "non-empty" could be inserted in the specification:
The group function takes a list and returns a list of *non-empty* lists such that the concatenation of the result is equal to the argument. [...]
best regards, J.W.

This is also the case for Data.List.insert; it always returns a non-empty list, and so could have the signature insert :: Ord a => a -> [a] -> (a,[a]) Jim

On 2008-08-26, Jim Apple
Why isn't the type of group
Eq a => [a] -> [(a,[a])]
That matches more exactly what group does, and it's easy to see that functions like
nubOrd = map fst . group . sort
are clearly safe, whereas
map head . group . sort
is not.
I think the big thing is that while this is safer type, it's also much harder to use. Knowing I have a non-empty list is certainly useful information that can be reasoned about at the type level, but if I can't pass them easily to normal list functions, it's much less useful. Perhaps judicious use of typeclasses would make this easier, but I don't think those were in the original either. In any case the design space for a standard prelude that had the normal list type and a non-empty list type united in a sequence class is rather large, particularly if it's an open class that new sequence-like data types are expected to be an instance of. -- Aaron Denney -><-

The current type is easier to use in general.
And it was also what I needed back in the days when I added the
function to the LML libraries.
-- Lennart
On Tue, Aug 26, 2008 at 8:45 PM, Jim Apple
Why isn't the type of group
Eq a => [a] -> [(a,[a])]
That matches more exactly what group does, and it's easy to see that functions like
nubOrd = map fst . group . sort
are clearly safe, whereas
map head . group . sort
is not.
Jim _______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
participants (4)
-
Aaron Denney
-
Jim Apple
-
Johannes Waldmann
-
Lennart Augustsson