Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 LING 438/538 Computational Linguistics Sandiway Fong Lecture 4: 8/31.

Similar presentations


Presentation on theme: "1 LING 438/538 Computational Linguistics Sandiway Fong Lecture 4: 8/31."— Presentation transcript:

1 1 LING 438/538 Computational Linguistics Sandiway Fong Lecture 4: 8/31

2 2 Administrivia Homework 1 is due next Tuesday –please attempt it early as possible I’m glad to have seen questions about the homework already –today’s lecture will also help we’ll do more on base/recursive definitions –also check out the LING 388 class slides there is more detail there... that may help By request –slides are now numbered

3 3 So far... our introduction to Prolog Concepts –database DB (assert facts, closed world assumption) –transitivity (Prolog rule :-) –recursion, also left recursion –variables –lists (comma-delimited, head/tail, infinite list) –computation tree (matching the DB)

4 4 Today’s Topics Negation More practice on recursive definitions

5 5 Prolog Negation Prolog negation –“failure to prove” –is limited form of logical negation –denoted by \+ –used in queries and the bodies (not head) of rules only negative facts are not permitted –e.g. you can’t add a negative fact (using assert ) to the database ?- assert(\+ medal(aluminum)). ERROR: assert/1: No permission to modify static_procedure `(\+)/1'

6 6 Prolog Negation [From lecture 2] Facts: medal(gold). medal(silver). medal(bronze). Negated database query: ?- \+ medal(silver). No ?- \+ medal(aluminum). Yes Computation tree: –?- \+ medal(silver). –true if medal(silver) false ?- medal(silver). –Yes –medal(silver) true, –so \+ medal(silver) false Computation tree: –?- \+ medal(aluminum). true if medal(aluminum) false ?- medal( aluminum ). –No –medal(aluminum) false, –so \+ medal(aluminum) true SWI-Prolog trace: ?- trace. Yes [trace] ?- \+ medal(silver). Call: (8) medal(silver) ? creep Exit: (8) medal(silver) ? creep No SWI-Prolog trace: ?- trace. Yes [trace] ?- \+ medal(aluminum). Call: (8) medal(aluminum) ? creep Fail: (8) medal(aluminum) ? creep Yes

7 7 Prolog Negation [From lecture 2] Facts: medal(gold). medal(silver). medal(bronze). Negated database query: ?- \+ medal(X). No –does not give a value for X Computation tree: –?- \+ medal(X). – true if medal(X) false ?- medal(X). –X = gold – medal(X) true so \+ medal(X) false SWI-Prolog trace: ?- trace. Yes [trace] ?- \+ medal(X). Call: (8) medal(_G312) ? creep Exit: (8) medal(gold) ? creep No

8 8 Prolog Negation [From lecture 2] Facts: medal(gold). medal(silver). medal(bronze). Negated database query: ?- \+ \+ medal(X). Yes –does not give a value for X Computation tree: –?- \+ \+ medal(X). –true if \+ medal(X) false ?- \+ medal(X). true if medal(X) false –?- medal(X). »X = gold –medal(X) true, so \+ medal(X) false –\+ medal(X) false, so \+ \+ medal(X) true SWI-Prolog trace: ?- trace. Yes [trace] ?- \+ \+ medal(X). Call: (8) medal(_G312) ? creep Exit: (8) medal(gold) ? creep Yes

9 9 Prolog Negation We can write a complex query like: –?- \+ \+ medal(X), X = aluminum. –Yes –and the query will succeed because the variable X is not bound (i.e. set) by the sub- query ?- \+ \+ medal(X). However, the similar-looking query: ?- X = aluminum, \+ \+ medal(X). No returns the very different answer No because X = aluminum sets the value of X and the sub-query ?- \ + \+ medal(aluminum). –returns No

10 10 Prolog Negation Prolog negation in rules Example –Modal auxiliary verbs in English generally have contracted negative counterparts shouldshouldn’t wouldwouldn’t couldcouldn’t may*mayn’t –[* indicates ungrammaticality]

11 11 Prolog Negation write this rule in Prolog –modal auxiliary verbs in English generally have contracted negative counterparts Database modal(should). “should is a modal” modal(would). “would is a modal” modal(could). “could is a modal” modal(may). “may is a modal” Rule hasCNeg(X) :- modal(X). Rule with exception hasCNeg(X) :- modal(X), \+ X = may. Notes –, is to be read as “and” (conjunction) –\+ is in the body (not the head) of a rule – that’s allowed –but we can’t write something like: \+ hasCNeg(X) :- modal(X), X = may. –where the negation is in the head of the rule

12 12 Prolog Negation Rule with exception hasCNeg(X) :- modal(X), \+ X = may. Query ?- hasCNeg(X).X = should ;X = would ;X = could ;No Computation tree ?- hasCNeg(X). –?- modal(X). –?- \+ X = may. ?- modal(X).(subquery will be retried) –X = should ?- \+ should = may. –Yes

13 13 Prolog Rules and Negation Database modal(should). modal(could). modal(would). modal(may). hasCNeg(X) :- modal(X), \+ X = may. hasCNeg(X) modal(X)\+ X=may modal(should)\+ should=may true modal(could)\+ could=maymodal(would)\+ would=maymodal(may)\+ may=may false powerpoint animation

14 14 Recursive Definitions

15 15 More infinity Set of natural numbers –N = [1,2,3,4,...] Rules –nn(1). “ 1 is a natural number ” –nn(N) :- nn(M), N is M+1. Notes –recursive definition Query –?- nn(X). what happens? “M is a natural number then M+1 is a natural number” SWI-Prolog built-in computes the arithmetic expression on the right side of “ is ” and matches it with the variable on the left

16 16 List Reversal Task –reversing a list –e.g. [1,2,3] => [3,2,1] Specification –rev(L1,L2) –L1 and L2 lists –L2 is L1 reversed Base Case –rev([],[]). Recursive Case –rev([H|T],L) :- rev(T,L2), app(L2,[H],L). Example –?- rev([1,2,3],L) ?- rev([2,3],L2) L2 = [3,2] ?- app([3,2],[1],L) L = [3,2,1]

17 17 List Reversal Accumulating Parameter version –reverse([],L,L). (base case) –reverse([H|T],L,L2) :- reverse(T,[H|L],L2). (recursive case) Usage –reverse(L1,[],L2). –[] is the initial value of the accumulating parameters –accumulating parameter gets progressively instantiated (or filled out) Examples –?- reverse([1,2,3],[],L2). –?- reverse(L1,[], [1,2,3]). does it work?


Download ppt "1 LING 438/538 Computational Linguistics Sandiway Fong Lecture 4: 8/31."

Similar presentations


Ads by Google