RE: RULES pragma with class constraint

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@haskell.org [mailto:glasgow-haskell-users- | bounces@haskell.org] On Behalf Of John Meacham | Sent: 20 March 2006 11:49 | To: glasgow-haskell-users@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@haskell.org | http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

On Mon, Mar 20, 2006 at 12:09:41PM -0000, Simon Peyton-Jones wrote:
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.
I suspected this was the case. Perhaps it could be solved with something like rule schemes, where a RULES pragma defines a template that is instantiated as new types that meet certain criteria are introduced. so a RULE with a class constraint would actually not be a single rule, but be copied and instantiated every time a new instance is declared at the new type. Something like this might also be useful for writing rules that apply to all built in C numeric types for instance.. not sure how much demand there is for something like this though. I have a sort of hack in jhc to write rules that apply to any type that maps directly to a C integral type for basic numeric rules. but it isn't user extensible in any way. (other than by adding new primitive numeric types that is) John -- John Meacham - ⑆repetae.net⑆john⑈
participants (2)
-
John Meacham
-
Simon Peyton-Jones