使用 Comonad Fix 组合器

Chr*_*ner 3 haskell comonad fixpoint-combinators

所以我最近一直在尝试固定点,并最终通过常规固定点的努力发现了一些用途;现在我正在转向共生不动点,我担心我已经陷入困境了;

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

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

import Control.Comonad
import Control.Comonad.Cofree
import Control.Monad.Fix
Run Code Online (Sandbox Code Playgroud)

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

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

好的,我已经完成了基本的修复工作,是时候继续讨论 comonad 类型了!以下是一些简单的 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')
Run Code Online (Sandbox Code Playgroud)

好的,以下组合器来自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
Run Code Online (Sandbox Code Playgroud)

我开始尝试 wfix:

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

> streamWFix
-- 2
Run Code Online (Sandbox Code Playgroud)

在这种情况下,这个似乎是通过调用第一个w a -> aw 直到达成解决方案来工作的;const 0这就说得通了。我们也可以用磁带来做到这一点:

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

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

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

simpleCFix :: Stream Int
simpleCFix = cfix go
  where
    go _ = 0
Run Code Online (Sandbox Code Playgroud)

与 kfix 类似;即使是简单的尝试似乎也不会终止。我对 kfix 的理解是,每个“槽”中的函数都会传递一个专注于该点的评估 comonad 的副本;是这样吗?

我尝试对此使用“getOne”:

streamKFix :: Stream Int
streamKFix = kfix st
  where
    go _ = 0
    st = S go st
Run Code Online (Sandbox Code Playgroud)

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

tapeKFix :: Tape Int
tapeKFix = kfix $ Tape [] (const 0) []
Run Code Online (Sandbox Code Playgroud)

所以; 回到我的问题,有人可以提供一些使用 cfix 和 kfix 的可运行(非平凡)示例,并解释它们是如何工作的吗?我计划使用 kfix 最终进行“康威生命游戏”风格的实验,我认为 kfix 在与给定单元格周围的社区合作时有用吗?

请随意提出任何澄清问题,并帮助我扩展我的知识和修复直觉!

谢谢!

Cir*_*dec 5

ComonadApplyComonad实例Tape不够懒惰,无法与 一起使用kfix

duplicateforTape要求您先证明磁带存在,然后才能得出结果是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
Run Code Online (Sandbox Code Playgroud)

<@>在得出结果是 a 的结论之前检查两个参数都是磁带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
Run Code Online (Sandbox Code Playgroud)

结合起来就没有办法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>> -------------
Run Code Online (Sandbox Code Playgroud)

您可以通过使用无可辩驳的模式来提高工作效率来解决此问题duplicate。即使尚未生成构造函数,模式也会匹配。以下是您如何使用它来提高生产力<@>~(Tape l a r)Tapeduplicate

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
Run Code Online (Sandbox Code Playgroud)

无可辩驳的模式匹配相当于使用函数来提取值。因为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
      ...
Run Code Online (Sandbox Code Playgroud)