logical-purity

How to implement a not_all_equal/1 predicate

拜拜、爱过 提交于 2019-12-07 05:17:58
问题 How would one implement a not_all_equal/1 predicate, which succeeds if the given list contains at least 2 different elements and fails otherwise? Here is my attempt (a not very pure one): not_all_equal(L) :- ( member(H1, L), member(H2, L), H1 \= H2 -> true ; list_to_set(L, S), not_all_equal_(S) ). not_all_equal_([H|T]) :- ( member(H1, T), dif(H, H1) ; not_all_equal_(T) ). This however does not always have the best behaviour: ?- not_all_equal([A,B,C]), A = a, B = b. A = a, B = b ; A = a, B = b

Logical purity of when/2 and ground/1

。_饼干妹妹 提交于 2019-12-06 02:55:42
问题 The question I have a question related to logical purity. Is this program pure? when(ground(X), X > 2). Some [ir]relevant details about the context I'm trying to write pure predicates with good termination properties. For instance, I want to write a predicate list_length/2 that describes the relation between a list and its length. I want to achieve the same termination behaviour as the built-in predicate length/2 . My question seeks to find if the following predicate is pure: list_length([],

How to implement a not_all_equal/1 predicate

﹥>﹥吖頭↗ 提交于 2019-12-05 10:04:24
How would one implement a not_all_equal/1 predicate, which succeeds if the given list contains at least 2 different elements and fails otherwise? Here is my attempt (a not very pure one): not_all_equal(L) :- ( member(H1, L), member(H2, L), H1 \= H2 -> true ; list_to_set(L, S), not_all_equal_(S) ). not_all_equal_([H|T]) :- ( member(H1, T), dif(H, H1) ; not_all_equal_(T) ). This however does not always have the best behaviour: ?- not_all_equal([A,B,C]), A = a, B = b. A = a, B = b ; A = a, B = b, dif(a, C) ; A = a, B = b, dif(b, C) ; false. In this example, only the first answer should come out,

Logical purity of when/2 and ground/1

好久不见. 提交于 2019-12-04 07:32:25
The question I have a question related to logical purity. Is this program pure? when(ground(X), X > 2). Some [ir]relevant details about the context I'm trying to write pure predicates with good termination properties. For instance, I want to write a predicate list_length/2 that describes the relation between a list and its length. I want to achieve the same termination behaviour as the built-in predicate length/2 . My question seeks to find if the following predicate is pure: list_length([], 0). list_length([_|Tail], N):- when(ground(N), (N > 0, N1 is N - 1)), when(ground(N1), N is N1 + 1),

Steadfastness: Definition and its relation to logical purity and termination

北城余情 提交于 2019-12-04 00:07:28
问题 So far, I have always taken steadfastness in Prolog programs to mean: If, for a query Q , there is a subterm S , such that there is a term T that makes ?- S=T, Q. succeed although ?- Q, S=T. fails , then one of the predicates invoked by Q is not steadfast. Intuitively, I thus took steadfastness to mean that we cannot use instantiations to "trick" a predicate into giving solutions that are otherwise not only never given, but rejected . Note the difference for nonterminating programs! In

Logical Negation in Prolog

那年仲夏 提交于 2019-12-01 15:21:43
I've read quite a bit about Prolog's Negation by Failure where Prolog in order to prove that \+Goal holds tries to prove that Goal fails. This is highly connected with CWA (close world assumption) where for example if we query \+P(a) (where P is a predicate of arity 1) and we have no clues that lead to prove P(a) Prolog assumes (due to CWA) that not P(a) holds so \+P(a) succeeds. From what I've searched this is a way to solve classical logic weakness where if we had no clue about P(a) then we could not answer whether \+P(a) holds. What described above was the way of introducing non-monotonic

Logical Negation in Prolog

家住魔仙堡 提交于 2019-12-01 14:06:05
问题 I've read quite a bit about Prolog's Negation by Failure where Prolog in order to prove that \+Goal holds tries to prove that Goal fails. This is highly connected with CWA (close world assumption) where for example if we query \+P(a) (where P is a predicate of arity 1) and we have no clues that lead to prove P(a) Prolog assumes (due to CWA) that not P(a) holds so \+P(a) succeeds. From what I've searched this is a way to solve classical logic weakness where if we had no clue about P(a) then we

Steadfastness: Definition and its relation to logical purity and termination

只谈情不闲聊 提交于 2019-12-01 02:58:35
So far, I have always taken steadfastness in Prolog programs to mean: If, for a query Q , there is a subterm S , such that there is a term T that makes ?- S=T, Q. succeed although ?- Q, S=T. fails , then one of the predicates invoked by Q is not steadfast. Intuitively, I thus took steadfastness to mean that we cannot use instantiations to "trick" a predicate into giving solutions that are otherwise not only never given, but rejected . Note the difference for nonterminating programs! In particular, at least to me, logical-purity always implied steadfastness. Example . To better understand the

What is meant by “logical purity” in Prolog?

送分小仙女□ 提交于 2019-11-29 13:12:21
What is meant by "logical purity" (in the context of Prolog programming)? The logical-purity tag info says "programs using only Horn clauses" , but then, how would predicates like if_/3 qualify, using as much as it does the cut, and the various meta-logical (what's the proper terminology? var/1 and such) predicates, i.e. the low-level stuff. I get it that it achieves some "pure" effect, but what does this mean, precisely? For a more concrete illustration, please explain how does if_/3 qualify as logically pure, seen in use e.g. in this answer ? Let us first get used to a declarative reading of

Purity of Prolog predicates that use impure primitives

谁都会走 提交于 2019-11-29 02:14:01
I know that var/1 , nonvar/1 and !/0 are impure primitives, but does their use make every program that uses them impure? I wrote the following predicate plus/3 that behaves as if it were pure or at least that is what I claim. The predicate is demonstrative, not designed to be efficient. % nat(X) is true if X is a natural number nat(0). nat(X):- nonvar(X), !, X > 0. nat(X):- nat(X1), X is X1 + 1. % plus(A, B, C) is true if A,B and C are natural numbers and A+B=C plus(A, B, C):- nat(A), (nonvar(C), C < A, !, false ; true), plus_(B, A, C). plus_(A, B, C):- nat(A), (nonvar(C), C < A, !, false ;