我无法弄清楚为什么在ghci中直接执行代码
Prelude> import Data.Aeson
Prelude Data.Aeson> toJSON 3
Number 3.0
这个简单的程序
import Data.Aeson
main = print . toJSON $ 3
失败并显示错误
/mnt/data/Maggot/tmp.hs:2:16:
No instance for (ToJSON s0) arising from a use of ‘toJSON’
In the second argument of ‘(.)’, namely ‘toJSON’
In the expression: print . toJSON
In the expression: print . toJSON $ 3
/mnt/data/Maggot/tmp.hs:2:25:
No instance for (Num s0) arising from the literal ‘3’
The type variable ‘s0’ is ambiguous
Note: there are several potential instances:
instance Num Double -- Defined in ‘GHC.Float’
instance Num Float -- Defined in ‘GHC.Float’
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
...plus 17 others
In the second argument of ‘($)’, namely ‘3’
In the expression: print . toJSON $ 3
In an equation for ‘main’: main = print . toJSON $ 3
我知道我可以直接指定type(3 :: Int),但我很好奇如何解决这个问题? 感谢。