purescript-datetime icon indicating copy to clipboard operation
purescript-datetime copied to clipboard

Switch to module over ring for duration arithmetic

Open rightfold opened this issue 7 years ago • 7 comments

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.

rightfold avatar Aug 12 '17 18:08 rightfold

Hi RF Is there any issue with modules being hosted/owned by tinker? I notice the website.

Thanks A

AlexMouton avatar Oct 23 '17 18:10 AlexMouton

The company is bankrupt so I don’t think anybody cares copyright wise. It’s more of a problem that the library isn’t part of core. There’s a also an open purescript-prelude PR that adds modules over rings.

rightfold avatar Oct 23 '17 20:10 rightfold

aha I see. Thanks. https://github.com/purescript/purescript-prelude/pull/136

AlexMouton avatar Oct 23 '17 20:10 AlexMouton

As long as the library has a clear license, we should be good to use it under those terms, I think.

But we should still have the discussion to see if it's the exact design we want.

paf31 avatar Oct 23 '17 20:10 paf31

Discussion about Module design or its use here?

AlexMouton avatar Oct 24 '17 06:10 AlexMouton

Module design

paf31 avatar Oct 24 '17 15:10 paf31

The dodgy Semiring instance have now been removed, and additive Monoid instances have been added (since the sum of two durations is a duration), so I think all that remains here is the question of the module-over-a-ring interface for durations.

hdgarrood avatar Sep 25 '20 01:09 hdgarrood