[Haskell-cafe] Function signatures and type class constraints

Oscar Finnsson oscar.finnsson at gmail.com
Mon Aug 23 16:30:03 EDT 2010


Hi,

I'm wondering why I have to repeat the class constraints at every function.

If I got the data type

> data (Eq a, Show a, Data a, Eq b, Show b, Data b, Eq c, Show c, Data c) => Foo a b c = Foo a b c

and then a function from Foo to String I have to supply the signature

> bar :: (Eq a, Show a, Data a, Eq b, Show b, Data b, Eq c, Show c, Data c) => Foo a b c -> String

even though it should be clear that a, b and c *must* fulfill the
constraints already so I should be able to just supply the signature

> bar :: Foo a b c -> String

Another related problem I got is that even though I can create the type

> type B = (Eq a, Show a, Data a, Eq b, Show b, Data b, Eq c, Show c, Data c) => Foo a b c

I cannot use it like

> bar :: B -> String

so my type class constraints got a tendency become huge!

It is possible to work around this somehow? I'm in a situation at the
moment where I got a data type with four fields each with three
constraints (Show, Eq, Data), so I have to repeat 12 constraints at
every function signature... :(

Finally is there some way to bundle type class constraints, something like

> data {Eq, Show, Data} {a, b, c} => Foo a b c = Foo a b c -- make believe syntax

so I don't have to repeat every constraint and variable all the time?

-- Oscar


More information about the Haskell-Cafe mailing list