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.  How to write Read instance (Baa)
   2. Re:  How to write Read instance (David McBride)
   3. Re:  How to write Read instance (Baa)
   4. Re:  How to write Read instance (David McBride)


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

Message: 1
Date: Thu, 2 Nov 2017 15:41:21 +0200
From: Baa <aqua...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] How to write Read instance
Message-ID: <20171102154121.6535e6ac@Pavel>
Content-Type: text/plain; charset=US-ASCII

Hello all!

I found some errors in the reading of previously shown big and
complex record. Reason is: some of fields are reading wrongly. So, is
there any useful documents how to write `Read` instance correctly
(because I can't find good tutorial in the Web and often hit errors
with `Read` instance)? May be tutorial/examples/any good info...


===
Best regards, Paul


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

Message: 2
Date: Thu, 2 Nov 2017 10:27:12 -0400
From: David McBride <toa...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] How to write Read instance
Message-ID:
        <can+tr41uxx59uxnyuav5rz3fc_k6zcvxdhm6bnwxqkppym7...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

The most common way is to just auto derive Read.  I'm not sure that that
ever really fails.  Are you sure the problem isn't with the Show instance
of the type?  People commonly write invalid Show instances to make them
look pretty and they shouldn't.  read and show are supposed to be inverses
of each other because when they aren't, problems like this occur.

The simple way to do a Read instance is to implement the reads function for
it.  The confusion comes from its type.  readsPrec :: Int -> ReadS a.
ReadS is defined as String -> [(a, String)], where a is the parsed result
and String is the rest of the string that is being parsed, , which may look
confusing, and the Int is precedence, which can usually be ignored.  It
could have been Int -> String -> Maybe (a, String), but Read predates
Maybe.  So instead it returns a list and if it fails to parse, it returns
[] instead of Nothing.  So.

data MyFoo = MyFoo

instance Read MyFoo where
  -- readsPrec :: Int -> String -> [(MyFoo, String)]
  readsPrec _ = readFoo

readFoo :: String -> [(MyFoo, String)]
readFoo str = case splitAt 5 str of
  ("MyFoo", rest) -> [(MyFoo, rest)]
  otherwise -> []

If you need something more complex, there are functions to do it in base
that perform lexing and parsing.  I have never used them but you can go
ahead and read some of the instances such as Ordering at
https://hackage.haskell.org/package/base-4.10.0.0/docs/src/GHC.Read.html#line-398
to try and learn how it might work for you.

But honestly I think you should look at fixing your Show instance first, if
possible.


On Thu, Nov 2, 2017 at 9:41 AM, Baa <aqua...@gmail.com> wrote:

> Hello all!
>
> I found some errors in the reading of previously shown big and
> complex record. Reason is: some of fields are reading wrongly. So, is
> there any useful documents how to write `Read` instance correctly
> (because I can't find good tutorial in the Web and often hit errors
> with `Read` instance)? May be tutorial/examples/any good info...
>
>
> ===
> Best regards, Paul
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20171102/3d788e54/attachment-0001.html>

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

Message: 3
Date: Thu, 2 Nov 2017 16:51:06 +0200
From: Baa <aqua...@gmail.com>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] How to write Read instance
Message-ID: <20171102165106.4f61f6f0@Pavel>
Content-Type: text/plain; charset=US-ASCII

Hello, David!

`Show` instance is simple:

  instance Show Hex where
    show = T.unpack . ghValue

so

  > show (Hex "1234ab")
  1234ab
  > read "1234ab"::Hex
  1234ab
  > read "Just 1234ab"::Maybe Hex -- fails like wrapped in the record!!

Yes, I'm ignoring precedence usually too. And I return rest of the
string. Playing with `UTCTime`'s readsPrec shows me that behaviour
looks the same: UTCTime's reader returns the same rest. So, may be
trick is in the:

1. Precedence ?!
2. readListPrec = readListPrecDefault
   readList     = readListDefault   ?

   But I get error: readListPrec is not visible method of class Read...
3. readPrec ? I implemented readsPrec which is old-style, but am I
   right that old-style and new-style are absolutely the same from
   reading point of view and old-style can replace new-style and vice
   versa?

PS. I don't import any special modules.


