I'm looking for a class in like the following in a Haskell library (or at least to know the mathematical name for such a thing):
class Monoid patch => MyThing patch t where
applyPatch :: t -> patch -> t
I can have instances like this:
instance MyThing (Last t) t where
applyPatch x patch = case getLast patch of
Just y => y
Nothing => x
But I could also have instances like this:
instance MyThing (Dual (Map key value)) (Map key value) where
applyPatch x patch = ...
Where the patch essentially adds and/or replaces key/value pairs from the map.
One could go further if you wanted to do deletions:
instance MyThing (Dual (Map key (Maybe value))) (Map key value) where
applyPatch x patch = ...
Aside from patch
being a monoid, the main law I want this class to follow is associativity, concretely:
forall (x :: t, y :: patch, z :: patch).
(x `applyPatch` y) `applyPatch` z == x `applyPatch` (y <> z)
My thought (well, actually ChatGPTs thought) that this was an "Affine Space", but the issue is that my underlying "patch" type, whilst a monoid, is not an additive group, because it doesn't have inverses.
So basically I think I want an affine space without inverses. Does this exist, either mathematically or in a Haskell library?