meta-predicate

Why do we use '!' in prolog

匆匆过客 提交于 2019-11-29 18:25:24
This is the code that i am trying to understand. co(X) :- co(X,[],L). co([],A,A):- write(A). co([X|Xs], A, L) :- p(X-Z,A,R), !, Z1 is Z+1, co(Xs, [X-Z1|R], L). co([X|Xs], A, L) :- co(Xs, [X-1|A], L). p(X-Y,[X-Y|R],R):- !. p(X,[H|Y], [H|Z]) :- p(X,Y,Z). What is the use of '!' and predicate p(,,) in the above code. OR Can anybody just add comments in every step of the above code so that i can able to understand . Thanks. mat Beginners tend to use !/0 because they are not aware of its negative consequences. This is because most Prolog textbooks that are popular among beginners are quite bad and

Why do we use '!' in prolog

爷,独闯天下 提交于 2019-11-28 14:16:53
问题 This is the code that i am trying to understand. co(X) :- co(X,[],L). co([],A,A):- write(A). co([X|Xs], A, L) :- p(X-Z,A,R), !, Z1 is Z+1, co(Xs, [X-Z1|R], L). co([X|Xs], A, L) :- co(Xs, [X-1|A], L). p(X-Y,[X-Y|R],R):- !. p(X,[H|Y], [H|Z]) :- p(X,Y,Z). What is the use of '!' and predicate p(,,) in the above code. OR Can anybody just add comments in every step of the above code so that i can able to understand . Thanks. 回答1: Beginners tend to use !/0 because they are not aware of its negative

Pairwise relation over list

不打扰是莪最后的温柔 提交于 2019-11-28 13:23:06
The following higher order predicate succeeds if all pairs of the list's elements are true for a given relation. Is there a common or better, more intention revealing name for this relation? My original motivation for this name was that in clpfd , there is often a constraint all_different/1 which is described as being true, iff the elements are pairwise different . In fact, rather preferred to say the elements are all different, but I have been frequently corrected (by fellow Prolog programmers) to use pairwise different. In fact, this constraint can now most naturally be expressed as pairwise

Prolog GNU - Univ operator? Explanation of it

本秂侑毒 提交于 2019-11-27 21:26:58
So the univ operator. I don't exactly understand it. For example this: foo(PredList,[H|_]) :- bar(PredList,H). foo(PredList,[_|T]) :- foo(PredList,T),!. bar([H|_],Item) :- G =.. [H,Item],G. bar([_|T],Item) :- bar(T,Item). What is this doing? This looks to see if another predicate is true. I don't understand what the ".." does. How would you rewrite this without the univ operator? Univ ( =.. ) breaks up a term into a list of constituents, or constructs a term from such a list. Try: ?- f(x,y) =.. L. L = [f, x, y]. ?- f(x,y,z) =.. [f|Args]. Args = [x, y, z]. ?- Term =.. [g,x,y]. Term = g(x, y).

Collect all “minimum” solutions from a predicate

霸气de小男生 提交于 2019-11-27 05:28:16
Given the following facts in a database: foo(a, 3). foo(b, 2). foo(c, 4). foo(d, 3). foo(e, 2). foo(f, 6). foo(g, 3). foo(h, 2). I want to collect all first arguments that have the smallest second argument, plus the value of the second argument. First try: find_min_1(Min, As) :- setof(B-A, foo(A, B), [Min-_|_]), findall(A, foo(A, Min), As). ?- find_min_1(Min, As). Min = 2, As = [b, e, h]. Instead of setof/3 , I could use aggregate/3 : find_min_2(Min, As) :- aggregate(min(B), A^foo(A, B), Min), findall(A, foo(A, Min), As). ?- find_min_2(Min, As). Min = 2, As = [b, e, h]. NB This only gives the

Prolog GNU - Univ operator? Explanation of it

做~自己de王妃 提交于 2019-11-26 20:40:17
问题 So the univ operator. I don't exactly understand it. For example this: foo(PredList,[H|_]) :- bar(PredList,H). foo(PredList,[_|T]) :- foo(PredList,T),!. bar([H|_],Item) :- G =.. [H,Item],G. bar([_|T],Item) :- bar(T,Item). What is this doing? This looks to see if another predicate is true. I don't understand what the ".." does. How would you rewrite this without the univ operator? 回答1: Univ ( =.. ) breaks up a term into a list of constituents, or constructs a term from such a list. Try: ?- f(x

Prolog: Filtering a list?

会有一股神秘感。 提交于 2019-11-26 11:53:34
I'm currently working on a very short project on Prolog, and just got stuck trying to apply a "filter" I have created to a list. I have what you could call the filter ready, but I can't apply it. It'd be better if I illustrate: filter(A, B) ...outputs 'true' if certain conditions are met. filterList(A, [X, Y, Z]) ...outputs a list which includes all elements from the second argument that make the filter output false . (So if filter(A, X) is true, the output is [Y, Z] ). I have the "filter" function ready, but now I need to apply it to a list as shown on the second example, excluding all

Collect all “minimum” solutions from a predicate

谁说胖子不能爱 提交于 2019-11-26 11:35:10
问题 Given the following facts in a database: foo(a, 3). foo(b, 2). foo(c, 4). foo(d, 3). foo(e, 2). foo(f, 6). foo(g, 3). foo(h, 2). I want to collect all first arguments that have the smallest second argument, plus the value of the second argument. First try: find_min_1(Min, As) :- setof(B-A, foo(A, B), [Min-_|_]), findall(A, foo(A, Min), As). ?- find_min_1(Min, As). Min = 2, As = [b, e, h]. Instead of setof/3 , I could use aggregate/3 : find_min_2(Min, As) :- aggregate(min(B), A^foo(A, B), Min)

Definition of a path/trail/walk

家住魔仙堡 提交于 2019-11-26 01:22:40
问题 Many predicates define some kind of an acyclic path built from edges defined via a binary relation, quite similarly to defining transitive closure. A generic definition is thus called for. Note that the notions defined in graph theory do not readily match what is commonly expected. Most notably, we are not interested in the edges\' names. Worse, also graph theory has changed a bit, introducing the notion of walk, noting Traditionally, a path referred to what is now usually known as an open

Prolog map procedure that applies predicate to list elements

点点圈 提交于 2019-11-25 22:58:08
问题 How do you write a Prolog procedure map(List, PredName, Result) that applies the predicate PredName(Arg, Res) to the elements of List , and returns the result in the list Result ? For example: test(N,R) :- R is N*N. ?- map([3,5,-2], test, L). L = [9,25,4] ; no 回答1: This is usually called maplist/3 and is part of the Prolog prologue. Note the different argument order! :- meta_predicate maplist(2, ?, ?). maplist(_C_2, [], []). maplist( C_2, [X|Xs], [Y|Ys]) :- call(C_2, X, Y), maplist( C_2, Xs,