I'm working in an ihaskell jupyter notebook, so I know the obvious answer to this.
But I'm also using an explicit import from Data.List where it shows this error
import qualified Data.List as L
So I am actually confused about the line that quotes IHaskellPrelude is involved instead of my explicit import from Data.List which is not prelude. (This is a more specific question following from this one)
My error message:
<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}’
See the line about the fieldLabelModifier? It calls IHaskellPrelude.drop. But if you look below, my code calls L.drop:
Full code:
{-# 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
I am working in a new session, new terminal, have only run this. I don't understand why the error message quoted to me is using a function sourced from IHaskellPrelude.drop rather than the L.drop I am explicitly calling.
From suggestion in answer, when I try:
instance ToJSON R3 where
toJSON = genericToJSON defaultOptions {
fieldLabelModifier = map toLower . L.drop 3 }
I get:
<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’
Which combination of these is compatible with what Prelude is asking for?
It doesn't matter if it's IHaskellPrelude.drop
or Data.List.drop
: they're the same thing. Both the standard Prelude and the IHaskell one simply re-exports the list drop
function. GHC notices that, and thus apparently decides to inform you about the more “basic” import-path, i.e. the one from the prelude. (I don't know how the heuristic works, but usually GHC is quite good at showing the most convenient import qualifier-name for a binding.)
The problem is that your toJSON
is just not type correct, because toLower
only works on a Char
, not on a String
. This can easily be solved by using map toLower
.