© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation,...

43
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search

Transcript of © Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation,...

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz Lecture 3

“Input & Output”, Negation, Search

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Last Lecture

• In the previous lecture we have introduced:– recursion– declarative and procedural semantics– arithmetic operators – list, head & tail of a list, recursion on lists– predicates for collecting solutions

Aim of this lectureLPN Sect. 10.3

• After this lecture, you should be able to explain the following key notions and their relation to Prolog: – “input/output” in Prolog– negation as failure– search algorithms

“Input and output” in Prolog

Output

• Predicates do not return values. Variables are instantiated through unification. This is what gives Prolog its computation power!

• E.g., suppose you want to do something with the result of pairing the elements of a list with another element. Then don’t write doSomething(pair(L1,X,L2)) but:

..... :- pair(L1,X,L2), doSomething(L2, L3).

• Here, L2 is taken as the “output” resulting from proving pair(L1,X,L2)

Input or Output

• When posing a query, often it is not fixed whether a parameter has to be “input” or “output”.

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Example: a2b/2

• The predicate a2b/2 takes two lists as arguments and succeeds – if the first argument is a list of as, and – the second argument is a list of bs of

exactly the same length?- a2b([a,a,a,a],[b,b,b,b]).

yes

?- a2b([a,a,a,a],[b,b,b]).

no

?- a2b([a,c,a,a],[b,b,b,t]).

no

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

a2b/2

a2b([],[]).

a2b([a|L1],[b|L2]):- a2b(L1,L2).

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Both Parameters “Input”

a2b([],[]).

a2b([a|L1],[b|L2]):- a2b(L1,L2).

?- a2b([a,a,a],[b,b,b]).

yes

?-

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

First Parameter “Input” Second Parameter “Output”

a2b([],[]).

a2b([a|L1],[b|L2]):- a2b(L1,L2).

?- a2b([a,a,a,a,a], X).

X = [b,b,b,b,b]

yes

?-

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

a2b([],[]).

a2b([a|L1],[b|L2]):- a2b(L1,L2).

?- a2b(X,[b,b,b,b,b,b,b]).

X = [a,a,a,a,a,a,a]

yes

?-

First Parameter “Output” Second Parameter “Input”

Input or Output

• Thus: When posing a query, often it is not fixed whether a parameter has to be “input” or “output”.

• But: Not in all cases can any parameter be used as input or as output. Sometimes parameters have to be instantiated.

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Defining predicates with arithmetic

addThreeAndDouble(X, Y):-

Y is 2 * (X+3) .f(x) = 2(x+3)

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Defining predicates with arithmetic

addThreeAndDouble(X, Y):-

Y is 2 * (X+3).

?- addThreeAndDouble(1,Y).

Y=8

yes

?- addThreeAndDouble(2,Y).

Y=10

yes

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Defining predicates with arithmetic

addThreeAndDouble(X, Y):-

Y is 2 * (X+3).

?- addThreeAndDouble(X,10).

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Defining predicates with arithmetic

addThreeAndDouble(X, Y):-

Y is 2 * (X+3).

?- addThreeAndDouble(X,10).

ERROR: is/2: Arguments are not sufficiently instantiated

Prolog Manual

• + Argument must be fully instantiated to a term that satisfies the required argument type. Think of the argument as input.

• - Argument must be unbound. Think of the argument as output.

• ? Argument must be bound to a partial term of the indicated type. Note that a variable is a partial term for any type. Think of the argument as either input or output or both input and output.

• E.g., flatten(+List1, ?List2)

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Negation as Failure

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Negation in Logic

• The assertion ¬p specifies that ¬p is true, and therefore p is false

• E.g.: ¬p, ¬p → q |= q

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Negation in Prolog

• Negation in Prolog is different from negation in logic!

• In Prolog, negation is represented using the special predicate ‘not/1’

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Example

burger(X):- bigMac(X).burger(X):- bigKahunaBurger(X).burger(X):- whopper(X).

bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

enjoys(vincent,X) :- burger(X), not(bigKahunaBurger(X)).

• not(bigKahunaBurger(X)) succeeds if bigKahunaBurger(X) cannot be derived, i.e., if bigKahunaBurger(X) fails

Prolog uses “negation as failure”

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Example

burger(X):- bigMac(X).burger(X):- bigKahunaBurger(X).burger(X):- whopper(X).

bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

enjoys(vincent,X) :- burger(X), not(bigKahunaBurger(X)).

?- enjoys(vincent,X).

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Example

