Algorithm Haskell递归方案:同时遍历两个结构
我正在尝试使用递归方案编写Robinson的统一算法。 统一算法采用两种类型,并给出一个结果。 A类型是:Algorithm Haskell递归方案:同时遍历两个结构,algorithm,haskell,recursion,unification,recursion-schemes,Algorithm,Haskell,Recursion,Unification,Recursion Schemes,我正在尝试使用递归方案编写Robinson的统一算法。 统一算法采用两种类型,并给出一个结果。 A类型是: data TypeF a = TypeApplication a a | TypeVariable Name deriving (Read,Show,Eq,Functor,Foldable,Traversable) type Type = Fix TypeF unify :: Type -> Type -> Result unify = ... 如何
data TypeF a
= TypeApplication a a
| TypeVariable Name
deriving (Read,Show,Eq,Functor,Foldable,Traversable)
type Type = Fix TypeF
unify :: Type -> Type -> Result
unify = ...
如何使用递归方案优雅地实现这一点?我建议使用curry和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
,如下所示,使用这个包
在这种情况下,这将自动生成您想要的内容,而无需使用
修复什么是结果
?替换,但这并不重要。想想您的基本情况,类型变量n
和类型变量m
应该如何统一?aTypeVariable n
和aTypeApplication x y
?aTypeApplication xy
和aTypeApplication uv
?i)使用自由单子而不是原始固定点来区分变量与所有其他构造(并获得替换的简便实现);ii)了解halfZip操作(谷歌it!);iii)请注意,任何半可压缩和可遍历的函子都会产生一个带有罗宾逊统一算法的自由单子,因为@pigworker的谷歌搜索找到了很多衣服,你可能会看到
import Data.Functor.Foldable.TH (makeBaseFunctor)
data Type = TypeApplication Type Type
| TypeVariable Name
makeBaseFunctor ''Type