RULES and type classes
ndmitchell at gmail.com
Fri Mar 30 12:06:03 EDT 2007
I was thinking about this, and I think pattern matching with rules and
class context pretty much _guarantees_ a change in semantics. If you
match on a class constraint, the pretty much only reason to do so is
to exploit that type class. Unfortunately, this isn't safe.
The user has called a function which explicitly annotates which
classes it requires. The user is completely allowed to write "instance
Ord a where compare = undefined", and they should have a reasonable
expectation that unless Ord a => is in the context, Ord is not
So perhaps before you tackle the issue of adding classes to rules, you
need to tackle the issue of getting rules to check what they really
On 3/30/07, Simon Peyton-Jones <simonpj at microsoft.com> wrote:
> | 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?
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
More information about the Glasgow-haskell-users