Tests, Facts and Backtracking Contents Tests Returning results - - PDF document

tests facts and backtracking contents
SMART_READER_LITE
LIVE PREVIEW

Tests, Facts and Backtracking Contents Tests Returning results - - PDF document

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


slide-1
SLIDE 1

Lecture 2

Tests, Facts and Backtracking Contents

  • Tests
  • Returning results
  • Facts
  • More on backtracking
slide-2
SLIDE 2

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.

“Introduction to Artificial Intelligence Programming”, School of Informatics

2

slide-3
SLIDE 3

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

“Introduction to Artificial Intelligence Programming”, School of Informatics

3

slide-4
SLIDE 4

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.

“Introduction to Artificial Intelligence Programming”, School of Informatics

4

slide-5
SLIDE 5

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

“Introduction to Artificial Intelligence Programming”, School of Informatics

5

slide-6
SLIDE 6

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.

“Introduction to Artificial Intelligence Programming”, School of Informatics

6

slide-7
SLIDE 7

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

“Introduction to Artificial Intelligence Programming”, School of Informatics

7

slide-8
SLIDE 8

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.

“Introduction to Artificial Intelligence Programming”, School of Informatics

8

slide-9
SLIDE 9

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).

“Introduction to Artificial Intelligence Programming”, School of Informatics

9

slide-10
SLIDE 10

Querying this “database”

| ?- man(peter). yes | ?- man(louise). no | ?- woman(Someone). Someone = louise; Someone = helen; Someone = mandy; no

“Introduction to Artificial Intelligence Programming”, School of Informatics

10

slide-11
SLIDE 11

| ?- 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

“Introduction to Artificial Intelligence Programming”, School of Informatics

11

slide-12
SLIDE 12

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).

“Introduction to Artificial Intelligence Programming”, School of Informatics

12

slide-13
SLIDE 13

| ?- parentof(peter, Child). Child = mandy yes | ?- parentof(louise, peter). no | ?- parentof(Parent, peter). Parent = paul yes | ?- parentof(Parent, Child). Child = mandy Parent = peter yes

“Introduction to Artificial Intelligence Programming”, School of Informatics

13

slide-14
SLIDE 14

More on backtracking

Suppose we add: grandparent(OldPerson, YoungerPerson):- parentof(OldPerson, Another), parentof(Another, YoungerPerson). and try the goal: | ?- grandparent(Oldone, Youngone).

“Introduction to Artificial Intelligence Programming”, School of Informatics

14

slide-15
SLIDE 15

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).

“Introduction to Artificial Intelligence Programming”, School of Informatics

15

slide-16
SLIDE 16

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}

“Introduction to Artificial Intelligence Programming”, School of Informatics

16

slide-17
SLIDE 17

| ?- 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

“Introduction to Artificial Intelligence Programming”, School of Informatics

17

slide-18
SLIDE 18

?- spy(grandparent). {The debugger will first leap -- showing spypoints {Spypoint placed on user:grandparent/2} yes {debug}

“Introduction to Artificial Intelligence Programming”, School of Informatics

18

slide-19
SLIDE 19

| ?- 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

“Introduction to Artificial Intelligence Programming”, School of Informatics

19