RULES pragma with class constraint

Simon Peyton-Jones simonpj at microsoft.com
Mon Mar 20 07:09:41 EST 2006


Definitely not at present, and I see no easy way to implement it. 

RULES are implemented by simple matching in Core.  A call to nub will have an Eq dictionary, but Core knows nothing of instance declarations, and has no clue how to make an Ord dictionary.  But an Ord dictionary is what you want on the RHS of the rule.

The type checker, on the other hand, knows about dictionary construction, but nothing about RULES.

Simon

| -----Original Message-----
| From: glasgow-haskell-users-bounces at haskell.org [mailto:glasgow-haskell-users-
| bounces at haskell.org] On Behalf Of John Meacham
| Sent: 20 March 2006 11:49
| To: glasgow-haskell-users at haskell.org
| Subject: RULES pragma with class constraint
| 
| Is it possible to create a RULES that fires only if a type has a given
| class constraint? something like:
| 
| > snub :: Ord a => [a] -> [a]
| > snub xs = f Set.empty xs where
| >     f _ [] = []
| >     f (x:xs) set
| >         | x `Set.member` set = f xs
| >         | otherwise = x:f xs (Set.insert x set)
| >
| >
| > {-# RULES "nub/snub" Ord a => forall a xs .  nub (xs::[a]) = snub xs #-}
| 
| Such a rule would probably only be able to fire on monotypes with known
| instances since otherwise the dictionary won't necessarily be available.
| but it seems like it might be a useful feature even with that
| restriction.
| 
|         John
| 
| --
| John Meacham - ⑆repetae.net⑆john⑈
| _______________________________________________
| Glasgow-haskell-users mailing list
| Glasgow-haskell-users at haskell.org
| http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


More information about the Glasgow-haskell-users mailing list