[Haskell-cafe] Class and Instance

Daniel Patterson lists.haskell at dbp.mm.st
Sat Jun 11 16:05:21 CEST 2011


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 at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe




More information about the Haskell-Cafe mailing list