[Haskell-cafe] Type Constraints on Data Constructors

DavidA polyomino at f2s.com
Thu Jun 9 09:46:03 CEST 2011


Malcolm Wallace <malcolm.wallace <at> me.com> writes:

> 
> 
> > data Bar f a = Foo f => Bar {bar :: f a}
> 
> The class context on the data constructor buys you nothing extra in terms of 
expressivity in the language. 
> All it does is force you to repeat the context on every function that uses the 
datatype.  For this reason, the
> language committee has decided that the feature will be removed in the next 
revision of Haskell.
> 
> Regards,
>     Malcolm
> 

"All it does is force you to repeat the context on every function that uses the 
datatype"

I think that's exactly what the original poster is complaining about. As a real-
life example, consider
data Graph a = Ord a => G [a] [[a]]

My intention is that whenever I have a Graph a, I want to be able to use the Ord 
instance on a.

So suppose I now define
automorphisms :: (Ord a) => Graph a -> [Permutation a]

On the basis of the "don't repeat yourself" principle, it seems redundant to 
have to specify the (Ord a) context here, since I already specified it in the 
data constructor for Graph a.

So this is a proposal for a change to the language: don't require a context on a 
function if it is already implied by a context on a data type.

(This would make even more sense for newtypes, where we know there is exactly 
one data constructor, so no possibility of different data constructors requiring 
different contexts.)





More information about the Haskell-Cafe mailing list