Questions tagged [prolog-dif]

dif/2 is a Prolog built-in predicate to express in a sound manner syntactic inequality.

dif/2 is a Prolog built-in predicate to express in a sound manner syntactic inequality, also called disequality of terms. dif/2 is true if and only if its arguments are different terms. Hence the name, which is short for "different".

dif/2 is available directly in B, Prolog IV, SICStus, SWI, YAP and as a library in Ciao, IF, XSB. It was available already in the very first Prolog system, Prolog 0.

dif/2 cannot be directly expressed in ISO Prolog, but a safe approximation is still possible.

Further reading:

109 questions
44
votes
5 answers

What is the logical 'not' in Prolog?

The problem that I face, is a bit trivial. I want to use logical not in Prolog, but it seems that not/1 is not the thing that I want: course(ai). course(pl). course(os). have(X,Y) :- course(X),course(Y),not(X = Y). I query: have(X,Y),…
Masood Delfarah
  • 687
  • 3
  • 7
  • 13
40
votes
6 answers

Reification of term equality/inequality

Pure Prolog programs that distinguish between the equality and inequality of terms in a clean manner suffer from execution inefficiencies ; even when all terms of relevance are ground. A recent example on SO is this answer. All answers and all…
false
  • 10,264
  • 13
  • 101
  • 209
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
33
votes
8 answers

How to define (and name) the corresponding safe term comparison predicates in ISO Prolog?

Standard term order (ISO/IEC 13211-1 7.2 Term order) is defined over all terms — including variables. While there are good uses for this — think of the implementation of setof/3, this makes many otherwise clean and logical uses of the built-ins in…
false
  • 10,264
  • 13
  • 101
  • 209
22
votes
5 answers

What are the uses of the fail predicate in Prolog?

I can't come up with a situation where I would need it.
Igor
  • 2,673
  • 5
  • 33
  • 39
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
4 answers

How to express a disjunction of inequalities compactly without redundant answers/solutions

Consider what I have tried: dif_to_orto(A, B, C) :- ( dif(A, B) ; dif(A, C) ). While this definition is fine from a declarative viewpoint it contains many redundancies. Think of: ?- dif_to_orto(A, B, C), A = 1, B = 2, C = 2. A = 1, B…
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
12
votes
6 answers

Remove leading zeros in list in Prolog

I have a list with an unknown number of zeros at the beginning of it, for example [0, 0, 0, 1, 2, 0, 3]. I need this list to be stripped of leading zeros, so that it would look like [1, 2, 0 , 3]. Here's what I have: lead([Head | _], _) :- Head =\=…
bendl
  • 1,583
  • 1
  • 18
  • 41
9
votes
3 answers

Simplified Travelling Salesman in Prolog

I've looked through the similar questions but can't find anything that's relevant to my problem. I'm struggling to find an algorithm or set of 'loops' that will find a path from CityA to CityB, using a database…
g.a.kilby
  • 93
  • 1
  • 1
  • 3
9
votes
3 answers

Guard clauses in prolog?

Do they exist? How are they implemented? The coroutining predicates of SWI-Prolog (freeze, when, dif etc.) have the functionality of guards. How do they fit in the preferred Prolog programming style? I am very new to logic programming (with Prolog…
user1812457
9
votes
4 answers

Deleting all occurrences of an element from a list

Trying to write a procedure that given a value and a list, it deletes all the occurence of that value in the list a wrote: delMember(X, [], []) :- !. delMember(X, [X|Xs], Y) :- !, delMember(X, Xs, Y). delMember(X, [T|Xs], Y) :- !, delMember(X, Xs,…
Aslan986
  • 9,984
  • 11
  • 44
  • 75
8
votes
3 answers

Creating and working with an explicit list vs enumeration through fail

I come up against this all the time, and I'm never sure which way to attack it. Below are two methods for processing some season facts. What I'm trying to work out is whether to use method 1 or 2, and what are the pros and cons of each, especially…
magus
  • 1,347
  • 7
  • 13
8
votes
2 answers

Prolog: a person is a sibling of himself?

I'm having some trouble understanding why my code in prolog does something based on the order I put my rules in. Here is my database: parent(tom, bob). parent(tom, liz). parent(mary, bob). parent(mary,…
user84399
  • 83
  • 3
8
votes
3 answers

Force Prolog to choose unique values of variables

OK I am new to Prolog, so excuse me if this is something trivial, but I can't seem to find a proper elegant answer to this. I am trying to work out the exercise here on learnprolognow.org, exercise 2.4 (the crossword). The exercise provides these…
jbx
  • 21,365
  • 18
  • 90
  • 144
1
2 3 4 5 6 7 8