prolog-setof

What is the Prolog operator ^?

≡放荡痞女 提交于 2019-12-05 06:16:54
What is the Prolog operator ^ ? Looking at The Prolog Built-in Directive op gives a list of the built-in operators. I see ** is exponentiation /\ is or but what is ^ ? Each of the three current answers are of value and I learned something: Roy for the book false for the examples I accepted the answer by CapelliC because it made clear that ^/2 has multiple meanings depending on context which instantly cleared up my confusion. In Prolog, most symbols can be used 'uninterpreted', at syntactic level, in particular after a op/3 declaration, any atom can be used as operator . Then you can use, for

Correct use of findall/3, especially the last result argument

倖福魔咒の 提交于 2019-12-02 06:46:07
问题 I'm a beginner in Prolog and I am dealing with a problem that might seem stupid to you, but I really can't understand what I'm doing wrong! Ok, I have this file fruits.pl and inside that I have something like this: fruit(apple,small,sweet). fruit(lemon,small,nosweet). fruit(melon,big,sweet). I have already (inside that file made a coexist(X,Y) atom that checks if two fruits can be put together in a plate. It works fine! But now I can't create a suggest(X) that takes as a parameter a fruit and

How do I find all solutions to a goal in Prolog?

孤者浪人 提交于 2019-11-30 17:33:39
I have predicate P1 that returns values one after the other like this: -? P1(ARGUMENTS, RETURN). -? RETURN = 1; -? RETURN = 2; -? RETURN = 3; -? fail. I also have another predicate called P2: P2(ARGUMENTS, LIST) :- P1(ARGUMENTS, RETURN),... % SOMEHOW HERE I NEED TO INSERT ALL VALUES OF RETURN TO LIST. How do find all of the values of RETURN and assign them to LIST ? Pesto Use findall to accomplish this: P2(ARGUMENTS, LIST) :- findall(X, P1(ARGUMENTS, X), LIST). This is related to the bagof function mentioned in the question linked to by Anders Lindahl. There is a good explanation on the

Prolog - how to do setof that returns empty list instead of failing

依然范特西╮ 提交于 2019-11-30 09:08:03
问题 I need an ordered list of Objects that satisfy Goal. setof takes care of the ordering, but fails when no Objects satisfy Goal. I want to return an empty list instead like findall does. This works, but is there a way of accomplishing this without a cut? I'm using SWI-Prolog. setof(Object, Goal, List), !; List = []. 回答1: First, ..., ( setof(Object, Goal, List), ! ; List = [] ), ... does not work, as you suggest. It always succeeds for List = [] , and it only shows the first answer of setof/3 .

How do I find all solutions to a goal in Prolog?

别说谁变了你拦得住时间么 提交于 2019-11-30 01:16:39
问题 I have predicate P1 that returns values one after the other like this: -? P1(ARGUMENTS, RETURN). -? RETURN = 1; -? RETURN = 2; -? RETURN = 3; -? fail. I also have another predicate called P2: P2(ARGUMENTS, LIST) :- P1(ARGUMENTS, RETURN),... % SOMEHOW HERE I NEED TO INSERT ALL VALUES OF RETURN TO LIST. How do find all of the values of RETURN and assign them to LIST ? 回答1: Use findall to accomplish this: P2(ARGUMENTS, LIST) :- findall(X, P1(ARGUMENTS, X), LIST). This is related to the bagof

Prolog - how to do setof that returns empty list instead of failing

不羁的心 提交于 2019-11-29 12:12:17
I need an ordered list of Objects that satisfy Goal. setof takes care of the ordering, but fails when no Objects satisfy Goal. I want to return an empty list instead like findall does. This works, but is there a way of accomplishing this without a cut? I'm using SWI-Prolog. setof(Object, Goal, List), !; List = []. First, ..., ( setof(Object, Goal, List), ! ; List = [] ), ... does not work, as you suggest. It always succeeds for List = [] , and it only shows the first answer of setof/3 . But setof/3 may produce several answers. The general method that works in any Prolog is: ..., ( \+ Goal ->

existential qualifier in prolog, using setof / bagof

家住魔仙堡 提交于 2019-11-29 11:07:13
I had a quick question re. existential qualifier using setof in prolog (i.e. ^). using SICStus it seems that (despite what a number of websites claim), S does indeed appear to be quantified in the code below (using the bog standard, mother of / child of facts, which i havent included here): child(M,F,C) :- setof(X,(mother(S,X)),C). i check the unification using: child(M,F,C) :- setof(X-S,(mother(S,X)),C). so the following code, with the existential operator seem to make no difference: child(M,F,C) :- setof(X,S^(mother(S,X)),C). Any ideas why this is? What would be a situation where you would

existential qualifier in prolog, using setof / bagof

喜你入骨 提交于 2019-11-28 04:50:18
问题 I had a quick question re. existential qualifier using setof in prolog (i.e. ^). using SICStus it seems that (despite what a number of websites claim), S does indeed appear to be quantified in the code below (using the bog standard, mother of / child of facts, which i havent included here): child(M,F,C) :- setof(X,(mother(S,X)),C). i check the unification using: child(M,F,C) :- setof(X-S,(mother(S,X)),C). so the following code, with the existential operator seem to make no difference: child(M

Prolog Recursion skipping same results

会有一股神秘感。 提交于 2019-11-27 16:13:42
My code runs but the problem is it shows the same results more than once. Here's my code: disease(hiv,[sore_throat,headache,fever,rash]). disease(pregnancy,[fatigue,vomiting,light_headedness,increased_waistline]). disease(flu,[fatigue,fever,tiredness,nasal_discharge]). diagnose([], []). diagnose(Name, [H|T]) :- disease(The_Disease, Symptoms), member(H, Symptoms), write(Name), write(' has/is '), writeln(The_Disease), diagnose(Name, T). member(X,[X|_]). member(X,[_|T]):- member(X,T). Result when executed in prolog: ?- diagnose(kevin,[sore_throat,fatigue,tiredness,rash]). kevin has/is hiv kevin

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