haskelllanguage-extension

OverloadedLists extension not working


The OverloadedLists language pragma in GHC 7.8 is quite attractive, so I decided to try it:

{-# LANGUAGE OverloadedLists #-}

import Data.Set (Set)                                                                     
import qualified Data.Set as Set

mySet :: Set Int
mySet = [1,2,3]

And the compiler gives me:

    No instance for (GHC.Exts.IsList (Set Int))
      arising from an overloaded list
    In the expression: [1, 2, 3]
    In an equation for ‘mySet’: mySet = [1, 2, 3]

    No instance for (Num (GHC.Exts.Item (Set Int)))
      arising from the literal ‘1’
    In the expression: 1
    In the expression: [1, 2, 3]
    In an equation for ‘mySet’: mySet = [1, 2, 3]
Failed, modules loaded: none.

Even the example from the release notes doesn't work:

> ['0' .. '9'] :: Set Char

<interactive>:5:1:
    Couldn't match type ‘GHC.Exts.Item (Set Char)’ with ‘Char’
    Expected type: [Char] -> Set Char
      Actual type: [GHC.Exts.Item (Set Char)] -> Set Char
    In the expression: ['0' .. '9'] :: Set Char
    In an equation for ‘it’: it = ['0' .. '9'] :: Set Char

Does anyone know what's going on here?


Solution

  • There is only a trivial instance defined in the source. You can define your own instance for Data.Set using:

    {-# LANGUAGE TypeFamilies #-}
    
    instance IsList (Set a) where
      type Item (Set a) = a
      fromList = Data.Set.fromList
      toList = Data.Set.toList
    

    Note that IsList is only available for GHC >= 7.8.