是否有一个模拟“补丁”的类?

问题描述 投票:0回答:1

我正在 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 = ...

补丁本质上是添加和/或替换地图中的键/值对。

如果您想删除,可以更进一步:

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)

我的想法(嗯,实际上 ChatGPT 认为)这是一个“仿射空间”,但问题是我的底层“补丁”类型虽然是幺半群,但不是一个加法群,因为它没有逆.

所以基本上我想我想要一个没有逆的仿射空间。无论是在数学上还是在 Haskell 库中,这是否存在?

haskell math discrete-mathematics
1个回答
0
投票

您所描述的相当于一个幺半群(或半群)动作。一种可用的实现是来自 monoid-extras

Data.Monoid.Action:

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.
 
© www.soinside.com 2019 - 2024. All rights reserved.