Skip to content

Provide generators for num structs#160

Open
echoumcp1 wants to merge 24 commits intohegeldev:mainfrom
echoumcp1:echoumcp1/impl-num
Open

Provide generators for num structs#160
echoumcp1 wants to merge 24 commits intohegeldev:mainfrom
echoumcp1:echoumcp1/impl-num

Conversation

@echoumcp1
Copy link
Copy Markdown
Member

@echoumcp1 echoumcp1 commented Mar 30, 2026

Closes #91

@echoumcp1
Copy link
Copy Markdown
Member Author

echoumcp1 commented Mar 30, 2026

@Liam-DeVoe I will keep iterating on this. The rationals generator seems a bit annoying to use, but not sure what a more ergonomic design would look like atm.

@echoumcp1 echoumcp1 marked this pull request as ready for review April 2, 2026 17:59
@echoumcp1 echoumcp1 requested a review from DRMacIver April 2, 2026 18:02
Copy link
Copy Markdown
Member

@DRMacIver DRMacIver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love the API. If this is the best we can do because of rust limitations then so be it, but I don't think it is and I'd really rather these feel more contiguous with the main API if possible.

Comment thread .github/coverage-ratchet.json Outdated
@@ -1,3 +1,3 @@
{
"nocov": 521
"nocov": 548
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did the ratchet go up? I'd expect everything in this PR to be coverable, and the goal of the ratchet is to drive coverage to 100% so introducing new untested code is to be avoided wherever possible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bumped it too high by accident. Misread 540 as 548. Some are just uncovered closing braces. In cbor_to_bigint I added 3 no covs under the uninteresting panic cases.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think any of them should be uncovered closing braces - the script already filters those out, and should auto-ratchet the rest down.

Fair enough RE the uninteresting panic cases. I'd like to get us covering those in general but you shouldn't have to do that in this branch.

Comment thread src/generators/num.rs Outdated
Comment thread src/generators/num.rs Outdated
Copy link
Copy Markdown
Member

@DRMacIver DRMacIver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I'd ideally like the as_basic feature before merging, but it's not critical, and I don't need to rereview before merging.

Comment thread src/generators/num.rs
}
}

/// Set the denominator generator. Must not produce zero.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be better to handle the zero case.

I think there are a couple of principled things we could do here. Adding a filter would make this non-basic in cases where it should be basic, but we could add an assume or it might actually make sense to just replace zeroes with one.

Comment thread src/generators/num.rs
}
}

impl<T, NG, DG> Generator<Ratio<T>> for RationalGenerator<NG, DG>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good for this to implement as_basic, because whenever numer_gen and denom_gen are basic, this can also be basic.

Copy link
Copy Markdown
Member

@Liam-DeVoe Liam-DeVoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The design of these generators accepts sub-generators: rationals(gen1, gen2); complex(gen1, gen2). Hypothesis doesn't do this, and instead accepts parameters controlling the domain of the generators: complex(min_magnitude=..., max_magnitude=...), fractions(min_value=..., maxv_value=...).

Accepting sub-generators gives Hegel substantially less control over the distribution, which we want because most users are going to pass bad generators. I think we should change to the API design of Hypothesis.

@echoumcp1
Copy link
Copy Markdown
Member Author

The design of these generators accepts sub-generators: rationals(gen1, gen2); complex(gen1, gen2). Hypothesis doesn't do this, and instead accepts parameters controlling the domain of the generators: complex(min_magnitude=..., max_magnitude=...), fractions(min_value=..., maxv_value=...).

Accepting sub-generators gives Hegel substantially less control over the distribution, which we want because most users are going to pass bad generators. I think we should change to the API design of Hypothesis.

Two paths here I think. One is to add fractions() to the core and call into that. The other is to generate a denominator and generator a numerator within the range given the min/max value bounds, but we lose as_basic and of course it's two calls to the server. I'm leaning towards the former since calling into the server is really slow.

@Liam-DeVoe
Copy link
Copy Markdown
Member

I'd add fractions and complex into the protocol, @DRMacIver thoughts?

@DRMacIver
Copy link
Copy Markdown
Member

I'd add fractions and complex into the protocol, @DRMacIver thoughts?

Seems basically reasonable to me yeah.

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.

Provide generators for num structs

3 participants