haskell 有没有一个类可以对“补丁”进行建模?

vecaoik1  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(53)

我正在Haskell库中寻找一个类似于以下的类(或者至少知道这样的东西的数学名称):

class Monoid patch => MyThing patch t where
  applyPatch :: t -> patch -> t

我可以有这样的示例:

instance MyThing (Last t) t where
  applyPatch x patch = case getLast patch of
    Just y => y
    Nothing => x

但我也可以有这样的例子:

instance MyThing (Dual (Map key value)) (Map key value) where
  applyPatch x patch = ...

其中补丁本质上添加和/或替换来自Map的键/值对。
如果你想做删除,你可以走得更远:

instance MyThing (Dual (Map key (Maybe value))) (Map key value) where
  applyPatch x patch = ...

除了patch是么半群之外,我希望这个类遵循的主要定律是结合性,具体来说:

forall (x :: t, y :: patch, z :: patch). 
  (x `applyPatch` y) `applyPatch` z == x `applyPatch` (y <> z)

我认为(好吧,实际上ChatGPTs认为)这是一个"Affine Space",但问题是,我的底层“补丁”类型,虽然是一个幺半群,但不是一个加法群,因为它没有逆。
所以基本上我想要一个没有逆的仿射空间。这在数学上或Haskell库中存在吗?

nfs0ujit

nfs0ujit1#

你所描述的相当于一个幺半群(或半群)作用。可以找到一个类的地方是Data.Monoid.Action from monoid-extras

class Action m s where
    act :: m -> s -> s

根据文件的解释,这些法律是:

act (m1 <> m2) = act m1 . act m2
-- Also, if m is a monoid:
act mempty = id
-- Additionally, if s has some algebraic structure then act m preserves it.
-- For instance, if s is a monoid, then act m should be a monoid homomorphism.

相关问题