定义问题而不是解决方案的编程语言?

发布于 2024-08-01 13:13:28 字数 446 浏览 5 评论 0原文

是否有任何编程语言旨在定义给定问题的解决方案,而不是定义解决问题的指令? 因此,人们将定义解决方案或最终结果应该是什么样子,而语言解释器将确定如何得出该结果。 看看编程语言列表,我不知道如何开始研究这个。

我目前能想到的最好的例子是 SQL 和 MapReduce,尽管它们都是旨在检索数据的小型语言,但它们有助于说明我想要问的问题。 但是,在编写 SQL 或 MapReduce 语句时,您定义最终结果,并且数据库决定获得最终结果集的最佳操作方案。

我可以看到这些类型的语言(如果存在的话)被用于处理大量数据或寻找一组方程的解决方案。 理想的语言将是一种能够解释定义的问题、识别哪些部分是可并行的、并跨多个进程/核心/框执行解决方案的语言。

Are there any programming languages designed to define the solution to a given problem instead of defining instructions to solve it? So, one would define what the solution or end result should look like and the language interpreter would determine how to arrive at that result. Looking at the list of programming languages, I'm not sure how to even begin to research this.

The best examples I can currently think of to help illustrate what I'm trying to ask are SQL and MapReduce, although those are both sort of mini-languages designed to retrieve data. But, when writing SQL or MapReduce statements, you're defining the end result, and the DB decides the best course of action to arrive at the end result set.

I could see these types of languages, if they exist, being used in crunching a lot of data or finding solutions to a set of equations. The dream language would be one that could interpret the defined problem, identify which parts are parallelizable, and execute the solution across multiple processes/cores/boxes.

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

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

发布评论

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

评论(15

清醇 2024-08-08 13:13:28

这些语言通常称为“第五代编程语言”。 我链接到的维基百科条目中有一些示例。

These languages are commonly referred to as 5th generation programming languages. There are a few examples on the Wikipedia entry I have linked to.

懷念過去 2024-08-08 13:13:28

最接近这样的事情是使用逻辑语言,例如 Prolog。 在这些语言中,您可以对问题的逻辑进行建模,但这并不是魔法。

The closest you can get to something like this is with a logic language such as Prolog. In these languages you model the problem's logic but again it's not magic.

唐婉 2024-08-08 13:13:28

这听起来像是对声明性语言(特别是逻辑编程语言)的描述,其中最著名的例子是 Prolog。 不过,我不知道 Prolog 是否可并行。

根据我的经验,Prolog 非常适合解决约束满足问题(必须满足一组条件的问题)——您定义输入集,定义约束(例如,必须对先前无序的数据强加的排序)输入)——但病理情况是可能的,有时逻辑推演过程需要很长时间才能完成。

如果您可以用布尔公式定义问题,则可以使用 SAT 求解器来解决它,但请注意,3SAT 问题(三变量子句上的布尔变量赋值)是 NP 完全问题,并且其一阶逻辑大兄弟,量化布尔公式问题(使用存在量词和全称量词)是 PSPACE 完备的。

有一些非常好的定理证明器是用 OCaml 和其他 FP 语言编写的; 这里是一大堆。

当然,总是有通过单纯形法的线性规划。

This sounds like a description of a declarative language (specifically a logic programming language), the most well-known example of which is Prolog. I have no idea whether Prolog is parallelizable, though.

