Top Banner
Datalog COMP 9416, session 1, 2006 COMP 9416, session 1, 2006 – p. 1
45

Datalog - cse.unsw.edu.au

Mar 11, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Datalog - cse.unsw.edu.au

DatalogCOMP 9416, session 1, 2006

COMP 9416, session 1, 2006 – p. 1

Page 2: Datalog - cse.unsw.edu.au

Atoms, integers and variables

The fact "person(socrates). ", the fact "sum(2,3,Y) ."and the rule "mortal(X) :- person(X). " are built fromthe following sequences of symbols:

atoms: "person ", "socrates ", "sum", "mortal " and":- "

integers: "2" and "3"

variables: "X" and "Y"

plus some special characters: "( ", ") ", ". " and ", ".

A constant is either an atom or an integer.

COMP 9416, session 1, 2006 – p. 2

Page 3: Datalog - cse.unsw.edu.au

Atoms

An atom names specific individuals (like "socrates "), ornonlogical properties or relationships (like "mortal "), orlogical properties or relationships (like ":- " which means if ).

Formally, an atom is either:

a sequence of alphanumeric characters and theunderscore symbol that begin with a lower-case letter,like "george w bush ", or "agent 007 ", or

a sequence of signs, like "@--> " or ":- ", or

a sequence of alphanumeric characters, the underscoresymbol and signs enclosed in single quotes, like"’a#23&’ "

where a sign is any of:+ - * / \ ˜ ˆ < > : . ? @ # $ &

COMP 9416, session 1, 2006 – p. 3

Page 4: Datalog - cse.unsw.edu.au

Variables

A variable denotes an unspecified individual.

Formally, a variable is a sequence of alphanumericcharacters and the underscore symbol that begins with anupper-case letter or the underscore.

The following are examples of variables:X R10 George R10

COMP 9416, session 1, 2006 – p. 4

Page 5: Datalog - cse.unsw.edu.au

Structures

"person(socrates) ", "sum(2,3,Y) " and"mortal(father(X)) " are called compound terms orstructures, built from functors and components orarguments.

In "person(socrates) ", "person " is the functor and"socrates " is the argument.

In "sum(2,3,Y) ", "sum" is the functor and "2", "3" and"Y" are the arguments.

In "mortal(father(X)) ", "mortal " is the functor and"father(X) " is the argument, which is itself acompound term built from the functor "father " and theargument "X".

COMP 9416, session 1, 2006 – p. 5

Page 6: Datalog - cse.unsw.edu.au

Operators

Operators offer an alternative syntax to functors for writingstructures.

"+" is a functor in "+(2,3) ", but an infix operator in thealternative syntax "2+3".

"- " is a functor in "-(4) ", but an prefix operator in thealternative syntax "-4 ".

It is also possible to write functors as postfix operators.

You can declare or redefine operators at will:

:- op(650, xfy, +), % exclusive or:- op(650, xfy, <->), % equivalence:- op(640, xfy, ->), % implication

COMP 9416, session 1, 2006 – p. 6

Page 7: Datalog - cse.unsw.edu.au

Datalog programs

Datalog programs are simple Prolog programs: thearguments of any structure in a Datalog program are eitherconstants or variables (not structures).

COMP 9416, session 1, 2006 – p. 7

Page 8: Datalog - cse.unsw.edu.au

Example (1)

Consider the following statements.

Everybody is a descendant of Gaia.

Poseidon and Zeus are offsprings of Cronus, andHermes is an offspring of Zeus.

Someone’s offspring is a descendant of that person.

A descendant of someone’s offspring is a descendantof that person.

Everyone Hermes is a descendant of is a god.

Poseidon is a god.

COMP 9416, session 1, 2006 – p. 8

Page 9: Datalog - cse.unsw.edu.au

Example (2)

They can be expressed as a Datalog program:

descendant(gaia, ).descendant(X,Y) :- offspring(X,Y).descendant(X,Z) :- offspring(X,Y),

descendant(Y,Z).

offspring(cronus,poseidon).offspring(cronus,zeus).offspring(zeus,hermes).

god(X) :- descendant(X,hermes).god(poseidon).

"descendant/2 " is defined from 1 fact and 2 rules.

"offspring/2 " is defined from 3 facts.

"god/1 " is defined from 1 rule and 1 fact.COMP 9416, session 1, 2006 – p. 9

Page 10: Datalog - cse.unsw.edu.au

Simple queries

Answering a simple query requires solving a single goal. Asimple query can contain variables.

?- god(cronus).

Yes?- descendant(cronus,X).

X = poseidon ;

X = zeus ;

X = hermes ;

No?- descendant(gaia,X).

X = G152 ;

No

COMP 9416, session 1, 2006 – p. 10

Page 11: Datalog - cse.unsw.edu.au

Complex queries

Answering complex queries requires solving many goals.

Thanks to a complex query, we can find out who is a godand a descendant of Cronus:

