Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Factor out parsec types (Max Gautier)
   2. Re:  Factor out parsec types (Francesco Ariis)
   3.  Fwd:  Factor out parsec types (Max Gautier)
   4. Re:  Fwd:  Factor out parsec types (Francesco Ariis)


----------------------------------------------------------------------

Message: 1
Date: Tue, 24 Mar 2020 16:06:03 +0100
From: Max Gautier <ashelia1...@gmail.com>
To: beginners@haskell.org
Subject: [Haskell-beginners] Factor out parsec types
Message-ID:
        <CAB=R4ugWYu=Py1s4ksm=boJqiyYK1+D8aOY=dk3v0m9ahf1...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I'm writing a parser for polynomial equation (ex : 5 x ^2 + 3 x ^ 1 = 3 x ^
2).

I'm using parsec to decompose my parser into smaller parts, and it works,
but all my parser functions have the type :
parser :: Stream s m Char => ParsecT s u m <Type to Parse>

Is there a way to factor out that boilerplate, in such a way that I could
simply write a type like
parser :: Parser <Type to parse>
?

The farther I've been is :
type Parser s = ParsecT s () Identity
type St = Stream s Identity Char (using ConstraintKinds)
parser :: St s => Parser s <Type to Parse>

But that still feels tedious to write every time...

I've tried to search the wiki and the internet in general, but did not find
an answer to that particularly.

Thxs a lot if you have any hindsights.

Max Gautier
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20200324/f1d69fce/attachment-0001.html>

------------------------------

Message: 2
Date: Tue, 24 Mar 2020 17:26:55 +0100
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Factor out parsec types
Message-ID: <20200324162655.GA21197@aspire.station>
Content-Type: text/plain; charset=us-ascii

Hello Max,

On Tue, Mar 24, 2020 at 04:06:03PM +0100, Max Gautier wrote:
> Is there a way to factor out that boilerplate, in such a way that I could
> simply write a type like
> parser :: Parser <Type to parse>
> ?
> 
> The farther I've been is :
> type Parser s = ParsecT s () Identity
> type St = Stream s Identity Char (using ConstraintKinds)
> parser :: St s => Parser s <Type to Parse>
> 
> But that still feels tedious to write every time...

Is there any reason you are using `St s => s ...` rather than a concrete
type?

If not,

    type MyPar a = ParsecT Char () Identity a
    p :: MyPar Int

could be useful. Does that work for you?
-F


------------------------------

Message: 3
Date: Tue, 24 Mar 2020 18:08:42 +0100
From: Max Gautier <ashelia1...@gmail.com>
To: beginners@haskell.org
Subject: [Haskell-beginners] Fwd:  Factor out parsec types
Message-ID:
        <CAB=r4ugs+s2hs9i4gtpx9fpavo-u81tt+pdicu_c9ygfkmg...@mail.gmail.com>
Content-Type: text/plain; charset="UTF-8"

Hi Francesco.

If I understand correctly the parsec interface, leaving the stream
type s as a type variable
allows me to specify the concrete stream type at the time of use
without changing the parser,
as long as that stream gives Char tokens, is that correct ?
And using a concrete stream type forfeit that, does'nt it ?

(So for example in Quickcheck tests in can just use a String as my
stream, and in other contexts something like Text).
(I have not really experimented yet with the various "string" type, so
maybe this is unnecessary (but in that
case, what is the point of ParsecT exposing these type parameters ?))

The main purpose of my project is to learn Haskell, so I'm trying to
understand the full power of the tools
I use^ .


------------------------------

Message: 4
Date: Tue, 24 Mar 2020 19:52:07 +0100
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Fwd:  Factor out parsec types
Message-ID: <20200324185207.GA24457@aspire.station>
Content-Type: text/plain; charset=us-ascii

Before replying, my example should have of course been:

    type MyPar a = ParsecT String () Identity a

On Tue, Mar 24, 2020 at 06:08:42PM +0100, Max Gautier wrote:
> If I understand correctly the parsec interface, leaving the stream
> type s as a type variable
> allows me to specify the concrete stream type at the time of use
> without changing the parser,
> as long as that stream gives Char tokens, is that correct ?
> And using a concrete stream type forfeit that, does'nt it ?

Yes you do forfeit polymorphism using a concrete type.
With your current synonym:

    type Parser s = ParsecT s () Identity

you already missing polymorphism on state (`()`) and a monadic
transformers (`Identity`).

Parsec itself [1] uses a very similar shorthand

    type Parser = Parsec String ()

[1] 
https://hackage.haskell.org/package/parsec-3.1.14.0/docs/Text-Parsec-String.html


I do not think there is a sensible way not to have the constraint
part (`... =>`) written out in full.


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 141, Issue 5
*****************************************

Reply via email to