0

I'm a Haskell newb, having only just finished reading LYAH. I am reading this Aeson tutorial, but I'm having trouble executing most of the code examples. For instance, I have the following in Scratch01.hs ...

{-# LANGUAGE OverloadedStrings #-}

module Scratch01 where

import Data.Aeson
import GHC.Exts

val :: Value
val = Object $ fromList [
  ("numbers", Array $ fromList [Number 1, Number 2, Number 3]),
  ("boolean", Bool True) ]

... and I'm trying to execute it like this

$ ghci --version
The Glorious Glasgow Haskell Compilation System, version 7.8.3
$ ghci -XOverloadedStrings
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
Prelude> :load Scratch01.hs
[1 of 1] Compiling Scratch01        ( Scratch01.hs, interpreted )

Scratch01.hs:10:3:
    Couldn't match expected type ‘Item Object’
                with actual type ‘(t0, Value)’
    The type variable ‘t0’ is ambiguous
    In the expression:
      ("numbers", Array $ fromList [Number 1, Number 2, Number 3])
    In the first argument of ‘fromList’, namely
      ‘[("numbers", Array $ fromList [Number 1, Number 2, ....]),
        ("boolean", Bool True)]’
    In the second argument of ‘($)’, namely
      ‘fromList
         [("numbers", Array $ fromList [Number 1, Number 2, ....]),
          ("boolean", Bool True)]’

Scratch01.hs:10:33:
    Couldn't match expected type ‘Item Array’ with actual type ‘Value’
    In the expression: Number 1
    In the first argument of ‘fromList’, namely
      ‘[Number 1, Number 2, Number 3]’
Failed, modules loaded: none.
Prelude>

I've been tinkering with the types for a bit, but I'm stumped. Am I using the wrong version of GHC or something?

Edit

I did what artyom suggested. This works:

{-# LANGUAGE OverloadedStrings #-}

module Scratch01 where

import Data.Aeson
import qualified Data.HashMap.Lazy as HM
import qualified Data.Vector as V

val :: Value
val = Object $ HM.fromList [
  ("numbers", Array $ V.fromList [Number 1, Number 2, Number 3]),
  ("boolean", Bool True) ]
Jonny Appleseed
  • 121
  • 1
  • 8
  • 1
    What's your version of vector? (You can determine it by doing `cabal info vector | grep installed`.) If it's lower than 0.10.12.0, I guess that's the problem (in which case you'll have to replace `fromList` with `Vector.fromList` and `HashMap.fromList` respectively). – Emily Feb 04 '16 at 15:30
  • That works! I've edited the post with the updates. – Jonny Appleseed Feb 05 '16 at 12:25
  • @JonnyAppleseed if you found a solution, you should post it as an answer and accept it, rather than just update the post. That way this doesn't linger as an unanswered question... – sclv Mar 03 '16 at 20:59

0 Answers0