deducing type of multi-parameter type class

Hi, I'm trying to get the following code to compile: ======= Main.hs ========== import IO data MyInt = MyInt Int data MyString = MyString String deriving Show class Show b => MyClass a b where fn :: a -> b instance MyClass MyInt MyString where fn (MyInt i) = MyString (show i) myprint :: (MyClass a b) => a -> IO () myprint a = putStrLn $ show (fn a) main = myprint 3 ======= Main.hs ========== with |ghc Main.hs -XMultiParamTypeClasses|. However, the compiler cannot deduce the type of the |b| type variable (which in this case is |MyString|). How can I explicitly tell this information to the compiler? Many thanks, Alex.

I think what you're looking for is functional dependencies. Basically, the
way you have the typeclass set up right now, "fn $ MyInt 5" could in theory
turn into anything; if you wanted, you could declare an "instance MyClass
MyInt Double" or anything else. You would like the compiler to be able to
automatically determine the output type. Using functional dependencies:
class Show b => MyClass a b | a -> b where
You could also use type families for this, but I believe you cannot express
the "Show" superclass:
class MyClass a where
type MyResult a
fn :: a -> MyResult a
instance MyClass MyInt where
type MyResult MyInt = MyString
fn (MyInt i) = MyString (show i)
Michael
On Thu, Jul 15, 2010 at 8:59 AM, Alex
Hi,
I'm trying to get the following code to compile:
======= Main.hs ==========
import IO
data MyInt = MyInt Int data MyString = MyString String deriving Show
class Show b => MyClass a b where fn :: a -> b
instance MyClass MyInt MyString where fn (MyInt i) = MyString (show i)
myprint :: (MyClass a b) => a -> IO () myprint a = putStrLn $ show (fn a)
main = myprint 3
======= Main.hs ==========
with ghc Main.hs -XMultiParamTypeClasses. However, the compiler cannot deduce the type of the b type variable (which in this case is MyString). How can I explicitly tell this information to the compiler?
Many thanks, Alex.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Thu, Jul 15, 2010 at 3:13 AM, Michael Snoyman
You could also use type families for this, but I believe you cannot express the "Show" superclass: class MyClass a where type MyResult a fn :: a -> MyResult a
I guess this works: class Show (MyResult a) => MyClass a where type MyResult a fn :: a -> MyResult a HTH, -- Felipe.

On Thu, Jul 15, 2010 at 10:09 AM, Felipe Lessa
On Thu, Jul 15, 2010 at 3:13 AM, Michael Snoyman
wrote: You could also use type families for this, but I believe you cannot express the "Show" superclass: class MyClass a where type MyResult a fn :: a -> MyResult a
I guess this works:
class Show (MyResult a) => MyClass a where type MyResult a fn :: a -> MyResult a
Thanks, I forgot about that. Funny, because I use it in Yesod ;). Michael
participants (3)
-
Alex
-
Felipe Lessa
-
Michael Snoyman