1 / 19

LING 388 Language and Computers

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

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LING 388Language and Computers Lecture 3 9/09/03 Sandiway FONG

  2. 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)

  3. 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

  4. 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)

  5. Last Class: Prolog • Also introduced… • Clauses • Facts - Structures + ‘.’ • Rules - head :- body + ‘.’ • Disjunction (;) • Unification (=) - matching mechanism • Negation (\+) • Queries • Facts prefixed by ?-

  6. 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)

  7. 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

  8. 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).

  9. 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

  10. 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).

  11. 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

  12. 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

  13. 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…

  14. 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

  15. 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

  16. 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]).

  17. 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).

  18. 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

  19. 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.

More Related