RULES and type classes
Donald Bruce Stewart
dons at cse.unsw.edu.au
Thu Mar 29 05:53:04 EDT 2007
haskell:
> Is there any way to use RULES substitutions with type classes?
>
> I'm writing a reactive programming arrow (same idea as Yampa, different
> design goals), and it would help performance (and not just in the speed
> sense) to be able to tell when a value derived with arr hasn't changed.
> So I'd like to be able to specialize arr to functions whose result is an
> instance of Eq.
>
> I tried
> {-# RULES "reactiveArr/Eq" reactiveArr = reactiveArrEq #-}
> but got the message
>
> Control/Arrow/Reactive/Reactive.hs:89:41:
> No instance for (Eq b)
> arising from instantiating a type signature
> at Control/Arrow/Reactive/Reactive.hs:89:41-89
> Possible fix: add (Eq b) to the tcRule
> When checking the transformation rule "reactiveArr/Eq"
>
> I tried adding various sorts of type signatures, but I couldn't find any
> way around this... is it a restriction in the RULES rewrite engine? Is
> there a workaround, or some mechanism other than RULES that I should be
> using? I could write a special "arrEq" function, but I'd like to
> minimize the number of extraneous operations outside the Arrow class.
>
> Thanks,
> Mike Hamburg
>
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.
-- Don
More information about the Glasgow-haskell-users
mailing list