提问人:Clinton 提问时间:8/22/2023 最后编辑:cafce25Clinton 更新时间:8/22/2023 访问量:98
有没有对“补丁”进行建模的类?
Is there a class that models "patches"?
问:
我在 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库中是否存在?
答:
7赞
duplode
8/22/2023
#1
您所描述的内容相当于单体(或半群)操作。可以找到该类的一个地方是来自 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.
评论
0赞
Clinton
8/22/2023
恰到好处,正是我想要的。谢谢!
1赞
Iceland_jack
8/22/2023
值得注意的是,这是一个函子: = 并且定律是函子定律的一个例子!Action m a
FunctorOf (Basically m) (->) (Const a)
0赞
Iceland_jack
8/22/2023
其中 newtype Basically m a b = 基本上 m
将 Monoid 提升到 Category: .instance Monoid m => Category (Basically m)
1赞
Daniel Wagner
8/23/2023
你知道第一次约会穿什么吗?集体诉讼。act mempty = id
0赞
duplode
8/23/2023
@Iceland_jack 是的 -- 作为函子的动作,从单对象类别到 Hask(然后制作成适合/通过幻影)。此外,同态也是函子(用这种样式表述它,从 到 ),特别是动作是 的同态。Category
FunctorOf
Basically m
Basically n
Endo s
评论
t
Maybe t
Maybe
value