Questions tagged [logical-purity]

Logical purity is the property of logic programs that are written only using Horn clauses.

There are two equally justifiable ways to characterise purity:

The first is based on the intrinsic property of the program being composed of pure predicates. A pure predicate is a true relation, which is logically sound, preserving algebraic laws like commutativity of conjunction.

The second way to characterise purity is based on the operational properties of predicates: Only monotonic (also called: "monotone") predicates are pure: If the predicate succeeds for any arguments, then it does not fail for any generalisation of these arguments, and if it fails for any combination of arguments, then it does not succeed for any specialisation of these arguments.

In addition, pure predicates must not produce side-effects.

Examples of pure predicates are (=)/2, dif/2 and CLP(FD) constraints like (#=)/2. Further examples would be (=)/2 from any E-unification.

57 questions
76
votes
10 answers

'if' in prolog?

Is there a way to do an if in prolog, e.g. if a variable is 0, then to do some actions (write text to the terminal). An else isn't even needed, but I can't find any documentation of if.
jreid9001
  • 1,207
  • 2
  • 10
  • 8
36
votes
2 answers

What use does if_/3 have?

The predicate if_/3 seems to be fairly popular among the few main contributors in the Prolog part of Stack Overflow. This predicate is implemented as such, courtesy of @false: if_(If_1, Then_0, Else_0) :- call(If_1, T), ( T == true ->…
Fatalize
  • 3,513
  • 15
  • 25
33
votes
8 answers

different/2 - does a pure, determinate definition exist?

different(Xs, Ys) :- member(X, Xs), non_member(X, Ys). different(Xs, Ys) :- member(Y, Ys), non_member(Y, Xs). While this definition using member/2 and non_member/2 is almost1 perfect from a declarative viewpoint, it produces redundant…
false
  • 10,264
  • 13
  • 101
  • 209
20
votes
6 answers

Longest common prefix (LCP) of a list of strings

lcs([ H|L1],[ H|L2],[H|Lcs]) :- !, lcs(L1,L2,Lcs). lcs([H1|L1],[H2|L2],Lcs):- lcs( L1 ,[H2|L2],Lcs1), lcs([H1|L1], L2 ,Lcs2), longest(Lcs1,Lcs2,Lcs), !. lcs(_,_,[]). longest(L1,L2,Longest) :- length(L1,Length1), …
blazing
  • 557
  • 2
  • 4
  • 20
19
votes
3 answers

Declarative uses of memberchk/2

memberchk/2 is a commonly defined predicate that is defined in terms of member/2 like so: memberchk(X, Xs) :- once(member(X, Xs)). It therefore succeeds only for the first answer of member/2. Its full procedural meaning does not fit into a pure…
false
  • 10,264
  • 13
  • 101
  • 209
18
votes
3 answers

Using \==/2 or dif/2

If I want to make sure that two variables do not instantiate to the same term, what is the preferred way to do it? Let's say I need to find directed edges in a graph, and a node cannot have an edge to itself: node(a, x, y). node(b, z, x). node(c, y,…
user1812457
17
votes
1 answer

What is meant by "logical purity" in Prolog?

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…
Will Ness
  • 70,110
  • 9
  • 98
  • 181
14
votes
2 answers

Use of redundant goals in queries

(Upon the suggestion of @repeat) Consider a query of a pure program1 ?- G_0. What use if any would the query ?- G_0, G_0. have? Footnotes 1 No tabling (to be safe), constraints are OK. Previous post on the subject.
false
  • 10,264
  • 13
  • 101
  • 209
14
votes
2 answers

Logical Negation in Prolog

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…
coder
  • 12,832
  • 5
  • 39
  • 53
14
votes
1 answer

Steadfastness: Definition and its relation to logical purity and termination

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…
mat
  • 40,498
  • 3
  • 51
  • 78
11
votes
1 answer

Features of good Prolog code?

What are the design heuristics one has to master to write good Prolog? I've heard it takes an experienced programmer about two years to become proficient in Prolog. Using recursion effectively is part of it, but that seems to be a relatively minor…
user287424
  • 1,219
  • 12
  • 22
10
votes
2 answers

Are cuts that bad in programming?

I am taking an AI course this semester in which we are learning Prolog. Our lecturer has told us to try and avoid using cuts in our assignment, however, for a couple of the questions I can't seem to avoid using them. I'm just curious why are cuts…
Achaldo
  • 163
  • 8
9
votes
2 answers

Purity of Prolog predicates that use impure primitives

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…
Tudor Berariu
  • 4,910
  • 2
  • 18
  • 29
8
votes
2 answers

Prolog : avoid redundant choice points (non-determinism) with and without cut operator

Firstly, I have read all other posts on SO regarding the usage of cuts in Prolog and definitely see the issues related to using them. However, there's still some unclarity for me and I'd like to settle this once and for all. In the trivial example…
SND
  • 1,552
  • 2
  • 16
  • 29
7
votes
2 answers

How to convert if else to fully declarative in Prolog?

It's one of my classroom question. I was able to create my own Prolog program with bunch of if-else but I was told that my program is not fully declarative as its one of the very foundation principle in Prolog. Here's my code %start with :-…
J.Doe
  • 1,097
  • 1
  • 16
  • 34
1
2 3 4