.eclipserc in your
home directory, containing the following ...
%----Silently fail on undefined predicates (like an ATP system) :-set_event_handler(68,fail/0). %----Load some useful libraries :-use_module(library(iso)). :-use_module(library(numbervars)). %----Undefine "not" so it can be used as a user predicate :-op(0,fy,not). %----Use occur check :-set_flag(occur_check,on).
%----Silently fail on undefined predicates (like an ATP system)
:- set_prolog_flag(unknown,fail).
%----Use unification with occurs check
:- set_prolog_flag(occurs_check,true).
%----Use the lists library
:- use_module(library(lists)).
%----Allow code to be listed
:- use_module(library(listing)).
%----Turn on traditional debugging
:- use_module(library(prolog_debug)).
:- use_module(library(edinburgh)).
%----Undefine "not" so it can be used as a user predicate
:- op(0,fy,not).
%----Limit depth and style of printing
:- set_prolog_flag(answer_write_options,
[ quoted(true),
portray(true),
max_depth(100), attributes(portray)
]).
Atomic terms
geoff
'the cat and the rat'
'ABCD'
123
123.456
"the quick brown fox"
Function terms
prerequisite_to(adv_ai)
grade_attained_in(prerequisite_to(adv_ai),pass)
Variables
Who
What
_special
_
geoff_is_a_lecturer
can_study_adv_ai
lecturer(geoff,adv_ai)
lecturer(olivier,prerequisite_to(adv_ai))
has_studied(Everyone,cp1200)
%------------------------------------------------------------------------------
make_tree(Tree):-
read(Word),
insert(Word,Tree). %----Note recursion for looping
%------------------------------------------------------------------------------
insert(exit,_).
insert(Word,Tree):-
insert_word(Word,Tree),
make_tree(Tree).
%------------------------------------------------------------------------------
insert_word(Word,node(Word,_,_)).
insert_word(Word,node(NodeWord,Smaller,_)):-
ordered(Word,NodeWord),
insert_word(Word,Smaller).
insert_word(Word,node(NodeWord,_,Larger)):-
ordered(NodeWord,Word),
insert_word(Word,Larger).
%------------------------------------------------------------------------------
ordered(Big,Small):-
Big @> Small.
%------------------------------------------------------------------------------
geoff_is_a_lecturer.
lecturer(geoff,adv_ai).
lecturer(olivier,prerequisite_to(adv_ai)).
can_study_adv_ai:-
has_studied_ai.
can_study_adv_ai:-
hod_says_so.
will_study_adv_ai:-
can_study_adv_ai,
enrolled_in_adv_ai.
can_study(geoff,adv_ai):-
has_studied(geoff,prerequisite_to(adv_ai)).
can_study(geoff,adv_ai):-
hod_permission_to_study(geoff,adv_ai).
will_study(geoff,adv_ai):-
can_study(geoff,adv_ai),
enrolled(geoff,adv_ai).
?-
prompt, and end with a .
yes or no appropriately.
geoff_is_a_lecturer.
----------------------------------------------------
?- geoff_is_a_lecturer.
yes
?- alan_is_a_lecturer.
no
has_studied_ai.
has_brains_to_pass_adv_ai.
enrolled_in_adv_ai.
can_study_adv_ai:-
has_studied_ai.
will_study_adv_ai:-
can_study_adv_ai,
enrolled_in_adv_ai.
--------------------------------------------------------
?- will_study_adv_ai.
yes
has_studied_ai.
has_brains_to_pass_adv_ai.
enrolled_in_adv_ai.
can_study_adv_ai:-
has_studied_ai.
will_study_adv_ai:-
can_study_adv_ai,
enrolled_in_adv_ai.
--------------------------------------------------------
?- will_study_adv_ai,has_brains_to_pass_adv_ai.
yes
has_studied_ai.
has_brains_to_pass_adv_ai.
can_study_adv_ai:-
has_studied_ai.
will_study_adv_ai:-
can_study_adv_ai,
enrolled_in_adv_ai.
--------------------------------------------------------
?- will_study_adv_ai,has_brains_to_pass_adv_ai.
no
has_studied(geoff,prerequisite_to(adv_ai)).
can_study(geoff,adv_ai):-
has_studied(geoff,prerequisite_to(adv_ai)).
--------------------------------------------------------
?- can_study(geoff,adv_ai).
yes
hod_says_so.
can_study_adv_ai:-
has_studied_ai.
can_study_adv_ai:-
hod_says_so.
--------------------------------------------------------
?- can_study_adv_ai.
yes
has_studied_ai.
has_studied_ai.
hod_says_so.
enrolled_in_adv_ai.
has_brains_to_pass_adv_ai.
can_study_adv_ai:-
has_studied_ai.
can_study_adv_ai:-
hod_says_so.
will_study_adv_ai:-
can_study_adv_ai,
enrolled_in_adv_ai.
--------------------------------------------------------
?- will_study_adv_ai, has_brains_to_pass_adv_ai.
yes ? ;
yes ? ;
yes
has_studied(Everyone,cp1200).
can_study(Anyone,Anything):-
has_studied(Anyone,prerequisite_to(Anything)).
can_study(Anyone,Anything):-
hod_permission_to_study(Anyone,Anything).
Query with unification
lecturer(geoff,ai).
-------------------------------------------------------
?- lecturer(geoff,ai).
yes
?- lecturer(prof,ai).
no
?- lecturer(geoff,networks).
no
yes or no, the bindings of
any variables in the original query are printed by the interpreter.
lecturer(geoff,ai).
--------------------------------------------------------
?- lecturer(geoff,What).
yes
What = ai
?- lecturer(Who,ai).
yes
Who = geoff
?- lecturer(Who,What).
yes
Who = geoff
What = ai
?- lecturer(Same,Same).
no
has_studied(Everyone,programming).
-----------------------------------------------------
?- has_studied(geoff, programming).
yes
?- has_studied(geoff,Anything).
yes
Anything = programming
?- has_studied(Anyone, programming).
yes
Anyone = Everyone_1234
will_study(Student,Course):-
can_study(Student,Course),
has_enrolled(Student,Course).
can_study(Anyone,Anything):-
hod_permission_to_study(Anyone,Anything).
hod_permission_to_study(jane,adv_ai).
has_enrolled(jane,adv_ai).
-----------------------------------------------------
?- can_study(jane,Something).
yes
Something = adv_ai
?- will_study(Anyone,Anything).
yes
Anyone = jane
Anything = adv_ai
Query with variables and backtracking on failure
lecturer(olivier,ai).
lecturer(geoff,ai).
rides_motorcycle(geoff).
-----------------------------------------------------------
?- lecturer(Anyone,ai),rides_motorcycle(Anyone).
yes
Anyone = geoff
has_studied(joe,prerequisite_to(adv_ai)).
hod_permission_to_study(jane,adv_ai).
can_study(Student,Course):-
has_studied(Student,prerequisite_to(Course)).
can_study(Student,Course):-
hod_permission_to_study(Student,Course).
--------------------------------------------------------
?- can_study(jane,Anything).
yes
Anything = adv_ai
lecturer(olivier,ai).
lecturer(geoff,ai).
lecturer(geoff,projects).
--------------------------------------------------------
?- lecturer(geoff,Anything).
yes
Anything = ai ? ;
yes
Anything = projects
lecturer(olivier,ai).
lecturer(john,pascal).
lecturer(olivier,ai).
lecturer(geoff,ai).
lecturer(john,programming).
lecturer(nizam,networks).
lecturer(peter,graphics).
lecturer(geoff,projects).
lecturer(olivier,ppl).
lecturer(john,graphics).
--------------------------------------------------------
?- lecturer(Anyone,Subject),lecturer(Anyone,AnotherSubject).
yes
Anyone = olivier,
Subject = AnotherSubject, AnotherSubject = ai ;
Anyone = olivier,
Subject = AnotherSubject, AnotherSubject = ai ;
Anyone = olivier,
Subject = ai,
AnotherSubject = ppl ;
Anyone = john,
Subject = AnotherSubject, AnotherSubject = pascal ;
Anyone = john,
Subject = pascal,
AnotherSubject = programming ... etc.
has_studied(joe, prerequisite_to(adv_ai)).
hod_permission_to_study(jane,adv_ai).
has_enrolled(jane,adv_ai).
has_brains_to_pass(adv_ai,joe).
can_study(Student,Course):-
has_studied(Student,prerequisite_to(Course)).
can_study(Student,Course):-
hod_permission_to_study(Student,Course).
--------------------------------------------------------
?- can_study(Anyone,adv_ai).
yes
Anyone = joe ;
yes
Anyone = jane
has_studied(fred,apt).
has_studied(Anyone,Prerequisite):-
is_prerequisite(Prerequisite,Anything),
has_studied(Anyone,Anything).
is_prerequisite(adv_ai,apt).
is_prerequisite(pascal,adv_ai).
--------------------------------------------------------
?- has_studied(fred,Course).
yes
Course = apt, ? ;
yes
Course = adv_ai, ? ;
yes
Course = pascal
has_studied clauses are swapped!
Notice the tree like nature of the search for alternative solutions.
lecturer(olivier,ai).
lecturer(john,pascal).
lecturer(olivier,ai).
lecturer(geoff,ai).
lecturer(john,programming).
lecturer(nizam,networks).
lecturer(peter,graphics).
lecturer(geoff,projects).
lecturer(olivier,ppl).
lecturer(john,graphics).
--------------------------------------------------------
?- findall(Lecturer,lecturer(Lecturer,_),AllLecturers).
yes
Lecturer = Lecturer
AllLecturers = [olivier, john, olivier, geoff, john, nizam, peter, geoff, olivier, john]
lecturer(olivier,ai).
lecturer(john,pascal).
lecturer(olivier,ai).
lecturer(geoff,ai).
lecturer(john,programming).
lecturer(nizam,networks).
lecturer(peter,graphics).
lecturer(geoff,projects).
lecturer(olivier,ppl).
lecturer(john,graphics).
--------------------------------------------------------
?- findall(whowhat(Lecturer,Subject),lecturer(Lecturer,Subject),AllLecturers).
yes
Lecturer = Lecturer
Subject = Subject
AllLecturers = [whowhat(olivier, ai), whowhat(john, pascal), whowhat(olivier, ai),
whowhat(geoff, ai), whowhat(john, programming), whowhat(nizam, networks),
whowhat(peter, graphics), whowhat(geoff, projects), whowhat(olivier, ppl),
whowhat(john, graphics)]
/*----This is a comment */ %----This is a comment
%------------------------------------------------------------------------------
make_tree(Tree):-
read(Word),
insert(Word,Tree). %----Note recursion for looping
%------------------------------------------------------------------------------
insert(exit,_).
insert(Word,Tree):-
insert_word(Word,Tree),
make_tree(Tree).
%------------------------------------------------------------------------------
insert_word(Word,node(Word,_,_)).
insert_word(Word,node(NodeWord,Smaller,_)):-
ordered(Word,NodeWord),
insert_word(Word,Smaller).
insert_word(Word,node(NodeWord,_,Larger)):-
ordered(NodeWord,Word),
insert_word(Word,Larger).
%------------------------------------------------------------------------------
ordered(Big,Small):-
Big @> Small.
%------------------------------------------------------------------------------
person(geoff,male,bob,margaret).Here are the queries you should be able to answer ...
?- father(Person,Father).
?- mother(Person,Mother).
?- parent(Person,Parent).
Algorithm : Either a mother or father
?- offspring(Person,Offspring).
Algorithm : The inverse of parent.
?- son(Person,Son).
Algorithm : A son is a male offspring.
?- daughter(Person,Daughter).
Algorithm : A daughter is a female offspring.
?- sibling(Person,Sibling).
Algorithm : Siblings have the same parents.
?- brother(Person,Brother).
Algorithm : A brother is a male sibling.
?- sister(Person,Sister).
Algorithm : A sister is a female sibling.
?- aunt(Person,Aunt).
Algorithm : An aunt is a parent's sister.
?- uncle(Person,Uncle).
Algorithm : An uncle is a parent's brother.
?- nephew(Person,Nephew).
Algorithm : A nephew is a sibling's son.
?- niece(Person,Niece).
Algorithm : A niece is a sibling's daughter.
?- descendant(Person,Descendant).
Algorithm : A descendant is an offspring or a descendant's offspring.
?- ancestor(Person,Ancestor).
Algorithm : The inverse of descendant.
?- relation(Person,Relation).
Algorithm : Descendants of most distant ancestor.
trace turns on tracing
notrace turns off tracing
spy <predicate> places a spy point on the predicate.
nospy <predicate> removes a spy point from the
predicate.
nodebug removes all spy points.
debugging lists what spy points are set.