I have a problem as follows - type CF = Char -> Float type IF = Integer -> Float type CIF = (Char,Integer) -> Float There be two functions: fun :: CF fun = fromInt.ord sun :: IF sun = fromInteger I want the followings to be valid. x :: IF x = sun \+ sun y :: IF y = sun \+ sun \+ sun -- when only sun is there, type should be IF a :: CF a = fun \+ fun b :: CF b = fun \+ fun \+ fun -- when only fun is there, type should be IF p :: CIF p = sun \+ fun q :: CIF q = sun \+ fun \+ sun - -when sun and fun are mixed type should be CIF I guess this can be done using classes, but I have not been successful after trying many variations. Any help is very much appreciated. Thanks, Saswat
On Tue, 6 Mar 2001, Saswat Anand wrote:
I guess this can be done using classes, but I have not been successful after trying many variations.
It requires extensions supported by ghc and Hugs: multiparameter type classes are required, functional dependencies are recommended. Functional dependencies don't work well for released ghc versions, but shoud work in the upcoming ghc-5.0. You might need to remove '| a b -> c' below and need more explicit type annotations (will not be very convenient). class Plus a b c | a b -> c where (\+) :: a -> b -> c instance Plus CF CF CF where f \+ g = ... instance Plus IF IF IF where f \+ g = ... instance Plus CF IF CIF where f \+ g = ... instance Plus IF CF CIF where f \+ g = ... instance Plus CF CIF CIF where f \+ g = ... instance Plus IF CIF CIF where f \+ g = ... instance Plus CIF CF CIF where f \+ g = ... instance Plus CIF IF CIF where f \+ g = ... instance Plus CIF CIF CIF where f \+ g = ... -- Marcin 'Qrczak' Kowalczyk
participants (2)
-
Marcin 'Qrczak' Kowalczyk -
Saswat Anand