Sponsored Links
This presentation is the property of its rightful owner.
1 / 14

LING 388: Language and Computers PowerPoint PPT Presentation

LING 388: Language and Computers. Sandiway Fong Lecture 6: 9/7. Administrivia. Homework 2 out today usual rules due next Thursday by midnight. Building and Taking Atoms Apart built-in predicate atom_chars/2 has two modes of usage atom_chars( symbol , list ) takes names apart

Download Presentation

LING 388: Language and Computers

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

LING 388: Language and Computers

Sandiway Fong

Lecture 6: 9/7

Administrivia

• Homework 2

• out today

• usual rules

• due next Thursday by midnight

Building and Taking Atoms Apart

built-in predicate atom_chars/2 has two modes of usage

atom_chars(symbol,list)

takes names apart

?- atom_chars(will,X).

X=[w,i,l,l]

builds names from a list of characters

?- atom_chars(X,[’J’,o,h,n]).

X=‘John’

Building and Taking Lists Apart

append/3 is a built-in predicate in SWI-Prolog defined as follows:

append(L1,L2,L3) is true

if list L3 is the linear concatenation of lists L1 and L2

concatenate two lists together

?- append([1],[2,3],X).

X = [1,2,3]

split a list up into two parts

?- append(X,Y,[1,2]).

X=[] Y=[1,2]

X=[1] Y=[2]

X=[1,2] Y=[]

Last Time

Recursive Definition

Base Case

Recursive Case

[member/2 is already taken in SWI-Prolog, let’s us mem/2.]

Let’s define mem/2 such that

?- mem(List,X). is true if X is a member of List

Examples:

?- mem([1,2,3],2). true

?- mem([1,2,3],4). false

Exercise 1

Let’s define mem/2 such that

?- mem(List,X). is true if X is a member of List

Examples:

?- mem([1,2,3],2). true

?- mem([1,2,3],4). false

Recursive Definition

Base Case

mem([X|_],X).

X is a member of a list is X is the head of that list

(tail = _ )

Recursive Case

mem([_|L],X):- mem(L,X).

X is a member of a list [_|L] if X is a member of its tail L

Exercise 1

Put the definition of mem/2 in a file and load it into Prolog:

mem([X|_],X).

mem([_|L],X):- mem(L,X).

test to see if it’s loaded properly using

?- listing.

Run the following queries:

?- mem([1,2,3],2).

?- mem([],2).

?- mem([1,2,3],X).

what answer(s) does this last query give?

Exercise 1

(6pts) Give the complete (i.e. all answers) step-by-step computation tree for

?- mem([1,2,3],X).

given the database

mem([X|_],X).

mem([_|L],X):- mem(L,X).

Hint: using

?- trace.

will help but you will need to list out the matches and variable binding at each step

see Lecture 5 slides for app/3 to see what format you should use

Homework Question 1

Modal auxiliary verbs in English generally have contracted negative counterparts

shouldshouldn’t

wouldwouldn’t

couldcouldn’t

may*mayn’t

[* indicates ungrammaticality]

Let’s write a rule to form the contracted negated form

call the predicate addNT/2

?- addNT(Word,WordNT).

Word = symbol

WordNT = symbol with n’t suffixed

Examples:

?- addNT(should,X).

X = ’shouldn\’t’

Exercise 2

We can use atom_chars/2

atom_chars(symbol,list)

takes names apart

?- atom_chars(will,X).

X=[w,i,l,l]

builds names from a list of characters

?- atom_chars(X,[’J’,o,h,n]).

X=‘John’

Definition (rule):

addNT(W,Wnt) :-

atom_chars(W,L),

append(L,[n,’\’’,t],Lnt),

atom_chars(Wnt,Lnt).

add this rule to the database

?- listing.

addNT(A, C) :-

atom_chars(A, B),

append(B, [n, '\'', t], D),

atom_chars(C, D).

run queries

?- addNT(should,X).

?- addNT(would,X).

?- addNT(john,X).

Exercise 2

Assume database includes facts:

modal(should). “should is a modal”

modal(would). “would is a modal”

modal(could). “could is a modal”

modal(may). “may is a modal”

(4pts) Modify the definition of addNT/2 to accept only modals

Demonstrate your program works correctly for:

?- addNT(should,X).

?- addNT(would,X).

?- addNT(john,X).

Submit both your program and queries as your answer

put everything together,

not in separate files!

Homework Question 2

(4pts) Further modify your definition of addNT/2 to exclude the ungrammatical case:

i.e.

?- addNT(may,X).

No

shouldshouldn’t

wouldwouldn’t

couldcouldn’t

may*mayn’t

Homework Question 2

(6pts) Extra Credit Question

Notice that the following query doesn’t work:

?- addNT(X,'shouldn\'t').

ERROR: atom_chars/2: Arguments are not sufficiently instantiated

Write the corresponding “subtract n’t” rule, call it subNT/2, for removing the n’t suffix:

?- addNT(X,'shouldn\'t').

X = should

Homework Question 2

Let’s define a recursive predicate for reversing lists

Examples:

[1,2][2,1]

[1,2,3] [3,2,1]

[1][1]

[][]

Definition

reverse([],[]).

reverse([X|L],R) :-

reverse(L,LR),

append(LR,[X],R).

Exercise 3

• Run queries

?- reverse([1,2,3],X).

?- reverse(X,[1,2,3]).

what happens when you ask for more answers?

(6pts) Define a predicate pallindrome/1

that is true when a word can be spelt the same forwards or backwards

Examples:

radar

redivider

abba

Demonstrate your program works on queries like:

?- pallindrome(radar).

Yes

?- pallindrome(dog).

No

Homework Question 3

Hint: use atom_chars and reverse