用可变变量表示数据类型
我试图用变量表示公式,例如,公式或变量和常量:
R(a,b) -> Q [Q takes formulae as substitutions]
R(x,b) v P(b) [x takes constants or variables as substitutions]
公式上的函数具有类约束,指定正在考虑哪种变量类型。例如,术语、变量和替换的类可能具有以下结构:
class Var b where ...
class (Var b) => Term b a | b -> a where ...
class (Term b a) => Subst s b a | b a -> s where ...
有许多处理句法术语操作的算法,对于这些算法,在变量类型上参数化术语类型将是有益的。例如,考虑对具有不同变量类型的某些术语类型的公式的通用统一算法:
unify :: (Subst s b a) => a -> a -> s b a
unify (P -> F(a,b)) ((Q v R) -> F(a,b)) = {P\(Q v R)} -- formulae
unify (P(x,f(a,b))) (P(g(c),f(y,b))) = {x\g(c),y\a} -- variables and constants
表示此类可变变量的最佳方法是什么?我尝试了多种方法,但尚未找到令人满意的解决方案。
I am attempting to represent formulae with variables ranging over, for instance, either formulae or variables and constants:
R(a,b) -> Q [Q takes formulae as substitutions]
R(x,b) v P(b) [x takes constants or variables as substitutions]
Functions over formulae have class constraints specifying which variable type is being considered. For instance, classes of terms, variables and substitutions might have the following structure:
class Var b where ...
class (Var b) => Term b a | b -> a where ...
class (Term b a) => Subst s b a | b a -> s where ...
There are many algorithms dealing with syntactic term manipulation for which parameterizing term types on variable types would be beneficial. For instance, consider a generic unification algorithm over formulae of some term type having different variable types:
unify :: (Subst s b a) => a -> a -> s b a
unify (P -> F(a,b)) ((Q v R) -> F(a,b)) = {P\(Q v R)} -- formulae
unify (P(x,f(a,b))) (P(g(c),f(y,b))) = {x\g(c),y\a} -- variables and constants
What is the best way to represent such variable variables? I have experimented with a variety of methods, but haven't settled on a satisfactory solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您说以下简单的术语和公式表示有什么问题,也许您的问题会更清楚?有一百万种方法可以完成此类事情(
{-LANGUAGE GADTs-}
大大扩展了可能性)Maybe your question would be clearer if you said what was wrong with the following simple minded representation of terms and formulas? There are a million ways of doing this sort of thing (the possibilities much expanded by
{-LANGUAGE GADTs-}
)