{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Mcmc.Item
( Item (..),
)
where
import Data.Aeson
import Numeric.Log
instance ToJSON a => ToJSON (Log a) where
toJSON (Exp x) = toJSON x
toEncoding (Exp x) = toEncoding x
instance FromJSON a => FromJSON (Log a) where
parseJSON v = Exp <$> parseJSON v
data Item a = Item
{
state :: a,
prior :: Log Double,
likelihood :: Log Double
}
deriving (Eq, Ord, Show, Read)
instance ToJSON a => ToJSON (Item a) where
toJSON (Item x p l) = object ["s" .= x, "p" .= p, "l" .= l]
toEncoding (Item x p l) = pairs ("s" .= x <> "p" .= p <> "l" .= l)
instance FromJSON a => FromJSON (Item a) where
parseJSON = withObject "Item" $
\v ->
Item
<$> v .: "s"
<*> v .: "p"
<*> v .: "l"