Since there is no Num instance (see #106), it is impossible to subtract times. But this is an important thing to be able to do. For example, consider animation. Suppose I wish to display a new frame every 10 milliseconds, but it takes variable time to compute the frame and the remaining time I must wait with threadDelay. I may save the time of the start x₀ and the end x₁ of the computation, but now I must calculate 10ms − (x₁ − x₀) and there is no way to do that!
An immediate objection is that there is no indication of sign for time values. We can amend this by returning 0 whenever the result of the subtraction would be negative.
Currently I have this function:
subtractTime
∷ forall α. (KnownRat α, KnownRat (DivRat α (1 :% 1000000000)), KnownRat (DivRat (1 :% 1000000000) α))
⇒ Time α → Time α → Time α
subtractTime x y = let δ = toNum @Nanosecond @Integer x - toNum @Nanosecond y in (toUnit @α ∘ time @Nanosecond ∘ fromIntegral) (max δ 0)
I have no idea how much it is wrong. To be honest, I do not even understand the type signature — GHC wrote it for me. I can only say that it seems to work.
Since there is no
Numinstance (see #106), it is impossible to subtract times. But this is an important thing to be able to do. For example, consider animation. Suppose I wish to display a new frame every 10 milliseconds, but it takes variable time to compute the frame and the remaining time I must wait withthreadDelay. I may save the time of the start x₀ and the end x₁ of the computation, but now I must calculate 10ms − (x₁ − x₀) and there is no way to do that!An immediate objection is that there is no indication of sign for time values. We can amend this by returning 0 whenever the result of the subtraction would be negative.
Currently I have this function:
I have no idea how much it is wrong. To be honest, I do not even understand the type signature — GHC wrote it for me. I can only say that it seems to work.