Trying to do the JSON de-serialisation for a data type with TypeLits, I get stuck with the following problem:
Couldn't match type ‘n’ with ‘2’ ‘n’ is a rigid type variable bound by the instance declaration at test.hs:14:10 Expected type: aeson-0.11.2.1:Data.Aeson.Types.Internal.Parser (X n) Actual type: aeson-0.11.2.1:Data.Aeson.Types.Internal.Parser (X 2)
How would be the correct syntax to allow Nat generically in the FromJSON instance in the following example:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
import GHC.TypeLits
import Data.Aeson
import Control.Monad (mzero)
data X (n :: Nat) where
A :: Integer -> X 1
B :: Integer -> X 2
instance FromJSON (X n) where
parseJSON (Object o) = do
v <- o .: "val"
t <- o .: "type"
case t of
"a" -> return $ A v
"b" -> return $ B v
parseJSON _ = mzero
FromJSON (X 1)
andFromJSON (X 2)
, or do a typecase onn
(using a singleton). The way you have the instance written says that you can readX n
for anyn
the caller chooses. – luqui