-
Notifications
You must be signed in to change notification settings - Fork 83
Open
Milestone
Description
Not sure exactly how best to do this, but something like:
instance Semigroup DiffTime
instance Monoid DiffTime
instance Semigroup NominalDiffTime
instance Monoid NominalDiffTime
class Monoid a => Group a where
negateG :: a -> a
(~~) :: a -> a -> a
scaleG :: Int -> a -> a
instance Num a => Group (Sum a) -- but what about Natural?
instance Group DiffTime
instance Group NominalDiffTime
class Group (TorsorGroup a) => Torsor a where
type TorsorGroup a :: *
addT :: TorsorGroup a -> a -> a
diffT :: a -> a -> TorsorGroup a
instance Torsor Day where
type Torsor Day = Sum Integer
...
instance Torsor UTCTime where
type Torsor UTCTime = NominalDiffTime
...
-- etc. for other time types
Notes
Using this as a scratch-pad for ideas, editing often.
- See Baez to get the gist of torsors.
- See also type NominalDiffTime = DiffTime #281, although this and that can be done independently.
Torsorand especiallyGroupare generally useful classes; it might be a bit odd to define them in the time library.- Should also do Functions to truncate time types #277 with this.
- A stricter definition of
Groupwould beclass (Monoid a, Torsor a, TorsorGroup a ~ a) => Group a, not sure if that's too annoying.- Alternatively can do
instance {-# OVERLAPPABLE #-} Group a => Torsor a where ...
- Alternatively can do
- There's a bit of tension between the concepts "monoid" and "additive monoid" (likewise "group", etc.), and we use the
Sumtype to bridge the gap. For example there isn't aninstance Monoid Integer(using addition) because there can be other monoids onInteger. - This sort of thing has been attempted elsewhere:
- A nice way of looking at some of the classes is to consider the domain of the scaling function
:: i -> a -> a:iis the positive integers: semigroupiis the non-negative integers: monoidiis the integers: groupiis the rationals or reals: one-dimension vector space over rationals or reals
- There probably isn't a need to add a "one-dimension vector space" class (what members would it have?) even though
UTCTimewould morally be an instance.
Expanding on point 5, it is possible to collapse Group into Torsor:
class Group (TorsorGroup a) => Torsor a where
type TorsorGroup a :: *
addT :: TorsorGroup a -> a -> a
(~~) :: a -> a -> TorsorGroup a
type Group a = (Monoid a, Torsor a, TorsorGroup a ~ a)
negateG :: Group a => a -> a
negateG x = mempty ~~ x
Metadata
Metadata
Assignees
Labels
No labels