Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't make String an instance of a class in Haskell

Tags:

class

haskell

I'm playing around trying to understand classes in Haskell. I wrote a silly few lines of code to get the hang of it. I wrote a class called Slang that has one function. When I make Integer an instance of my class, it works fine. But when I make String an instance of my class it won't compile. I've been fidgeting with the program based on what the error output tells me but to no avail. I have idea why it work...

Here is the code followed by the error:

module Practice where

class Slang s where
    slangify :: s -> String

instance Slang Integer where
    slangify int = "yo"

instance Slang String where  -- When I take this segment out, it works fine
    slangify str = "bro"

ERROR:

Prelude> :load Practice
[1 of 1] Compiling Practice         ( Practice.hs, interpreted )

Practice.hs:9:10:
    Illegal instance declaration for `Slang String'
      (All instance types must be of the form (T t1 ... tn)
       where T is not a synonym.
       Use -XTypeSynonymInstances if you want to disable this.)
    In the instance declaration for `Slang String'
Failed, modules loaded: none.
Prelude>
like image 906
CodyBugstein Avatar asked Mar 08 '13 02:03

CodyBugstein


1 Answers

The problem is that String is not a base type like Integer. What you are trying to do is actually

instance Slang [Char] where
    slangify str = "bro"

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"

Anyway, as the error message suggests, you can get around this restriction by enabling the FlexibleInstances extension.

like image 60
hugomg Avatar answered Sep 22 '22 07:09

hugomg