In my experience, Prolog is great for solving constraint-satisfaction problems (ones where there's a set of conditions that must be satisfied) -- you define your input set, define the constraints (e.g., an ordering that must be imposed on the previously unordered inputs) -- but pathological cases are possible, and sometimes the logical deduction process takes a very long time to complete.

If you can define your problem in terms of a Boolean formula you could throw a SAT solver at it, but note that the 3SAT problem (Boolean variable assignment over three-variable clauses) is NP-complete, and its first-order-logic big brother, the Quantified Boolean formula problem (which uses the existential quantifier as well as the universal quantifier), is PSPACE-complete.

There are some very good theorem provers written in OCaml and other FP languages; here are a whole bunch of them.

And of course there's always linear programming via the simplex method.

Oo萌小芽oO 2024-08-08 13:13:28

声明式编程怎么样? 摘自维基百科文章(已添加重点):

在计算机科学中,声明式
编程是一种编程范式
表达了一个逻辑
计算而不描述其
控制流程。 多种语言
应用这种风格尝试
最大限度地减少或消除副作用
描述程序应该做什么
完成,而不是描述如何完成
去实现它
。 这
与命令式相反
编程,这需要一个
明确提供的算法。


What about Declarative Programming? Excerpt from wikipedia article (emphasis added):

In computer science, declarative
programming is a programming paradigm
that expresses the logic of a
computation without describing its
control flow
. Many languages
applying this style attempt to
minimize or eliminate side effects by
describing what the program should
accomplish, rather than describing how
to go about accomplishing it
. This
is in contrast with imperative
programming, which requires an
explicitly provided algorithm.

蝶舞 2024-08-08 13:13:28

我也会说 Objective Caml (OCaml)...

I would say Objective Caml (OCaml) too...

生死何惧 2024-08-08 13:13:28

这可能看起来很轻率,但从某种意义上说,这就是 stackoverflow 的本质。 您声明一个问题和/或预期结果,社区通常以代码形式提供解决方案。

将动态开放系统建模为有限数量的解决方案似乎非常困难。 我认为大多数编程语言都是命令式的,这是有原因的。 更不用说潜伏在黑暗中的大量 P = NP 问题将使这样的系统难以设计。

尽管有趣的是,如果有一个正式的框架可以利用人类输入来“处理数字”并提供解决方案,也许是命令式代码生成。 互联网和谷歌搜索引擎就是这种工具,但非常原始。

大问题和软件基本上只是用代码解决的小问题的集合。 因此,任何生成代码的系统都需要相当明确的问题集,这些问题集可以映射到或多或少的原子解决方案。

This may seem flippant but in a sense that is what stackoverflow is. You declare a problem and or intended result and the community provides the solution, usually in code.

It seems immensely difficult to model dynamic open systems down to a finite number of solutions. I think there is a reason most programming languages are imperative. Not to mention there are massive P = NP problems lurking in the dark that would make such a system difficult to engineer.

Although what would be interesting is if there was a formal framework that could leverage human input to "crunch the numbers" and provide a solution, perhaps imperative code generation. The internet and google search engines are kind of that tool but very primitive.

Large problems and software are basically just a collection of smaller problems solved in code. So any system that generated code would require fairly delimited problem sets that can be mapped to more or less atomic solutions.

七禾 2024-08-08 13:13:28

让我尝试回答...也许 Prolog 可以满足您的需求。

Let me try to answer ... may be Prolog could answer your needs.

偏爱你一生 2024-08-08 13:13:28

LINQ 也可以被视为另一种声明性 DSL(避免认为它与 SQL 太相似的论点)。 同样,您声明您的解决方案是什么样的,然后 LINQ 决定如何找到它。

这类语言的美妙之处在于,像 PLINQ(我刚刚发现的)这样的项目可以围绕它们涌现。 与 PLINQ 开发人员一起观看此视频(WMV 直接链接)关于他们如何在不修改 LINQ 语言(太多)的情况下并行化解决方案查找。

LINQ could also be considered another declarative DSL (aschewing the argument that it's too similar to SQL). Again, you declare what your solution looks like, and LINQ decides how to find it.

The beauty of these kinds of languages is that projects like PLINQ (which I just found) can spring up around them. Check out this video with the PLINQ developers (WMV direct link) on how they parallelize solution finding without modifying the LINQ language (much).

会傲 2024-08-08 13:13:28

虽然数学证明并不构成编程语言,但它们确实形成了一种形式语言,您可以在其中简单地定义解决方案(只要您允许非构造性证明)。 当然,它不是算法,所以“数学”可能不是一个可接受的答案。

While mathematical proofs don't constitute a programming language, they do form a formal language where you simply define solutions (as long as you allow nonconstructive proofs). Of course, it's not algorithmic, so "math" might not be an acceptable answer.

无可置疑 2024-08-08 13:13:28

元讨论

问题或解决方案的构成并不是绝对的,取决于您作为参考点的抽象级别。

我们来比较以下 3 种语言:SQL、C++ 和 CPU 指令。

C++ 与 CPU 指令

如果您选择数组操作作为所需的抽象级别,那么 C++ 允许您“定义问题”而不是解决方案:

array[i * 2 + 3] = 5;
array[t] = array[k - m] - 1;

请注意此 C++ 代码段没有说明的内容:内存如何布局、有多少位每个数组元素都使用哪些 CPU 寄存器保存数据,甚至算术运算将按什么顺序执行(只要结果相同)。

然而,C++ 编译器会将此代码转换为包含所有这些细节的较低级别的 CPU 指令。

在数组操作的抽象级别,C++ 是声明式的,而 CPU 指令是命令式的。

SQL 与 C++

如果您选择排序算法作为所需的抽象级别,那么 SQL 允许您“定义问题”而不是解决方案:

select *
from table
order by key

这段代码对于排序算法的抽象级别而言是声明性的,因为它声明了输出排序时不使用较低级别的概念(如数组操作)。

如果您必须在 C++ 中对数组进行排序(不使用库),则该程序将用特定排序算法的数组操作步骤来表示。

void sort(int *array, int size) {
   int key, j;
   for(int i = 1; i < size; i++) {
      key = array[i];
      j = i;
      while(j > 0 && array[j-1] > key) {
         array[j] = array[j-1];
         j--;
      }
      array[j] = key;
   }
}

此代码片段对于排序算法的抽象级别而言不是声明性的,因为它使用作为排序算法组成部分的概念(例如数组操作)。

总结

总而言之,一种语言是否定义问题或解决方案取决于您所指的问题和解决方案。

这里的许多答案都提出了示例:SQL、LINQ、Prolog、Lisp、OCaml。 我确信这些语言有许多有用的抽象级别是声明性的。

但是,不要忘记您可以在它们之上构建具有更高抽象级别的语言。

Meta Discussion

What constitutes a problem or a solution is not absolute and depends on the level of abstraction that you are taking as a reference point.

Let's compare the following 3 languages: SQL, C++, and CPU instructions.

C++ vs CPU instructions

If you choose array manipulation as the desired level of abstraction, then C++ allows you to "define the problem" instead of the solution:

array[i * 2 + 3] = 5;
array[t] = array[k - m] - 1;

Note what this C++ snippet does not state: how the memory is laid out, how many bits are used by each array element, which CPU registers hold the data, and even in which order the arithmetic operations will be performed (as long as the result is the same).

The C++ compiler, however, will translate this code to lower-level CPU instructions that will contain all of these details.

At the abstraction level of array manipulation, C++ is declarative, and CPU instructions are imperative.

SQL vs C++

If you choose a sorting algorithm as the desired level of abstraction, then SQL allows you to "define the problem" instead of the solution:

select *
from table
order by key

This snippet of code is declarative with respect to the sorting algorithm's level of abstraction because it declares that the output is sorted without using lower-level concepts (like array manipulation).

If you had to sort an array in C++ (without using a library), the program would be expressed in terms of array manipulation steps of a particular sorting algorithm.

void sort(int *array, int size) {
   int key, j;
   for(int i = 1; i < size; i++) {
      key = array[i];
      j = i;
      while(j > 0 && array[j-1] > key) {
         array[j] = array[j-1];
         j--;
      }
      array[j] = key;
   }
}

This snippet is not declarative with respect to the sorting algorithm's level of abstraction because it uses concepts (such as array manipulation) that are constituents of the sorting algorithm.

Summary

To summarize, whether a language defines problems or solutions depends on what problems and solutions you are referring to.

Many answers here have brought up examples: SQL, LINQ, Prolog, Lisp, OCaml. I am sure there are many useful levels of abstractions with respect to which these languages are declarative.

However, do not forget that you can build a language with an even higher level of abstraction on top of them.

゛清羽墨安 2024-08-08 13:13:28

口齿不清。 有很多 Lisp 系统是根据规则而不是命令式命令来定义的。 谷歌啊嘿...

Lisp. There are so many Lisp systems out there defined in terms of rules not imperative commands. Google ahoy...

鹊巢 2024-08-08 13:13:28

有各种基于 Java 的规则引擎允许声明式编程 - Drools 是我玩过的一个,看起来很有趣。

There are various Java-based rules engines which allow declarative programming - Drools is one that I've played with and it seems pretty interesting.

人疚 2024-08-08 13:13:28

许多语言定义的问题多于解决方案(不要认真对待这个问题)。

严肃地说:对 Prolog 和不同类型的 DSL 设计为声明性的又一票。

A lot of languages define more problems than solutions (don't take this one seriously).

On a serious note: one more vote for Prolog and different kinds of DSLs designed to be declarative.

李白 2024-08-08 13:13:28

我记得上大学时读过一些关于使用 DNA 进行计算的内容。 您可以将 DNA 片段放入代表问题片段的解决方案中,并以这样的方式定义它:如果 DNA 组合在一起,那么它就是一个有效的解决方案。 然后,您让化学品的特性为您解决问题,并寻找代表解决方案的成品线。 听起来有点像你所指的。

不过,我不记得这是理论上的还是已经完成的。

I remember reading something about computation using DNA back when I was in college. You would put segments of DNA in a solution that represented segments of the problem, and define it in such a way that if the DNA fits together, it's a valid solution. Then you let the properties of chemicals solve the problem for you and look for finished strands that represent a solution. It sounds sort of like what you are refering to.

I don't recall if it was theoretical or had been done, though.

不美如何 2024-08-08 13:13:28

谷歌创建了一个名为 FunSearch 的东西,它的功能与您所要求的类似。

Google created something called FunSearch that does something similar to what you’re asking.

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