OverloadedLists extension not working
Asked Answered
A

1

9

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?

Accommodate answered 25/4, 2014 at 0:38 Comment(1)
It says in the error: No instance for (GHC.Exts.IsList (Set Int)). Data.Set doesn't define an instance for IsList. You can write the instance yourself quite easily, here is the class: hackage.haskell.org/package/base-4.7.0.0/docs/…Drews
G
8

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.

Glaser answered 25/4, 2014 at 1:50 Comment(3)
That's unfortunate, I had thought those instances were built-in.Accommodate
Also note you need to use the TypeFamilies pragma as well for this to work.Accommodate
Thanks for pointing that out; I've edited the answer accordingly.Glaser

© 2022 - 2024 — McMap. All rights reserved.