> The most common way is to just auto derive Read.  I'm not sure that
> that ever really fails.  Are you sure the problem isn't with the Show
> instance of the type?  People commonly write invalid Show instances
> to make them look pretty and they shouldn't.  read and show are
> supposed to be inverses of each other because when they aren't,
> problems like this occur.
> 
> The simple way to do a Read instance is to implement the reads
> function for it.  The confusion comes from its type.  readsPrec ::
> Int -> ReadS a. ReadS is defined as String -> [(a, String)], where a
> is the parsed result and String is the rest of the string that is
> being parsed, , which may look confusing, and the Int is precedence,
> which can usually be ignored.  It could have been Int -> String ->
> Maybe (a, String), but Read predates Maybe.  So instead it returns a
> list and if it fails to parse, it returns [] instead of Nothing.  So.
> 
> data MyFoo = MyFoo
> 
> instance Read MyFoo where
>   -- readsPrec :: Int -> String -> [(MyFoo, String)]
>   readsPrec _ = readFoo
> 
> readFoo :: String -> [(MyFoo, String)]
> readFoo str = case splitAt 5 str of
>   ("MyFoo", rest) -> [(MyFoo, rest)]
>   otherwise -> []
> 
> If you need something more complex, there are functions to do it in
> base that perform lexing and parsing.  I have never used them but you
> can go ahead and read some of the instances such as Ordering at
> https://hackage.haskell.org/package/base-4.10.0.0/docs/src/GHC.Read.html#line-398
> to try and learn how it might work for you.
> 
> But honestly I think you should look at fixing your Show instance
> first, if possible.
> 
> 
> On Thu, Nov 2, 2017 at 9:41 AM, Baa <aqua...@gmail.com> wrote:
> 
> > Hello all!
> >
> > I found some errors in the reading of previously shown big and
> > complex record. Reason is: some of fields are reading wrongly. So,
> > is there any useful documents how to write `Read` instance correctly
> > (because I can't find good tutorial in the Web and often hit errors
> > with `Read` instance)? May be tutorial/examples/any good info...
> >
> >
> > ===
> > Best regards, Paul
> > _______________________________________________
> > Beginners mailing list
> > Beginners@haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> >  



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

Message: 4
Date: Thu, 2 Nov 2017 11:05:37 -0400
From: David McBride <toa...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] How to write Read instance
Message-ID:
        <CAN+Tr41Rx_zsFvKy1epYk6sLL5roXph6vHT85K=6a111rbw...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

My own toy example works.  I don't know how yours differs.

On Thu, Nov 2, 2017 at 10:51 AM, Baa <aqua...@gmail.com> wrote:

> Hello, David!
>
> `Show` instance is simple:
>
>   instance Show Hex where
>     show = T.unpack . ghValue
>
> so
>
>   > show (Hex "1234ab")
>   1234ab
>   > read "1234ab"::Hex
>   1234ab
>   > read "Just 1234ab"::Maybe Hex -- fails like wrapped in the record!!
>
> Yes, I'm ignoring precedence usually too. And I return rest of the
> string. Playing with `UTCTime`'s readsPrec shows me that behaviour
> looks the same: UTCTime's reader returns the same rest. So, may be
> trick is in the:
>
> 1. Precedence ?!
> 2. readListPrec = readListPrecDefault
>    readList     = readListDefault   ?
>
>    But I get error: readListPrec is not visible method of class Read...
> 3. readPrec ? I implemented readsPrec which is old-style, but am I
>    right that old-style and new-style are absolutely the same from
>    reading point of view and old-style can replace new-style and vice
>    versa?
>
> PS. I don't import any special modules.
>
>
> > The most common way is to just auto derive Read.  I'm not sure that
> > that ever really fails.  Are you sure the problem isn't with the Show
> > instance of the type?  People commonly write invalid Show instances
> > to make them look pretty and they shouldn't.  read and show are
> > supposed to be inverses of each other because when they aren't,
> > problems like this occur.
> >
> > The simple way to do a Read instance is to implement the reads
> > function for it.  The confusion comes from its type.  readsPrec ::
> > Int -> ReadS a. ReadS is defined as String -> [(a, String)], where a
> > is the parsed result and String is the rest of the string that is
> > being parsed, , which may look confusing, and the Int is precedence,
> > which can usually be ignored.  It could have been Int -> String ->
> > Maybe (a, String), but Read predates Maybe.  So instead it returns a
> > list and if it fails to parse, it returns [] instead of Nothing.  So.
> >
> > data MyFoo = MyFoo
> >
> > instance Read MyFoo where
> >   -- readsPrec :: Int -> String -> [(MyFoo, String)]
> >   readsPrec _ = readFoo
> >
> > readFoo :: String -> [(MyFoo, String)]
> > readFoo str = case splitAt 5 str of
> >   ("MyFoo", rest) -> [(MyFoo, rest)]
> >   otherwise -> []
> >
> > If you need something more complex, there are functions to do it in
> > base that perform lexing and parsing.  I have never used them but you
> > can go ahead and read some of the instances such as Ordering at
> > https://hackage.haskell.org/package/base-4.10.0.0/docs/
> src/GHC.Read.html#line-398
> > to try and learn how it might work for you.
> >
> > But honestly I think you should look at fixing your Show instance
> > first, if possible.
> >
> >
> > On Thu, Nov 2, 2017 at 9:41 AM, Baa <aqua...@gmail.com> wrote:
> >
> > > Hello all!
> > >
> > > I found some errors in the reading of previously shown big and
> > > complex record. Reason is: some of fields are reading wrongly. So,
> > > is there any useful documents how to write `Read` instance correctly
> > > (because I can't find good tutorial in the Web and often hit errors
> > > with `Read` instance)? May be tutorial/examples/any good info...
> > >
> > >
> > > ===
> > > Best regards, Paul
> > > _______________________________________________
> > > Beginners mailing list
> > > Beginners@haskell.org
> > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> > >
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20171102/13cb1774/attachment.html>

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 113, Issue 1
*****************************************

Reply via email to