?- god(X), descendant(cronus,X).

X = zeus ;

X = poseidon ;

No?- descendant(cronus,X), god(X).

X = poseidon ;

X = zeus ;

No

COMP 9416, session 1, 2006 – p. 11

Page 12: Datalog - cse.unsw.edu.au

Computations (1)

The simplest computation takes place when answeringqueries like:

?- offspring(cronus,zeus).

Yes?- offspring(cronus,hermes).

No

Prolog proceeds by pattern matching. In this case, thequery does not match the head of any rule, and matching afact is equivalent to being the same as this fact.

Prolog tries every clause in the program starting from thefirst one, and going down the list. The answer is yes whena match is found, and no otherwise.

COMP 9416, session 1, 2006 – p. 12

Page 13: Datalog - cse.unsw.edu.au

Computations (2)

The next level of complexity takes place when answeringsimple queries containing variables, but that can only bematched against a fact.

A match can be found by matching any variable with anyterm. If the same variable occurs many times in a goal, itshould obviously be bound to the same term.

?- offspring(cronus,X).

X = poseidon ;

X = zeus ;

No?- offspring(X,X).

NoCOMP 9416, session 1, 2006 – p. 13

Page 14: Datalog - cse.unsw.edu.au

Computations (3)

Of course, distinct variables can (but do not have to) bematched against distinct terms :

?- offspring(X,Y).

X = cronusY = poseidon ;

X = cronusY = zeus ;

X = zeusY = hermes ;

No

COMP 9416, session 1, 2006 – p. 14

Page 15: Datalog - cse.unsw.edu.au

Computations (4)

Note how anonymous variables can be used to expressthere exists:

?- offspring( ,X).

X = poseidon ;

X = zeus ;

X = hermes ;

No

Many occurrences of denote distinct anonymousvariables:

?- offspring( , ).

YesCOMP 9416, session 1, 2006 – p. 15

Page 16: Datalog - cse.unsw.edu.au

Computations (4)

Things become interesting with simple queries that can bematched against the head of a rule.

Answering the query then amounts to solving a sequenceof goals, starting with the query itself.

To trace the computation, we assign four ports to each goalto represent the flow of control though the goal:

call (C), exit (E), fail (F) and redo (R).

C Fgoal

E R

COMP 9416, session 1, 2006 – p. 16

Page 17: Datalog - cse.unsw.edu.au

Computations (5)

The goal is called via the call port.

If the computation succeeds then the goal is exited viathe exit port.

If the computation fails then the goal is exited via the failport.

When alternative matches are tried, the redo port isentered. This can happen:

‘internally,’ before the flow of control has gonethrough the exit port;‘externally’, after the flow of control has gonethrough the exit port

COMP 9416, session 1, 2006 – p. 17

Page 18: Datalog - cse.unsw.edu.au

Unification, backtracking

The process of pattern matching is called unification.

Once a variable X has been unified with a term t1, it cannotbe unified with another term t2.

A variable in Prolog cannot be overwritten.

First the bind between X and t1 has to be removed. Onlythen can X be unified with t2.

Unbinding happens every time Prolog searches foralternative solutions. This process is called backtracking.

We now illustrate these concepts on an example, tracingthe first steps of the computation when solving the querydescendant(cronus,X) .

COMP 9416, session 1, 2006 – p. 18

Page 19: Datalog - cse.unsw.edu.au

[0] CALL: descendant(cronus,_G278)

C Fdescendant(cronus,_G278)

E R

COMP 9416, session 1, 2006 – p. 19

Page 20: Datalog - cse.unsw.edu.au

[1] CALL: offspring(cronus,_G278)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,_G278)

E R

COMP 9416, session 1, 2006 – p. 20

Page 21: Datalog - cse.unsw.edu.au

[1] EXIT: offspring(cronus,poseidon)

C Fdescendant(cronus,poseidon)

E R

C Foffspring(cronus,poseidon)

E R

COMP 9416, session 1, 2006 – p. 21

Page 22: Datalog - cse.unsw.edu.au

[0] EXIT: descendant(cronus,poseidon)

C Fdescendant(cronus,poseidon)

E R

C Foffspring(cronus,poseidon)

E R

X=poseidon ;COMP 9416, session 1, 2006 – p. 22

Page 23: Datalog - cse.unsw.edu.au

[1] REDO: offspring(cronus,poseidon)

C Fdescendant(cronus,poseidon)

E R

C Foffspring(cronus,poseidon)

E R

X=poseidon ;COMP 9416, session 1, 2006 – p. 23

Page 24: Datalog - cse.unsw.edu.au

[1] EXIT: offspring(cronus,zeus)

C Fdescendant(cronus,zeus)

E R

C Foffspring(cronus,zeus)

E R

X=poseidon ;COMP 9416, session 1, 2006 – p. 24

Page 25: Datalog - cse.unsw.edu.au

[0] EXIT: descendant(cronus,zeus)

