bigger(elephant, horse). bigger(horse, monkey). We can quer - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

bigger(elephant, horse). bigger(horse, monkey). We can quer

Description:

bigger(elephant, horse). bigger(horse, monkey). We can query them: ?- bigger(X, horse) ... nil = the empty list [a,b,c] contains 3 elements. other ... – PowerPoint PPT presentation

Number of Views:128
Avg rating:3.0/5.0
Slides: 33
Provided by: eta4
Category:
Tags: elephant | horse | list | monkey | quer

less

Transcript and Presenter's Notes

Title: bigger(elephant, horse). bigger(horse, monkey). We can quer


1
Constraint Logic Programming
  • Lesson 1

2
Info
  • course number 211113
  • course page http//www.cs.utwente.nl/etalle/lp/
  • here comes the study material
  • dont use teletop much
  • schedule (important) WILL PROBABLY CHANGE
  • teacher Sandro Etalle etalle_at_cs.utwente.nl
  • Marcin Czenko assists the practical part

3
This course
  • The slides and the exercises are being
    rewritten dont download the slides of the next
    lesson.
  • slides material available via my homepage are
    sufficient
  • Goal
  • learn to PROGRAM in Prolog and Eclipse
  • learn and be able to put into practice the
    concepts of
  • backtracking search
  • constraint propagation
  • branch and bound search
  • ...

4
Exam
  • written
  • possible questions could be
  • write a program that ...
  • tell what this program exactly does ...
  • apply the XXX consistency algorithm to the
    following CSP...
  • some philosophical question about search
    propagation, backtracking, unification, etc..
  • but dont complain if I come up with a different
    one.

5
Structure
  • part 1 PROLOG
  • part 2 Constraint Satisfaction
  • CSP and CLP
  • each part 2 theory and 2 practice lessons
  • practice is done using the ECLIPSE compiler (you
    can download it)

6
Info/Warning
  • Exercises are not obligatory, and can be done at
    home and/or in group
  • You dont have to turn in the result
  • BUT1 unless you are a genius, there is no way
    youll pass the exam if you havent done the
    exercises
  • BUT2 programming in Prolog is totally different
    than programming in another language. Dont think
    you can learn it in a couple of days

7
First Lesson PROLOG (part 1)
  • tutorial by Ulle Endriss chapter 1 and 2.
  • available via the course page
  • differences Endriss speaks about matching
    instead of unification.

8
Syntax Terms
  • 3 kinds of terms
  • constants
  • numbers
  • atoms
  • variable
  • compound terms

9
Numbers
  • 3 kinds of terms
  • constants
  • numbers
  • atoms
  • variable
  • compound terms
  • In PROLOG follow the usual standard
  • 0
  • 1
  • -33.75
  • 12345E1

10
Atoms
  • 3 kinds of terms
  • constants
  • numbers
  • atoms
  • variable
  • compound terms
  • IN PROLOG
  • 3 possibilities
  • start with a lowercase letter (might contain _
    and numbers)
  • a2, a_2, pippo
  • not a2, a-2, -a, _a
  • between single quotes
  • _asdkfa-23
  • contain only signs
  • --

11
Variables
  • 3 kinds of terms
  • constants
  • numbers
  • atoms
  • variable
  • compound terms
  • IN PROLOG
  • 2 possibilities
  • start with an Uppercase letter
  • A2, A_2, Pippo
  • start with _
  • _pippo, _12344
  • might contain _ and numbers, just like atoms

12
Compound terms
  • 3 kinds of terms
  • constants
  • numbers
  • atoms
  • variable
  • compound terms
  • functorarguments
  • functor is an atom
  • arguments are terms
  • number of arguments arity
  • f(X), (X,a,f(X,z))

13
Compound terms 2 operators
  • 3 kinds of terms
  • constants
  • numbers
  • atoms
  • variable
  • compound terms
  • to simplify writing one can declare operators
  • infix
  • prefix
  • suffix
  • ab (a,b) if is declared as infix
  • -c -(c), if is declared as prefix
  • _xx yy yy(_xx) if yy is declared as suffix

14
Test
  • 3 kinds of terms
  • constants
  • numbers
  • atoms
  • variable
  • compound terms
  • jan
  • Jan
  • -jan
  • _jan
  • jan-willem
  • jan_willem
  • _jan(willem(jan))
  • jan(willem(_jan))
  • (_willem(_jan))
  • _willem _jan

