Switch to module over ring for duration arithmetic
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Refactor
- Clarity
- Clearly specified
- Activity status
- Stale
- Domain
- backend
Research direction
The issue names the Semiring instances for Milliseconds and other duration types, and links the modules-over-rings library. Start by locating those instances and reviewing the linked abstraction. The issue proposes replacing them and creating a vector-spaces-over-fields library for division; done means duration arithmetic follows those abstractions without the wrapping shown in the examples.
Written by the indexing model from the issue text.
Description
Currently a Semiring Milliseconds instance exists. However, durations are not a semiring: the closure property of the multiplication operation is not honoured because multiplying two durations gives not a duration but a duration squared.
This is incorrect, and causes awkward code where you need to add/remove nonsensical wrappers just so you can multiply or divide. One example is multiplying a duration by a scalar:
twiceAsLong :: Milliseconds -> Milliseconds
twiceAsLong a = a * Milliseconds 2.0 -- nasty wrap
Another example is where you divide two durations when implementing a multiplication between durations and frequencies where the frequency type is in terms of the reciprocal of the frequency, which is a duration:
newtype Freq = Freq Milliseconds
-- | The reciprocal of a frequency is a duration. (Note how 1/ms = kHz.)
reciprocal :: Freq -> Milliseconds
reciprocal (Freq r) = r
-- | Multiplying a duration (ms) by a frequency (1/ms) gives a number.
multiply :: Milliseconds -> Freq -> Number
multiply d f = un Milliseconds (d / reciprocal f) -- nasty unwrap
With the right abstraction, no wrapping and unwrapping would be necessary, because the types would just work out. We should remove the Semiring Milliseconds instance (and the instances for other duration types) and switch to modules over rings instead, which model this correctly. To add division we should make a library for vector spaces over fields.
Modules over rings and vector spaces over fields are generally useful for types that represent quantities, such as durations, distances, and file sizes.
- Dominant language
- PureScript
- Stars
- 51
- Forks
- 48
- Avg merge
- 2d 23h
- Merged PRs (30d)
- 1
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from purescript/purescript-datetime
-
Difficulty 3/5 1-2 days Newbie friendliness 25/100
purescript/purescript-datetime#102 · 5 comments ·
-
Add constructors for Date / Time components using type-level integersMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 5/5 Over a week Newbie friendliness 15/100
purescript/purescript-datetime#96 · 1 comment ·
-
type: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
purescript/purescript-datetime#92 · 1 comment ·
-
status: needs more info
Difficulty 5/5 Over a week Newbie friendliness 25/100
purescript/purescript-datetime#90 · 3 reactions ·
-
status: needs more info
Difficulty 5/5 Over a week Newbie friendliness 20/100
All issues in purescript/purescript-datetime
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
Maintainers usually reply within 1 day
-
bug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
milind-soni/OpenMausBot#5829 ·
Maintainers usually reply within 1 day