180 likes | 288 Views
LING 388: Language and Computers. Sandiway Fong Lecture 4: 9/1. Administrivia. LING 388 Homework #1 handed out today submit homework by email sandiway@email.arizona.edu due date is one week from now Wednesday September 8th (by midnight) Reminder No class on Monday (Labor Day)
E N D
LING 388: Language and Computers Sandiway Fong Lecture 4: 9/1
Administrivia • LING 388 Homework #1 • handed out today • submit homework by email • sandiway@email.arizona.edu • due date is one week from now • Wednesday September 8th (by midnight) • Reminder • No class on Monday (Labor Day) • Resume next Wednesday (Harvill 208)
Today’s Topic • Backup lecture material with an introduction to SWI-Prolog • www.swi-prolog.org • manuals available online • SWI-Prolog is already installed on all the lab machines
SWI-Prolog • How to start it? • from the Windows Program menu • interpreter window pops up and is ready to accept database queries (?-) • How to see what’s in the database? • ?- listing. • How to see what the current working directory is? • (the working directory is where your files are stored) • ?- working_directory(X,Y). • X: current working directory, Y: new working directory • How to change to a new working directory? • ?- working_directory(X,NEW).
SWI-Prolog • How to enter facts and rules into the database? • Method 1: At the interpreter prompt • ?- assert(medal(gold)). • will add to the database the fact: • medal(gold). • Remember that Prolog cares about the order of facts in the database: • ?- asserta(medal(gold)). • adds medal(gold). to the front • ?- assertz(medal(gold)). • adds medal(gold). to the end
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 using: • ?- consult(FILE). • or • ?- [FILE ]. • (comma-delimited list notation)
SWI-Prolog • A note on filenames • Convention: Prolog files normally have extension .pl • e.g. mydb.pl • (.pl is also used by Perl) • FILE above 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’].
SWI-Prolog • Stepping through the computation tree • By default, Prolog just returns answers without showing its work • Turn on tracing mode for the Prolog debugger • ?- trace. • Run queries as before • Hit the return key to step (“creep”) through the computation • e.g. • ?- a(X). • 1 1 Call: a(_430) ?RETURN • 1 1 Exit: a(1) ?RETURN • X = 1 ?; • 1 1 Redo: a(1) ?RETURN • Type h(help) instead of RETURN to see other options • Turn off Prolog debugger • ?- nodebug.
Exercise 1a: Prolog Queries • Create database • modal(should). • modal(could). • modal(shall). • modal(may). • Load the database into Prolog • Run queries • ?- modal(X). • use ; to get all answers • ?- \+ modal(be). • Note Prolog doesn’t give you the opportunity to use ; to get more answers. Why? • ?- findall(X,modal(X),L). • What does findall/3 do? /3 means “has three arguments”
Exercise 1b: Prolog Queries • Modify the database to include facts • aux(am). • aux(are). • aux(is). • aux(was). • aux(were). • aux(do). • aux(does). • aux(did). • and the rules for hasCNeg/1 implementing • English modals and auxiliaries have a contracted negative form • hasCNeg(X) :- modal(X). • hasCNeg(X) :- aux(X).
Exercise 1b: Prolog Queries • Verify the operation of hasCNeg/1 by running queries • ?- hasCNeg(X). • ?- findall(X,hasCNeg(X),L). • ?- hasCNeg(sleep). • Homework Question (3pts) • Modify predicate hasCNeg/1 to block • ?- hasCNeg(shall).*shalln’t • ?- hasCNeg(may).*mayn’t • ?- hasCNeg(am).*amn’t • Hint: see previous lecture • (Submit both your definition and results of queries)
Exercise 2a: Building and Taking Names Apart • Built-in predicate atom_chars/2 has two modes of usage • takes names apart • ?- atom_chars(will,X). • X is the list of characters representing will • builds names from a list of characters • ?- atom_chars(X,[’J’,o,h,n]). • use quotes around capitalized J to avoid intepretation as a variable • ?- atom_chars(X,[w,o,n,’’’’,t]). • ’’’’ denotes the single quote
Exercise 2a: Building and Taking Names Apart • Run queries • ?- atom_chars(has,[h,a,s]). • ?- atom_chars(will,[w,X,l,l]). • ?- atom_chars(X,Y). • ?- atom_chars(X,[J,o,h,n]). • What happens in the last two cases?
Exercise 2b: Building and Taking Names Apart • append/3 is a built-in predicate in SWI-Prolog defined as follows: • append(L1,L2,L3) holds if list L3 is the linear concatenation of lists L1 and L2 • append/3 has multiple modes of usage • Run example queries: • ?- append([1],[2,3],X). • ?- append(X,Y,[1,2]). • ?- append(_,[X],[1,2,3]). • ?- append(X,Y,Z). • Note: • the underscore character ‘_’ is a special variable • no binding will be reported by the interpreter for underscores
Exercise 2b: Building and Taking Names Apart • Homework Question (6pts) • Use both atom_chars/2 and append/3 to define a new rule • addNT/2 such that: • addNT(X,Y) converts between a modal or auxiliary verb X and its contracted negative counterpart Y • Examples: • ?- addNT(could,’couldn’’t’). • ?- addNT(is,’isn’’t’). • Make sure it • (A) rejects may <-> mayn’t • (B) handles irregular forms • can <-> can`t, shall <-> shan`t, will <-> won`t • (Submit both your definition and results of relevant queries)
Exercise 3: Computation Tree • append/3 can be defined recursively as follows: • app([],L2,L2). Base case • app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case • append/3 is already defined (built-in), so we use the predicate name app/3 to avoid a naming clash • Re-run queries for append/3 with app/3 to see that app/3 has the same behavior: • ?- app([1],[2,3],X). • ?- app(X,Y,[1,2]). • ?- app(_,[X],[1,2,3]). • ?- app(X,Y,Z). • Make use of the tracing facility to convince yourself that the definition is correct
Exercise 3: Computation Tree • Homework Question (3pts) • (A) How many inference steps does it take to run the following query: • ?- app([1,2,3],[4],L). • (B) How many inference steps does it take to run the following query: • ?- app([1],[2,3,4],L). • (C) Explain why the number of steps differ despite the fact both queries return the same result. • For inference steps: count the number of CALLs
Summary • 3 homework questions • 12 pts on offer