【发布时间】:2023-03-14 15:23:01
【问题描述】:
我正在关注这个blog 关于 F 代数 它解释了
终端代数通常在编程中被解释为配方 用于生成(可能是无限的)数据结构或转换 系统。
然后说
余代数的一个典型例子是基于一个函子,其固定 点是类型 e 元素的无限流。这是 函子:
data StreamF e a = StreamF e a
deriving Functor
这是它的固定点:
data Stream e = Stream e (Stream e)
我试过代码here
相关部分是
newtype Fix f = Fix (f (Fix f))
unFix :: Fix f -> f (Fix f)
unFix (Fix x) = x
cata :: Functor f => (f a -> a) -> Fix f -> a
cata alg = alg . fmap (cata alg) . unFix
ana :: Functor f => (a -> f a) -> a -> Fix f
ana coalg = Fix . fmap (ana coalg) . coalg
data StreamF e a = StreamF e a
deriving Functor
data Stream e = Stream e (Stream e)
era :: [Int] -> StreamF Int [Int]
era (p : ns) = StreamF p (filter (notdiv p) ns)
where notdiv p n = n `mod` p /= 0
primes = ana era [2..]
我收到了这个错误
main.hs:42:14: error:
• Can’t make a derived instance of ‘Functor (StreamF e)’:
You need DeriveFunctor to derive an instance for this class
• In the data declaration for ‘StreamF’
我哪里错了?
【问题讨论】:
-
你不能在 vanilla Haskell 中使用
deriving (Functor)。您需要文件顶部的编译指示{-# LANGUAGE DeriveFunctor #-}。更多信息here.
标签: haskell functor recursive-datastructures