C Fdescendant(cronus,zeus)

E R

C Foffspring(cronus,zeus)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 25

Page 26: Datalog - cse.unsw.edu.au

[1] REDO: offspring(cronus,zeus)

C Fdescendant(cronus,zeus)

E R

C Foffspring(cronus,zeus)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 26

Page 27: Datalog - cse.unsw.edu.au

[1] FAIL: offspring(cronus,_G278)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 27

Page 28: Datalog - cse.unsw.edu.au

[0] REDO: descendant(cronus,_G278)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 28

Page 29: Datalog - cse.unsw.edu.au

[1] CALL: offspring(cronus,_G332)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,_G332)

E R

C Fdescendant(_G332,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 29

Page 30: Datalog - cse.unsw.edu.au

[1] EXIT: offspring(cronus,poseidon)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,poseidon)

E R

C Fdescendant(poseidon,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 30

Page 31: Datalog - cse.unsw.edu.au

[1] CALL: descendant(poseidon,_G278)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,poseidon)

E R

C Fdescendant(poseidon,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 31

Page 32: Datalog - cse.unsw.edu.au

[2] CALL: offspring(poseidon,_G278)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,poseidon)

E R

C Fdescendant(poseidon,_G278)

E R

C Foffspring(poseidon,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 32

Page 33: Datalog - cse.unsw.edu.au

[2] FAIL: offspring(poseidon,_G278)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,poseidon)

E R

C Fdescendant(poseidon,_G278)

E R

C Foffspring(poseidon,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 33

Page 34: Datalog - cse.unsw.edu.au

[1] REDO: descendant(poseidon,_G278)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,poseidon)

E R

C Fdescendant(poseidon,_G278)

E R

C Foffspring(poseidon,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 34

Page 35: Datalog - cse.unsw.edu.au

[2] CALL: offspring(poseidon,_G332)C F

descendant(cronus,_G278)

E R

C Foffspring(cronus,poseidon)

E R

C Fdescendant(poseidon,_G278)

E R

C Foffspring(poseidon,_G332)

E R

C Fdescendant(_G332,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 35

Page 36: Datalog - cse.unsw.edu.au

[2] FAIL: offspring(poseidon,_G332)C F

descendant(cronus,_G278)

E R

C Foffspring(cronus,poseidon)

E R

C Fdescendant(poseidon,_G278)

E R

C Foffspring(poseidon,_G332)

E R

C Fdescendant(_G332,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 36

Page 37: Datalog - cse.unsw.edu.au

[1] REDO: descendant(poseidon,_G278)C F

descendant(cronus,_G278)

E R

C Foffspring(cronus,poseidon)

E R

C Fdescendant(poseidon,_G278)

E R

C Foffspring(poseidon,_G332)

E R

C Fdescendant(_G332,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 37

Page 38: Datalog - cse.unsw.edu.au

[1] FAIL: descendant(poseidon,_G278)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,poseidon)

E R

C Fdescendant(poseidon,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 38

Page 39: Datalog - cse.unsw.edu.au

[1] REDO: offspring(cronus,poseidon)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,poseidon)

E R

C Fdescendant(poseidon,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 39

Page 40: Datalog - cse.unsw.edu.au

[1] EXIT: offspring(cronus,zeus)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,zeus)

E R

C Fdescendant(zeus,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 40

Page 41: Datalog - cse.unsw.edu.au

[1] CALL: descendant(zeus,_G278)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,zeus)

E R

C Fdescendant(zeus,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 41

Page 42: Datalog - cse.unsw.edu.au

[2] CALL: offspring(zeus,_G278)

C Fdescendant(cronus,_G278)

E R

C Foffspring(cronus,zeus)

E R

C Fdescendant(zeus,_G278)

E R

C Foffspring(zeus,_G278)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 42

Page 43: Datalog - cse.unsw.edu.au

[2] EXIT: offspring(zeus,hermes)

C Fdescendant(cronus,hermes)

E R

C Foffspring(cronus,zeus)

E R

C Fdescendant(zeus,hermes)

E R

C Foffspring(zeus,hermes)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 43

Page 44: Datalog - cse.unsw.edu.au

[1] EXIT: descendant(zeus,hermes)

C Fdescendant(cronus,hermes)

E R

C Foffspring(cronus,zeus)

E R

C Fdescendant(zeus,hermes)

E R

C Foffspring(zeus,hermes)

E R

X=poseidon ; X=zeus ;COMP 9416, session 1, 2006 – p. 44

Page 45: Datalog - cse.unsw.edu.au

[0] EXIT: descendant(cronus,hermes)

C Fdescendant(cronus,hermes)

E R

C Foffspring(cronus,zeus)

E R

C Fdescendant(zeus,hermes)

E R

C Foffspring(zeus,hermes)

E R

X=poseidon ; X=zeus ; hermes .COMP 9416, session 1, 2006 – p. 45