X=1

advertisement
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Lecture 3
“Input & Output”, Negation,
Search
Last Lecture
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
• 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 lecture
LPN 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”.
Example: a2b/2
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
• 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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
a2b/2
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
Both Parameters “Input”
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
?- a2b([a,a,a],[b,b,b]).
yes
?-
First Parameter “Input”
Second Parameter “Output”
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
a2b([],[]).
a2b([a|L1],[b|L2]):- a2b(L1,L2).
?- a2b([a,a,a,a,a], X).
X = [b,b,b,b,b]
yes
?-
First Parameter “Output”
Second Parameter “Input”
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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
?-
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.
Defining predicates with arithmetic
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
addThreeAndDouble(X, Y):Y is 2 * (X+3) .
f(x) = 2(x+3)
Defining predicates with arithmetic
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
addThreeAndDouble(X, Y):Y is 2 * (X+3).
?- addThreeAndDouble(1,Y).
Y=8
yes
?- addThreeAndDouble(2,Y).
Y=10
yes
Defining predicates with arithmetic
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
addThreeAndDouble(X, Y):Y is 2 * (X+3).
?- addThreeAndDouble(X,10).
Defining predicates with arithmetic
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Negation as Failure
Negation in Logic
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
• The assertion ¬p specifies that ¬p is
true, and therefore p is false
• E.g.: ¬p, ¬p → q |= q
Negation in Prolog
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
• Negation in Prolog is different from
negation in logic!
• In Prolog, negation is represented
using the special predicate ‘not/1’
Example
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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”
Example
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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).
Example
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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;
Example
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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;
Example
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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).
not(bigKahunaBurger(X)) succeeds if
bigKahunaBurger(X) cannot be derived,
i.e., if there are no bigKahunaBurgers.
?- enjoys(vincent,X).
no
Negation as failure and logic
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
• 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
:- dynamic(r).
p.
q :- p.
?- not(r).
yes
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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
:- dynamic(r).
p. r.
q :- p.
?- not(r).
no
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Search
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Route Planning
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Robot Navigation
http://www.ics.forth.gr/cvrl/
Unreal
Search Problems
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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 Jeopardy Computer (2011)
http://mashable.com/2011/02/16/w
atson-jeopardy-day-2/
IBM Chess Computer (1997)
State Space and Moves
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Depth-First
Depth-first traversal: A→B→D→E→C→F→G
Depth-First
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Breadth-First
Breadth-first traversal: A→B→C→D→E→F→G
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
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
Download