[Haskell-beginners] FlexibleInstances

Emanuel Koczwara poczta at emanuelkoczwara.pl
Sun Nov 24 13:32:40 UTC 2013


Hi,

   I have very simple code:

   class XmlValue a where
       toValue :: a -> String

   instance XmlValue String where
       toValue _ = "lorem"

   It gives followingerror:

   Illegal instance declaration for `XmlValue String'
   (All instance types must be of the form (T t1 ... tn) where T is not 
a synonym.Use -XTypeSynonymInstancesif you want to disable this.)

   So, maybe I'll removethat synonym:

   instance XmlValue [Char] where
       toValue _ = "lorem"

   This gives following error:

   Illegal instance declaration for `XmlValue [Char]'
    (All instance types must be of the form (T a1 ... an) where a1 ... 
an are *distinct type variables*, and each type variable appears at most 
once in the instance head. Use -XFlexibleInstancesif you want to disable 
this.)

   So, maybe I'll try to use that (T a1 ... an)form:

   instance XmlValue ([] String) where
       toValue _ = "lorem"

   Illegal instance declaration for `XmlValue [String]'
    (All instance types must be of the form (T a1 ... an) where a1 ... 
an are *distinct type variables*, and each type variable appears at most 
once in the instance head. Use -XFlexibleInstancesif you want to disable 
this.)

   Ok. Maybe I'll try to enable FlexibleInstanceswith {-# LANGUAGE 
FlexibleInstances #-}

   Ok, this works, but:

http://stackoverflow.com/questions/15285822/cant-make-string-an-instance-of-a-class-in-haskell(second 
answer):

   "
   However, Haskell98 forbids this type of typeclass in order to keep 
things simple and to make it harder for people to write overlapping 
instances like

|   instance  Slang[a]  where
       -- Strings would also fit this definition.
       slangify list=  "some list"
   "|

   Ok, that's bad. But I want only [Char], [a] shouldn't work.

   https://ghc.haskell.org/trac/haskell-prime/wiki/FlexibleInstances  says, that I'll get all or nothing.

   I want to enable "instance [Char] where..." but disable "instance [a] where...". Is it possible?


Thanks,
Emanuel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20131124/a6e285ae/attachment.html>


More information about the Beginners mailing list