kowainik / kowainik/relude

New extra combinator: generalised 'readMaybe'

Open
#161 3 comments 0 reactions 0 assignees View on GitHub
enhancement new question reexport
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

Open the contributing 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.