Lecture 2 Tests, Facts and Backtracking Contents • Tests • Returning results • Facts • More on backtracking
The story so far... • A Prolog program may succeed or fail . • Prolog programs consist of predicate definitions • Predicate definitions consist of clauses • Clauses consist of a head and and (so far) a body • A clause head has a predicate name and sometimes some arguments . • A goal is a predicate name and sometimes some arguments . • A goal matches against clause heads in order. • If no clause matches, that goal fails. • Successful matching may cause variables to be bound to values. • If a variable in the top-level goal becomes bound, the user-interface reports this. 2 “Introduction to Artificial Intelligence Programming”, School of Informatics
Tests “Success” can act as “true”, “failure” as “false”: | ?- 5 < 7. yes | ?- 7 < 5. no So the comma acts like a (left-to-right) “AND” : | ?- 3 < 7, 2 < 4, 10 < 12. yes | ?- 3 < 7, 4 < 2, 10 < 12. no 3 “Introduction to Artificial Intelligence Programming”, School of Informatics
Tests in clauses This allows more precise selection of a clause: bigger(N, M):- N < M, write(’Bigger number is ’), write(M). bigger(N, M) :- M < N, write(’Bigger number is ’), write(N). bigger(N, M) :- write(’Numbers are the same’). If a test fails, then the system backtracks , and tries to choose a later clause. No need for an equality test in 3rd clause here – system tries this one only when other two have failed, and hence N and M must be equal. 4 “Introduction to Artificial Intelligence Programming”, School of Informatics
Passing results DON’T return values by printing messages. Return values by causing suitable variables to become bound. larger(N, M, M):- N < M. larger(N, M, N) :- M < N. larger(N, M, M). e.g. : | ?- larger(8,3,Result). Result = 8 yes | ?- larger(6,6,Value). Value = 6 yes | ?- larger(2,5,Value). Value = 5 yes 5 “Introduction to Artificial Intelligence Programming”, School of Informatics
Variable binding larger ( 8 , 3 , Result ) ˆ ˆ ˆ | | | v v v larger ( N, M, M ) :- N < M, ..... M bound to 3, and M bound to Result, so Result bound to 3. Clause body fails. Bindings discarded. Next clause tried: larger ( 8 , 3 , Result ) ˆ ˆ ˆ | | | v v v larger ( N, M, N ):- M < N, ..... Since N bound to 8, and N bound to Result, Result bound to 3. Clause body succeeds. Result binding is retained and displayed. 6 “Introduction to Artificial Intelligence Programming”, School of Informatics
More on variable matching • a variable can be bound to another variable ( sharing ) • a shared set can contain any number of variables • if variable A is bound to variable B, then variable B is bound to variable A • shared variables cannot be bound to different non-variable values • when one of a shared set of variables is bound to a value, all the variables in that shared set are bound to the same value 7 “Introduction to Artificial Intelligence Programming”, School of Informatics
Unit clauses A clause may have an empty body – no goals, and omit the “ :- ” symbol. greet(nasty). % Clause in your program Everything works as before: | ?- greet(nasty). yes | ?- greet(Who). Who = nasty? yes These are unit clauses . 8 “Introduction to Artificial Intelligence Programming”, School of Informatics
Unit clauses as “facts” %% A simple set of clauses describing %% some family relationships man(paul). man(david). man(peter). woman(louise). woman(helen). woman(mandy). wifeof(paul, louise). wifeof(peter, helen). sonof(paul, peter). daughterof(peter, mandy). Use constant symbols to represent objects, and predicates for properties (e.g. woman ) or relationships (e.g. sonof ). 9 “Introduction to Artificial Intelligence Programming”, School of Informatics
Querying this “database” | ?- man(peter). yes | ?- man(louise). no | ?- woman(Someone). Someone = louise; Someone = helen; Someone = mandy; no 10 “Introduction to Artificial Intelligence Programming”, School of Informatics
| ?- wifeof(paul, Hiswife). Hiswife = louise yes | ?- wifeof(Herhusband, louise). Herhusband = paul yes | ?- daughterof(Father, mandy). Father = peter yes | ?- sonof(Father, Son). Father = paul Son = peter yes 11 “Introduction to Artificial Intelligence Programming”, School of Informatics
Facts and rules together Alongside facts (unit clauses) we can have full clauses: husbandof(Woman, Man):- wifeof(Man, Woman). “For a goal involving husbandof , try a goal using wifeof , with the arguments in the other order.” | ?- husbandof(helen, peter). yes We could also have: parentof(Person1, Person2):- daughterof(Person1, Person2). parentof(Person1, Person2):- sonof(Person1, Person2). 12 “Introduction to Artificial Intelligence Programming”, School of Informatics
| ?- parentof(peter, Child). Child = mandy yes | ?- parentof(louise, peter). no | ?- parentof(Parent, peter). Parent = paul yes | ?- parentof(Parent, Child). Child = mandy Parent = peter yes 13 “Introduction to Artificial Intelligence Programming”, School of Informatics
More on backtracking Suppose we add: grandparent(OldPerson, YoungerPerson):- parentof(OldPerson, Another), parentof(Another, YoungerPerson). and try the goal: | ?- grandparent(Oldone, Youngone). 14 “Introduction to Artificial Intelligence Programming”, School of Informatics
Looks for a parentof relation. First clause says try daughterof . SUCCESS with daughterof(peter, mandy) So Another = mandy . Looks for parentof(mandy, YoungerPerson) . First clause says try daughterof . None with mandy . FAILURE. Second clause suggests sonof . None with mandy . FAILURE. So second parentof goal FAILS. Try first parentof goal again. Second clause suggests sonof . SUCCESS with sonof(paul, peter) So OldPerson = paul , Another = peter . Looks for parent(peter, YoungerPerson) . First clause says try daughterof . SUCCESS with daughterof(peter, mandy) So YoungerPerson) = mandy Both parentof goals successful. So grandparent goal successful (with bindings Oldone = paul , Youngone = mandy ). 15 “Introduction to Artificial Intelligence Programming”, School of Informatics
Tracing “ spy ” shows you what is going on inside your program. | ?- spy(larger). {The debugger will first leap -- showing spypoints {Spypoint placed on user:larger/3} yes {debug} | ?- larger(8,9, Output). + 1 1 Call: larger(8,9,_195) ? 2 2 Call: 8<9 ? 2 2 Exit: 8<9 ? + 1 1 Exit: larger(8,9,9) ? Output = 9 ? yes {debug} 16 “Introduction to Artificial Intelligence Programming”, School of Informatics
| ?- larger(9,8, Result). + 1 1 Call: larger(9,8,_195) ? 2 2 Call: 9<8 ? 2 2 Fail: 9<8 ? 2 2 Call: 8<9 ? 2 2 Exit: 8<9 ? + 1 1 Exit: larger(9,8,9) ? Result = 9 ? yes 17 “Introduction to Artificial Intelligence Programming”, School of Informatics
?- spy(grandparent). {The debugger will first leap -- showing spypoints {Spypoint placed on user:grandparent/2} yes {debug} 18 “Introduction to Artificial Intelligence Programming”, School of Informatics
| ?- grandparent(Oldone, Youngone). + 1 1 Call: grandparent(_193,_221) ? 2 2 Call: parentof(_193,_485) ? 3 3 Call: daughterof(_193,_485) ? 3 3 Exit: daughterof(peter,mandy) ? 2 2 Exit: parentof(peter,mandy) ? 4 2 Call: parentof(mandy,_221) ? 5 3 Call: daughterof(mandy,_221) ? 5 3 Fail: daughterof(mandy,_221) ? 5 3 Call: sonof(mandy,_221) ? 5 3 Fail: sonof(mandy,_221) ? 4 2 Fail: parentof(mandy,_221) ? 2 2 Redo: parentof(peter,mandy) ? 3 3 Redo: daughterof(peter,mandy) ? 3 3 Fail: daughterof(_193,_485) ? 3 3 Call: sonof(_193,_485) ? 3 3 Exit: sonof(paul,peter) ? 2 2 Exit: parentof(paul,peter) ? 4 2 Call: parentof(peter,_221) ? 5 3 Call: daughterof(peter,_221) ? 5 3 Exit: daughterof(peter,mandy) ? 4 2 Exit: parentof(peter,mandy) ? + 1 1 Exit: grandparent(paul,mandy) ? Oldone = paul, Youngone = mandy ? yes 19 “Introduction to Artificial Intelligence Programming”, School of Informatics
Recommend
More recommend