LING 388 Language and Computers Lecture 16 10/23/03 Sandiway FONG.

24
LING 388 Language and Computers Lecture Lecture 16 16 10/23 10/23 /03 /03 Sandiway FONG Sandiway FONG
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of LING 388 Language and Computers Lecture 16 10/23/03 Sandiway FONG.

LING 388Language and Computers

Lecture Lecture 1616

10/2310/23/03/03

Sandiway FONGSandiway FONG

Administrivia

Review

Last time, we discussed how to implement Last time, we discussed how to implement the filter:the filter:

All variables must be bound by an operator (All variables must be bound by an operator (xx)) To block examples like:To block examples like:

*John hit*John hit s(np(john),vp(v(hit),np(s(np(john),vp(v(hit),np(xx))))))

Implementation:Implementation: Based on a tree-walker (disjunctive form) that searched Based on a tree-walker (disjunctive form) that searched

and returned true if a variable np(x) and an and returned true if a variable np(x) and an accompanying operator accompanying operator xx could be found in phrase could be found in phrase structurestructure

Review: Disjunctive Tree-Walkervariable(np(x)).variable(np(x)).variable(X) :- variable(X) :-

X =.. X =.. [F,A1,A2],[F,A1,A2],variable(A1).variable(A1).

variable(X) :- variable(X) :- X =.. X =.. [F,A1,A2],[F,A1,A2],variable(A2).variable(A2).

variable(X) :-variable(X) :-X =.. [F,A],X =.. [F,A],variable(A).variable(A).

Given some tree structure X:

variable(X) holds if there is some np(x) in X

Clause by clause:

1. Matches np(x) (and stops)

2. Recursively calls variable(A1) when X is of form F(A1,A2)

3. Recursively calls variable(A2) when X is of form F(A1,A2)

4. Recursively calls variable(A) when X is of form F(A)

Review: Disjunctive Tree-Walkervariable(np(x)).variable(np(x)).variable(X) :- variable(X) :-

X =.. X =.. [F,A1,A2],[F,A1,A2],variable(A1).variable(A1).

variable(X) :- variable(X) :- X =.. X =.. [F,A1,A2],[F,A1,A2],variable(A2).variable(A2).

variable(X) :-variable(X) :-X =.. [F,A],X =.. [F,A],variable(A).variable(A).

Notes:

1. Every clause for variable/1 except for the first recursively calls variable/1 on a subtree

2. Only way variable/1 can terminate and succeed is if it terminates in the 1st clause

Review: Disjunctive Tree-Walker Worked example for *Worked example for *John hit John hit [[NPNP ee]] Prolog query:Prolog query:

?- variable(s(np(john),vp(v(hit),np(x)))).?- variable(s(np(john),vp(v(hit),np(x)))).

s

np vp

v np

xhit

john

Review: Disjunctive Tree-Walkervariable(np(x)).variable(np(x)).variable(X) :- variable(X) :-

X =.. X =.. [F,A1,A2],[F,A1,A2],variable(A1).variable(A1).

variable(X) :- variable(X) :- X =.. X =.. [F,A1,A2],[F,A1,A2],variable(A2).variable(A2).

variable(X) :-variable(X) :-X =.. [F,A],X =.. [F,A],variable(A).variable(A).

Example of derivation:

?- variable(s(np(john),vp(v(hit),np(x)))).?- variable(s(np(john),vp(v(hit),np(x)))).

?- variable(np(john))?- variable(np(john))

?- variable(john). FAILS?- variable(john). FAILS

?- variable(vp(v(hit),np(x))).?- variable(vp(v(hit),np(x))).

?- variable(v(hit)).?- variable(v(hit)).

?- variable(hit). ?- variable(hit). FAILSFAILS

?- variable(np(x)). ?- variable(np(x)). SUCCEEDSSUCCEEDSNote:Note:

?- variable(s(np(john),vp(v(hit),np(mary)))). FAILS?- variable(s(np(john),vp(v(hit),np(mary)))). FAILS

?-?-variablevariable((np(np(det(the),n(cat)),lambda(x,s(np(i),vp( v(saw),np(np(det(the),n(cat)),lambda(x,s(np(i),vp( v(saw),np(x)))))). np(x)))))). SUCCEEDSSUCCEEDS

Review: Disjunctive Tree-Walker

Operator/1 is a Operator/1 is a xx, i.e. lambda(x,_), in the phrase structure, i.e. lambda(x,_), in the phrase structure

operator(lambda(x,_)).operator(lambda(x,_)).operator(X) :- operator(X) :-

X =.. [F,A1,A2],X =.. [F,A1,A2],operator(A1).operator(A1).

operator(X) :- operator(X) :- X =.. [F,A1,A2],X =.. [F,A1,A2],operator(A2).operator(A2).

operator(X) :-operator(X) :-X =.. [F,A],X =.. [F,A],operator(A).operator(A).

ExamplesExamples::

•?- operator(s(np(john),vp(v(hit),np(x)))).?- operator(s(np(john),vp(v(hit),np(x)))).•NoNo

•?- operator(s(np(john),vp(v(hit),np(mary)))).?- operator(s(np(john),vp(v(hit),np(mary)))).•NoNo

•?-operator?-operator((np(np(det(the),n(cat)),np(np(det(the),n(cat)),lambda(x,s(np(i),vp(v(saw),np(x)))))).lambda(x,s(np(i),vp(v(saw),np(x)))))).

•YesYes

Review: Filter Implementation

Filter:Filter: All variables must be bound by an operator (All variables must be bound by an operator (xx))

Implementation (Implementation (Initial tryInitial try)):: Using predicates:Using predicates:

variable(X)variable(X) np(x) np(x) finderfinder operator(X)operator(X) lambda(x, … ) lambda(x, … ) finderfinder

Define:Define: filter(X) :- variable(X) -> operator(X).filter(X) :- variable(X) -> operator(X). To be read as:To be read as:

• filter(X) filter(X) holds ifholds if operator(X) operator(X) holds whenholds when variable(X) variable(X) holdsholds Note: -> is the Prolog built-in “if … then” constructNote: -> is the Prolog built-in “if … then” construct

Review: Filter Implementation UseUse::

?- s(X,Sentence,[]), filter(X).?- s(X,Sentence,[]), filter(X). XX is the phrase structure returned by the DCG is the phrase structure returned by the DCG SentenceSentence is the input sentence encoded as a list is the input sentence encoded as a list

Problem with definition:Problem with definition: filter(X) :- variable(X) -> operator(X).filter(X) :- variable(X) -> operator(X). Semantics:Semantics:

Holds if there is Holds if there is somesome variable that has an accompanying operator variable that has an accompanying operator Question: Question:

What happens if we have two variables only one of which is properly What happens if we have two variables only one of which is properly bound?bound?

ExampleExample: : filter(X) filter(X) fails to exclude …fails to exclude … *the cat that I saw hit*the cat that I saw hit *the cat that I saw [ *the cat that I saw [NPNP e] hit [ e] hit [NPNP e] e]

s(np(np(det(the),n(cat)),s(np(np(det(the),n(cat)),lambdalambda(x,s(np(i),vp(v(saw),(x,s(np(i),vp(v(saw),np(x)np(x))))),vp(v(hit),)))),vp(v(hit),np(x)np(x)))))

?

Filter Implementation: Part 2

A better (i.e. A better (i.e. more faithfulmore faithful) implementation…) implementation… When we find np(x), we check nodes dominating np(x) for signs of When we find np(x), we check nodes dominating np(x) for signs of xx

If we find lambda, we know If we find lambda, we know np(x)np(x) is bound by that operator is bound by that operator

Implementation Question:Implementation Question: We know predicate variable/1 finds np(x)We know predicate variable/1 finds np(x) How do we modify it to report when it finds lambda as well? How do we modify it to report when it finds lambda as well?

lambda(x, … )

np(x)

s

lambda

x

variable/1 true

Filter Implementation: Part 2 IdeaIdea::

Modify predicate variable/1 to produce a sign of some sort if it sees a Modify predicate variable/1 to produce a sign of some sort if it sees a lambda on the way down to np(x) lambda on the way down to np(x)

Note: if lambda doesn’t dominate np(x), we don’t want a signNote: if lambda doesn’t dominate np(x), we don’t want a sign Use the extra argument mechanism from previous lectures for feature Use the extra argument mechanism from previous lectures for feature

passing (person/number agreement)passing (person/number agreement) We’ll pass an arbitrary feature, call it We’ll pass an arbitrary feature, call it ff, if we see an appropriate , if we see an appropriate

lambda lambda

lambda(x, … )

np(x)

s

lambda

x

variable(X,F) true

F unbound

F = f

F = f

Filter Implementation: Part 2 variable(np(x)).variable(np(x)). variable(X) :- variable(X) :-

X =.. [F,A1,A2],X =.. [F,A1,A2], variable(A1).variable(A1).

variable(X) :- variable(X) :- X =.. [F,A1,A2],X =.. [F,A1,A2], variable(A2).variable(A2).

variable(X) :-variable(X) :- X =.. [F,A],X =.. [F,A], variable(A). variable(A).

variable(np(x),variable(np(x),__).). variable(X,variable(X,FF) :- ) :-

X =.. [_,A1,A2],X =.. [_,A1,A2], variable(A1,variable(A1,FF).).

variable(X,variable(X,FF) :- ) :- X =.. [_,A1,A2],X =.. [_,A1,A2], variable(A2,variable(A2,FF).).

variable(X,variable(X,FF) :-) :- X =.. [_,A],X =.. [_,A], variable(A,variable(A,FF). ).

• We need to modify variable/1 so that we can pass information indicating the presence or absence of lambda as we traverse the tree

• We do this by first adding a 2nd argument F to variable/1

Filter Implementation: Part 2 variable(np(x),_).variable(np(x),_). variable(lambda(x,Y),f) :-variable(lambda(x,Y),f) :-

variable(Y,_).variable(Y,_). variable(X,F) :- variable(X,F) :-

X =.. [_,A1,A2],X =.. [_,A1,A2], variable(A1,F).variable(A1,F).

variable(X,F) :- variable(X,F) :- X =.. [_,A1,A2],X =.. [_,A1,A2], variable(A2,F).variable(A2,F).

variable(X,F) :-variable(X,F) :- X =.. [_,A],X =.. [_,A], variable(A,F). variable(A,F). • step 2: add a clause to detect and

report the presence of lambda

variable(np(x),_).variable(np(x),_). variable(X,F) :- variable(X,F) :-

X =.. [_,A1,A2],X =.. [_,A1,A2], variable(A1,F).variable(A1,F).

variable(X,F) :- variable(X,F) :- X =.. [_,A1,A2],X =.. [_,A1,A2], variable(A2,F).variable(A2,F).

variable(X,F) :-variable(X,F) :- X =.. [_,A],X =.. [_,A], variable(A,F). variable(A,F).

Filter Implementation: Part 2 variable(np(x),_).variable(np(x),_). variable(lambda(x,Y),f) :-variable(lambda(x,Y),f) :-

variable(Y,_).variable(Y,_). variable(X,F) :- variable(X,F) :-

X =.. [_,A1,A2],X =.. [_,A1,A2], variable(A1,F).variable(A1,F).

variable(X,F) :- variable(X,F) :- X =.. [_,A1,A2],X =.. [_,A1,A2], variable(A2,F).variable(A2,F).

variable(X,F) :-variable(X,F) :- X =.. [_,A],X =.. [_,A], variable(A,F). variable(A,F).

Notes:

1. Every clause for variable/2 except for the first one recursively calls variable/2 on a subtree

• Only way variable/2 can succeed is if it terminates in the 1st clause

2. When variable/2 holds, we can inspect the 2nd argument to see if along the way it found a lambda expression as well

• This is signaled by the f feature

• The 2nd clause exclusively sets the 2nd argument

Filter Implementation: Part 2

np(x)

s

lambda

x

lambda

x

variable(np(x),_).variable(np(x),_). variable(lambda(x,Y),f) :-variable(lambda(x,Y),f) :-

variable(Y,variable(Y,__).). variable(X,F) :- variable(X,F) :-

X =.. [_,A1,A2],X =.. [_,A1,A2], variable(A1,F).variable(A1,F).

variable(X,F) :- variable(X,F) :- X =.. [_,A1,A2],X =.. [_,A1,A2], variable(A2,F).variable(A2,F).

variable(X,F) :-variable(X,F) :- X =.. [_,A],X =.. [_,A], variable(A,F). variable(A,F).

Underscore (don’t care variable) because we’re not interested in reporting multiple lambdas dominating np(x)

sets 2nd argument to be f

sets 2nd argument to be f

Filter Implementation: Part 2 variable(np(x),_).variable(np(x),_). variable(lambda(x,Y),f) :-variable(lambda(x,Y),f) :-

variable(Y,_).variable(Y,_). variable(X,F) :- variable(X,F) :-

X =.. [Y,A1,A2],X =.. [Y,A1,A2], \+ Y = lambda,\+ Y = lambda, variable(A1,F).variable(A1,F).

variable(X,F) :- variable(X,F) :- X =.. [Y,A1,A2],X =.. [Y,A1,A2], \+ Y = lambda,\+ Y = lambda, variable(A2,F).variable(A2,F).

variable(X,F) :-variable(X,F) :- X =.. [_,A],X =.. [_,A], variable(A,F). variable(A,F).

Since clause 2 must set f exclusively, we must prevent lambda from matching these two clauses

Filter Implementation: Part 2

Use:Use: filter(X) :- variable(X,F), F == f.filter(X) :- variable(X,F), F == f.

Note:Note: Filter/1 holds if variable/2 holds and the value reported Filter/1 holds if variable/2 holds and the value reported

in the 2nd argument F is identical to fin the 2nd argument F is identical to f We use identity (==) instead of unification (=) because We use identity (==) instead of unification (=) because

we’re testing whether F has been bound to a valuewe’re testing whether F has been bound to a value

Identity (==)

==== is a Prolog built-in taking two argumentsis a Prolog built-in taking two arguments compares but does not perform unificationcompares but does not perform unification

Note:Note:• = attempts to unify its arguments= attempts to unify its arguments• Hence, neither of the clausesHence, neither of the clauses

– filter(X) :- variable(X,F), F = f.filter(X) :- variable(X,F), F = f.– filter(X) :- variable(X,f). filter(X) :- variable(X,f). will work for our purposeswill work for our purposes

Examples:Examples: ?- abc == abc.?- abc == abc. YesYes ?- abc = abc.?- abc = abc. YesYes ?- X == abc.?- X == abc. NoNo ?- X = abc.?- X = abc. YesYes ?- X = abc, X == abc.?- X = abc, X == abc. YesYes ?- X = abc, X = abc. Yes?- X = abc, X = abc. Yes ?- X == Y.?- X == Y. NoNo ?- X == Y.?- X == Y. YesYes

Filter Implementation: Part 3

Definition:Definition: filter(X) :- variable(X,F), F == f.filter(X) :- variable(X,F), F == f.

Is still not quite right …Is still not quite right … filter(X) holds if there exists one valid variable/lambda pair in filter(X) holds if there exists one valid variable/lambda pair in XX

However …However … This fails to block the case where there may be more than one This fails to block the case where there may be more than one

variable, only one of which has a valid accompanying lambda variable, only one of which has a valid accompanying lambda expression, as in:expression, as in:

*the cat that I saw hit*the cat that I saw hit s(np(np(det(the),n(cat)),s(np(np(det(the),n(cat)),lambdalambda(x,s(np(i),vp(v(saw),(x,s(np(i),vp(v(saw),np(x)np(x))))),)))),

vp(v(hit),vp(v(hit),np(x)np(x)))))In other words, variable/1 is true for the whole expression because it In other words, variable/1 is true for the whole expression because it

finds the lambda/np(x) pairfinds the lambda/np(x) pair

Filter Implementation: Part 3

Solution:Solution: Want filter/1 to hold if there are Want filter/1 to hold if there are nono configurations for which configurations for which

variable(X,F) holds and variable(X,F) holds and FF remains unbound remains unbound New Definition:New Definition:

filter(X) :- \+ ( variable(X,F), var(F) ). filter(X) :- \+ ( variable(X,F), var(F) ). to be read as:to be read as: filter(X) filter(X) holds if it’s not the case that whenholds if it’s not the case that when variable(X,F) variable(X,F)

succeeds andsucceeds and F F is uninstantiatedis uninstantiated

Note:Note: var(F) is a Prolog built-in that holds if F is uninstantiated, i.e. a var(F) is a Prolog built-in that holds if F is uninstantiated, i.e. a

Prolog variableProlog variable

var/1

var(X)var(X) is a Prolog built-in that takes one argumentis a Prolog built-in that takes one argument holds if X is a variable at the time of the call to var/1holds if X is a variable at the time of the call to var/1

Examples:Examples: ?- var(X).?- var(X). YesYes ?- var(f).?- var(f). NoNo ?- X=f, var(X).?- X=f, var(X). NoNo ?- var(X), X = 2.?- var(X), X = 2. YesYes

… … fact that X is no longer a variable after unifying fact that X is no longer a variable after unifying with 2 doesn’t change the semantics of var/1with 2 doesn’t change the semantics of var/1

Summary: Filter Implementation

Summarizing:Summarizing: filter(X) :- \+ ( variable(X,F), var(F) ) implements filter:filter(X) :- \+ ( variable(X,F), var(F) ) implements filter: All variables must be bound by an operatorAll variables must be bound by an operator ( (xx))

variable(X,F)variable(X,F) is a disjunctive tree-walker that looks for np(x)is a disjunctive tree-walker that looks for np(x) also looks for lambda(x,Y) where also looks for lambda(x,Y) where YY contains a variable np(x) contains a variable np(x) returns f as the value of returns f as the value of FF if it finds the lambda above if it finds the lambda above

variable(X,F), var(F)variable(X,F), var(F) will succeed if there is an np(x) without an accompanying lambda will succeed if there is an np(x) without an accompanying lambda

expressionexpression

\+ ( variable(X,F), var(F) )\+ ( variable(X,F), var(F) ) will succeed if there aren’t any np(x)s without an accompanying lambda will succeed if there aren’t any np(x)s without an accompanying lambda

expressionexpression

Next Time …

We’ll look at how to implement the other We’ll look at how to implement the other constraint mentioned in Lecture 14:constraint mentioned in Lecture 14: All variables must be bound by an operator (All variables must be bound by an operator (xx)) Operator (Operator (xx) can only bind one variable) can only bind one variable