
Hi Below is a class that I wish to create some instances of. I do not wish to change the class definition. It is supposed to represent containers of type x that contain things of type y. My attempt at the insert function seems ok for Char and lists, but not ok for Integer. How do I instantiate this class for integers and lists? Does this class definition permit y to be a list and hence x to be a list of lists? Something like: instance Containers [] [] where .. Is this a constructor class? Thanks, Pat class Containers x y where insert :: y -> x y -> x y remove :: y -> x y -> x y whatsIn :: x y -> [y] instance Containers [] Char where insert y [] = y:[] insert y m = y:m instance Containers [] Integer where insert y [] = y:[] insert y m = y:m -- OK -- insert '1' ['u','u','l','i'] -- Not OK -- insert 2 [9,2] This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

On Fri, 10 Jun 2011 17:28:22 +0100, Patrick Browne
-- Not OK -- insert 2 [9,2]
This causes an error, because numeric literals like 2 are polymorphic:
:t 2 2 :: Num a => a
If you fix the type to Integer, it works as expected: insert (2 :: Integer) [9,2] By the way: It's helpful to include the error messages in your mail when some piece of code doesn't compile. Cheers, Daniel

You can also modify you class if your intent is that it always work with
polymorph types (And it will save you some trouble with functional
dependencies between types x and y. Plus, it will be Haskell98 compliant)
-- insert, remove and whatsIn are then supposed to work *forall y*, and then
impose the kind of x to be * -> * (x is a type constructor, not a mere type)
class Container x where
insert :: y -> x y -> x y
remove :: y -> x y -> x y
whatsIn :: x y -> [y]
Then you can do:
import Data.List (delete)
instance Container [] where
insert = (:)
remove = delete -- removes only the first occurence
-- remove x = filter (/= x) -- if you want to remove *every* occurence
of x (there may be a better way)
whatsIn = id
2011/6/10 Patrick Browne
Hi Below is a class that I wish to create some instances of. I do not wish to change the class definition. It is supposed to represent containers of type x that contain things of type y.
My attempt at the insert function seems ok for Char and lists, but not ok for Integer. How do I instantiate this class for integers and lists?
Does this class definition permit y to be a list and hence x to be a list of lists? Something like: instance Containers [] [] where ..
Is this a constructor class?
Thanks, Pat
class Containers x y where insert :: y -> x y -> x y remove :: y -> x y -> x y whatsIn :: x y -> [y]
instance Containers [] Char where insert y [] = y:[] insert y m = y:m
instance Containers [] Integer where insert y [] = y:[] insert y m = y:m
-- OK -- insert '1' ['u','u','l','i']
-- Not OK -- insert 2 [9,2]
This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks for the feedback. I have two further questions 1. Why is it that the Containers class signature does not allow instances to be list of list? I suspect it is because x is a constructor. 2. How would I change the Containers class signature to allow instances to be lists of lists. Thanks, Pat -- x is a type constructor, not a type class Containers x y where insert :: y -> x y -> x y remove :: y -> x y -> x y whatsIn :: x y -> [y] instance Containers [] Char where insert y [] = y:[] insert y m = y:m remove _ [] = [] remove x (y:ys) | x == y = remove x ys | otherwise = y : remove x ys whatsIn = id instance Containers [] Integer where insert y [] = y:[] insert y m = y:m remove _ [] = [] remove x (y:ys) | x == y = remove x ys | otherwise = y : remove x ys whatsIn = id -- cannot have containers of containers. -- instance Containers [] [] where -- container.hs:41:23: -- `[]' is not applied to enough type arguments -- Expected kind `*', but `[]' has kind `* -> *' -- In the instance declaration for `Containers [] []' -- Failed, modules loaded: none. This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

The problem is that [] alone is not a concrete type - that is what the error is saying, that [] needs to be applied to another type to yield a concrete type. IE, you need a list of something, not just the idea of a list. That something can be polymorphic, so the following works (note the [a]): class Containers x y where insert :: y -> x y -> x y {-remove :: y -> x y -> x y whatsIn :: x y -> [y]-} instance Containers [] [a] where insert x y = x:y On Jun 11, 2011, at 5:49 AM, Patrick Browne wrote:
Thanks for the feedback. I have two further questions 1. Why is it that the Containers class signature does not allow instances to be list of list? I suspect it is because x is a constructor. 2. How would I change the Containers class signature to allow instances to be lists of lists.
Thanks, Pat
-- x is a type constructor, not a type class Containers x y where insert :: y -> x y -> x y remove :: y -> x y -> x y whatsIn :: x y -> [y]
instance Containers [] Char where insert y [] = y:[] insert y m = y:m remove _ [] = [] remove x (y:ys) | x == y = remove x ys | otherwise = y : remove x ys whatsIn = id
instance Containers [] Integer where insert y [] = y:[] insert y m = y:m remove _ [] = [] remove x (y:ys) | x == y = remove x ys | otherwise = y : remove x ys
whatsIn = id
-- cannot have containers of containers. -- instance Containers [] [] where -- container.hs:41:23: -- `[]' is not applied to enough type arguments -- Expected kind `*', but `[]' has kind `* -> *' -- In the instance declaration for `Containers [] []' -- Failed, modules loaded: none.
This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Daniel Patterson
-
Daniel Schoepe
-
Patrick Browne
-
Yves Parès