[英]How can I detect if GHC is set to generate 32bit or 64bit code by default?
[英]is this a bug of 32bit GHC on MAC
為了使長STROY總之,我的代碼使用有關分析JSON文件埃宋
這是我的兩段代碼:
a.hs
{-# LANGUAGE OverloadedStrings #-}
import Data.Aeson
import qualified Data.ByteString.Lazy.Char8 as C
import Control.Monad
import Control.Applicative
data AuctionInfo = AuctionInfo {
realm :: Realm ,
alliance :: Auctions ,
horde :: Auctions ,
neutral :: Auctions
} deriving (Show )
instance FromJSON AuctionInfo where
parseJSON (Object o) = do
r <- o .: "realm" >>= parseJSON
a <- o .: "alliance" >>= parseJSON
h <- o .: "horde" >>= parseJSON
n <- o .: "neutral" >>= parseJSON
return $ AuctionInfo r a h n
parseJSON _ = mzero
data Realm = Realm { name2 :: String , slug:: String} deriving (Show )
instance FromJSON Realm where
parseJSON (Object o) = Realm <$>
o .: "name" <*>
o .: "slug"
parseJSON _ = mzero
data Auctions = Auctions {auctions :: [Auc]} deriving (Show)
instance FromJSON Auctions where
parseJSON (Object o ) = Auctions <$> o.: "auctions"
parseJSON _ = mzero
data Auc = Auc {
auc :: Integer,
itme :: Int,
owner :: String,
bid :: Integer,
buyout ::Integer,
quantity :: Int,
timeLeft :: String,
rand :: Integer,
seed :: Integer
} deriving (Show )
instance FromJSON Auc where
parseJSON (Object o ) = Auc <$>
o .: "auc" <*>
o .: "item" <*>
o .: "owner" <*>
o .: "bid" <*>
o .: "buyout" <*>
o .: "quantity" <*>
o .: "timeLeft" <*>
o .: "rand" <*>
o .: "seed"
parseJSON _ = mzero
main = do
au<- C.readFile "a.json"
let x = decode au :: Maybe AuctionInfo
case x of
Just a -> do
{-putStrLn.show $ a-}
putStrLn .show.length.auctions.alliance $ a
putStrLn "ok"
Nothing -> putStrLn "fail"
我的json測試文件
和測試步驟:
aeson
, $ cabal install aseon
$ ghc a.hs -oa
$ ./a
我從輸出中獲得的是“失敗”。
當我運行命令$ runghc a.hs
幾次時,我甚至得到了一些ok
,有些fail
混合在一起。
我也在我的linux和64bit mac ghc上嘗試了這個代碼,它們都按照我的預期輸出ok
。
我的一個朋友也在他的32位mac ghc上試過這個代碼,也fail
了。 而且他告訴我,他在我的代碼中扮演了一些黑魔法,並改變了一行
let x = decode $(C.pack. C.unpack) au :: Maybe AuctionInfo
則輸出是ok
。 但是當我做同樣的黑魔法時,輸出仍然fail
。
我只是想確定這是我的錯誤還是ghc的錯誤,或者我如何確定。
我不確定該行為是否與此相關,但您絕對不應該使用Data.ByteString.Lazy.Char8
因為這僅適用於8位ASCII數據而您的輸入是UTF-8。
嘗試用。替換該導入
import qualified Data.ByteString.Lazy as BL
並使用BL.readFile
讀取數據(當然實際名稱無關緊要,但BL
是lazy bytestring包的慣用簡寫)。
請注意,通常您會使用Data.Text
來處理unicode文本,但在這種情況下, aeson
API需要二進制(即ByteString
)表示並處理內部解碼unicode。
編輯:實際上現在我已經考慮過這個了,我認為問題不在於使用Char8
(盡管關於一般的unicode文本沒有使用它),因為你沒有做任何轉換String
或Char
(期望使用C.pack . C.unpack
實驗,這將破壞所有多字節字符)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.