Skip to content

Inconsistent behavior of stimes for Maybe on negative repetitions

Summary

λ> stimes (-1) (Just ())
*** Exception: stimes: Maybe, negative multiplier
λ> stimes (-1) Nothing
Nothing

I think both of the above should error.

stimes for Maybe is defined as

stimesMaybe :: (Integral b, Semigroup a) => b -> Maybe a -> Maybe a
stimesMaybe _ Nothing = Nothing
stimesMaybe n (Just a) = case compare n 0 of
    LT -> errorWithoutStackTrace "stimes: Maybe, negative multiplier"
    EQ -> Nothing
    GT -> Just (stimes n a)

stimes docs say

Given that this works on a Semigroup it is allowed to fail if you request 0 or fewer repetitions, and the default definition will do so.

So I can't claim that this behavior violates the docs.
But I would expect the failure condition to apply consistently on all values of the type. I find it odd that it fails for some values but not for others.

Expected behavior

stimes should error on negative n for Nothing.

Environment

  • GHC version used: 9.2.5
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information