logical-purity

Declarative uses of memberchk/2

风流意气都作罢 提交于 2019-11-28 10:49:49
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 relation. As an example for its non-relational behavior consider ?- memberchk(b, [X,b]), X = a. false. ?- X = a, memberchk(b, [X,b]). X = a. On the other hand, in many cases memberchk/2 will be called with sufficiently instantiated arguments, where it can be seen as an efficient approximation of a pure relation. One such pure relation behind is memberd

What use does if_/3 have?

若如初见. 提交于 2019-11-28 08:55:19
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 -> call(Then_0) ; T == false -> call(Else_0) ; nonvar(T) -> throw(error(type_error(boolean,T),_)) ; /* var(T) */ throw(error(instantiation_error,_)) ). However, I have been unable to find a clear, simple, and concise explanation of what this predicate does, and what use it has compared to e.g. the classical if-then-else construct of Prolog if -> then ; else . Most

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

心已入冬 提交于 2019-11-28 01:47:11
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 for ground queries (thus does not leave any useless choice points open) and omits (if possible) any

'if' in prolog?

梦想的初衷 提交于 2019-11-27 20:21:39
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. stonemetal A standard prolog predicate will do this. isfive(5). will evaluate to true if you call it with 5 and fail(return false) if you run it with anything else. For not equal you use \= isNotEqual(A,B):- A\=B. Technically it is does not unify, but it is similar to not equal. Learn Prolog Now is a good website for learning prolog. Edit: To add another example. isEqual(A,A). Yes, there is such a control

Declarative uses of memberchk/2

安稳与你 提交于 2019-11-27 05:54:13
问题 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 relation. As an example for its non-relational behavior consider ?- memberchk(b, [X,b]), X = a. false. ?- X = a, memberchk(b, [X,b]). X = a. On the other hand, in many cases memberchk/2 will be called with sufficiently instantiated arguments, where it

'if' in prolog?

雨燕双飞 提交于 2019-11-27 04:27:21
问题 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. 回答1: A standard prolog predicate will do this. isfive(5). will evaluate to true if you call it with 5 and fail(return false) if you run it with anything else. For not equal you use \= isNotEqual(A,B):- A\=B. Technically it is does not unify, but it is similar to not equal. Learn Prolog Now is a good website

Declarative uses of memberchk/2

大兔子大兔子 提交于 2019-11-27 03:57:17
问题 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 relation. As an example for its non-relational behavior consider ?- memberchk(b, [X,b]), X = a. false. ?- X = a, memberchk(b, [X,b]). X = a. On the other hand, in many cases memberchk/2 will be called with sufficiently instantiated arguments, where it

What use does if_/3 have?

天大地大妈咪最大 提交于 2019-11-27 02:31:35
问题 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 -> call(Then_0) ; T == false -> call(Else_0) ; nonvar(T) -> throw(error(type_error(boolean,T),_)) ; /* var(T) */ throw(error(instantiation_error,_)) ). However, I have been unable to find a clear, simple, and concise explanation of what this predicate does, and

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