burger(X):- bigMac(X).burger(X):- bigKahunaBurger(X).burger(X):- whopper(X).

bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

enjoys(vincent,X):- burger(X), not(bigKahunaBurger(X)).

?- enjoys(vincent,X).

X = a;

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Example

burger(X):- bigMac(X).burger(X):- bigKahunaBurger(X).burger(X):- whopper(X).

bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

enjoys(vincent,X):- burger(X), not(bigKahunaBurger(X)).

?- enjoys(vincent,X).

X = a;

X = c;

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Example

burger(X):- bigMac(X).burger(X):- bigKahunaBurger(X).burger(X):- whopper(X).

bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

enjoys(vincent,X):- burger(X), not(bigKahunaBurger(X)).

?- enjoys(vincent,X).

X = a;

X = c;

X = d.

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Negation as failure and logic

• Negation as failure is not logical negation

• Changing the order of the goals in the vincent and burgers program gives a different behaviour:

enjoys(vincent,X):- not(bigKahunaBurger(X)), burger(X). ?- enjoys(vincent,X).

no

not(bigKahunaBurger(X)) succeeds if bigKahunaBurger(X) cannot be derived, i.e., if there are no bigKahunaBurgers.

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Negation as failure and logic

• Negation as failure is not logical negation

• “not(p(X))” succeeds if p(X) cannot be derived – this is different from classical negation

p, p → q |= ¬r

?- not(r).

yes

:- dynamic(r). p.q :- p.

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Negation as failure and logic

• Negation as failure is not logical negation

• “not” has a non-monotonic behavior: if formulas are added, it could be that it can no longer be derived

p, p → q, r |= ¬r

?- not(r).

no

:- dynamic(r). p. r.q :- p.

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Negation as failure and closed world assumption (CWA)

• Closed world assumption: what is not currently known to be true, is false

• Example: if the train schedule does not include a train at 15:00 to A’dam, conclude that there will not be a train at 15:00 to A’dam

• NAF: if we cannot derive that something is true, conclude that it is not true

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Search

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Route Planning

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Robot Navigation

http://www.ics.forth.gr/cvrl/

Unreal

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Search Problems

Common structure:

• We are faced with an initial situation and we would like to achieve a certain goal.

• At any point in time we have different simple actions available to us (e.g. “turn left” vs. “turn right”). Executing a particular sequence of such actions may or may not achieve the goal.

• Search is the process of inspecting several such sequences and choosing a path that achieves the goal.

Deep Blue & Watson

IBM Chess Computer (1997)

IBM Jeopardy Computer (2011)

http://mashable.com/2011/02/16/watson-jeopardy-day-2/

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

State Space and Moves

Navigation in Unreal Tournament:

• States are navigation points• Navigation points can be connected to other

navigation points (determine moves)• Use search to determine how to get from the current

navigation point to a desired one• Moving from one navigation point to the next is

associated with a cost (distance); Try to reach desired navigation point at minimal cost: optimisation problem.

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Searching the State Space

• The set of all possible sequences of legal moves form a tree

• Each branch (path from root to leaf) corresponds to a sequence of states (and thereby also a sequence of moves).

• Two basic ways of moving through such a tree: depth-first and breadth-first

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Depth-First

Depth-first traversal: A→B→D→E→C→F→G

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Depth-First

solve_depthfirst(Node, TargetNode, SolPath) :- depthfirst([], Node, TargetNode, SolPath).

depthfirst(Path, Node, Node, [Node|Path]).

depthfirst(Path, Node, TargetNode, SolPath) :- move(Node, NextNode), depthfirst([Node|Path], NextNode,

TargetNode, SolPath).

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Breadth-First

Breadth-first traversal: A→B→C→D→E→F→G

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Breadth-First

• Store a set of candidate paths

• If head of the first candidate path = target node then this path is a solution

• Otherwise remove first candidate path, generate the set of all one-step extensions of this path, add this set of extensions at the end of the candidate set, and execute breadth-first search on this updated set.

Summary of this Lecture

• We introduced the following key notions and their relation to Prolog: – “input & output” – negation as failure– search algorithms

© P

atr

ick B

lackb

urn

, Jo

han

Bos &

Kri

sti

na S

trie

gn

itz

Organization

• Reading: LPN Sect. 10.3 from "Negation as failure is an impotant tool." to "The difference is crucial."

• Tomorrow “werkcollege”- Assignment 2

• Next lecture (Koen Hindriks)

- GOAL: introduction to agent programming