在实践中,非关系对core.logic意味着什么? [英] What does non-relational mean in practice for core.logic?

查看:115
本文介绍了在实践中,非关系对core.logic意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图通过API文档理解core.logic时,我遇到了非关系目标和关系目标.我不知道这在实践中意味着什么,为什么要注释目标(无论它们是否相关)为何很重要.

When trying to understand core.logic throgh the API docs I come across Non-Relational goals and relational goals. I have no idea what this means in practice and why it is important to annotate goals if they are relational or not.

您能否举例说明目标的使用方式,取决于它们是否相关?

Can you explain with example how the goals are used differently depending on if they are relational or not?

推荐答案

为了解释什么是非关系型,我们需要重新研究relational的含义.

In order to explain what non-relational means we need to revisit what relational means.

如果在函数式编程中考虑纯函数,则它们总是返回一个值,并且对于相同的输入参数,将返回相同的输出值.

If you consider pure functions in functional programming, they always return one value, and for the same input arguments, the same output value is returned.

例如:

 f(x) = x + 2

此函数始终为输入值3返回5.

This function always returns 5 for input value 3.

但是在很多情况下,函数是不适当的,例如平方根,有2个结果.

But there are many situations were functions are inappropriate, as square root, that has 2 results.

 sqrt(4) => 2 and -2

或将数字除以零,没有结果

Or divide a number by zero, with no results

将关系视为通用函数,您可以:

Seeing a relation as a generalized function, you have:

  • 任意数量的结果(零个或多个)
  • 不确定的
  • 每次调用的
  • 输入/输出参数可以不同
  • 如果关系为true,则
  • 关系返回 true ,否则为 false .
  • Any number of results (zero or more)
  • Non deterministic
  • in/out arguments can be different for each invocation
  • Relationships return true if relation is true and false otherwise.

为了将函数转换为关系,我们将结果设置为新参数:

In order to convert a function to a relation we set the result as a new parameter:

(cons 1 [2]) => [1 2]

(conso 1 [2] [1 2]) => true

但是现在,如果一个参数是变量,则conso可以用作生成器:

But now conso can be used as a generator if one argument is a variable:

 (run 1 [x]
    (conso 1 [2] x)) => ([1 2])

 (run 1 [x]
    (conso 1 x [1 2])) => ([2])

在逻辑编程中,统一回答了以下问题:要满足这种关系,世界应该是什么样子?

In logic programming the unification answers the question: What the world should look like for this relation to be satisfied?

non-relational运算符或函数不是作为关系而是作为简单函数的运算符,因此不可能将任何参数作为变量进行统一.

A non-relational operator or function is operator that doesn't work as a relation but as a simple function, so unification taking any parameter as variable is not possible.

例如,在命名空间clojure.core.logic.fd中引入了有限域上的CLP之前,例如><这样的运算符就发生了这种情况.

This happened for instance with operators such as > and < before CLP over finite domains was introduced in namespace clojure.core.logic.fd.

这里的许多概念都可以在Ambrose Bonnaire-Sergeant的此话题中找到.

Many of the concepts here you can find in this talk by Ambrose Bonnaire-Sergeant.

这篇关于在实践中,非关系对core.logic意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