rules
play

Rules John likes all people Could list all people - PDF document

PROLOG Edward S. Blurock Rules John likes all people Could list all people likes(john,alfred). likes(john,bertrand). likes(john,charles). likes(john,david). . . . Rule more Compact John likes any object provided it is a person PROLOG


  1. PROLOG Edward S. Blurock Rules John likes all people Could list all people likes(john,alfred). likes(john,bertrand). likes(john,charles). likes(john,david). . . . Rule more Compact John likes any object provided it is a person

  2. PROLOG Edward S. Blurock Rule Examples Rules state Dependence I use an umbrella If there is rain Rules Define X is a bird If X is an animal and X has feathers

  3. PROLOG Edward S. Blurock Formulating Rules John likes anyone who likes wine John likes any something if it likes wine John likes X if X likes wine

  4. PROLOG Edward S. Blurock Rule Syntax likes(john,X) :- likes(X,wine). rule delimitor head body

  5. PROLOG Edward S. Blurock Variable Scope instantiated here checked here returned here likes(john,X) :- likes(X,wine), likes(X,food). X’s Within Scope of Rule

  6. PROLOG Edward S. Blurock Royal Parents Predicate The parents of X are Y and Z Y is the mother Z is the father Database male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert).

  7. PROLOG Edward S. Blurock Sisters X is a sister of Y if: X is female X has mother M and father F Y has mother M and father F Rule sisters of(X,Y) :- female(X), parents(X,M,F), parents(Y,M,F).

  8. PROLOG Edward S. Blurock Scope sister_of(X,Y) :- female(X), parents(X,M,F), parents(Y,M,F). Scope only within body of rule

  9. PROLOG Edward S. Blurock Sisters Question Rule sister_of(X,Y) :- female(X), parents(X,M,F), parents(Y,M,F). Question sister_of(alice,edward). sister_of(X,Y) Y = edward X = alice female(alice), parents(alice,M,F), New Goal parents(edward,M,F).

  10. PROLOG Edward S. Blurock Sisters Question female(alice), parents(alice,M,F), New Goal parents(edward,M,F). female(alice) yes next goal parents(alice,M,F) parents(alice,victoria,albert) M = victoria, F = albert Database next goal parents(edward,victoria,albert) yes, found goal satisfied

  11. PROLOG Edward S. Blurock Who is the Sister? Rule sister_of(X,Y) :- female(X), parents(X,M,F), parents(Y,M,F). Question Different X’s sister_of(alice,X). sister_of(X,Y) Y = X (toplevel) X = alice uninstantiated variable female(alice), parents(alice,M,F), New Goal parents(X,M,F).

  12. PROLOG Edward S. Blurock Who is the Sister? female(alice), parents(alice,M,F), New Goal parents(X,M,F). female(alice) 1 yes next goal 2 parents(alice,M,F) parents(alice,victoria,albert) M = victoria, F = albert Database next goal 3 parents(X,victoria,albert) parents(edward,victoria,albert) X = edward

  13. PROLOG Edward S. Blurock Stealing The Rule A person may steal something if the person is a thief and he likes the thing Prolog Rule may steal(P,T) :- thief(P),likes(P,T).

  14. PROLOG Edward S. Blurock Example [sun:examples!7] cat thief.pro thief(john). likes(mary,food). likes(mary,wine). likes(john,X) :- likes(X,wine). may_steal(X,Y) :- thief(X),likes(X,Y). /software/prolog/sicstus/bin/sicstus SICStus 2.1 #8:MET DST 1995 | ?- [’thief.pro’]. {consulting thief.pro...} {thief.pro consulted,0 msec 1136 bytes} yes | ?- may_steal(john,X). X = mary ? ; no | ?- halt. [sun:examples!9]

  15. PROLOG Edward S. Blurock Trace and Debug /software/prolog/sicstus/bin/sicstus SICStus 2.1 #8: DST 1995 | ?- debug. {The debugger will first leap -- showing spypoints (debug)} yes {debug} | ?- trace. {The debugger will first creep -- showing everything (trace)} {trace}

  16. PROLOG Edward S. Blurock Function Trace | ?- may_steal(john,X). + 1 1 Call: may_steal(john,_67) ? + 2 2 Call: thief(john) ? + 2 2 Exit: thief(john) ? + 3 2 Call: likes(john,_67) ? + 4 3 Call: likes(_67,wine) ? + 4 3 Exit: likes(mary,wine) ? + 3 2 Exit: likes(john,mary) ? + 1 1 Exit: may_steal(john,mary) ? X = mary ? ;

  17. PROLOG Edward S. Blurock Redo + 1 1 Redo: may_steal(john,mary) ? + 3 2 Redo: likes(john,mary) ? + 4 3 Redo: likes(mary,wine) ? + 5 4 Call: likes(wine,wine) ? + 5 4 Fail: likes(wine,wine) ? + 4 3 Fail: likes(_67,wine) ? + 3 2 Fail: likes(john,_67) ? + 2 2 Redo: thief(john) ? + 2 2 Fail: thief(john) ? + 1 1 Fail: may_steal(john,_67) ? no {trace} | ?-

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend