Difference between revisions of "List instance"

From HaskellWiki
Jump to navigation Jump to search
(taken from Haskell-Cafe)
 
m
Line 10: Line 10:
 
class C a where
 
class C a where
 
toX :: a -> X
 
toX :: a -> X
</haskell>
+
</haskell>
 
 
 
I can define instances for
 
I can define instances for
   
<haskell>
+
<haskell>
 
instance C Int where toX = ...
 
instance C Int where toX = ...
 
instance C Double where toX = ...
 
instance C Double where toX = ...
 
instance C Tuple where toX = ...
 
instance C Tuple where toX = ...
</haskell>
+
</haskell>
 
 
 
but not for Strings, given that they are a synonym for [Char]. Hence:
 
but not for Strings, given that they are a synonym for [Char]. Hence:
   
<haskell>
+
<haskell>
 
instance C String where toX = ...
 
instance C String where toX = ...
</haskell>
+
</haskell>
 
 
 
results in:
 
results in:
Line 38: Line 38:
 
98? I can create a new wrapper type for strings:
 
98? I can create a new wrapper type for strings:
   
<haskell>
+
<haskell>
 
newtype StringWrap = StringWrap String
 
newtype StringWrap = StringWrap String
</haskell>
+
</haskell>
   
 
and write an instance for that, but then I'll have to litter my code
 
and write an instance for that, but then I'll have to litter my code
Line 52: Line 52:
 
toX :: a -> X
 
toX :: a -> X
 
listToX :: [a] -> X
 
listToX :: [a] -> X
</haskell>
+
</haskell>
 
 
 
but I believe this says that whenever we can convert a to an <hask>X</hask> we can also
 
but I believe this says that whenever we can convert a to an <hask>X</hask> we can also
Line 61: Line 61:
 
The trick in the Prelude is that <hask>listToX</hask> has a default implementation.
 
The trick in the Prelude is that <hask>listToX</hask> has a default implementation.
   
If this is not possible the introduce a new class like
+
If this is not possible in your application then introduce a new class like
   
 
<haskell>
 
<haskell>
 
class Element a where
 
class Element a where
 
listToX :: [a] -> X
 
listToX :: [a] -> X
</haskell>
+
</haskell>
   
 
and define instances like
 
and define instances like
Line 77: Line 77:
 
toX = listToX
 
toX = listToX
 
</haskell>
 
</haskell>
  +
.
   
 
== Source ==
 
== Source ==

Revision as of 12:05, 22 May 2007

Question

How to make a list type an instance of some type class in Haskell 98? Haskell 98 does not support instances on particular composed types like String.


If I have a type class for conversion to a type X:

 
    class C a where
        toX   :: a -> X

I can define instances for

    instance C Int where toX  = ...
    instance C Double where toX  = ...
    instance C Tuple where toX  = ...

but not for Strings, given that they are a synonym for [Char]. Hence:

    instance C String where toX  = ...

results in:

   Illegal instance declaration for `C String'
       (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 `C String'

Is there some type class cleverness that can make this work in haskell 98? I can create a new wrapper type for strings:

    newtype StringWrap = StringWrap String

and write an instance for that, but then I'll have to litter my code with calls to this constructor.

I'm aware of the approach taken by class Show in the prelude, which adds a extra method to the class:

    class C a where
        toX     :: a -> X
        listToX :: [a] -> X

but I believe this says that whenever we can convert a to an X we can also convert [a] to an X, whereas I only want [Char] to be acceptable.

Answer

The trick in the Prelude is that listToX has a default implementation.

If this is not possible in your application then introduce a new class like

    class Element a where
        listToX :: [a] -> X

and define instances like

    instance Element Char where
        listToX = ...

    instance Element a => C [a] where
        toX = listToX

.

Source

http://www.haskell.org/pipermail/haskell-cafe/2007-May/025742.html