gpt4 book ai didi

haskell - 重载列表扩展不起作用

转载 作者:行者123 更新时间:2023-12-02 01:56:52 26 4
gpt4 key购买 nike

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

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

最佳答案

只定义了一个简单的实例in the 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。

关于haskell - 重载列表扩展不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23282258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com