Separate exact and inexact division
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Lĩnh vực
- backend-api-design
Hướng nghiên cứu
Start by reading the EuclideanRing and Field class definitions and the existing division, modulo, gcd, and lcm implementations and tests. The issue proposes changing class constraints and operator aliases, but leaves key behavior and compatibility decisions open. Done would require a settled design, updated instances and tests, and agreement on the breaking change.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
This would be a pretty seismic breaking change, so I would not be at all surprised if we end up deciding to close this issue on this basis, but it comes up semi-frequently and I think it is at least worth considering and having a discussion to link to.
At the moment, we have just one division operator, div aka /, which is provided by the EuclideanRing class. This class also provides a mod operator:
class EuclideanRing a where
div :: a -> a -> a
mod :: a -> a -> a
The mod operator is required to be compatible with / in the sense that a = (a / b) * b + a `mod` b. This means that for types that support exact division, such as Rational and Number, we are required to set mod _ _ = 0. In a theoretical sense this is fine: every field is a Euclidean ring, and all of the laws are satisfied. Practically, however, this can be a bit of a footgun:
- PureScript beginners often expect
Prelude.modto behave the same way as the%operator in JS and other languages, so that e.g.5.0 `mod` 2.0 = 1.0and8.0 `mod` 3.0 = 2.0. In my view, it is almost always a mistake to usemodwith a type that is aField. - Functions like
lcmandgcdwhich make use ofmodinternally are likely to produce unexpected results when used with types that haveFieldinstances. For example,lcm = constandgcd = flip constfor any type that has aFieldinstance. It's probably almost always an error to uselcmorgcdwith types that haveFieldinstances.
If something is probably almost always an error, it suggests that we should be leaning on the compiler to let us know when we are doing it. My proposal is that we define a new division operator to separate inexact/integer division from exact division, as follows:
- Add
exactDiv :: a -> a -> atoField, with the requirement thatexactDiv a b = a * recip b(provided thatbis nonzero). TheexactDivfunction could be a member ofFieldor it could be a separate function with aFieldconstraint, but I think the former would be nice to allow people to supply more efficient implementations; division of floating point numbers is usually implemented in hardware, for example. - Weaken the superclass constraints on
Fieldfrom(EuclideanRing a, DivisionRing a)to(CommutativeRing a, DivisionRing a) - Change the operator alias
/to refer toexactDiv - Add a new operator alias
//for EuclideanRing'sdiv, which we could describe as "integer division".
After making these changes, we are free to separate the behaviour of mod from that of /. We can choose to either remove EuclideanRing instances for types like Number and Rational entirely, so that using mod on them is a type error, or we can choose to provide instances with a more Euclidean-ring-like behaviour. For example, we would be free to define mod on Number so that it does behave (more or less) the same way as JS's %; in this case we'd have to define // such that it always returns a integer, which is probably appropriate since we would be describing // as "integer division". For example we might have:
4.5 // 1.25 = 3.0- since1.25 * 3.0 = 3.754.5 `mod` 1.25 = 0.75matching JS's%
which expresses that 1.25 goes into 4.5 three times, with 0.75 left over. Doing this would also allow lcm and gcd to behave more sensibly; with this instance, lcm and gcd would work the same way on integral Numbers as they would on Int. For fractional Numbers it'd still be a bit weird because of floating point inaccuracy, but for rationals we'd be able to have eg. lcm (1%2) (1%3) = 1%1.
This approach is partially inspired by what Python does: / is for exact division and // is for integer division. https://docs.python.org/3.1/tutorial/introduction.html#numbers
- Ngôn ngữ chính
- PureScript
- Star
- 168
- Fork
- 90
- Merge trung bình
- 3 ngày 1 giờ
- Pull request đã merge (30 ngày)
- 1
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Không có hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của purescript/purescript-prelude
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 57/100
purescript/purescript-prelude#317 · 1 reaction ·
-
Int bottom is not safeĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 32/100
purescript/purescript-prelude#312 · 5 bình luận ·
-
add liftCompare & friendsĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
purescript/purescript-prelude#310 · 5 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 24/100
purescript/purescript-prelude#308 · 2 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
purescript/purescript-prelude#307 · 1 bình luận · 1 reaction ·
Tất cả issue của purescript/purescript-prelude
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
-
ManageSieve proxy: a backend's NO/BYE greeting is skipped as a capability line and reported as EOFĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Mend: dependency security vulnerability
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
Mend: dependency security vulnerability
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
ManageIQ/manageiq-pods#1476 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
prime-radiant-inc/evener#6366 ·
Maintainer thường phản hồi trong vòng 1 ngày