I have a data structure that I have created with GADT and I want to parse some json to this GADT using aeson
. But the type checker complains that It's only possible to create one of the constructors of the GADT in all the cases. See this example:
data Foo = Hello | World
data SFoo :: Foo -> Type where
SHello :: SFoo 'Hello
SWorld :: SFoo 'World
instance FromJSON (SFoo a) where
parseJSON = withText "Foo" \case
"hello" -> pure SHello
"world" -> pure SWorld
So I want to be able to parse the "hello" string to SHello
and the "world" string to SWorld
. The type checker complains with the following error:
• Couldn't match type ‘'World’ with ‘'Hello’
Expected type: Parser (SFoo 'Hello)
Actual type: Parser (SFoo 'World)
• In the expression: pure SWorld
In a case alternative: "world" -> pure SWorld
In the second argument of ‘withText’, namely
‘\case
"hello" -> pure SHello
"world" -> pure SWorld’
How can I parse some json to a GADT structure like this?