AndrasKovacs / AndrasKovacs/flatparse

There is no MonadFail for `ParserT`

Open
#75 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
178
Forks
18
PR merge metrics
No merged PRs in 30d

Description

Referencing Issue #11, it was decided to add Alternative and MonadPlus. Is there any reason not to add MonadFail.

My use case is that I have parsed a value, and therefore I know it is valid to operate on the internal data type, but I have to convince Haskell that this is true.

Example parser data structure (taken from this repos BasicLambda example):

-- |
-- A term in the language. The precedences of different constructs are the following, in decreasing
-- order of strength:
--
-- * Identifiers, literals and parenthesized expressions
-- * Function application (left assoc)
-- * Multiplication (left assoc)
-- * Addition (left assoc)
-- * Equality, less-than (non-assoc)
-- * @lam@, @let@, @if@ (right assoc)
data Tm
    = -- | @x@
      Var Name
    | -- | @t u@
      App Tm Tm
    | -- | @lam x. t@
      Lam Name Tm
    | -- | @let x = t in u@
      Let Name Tm Tm
    | -- | @true@ or @false@.
      BoolLit Bool
    | -- | A positive `Int` literal.
      IntLit Int
    | -- | @if t then u else v@
      If Tm Tm Tm
    | -- | @t + u@
      Add Tm Tm
    | -- | @t * u@
      Mul Tm Tm
    | -- | @t == u@
      Eq Tm Tm
    | -- | @t < u@
      Lt Tm Tm
    deriving (Show)

-- | Parse a `Lam`.
lam :: Parser Tm
lam = do
    $(keyword "lam")
    x <- ident'
    $(symbol' ".")
    t <- tm'
    pure $ Lam x t

parseLam :: Parser Tm
parseLam = lam `cut` [Msg "bad lam"]

Suppose that I am have parsed a Lam Name Tm and want to do something with the Name. I just parsed this, so I know it exists as a Lam and has the correct structure.

parseLamAndCapitalize :: Parser Tm
parseLamAndCapitalize = do
    myLam@(Lam n tm) <- parseLam -- MonadFail does not exist for ParserT ...
    ...

Please understand that this is a simple, minimal example to cause a MonadFail compiler error to show. The goal is to be able to do as-pattern binding when you know for a fact the type is what you are saying it should be.

Is there a performance reason why there is no MonadFail instance for ParserT?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the ParserT definition and the BasicLambda example referenced in the issue. Check the existing Alternative and MonadPlus instances, then determine whether adding MonadFail is appropriate and verify the as-pattern example compiles without the reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.