标签: representable

镜头超过共群或可代表

这是这个问题的一个更具体的变体:Mutate only focus of Store Comonad?,以免一次问多个问题。

是否有任何与Control.Lens兼容的镜头允许我与 comonad 的焦点(来自 的值extract)或 Store Comonad ( pos)的索引/值进行交互?

似乎镜头在这里可能有用,但我一直找不到适合的东西;任何帮助将不胜感激,谢谢!

haskell comonad haskell-lens representable

5
推荐指数
1
解决办法
278
查看次数

Ap的Num实例什么时候合法?

考虑这种类型:

newtype Ap f a = Ap (f a)
instance (Applicative f, Num a) => Num (Ap f a) where
  (+)         = liftA2 (+)
  (*)         = liftA2 (*)
  negate      = fmap negate
  fromInteger = pure . fromInteger
  abs         = fmap abs
  signum      = fmap signum
Run Code Online (Sandbox Code Playgroud)

该实例必须满足什么条件f才合法?显然,仅仅成为合法的Applicative还不够(例如,与is时x + negate x不同,即使 is是合法的)。我的猜测是,它对于成为一个可表示函子来说既是必要的也是充分的。不过,我不确定如何证明这一点(或者如果它是错误的,则找到一个反例)。fromInteger 0xAp NothingMaybeApplicativef

为了供参考,以下是文档建议Num法律

-- Associativity of (+)
(x + y) + z = x …
Run Code Online (Sandbox Code Playgroud)

haskell typeclass representable typeclass-laws

5
推荐指数
1
解决办法
161
查看次数

Haskell 的评估策略在 Memoization via Representable 中的作用是什么

通过 Representables 进行记忆化一文很好地解释了如何通过 Representables 记忆递归函数。首先将斐波那契数列实现为 的固定点fibOp

fibOp :: Num a => (Natural -> a) -> (Natural -> a)
fibOp v 0 = 0
fibOp v 1 = 1
fibOp v n = v (n-1) + v (n-2)

fix f = let x = f x in x

fibNaive :: Num a => Natural -> a
fibNaive = fix fibOp  
Run Code Online (Sandbox Code Playgroud)

此实现效率不高,因为它多次计算相同的值。

文章接着介绍了互反函数streamTabulatestreamIndex(稍后将在Representable类型类中进行推广)。这些函数允许我们实现以下的记忆版本fibNaive

fibSmart :: Num a => Natural -> a …
Run Code Online (Sandbox Code Playgroud)

haskell memoization representable

4
推荐指数
1
解决办法
188
查看次数