Difference between revisions of "List instance"

From HaskellWiki
Jump to navigation Jump to search
(→‎Question: Haskell 98 -> Haskell 2010)
(→‎FlexibleInstances: add constraint trick, update link)
Line 102: Line 102:
 
then you may choose the <code>FlexibleInstances</code> language extension
 
then you may choose the <code>FlexibleInstances</code> language extension
 
that is available in GHC and Hugs.
 
that is available in GHC and Hugs.
See http://www.haskell.org/ghc/docs/6.8-latest/html/users_guide/type-class-extensions.html#instance-rules
+
See [https://downloads.haskell.org/~ghc/8.0.2/docs/html/users_guide/glasgow_exts.html#instance-termination-rules instance rules].
   
 
<haskell>
 
<haskell>
Line 108: Line 108:
 
instance C [Char] where toX = ...
 
instance C [Char] where toX = ...
 
</haskell>
 
</haskell>
  +
  +
Note the [http://chrisdone.com/posts/haskell-constraint-trick constraint trick for instances].
  +
  +
It works because Haskell's instance resolution never backtracks. Once an instance "matches", the compiler has to commit to any equalities it discovers in the preconditions of the instance declaration.<ref>http://stackoverflow.com/a/41086866/309483</ref>
   
 
== Source ==
 
== Source ==

Revision as of 16:06, 22 March 2017

Question

How to make a list type an instance of some type class in Haskell 2010? Haskell 2010 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'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

First you have to find out, what you really want to do.

Define a custom type

If you do not need list functions, but e.g. just want to use String values as identifiers, then I advise to just define a custom type:

    newtype Identifier = Identifier String

and write the instances you need for that type.

Keep working with the list (or container) type

If you need list functions, then the wrapper approach means that you have to litter your code with calls to the wrapper constructor. In this case you should stick to the original composed type. The trick in the Prelude for the Show class 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

.

More generally, you can introduce a new class like

    class IsChar a where
        fromChar :: Char -> a
        toChar :: a -> Char
    instance IsChar Char where
        fromChar = id
        toChar = id

and then whenever you want to use the "a" type as a Char, you just convert it from or to Char with fromChar or toChar, respectively.

FlexibleInstances

If none of the above solution is appropriate for you, then you may choose the FlexibleInstances language extension that is available in GHC and Hugs. See instance rules.

    {-# LANGUAGE FlexibleInstances #-}
    instance C [Char] where toX  = ...

Note the constraint trick for instances.

It works because Haskell's instance resolution never backtracks. Once an instance "matches", the compiler has to commit to any equalities it discovers in the preconditions of the instance declaration.[1]

Source

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