[GHC] #12358: Read1/Read2 don't have methods defined in terms of ReadPrec

GHC 8.0 added the [http://hackage.haskell.org/package/base-4.9.0.0/docs /Data-Functor-Classes.html Data.Functor.Classes] module to `base`, and with it the `Read1` and `Read2` typeclasses. The current definition of `Read1` is this:
{{{#!hs class Read1 f where liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a) liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [f a] }}}
There's a pretty big problem with this definition: it uses `ReadS` (a synonym for `String -> [(a, String)]`). This sort of parser is very slow (the docs even [http://hackage.haskell.org/package/base-4.9.0.0/docs/Text- ParserCombinators-ReadP.html#t:ReadS admit as such]), and moreover, the actual `Read` typeclass on which Read1 is based tries to avoid using it whenever possible.
The `Read` typeclass has this definition currently:
{{{#!hs class Read a where readsPrec :: Int -> ReadS a readList :: ReadS [a] readPrec :: ReadPrec a readListPrec :: ReadPrec [a] }}}
Where `ReadPrec` is a much more efficient parser datatype. When deriving `Read` instances, GHC defines them in terms of `readPrec`, and gives the other methods default definitions that leverage `readPrec`.
For the sake of consistency, I propose adding analogous methods to `Read1` and `Read2` that use the `ReadPrec` datatype. For example, here is how I would change `Read1`:
{{{#!hs class Read1 f where liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a) liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [f a] liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (f a) liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [f a] }}}
And similarly for `Read2`. Here is a [https://gist.github.com/RyanGlScott/7cdd11d6aa878e4229acf1a682beb1fc full gist] with a sketch of what the new `Read1`/`Read2` definitions would look
#12358: Read1/Read2 don't have methods defined in terms of ReadPrec -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: Type: feature | Status: new request | Priority: normal | Milestone: 8.2.1 Component: | Version: 8.0.1 libraries/base | Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- Original Haskell libraries mailing list discussion: https://mail.haskell.org/pipermail/libraries/2016-June/027102.html like, including what the default definitions of the other methods would be. Diff coming soon. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12358 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12358: Read1/Read2 don't have methods defined in terms of ReadPrec -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: Type: feature request | Status: patch Priority: normal | Milestone: 8.2.1 Component: libraries/base | Version: 8.0.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2379 Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * status: new => patch * differential: => Phab:D2379 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12358#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12358: Read1/Read2 don't have methods defined in terms of ReadPrec -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: Type: feature request | Status: patch Priority: normal | Milestone: 8.2.1 Component: libraries/base | Version: 8.0.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2379 Wiki Page: | -------------------------------------+------------------------------------- Comment (by simonpj): Are you liaising with the Core Libraries Committee on this? I didn't even know about `Read1` and `Read2`! Simon PS: does kind polymorphism help? What about `Read3`, `Read4`...? I suppose that's another story though. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12358#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12358: Read1/Read2 don't have methods defined in terms of ReadPrec -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: Type: feature request | Status: patch Priority: normal | Milestone: 8.2.1 Component: libraries/base | Version: 8.0.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2379 Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * cc: core-libraries-committee@… (added) Comment: Sorry, I should know by now to forward the CLC on things like this, seeing as how I'm on the CLC myself :) For now, I only have the above [https://mail.haskell.org/pipermail/libraries/2016-June/027102.html mailing list discussion] as evidence that this move is supported by the community. Anyways, adding `Read1` and `Read2` was decided in #11135 (associated mailing list discussion [https://mail.haskell.org/pipermail/libraries/2015-July/026014.html here]). `Read1/2` were originally defined in `transformers`, but migrated to `base` as part of an effort to bring in `Compose`/`Sum`/`Product`/`MonadIO` (and remove things from `transformers` that aren't actually, y'know, monad transformers). Ross's original definition of `Read1/2` uses `ReadS`, since that's what the report [https://www.haskell.org/onlinereport/basic.html#sect6.3.3 dictates] the definition of `Read` should use, so that's what was migrated to `base`. It didn't occur to me at the time the problems that would pose when I began to work on [https://github.com/haskell-compat/deriving- compat/issues/3 automatic derivation] of `Read1/2` instances, since GHC's derived `Read` instances use `ReadPrec` instead of `ReadS`. This ticket just serves as a way to make the methods of `Read1/2` parallel to those of `Read`, and to eventually make deriving `Read1/2` easier. (P.S., if you're curious about whether kind polymorphism can obviate the need for multiple `Read(n)` classes, you might be interested in the discussion [https://github.com/haskell/deepseq/issues/8 here]. phadej has proposed one way to solve such a problem, although it involves some eyebrow-raising `-XTypeInType` tricks.) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12358#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12358: Read1/Read2 don't have methods defined in terms of ReadPrec -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: Type: feature request | Status: patch Priority: normal | Milestone: 8.2.1 Component: libraries/base | Version: 8.0.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2379 Wiki Page: | -------------------------------------+------------------------------------- Comment (by ekmett): Makes sense to me. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12358#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12358: Read1/Read2 don't have methods defined in terms of ReadPrec -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: Type: feature request | Status: patch Priority: normal | Milestone: 8.2.1 Component: libraries/base | Version: 8.0.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2379 Wiki Page: | -------------------------------------+------------------------------------- Comment (by ekmett): Regarding the committee: Ryan joined the committee back in January or so. I like to think of him as our "backwards compatibility specialist." Neil Mitchell joined around the same time. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12358#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12358: Read1/Read2 don't have methods defined in terms of ReadPrec
-------------------------------------+-------------------------------------
Reporter: RyanGlScott | Owner:
Type: feature request | Status: patch
Priority: normal | Milestone: 8.2.1
Component: libraries/base | Version: 8.0.1
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture:
| Unknown/Multiple
Type of failure: None/Unknown | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s): Phab:D2379
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Ben Gamari

#12358: Read1/Read2 don't have methods defined in terms of ReadPrec -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: Type: feature request | Status: closed Priority: normal | Milestone: 8.2.1 Component: libraries/base | Version: 8.0.1 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2379 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: patch => closed * resolution: => fixed Comment: Ryan, is this something you would like to have in 8.0.2? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12358#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12358: Read1/Read2 don't have methods defined in terms of ReadPrec -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: Type: feature request | Status: closed Priority: normal | Milestone: 8.2.1 Component: libraries/base | Version: 8.0.1 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2379 Wiki Page: | -------------------------------------+------------------------------------- Comment (by RyanGlScott): No, this can wait until 8.2. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12358#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC