Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redefine MonadFail Gen to call discard #418

Closed
wants to merge 1 commit into from

Conversation

ocharles
Copy link
Contributor

I find it very counterintuitive that we can't have failing pattern matches act like discard when generating values.

To provide an example of where this is useful, I'm trying to generate trees of well typed terms. Some types have a rich structure that lets me learn about more type class constrains. In my list of alternatives, I would like to write:

        union = do
          Just Dict <- return (isEqTable t)
          Union <$> genQuery t <*> genQuery t 

But I can't do this, because if isEqTable returns Nothing this whole generator just calls error, rather than not considering this alternative. Instead, I have to write

          Dict <- just $ pure $ isEqTable t
          Union <$> genQuery t <*> genQuery t 

which I find a little more clunky (and this doesn't scale to other patterns).

I find it very counterintuitive that we can't have failing pattern matches act like `discard` when generating values. 

To provide an example of where this is useful, I'm trying to generate trees of well typed terms. Some types have a rich structure that lets me learn about more type class constrains. In my list of alternatives, I would like to write:

```haskell
        union = do
          Just Dict <- return (isEqTable t)
          Union <$> genQuery t <*> genQuery t 
```

But I can't do this, because if `isEqTable` returns `Nothing` this whole generator just calls `error`, rather than not considering this alternative. Instead, I have to write

```haskell
          Dict <- just $ pure $ isEqTable t
          Union <$> genQuery t <*> genQuery t 
```

which I find a little more clunky (and this doesn't scale to other patterns).
@ocharles
Copy link
Contributor Author

Alternatively I would propose this instance be entirely removed, as calling error is never the right implementation imo

@ocharles
Copy link
Contributor Author

Duplicate of #257

@ocharles ocharles closed this Mar 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant