You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Haskell base库中是否存在翻转半群操作的newtype?

问题解答

在Haskell的base标准库中,不存在名为SemigroupFlip的newtype,但有一个功能完全一致的类型Dual,定义在Data.Monoid模块中(base 4.8及以上版本也可通过Data.Semigroup访问)。

Dual的实现逻辑和你给出的SemigroupFlip完全相同:

newtype Dual a = Dual { getDual :: a }

instance Semigroup a => Semigroup (Dual a) where
  Dual x <> Dual y = Dual (y <> x)

instance Monoid a => Monoid (Dual a) where
  mempty = Dual mempty
  mappend (Dual x) (Dual y) = Dual (y `mappend` x)

它的核心作用就是反转底层类型的Semigroup(及Monoid)运算顺序,和你自定义的SemigroupFlip功能完全等价。

内容的提问来源于stack exchange,提问作者Clinton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 12:29:49