0

我正在使用 ihaskell jupyter 笔记本,所以我知道这个问题的明显答案。

但我也在使用从 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 的要求兼容?

4

1 回答 1

2

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

问题是您toJSON的类型不正确,因为toLower仅适用于 a Char,而不适用于 a String。这可以通过使用轻松解决map toLower

于 2019-04-24T14:13:26.023 回答