为无限流派生一个Functor

时间:2017-12-29 13:28:03

标签: haskell functor

我正在关注这个关于F-代数的blog 它解释了

  

终端代数通常在编程中被解释为配方   用于生成(可能是无限的)数据结构或转换   系统

并说

  

一个代数的规范例子是基于一个固定的算子   point是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’

我哪里错了?

1 个答案:

答案 0 :(得分:1)

在没有使用语言扩展的情况下,

deriving在Haskell中非常有限。由于编译器不能总是计算Functor实例应该是什么,deriving Functor不是标准的Haskell。

但是,有一种语言扩展允许这种情况,即-XDeriveFunctor。要启用此扩展,请执行以下操作之一:

  • 使用标记-XDeriveFunctor进行编译。 (例如:编译时运行ghc -XDeriveFunctor Main.hs

  • 将编译语{-# LANGUAGE DeriveFunctor #-}写在文件顶部。

以下是添加此pragma后文件的外观:

{-# LANGUAGE DeriveFunctor #-}

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..]

如果您打算使用GHCi,请在加载文件前使用:set -XDeriveFunctor