8

One can use GADT to express Existentially quantified types.

I see that GADT is more generic - data-type-extensions, paragraph section 7.4.7

When it's better to use Existentially quantified types then GADT? Are there any drawbacks using GADT compared to Existentially quantified types?

Robert Zaremba
  • 8,081
  • 7
  • 47
  • 78

2 Answers2

11

GADTs came along later than existentials, and they generalise them. I'm not aware of any drawbacks and would always use GADT syntax in new code as it's much clearer.

The documentation confirms this:

Notice that GADT-style syntax generalises existential types.

Ganesh Sittampalam
  • 28,821
  • 4
  • 79
  • 98
  • 2
    That's not quite true, the old broken behaviour was for data type contexts, not existentials. This works http://lpaste.net/108693 – daniel gratzer Aug 03 '14 at 11:42
  • 1
    The difference you mentioned is not the difference between GADTs and existentials. Both provide class context when you match against the constructor. – max taldykin Aug 03 '14 at 11:45
  • Thanks for the correction, my mistake. I misread the bit talking about the difference between vanilla H98 and GADTs and didn't think about it properly. – Ganesh Sittampalam Aug 03 '14 at 11:48
  • Yes, the GADT syntax is much cleaner, I too would advise sticking to GADTs. – AndrewC Aug 03 '14 at 11:51
2

If you use the newest version of GHC, then there are no drawbacks I'm aware of. But in older versions you could combine GADTs and GeneralizedNewtypeDeriving in a type-unsafe way. I don't think the same was possible with ExistentialQuantification.

MigMit
  • 1,698
  • 12
  • 14
  • 2
    Can you please expand on how combining GND with "GADTs as existentials" can lead to type unsafety? I think it is "type-level programming" part of GADTs that leads to unsafe behavior not the existential one. – max taldykin Aug 03 '14 at 12:31
  • @maxtaldykin I don't think it can. I recall GADTs are equivalent in power to combining existential quantification with type equality contexts, and it's the latter that are obviously broken by the old unrestricted newtype deriving. – Ørjan Johansen Aug 03 '14 at 16:53
  • @maxtaldykin I think it was only due GND not properly checking type equalities (which I believe is fixed now): http://joyoftypes.blogspot.com/2012/08/generalizednewtypederiving-is.html – David Young Aug 03 '14 at 20:21
  • 1
    @maxtaldykin, sure. Check this out: http://migmit.livejournal.com/44473.html — text is in Russian, but the code should be clear enough. And yes, it's fixed now. – MigMit Aug 06 '14 at 08:27
  • There are no existentially quantified variables in your code. My point is that it is safe enough to use GADTs instead of ExistentialQuantification. – max taldykin Aug 06 '14 at 11:21
  • 1
    Of course there are no existentially quantified variables in my code, because you CAN'T do such a thing with existential quantification. THAT'S WHY existential quantification is safer than GADTs in older versions of GHC. – MigMit Aug 24 '14 at 18:21