prolog

cat(‘Myrtle’).
dog(‘Fido’).
tiger(‘Huhu’).
mouse(‘Jerry’).

direct_afraid(X,Y):-mouse(X),cat(Y).
direct_afraid(X,Y):-cat(X),dog(Y).
direct_afraid(X,Y):-dog(X),tiger(Y).

afraid(X,Z):-direct_afraid(X,Y),afraid(Y,Z).
afraid(X,Z):-direct_afraid(X,Z).

查询:
| ?- afraid(‘Jerry’,Who).
结果:
Who = ‘Huhu’ ? ;

Who = ‘Fido’ ? ;

Who = ‘Myrtle’ ? ;

no

—————————————————–
dog(‘Fido’).
barks(‘Fido’):-fail.
wags_tail(‘Fido’).
meows(‘Myrtle’).

friendly(X):-wags_tail(X),dog(X).
afraid(_,X):-friendly(X),fail.
afraid(_,X):-barks(X).
cat(X):-meows(X).
查询:
| ?- dog(X),cat(Y),((afraid(X,Y),fail);true).

结果:
X = ‘Fido’
Y = ‘Myrtle’

yes

prolog对否定的处理太麻烦了…………

几个prolog在线教程:
http://www.coli.uni-saarland.de/~kris/learn-prolog-now/lpnpage.php?pageid=online
http://cs.wwc.edu/KU/PR/Prolog.html
http://www.chinaai.org/Article_Show.asp?ArticleID=314

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.