[Haskell-cafe] What's the new type ?

Edward Kmett ekmett at gmail.com
Fri Nov 6 09:34:12 EST 2009

Given a newtype declaration:

newtype Foo a = Bar (...)

The newtype is Foo a, and it uses a constructor Bar, which gets 'erased' at
compile time, unlike a data declaration. By convention, usually Foo and Bar
are the same thing. In this case the constructor for GenParser is named
Parser instead.

To understand the GenParser type, you must consider that originally,
'GenParser' probably didn't exist. And if it did, there is a pedagogical
justification to just explaining the simpler 'Parser' case first without
appealing to the notion of a parser in its full generality.

So if you started from a type like

newtype Parser a = Parser (State Char () -> Consumed (Reply Char () a))

and later want to generalize that to a more permissive signature, without
breaking all of the code that uses that constructor, then the upgrade path
for that code is to keep the same constructor name, but generalize the type.

So Parser becomes a type alias:

type Parser = GenParser Char ()

and GenParser is introduced as a newtype, which happens to use the
constructor Parser for the dual reasons of backwards compatibility and so
that people working on simple parsers don't need to think about alternative
user state and token types.

newtype GenParser tok st a = Parser (State tok st -> Consumed (Reply tok st

Now the only thing that breaks is that any code that previously defined
instances for Parser before the notion of GenParser must add a LANGUAGE
pragma indicating that TypeSynonymInstances are allowed.

-Edward Kmett

On Thu, Nov 5, 2009 at 2:17 AM, zaxis <z_axis at 163.com> wrote:

> type Parser a = GenParser Char () a
> newtype GenParser tok st a = Parser (State tok st -> Consumed (Reply tok st
> a))
> As i know, the Parser type is just an alias of GenParser. Then  can the
> second line be replaced as below?
> newtype GenParser tok st a = GenParser Char () (State tok st -> Consumed
> (Reply tok st a))
> If it can , then what is the new type ?
> Sincerely!
> -----
> fac n = foldr (*) 1 [1..n]
> --
> View this message in context:
> http://old.nabble.com/What%27s-the-new-type---tp26208600p26208600.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091106/e42d4c58/attachment.html

More information about the Haskell-Cafe mailing list