[Haskell-cafe] an idea for modifiyng data/newtype syntax: use `::=` instead of `=`

Richard Eisenberg eir at cis.upenn.edu
Sun Aug 9 01:54:53 UTC 2015

GHC ships with a solution to the original problem about a very confusing `=`: GADT syntax. I *don't* mean GADTs, in all their glory. It has been correctly observed that GADTs are not for the beginner. But there's nothing wrong at all with GADT syntax:

> data Maybe a where
>   Just :: a -> Maybe a
>   Nothing :: Maybe a

This syntax seems to fix the original poster's problem, and more besides. I've had several beginners complain about things like `data Foo = MkFoo Int`, in that we see the phrase `MkFoo Int`, but these two things -- a data constructor and a type -- belong to two very different parts of the syntax. GADT syntax gets rid of these problems.

Perhaps tutorials/teachers should just introduce GADT syntax and mention traditional syntax as a historical note. GHC even has the -XGADTSyntax extension to allow the syntax without full-blown GADTs.

To respond to two other strands of this thread:
 - Beyond the difference already observed between `data` and `newtype`, using `newtype` also works with GHC's Coercible mechanism. This is, of course, GHC-specific and not part of standard Haskell, but I thought it worth mentioning. See https://wiki.haskell.org/GHC/Coercible

 - Haskellers are, of course, welcome to change keywords in their files via CPP. This works, for example:

> #define datatype data
> #define alias type
> datatype Boolean = F | T
> alias IsGood = Boolean
> invert :: IsGood -> IsGood
> invert T = F
> invert F = T

You can even specify such things via the command line via the -D option, so you could create a wrapper around GHC that added the necessary -D options and then you'd have your new language, with nothing extra in your source files. Good luck sharing your code though! Note that this solution is functionally equivalent to hiding the change behind a pragma.


More information about the Haskell-Cafe mailing list