【问题标题】:Using Comonad Fix Combinators使用 Comonad 修复组合器
【发布时间】:2017-12-22 20:36:11
【问题描述】:

所以我最近一直在尝试固定点,终于挣扎了 通过足以发现一些用途的规则固定点;现在我要继续 comonadic 固定点,我怕我卡住了;

以下是一些我尝试过的方法以及哪些方法有效/无效的示例:

{-# language DeriveFunctor #-}
{-# language FlexibleInstances #-}
module WFix where

import Control.Comonad
import Control.Comonad.Cofree
import Control.Monad.Fix

所以我从勒布定理开始作为一个列表;列表的每个元素都是一个函数 它采用最终结果来计算其答案;这让我可以做“电子表格” 值可以依赖于其他值的计算。

spreadSheetFix :: [Int]
spreadSheetFix = fix $ \result -> [length result, (result !! 0) * 10, (result !! 1) + 1, sum (take 3 result)]

好的,我已经完成了基本的修复工作,是时候继续讨论comonad 类型了! 这里有一些简单的共单子可以用作示例:

  data Stream a = S a (Stream a)
    deriving (Eq, Show, Functor)

  next :: Stream a -> Stream a
  next (S _ s) = s

  instance Comonad Stream where
    extract (S a _) = a
    duplicate s@(S _ r) = S s (duplicate r)

  instance ComonadApply Stream where
    (S f fs) <@> (S a as) = S (f a) (fs <@> as)

  data Tape a = Tape [a] a [a]
    deriving (Show, Eq, Functor)

  moveLeft, moveRight :: Tape a -> Tape a
  moveLeft w@(Tape [] _ _) = w
  moveLeft (Tape (l:ls) a rs) = Tape ls l (a:rs)

  moveRight w@(Tape _ _ []) = w
  moveRight (Tape ls a (r:rs)) = Tape (a:ls) r rs

  instance Comonad Tape where
    extract (Tape _ a _) = a
    duplicate w@(Tape l _ r) = Tape lefts w rights
      where
        lefts = zipWith const (tail $ iterate moveLeft w) l
        rights = zipWith const (tail $ iterate moveRight w) r

  instance ComonadApply Tape where
    Tape l f r <@> Tape l' a r' = Tape (zipWith ($) l l') (f a) (zipWith ($) r r')

好的,下面的组合子来自Control.Comonad

wfix :: Comonad w => w (w a -> a) -> a
wfix w = extract w (extend wfix w)

cfix :: Comonad w => (w a -> a) -> w a
cfix f = fix (extend f)

kfix :: ComonadApply w => w (w a -> a) -> w a
kfix w = fix $ \u -> w <@> duplicate u

我开始尝试 wfix:

streamWFix :: Int
streamWFix = wfix st
  where
    incNext = succ . extract . next
    st = (S incNext (S incNext (S (const 0) st)))

> streamWFix
-- 2

这似乎可以通过在 w 上调用第一个 w a -&gt; a 直到到达 在这种情况下,分辨率为const 0;这就说得通了。我们也可以这样做 用胶带:

selfReferentialWFix :: Int
selfReferentialWFix = wfix $ Tape [const 10] ((+5) . extract . moveLeft) []
-- selfReferentialWFix == 15

K,我想我明白了,但接下来的我有点卡住了, 我似乎对 cfix 应该做什么没有直觉。 即使是我能想到的最简单的事情也会永远旋转 当我评估它时;甚至试图提取流的第一个元素 使用 getOne 失败。

getOne :: Stream a -> a
getOne (S a _) = a

simpleCFix :: Stream Int
simpleCFix = cfix go
  where
    go _ = 0

与 kfix 类似;即使是简单的尝试似乎也不会终止。 我对 kfix 的理解是每个“插槽”中的函数 通过了一份针对该点的评估共生体的副本;是这样吗?

我尝试对此使用“getOne”:

streamKFix :: Stream Int
streamKFix = kfix st
  where
    go _ = 0
    st = S go st

这是一次使用 Tape 的有限尝试,但也无法运行:

tapeKFix :: Tape Int
tapeKFix = kfix $ Tape [] (const 0) []

所以;归结为我的问题,有人可以提供一些可运行的(非平凡的) 使用 cfix 和 kfix 的例子,并解释它们是如何工作的?我计划使用 kfix 最终做一个“Conway's 生活游戏”风格的实验,我认为 kfix 有用吗? 与给定单元周围的社区合作?

欢迎提问 任何澄清问题,并帮助我扩展我的知识和修复直觉!

谢谢!

【问题讨论】:

标签: haskell comonad fixpoint-combinators


【解决方案1】:

TapeComonadApplyComonad 实例不足以懒惰地与 kfix 一起使用。

duplicate for Tape 要求您证明磁带存在,然后才能断定结果是 Tape

instance Comonad Tape where
  extract (Tape _ a _) = a
  duplicate w@(Tape l _ r) = Tape lefts w rights
--             ^             ^
-- matches a Tape            |               
-- before determining that the result is a Tape

&lt;@&gt; 检查两个参数是否都是磁带,然后才能断定结果是Tape

instance ComonadApply Tape where
  Tape l f r <@> Tape l' a r' = Tape (zipWith ($) l l') (f a) (zipWith ($) r r')
-- ^             ^              ^
-- matches two Tapes            |
-- before detrmining that the result is a Tape

结合起来,kfix (Tape _ _ _) 不可能产生Tape

kfix w            = fix $ \u -> w            <@> duplicate u
kfix (Tape _ _ _) = fix $ \u -> (Tape _ _ _) <@> duplicate u
kfix (Tape _ _ _) = fix $ \u -> (Tape _ _ _) <@> case u of (Tape _ _ _) -> ...
--                         ^                                |
--                         ----------- <<loop>> -------------

您可以通过使用irrefutable patterns 提高duplicate&lt;@&gt; 或两者的效率来解决此问题。即使尚未生成 Tape 构造函数,模式 ~(Tape l a r) 也会匹配。以下是您如何使用它来提高 duplicate 的生产力

instance Comonad Tape where
  extract (Tape _ a _) = a
  duplicate w@(~(Tape l _ r)) = Tape lefts w rights
    where
      lefts = zipWith const (tail $ iterate moveLeft w) l
      rights = zipWith const (tail $ iterate moveRight w) r

无可辩驳的模式匹配等同于使用函数来提取值。对于duplicate,相当于写

left  (Tape l _ _) = l
right (Tape _ _ r) = r

instance Comonad Tape where
  extract (Tape _ a _) = a
  duplicate w = Tape lefts w rights
    where
      l = left w
      r = right w
      ...

【讨论】:

  • 这太棒了!感谢该文章的链接,非常有用!我将您的更改应用于复制,现在我可以进行一些简单的 kfix 尝试;不过,我仍然对某些事情感到有些困惑;例如,我没有得到这个示例的预期输出:gist.github.com/ChrisPenner/a8e7a72f4765aaa9941a58b70fd57ec3
  • 我不认为moveLeftmoveRight 对于Tape [a] a [a] 足够懒惰(或者可以足够懒惰)。 moveLeft 在决定计算左侧、中间或右侧值之前必须查看左侧内部。我的直觉是,如果将其用作duplicate&lt;@&gt; 的一部分,则任何需要检查结构以确定结构的参考都将不起作用。 Tape (Stream a) a (Stream a) works.
  • 这是有道理的,就我发布的示例而言,它不使用 moveLeft 或 moveRight,并且每次执行仅取决于左侧的内容,所以我认为最左边的单元格应该执行并且流向其他人?
  • 完全公开,最终目标是使用 kfix 使用计算每个单元格的邻居的结果来计算 2D 向量。理论上,每个单元格都有一个邻居作为依赖项,因此计算应该终止。希望 Vectors 足够懒惰?我通过将向量与索引配对来使向量成为共单子,并且我将所有向量限制为相同的维度
  • 也许我最终会使用这个库:github.com/kwf/ComonadSheet/blob/master/README.md 但我想先自己尝试一下!
猜你喜欢
  • 2020-03-26
  • 1970-01-01
  • 2015-02-01
  • 2019-06-16
  • 2012-12-05
  • 2017-03-22
  • 2019-09-07
  • 2013-03-20
  • 2018-03-15
相关资源
最近更新 更多