Coder Social home page Coder Social logo

Comments (4)

natefaubion avatar natefaubion commented on June 29, 2024

Can these all be written in terms of MonadError? Why should they be specialized to ParserT?

from purescript-parsing.

jamesdbrock avatar jamesdbrock commented on June 29, 2024

Can these all be written in terms of MonadError?

Yes, in like one line.

Why should they be specialized to ParserT?

Mostly as documentation to encourage users to “parse, don’t validate?”

from purescript-parsing.

natefaubion avatar natefaubion commented on June 29, 2024

To clarify, if they are implemented only with MonadError, then their utility is far greater than just ParserT, and it would be worth potentially upstreaming them to transformers if they don't exist.

from purescript-parsing.

jamesdbrock avatar jamesdbrock commented on June 29, 2024

Why should they be specialized to ParserT?

In Control.Monad.Error.Class we have

-- | Lift a `Maybe` value to a MonadThrow monad.
liftMaybe :: forall m e a. MonadThrow e m => e -> Maybe a -> m a

-- | Lift an `Either` value to a MonadThrow monad.
liftEither :: forall m e a. MonadThrow e m => Either e a -> m a

It's awkward for parsing users to use these functions because the type for e would be ParseError, which has constructor ParseError String Position. We don't want to require users to supply the Position argument of the constructor. We would rather specialize the e type to String and use the current parsing position.

Also it can be expensive to construct Strings, especially on a parsing hot path where we expect a parsing failure to be rare, so in liftMaybe it makes sense to defer the String construction.

So with those adjustments we get

liftMaybe :: forall s m a. Monad m => (Unit -> String) -> Maybe a -> ParserT s m a
liftEither :: forall s m a. Monad m => Either String a -> ParserT s m a

As for liftExceptT (and liftMaybeT?) it”s the same as the above, with the stipulation that if the user needs some special base monad feature for parsing then maybe they also need the special base monad feature for validation. This could be true while parsing DataViews with a base monad Effect, for example.

So that would be

liftMaybeT :: forall s m a. Monad m => (Unit -> String) -> MaybeT m a -> ParserT s m a
liftExceptT :: forall s m a. Monad m => ExceptT String m a -> ParserT s m a

Those two functions don't seem to exist in transformers already but if they did then they would look like

-- | Lift a `MaybeT` value to a MonadThrow monad.
liftMaybeT :: forall m e a. MonadThrow e m => e -> MaybeT m a -> m a

-- | Lift an `ExceptT` value to a MonadThrow monad.
liftExceptT :: forall m e a. MonadThrow e m => ExceptT e m a -> m a

from purescript-parsing.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.