Col*_*ury 9 haskell language-extension
GHC 7.8中的OverloadedLists语言编译器非常有吸引力,所以我决定尝试一下:
{-# LANGUAGE OverloadedLists #-}
import Data.Set (Set)
import qualified Data.Set as Set
mySet :: Set Int
mySet = [1,2,3]
Run Code Online (Sandbox Code Playgroud)
编译器给了我:
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.
Run Code Online (Sandbox Code Playgroud)
即使是发行说明中的示例也不起作用:
> ['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
Run Code Online (Sandbox Code Playgroud)
有谁知道这里发生了什么?
源中只定义了一个简单的实例.您可以定义自己的实例以供Data.Set使用:
{-# LANGUAGE TypeFamilies #-}
instance IsList (Set a) where
type Item (Set a) = a
fromList = Data.Set.fromList
toList = Data.Set.toList
Run Code Online (Sandbox Code Playgroud)
请注意,IsList是仅适用于GHC> = 7.8.