基于表的声明性反应式编程

时间:2012-04-21 03:44:12

标签: sql functional-programming declarative

是否有一种编程语言或软件包在内存中支持基于表的反应式声明式编程,与SQL语言和触发器具非常相似?

例如,我可以将PERSONJOB表定义为函数

name: PERSON -> STRING
female: PERSON -> BOOLEAN
mother: PERSON -> PEOPLE
father: PERSON -> PEOPLE

title: JOB -> STRING
company: JOB -> STRING
salary: JOB -> INTEGER
empoyee: JOB -> PERSON

然后我想计算像:

这样的函数
childcount: PERSON -> INTEGER
childcount(P) = |{ Q in PERSON : father(Q) = P or mather(Q) = P }|

income: PERSON -> INTEGER
income(P) = SUM { salary(J) : J in JOB and empoyee(J) = P }

incomeperchild: PERSON -> INTEGER
incomeperchild(P) = income(P) / childcount(P)

parent: PERSON x PERSON -> BOOLEAN
person(P,Q) = (P = father(Q)) or (P = mother(Q))

error: PERSON -> BOOLEAN
error(P) = (female(P) and (exists Q in PERSON)(father(Q) = P))
   or (not female(P) and (exists Q in PERSON)(mother(Q) = P))
   or (exists Q in PERSON)(parent(P,Q) and error(Q))

所以基本上我想在表中计算列,这些列会在表中的值发生变化时自动更新。类似的东西可以用SQL触发器来表达,但我希望将这些功能内置到一种语言中并在内存中执行。需要优化变化的传播。有框架可以做到这一点吗?

观察者模式和反应式编程侧重于单个对象。但是我不想为表中的每一行维护指针和额外的结构,因为可能有数百万行。所有规则都是通用的(尽管它们可以通过父/子关系引用不同的行等),因此需要某种形式的递归。

1 个答案:

答案 0 :(得分:0)

解决此问题的一种方法是通过属性语法:http://www.haskell.org/haskellwiki/Attribute_grammar