[Haskell-cafe] How to bring existentially quantified type variables into scope

Wolfgang Jeltsch wolfgang-it at jeltsch.info
Thu Aug 10 23:16:04 UTC 2017


Hi!

GHC has some support for referring to existentially quantified type
variables. For example, consider the following definition:

> data SomeList = forall a . SomeList [a]

With the ScopedTypeVariables extension, you can get hold of the concrete
type a used in a SomeList value:

> f :: SomeList -> SomeList
> f (SomeList [x :: a]) = SomeList xs where
>
>     xs :: [a]
>     xs = [x, x]

However, this approach does not work if there are no fields whose type
involves the existentially quantified type variable. Consider, for
example, the following definition:

> data IsListType a where
>
>     IsListType :: IsListType [b]

For each type T, we can match a value of type IsListType T against the
pattern IsListType. If this succeeds, we know that T is a list type, but
we do not have access to the respective element type.

Is there a way to determine existentially quantified type variables like
the b in the above example?

All the best,
Wolfgang


More information about the Haskell-Cafe mailing list