Skip to content

Redefine MonadFail Gen to call discard#418

Closed
ocharles wants to merge 1 commit into
hedgehogqa:masterfrom
ocharles:patch-2
Closed

Redefine MonadFail Gen to call discard#418
ocharles wants to merge 1 commit into
hedgehogqa:masterfrom
ocharles:patch-2

Conversation

@ocharles

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

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

@ocharles

Copy link
Copy Markdown
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