Haskell递归方案:同时遍历两个结构

时间:2017-09-09 11:13:37

标签: algorithm haskell recursion unification recursion-schemes

我试图使用递归方案编写Robinson的统一算法。 统一算法采用两种类型并吐出结果。 类型是:

data TypeF a
    = TypeApplication a a
    | TypeVariable Name
    deriving (Read,Show,Eq,Functor,Foldable,Traversable)
type Type = Fix TypeF

unify :: Type -> Type -> Result
unify = ...

如何使用递归方案优雅地完成这项工作?

1 个答案:

答案 0 :(得分:1)

我只是建议使用currying和hylomorphism。

data TypeF a
    = TypeApplication a a
    | TypeVariable Name
    deriving (Read,Show,Eq,Functor,Foldable,Traversable)
type Type = Fix TypeF

unify :: (Type, Type) -> Result
unify = hylo algebra coalgebra
    where algebra :: TypeF Result -> Result
          algebra = ...
          coalgebra :: (Type, Type) -> TypeF (Type, Type)
          coalgebra = ...

顺便说一句,我可能会使用TypeF使用recursion-schemes包,如下import Data.Functor.Foldable.TH (makeBaseFunctor) data Type = TypeApplication Type Type | TypeVariable Name makeBaseFunctor ''Type

Fix

在这种情况下,这将自动生成您想要的内容,而无需使用// merge-conflict.next // merge-conflict.previous