190 likes | 260 Views
LING 388 Language and Computers. Lecture 3 9/09/03 Sandiway FONG. Administrivia. Today Computer Lab SBS 224 3 Exercises Each exercise will end with one or more homework questions Due date Next Tuesday Submit answers to Charles ( clin@u.arizona.edu ). Administrivia. From last class
E N D
LING 388Language and Computers Lecture 3 9/09/03 Sandiway FONG
Administrivia • Today • Computer Lab SBS 224 • 3 Exercises • Each exercise will end with one or more homework questions • Due date • Next Tuesday • Submit answers to Charles (clin@u.arizona.edu)
Administrivia • From last class • Install SWI-Prolog on your PC • http://www.SWI-Prolog.org/ • Problems? • See Charles Lin • Lecture2 slides • Download permission problem fixed • Slides now available in both PDF and Powerpoint formats
Last Class: Prolog • Introduced… • Basic Data Structures • Constants • Names (begin with lower case letter) • Numbers • Variables (begin with an upper case letter) • Complex Data Structures • Lists (delimited by square brackets) • Structures - functor/argument(s)
Last Class: Prolog • Also introduced… • Clauses • Facts - Structures + ‘.’ • Rules - head :- body + ‘.’ • Disjunction (;) • Unification (=) - matching mechanism • Negation (\+) • Queries • Facts prefixed by ?-
Using SWI Prolog • Start it up from the Program Menu • Interpreter accepts queries (?-) • Two methods of entering program clauses • At the interpreter • ?- assert(modal(should)). • Note: • asserta (add to the beginning), • assertz (add to the end)
Using SWI Prolog • Two methods of entering program clauses… • Consulting a file from the working directory • ?- consult(file). • Alternative notation: • ?- [file]. - list notation Note: file should be the filename without the usual .pl extension Note: the file must be reloaded if the contents have been changed • How to find out what the working directory is • ?- working_directory(X,Y). • X - current working directory • Y - new working directory
Prolog Syntax: Care With Spacing • Simple guidelines • Omit spaces between predicate/functor name and “(“, • Example: * pred (X) • Omit spaces before period at the end of a clause, • Example: * verb(run) . • Spaces usually ok in other situations, • Example: headTail( [ H | T ] , H , T ). • Need a space following end period if another clause follows on the same line, • Example: * a(1).a(2).
Exercise 1: Prolog Queries • Enter program database: • modal(should). modal(could). modal(shall). modal(may). • Run queries: • ?- modal(X). • Run negative queries: • ?- \+ modal(be). • ?- \+ modal(should). • Note: • type ; (disjunction) to get Prolog interpreter to look for additional answers
Exercise 1: Prolog Queries • Enter program facts: • aux(am). aux(are). aux(is). aux(was). aux(were). • aux(do). aux(does). aux(did). • Enter program rules for predicate hasCNeg/1: “has contracted negated form” e.g. could - couldn’t, does - doesn’t • hasCNeg(X) :- modal(X). • hasCNeg(X) :- aux(X). • Run queries: • ?- hasCNeg(X). • ?- findall(X,hasCNeg(X),L). • ?- hasCNeg(sleep).
Exercise 1: Prolog Queries • Homework Questions • Question (A) • What does the query ?- \+ modal(X) return? • Explain why it doesn’t return X = be • cf. queries ?- modal(be). ?- \+ modal(be). • Question (B) • Modify predicate hasCNeg/1 to block • ?- hasCNeg(shall). *shalln’t • ?- hasCNeg(may). *mayn’t • ?- hasCNeg(am). *amn’t from succeeding • Hint: see previous lecture
Exercise 2: Building Names • Built-in bidirectional predicate: atom_chars/2 • takes names apart • ?- atom_chars(will,X). • builds names from characters • ?- atom_chars(X,[‘J’,o,h,n]). • Note: use quotes around capitalized J to avoid intepretation as a variable • ?- atom_chars(X,[w,o,n,’’’’,t]). • Note: ’’’’ denotes the single quote
Exercise 2: Building Names • 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]). • Note: • atom_chars/2 is an example of a built-in predicate that has multiple modes of usage - very versatile • in other programming languages, you’d need a variety of functions • e.g. atom_to_chars, chars_to_atom, check_atom_with_chars and more…
Exercise 2: Building Names • Another versatile built-in predicate (in SWI-Prolog) with multiple modes of usage: append/1 • append(L1,L2,L3) concatenates lists L1 and L2 to form list L3 • Run 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 the anonymous variable, no binding will be reported by the interpreter
Exercise 2: Building Names • Homework question • Use both built-ins atom_chars/2 and append/3 to write a general rule • addNT/2 “add n’t” defined as follows: • addNT(X,Y) converts between a modal or auxiliary verb X and its contracted negative counterpart Y • Examples: could <-> couldn’t, is <-> isn’t • Make sure it • (A) rejects may <-> mayn’t • (B) handles irregular forms can <-> can`t, shall <-> shan`t, will <-> won`t
Exercise 3: Lists • Recall alternative notation (| list separator) • [1|[2|[3]]] = [1,2,3] • or [1|[2,3]] - mixed form • Generally, in [H|T] H=head of list, T=tail of list • Enter program fact: • headTail([H|T],H,T). • Run queries: • ?- headTail([1,2,3],X,Y). • ?- headTail([],X,Y). • ?- headTail(X,1,[2]). • ?- headTail(X,[1],[2,3]).
Exercise 3: Lists • How can built-in append/3 be defined? • Enter program clauses: • app([],L,L). • app([H|T],L,[H|U]) :- app(T,L,U). • Note: append/3 is taken (built-in), so we use app/3 • Re-run queries: • ?- app([1],[2,3],X). • ?- app(X,Y,[1,2]). • ?- app(_,[X],[1,2,3]). • ?- app(X,Y,Z).
Exercise 3: Lists • Prolog Debugger • Use debugger to run program step-by-step • ?- debug. • turn on debugger • ?- trace. • turn on tracing • ?- notrace. • turns off tracing but stays in the debugger • ?- nodebug. • turns off debugger
Exercise 3: Lists • Homework Question • (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.