-
Notifications
You must be signed in to change notification settings - Fork 15
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
Would a Monus class allow defining a Quotient Semifield over non-negative numbers? #173
Comments
Thanks for your interest and kind words. We don't have anything as principled in Haskell either! Sadly, the Num, Rational, Real and friends are baked into the prelude, and this make it very difficult to shift habits.
Yes, and I also agree that Subtraction as a constraint of QuotientField is a wart. Presaging both of these is defining Positive - the closed set of non-negative numbers as you say. In Haskell, there's always the unwrapping and wrapping of newtypes that gets in the way of something such as this. You're working with a double, take the magnitude, and some fancy library wraps the answer in a newtype Positive that you then have to unwrap etc etc.
You could put the Mag type as an extra parameter to Basis (moving away form TypeFamilies), but then the user invariably has to write Back to Monus, I think the major design decisions would be:
|
I really like your But this begs the question, how to work with the magnitude, which is always non-negative. NumHask feels a bit incomplete atm in that it doesnt have a complete toolkit for closed arithmetic on the magnitudes. Monus & MSemiring (?) feel like a nice answer here. re " abstract the Positive type to a more general (open or closed) "Interval" type", there's a few intervals that seem perennially useful:
|
Apologies for going ahead and getting some code laid. I found I couldn't think about it without something concrete. |
Beautiful seeing the maths come to life, and so elegantly. Gave me Haskell envy just reading it. MSemifield is certainly a thing now, there's an executable existence proof.. |
Sitting on this for a bit. I feel like saying that Saying |
After a bit more tweaking, QuotientField doesn't need Monus at all for a good Positive instance. This leaves Monus floating around a bit, prior to working out if it can be integrated with Basis. Until then saying |
My interpretation is that in common language, when talking about
"subtraction" of non negative quantities, we actually mean monus, not
minus, but don't bother to distinguish. So I'm for treating it as a
separate operation.
…On Sun, 31 Dec 2023, 4:21 pm Tony Day, ***@***.***> wrote:
#175 <#175>
After a bit more tweaking, QuotientField doesn't need Monus at all for a
good Positive instance.
This leaves Monus floating around a bit, prior to working out if it can be
integrated with Basis. Until then saying 4 ∸ 7 might allay my concerns
that it could be misinterpreted.
—
Reply to this email directly, view it on GitHub
<#173 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAXJZDKD4CL3SYCCJANLBDYMDY5RAVCNFSM6AAAAAA7A5R3FGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZSGY3TIMRWGM>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
So I finally found time to go through #175 and understand how you built a quotient field off a semifield. I have one question.
I feel like a variant of round that rounds towards zero/smaller magnitude (like |
That is a default signature, and the implementation for Positive overrides this, here: https://github.com/tonyday567/numhask/blob/ghc-9.8.1/src/NumHask/Data/Positive.hs#L100 I tried to wrangle the subtractive out of the default but it really does need the negate. |
Got it. Thanks for explaining. As a Scala native, I didn't understand how DefaultSignatures works and that the default method can include more constraints than explicitly provided methods. Now I'm actually curious/doubtful if such is possible in Scala, where TCs are modelled as implicitly passed traits, and instances as trait implementations.. 🤔 |
It tends to work in practice and becomes very comfortable. Just part of the ghc api, I suppose. I might merge this and do a major bump. |
I was thinking and reading on this a little more. I do think semantically there's a distinction between rounding down (towards negative infinity) vs rounding towards zero. And it may be worth a separately named method on For PS
|
I, a self-described rusted-on Scala programmer, spent today studying (er.. admiring) NumHask for inspiration. We don't have anything as nice or as principled in Scala.
Spire is the closest thing and quite praiseworthy. Nonetheless, NumHask is considerably more principled in some areas, one being the definition of rounding functions in
QuotientField
. Spire puts these in anIsReal
type class thatIsReal
y as lawless as the wild west.But one aspect of
QuotientField
that troubles me is theSubtractive
requirement, via Field, which implies that to support round/floor/ceiling, a number system must admit negative numbers. That clashes with my real-world common sense that says that we ought to be able to sensibly define round/floor/ceiling on non-negative rationals, naturals etc.Possibly, a QuotientSemifield built on top of Semifield + Monus (truncating subtraction) is an answer. I suspect there's an abstraction there that can be lawfully defined on a closed set of non-negative numbers.
I also think Monus itself is useful for safely decreasing Magnitudes generally without straying outside the positive domain.
The text was updated successfully, but these errors were encountered: