CSC 270 Survey of Programming Languages Prolog Lecture

  • Slides: 24
Download presentation
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof

CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search

Unification • From Knowledge Base 4: woman(mia). woman(jody). woman(yolanda). … • Querying Knowledge Base

Unification • From Knowledge Base 4: woman(mia). woman(jody). woman(yolanda). … • Querying Knowledge Base 4: 1 ? - woman(X). X = mia. 2 ? - • Prolog unifies woman(X) with woman(mia) , thereby instantiating the variable X to mia

Types in Prolog – A Quick Review • Recall that there are three types

Types in Prolog – A Quick Review • Recall that there are three types of term: – Constants. These can either be atoms (such as vincent ) or numbers (such as 24 ). – Variables. (Such as X , Z 3 , and List. ) – Complex terms. These have the form: – functor(term_1, . . . , term_n).

Unification – A Definition • Two terms unify: – they are the same term

Unification – A Definition • Two terms unify: – they are the same term or – if they contain variables that can be uniformly instantiated with terms in such a way that the resulting terms are equal.

Unification – An Example • This means that – mia and mia unify, because

Unification – An Example • This means that – mia and mia unify, because they are the same atom. – 42 and 42 unify, because they are the same number. – X and X unify, because they are the same variable. – woman(mia) and woman(mia) unify, because they are the same complex term.

Unification – An Example (continued) • This means that – woman(mia) and woman(vincent) do

Unification – An Example (continued) • This means that – woman(mia) and woman(vincent) do not unify, because they are not the same (and neither of them contains a variable that could be instantiated to make them the same).

Unification and Instantiation • mia and X are not the same, but X can

Unification and Instantiation • mia and X are not the same, but X can be instantiated to mia which makes them equal. Therefore, mia and X unify. • woman(X) and woman(mia) unify, because they can be made equal by instantiating X to mia. • loves(vincent, X) and loves(X, mia) do not unify because it is impossible to find an instantiation of X that makes the two terms equal.

A Formal Definition of Unification • If term 1 and term 2 are constants,

A Formal Definition of Unification • If term 1 and term 2 are constants, then term 1 and term 2 unify if and only if they are the same atom, or the same number.

A Formal Definition of Unification (continued) • If term 1 is a variable and

A Formal Definition of Unification (continued) • If term 1 is a variable and term 2 is any type of term, then term 1 and term 2 unify, and term 1 is instantiated to term 2. • If term 2 is a variable and term 1 is any type of term, then term 1 and term 2 unify, and term 2 is instantiated to term 1. • If they are both variables, they’re both instantiated to each other, and we say that they share values.

A Formal Definition of Unification (continued) • If term 1 and term 2 are

A Formal Definition of Unification (continued) • If term 1 and term 2 are complex terms, then they unify if and only if: – They have the same functor and arity, and – all their corresponding arguments unify, and – the variable instantiations are compatible.

A Formal Definition of Unification (continued) • E. g. , it’s impossible to instantiate

A Formal Definition of Unification (continued) • E. g. , it’s impossible to instantiate variable X to mia when unifying one pair of arguments, and to instantiate X to vincent when unifying another pair of arguments. ) • Two terms unify if and only if it follows from the previous three clauses that they unify.

Unification – Some Examples 1 ? - =(mia, mia). true. 2 ? - =(2,

Unification – Some Examples 1 ? - =(mia, mia). true. 2 ? - =(2, 2). true. 3 ? - mia = mia. true. 4 ? - 2 = 2. true. 5 ? - mia = vincent. false. 6 ? - 'mia' = mia. true. 7 ? - '2' = 2. false. 8 ? -

8 ? - mia = X. X = mia. 9 ? - mia =

8 ? - mia = X. X = mia. 9 ? - mia = X. X = mia. 10 ? - X = Y. 11 ? - X = _5067. 12 ? - Y = _5067. 13 ? - X = mia, X = vincent. false.

Unification – Another Example 2 ? - k(s(g), Y) = k(X, t(k)). Y =

Unification – Another Example 2 ? - k(s(g), Y) = k(X, t(k)). Y = t(k), X = s(g). 3 ? - • We use clause 3 because we are trying to unify two complex terms. Do both complex terms have the same functor and arity? Yes. • So do the first arguments, s(g) and X , unify? By clause 2, yes. • So do the second arguments, Y and t(k) , unify? By clause 2, yes.

Unification – Other Examples 3 ? - k(s(g), t(k)) = k(X, t(Y)). X =

Unification – Other Examples 3 ? - k(s(g), t(k)) = k(X, t(Y)). X = s(g), Y = k. 4 ? - loves(X, X) = loves(marcellus, mia). false. 5 ? - • These terms do not unify because while they are both complex terms and have the same functor and arity, all corresponding arguments do not unify in this example

Prolog and Unification Algorithm • Prolog does not use a standard unification algorithm when

Prolog and Unification Algorithm • Prolog does not use a standard unification algorithm when it performs its version of unification; it takes a shortcut. You need to know about this shortcut. • Consider the following query: ? - father(X) = X.

Prolog and Unification Algorithm (continued) • Do these terms unify or not? No •

Prolog and Unification Algorithm (continued) • Do these terms unify or not? No • Why is that? Pick any term and instantiate X to the term you picked. father(father(butch))) = father(butch)) and so on.

Prolog and Unification Algorithm (continued) • Prolog unification won’t spot the problem and halt.

Prolog and Unification Algorithm (continued) • Prolog unification won’t spot the problem and halt. Having instantiated X to father(X) , Prolog is committed to carrying out an unending sequence of expansions and will run until it runs out of memory.

Prolog and Unification Algorithm (continued) • There actually 3 responses to “does father(X) unify

Prolog and Unification Algorithm (continued) • There actually 3 responses to “does father(X) unify with X ”. – Standard unification algorithm (no) – Older Prolog implementations (run amok until they use up the available memory) – Sophisticated Prolog implementations (yes) • In short, there is no ‘right’ answer to this question.

occurs check • A standard algorithm given two terms to unify carries out what

occurs check • A standard algorithm given two terms to unify carries out what is known as the occurs check. • This means that if it is asked to unify a variable with a term, it first checks whether the variable occurs in the term. • If it does, the standard algorithm declares that unification is impossible, for clearly it is the presence of the variable X in father(X) which leads to problems. • Only if the variable does not occur in the term do standard algorithms attempt to carry out the unification.

Prolog and Unification Algorithm (continued) • Prolog comes with a built-in predicate that carries

Prolog and Unification Algorithm (continued) • Prolog comes with a built-in predicate that carries out standard unification (that is, unification with the occurs check). The predicate is unify_with_occurs_check/2. • So if we posed the query ? - unify_with_occurs_check (father(X), X). we would get the response no.

Unification Proof Search f(a). f(b). g(a). g(b). h(b). k(X): - f(X), g(X), h(X). •

Unification Proof Search f(a). f(b). g(a). g(b). h(b). k(X): - f(X), g(X), h(X). • try to match f (_1), g(_1), h(_1) • When f(a), then _1 = a. • Does it find f(a), g(a), h(a) – nope • When f(b) then _1 = b. • Does it find f(b), g(b), h(b) – yes so it stops and says true. • See with trace • Stop trace with notrace

Crossword puzzle match • • • • word(astante, a, s, t, a, n, t,

Crossword puzzle match • • • • word(astante, a, s, t, a, n, t, e). word(astoria, a, s, t, o, r, i, a). word(baratto, b, a, r, a, t, t, o). word(cobalto, c, o, b, a, l, t, o). word(pistola, p, i, s, t, o, l, a). word(statale, s, t, a, l, e). crossword(X, Y, Z, A, B, C) : word(X, _, X 1, _, X 2, _, X 3, _), word(Y, _, Y 1, _, Y 2, _, Y 3, _), word(Z, _, Z 1, _, Z 2, _, Z 3, _), word(C, _, X 3, _, Y 3, _, Z 3, _), word(B, _, X 2, _, Y 2, _, Z 2, _), word(A, _, X 1, _, Y 1, _, Z 1, _).

Summary • Prolog proves by instantiating variables and then checking truth • Stops at

Summary • Prolog proves by instantiating variables and then checking truth • Stops at first complete matching truth • Will continue finding match when you press ; • Trace using trace / notrace • Can go into infinite checking unless you call with unify_with_occurs_check