Cli*_*ton 4 haskell discrete-mathematics monoids semigroup
我正在 Haskell 库中寻找如下所示的类(或者至少知道此类事物的数学名称):
class Monoid patch => MyThing patch t where
applyPatch :: t -> patch -> t
Run Code Online (Sandbox Code Playgroud)
我可以有这样的实例:
instance MyThing (Last t) t where
applyPatch x patch = case getLast patch of
Just y => y
Nothing => x
Run Code Online (Sandbox Code Playgroud)
但我也可以有这样的实例:
instance MyThing (Dual (Map key value)) (Map key value) where
applyPatch x patch = ...
Run Code Online (Sandbox Code Playgroud)
补丁本质上是添加和/或替换映射中的键/值对。
如果你想删除,可以更进一步:
instance MyThing (Dual (Map key (Maybe value))) (Map key value) where
applyPatch x patch = ...
Run Code Online (Sandbox Code Playgroud)
除了patch成为幺半群之外,我希望此类遵循的主要法则是结合性,具体来说:
forall (x :: t, y :: patch, z :: patch).
(x `applyPatch` y) `applyPatch` z == x `applyPatch` (y <> z)
Run Code Online (Sandbox Code Playgroud)
我的想法(好吧,实际上 ChatGPT 认为)这是一个“仿射空间”,但问题是我的底层“补丁”类型虽然是一个幺半群,但不是一个加法群,因为它没有逆元。
所以基本上我想我想要一个没有逆的仿射空间。无论是在数学上还是在 Haskell 库中,这是否存在?
您所描述的相当于一个幺半群(或半群)动作。可以找到该类的一个地方是Data.Monoid.Action来自monoid-extras:
class Action m s where
act :: m -> s -> s
Run Code Online (Sandbox Code Playgroud)
解释一下文档,法律是:
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.
Run Code Online (Sandbox Code Playgroud)