这个错误 IHaskellPrelude 是从哪里来的?

Where is IHaskellPrelude coming from in this error?

我在 ihaskell jupyter notebook 上工作,所以我知道这个问题的明显答案。

但我也使用了从 Data.List 显式导入,它显示了这个错误

import qualified Data.List as L

所以我实际上对引用 IHaskellPrelude 的行感到困惑 我从 Data.List 明确导入的内容,这不是前奏。 (这是来自 的一个更具体的问题)

我的错误信息:

<interactive>:14:35: error:
    • Couldn't match type ‘Char’ with ‘String’
      Expected type: String -> String
        Actual type: String -> Char
    • In the ‘fieldLabelModifier’ field of a record
      In the first argument of ‘genericToJSON’, namely ‘defaultOptions {fieldLabelModifier = Data.Char.toLower . IHaskellPrelude.drop 3}’
      In the expression: genericToJSON defaultOptions {fieldLabelModifier = Data.Char.toLower . IHaskellPrelude.drop 3}
<interactive>:14:47: error:
    • Couldn't match type ‘String’ with ‘Char’
      Expected type: String -> Char
        Actual type: String -> String
    • In the second argument of ‘(.)’, namely ‘IHaskellPrelude.drop 3’
      In the ‘fieldLabelModifier’ field of a record
      In the first argument of ‘genericToJSON’, namely ‘defaultOptions {fieldLabelModifier = Data.Char.toLower . IHaskellPrelude.drop 3}’

看到关于 fieldLabelModifier 的那一行了吗?它调用 IHaskellPrelude.drop。但是如果你看下面,我的代码调用 L.drop:

完整代码:

{-# LANGUAGE DeriveGeneric,  OverloadedStrings, RankNTypes, KindSignatures #-}
:ext DeriveGeneric OverloadedStrings FlexibleContexts RankNTypes KindSignatures DataKinds

-- stack overflow question
import GHC.Generics
import Data.Aeson 
import Data.Aeson.Encode.Pretty 
import Data.Time
-- every dumb library working with any text in haskell requires
import Data.Text as T
import Text.Show.Pretty
import Data.Char(toLower)
import qualified Data.List as L
import qualified Data.Char as C
import Data.ByteString as BS
import Data.Aeson.Text (encodeToLazyText)
import Data.Text.Lazy.IO as I
--
import Text.Regex.PCRE
(.=) = (Data.Aeson..=)

type Code = Text
type Value = Float

-- currency parser
-- Sample
currency = "100.01"

-- -- Value part
vparse :: T.Text -> Float
vparse raw = (read ((T.unpack raw) =~ ("[\d].*") :: String) :: Float)


data R3 = R3 { recCode :: Code 
             , recValue :: Value} deriving (Show, Generic)


makeR3 rawcode rawval = R3 code value where
                                     code = rawcode
                                     value = vparse rawval


instance ToJSON R3 where
  toJSON = genericToJSON defaultOptions {
             fieldLabelModifier = C.toLower . L.drop 3 }
-- this says L.drop not prelude!  


instance FromJSON R3 where
  parseJSON = withObject "R3" $ \r ->
      R3 <$> r .: "code"
         <*> r .: "value"

r3 = makeR3 "TD" "100.42"
as_json = encode r3


main = do
    let out = encodeToLazyText r3
    I.putStrLn out
    I.writeFile "./so2.json" out
    return ()

main

我正在一个新会话、新终端中工作,只有 运行 这个。我不明白为什么向我引用的错误消息使用的是来自 IHaskellPrelude.drop 的函数,而不是我明确调用的 L.drop。

更新

根据回答中的建议,当我尝试时:

instance ToJSON R3 where
  toJSON = genericToJSON defaultOptions {
             fieldLabelModifier = map toLower . L.drop 3 }

我得到:

<interactive>:14:35: error:
    Ambiguous occurrence ‘map’
    It could refer to either ‘BS.map’, imported from ‘Data.ByteString’
                          or ‘T.map’, imported from ‘Data.Text’
                          or ‘IHaskellPrelude.map’, imported from ‘Prelude’ (and originally defined in ‘GHC.Base’)
<interactive>:14:39: error:
    Ambiguous occurrence ‘toLower’
    It could refer to either ‘Data.Char.toLower’, imported from ‘Data.Char’ at <interactive>:1:18-24 (and originally defined in ‘GHC.Unicode’)
                          or ‘T.toLower’, imported from ‘Data.Text’

哪种组合与 Prelude 的要求兼容?

IHaskellPrelude.drop 还是 Data.List.drop 都没关系:它们 是一样的 。标准 Prelude 和 IHaskell 都只是重新导出列表 drop 函数。 GHC 注意到这一点,因此显然决定通知您有关更“基本”的导入路径,即前奏曲中的导入路径。 (我不知道启发式是如何工作的,但通常 GHC 非常擅长为绑定显示最方便的导入限定符名称。)

问题是您的 toJSON 类型不正确,因为 toLower 仅适用于 Char,不适用于 String。使用 map toLower.

可以很容易地解决这个问题