15
Test
  • jan
  • atom
  • Jan
  • variable
  • -jan
  • if - is declared as prefix is equal to (jan)
    (compound), otherwise is wrong
  • _jan
  • variable
  • jan-willem
  • f - is declared as infix is equal to
    (jan,willem), otherwise is wrong
  • jan_willem
  • atom
  • _jan(willem(jan))
  • WRONG (why?)
  • jan(willem(_jan))
  • compound
  • (_willem(_jan))
  • WRONG (why?)
  • _willem _jan
  • if is declared as infix is equal to
    (_willem,_jan), otherwise is wrong.

16
Substitutions
  • Substitution mapping from vars to terms
  • ? X1/t1,...,Xn/tn
  • Dom(?) X1,...,Xn and must be finite
  • Ran(?) Var(t1,...,tn)
  • Var(?) Dom(?) U Ran(?)
  • e.g. ? X/f(Z)
  • h(X,Z) ? h(f(Z),Z)
  • renaming if t1,...,tn is a set of distinct
    variables
  • ground, if t1,...,tn contain no variables

17
Unification
  • Basic computing mechanism
  • two terms t and s UNIFY if there exist a
    substitution ? such that t? s?.
  • there exist an algorithm computing the MOST
    GENERAL UNIFIER
  • ? is more general than ? if ? ? ? for some
    ?
  • ? X/f(a),Z/a is a unifier of t(X) and
    t(f(Z)), but the most general unifier is ?
    X/f(Z)
  • In the tutorial of Endriss, this is called
    matching

18
Unifying in practice
  • In Prolog, t s is a unification test
  • ?- f(X,a) f(b,Y).
  • X b
  • Y a
  • Yes.
  • ?- f(X,a) f(b,X).
  • No.
  • ?- f(X,a) f(b,Y). is a query. (?- might be
    omitted in Eclipse). the full stop at the end is
    important.
  • X b Y a is the computed answer substitution
    (c.a.s.) i.e., the result of the query.
  • a query can FAIL (second example).
  • once a variable is instantiated to a value, it
    cannot be instantiated again
  • ?- Xa, bX.
  • No.
  • In this case , gt and operaror

19
Test compute the c.a.s (if any)
  • _x y.
  • g(X) f(Y).
  • f(Y,Y) f(a,Z).
  • f(Y) f(a,Z).
  • f(Y,Y) f(a,b).
  • f(Z,Z) f(f(a,W),f(Y,b)).
  • X g(Y).
  • Y g(Y).

20
Test compute the c.a.s (if any)
  • _x y.
  • _x y is the cas.
  • g(X) f(Y).
  • fails two terms with a different functor are not
    unifiable
  • f(Y,Y) f(a,Z).
  • Y a, Z a
  • f(Y) f(a,Z).
  • fails two terms cannot be unified if they have
    different arity
  • f(Y,Y) f(a,b).
  • fails, a and b cannot unify
  • f(Z,Z) f(f(a,W),f(Y,b)).
  • Y a
  • W b
  • Z f(a,b)
  • X g(Y)
  • X g(Y)
  • Y g(Y)
  • this unification fails (at least in principle) Y
    cannot be unified with a term containing it (try
    to find the right unifier!). In practice, PROLOG
    interpreters dont carry out the so-called
    occur-check, and this would succeed with cas Y
    g(Y), i.e. Y g(g(g(g(g(g(...))))))

21
Facts
  • Prolog programs contain FACTS and RULES
  • Example of facts (well see the rules later)
  • bigger(elephant, horse).
  • bigger(horse, monkey).
  • We can query them
  • ?- bigger(elephant, horse).
  • Yes
  • ?- bigger(elephant, monkey).
  • No
  • The program doesnt know

22
Computed answer substitutions and multiple answers
  • our program
  • bigger(elephant, horse).
  • bigger(horse, monkey).
  • We can query them
  • ?- bigger(X, horse).
  • X elephant
  • Yes
  • even better
  • ?- bigger(X, Y).
  • X elephant
  • Y horse
  • More?
  • X horse
  • Y monkey
  • peculiar of PROLOG
  • More? yes, but the re might be other answers
  • input from the user I want to see more
    answers (if any).

23
Complex Goals and Left-to-Right execution
  • consider the program
  • friend(a,b).
  • friend(a,c).
  • friend(c,d).
  • friend(d,e).
  • And the query
  • ?- friend(X,Y), friend(Y,Z).
  • What are going to be the results?
  • Xa
  • Yc
  • Zd
  • More?
  • Xc
  • Yd
  • Ze
  • Yes

