等价关系代数和图灵语言

发布于 2024-09-26 22:58:29 字数 458 浏览 7 评论 0原文

我正在启动一个项目,我想尝试用类似 java 的编程语言来识别关系代数。

例如,给定两个关系: 美国演员(姓名) EuropianActor(Name)

RA 中的以下表达式:

AmericanActor U EuropianActor

应等效于以下程序:

void RAMethod(Set<Name> AmericanActors, Set<Name> EuropianActors) {
  Set<Name> xs = new Set<Name>();

  for(R r : rs) {
     xs.Add(r);
  }

  for(S s : ss) {
     xs.Add(s);
  }

  return xs;
}

我正在寻找有关此主题的任何出版物。

I'm starting a project where I want to try to recognize relational algebra's in java-like programming language.

For example, given two relations:
AmericanActor(Name)
EuropianActor(Name)

The following expression in RA:

AmericanActor U EuropianActor

should be equivalent to the following program:

void RAMethod(Set<Name> AmericanActors, Set<Name> EuropianActors) {
  Set<Name> xs = new Set<Name>();

  for(R r : rs) {
     xs.Add(r);
  }

  for(S s : ss) {
     xs.Add(s);
  }

  return xs;
}

I'm looking for any publications about this topic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后eg是否自 2024-10-03 22:58:29

我个人不知道有任何软件库可以做到这一点。
但是,如果我要做这样的事情,我会编写一个解析器,将关系代数表达式拆分为标记。然后调用您编写的“RAMethod”,以便根据令牌列表中的关系代数运算符在幕后执行关系代数。

I don't know personally any software libraries that do this.
But if I was going to do something like this, I would write a parser that splits up your relational algebra expressions into tokens. Then call your "RAMethod" that you wrote in order to do the relational algebra behind the scenes based on which relational algebra operators are in the token list.

时光暖心i 2024-10-03 22:58:29

你可以这样实现关系代数:

 Set<T> union(Set<T> a, Set<T> b) {
    Set<T> res = new Set<T>();
    for (T i: a) res.Add(i);
    for (T i: b) res.Add(i);
    return res;
 }

 Set<T> projection(Set<Pair<T,U>> a) {
    Set<T> res = new Set<T>();
    for (Pair<T,U> i: a) x.Add(i.first);
    return res;
 }

 Set<T> selection(Set<T> a, Predicate<T> p) {
    Set<T> res = new Set<T>();
    for (T i: a) if (p.Call(i)) x.Add(i);
    return res;
 }

 Set<Pair<T,U>> cross(Set<T> a, Set<U> b) {
    Set<Pair<T,U>> res = new Set<Pair<T,U>>();
    for (T i: a) for (U j: b) x.Add(Pair(i,j));
    return res;
 }

然后直接在代码中编写关系代数:

selection(projection(union(A,B)), isPositive)

基本上“投影”对应“地图”,“选择”对应“过滤器”。

显然并不是每个代码都对应一个关系代数表达式。如果您限制语言没有 while、异常等,您可以将条件转换为选择,将多个嵌套循环转换为叉积,添加到联合等。形式化这是另一个故事。

您可能更喜欢使用更具声明性的语言,例如 Haskell 或 ML;这是使用列表的实现:

type Database a = [a]
select :: (a -> Bool) -> Database a -> Database a
select = filter

projectFirst :: Database (a,b) -> Database a
projectFirst = map fst

projectSecond :: Database (a,b) -> Database b
projectSecond = map snd

union :: Database a -> Database a -> Database a
union = (++)

You can implement relational algebra like this:

 Set<T> union(Set<T> a, Set<T> b) {
    Set<T> res = new Set<T>();
    for (T i: a) res.Add(i);
    for (T i: b) res.Add(i);
    return res;
 }

 Set<T> projection(Set<Pair<T,U>> a) {
    Set<T> res = new Set<T>();
    for (Pair<T,U> i: a) x.Add(i.first);
    return res;
 }

 Set<T> selection(Set<T> a, Predicate<T> p) {
    Set<T> res = new Set<T>();
    for (T i: a) if (p.Call(i)) x.Add(i);
    return res;
 }

 Set<Pair<T,U>> cross(Set<T> a, Set<U> b) {
    Set<Pair<T,U>> res = new Set<Pair<T,U>>();
    for (T i: a) for (U j: b) x.Add(Pair(i,j));
    return res;
 }

and then directly write relational algebra in code:

selection(projection(union(A,B)), isPositive)

basically "projection" corresponds to "map" and "selection" to "filter".

Obviously not every code corresponds to a relational algebra expression. If you constrain to language without while, exceptions etc. you could translate conditionals to selection, multiple nested loops to cross product, adding to union etc. Formalising that is another story.

You might be more comfortable with a more declarative language, like Haskell or ML; here's an implementation using lists:

type Database a = [a]
select :: (a -> Bool) -> Database a -> Database a
select = filter

projectFirst :: Database (a,b) -> Database a
projectFirst = map fst

projectSecond :: Database (a,b) -> Database b
projectSecond = map snd

union :: Database a -> Database a -> Database a
union = (++)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文