我把变质作用定义如下:
-- Fixed point of a Functor
newtype Fix f = In (f (Fix f))
deriving instance (Eq (f (Fix f))) => Eq (Fix f)
deriving instance (Ord (f (Fix f))) => Ord (Fix f)
deriving instance (Show (f (Fix f))) => Show (Fix f)
out :: Fix f -> f (Fix f)
out (In f) = f
-- Anamorphism
type Coalgebra f a = a -> f a
ana :: (Functor f) => Coalgebra f a -> a -> Fix f
ana f = In . fmap (ana f) . f我现在必须写一个像这样的余代数:
appendListCoAlg :: (ListF' a, ListF' a) -> ListF a (ListF' a, ListF' a)
appendListCoAlg (In (ConsF a as), listb) = ConsF a (as, listb)
appendListCoAlg (In NilF, In (ConsF b bs)) = ConsF b (In NilF, bs)
appendListCoAlg (In NilF, In NilF) = NilF在这里,必须从“基本情况”(NilF)中构造一个非质体。
我感兴趣的是是否有可能编写ana,这样我就可以列出以下内容:
appendListCoAlg :: (ListF' a, ListF' a) -> ?
appendListCoAlg (In (ConsF a as), listb) = ConsF a (as, listb)
appendListCoAlg (In NilF, **bs**) = **bs**
appendListCoAlg (In NilF, In NilF) = NilF在这里,我可以返回一个类型为“早期”的值。
发布于 2022-11-19 12:09:08
ana不允许您这样做。您可以使用apo (尽管它仍然不像可能的那样整洁;Either确实应该在外部)。
apo :: Corecursive t => (a -> Base t (Either t a)) -> a -> tappendListCoAlg :: [a] -> [a] -> ListF a (Either [a] [a])
appendListCoAlg listb (a : as) = ConsF a (Right as) -- Right: continue unfolding
appendListCoAlg listb [] = Left <$> project listb -- Left: stop unfoldingappend :: [a] -> [a] -> [a]
append lista listb = apo (appendListCoAlg listb) listahttps://stackoverflow.com/questions/74499752
复制相似问题