Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use better implementations for many and some by default #570

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
370](https://github.com/mrkkrp/megaparsec/issues/370).
* Inlined `Applicative` operators `(<*)` and `(*>)`. [PR
566](https://github.com/mrkkrp/megaparsec/pull/566).
* `many` and `some` of the `Alternative` instance of `ParsecT` are now more
efficient, since they use the monadic implementations under the hood.
[Issue 567](https://github.com/mrkkrp/megaparsec/issues/567).

## Megaparsec 9.6.1

Expand Down
6 changes: 5 additions & 1 deletion Text/Megaparsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,16 @@ import Text.Megaparsec.Stream
--
-- Note that we re-export monadic combinators from
-- "Control.Monad.Combinators" because these are more efficient than
-- 'Applicative'-based ones. Thus 'many' and 'some' may clash with the
-- 'Applicative'-based ones (†). Thus 'many' and 'some' may clash with the
-- functions from "Control.Applicative". You need to hide the functions like
-- this:
--
-- > import Control.Applicative hiding (many, some)
--
-- † As of Megaparsec 9.7.0 'Control.Applicative.many' and
-- 'Control.Applicative.some' are as efficient as their monadic
-- counterparts.
--
-- Also note that you can import "Control.Monad.Combinators.NonEmpty" if you
-- wish that combinators like 'some' return 'NonEmpty' lists. The module
-- lives in the @parser-combinators@ package (you need at least version
Expand Down
3 changes: 3 additions & 0 deletions Text/Megaparsec/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ where

import Control.Applicative
import Control.Monad
import qualified Control.Monad.Combinators
import Control.Monad.Cont.Class
import Control.Monad.Error.Class
import qualified Control.Monad.Fail as Fail
Expand Down Expand Up @@ -211,6 +212,8 @@ pAp m k = ParsecT $ \s cok cerr eok eerr ->
instance (Ord e, Stream s) => Alternative (ParsecT e s m) where
empty = mzero
(<|>) = mplus
many = Control.Monad.Combinators.many
some = Control.Monad.Combinators.some

-- | 'return' returns a parser that __succeeds__ without consuming input.
instance (Stream s) => Monad (ParsecT e s m) where
Expand Down