liveBook · Manning

Fail

As explained in Learn Haskell by Example

Fail is an Attoparsec Result constructor representing a failed parse together with remaining input and error information.

Fail is an Attoparsec Result constructor for a parser that cannot complete successfully. It carries the remaining input together with error information, unlike Partial, which carries a continuation for more input. section 12.2.1

Read Attoparsec results by distinguishing their three outcomes:

  • Done reports a successful parse and the unparsed remainder. section 12.2.1
  • Fail reports the unparsed remainder and error details. section 12.2.1
  • Partial requests more input through a continuation rather than declaring failure. section 12.2.1

A failed byte-string parse

Running AP.parse (AP.string "Hello") "hello" produces Fail "hello" [] "string". The parser has not consumed the input, and the result records the failure information. By contrast, parsing "He" produces Partial _, because more input may still complete the match. section 12.2.1

mhead :: (Monad m) => m [a] -> m a
mhead m = do
  (x : _) <- m
  return x

Inspect the Attoparsec REPL examples to compare successful, failed, and incomplete parses. section 12.2.1

How Learn Haskell by Example covers it

How deep it goes
Defines Fail and contrasts it with Done and Partial, but defers detailed error handling and focuses on whole-input parsing.
Examples
Uses AP.parse with matching, mismatching, and incomplete inputs such as "Hello", "hello", and "He".
What it assumes
Requires familiarity with Attoparsec Parser values, parse, and ByteString input.
What you can do after
Evaluate parser results by distinguishing failure from success and incomplete input.
How it connects
Connects Result with Parser, Done, Partial, parse, and parseOnly.

Also explained in

Compare how 2 books explain Fail →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.