Download presentation
Presentation is loading. Please wait.
1
LING 388 Language and Computers Lecture 3 9/09/03 Sandiway FONG
2
Administrivia Today Today Computer Lab SBS 224 3 Exercises Each exercise will end with one or more homework questions Due date Next TuesdayNext Tuesday Submit answers to Charles (clin@u.arizona.edu)Submit answers to Charles (clin@u.arizona.edu)clin@u.arizona.edu
3
Administrivia From last class From last class Install SWI-Prolog on your PC http://www.SWI-Prolog.org/ http://www.SWI-Prolog.org/ Problems? See Charles Lin Lecture2 slides Lecture2 slides Download permission problem fixed Slides now available in both PDF and Powerpoint formats
4
Last Class: Prolog Introduced… Introduced… Basic Data Structures Constants Names (begin with lower case letter)Names (begin with lower case letter) NumbersNumbers 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… 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 Start it up from the Program Menu Interpreter accepts queries (?-) Two methods of entering program clauses Two methods of entering program clauses u At the interpreter ?- assert(modal(should)). Note: asserta (add to the beginning),asserta (add to the beginning), assertz (add to the end)assertz (add to the end)
7
Using SWI Prolog Two methods of entering program clauses… Two methods of entering program clauses… Consulting a file from the working directory ?- consult(file). Alternative notation: ?- [file]. - list 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).?- working_directory(X,Y). X - current working directoryX - current working directory Y - new working directoryY - new working directory
8
Prolog Syntax: Care With Spacing Simple guidelines 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: Enter program database: modal(should). modal(could). modal(shall). modal(may). Run queries: Run queries: ?- modal(X). Run negative queries: Run negative queries: ?- \+ modal(be). ?- \+ modal(should). Note: Note: type ; (disjunction) to get Prolog interpreter to look for additional answers
10
Exercise 1: Prolog Queries Enter program facts: 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: 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: Run queries: ?- hasCNeg(X). ?- findall(X,hasCNeg(X),L). ?- hasCNeg(sleep).
11
Exercise 1: Prolog Queries Homework Questions 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).cf. queries ?- modal(be).?- \+ modal(be). Question (B) Modify predicate hasCNeg/1 to block ?- hasCNeg(shall).*shalln’t?- hasCNeg(shall).*shalln’t ?- hasCNeg(may).*mayn’t?- hasCNeg(may).*mayn’t ?- hasCNeg(am). *amn’t?- hasCNeg(am). *amn’t from succeeding Hint: see previous lecture
12
Exercise 2: Building Names Built-in bidirectional predicate: atom_chars/2 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 variableNote: use quotes around capitalized J to avoid intepretation as a variable ?- atom_chars(X,[w,o,n,’’’’,t]). Note: ’’’’ denotes the single quoteNote: ’’’’ denotes the single quote
13
Exercise 2: Building Names Run queries: 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: 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 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 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(A) rejects may mayn’t (B) handles irregular forms can can`t, shall shan`t, will won`t(B) handles irregular forms can can`t, shall shan`t, will won`t
16
Exercise 3: Lists Recall alternative notation (| list separator) 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: Enter program fact: headTail([H|T],H,T). Run queries: 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? How can built-in append/3 be defined? Enter program clauses: 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: 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 Prolog Debugger Use debugger to run program step-by-step ?- debug. turn on debuggerturn on debugger ?- trace. turn on tracingturn on tracing ?- notrace. turns off tracing but stays in the debuggerturns off tracing but stays in the debugger ?- nodebug. turns off debuggerturns off debugger
19
Exercise 3: Lists Homework Question 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.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.