swi-prolog how to prevent repeated output -
i have knowledge base
parent(dad, son). parent(mom, son). parent(dad, daughter). parent(mom, daughter). i have functions
mother/2 father/2 child/2 that work expected, when use sibling
sibling(x,y):- parent(z,x), parent(z,y), not(x=y). the output repeats
?- sibling(x,y). x = son, y = daughter ; x = daughter, y = son ; x = son, y = daughter ; x = daughter, y = son ; i understand why it's repeating, question how limit 1 pair non-repeating? in
?- sibling(x,y). x = son, y = daughter ;
you try lurker wrote : using x @< y instead of not(x = y), you'll :
?- sibling(x,y). x = daughter , y = son; x = daughter , y = son; and sibling(son,daughter). return no whereas should return true.
or can hit enter after getting first result , not prompted other results (but depends on environment use prompt every result without asking if want them or not).
edit:
what can limit results following :
sibling(x,y):- parent(z,x), parent(z,y), parent(a,x), parent(a,y), not(x=y), z @< a. let me explain : in current implementation 4 results because
- son & daughter have same mom
- daughter & son have same mom
- son & daughter have same dad
- daughter & son have same dad
with previous implementation, sibling(x,y) true, x , y need have same 2 parents, you'll :
- son & daughter have same 2 parents
- daughter & son have same 2 parents
and knowledge minimum 2 results can have if want both sibling(son,daughter) & sibling(daughter,son) true.
Comments
Post a Comment