Skip to content

Commit

Permalink
Use better implementations for many and some by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkkrp committed Sep 28, 2024
1 parent ca56748 commit 8d1f5cc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
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

0 comments on commit 8d1f5cc

Please sign in to comment.