prolog-dif

What are the uses of the fail predicate in Prolog?

不羁岁月 提交于 2019-11-27 15:28:20
I can't come up with a situation where I would need it. Elegant systems provide false/0 as a declarative synonym for the imperative fail/0 . An example where it is useful is when you manually want to force backtracking for side-effects, like: ?- between(1,3,N), format("line ~w\n", [N]), false. line 1 line 2 line 3 Instead of false/0 , you can also use any goal that fails, for example a bit shorter: ?- between(1,3,N), format("line ~w\n", [N]), 0=1. line 1 line 2 line 3 Thus, false/0 is not strictly needed but quite nice. EDIT : I sometimes see beginners who want to state for example "my

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

泄露秘密 提交于 2019-11-27 14:34:43
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 8.4 Term comparison a declarative nightmare with imps (short form for imperative constructs) all around. 8.4 Term comparison features: 8.4 Term comparison 8.4.1 (@=<)/2, (==)/2, (\==)/2, (@<)/2, (@>)/2, (@>=)/2. 8.4.2 compare/3 . 8.4.3 sort/2 . 8.4.4 keysort/2 . To give an example, consider: ?- X @< a. true. This succeeds, because 7.2 Term order An

Prolog: First duplicate value

霸气de小男生 提交于 2019-11-27 05:36:41
I need to find the first duplicate value in a list. prep(3,[1,3,5,3,5]). Should be true. prep(5,[1,3,5,3,5]). Should be false. I thought checking for equality with the current value and the previous list members until I find a duplicate, if it finds one it will test for equality with X but I have no idea how to do that in Prolog! I appreciate any help! Thanks Here is a pure version using dif/2 which implements sound inequality. dif/2 is offered by B-Prolog, YAP-Prolog, SICStus-Prolog and SWI-Prolog. firstdup(E, [E|L]) :- member(E, L). firstdup(E, [N|L]) :- non_member(N, L), firstdup(E, L).

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

让人想犯罪 __ 提交于 2019-11-26 23:34:07
问题 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 almost 1 perfect from a declarative viewpoint, it produces redundant solutions for certain queries and leaves choice points all around. What is a definition that improves upon this (in a pure manner probably using if_/3 and (=)/3) such that exactly the same set of solutions is described by different/2 but is determinate at least

Using \\==/2 or dif/2

故事扮演 提交于 2019-11-26 21:02:42
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, y). (the edges here are a -> c, b -> a, but not c -> c) The following works: edge(A, B) :- node(A, _, X), node(B, X, _), A \== B. This works too [swi-prolog]: edge(A, B) :- dif(A, B), node(A, _, X), node(B, X, _). This does not work, apparently (because neither A nor B are instantiated yet?): edge(A, B) :- A \== B, node(A, _, X), node(B, X, _). I

What are the uses of the fail predicate in Prolog?

左心房为你撑大大i 提交于 2019-11-26 17:09:50
问题 I can't come up with a situation where I would need it. 回答1: Elegant systems provide false/0 as a declarative synonym for the imperative fail/0 . An example where it is useful is when you manually want to force backtracking for side-effects, like: ?- between(1,3,N), format("line ~w\n", [N]), false. line 1 line 2 line 3 Instead of false/0 , you can also use any goal that fails, for example a bit shorter: ?- between(1,3,N), format("line ~w\n", [N]), 0=1. line 1 line 2 line 3 Thus, false/0 is

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

Deadly 提交于 2019-11-26 16:48:46
问题 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 8.4 Term comparison a declarative nightmare with imps (short form for imperative constructs) all around. 8.4 Term comparison features: 8.4 Term comparison 8.4.1 (@=<)/2, (==)/2, (\==)/2, (@<)/2, (@>)/2, (@>=)/2. 8.4.2 compare/3. 8.4.3 sort/2. 8.4.4

Prolog: a person is a sibling of himself?

眉间皱痕 提交于 2019-11-26 12:33:09
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, liz). male(tom). male(bob). female(mary). female(liz). And here are the rules: %difference(X, Y) ==> Predicate to check if two people X and Y are not the same person. difference(X, Y) :- \==(X, Y). father(X, Y) :- male(X), parent(X, Y), difference(X, Y). mother(X, Y) :- female(X), parent(X, Y), difference(X, Y). sibling(X, Y) :- difference(X, Y), mother(M, X), mother(M, Y), father(F, X), father

Prolog: First duplicate value

自闭症网瘾萝莉.ら 提交于 2019-11-26 11:38:30
问题 I need to find the first duplicate value in a list. prep(3,[1,3,5,3,5]). Should be true. prep(5,[1,3,5,3,5]). Should be false. I thought checking for equality with the current value and the previous list members until I find a duplicate, if it finds one it will test for equality with X but I have no idea how to do that in Prolog! I appreciate any help! Thanks 回答1: Here is a pure version using dif/2 which implements sound inequality. dif/2 is offered by B-Prolog, YAP-Prolog, SICStus-Prolog and

Using \==/2 or dif/2

爷,独闯天下 提交于 2019-11-26 07:49:40
问题 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, y). (the edges here are a -> c, b -> a, but not c -> c) The following works: edge(A, B) :- node(A, _, X), node(B, X, _), A \\== B. This works too [swi-prolog]: edge(A, B) :- dif(A, B), node(A, _, X), node(B, X, _). This does not work, apparently