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
6
votes
2 answers

Prolog if-then-else constructs: -> vs *-> vs. if_/3

As noted in another StackOverflow answer that I can't seem to find anymore, this pattern emerges frequently in practical Prolog code: pred(X) :- guard(X), ... pred(X) :- \+ guard(X), ... and many people try to condense this…
6
votes
1 answer

`var(A)` and order of execution

Exercise 09 on this page http://www.ic.unicamp.br/~meidanis/courses/mc336/2009s2/prolog/problemas/ asks to create a predicate that packs repeated elements into sublists. A straightforward solution is straightforward pack([], []). pack([H|T], [I|U])…
vasily
  • 2,850
  • 1
  • 24
  • 40
6
votes
4 answers

Prolog: how to avoid backtracking without cuts?

So i am trying to write a predicate in prolog that can take a list L1 and a list L2 and return a list of all the elements in L1 that are not in L2. This is what i have so far: % Append an element to a list. myappendelem(X,L,[X|L]). % True if input…
Gnurgen
  • 148
  • 3
  • 11
5
votes
5 answers

Better pure version of same_length/2

Given the frequent pure definition of same_length/2 as same_length([],[]). same_length([_|As], [_|Bs]) :- same_length(As, Bs). ?- same_length(L, [_|L]). loops. Is there a pure definition that does not loop for such cases? Something in…
false
  • 10,264
  • 13
  • 101
  • 209
5
votes
1 answer

How to implement list item deletion for all argument modes?

The following Prolog program defines a predicate deleted/3 for deleting all the occurrences of the item passed in first argument from the list passed in second argument and results in the list passed in third argument: deleted(_, [], []). deleted(X,…
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
5
votes
2 answers

How to implement a not_all_equal/1 predicate

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…
Fatalize
  • 3,513
  • 15
  • 25
5
votes
1 answer

What are the requirements a computer function must meet to be considered "monotonic"?

What are the requirements a computer function/procedure/predicate must meet to be considerd "monotonic"? Let A be some thing , Let B be some thing , Let R be a monotonic relationship between A and B , Let R_ be a non-monotonic relationship between A…
Kintalken
  • 763
  • 5
  • 9
5
votes
2 answers

Prolog program to get an (integer) number as the sum of two integer squares, why does it not work?

I'm starting learning Prolog and I want a program that given a integer P gives to integers A and B such that P = A² + B². If there aren't values of A and B that satisfy this equation, false should be returned For example: if P = 5, it should give A…
Kevin
  • 685
  • 1
  • 7
  • 16
5
votes
1 answer

is_list/1 and free variables

Here is a first observation: ?- is_list([]), is_list([_,_,_]). true. Here is another observation: ?- [] = _, [_,_,_] = _. true. Therefore, why would is_list/1 be implemented such that ?- is_list(_). false. or ?- is_list([_|_]). false. when _ can…
Fatalize
  • 3,513
  • 15
  • 25
5
votes
1 answer

Logical purity of when/2 and ground/1

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

What minimal change to my code would make it preserve logical purity?

I posted the code below as an answer to this question and user "repeat" answered and commented that it's not logically pure and "if you are interested in a minimal change to your code that makes it preserve logical-purity, I suggest posting a new…
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
4
votes
3 answers

Pure Prolog Scheme Quine

There is this paper: William E. Byrd, Eric Holk, Daniel P. Friedman, 2012 miniKanren, Live and Untagged Quine Generation via Relational Interpreters http://webyrd.net/quines/quines.pdf Which uses logic programming to find a Scheme Quine.…
user502187
4
votes
3 answers

Is pure Prolog Turing-complete, and if so, why can't it implement list intersection?

The Wikipedia section on this topic is a mess. It states: Pure Prolog is based on a subset of first-order predicate logic, Horn clauses, which is Turing-complete. Turing completeness of Prolog can be shown by using it to simulate a Turing…
4
votes
4 answers

Doubly Linked List in Prolog

I have been learning Prolog in my spare time for about 8 months to a year and now I am moving on to tackle implementing some of the classic data structures and algorithms . I am interested in achieving a doubly linked list in Prolog, but quite…
S. Selfial
  • 85
  • 4
4
votes
1 answer

Is there a cut-less way to implement same_length/3?

Say I want to assert that three lists are of the same length. I could do something like this: same_length(First, Second, Third) :- same_length(First, Second), same_length(Second, Third). This does the right thing when either First or Second are…
num1
  • 4,825
  • 4
  • 31
  • 49