What's the new type ?

View: New views
6 Messages — Rating Filter:   Alert me  

What's the new type ?

by zaxis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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]

Re: What's the new type ?

by Ryan Ingram :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

"newtype" creates a wrapper for an existing type that gets erased (so
it has no cost) at runtime, but is distinct during typechecking.

"type" creates an alias for an existing type, which is interchangeable
(so it doesn't really exist during typechecking).

In this case:

GenParser tok st a
   is a function of the type
State tok st -> Consumed (Reply tok st a)

But you have to extract the value (via pattern matching) to call the
function; to the typechecker they are distinct types.

On the other hand, anywhere you see "Parser a", you could write
"GenParser Char () a" and it would work the same.  It's just giving
you a shorter name for that common parser type.

  -- ryan

On Wed, Nov 4, 2009 at 11:17 PM, zaxis <z_axis@...> 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@...
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: What's the new type ?

by Miguel Mitrofanov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In "type Parser" Parser is a type synonym. In GenParser, Parser is a
data constructor; they live in separate namespaces.

Yes, I admit, it's confusing.

zaxis 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]
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: What's the new type ?

by edwardk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
a))

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@...> 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@...
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: What's the new type ?

by zaxis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


very clear !  thank you


edwardk wrote:

>
> 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
> a))
>
> 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@...> 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@...
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@...
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


-----
fac n = foldr (*) 1 [1..n]
--
View this message in context: http://old.nabble.com/What%27s-the-new-type---tp26208600p26233954.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe
fac n = foldr (*) 1 [1..n]

Re: What's the new type ?

by zaxis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

very clear !  thank you

edwardk wrote:
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
a))

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@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@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
fac n = foldr (*) 1 [1..n]