Title: PROLOG
1PROLOG
2COMPOUND QUERIES
- Small knowledge base for the in-class exercises.
- PROLOG works in exactly the same fashion with a
large knowledge base, but you would not be able
to identify every relationship due to the size. - With a large knowledge base, PROLOG allows you to
uncover relationships that you couldnt identify
manually.
3IN-CLASS PROBLEM
- Using the problem from last week
- Add the relationships according to the revised
organization chart (handout).
4NEW RELATIONSHIPS
5PRACTICE QUERY
- Query PROLOG to find someone who reports to both
- the vice president of engineering, and
- the director of research.
6PROLOG QUERY
7HOW PROLOG SOLVES QUERIES
- PROLOGs method of solving queries is fairly
"brute force via the following steps - work from left to right
- take the first literal (up to first ,)
- find a match in the knowledge base
- mark the the location of the match
- the solution to the first literal becomes a
constraint for solving the rest of the query - attempt to solve the rest of the query
8BACKTRACKING
- PROLOGs procedure to back up when a search path
fails. - e.g., if the second half of the query does not
find a match - Recall that PROLOG placed a marker at the
location of the match on the first predicate.
9BACKTRACKING PROCEDURE
- If a search through the entire knowledge base
fails to find a match on the second half of the
query, BACKTRACK to the marker. - Erase the "binding" of the variable to that
value. - Move forward through the knowledge base to the
next match with the first part of the query. - Repeat the search attempting to match the second
part of the query.
10TO FIND ALL SOLUTIONS
- Expand the backtracking process.
- When a complete match is found, "undo" that match
(backtrack to the last marker and remove it). - Move forward to try to solve again.
- Moves forward from the earlier match so it will
look for matches in the rest of the knowledge
base. - When it fails (e.g., no more matches), backtrack
further to the match of an earlier predicate
repeat the search.
11MORE ON BACKTRACKING
- Prolog uses "strict chronological backtracking".
- When it cannot solve the latest match (2nd, 3rd,
4th, whatever) - "unbinds" only the most recent solution
- moves forward to find the next solution
- If, after searching the entire knowledge base, a
solution is not found, PROLOG backtracks further,
"unbinding" the next most previous variable.
12MULTIPLE VARIABLES
- Use the same logic for multiple variables as for
single variables. - add new predicates to the data base
- aggravates(secretary, sales_secretary).
- aggravates(secretary, eng_secretary).
- aggravates(secretary, exec_vp).
- Is there someone who reports to the president who
aggravates others in the organization?
13QUERY
14TRACE
- ALS-PROLOG has a trace capability
- Allows you to examine which predicates are
executed and where system fails - tools --gt debugger
- window should pop up
- Preface each predicate with the word TRACE and
predicate calls will be shown in debugging window - Tip manually clear or insert spaces between
executions
15QUERY with TRACE
- trace reports_to(Someone,president), trace
aggravates(Someone,Others). - Result
- Someonesecretary
- Otherssales_secretary
- Someonesecretary
- Otherseng_secretary
- Someonesecretary
- Othersexecutive_vp
- no.
16TRACE RESULTS FIRST MATCH
- (1) 1 call userreports_to(_6665,president)
- (1) 1 exit userreports_to(executive_vp,president
) - (1) 1 call useraggravates(executive_vp,_6671)
- (1) 1 fail useraggravates(executive_vp,_6671)
- (1) 1 redo userreports_to(executive_vp,president
) - (1) 1 exit userreports_to(vp_manufact,president)
- (1) 1 call useraggravates(vp_manufact,_6671)
- (1) 1 fail useraggravates(vp_manufact,_6671)
- (1) 1 redo userreports_to(vp_manufact,president)
- (1) 1 exit userreports_to(vp_engineering,preside
nt) - (1) 1 call useraggravates(vp_engineering,_6671)
- (1) 1 fail useraggravates(vp_engineering,_6671)
- (1) 1 redo userreports_to(vp_engineering,preside
nt) - (1) 1 exit userreports_to(treasurer,president)
- (1) 1 call useraggravates(treasurer,_6671)
- (1) 1 fail useraggravates(treasurer,_6671)
- (1) 1 redo userreports_to(treasurer,president)
- (1) 1 exit userreports_to(secretary,president)
- (1) 1 call useraggravates(secretary,_6671)
17REMAINDER OF TRACE
- (1) 1 exit useraggravates(secretary,eng_secretar
y) - (1) 1 redo useraggravates(secretary,eng_secretar
y) - (1) 1 exit useraggravates(secretary,executive_vp
) - (1) 1 redo useraggravates(secretary,executive_vp
) - (1) 1 fail useraggravates(secretary,_6671)
- (1) 1 redo userreports_to(secretary,president)
- (1) 1 exit userreports_to(vp_sales,president)
- (1) 1 call useraggravates(vp_sales,_6671)
- (1) 1 fail useraggravates(vp_sales,_6671)
- (1) 1 redo userreports_to(vp_sales,president)
- (1) 1 fail userreports_to(_6665,president)
18DIFFERENCES
- To ensure that unique solutions are found, it is
sometimes necessary to constrain the solution so
that two variables are not assigned the same
value. - \ is used to indicate "not equal".
19QUERIES AND RULES
- Instead of typing complicated queries, we can
build Rules. - The format for rules is as follows
- head - body.
- - is interpreted as "lt---"
- "If the body is true, then the head is true "
- Once the rule is added to the knowledge base, it
can be executed.
20EXAMPLE RULE
- Write a rule to define co-workers
- Where co-workers are
- Two people who work for the same person
21CO-WORKERS RULE
22PERSONNEL PROBLEMS RULE
- How about a rule to define personnel problems?
- Let's say you have a personnel problem when two
people are co-workers and one of them aggravates
the other. - Hint use existing rule!
23PERSONNEL PROBLEM PREDICATE
24HOW PROLOG EXECUTES A SAVED RULE
- PROLOG substitutes the body of the rule for the
head, and solves as it would any compound query. - see p. 72 of text for a summary of how rules
work. - Use TRACE, note you can creep one predicate at
a time, or leap to the next solution.
25MULTIPLE VERSIONS OF A RULE
- Frequently you will define multiple versions of
the same rule. - PROLOG will search the knowledge base, starting
from the top and attempting to solve all versions
of the rule. - If any version of the rule is true (there exist
values to satisfy all variables in that
particular version of the rule), then the rule
is true. - Used to implement OR.
26EXAMPLE OF MULTIPLE VERSIONS OF A RULE
- Using the organization chart, what if different
kind of personnel problems exists? - For instance what if an employee aggravates
their boss? - Define this as a rule, also a personnel problem
(i.e., use the same name as your last rule).
27NEW TYPE OF PERSONNEL PROBLEM
28HOW PROLOG WORKS
- Refer also to 4.4 in the text.
- Figure 4-11 - this is the simple version.
- A goal is empty when there is nothing to be done
to solve it. - Figure 4-12 - includes chronological
backtracking. - Note extra loop. If more rules for predicate,
apply those rules. - If no more rules for the predicate, check for
previous choices, if any exist, undo those
choices (backtrack) to find other possible
solutions.
29CYCLIC ACTION OF PROLOG
Start Submit Query
Is Goal Empty?
Yes
Halt with Success
No
More Rules for Predicate?
No
Halt with Failure
Yes
Apply (next) rule for Predicate
30CYCLE with BACKTRACKING
Start Submit Query
Is Goal Empty?
Yes
Halt with Success
No
Previous DB Choices?
More Rules for Predicate?
No
No
Halt with Failure
Yes
Yes
Apply (next) rule for Predicate
Undo effects of application of most recently
chosen rule from the database
31UNIFICATION
- Unification is the process of finding successful
matches. Prolog works by "unifying" expressions.
- Figure 4-19 in text shows unification algorithm
- Figure 4-20 shows examples - work through, make
sure you understand these examples. - Be aware that
- No such thing as a GLOBAL variable.
- Scope is limited usually just that instance of
the predicate.
32ARITHMETIC
- See pages 192-194
- Usual arithmetic operators, functions, and
comparisons are available.