RULES and type classes

Simon Peyton-Jones simonpj at
Fri Mar 30 09:07:17 EDT 2007

| In particular, it would be nice to be able to specialise based on the
| instances, as we do for [a] --> [Int], e.g.
|     RULES sum = sumInt :: [Int] -> Int
| is fine in the current system. So I could imagine some nice
| specialisations based on say, the good old Ord:
|     RULES nub = nubOrd :: (Eq a, Ord a) => [a] -> [a]
| which might use a Map, say.
| I don't know how costly this instance matching would be.

At the moment, RULES are a pure term rewriting system.  That is,

        when the simplifer sees an instance of the LHS
        it rewrites it to the RHS

Every variable used on the RHS of the rule is either a constant, or is bound on the LHS.

What you want is different.  Remember nub :: Eq a => [a] -> [a]

You want to the rule

        forall a:*, d1:Eq a,
                nub a d1  --->   nubOrd a d1 d2

where d2:Ord a.  But where did d2 come from?  Out of thin air!

The simplifier does not do this.  It knows nothing of type classes and instances; it just does term rewrites.

Now it would be quite reasonable to have something more sophisticated that *does* know about how to construct instance dictionaries "out of thin air", but that goes significantly beyond what the current RULES system does.

Would someone care to add this info to the Wiki?


More information about the Glasgow-haskell-users mailing list