【问题标题】:An example for chronomorphism时态的一个例子
【发布时间】:2022-04-12 19:35:18
【问题描述】:

我不明白如何使用时态创建一些示例。 我知道亚型(cataana)也知道histofutu

但我没有意识到时态的一些例子(可能是 Tardis monad 中的一些行为)。

还有相关链接https://github.com/ekmett/recursion-schemes/issues/42

这与Histomorphisms, Zygomorphisms and Futumorphisms specialised to lists 无关,因为没有时间同态的例子。

【问题讨论】:

标签: haskell recursion-schemes


【解决方案1】:

可能时态的最大用途是折叠命名语法树。特别是,您可以引用尚未处理的名称以及已经已处理的名称。

你可以用时态函数做的另一件事是重写动态函数!你可以阅读更多关于变形的信息here。他们引用的例子之一是加泰罗尼亚数字。我在下面将它翻译成 Haskell。

import Data.Functor.Foldable
import Control.Arrow
import Control.Comonad.Cofree

dyna :: (Functor f) => (f (Cofree f a) -> a) -> (c -> f c) -> c -> a
dyna a c = extract . h where h = (uncurry (:<)) . (a &&& id) . fmap h . c

natural :: Int -> ListF Int Int
natural 0 = Nil
natural n = Cons n (n - 1)

takeCofree :: Int -> Cofree (ListF Int) a -> [a]
takeCofree 0 _ = []
takeCofree _ (a :< Nil) = [a]
takeCofree n (a :< Cons _ c) = a : takeCofree (n - 1) c

catalan :: Int -> Int
catalan = dyna coa natural where
    coa :: ListF Int (Cofree (ListF Int) Int) -> Int
    coa Nil = 1
    coa (Cons x table) = sum $ zipWith (*) xs (reverse xs)
        where xs = takeCofree x table

您可能还会发现this 很有用。它有一个使用 futumorphism 构建树和 catamorphism 将其拆除的示例(尽管这已被遮挡)。当然,这张地图实际上是时态的另一种特殊化。

【讨论】:

  • 抱歉,但您的代码似乎不起作用。你能提供工作版本吗?
  • 我不明白为什么该论文中给出的加泰罗尼亚数的组织同构被称为虚假解决方案,即使它在 Haskell 中给出了正确的结果?为什么将 Coinductive 参数转换为 elems 函数到一个简单的归纳列表是伪造的?它只是在像 Agda 这样具有更强大类型系统的语言中是虚假的吗?
猜你喜欢
  • 2011-06-25
  • 2011-02-22
  • 1970-01-01
  • 1970-01-01
  • 2016-07-01
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多