
On 11/2/06, Sebastian Sylvan
On 11/2/06, Slavomir Kaslev
wrote: Visible 'subclasses' Show. Doesn't this mean that Visible should be defined for all types with Show instances?
No, it just means that any type that you want to instantiate in Visible needs to also have a Show instance (is this a restriction you really want? See below).
Well, yes that's what I meant. I want class Visible for objects that have Show instance and I want to be able to use this Show instance in the definition of Visible. Again this is not some practical example, I am just trying to grasp Haskell classes and instances.
You could do something like
instance Show a => Visible a
(the default implementations that you provided in the class should be enough, you could possibly move the implementations to the "Show instance" and remove the Show super class from the Visible class - then you could make even non-Show types instances of Visible without having to define their instance for Show as well).
/S
-- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
This doesn't work either: import List class Visible a where toString :: a -> String size :: a -> Int instance Show a => Visible a where toString = show size = length . show instance Visible a => Visible [a] where toString = concat . map toString size = foldl (+) 0 . map size vSort :: (Visible a, Ord a) => [a] -> String vSort = toString . List.sort s = vSort [1..3] gives: Illegal instance declaration for `Visible a' (The instance type must be of form (T a b c) where T is not a synonym, and a,b,c are distinct type variables) In the instance declaration for `Visible a' Failed, modules loaded: none. at instance Show a => Visible a where. Probably I should reconsider my expectations? How should something like this designed? -- Slavomir Kaslev

Hello Slavomir, Thursday, November 2, 2006, 4:42:21 PM, you wrote:
instance Show a => Visible a where toString = show size = length . show
instance Visible a => Visible [a] where toString = concat . map toString size = foldl (+) 0 . map size
Illegal instance declaration for `Visible a' (The instance type must be of form (T a b c) where T is not a synonym, and a,b,c are distinct type variables) In the instance declaration for `Visible a' Failed, modules loaded: none.
at instance Show a => Visible a where.
Probably I should reconsider my expectations? How should something like this designed?
1) i'm still highly recommend you wiki i've pointed before. i'd a lot of similar problems until i realized that haskell classes are somethat different form c++ ones 2) there are some proposals that will allow one to specify anti-conditions or priority of declarations. but currently compiler has no way to distinguish that instance should be used, for example, for [Char] - both are good enough. you may use -fglasgow-exts to supress this error message and and then -fallow-incoherent-instances to allow compiler select random definition from these two. you should look into ghc documentation for more details about it in practice, i just define required function bodies and then declare all specific instances i need: toString1 = concat . map toString size1 = foldl (+) 0 . map size instance Visible [Char] where toString = toString1 size = size1 instance Visible [Int] where toString = toString1 size = size1 -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

On 11/2/06, Bulat Ziganshin
Hello Slavomir,
Thursday, November 2, 2006, 4:42:21 PM, you wrote:
instance Show a => Visible a where toString = show size = length . show
instance Visible a => Visible [a] where toString = concat . map toString size = foldl (+) 0 . map size
Illegal instance declaration for `Visible a' (The instance type must be of form (T a b c) where T is not a synonym, and a,b,c are distinct type variables) In the instance declaration for `Visible a' Failed, modules loaded: none.
at instance Show a => Visible a where.
Probably I should reconsider my expectations? How should something like this designed?
1) i'm still highly recommend you wiki i've pointed before. i'd a lot of similar problems until i realized that haskell classes are somethat different form c++ ones
I am on it. Thanks for pointing it to me. =-)
2) there are some proposals that will allow one to specify anti-conditions or priority of declarations. but currently compiler has no way to distinguish that instance should be used, for example, for [Char] - both are good enough.
you may use -fglasgow-exts to supress this error message and and then -fallow-incoherent-instances to allow compiler select random definition from these two. you should look into ghc documentation for more details about it
in practice, i just define required function bodies and then declare all specific instances i need:
toString1 = concat . map toString size1 = foldl (+) 0 . map size
instance Visible [Char] where toString = toString1 size = size1
instance Visible [Int] where toString = toString1 size = size1
I wouldn't like to manually define instances of Visible for all types that have Show instances. I think I need something like that: class Visible a where toString :: a -> String size :: a -> Int instance Show a => Visible a where toString = show size = length . show Which still doesn't work. Any ideas? -- Slavomir Kaslev

Hello Slavomir, Thursday, November 2, 2006, 5:51:17 PM, you wrote:
I wouldn't like to manually define instances of Visible for all types that have Show instances.
believe it or not but i had the same problems
I think I need something like that:
class Visible a where instance Show a => Visible a where
Which still doesn't work. Any ideas?
this will work. with addition of instance Visible a => Visible [a] where this will not work. it's a mission impossible -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (2)
-
Bulat Ziganshin
-
Slavomir Kaslev