24
Rules
  • Recall
  • bigger(elephant, horse).
  • bigger(horse, monkey).
  • ?- bigger(elephant, monkey).
  • No
  • The program doesnt know
  • Consider now
  • drinks(john,water).
  • drinks(john, whyskey).
  • drinks(anna,water).
  • drinks(jan,X)- drinks(john,X).
  • What could this mean?
  • - IF
  • drinks(jan,X) is the HEAD
  • drinks(john,X) is the BODY
  • BODIES could be more complex
  • drinks(jan,X)-
  • drinks(john,X), drinks(anna,X).
  • What could this mean?
  • HOMEWORK What happens to the queries
  • ?- drinks(jan,X).
  • ?- drinks(X,Y).
  • ?- drinks(X,Y), drinks(Z,Y).

25
Execution Model
  • given a query A1,...,An
  • looks for a (renaming of a) clause H -
    B1,...,Bm. whose head unifies with A1
  • ? mgu(A1,H).
  • proceeds with the query (B1,...,Bm,A2,...,An) ?.
    (the RESOLVENT)
  • if (B1,...,Bm,A2,...,An) ?. is empty, we are
    finished and the c.a.s. is ?
  • Otherwise
  • if (B1,...,Bm,A2,...,An) ? succeeds with c.a.s.
    ?, then A1,...,An succeeds with c.a.s. ??
  • if (B1,...,Bm,A2,...,An) ? fails then the system
    goes back to () (last choice point) to see if it
    finds another clause G-C1,...,Ck whose head
    unifies with A1
  • if so, it proceeds with (C1,...,Ck,A2,...,An)?,
    where ? mgu(A1,G). Etcetera, in a recursive
    way.
  • if not, A1,...,An fails.

26
Example
  • program
  • a(1).
  • a(X) - c(X).
  • c(2).
  • b(2).
  • query
  • a(Y), b(Y).
  • a(Y),b(Y).
  • resolve with (1), Y 1
  • b(1).
  • no clause whose head match this gt go back to
    the previous query
  • a(Y),b(Y).
  • resolve with (2) YZ
  • c(Z),b(Z).
  • resolve with (3) Z2
  • b(2).
  • succeeds
  • c.a.s (YZ)(Z2) Y2.

27
Need for operators
  • Think of the expressios 32 37
  • In Prolog (((3,2),3),7).
  • Fortunately we can define our own operators.
  • See tutorial, chapter 4 to see how you can define
    extra operators.

28
Arithmetic expressions
  • Try
  • ?- 4 22.
  • No.
  • Why?
  • 4 does not unify with (2,2).
  • to evaluate an arithmetic expression, we use is
  • ?- 4 is 22.
  • Yes
  • ?- X is 22.
  • X 4
  • Yes
  • ?- 2 2 is 4.
  • No
  • is evaluates the rhs only of the expression
  • Operators which perform expression evaluation
    before they are evaluated
  • , gt, gt, lt, \

29
Lists
  • nil the empty list
  • a,b,c contains 3 elements
  • other representations (equivalent)
  • a.b.c.nil
  • .(a, .(b, .(c,)))
  • The first element is the HEAD
  • the rest is the TAIL

30
The BAR
  • may appear before a suffix of the list
  • a,b,c
  • ab,c
  • a,bc
  • a,b,c
  • are the same list
  • Used to split a list
  • ?- a,b,c HT.
  • H a
  • T b,c
  • Yes
  • ?- a,b,c H1,H2T
  • H1 a
  • H2 b
  • T c
  • Yes
  • ?- a,b H1,H2T
  • H1 a
  • H2 b
  • T
  • Yes
  • ?- a H1,H2T
  • No

31
a useful program
  • m(El,El_).
  • m(El,_T)-m(El,T).
  • _ is the anonymous variable it behaves like a
    variable whose name is different than all other
    variables.
  • What are answers to
  • ?- m(a,a,b).
  • ?- m(1,a,b).
  • ?- m(X,a,b).
  • ?- m(X,Y).

32
append
  • append(Xs,Ys,XsYs) -
  • XsYs is the result of concatenating the
  • list Xs and Ys.
  • append(,Ys,Ys).
  • append(XXs,Ys,XZs) - append(Xs,Ys,Zs).
Write a Comment
User Comments (0)
About PowerShow.com