9

GHC 7.8 中的 OverloadedLists 语言 pragma 非常吸引人,所以我决定尝试一下:

{-# LANGUAGE OverloadedLists #-}

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

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

编译器给了我:

    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.

即使是发行说明中的​​示例也不起作用:

> ['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

有谁知道这里发生了什么?

4

1 回答 1

8

在 source中只定义了一个简单的实例。您可以定义自己的实例以Data.Set使用:

{-# LANGUAGE TypeFamilies #-}

instance IsList (Set a) where
  type Item (Set a) = a
  fromList = Data.Set.fromList
  toList = Data.Set.toList

请注意,IsList适用于 GHC >= 7.8。

于 2014-04-25T01:50:18.453 回答