考虑以下示例简单的Prolog程序:
child(johnny, eve, john).
child(stan, eve, john).
child(barbs, ann, peter).
child(john, liz, james).
father(Child, Father) :- child(Child, _, Father).
mother(Child, Mother) :- child(Child, Mother, _).
parent(Child, Parent) :- father(Child, Parent).
parent(Child, Parent) :- mother(Child, Parent).
grandma(Child, Grandma) :- parent(Child, Parent), mother(Parent, Grandma).
grandpa(Child, Grandpa) :- parent(Child, Parent), father(Parent, Grandpa).
我注意到了一件令人惊讶的事情。列举其中一些关系(grandma
和grandpa
),最终会打印出false
:
?- grandma(Child, Grandma).
Child = johnny,
Grandma = liz ;
Child = stan,
Grandma = liz ;
false.
?-
枚举其他人不会:
?- mother(Child, Mother).
Child = johnny,
Mother = eve ;
Child = stan,
Mother = eve ;
Child = barbs,
Mother = ann ;
Child = john,
Mother = liz.
?-
为什么会发生这种情况以及它取决于什么?