Use better implementations for many and some by default

This commit is contained in:
Mark Karpov 2024-09-28 20:50:22 +02:00
parent ca56748247
commit 8d1f5cc35e
3 changed files with 11 additions and 1 deletions

View File

@ -7,6 +7,9 @@
370](https://github.com/mrkkrp/megaparsec/issues/370). 370](https://github.com/mrkkrp/megaparsec/issues/370).
* Inlined `Applicative` operators `(<*)` and `(*>)`. [PR * Inlined `Applicative` operators `(<*)` and `(*>)`. [PR
566](https://github.com/mrkkrp/megaparsec/pull/566). 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 ## Megaparsec 9.6.1

View File

@ -130,12 +130,16 @@ import Text.Megaparsec.Stream
-- --
-- Note that we re-export monadic combinators from -- Note that we re-export monadic combinators from
-- "Control.Monad.Combinators" because these are more efficient than -- "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 -- functions from "Control.Applicative". You need to hide the functions like
-- this: -- this:
-- --
-- > import Control.Applicative hiding (many, some) -- > 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 -- Also note that you can import "Control.Monad.Combinators.NonEmpty" if you
-- wish that combinators like 'some' return 'NonEmpty' lists. The module -- wish that combinators like 'some' return 'NonEmpty' lists. The module
-- lives in the @parser-combinators@ package (you need at least version -- lives in the @parser-combinators@ package (you need at least version

View File

@ -46,6 +46,7 @@ where
import Control.Applicative import Control.Applicative
import Control.Monad import Control.Monad
import qualified Control.Monad.Combinators
import Control.Monad.Cont.Class import Control.Monad.Cont.Class
import Control.Monad.Error.Class import Control.Monad.Error.Class
import qualified Control.Monad.Fail as Fail import qualified Control.Monad.Fail as Fail
@ -211,6 +212,8 @@ pAp m k = ParsecT $ \s cok cerr eok eerr ->
instance (Ord e, Stream s) => Alternative (ParsecT e s m) where instance (Ord e, Stream s) => Alternative (ParsecT e s m) where
empty = mzero empty = mzero
(<|>) = mplus (<|>) = mplus
many = Control.Monad.Combinators.many
some = Control.Monad.Combinators.some
-- | 'return' returns a parser that __succeeds__ without consuming input. -- | 'return' returns a parser that __succeeds__ without consuming input.
instance (Stream s) => Monad (ParsecT e s m) where instance (Stream s) => Monad (ParsecT e s m) where