【发布时间】:2018-04-21 05:18:26
【问题描述】:
当我从 GHC Data.Complex 模块中阅读 Applicative Complex 和 Monad Complex 实例的源代码时,我有点惊讶:
-- | @since 4.9.0.0
instance Applicative Complex where
pure a = a :+ a
f :+ g <*> a :+ b = f a :+ g b
liftA2 f (x :+ y) (a :+ b) = f x a :+ f y b
-- | @since 4.9.0.0
instance Monad Complex where
a :+ b >>= f = realPart (f a) :+ imagPart (f b)
什么...? Applicative Complex 实例似乎将复数视为大小为 2 的数组。而且它们看起来更像是箭头操作。它们背后有什么数学基础吗?有没有,有什么用?
【问题讨论】:
-
它们是守法的实例,所以它们没有理由不存在(这是“数学”基础)
-
所以与复分析无关,复数在这里只是大小为 2 的数组?
-
复数是,在某种意义上,只是大小为2的数组。ℂ与ℝ²同构。
标签: haskell monads functor complex-numbers applicative