haihongyuan.com

# Language and Computers

ComputersLING 388: Language and

Slides on Homepage?Courses

?LING 388

?LING 388 Homework #1

–handed out today

–arrive in my mailbox

?by midnight Thursday August 31st

–see lecture 1 slides for general policy about write-ups–you should collect all your answers to the homework exercises into one file

?put your name on the homework

?use Microsoft Word or plain text (something I can easily read)?Adobe PDF is also acceptable

Today’s Topic

?first hands-on experience using SWI-Prolog

–SWI-Prolog is already installed on all the lab machines–

Today’s Topic

?Gentle hands-on introduction to Prolog–a powerful tool for writing grammars–grammars that can also be run on a computer

Some Background

?Prolog = Programming in Logic

–Horn clause logic (subset of first-order predicate calculus)–roots in Mechanical Theorem Proving

?Resolution Rule (Robinson, 65)

–invented in the early 1970s

–designed to support natural language processing

?… has grammar rules

–has been taught to schoolkids

Prolog is a Database

?

Prolog can be used to store a list of facts

–facts are things that are true in Prolog’s world

initially Prolog’s world is empty

?

We can look up facts just like in a database

mechanism is a Prolog databasequery

?Usage

–We can store facts in the database by using assert

e.g. assert(bird(tweety)).?adds fact bird(tweety).to the Prolog world

Example?Facts in the database

bird(tweety). “tweety is a bird”

bird(woody). “woody is a bird”

?Database queries

?-bird(tweety).Yes?-bird(daffy).

No

?

Jargon

–birdis a predicate

–the predicate birdhas one argument–e.g. tweety and woodyare arguments of the predicate bird–bird(tweety)is a fact

?

meaning bird(tweety)is true in the database

?-bird(tweety). is a query

?

meaning we want to know if bird(tweety)is true in the database

SWI-Prolog

how to start it?

–from the Windows Program

–interpreter window pops up and –is ready to accept database queries (?-)

how to see what’s in the Prolog database?

–?-listing.Other useful commands –once we start storing facts in files instead of entering them using assert.how to see what the current working directory is?–(the working directory is where your files are stored)–important: every machine in

the lab is different

–?-working_directory(X,Y).

–X: current working

directory, Y: new working

directory

how to change to a new working directory?

–?-working_directory(X,NEW).?????

Exercise 1

Let’s start Prolog

Enter the bird facts into the

database

using assert

Query the databaseList the database

–using listing.:all facts and queries must end

in a period.Prolog is case-sensitive

–don’t mix upper and lower

case

use lower case for symbols

Facts in the database

bird(tweety). bird(woody).

Database queries

?-bird(tweety).?-bird(daffy).

To remove a fact from the databaseuse retract

(the counterpart of assert)

e.g. ?-retract(bird(tweety)).would leave just woody

1.?????Note??

Exercise 2: Variables

?Database

–bird(tweety).–bird(woody).

?Query containing a variable X

–?-bird(X).–X = tweety;–X = woody

;–

No

?Query with a variable

–?-bird(X).

?

means

tell me if bird(X).is true in Prolog’s world.

?Closed World Assumption

–In general, Prolog obeys the closed world assumption: things aren’t true unless explicitly stated to be true

but unless we explicitly tell Prolog daffy is a bird, Prolog assumes it’s not.

?

Notation: variables and symbols

–X(capitalized 1st letter) is a variable–

tweety, woody(beginning with a

lowercase letter) are simple symbols(not variables)

Exercise 3: Negation–“failure to prove”

–a limited form of logical negation denoted by \+

–used in queries and bodies (not

–negative facts are not permitted,

can’t put them in the database

–can’t do

?-assert(\+ bird(mickey)).

ERROR: assert/1: No permission

to modify static_procedure?Example?-\+ bird(tweety).No?-\+ bird(mickey).Yes?Reasoning?\+ bird(tweety)is true –if bird(tweety)is false,?i.e. can’t be proved from the database–but bird(tweety)is true–so \+ bird(tweety)is false?\+ bird(mickey)is true –if bird(mickey)is false, i.e. can’t be proved from the database–bird(mickey)is not in the database, so

it’s false

–so \+ bird(mickey)is true Prolog negation

Homework Question

?Use database

bird(tweety).“tweety is a bird”

bird(woody).“woody is a bird”

?(2pts) What do the following

?The queries

?-\+ \+ bird(tweety).

?-\+ bird(X).(6pts) Give the logical reasoning for each answer –(in the form given in Exercise 3)?

Exercise 4: Rules

?Database

bird(tweety).

bird(woody).“tweety is a bird”“woody is a bird”

?Rules (to be asserted)

has_feathers(Y) :-bird(Y).“Y has feathers if Y is a bird”can_fly(X) :-has_feathers(X).“X can fly if X has feathers”

Run Queries

–?-has_feathers(tweety).

?if bird(tweety) is trueYes

–?-can_fly(tweety).

??-has_feathers(tweety).

–?-bird(tweety).

?bird(tweety) trueYes(can chain inferences)

Notation

–:-means “if”

SWI-Prolog

?Getting stuck?

–Type <control>-C–Type a(for abort)

SWI-Prolog

?How to enter facts and rules into the database?

–Method 2:

?Create a file in the current working directory containing

database facts and rules.

?Load the file at the Prolog prompt using:

??-consult(FILE).

?or

??-[FILE ]. file are loaded from –(comma-delimited list notation)Prolog’s working directory?or

see slide 7

for working directory

queries

SWI-Prolog

?How to enter facts and rules into the database?

–consult/edit/new under theFile

SWI-Prolog

?A note on filenames

–Convention: Prolog files normally have extension .pl

?e.g. mydb.pl

?(.pl is also used by Perl)

–FILEabove should be the filename without the extension

?e.g. ?-[mydb].

–The period (.) is a special symbol in Prolog.

If you wish to specify the full name, you must enclose it in single quotes

?e.g.?-[’mydb.pl’].