










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











LEVELS OF KNOWLEDGE-BASED SYSTEM
Defines capabilities of an intelligent system formalizing representation language
Facts, Rules, and Queries
Open Swi-Prolog window
File-New-Type the facts and rules with full stops at the end
Add facts to database
Save- close file -backtoSwiprolog- File-Consult-choose file-open
To add more facts and rules, eg.
File- Edit – Choose File – type new rule to indicate relation mother
mother(X,Y) :- parent(Y,X) , female(X).
Save-Close file
Consult, then write a Query to: List who is my mother See if ayah is my mother? List how many mothers in the database?
father_of(Father,Child) mother_of(Mother,Child) grandparent_of(Grandparent,Child) sister_of(Sister,Person) aunt_of(Aunt,Person) grandchild_of(Grandchild,Child)
ancestor( Old, Young ) :- parent( Old, Young ). ancestor( Old, Young ) :- parent( Old, Middle ), ancestor( Middle, Young ).
When we want to write recursive programs, we need to think about two things: 1. How will the program terminate? 2. How will the program break up the data it works on?
Recursion is an example of a divide-and-conquer Strategy Note that we normally put the base case first, so that Prolog tests it first! To ensure that a program terminates, we must have at least one base case – a non-recursive clause We must also ensure that something gets (in some sense) ”reduced” each time a recursive step happens, so that we can say when we have got to the end
Example – testing if a term is a list: – The base case is when we have an empty list – the smallest list possible – The recursive case breaks down a non-empty list into a head and a tail and then tests the tail, so the thing being tested gets smaller each time.
ancestor( Old, Young ) :- parent( Old, Young ). ancestor( Old, Young ) :- parent( Old, Middle ), ancestor( Middle, Young ).
Base case
Recursive- clause
recursive predicate definitions http://www.coli.uni-saarland.de/~kris/esslli04prolog/slides/0.day2.pdf
Task: Define a predicate ancestor of (X,Y) which is true if X is an ancestor of Y.
Exercise the recursive predicate ancestor using your
family tree, add the rule above, then make queries:
?- ancestor (my,X).
?- ancestor (X,my).
?- ancestor (X, mum).
USE TRACE FACILITY TO DISPLAY RECURSION
ancestor( X, Y):- parent( X,Y ). ancestor( X, Z ):- parent( X,Z ), ancestor( Z,Y ).