[Haskell-cafe] Type Constraints on Data Constructors
Erik Hesselink
hesselink at gmail.com
Thu Jun 9 15:03:07 CEST 2011
On Thu, Jun 9, 2011 at 09:46, DavidA <polyomino at f2s.com> wrote:
> 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.
You can do this using GADTs. Like this:
data Graph a where
G :: Ord a => [a] -> [[a]] -> Graph a
Now functions that pattern match on the 'G' constructor automatically
have the Ord instance in scope, so it is no longer needed in the
signature.
Erik
More information about the Haskell-Cafe
mailing list