我所说的逻辑编程是指声明性编程语言的子范式。 不要把这个问题和“用if-then-else能解决什么问题?”搞混了
像Prolog这样的语言很迷人,为了学而值得学,但我不得不想知道是什么课现实世界的问题可以通过这种语言得到最好的表达和解决。 有更好的语言吗? 在更流行的编程语言中,逻辑编程是否以另一个名称存在? 答案的愤世嫉俗版本是 Python 悖论 的变体吗?
By Logic Programming I mean the a sub-paradigm of declarative programming languages. Don't confuse this question with "What problems can you solve with if-then-else?"
A language like Prolog is very fascinating, and it's worth learning for the sake of learning, but I have to wonder what class of real-world problems is best expressed and solved by such a language. Are there better languages? Does logic programming exist by another name in more trendy programming languages? Is the cynical version of the answer a variant of the Python Paradox?
发布评论
评论(8)
原型制作。
Prolog 是充满活力的,并且已经存在了 50 年。 编译器是自由的,语法是简约的,“做事”是简单、有趣和高效的。 SWI-Prolog 有一个内置的跟踪器(调试器!),甚至 图形跟踪器。 您可以使用
make/0
即时更改代码,您可以动态加载模块,在不离开解释器的情况下添加几行代码,或者编辑当前正在即时运行的文件使用编辑(1)
。 您认为您发现了foobar/2
谓词有问题吗?一旦你离开编辑器,那个东西就会被重新编译。 当然,Eclipse 对 Java 做了同样的事情,但 Java 并不完全是一种原型语言。
除了纯粹的原型设计之外,Prolog 非常适合将一段逻辑转换为代码。 因此,自动证明器和此类内容可以轻松地用 Prolog 编写。
第一个 Erlang 解释器是用 Prolog 编写的 - 这是有原因的,因为 Prolog 非常适合解析和编码在解析树中找到的逻辑。 事实上,Prolog 带有一个内置的解析器! 不,不是一个库,它是在语法中,即 DCGs 。
Prolog 在 NLP 中被大量使用,特别是在语法和计算语义方面。
但是,Prolog 并未得到充分利用和重视。 不幸的是,它似乎带有学术或“无法用于任何实际目的”的耻辱。 但它可以在许多涉及事实和事实之间关系计算的现实应用中得到很好的利用。 它不太适合数字运算,但 CS 不仅仅涉及数字运算。
Prototyping.
Prolog is dynamic and has been for 50 years. The compiler is liberal, the syntax minimalist, and "doing stuff" is easy, fun and efficient. SWI-Prolog has a built-in tracer (debugger!), and even a graphical tracer. You can change the code on the fly, using
make/0
, you can dynamically load modules, add a few lines of code without leaving the interpreter, or edit the file you're currently running on the fly withedit(1)
. Do you think you've found a problem with thefoobar/2
predicate?And as soon as you leave the editor, that thing is going to be re-compiled. Sure, Eclipse does the same thing for Java, but Java isn't exactly a prototyping language.
Apart from the pure prototyping stuff, Prolog is incredibly well suited for translating a piece of logic into code. So, automatic provers and that type of stuff can easily be written in Prolog.
The first Erlang interpreter was written in Prolog - and for a reason, since Prolog is very well suited for parsing, and encoding the logic you find in parse trees. In fact, Prolog comes with a built-in parser! No, not a library, it's in the syntax, namely DCGs.
Prolog is used a lot in NLP, particularly in syntax and computational semantics.
But, Prolog is underused and underappreciated. Unfortunately, it seems to bear an academic or "unusable for any real purpose" stigma. But it can be put to very good use in many real-world applications involving facts and the computation of relations between facts. It is not very well suited for number crunching, but CS is not only about number crunching.
由于 Prolog = 句法统一 + 向后链接 + REPL,
大多数使用句法统一的地方也是 Prolog 的一个很好的用途。
Since Prolog = Syntactic Unification + Backward chaining + REPL,
most places where syntactic unification is used is also a good use for Prolog.
约束逻辑编程 (CLP) 前面
已经提到了许多非常好的且非常适合的逻辑编程用例。 我想用来自逻辑编程极其重要的应用领域的几项任务来补充现有的列表:
逻辑编程与约束无缝地融合在一起,比其他范式更无缝,从而产生了一个名为的框架约束逻辑编程。
这导致了不同域的专用约束求解器,例如:
这些专用的约束求解器导致了逻辑编程的几个尚未提及的重要用例,我在下面展示了其中一些用例。
选择 Prolog 系统时,其约束求解器的功能和性能通常是决定因素,特别是对于商业用户而言。
CLP(FD) — 整数推理
实际上,CLP(FD) 是逻辑编程最重要的应用之一,用于解决以下领域的任务:
参见clpfd 了解更多信息和几个示例。
CLP(B) — 布尔约束
CLP(B) 通常与以下各项结合使用:
请参阅clpb。
CLP(Q) — 有理数
CLP(Q) 用于解决运筹学中出现的重要类别问题:
请参阅clpq。
Constraint Logic Programming (CLP)
Many very good and well-suited use cases of logic programming have already been mentioned. I would like to complement the existing list with several tasks from an extremely important application area of logic programming:
Logic programming blends seamlessly, more seamlessly than other paradigms, with constraints, resulting in a framework called Constraint Logic Programming.
This leads to dedicated constraint solvers for different domains, such as:
These dedicated constraint solvers lead to several important use cases of logic programming that have not yeen been mentioned, some of which I show below.
When choosing a Prolog system, the power and performance of its constraint solvers are often among the deciding factors, especially for commercial users.
CLP(FD) — Reasoning over integers
In practice, CLP(FD) is one of the most imporant applications of logic programming, and is used to solve tasks from the following areas, among others:
See clpfd for more information and several examples.
CLP(B) — Boolean constraints
CLP(B) is often used in connection with:
See clpb.
CLP(Q) — Rational numbers
CLP(Q) is used to solve important classes of problems arising in Operations Research:
See clpq.
Prolog 免费为您提供的功能之一是回溯搜索算法——您可以自己实现它,但如果您的问题通过使用该算法可以得到最好的解决,那么使用它就好了。
我发现它擅长的两件事是数学证明和自然语言理解。
One of the things Prolog gives you for free is a backtracking search algorithm -- you could implement it yourself, but if your problem is best solved by having that algorithm available, then it's nice to use it.
The two things I've seen it be good at is mathematical proofs and natural language understanding.
Prolog 非常适合解决非数字问题。 这篇文章给出了Prolog的一些应用程序的一些示例,它可能会帮助您理解问题的类型它可能会解决。
Prolog is ideal for non-numeric problems. This article gives a few examples of some applications of Prolog and it might help you understand the type of problems that it might solve.
Prolog 非常擅长解决谜题等问题。 也就是说,在解谜领域,它使简单/中等的谜题解决变得更容易,而复杂的谜题解决则更加困难。 尽管如此,为诸如 Hexiom、Sudoku 或 Nurikabe 之类的网格谜题编写求解器并不是特别困难。
Prolog is great at solving puzzles and the like. That said, in the domain of puzzle-solving it makes easy/medium puzzle-solving easier and complicated puzzle solving harder. Still, writing solvers for grid puzzles and the like such as Hexiom, Sudoku, or Nurikabe is not especially tough.
一个简单的答案是“构建系统”。 用于构建Makefile(至少是描述依赖关系的部分)的语言本质上是一种逻辑编程语言,尽管并不是真正的“纯粹”逻辑编程语言。
One simple answer is "build systems". The language used to build Makefiles (at least, the part to describe dependencies) is essentially a logic programming language, although not really a "pure" logic programming language.
是的,Prolog 自 1972 年就已存在。 它是由 Alain Colmerauer 和 Philippe Roussel 基于 Robert Kowalski 对 Horn 条款的程序解释而发明的。 阿兰 (Alain) 是法国计算机科学家,1970 年至 1995 年间担任艾克斯-马赛大学教授。
阿兰发明它是为了分析自然语言。 他和他的“追随者”创造了几个成功的原型。
所以你可以想象这就是它的力量所在。
他于 2006 年退休,一直活跃到 2017 年去世。1986 年,他被法国政府授予法国荣誉军团骑士勋章。
Yes, Prolog has been around since 1972. It was invented by Alain Colmerauer with Philippe Roussel, based on Robert Kowalski's procedural interpretation of Horn clauses. Alain was a French computer scientist and professor at Aix-Marseille University from 1970 to 1995.
And Alain invented it to analyse Natural Language. Several successful prototypes were created by him and his "followers".
So you can imagine that's where it's strength is.
Retired in 2006, he remained active until he died in 2017. He was named Chevalier de la Legion d’Honneur by the French government in 1986.