4 ms·
This is not correct. Clauses of a predicate are evaluated in order, which can be significant if, for example, you want to do in-order vs pre-order vs post-order
by hackyhacky 7y ago
This is not correct. Clauses of a predicate are evaluated in order, which can be significant if, for example, you want to do in-order vs pre-order vs post-order traversal of a binary tree.
inorderTraversal(t(V, L, R), Z) :- inorderTraversal(L, Z).
inorderTraversal(t(V, L, R), V).
inorderTraversal(t(V, L, R), Z) :- inorderTraversal(R, Z).
Also, the meaning of ordering of goals with a rule may be significant:
?- not(member(X,[a,b,c])), X=f.
false.
?- X=f, not(member(X,[a,b,c])).
X = f.
Although Prolog aspires to represent logic, it is still really just a logic-flavored programming language.
- triska 7y agoWhen you say "evaluated", then this is a procedural aspect that depends on the concrete evaluation strategy that is being used. For example, we can evaluate logic programs top down, bottom up, using various forms of resolution etc. Mercury will reorder goals automatically to satisfy certain mode constraints etc. Importantly, the actual meaning of a logic program in the sense of solutions it describes are not influenced by different evaluation strategies, although some may be more effective or efficient than others. As to the examples using not/1: This is an extra-logical predicate where this flexibility indeed does not apply, since its meaning depends on the evaluation strategy. However, for this concrete case, we can use the purely logical predicate memberd_t/3 to retain desirable logical properties such as commutativity of conjunction: ?- memberd_t(X, [a,b,c], true), X = f. false. ?- X = f, memberd_t(X, [a,b,c], true). false. memberd_t/3 is available in library(reif) for various Prolog systems.
- kragen 7y ago> then this is a procedural aspect that depends on the concrete evaluation strategy that is being used. Yes, but Prolog specifies a particular concrete evaluation strategy, and you have to rely on it to write practical programs.
- triska 7y agoThe way I see it, a key attraction of Prolog and logic programming languages in general is that different evaluation strategies are possible, and in fact also starting to be implemented in several systems to various degrees. For example, an increasing number of Prolog systems provide, in addition to SLDNF resolution (which is the the default evaluation strategy), also SLG resolution (tabling) as an evaluation strategy with different termination and performance characteristics. These different strategies are very useful in practical programs, and it would be limiting if one were to prevent them by using extra-logical predicates.
- kragen 7y agoI agree.