I'm currently having a fight with the typechecker when I try to create a function returning Thing a
(where Thing
is a GADT). A minimal contrived example:
{-#LANGUAGE GADTs, EmptyDataDecls #-}
module Main where
-- Define a contrived GADT
data TFoo
data TBar
data Thing a where
Foo :: Int -> Thing TFoo
Bar :: String -> Thing TBar
combine :: [Thing a]
combine = [Foo 1, Bar "abc"]
main :: IO ()
main = undefined
The typechecker gets unhappy that a
doesn't match TBar
. Presumably this is because it has already inferred that a
is TFoo
. But, this is surprising, because with regular sum types you can do:
data Thing = Foo Int | Bar String
combine :: [Thing]
combine = [Foo 1, Bar "abc"]
Is there anyway to return a type parametric over the GADT parameter?
Outside of the context of the contrived examples, I need GADTs so I can type certain functions to only take a Foo
, but before that I need to also be able to just return a list of Thing
s.