-
Notifications
You must be signed in to change notification settings - Fork 9
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
Algebra in core #254
Algebra in core #254
Conversation
@armanbilge my port for |
It looks like the compiler is failing because it cannot find |
I guess we can add so-called "alley-cat" instances, that are kind of ok but not really lawful. Some discussion in typelevel/algebra#247 (comment). |
I can probably punt Int and Long back to scala.math.Integral, for now. alley-cats instances seems like a reasonable thing to add |
I think the bigger issue here is Let me check what spire does. |
Yeah, spire doesn't use So personally I'm 👎 on using |
It could be cleaner to just support In coulomb, there is a parallel distinction where unit conversions are concerned. Possibly there is some analogy relating to unit conversions involving integral quotients. Unsure what such a policy might be, possibly:
Having grown up in a world where My inclination would be to use |
It's because algebra distinguishes between
I think it's fine to go with |
|
Good observation :)
|
There are two main changes proposed on this PR
"explicit truncation" takes two main forms.
The unit tests provide a feel for what all this looks like in the code. |
inline given ctx_TVC_Long[VF](using num: Fractional[VF]): TruncatingValueConversion[VF, Long] = | ||
new TruncatingValueConversion[VF, Long]: | ||
def apply(v: VF): Long = num.toLong(v) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bikeshed for another issue/PR, but I'm concerned that inline
combined with new ...
is sub-optimal and can lead to a proliferation of anonymous classes in the jar. It depends on whether the compiler will automatically recognize this is a SAM and use invokedynamic
instead. It might be better to implement this as a lambda e.g. v => num.toLong(v)
to encourage this optimization.
I'm less familiar with Scala.js internals but this could be especially bad for JavaScript actually.
See typelevel/cats#3871 for a discussion of this topic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it's not obvious to me why inline
is needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not assume I'm using these only when they are needed 😁
I'm still working out my intuitions about when inline
and transparent inline
are necessary. Situations where a typeclass involved dependent typing seem unambiguous - they need transparent inline
. Others are less obvious (to me).
Some places I am definitely using inline
for efficiency gains, but other places it may be a bad trade
At any rate, if inline
is problematic, I'm happy to clean all that up where it is not needed. Having some decent unit test coverage should make this a bit easier now. If something introduces a compile error, it ought to show up pretty fast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I would use inline
where needed only for its compile-time semantics.
For runtime performance, both the JVM and the Scala.js linker backend do a fantastic job of inlining themselves.
We can always revisit inline
opportunities in the future with a benchmark suite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have had bad experiences with macro generated classes in the past. When defining boopickle generated serialized it was very easy to endup generating thousands of instances that would bloat the resulting js size. It wasn't really noticeable on the jvm thus it was quite hard to spot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overall I expect scala-3 coulomb to be better in this regard. I am using metaprogramming in multiple places where I was using intermediate typeclasses, and so these intermediate typeclasses should no longer appear in byte code. However, some of this may be offset by use of transparent inline
, so it will definitely be helpful to remove any unimportant usages of inline
transparent inline def tquot[VR, UR](qr: Quantity[VR, UR])(using tq: TQuot[VL, UL, VR, UR]): Quantity[tq.VO, tq.UO] = | ||
tq(ql.value, qr.value).withUnit[tq.UO] | ||
|
||
transparent inline def pow[P](using pow: Pow[VL, UL, P]): Quantity[pow.VO, pow.UO] = | ||
pow(ql.value).withUnit[pow.UO] | ||
|
||
transparent inline def tpow[P](using tp: TPow[VL, UL, P]): Quantity[tp.VO, tp.UO] = | ||
tp(ql.value).withUnit[tp.UO] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding the infix
modifier?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might. In scala 3 it's almost as easy to just use backquotes if you really want to go infix
q1 `tquot` q2
but will consider
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I learned that one today :)
👍 thank you so much for pursuing this. I think an algebra dependency in core definitely pays off. The unit tests look really, really good :) the implementation complexity feels a bit overwhelming tbh, but it's well hidden from the user experience AFAICT. |
It looks great. I think having to explicity say when you truncate is an overall win |
I'm glad you like it 🎉 It will be a couple days before I can get back to this. I'm going to stew on it and merge it when I can get back to it and refactor the algebra bits a little. |
refactorings relating to the use of |
port operations to algebraic typeclasses defined in
algebra.ring