逻辑编程和函数式编程的区别 [英] Difference between logic programming and functional programming
问题描述
我真的很感谢功能和逻辑编程之间的区别。
我不会说逻辑编程通过数学表达式来定义程序;这听起来更像是函数式编程。逻辑编程使用逻辑表达式(当然,最终的逻辑是数学)。
在我看来,功能编程和逻辑编程的主要区别在于构建块:函数式编程使用函数,而逻辑编程使用谓词。谓词不是一个函数;它没有返回值。根据它的论点的价值,它可能是真的或假的;如果某些值未定义,它将尝试查找将使谓词为真的值。
特别是,Prolog使用了一种特殊形式的逻辑子句,名为属于一阶逻辑的Horn子句; Hilog使用高阶逻辑的子句。
当你编写一个prolog谓词时,你定义了一个horn子句:
foo: - bar1 ,bar2,bar3。
表示如果bar1,bar2和bar3为真,则foo为true。
请注意,我并没有说过,如果且仅当;您可以为一个谓词创建多个子句:
foo: -
bar1。
foo: -
bar2。
表示如果bar1为true或者bar2为真,则foo为true
有人说逻辑编程是函数式编程的超集,因为每个函数都可以表示为一个谓词:
foo(x,y) - > X + Y。
可写为
foo(X,Y,ReturnValue): -
ReturnValue是X + Y。
但我认为这样的表述有点误导性
逻辑和功能的另一个区别是回溯。在函数式编程中,一旦输入函数体,就不会失败并移至下一个定义。例如,您可以编写
abs(x) - >
如果x> 0 x else -x
甚至使用警卫:
abs(x)x> 0 - > X;
abs(x)x =< 0 - > -X。
但您无法写入
abs(x) - >
x> 0,
x;
abs(x) - >
-x。
另一方面,在Prolog中,您可以编写
pre $ abs(X,R): -
X> 0,
R是X. $ b $ abs(X,R): -
R是-X。
如果您调用 abs(-3,R)
,Prolog会尝试第一个子句,并在执行达到 -3>时失败。 0
点,但你不会得到一个错误; Prolog会尝试第二个子句并返回 R = 3
。
我不认为这是不可能的(但我没有使用过这种语言)。
总的来说,虽然两种范例都被认为是声明性的,但它们是完全不同的;如此不同,比较他们感觉就像比较功能和命令式样。我会建议尝试一些逻辑编程;这应该是一个令人难以置信的经验。但是,你应该尝试理解哲学,而不是简单地编写程序; Prolog允许你使用函数式或者命令式编写(带有可怕的结果)。
I have been reading many articles trying to understand the difference between functional and logic programming, but the only deduction I have been able to make so far is that logic programming defines programs through mathematical expressions. But such a thing is not associated with logic programming.
I would really appreciate some light being shed on the difference between functional and logic programming.
I wouldn't say that logic programming defines programs through mathematical expressions; that sounds more like functional programming. Logic programming uses logic expressions (well, eventually logic is math).
In my opinion, the major difference between functional and logic programming is the "building blocks": functional programming uses functions while logic programming uses predicates. A predicate is not a function; it does not have a return value. Depending on the value of it's arguments it may be true or false; if some values are undefined it will try to find the values that would make the predicate true.
Prolog in particular uses a special form of logic clauses named Horn clauses that belong to first order logic; Hilog uses clauses of higher order logic.
When you write a prolog predicate you are defining a horn clause:
foo :- bar1, bar2, bar3.
means that foo is true if bar1, bar2 and bar3 is true.
note that I did not say if and only if; you can have multiple clauses for one predicate:
foo:-
bar1.
foo:-
bar2.
means that foo is true if bar1 is true or if bar2 is true
Some say that logic programming is a superset of functional programming since each function could be expressed as a predicate:
foo(x,y) -> x+y.
could be written as
foo(X, Y, ReturnValue):-
ReturnValue is X+Y.
but I think that such statements are a bit misleading
Another difference between logic and functional is backtracking. In functional programming once you enter the body of the function you cannot fail and move to the next definition. For example you can write
abs(x) ->
if x>0 x else -x
or even use guards:
abs(x) x>0 -> x;
abs(x) x=<0 -> -x.
but you cannot write
abs(x) ->
x>0,
x;
abs(x) ->
-x.
on the other hand, in Prolog you could write
abs(X, R):-
X>0,
R is X.
abs(X, R):-
R is -X.
if then you call abs(-3, R)
, Prolog would try the first clause, and fail when the execution reaches the -3 > 0
point but you wont get an error; Prolog will try the second clause and return R = 3
.
I do not think that it is impossible for a functional language to implement something similar (but I haven't used such a language).
All in all, although both paradigms are considered declarative, they are quite different; so different that comparing them feels like comparing functional and imperative styles. I would suggest to try a bit of logic programming; it should be a mind-boggling experience. However, you should try to understand the philosophy and not simply write programs; Prolog allows you to write in functional or even imperative style (with monstrous results).
这篇关于逻辑编程和函数式编程的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!