haskellghcexistential-typetype-synonyms

Is it possible to have forgetful type synonyms in Haskell?


If I have a type with a phantom parameter that I only sometimes care about, like this one:

data Foo p a b = Bar a b

Is there any hack way to write a type synonym Baz such that Baz a b is Foo p a b for some p that I've forgotten?

You can't do:

type Baz a b = Foo p a b

and while you can do (with appropriate extensions):

type Baz a b = forall p.Foo p a b

It doesn't seem like that does what I want, because I can't convert a value of type Foo P1 a b to type Baz a b, with a message about a "rigid type variable".

Do you need another layer of contructors to achieve this effect, as below? Could you briefly explain why?

data Baz' a b = forall p.Baz' (Foo p a b)

Solution

  • There isn't currently a way to do this as a type synonym. However, if you have GHC 7.10, you can turn on the PartialTypeSignatures extension and write Foo _ a b instead. Use -fno-warn-partial-type-signatures to ask GHC not to warn you about each of the holes you leave in this way.