New extra combinator: generalised 'readMaybe'
- Dominant language
- Haskell
- Stars
- 742
- Forks
- 80
- PR merge metrics
- No merged PRs in 30d
Description
`readMaybe` is implemented like this:
```haskell
readMaybe :: Read a => String -> Maybe a
readMaybe s = case readEither s of
Left _ -> Nothing
Right a -> Just a
```
There's a lot of other types that it would be useful to generalize this to. Here's two ideas I've had:
```haskell
readFail :: (MonadFail f, Read a) => String -> f a
readFail s = case readEither s of
Left e -> fail e
Right a -> return a
```
```haskell
readAlt :: (Alternative f, Read a) => String -> f a
readAlt s = case readEither s of
Left _ -> empty
Right a -> pure a
```
Both of those are the same as `readMaybe` when `f ~ Maybe`. The first is useful for custom error monads to preserve whether the problem was "no parse" or "ambiguous parse". The second is useful for `MaybeT` and lots of other things that have some sort of failure state.
Contributor guide
Research direction
Review the existing readMaybe implementation and the surrounding parsing API. Compare the proposed readFail and readAlt abstractions, determine which behavior and naming fit the library, and define the tests needed to show the selected combinator preserves the intended parsing and failure behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- haskell
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100